Hello,
I have a FreeFEM simulation that works fine with a P2 fespace but not with a P1 one.
I wish to export it using pyfreefem for further manipulation and visualisation in a python environment.
Unfortunately, pyfreefem only supports P0 and P1 fespaces.
Is there a way to convert results obtained in a P2 fespace into a P1 one (with the best approximation) so that it can be exported?
Thanks.
Georges.
What I usually do is to just evaluate it on a grid u(na,mb) and you can use a very fine resolution.
Also I have become fond of 1D starting points as its a lot easier to understand when things
don’t make sense
You can also assign one to the other
mesh Th=square(10,10);
mesh Th2=square(5,5);
fespace Vh(Th,P1);
fespace Vh2(Th2,P2);
Vh u=x;
Vh2 u2=u;
real v=int2d(Th)(u-u2(x,y));
cout<<v<<endl; cout.flush;
Thanks. Actually, I wanted to do the opposite, i.e. convert from P2 to P1, and I wanted the same mesh for both. This was easy to adapt from your example. I actually tried something like that before asking it but it apparently failed so I thought that it was probably not that simple. It turned out that I had another problem elsewhere making this fail. Starting from your example helped me to fix it and, finally, it is that simple. It is good to know also that this can be combined with a mesh change.
Dear Georges,
I think FreeFEM supports easily the conversion of P2 to P1 structures. You can do
FreeFEM file script.edp
:
IMPORT "io.edp"
mesh Th=square(10,10);
fespace Fh1(Th,P1);
fespace Fh2(Th,P2);
Fh2 phi=x+y; //A P2 function
Fh1 phiP1 = phi; //Interpolation from P2 to P1
exportMesh(Th); //This will be available in python
exportArray(phiP1[]);
Python code main.py
:
from pyfreefem import FreeFemRunner
from pymedit import P1Function
exports = FreeFemRunner("script.edp").execute()
phi = P1Function(exports['Th'], exports['phiP1[]']);
phi.plot() # Will show the function computed in FreeFEM
If you really want to be safe (this will also work for other type of finite element spaces), you can also convert manually your P2 function to a P1 function by solving a variational problem with the mass matrices as follows.
FreeFEM file script.edp
:
IMPORT "io.edp"
mesh Th=square(10,10);
fespace Fh1(Th,P1);
fespace Fh2(Th,P2);
Fh2 phi=x+y; //A P2 function
Fh1 phiP1, v;
//variational formulation for interpolating
solve interp(phiP1,v) = int2d(Th)(phiP1*v)-int2d(Th)(phi*v);
exportMesh(Th); //This will be available in python
exportArray(phiP1[]);