Reconstruction of the internal problem matrix

Hello to all of you FreeFem users,

Thank you for your help in advance.

Acoording to the documentation, in a problem, the keyword init controls the reconstruction of the internal problem matrix. If init is set to false or 0, the matrix is reconstructed at each problem calls (or after a mesh modification), else the previously constructed matrix is used.

Typically, I set the value of init to be1 to reduce the reconstruction cost. However, if the matrix depends on a parameter that needs to be changed at some steps, the matrix must also be modified to solve the problem correctly. With the init set to 1, the only method I know to update the matrix is to remesh the domain, which is inefficient. I am wondering if there is another method to update the matrix in this problem.

Below is the code that shows the matrix doesn’t change unless I remesh the domain.

mesh Th = square(10,10);
fespace Vh(Th, P2);
Vh w,u=1.0;
real dl=1., dr=1.;
problem pbU (u,w, init=1, solver=LU)
= int2d(Th)(
(dx(u)*dx(w)+dy(u)*dy(w))*dl)
-int2d(Th)(1*w*dr)
+ on(1, 2, 3, 4, u=0);

pbU;
plot(u,wait=0, fill=1, value=1,aspectratio=0, WindowIndex=0 );
dr=10.;
pbU;
plot(u,wait=0, fill=1, value=1,aspectratio=0,WindowIndex=1 );
dl=10.;
pbU;
plot(u,wait=0, fill=1, value=1,aspectratio=0, WindowIndex=2);
Th = square(10,10);
pbU;
plot(u,wait=0, fill=1, value=1,aspectratio=0, WindowIndex=3);

Hello,

In FreeFem, when you set init=1, it reuses the previously constructed matrix, which indeed saves the reconstruction cost. However, as you pointed out, if the matrix depends on a parameter that needs to change, you need to update the matrix accordingly. Remeshing the domain is one way to force the update, but it’s inefficient.

To address your problem without remeshing, you can modify the parameter that affects the matrix and manually reconstruct the matrix by resetting init. One approach is to redefine the problem when the parameter changes. Here’s how you can do it:

mesh Th = square(10,10);
fespace Vh(Th, P2);
Vh w, u=1.0;
real dl=1., dr=1.;

// Define a function to set up and solve the problem
func problemSetup(real dl, real dr) {
    problem pbU (u, w, init=0, solver=LU)
    = int2d(Th)(
    (dx(u)*dx(w) + dy(u)*dy(w)) * dl)
    - int2d(Th)(1 * w * dr)
    + on(1, 2, 3, 4, u=0);
    
    pbU;
    plot(u, wait=0, fill=1, value=1, aspectratio=0);
}

// Initial solve
problemSetup(dl, dr);

// Change parameter dr and solve again
dr = 10.;
problemSetup(dl, dr);

// Change parameter dl and solve again
dl = 10.;
problemSetup(dl, dr);

// Optionally remesh and solve again if needed
Th = square(10,10);
problemSetup(dl, dr);

In this code, we define a function problemSetup that constructs and solves the problem with the current values of dl and dr. When you need to change these parameters, you call problemSetup with the new values, ensuring that the matrix is reconstructed correctly each time. Also check this : Out of Bound problem after refining a 3D meshlooker

This approach avoids remeshing and efficiently updates the matrix according to the changed parameters.

I hope this helps!

Thanks a lot for providing suggestion! I have two questions. First, I try to run the code you provide, but get the error:

    7 : func problemSetup(real dl, real dr)
 Error line number 7, in file E:\test.edp, before  token )
No Argument in line function
  current line = 7
Compile error : No Argument in line function
        line number :7, )
error Compile error : No Argument in line function
        line number :7, )

I don’t understand what is the problem that causes this error, could you help me with that?

Second, since init=0 in the code, I think it will not reuses the previously constructed matrix. Then how can I modify the code so that if I don’t change the parameter, the matrix in previous step will be reused?

1 Like