Save results in Mixed FEM

Dear Friends,
I want to know how I can save values of a primal variable from a mixed fem formulation, for example:

mesh Th=square(3,3);
fespace Vh(Th, [P1,P1]);
Vh [u, v];

func real initialize() {
real value1 = 1.0, value2 = -1.0;
if( x>=0.25 && x<=0.75 && y>=0.25 && y<=0.75 ) return value1;
else return value2;
}
[u, v] = [initialize(), 0.5];

/* Print results */
for(int i=0; i<Vh.ndof; ++i)
cout << " u = "<< u[][i] << endl;

Result:
Square mesh : nb vertices =16 , nb triangles = 18 , nb boundary edges 12
u = -1
u = 0.5
u = -1
u = 0.5
u = -1
u = 0.5
u = -1
u = 0.5
u = -1
u = 0.5
u = 1
u = 0.5
u = 1
u = 0.5
u = -1
u = 0.5
u = -1
u = 0.5
u = 1
u = 0.5
u = 1
u = 0.5
u = -1
u = 0.5
u = -1
u = 0.5
u = -1
u = 0.5
u = -1
u = 0.5
u = -1
u = 0.5
times: compile 0.026953s, execution 0.003083s, mpirank:0
CodeAlloc : nb ptr 3463, size :466120 mpirank: 0
Ok: Normal End

The result shows the output of the two variables, but how can I save only the variable of interest “u”

Just make a scalar P1 finite element space and save the component of interest as a scalar function.

You can note that the values for u are stored on even indices in u[] while those for v on odd indices in u[].


mesh Th=square(20,20);

fespace Vh2(Th,[P1,P1]);
fespace Vh(Th,P1);

Vh2 [u,v];
Vh w;
[u,v]= [sqrt(x^2+y^2),sqrt((x-0.5)^2)];
plot(u,fill=1,cmm=“u”,wait=1);

w = u;
plot(w,fill=1,cmm=“w”,wait=1);
w = v;
plot(w,fill=1,cmm=“w”,wait=1);

// or notice how the variables are stored in u[]
[u,v] = [0,1];

for(int i=0;i<Vh2.ndof;i++){
cout << u[][i] << endl;
}

Thanks Beniamin, this works for the tests I want to perform.

Best regards,