Call PETSc matrix operator MatDiagonalSet (…)

Dear all,

I need to set a pressure reference point in my parallel FSI code by modifying the matrix, and I realise the following PETSc function may allow me to do this:

MatDiagonalSet (…)

Has this been implemented in FreeFEM (seems I cannot directly call it in my FreeFEM code)?

More generally, what’ the best way to set a zero-pressure reference in FreeFEM code (I found a zero-pressure reference is more stable than a penalty method in the weak form – tested by a serial code)?

Best,
Yongxing.

1 Like

There is no real need for the PETSc interface. You can set the diagonal of your local matrix, and then put that into your parallel Mat.

matrix Af = ...;
real[int] diagofA(Af.n);
diagofA = Af.diag; // get the diagonal of the matrix
...; // some operations on diagofA
Af.diag = diagofA ; // set the diagonal of the matrix
Ap = Af; // where Ap is a PETSc Mat
1 Like

Thank you very much Pierre! I watched your online talk about parallel interpolation between two meshes, is there a apiece of template code of doing this?
Best,
Yongxing.

Thank pjr, I can see the interpolation of values from the post, but can we have the interpolation matrix like a serial code?

Search for the keyword transferMat.

Thank you very much!
I have found an example of PtAP-2d-PETSc.edp which uses transferMat().
It seems this is only implemented in FreeFEM v4.7, does this version have a docker image?
I can only use docker image on the university’s ARC system.
Best,
Yongxing.

https://hub.docker.com/r/freefem/freefem

It seems be working if the following is not an error:

Mat Object: 2 MPI processes
type: mpiaij
rows=1681, cols=1681
total: nonzeros=18721, allocated nonzeros=18721
total number of mallocs used during MatSetValues calls=0
not using I-node (on process 0) routines
Mat Object: 2 MPI processes
type: mpiaij
rows=521, cols=521
total: nonzeros=17109, allocated nonzeros=17109
total number of mallocs used during MatSetValues calls=0
using nonscalable MatPtAP() implementation
not using I-node (on process 0) routines

Do we also have v4.7 for Windows?

Good morning. Do we have v4.7 or above for Windows? when I install FreeFem on Ubuntu (make -j), I came across the following errors:

\n\n ****** mshmet ****** \n\n
grep: WHERE-LD: No such file or directory
make[5]: Entering directory ‘/home/yongxing/Downloads/FreeFem-sources-master/3rdparty/mshmet’
make[5]: Nothing to be done for ‘all-local’.
make[5]: Leaving directory ‘/home/yongxing/Downloads/FreeFem-sources-master/3rdparty/mshmet’
make[4]: Leaving directory ‘/home/yongxing/Downloads/FreeFem-sources-master/3rdparty’
make[4]: Entering directory ‘/home/yongxing/Downloads/FreeFem-sources-master/3rdparty’
\n\n ****** yams ****** \n\n
grep: WHERE-LD: No such file or directory
make[5]: Entering directory ‘/home/yongxing/Downloads/FreeFem-sources-master/3rdparty/yams’
make[5]: Nothing to be done for ‘all-local’.
make[5]: Leaving directory ‘/home/yongxing/Downloads/FreeFem-sources-master/3rdparty/yams’
make[4]: Leaving directory ‘/home/yongxing/Downloads/FreeFem-sources-master/3rdparty’
make[4]: Entering directory ‘/home/yongxing/Downloads/FreeFem-sources-master/3rdparty’
\n\n ****** mmg3d ****** \n\n
grep: WHERE-LD: No such file or directory
make[5]: Entering directory ‘/home/yongxing/Downloads/FreeFem-sources-master/3rdparty/mmg3d’
make[5]: Nothing to be done for ‘all-local’.
make[5]: Leaving directory ‘/home/yongxing/Downloads/FreeFem-sources-master/3rdparty/mmg3d’
make[4]: Leaving directory ‘/home/yongxing/Downloads/FreeFem-sources-master/3rdparty’
make[4]: Entering directory ‘/home/yongxing/Downloads/FreeFem-sources-master/3rdparty’
\n\n ****** mmg ****** \n\n
grep: WHERE-LD: No such file or directory
make[5]: Entering directory ‘/home/yongxing/Downloads/FreeFem-sources-master/3rdparty/mmg’
…/getall -o mmg -a
make[4]: Entering directory ‘/home/yongxing/Downloads/FreeFem-sources-master/src/libMesh’
make[4]: Nothing to be done for ‘all’.
make[4]: Leaving directory ‘/home/yongxing/Downloads/FreeFem-sources-master/src/libMesh’
test -f …/src/libMesh/libMesh.a
mkdir -p include/libMesh
cp …/src/libMesh/*h include/libMesh
echo libMesh LD -L@DIR@/lib -lMesh > lib/WHERE.libMesh
echo libMesh INCLUDE -I@DIR@/include/libMesh >> lib/WHERE.libMesh
cp …/src/libMesh/libMesh.a lib/libMesh.a
mmg mmg.zip done
make[5]: Leaving directory ‘/home/yongxing/Downloads/FreeFem-sources-master/3rdparty/mmg’
make[4]: Leaving directory ‘/home/yongxing/Downloads/FreeFem-sources-master/3rdparty’
make[4]: Entering directory ‘/home/yongxing/Downloads/FreeFem-sources-master/3rdparty’
\n\n ****** gmm ****** \n\n
grep: WHERE-LD: No such file or directory
make[5]: Entering directory ‘/home/yongxing/Downloads/FreeFem-sources-master/3rdparty/gmm’
make[5]: Nothing to be done for ‘gmm’.
make[5]: Leaving directory ‘/home/yongxing/Downloads/FreeFem-sources-master/3rdparty/gmm’
make[4]: Leaving directory ‘/home/yongxing/Downloads/FreeFem-sources-master/3rdparty’
make[4]: Entering directory ‘/home/yongxing/Downloads/FreeFem-sources-master/3rdparty’
\n\n ****** nlopt ****** \n\n
grep: WHERE-LD: No such file or directory
make[5]: Entering directory ‘/home/yongxing/Downloads/FreeFem-sources-master/3rdparty/nlopt’
make[5]: Nothing to be done for ‘all’.
make[5]: Leaving directory ‘/home/yongxing/Downloads/FreeFem-sources-master/3rdparty/nlopt’
make[4]: Leaving directory ‘/home/yongxing/Downloads/FreeFem-sources-master/3rdparty’
make[4]: Entering directory ‘/home/yongxing/Downloads/FreeFem-sources-master/3rdparty’
\n\n ****** mumps-seq ****** \n\n
grep: WHERE-LD: No such file or directory
make[5]: Entering directory ‘/home/yongxing/Downloads/FreeFem-sources-master/3rdparty/mumps-seq’
make[5]: Nothing to be done for ‘all-local’.
make[5]: Leaving directory ‘/home/yongxing/Downloads/FreeFem-sources-master/3rdparty/mumps-seq’
make[4]: Leaving directory ‘/home/yongxing/Downloads/FreeFem-sources-master/3rdparty’
make[4]: Entering directory ‘/home/yongxing/Downloads/FreeFem-sources-master/3rdparty’
\n\n ****** ipopt ****** \n\n
grep: WHERE-LD: No such file or directory
make[5]: Entering directory ‘/home/yongxing/Downloads/FreeFem-sources-master/3rdparty/ipopt’
cd Ipopt-3.12.4 ;
./configure --disable-dependency-tracking
–disable-shared --enable-static
–with-mumps=’-L/home/yongxing/Downloads/FreeFem-sources-master/3rdparty/lib -ldmumpsFREEFEM-SEQ -lzmumpsFREEFEM-SEQ -lmumps_commonFREEFEM-SEQ -lpordFREEFEM-SEQ -lmpiseqFREEFEM-SEQ’
–without-hsl
–with-mumps-incdir=’/home/yongxing/Downloads/FreeFem-sources-master/3rdparty/include/mumps_seq’
CXX=‘g++’ CXXFLAGS=’-g -DNDEBUG -O3 -mmmx -mavx -std=c++14 -DBAMG_LONG_LONG -DNCHECKPTR -fPIC -I/home/yongxing/Downloads/FreeFem-sources-master/3rdparty/include/mumps_seq’
CC=‘gcc’ CFLAGS=’-g -DNDEBUG -O3 -mmmx -mavx -fPIC -I/home/yongxing/Downloads/FreeFem-sources-master/3rdparty/include/mumps_seq’
F77=‘gfortran’ FFLAGS=’-g -O2 -fPIC’
FLIBS=’-L/usr/lib/gcc/x86_64-linux-gnu/9 -L/usr/lib/gcc/x86_64-linux-gnu/9/…/…/…/x86_64-linux-gnu -L/usr/lib/gcc/x86_64-linux-gnu/9/…/…/…/…/lib -L/lib/x86_64-linux-gnu -L/lib/…/lib -L/usr/lib/x86_64-linux-gnu -L/usr/lib/…/lib -L/usr/lib/gcc/x86_64-linux-gnu/9/…/…/… -lgfortran -lm -lquadmath’
CXXCPP=’@CXXCPP@’ CPP=’@CXXCPP@’ MPICC=’’ MPICXX=’’ MPIFC=’’
–with-blas-lib=’-llapack -lblas -lm’ --with-lapack=’-llapack -lblas -lm’ --prefix=’/home/yongxing/Downloads/FreeFem-sources-master/3rdparty’
checking build system type… x86_64-unknown-linux-gnu
checking whether we want to compile in debug mode… no
checking for C compiler default output file name… a.out
checking whether the C compiler works… yes
checking whether we are cross compiling… no
checking for suffix of executables…
checking for suffix of object files… o
checking whether we are using the GNU C compiler… yes
checking whether gcc accepts -g… yes
checking for gcc option to accept ANSI C… none needed
configure: C compiler options are: -g -DNDEBUG -O3 -mmmx -mavx -fPIC -I/home/yongxing/Downloads/FreeFem-sources-master/3rdparty/include/mumps_seq
checking whether we are using the GNU C++ compiler… yes
checking whether g++ accepts -g… yes
checking whether C++ compiler g++ works… yes
configure: C++ compiler options are: -g -DNDEBUG -O3 -mmmx -mavx -std=c++14 -DBAMG_LONG_LONG -DNCHECKPTR -fPIC -I/home/yongxing/Downloads/FreeFem-sources-master/3rdparty/include/mumps_seq
configure: Trying to determine Fortran compiler name
checking whether we are using the GNU Fortran 77 compiler… yes
checking whether gfortran accepts -g… yes
configure: Fortran compiler options are: -g -O2 -fPIC
checking for egrep… grep -E
checking whether ln -s works… yes
checking for a BSD-compatible install… /usr/bin/install -c
checking whether build environment is sane… yes
checking for gawk… gawk
checking whether make sets $(MAKE)… yes
checking for style of include used by make… GNU
checking dependency style of gcc… none
checking dependency style of g++… none
checking whether to enable maintainer-specific portions of Makefiles… no
checking host system type… x86_64-unknown-linux-gnu
checking for a sed that does not truncate output… /usr/bin/sed
checking for ld used by gcc… /usr/bin/ld
checking if the linker (/usr/bin/ld) is GNU ld… yes
checking for /usr/bin/ld option to reload object files… -r
checking for BSD-compatible nm… /usr/bin/nm -B
checking how to recognise dependent libraries… pass_all
checking how to run the C preprocessor… @CXXCPP@
configure: error: C preprocessor “@CXXCPP@” fails sanity check
See `config.log’ for more details.
make[5]: *** [Makefile:42: Ipopt-3.12.4/FAIT] Error 1
make[5]: Leaving directory ‘/home/yongxing/Downloads/FreeFem-sources-master/3rdparty/ipopt’
make[4]: *** [Makefile:991: compile-dir] Error 2
make[4]: Leaving directory ‘/home/yongxing/Downloads/FreeFem-sources-master/3rdparty’
make[3]: *** [Makefile:1000: tag-compile-pkg] Error 1
make[3]: Leaving directory ‘/home/yongxing/Downloads/FreeFem-sources-master/3rdparty’
make[2]: *** [Makefile:593: all-recursive] Error 1
make[2]: Leaving directory ‘/home/yongxing/Downloads/FreeFem-sources-master/3rdparty’
make[1]: *** [Makefile:798: all-recursive] Error 1
make[1]: Leaving directory ‘/home/yongxing/Downloads/FreeFem-sources-master’
make: *** [Makefile:748: all] Error 2

Where do you see an error?

Thank you Prof. Pierre Jolivet. Let me try to fix the install errors first, and I may come back to you if I will not fix it. Can I ask you a question about transferMat(), it seems the vectorial fespace function is not allowed? I did

func PV1=[P2,P2,P1];
func PV2=[P2,P2];

Mat A,K,P;
buildDmesh(Th);
{
macro def(i)[i, i#B, i#C]//
macro init(i)[i, i, i]//
createMat(Th, A, PV1)
}
buildDmesh(Ths);
{
macro def(i)[i, i#B]//
macro init(i)[i, i]//
createMat(Ths, K, PV2)
}
transferMat(Ths, PV2, K, Th, PV1, A, P)

and I came across the follow errors:

[scsywan@login2.arc4 fsi_two_meshes]$ singularity run -e --env OMP_NUM_THREADS=1 --bind /nobackup:/nobackup …/freefem.sif ff-mpirun -n 2 $PWD/fsi_2d.edp -v 0
‘/usr/freefem/ff-petsc/r/bin/mpiexec’ -n 2 /usr/freefem/bin/FreeFem+±mpi -nw /nobackup/scsywan/fsi_two_meshes/fsi_2d.edp -v 0
the array size must be 2 not 1
current line = 1304 mpirank 1 / 2
the array size must be 2 not 1

Error line number 1304, in file macro: transferBase in /usr/freefem/lib/ff++/4.7-1/idp/macro_ddm.idp, before token ;
Invalide array size for vectorial fespace function
current line = 1304 mpirank 0 / 2
Compile error : Invalide array size for vectorial fespace function
line number :1304, ;
error Compile error : Invalide array size for vectorial fespace function
line number :1304, ;
code = 1 mpirank: 0

Different number of components are not allowed yet (but could be added in the future if there is a real need). In the meantime, stick to three components all the way:

{
macro def(i)[i, i#B, i#C]//
macro init(i)[i, i, i]//
Mat B;
createMat(Ths,B,PV1)
transferMat(Ths, PV1, B, Th, PV1, A, P)
}

Great! it works. Can I now do similar operations of matrices multiplication and plus? such as

P’ * B *P + A

Great! it works. Can I now do similar operations of matrices multiplication and plus? such as

P’ * B *P + A or matrix vector multiplication such as: real[int] rhs = P’*rhs2 ?

Just look at the examples for crying out loud…

Mat C;
MatPtAP(B, P, C);
A += C;

Sorry, but A+=C seems not allowed, and also matrix vector multiplication

real[int] rhs = P ’ * rhs2

is illegal too. I have examples diffusion-mg-2d-PETSc.edp and PtAP-2d-PETSc.edp, but there are no such operations

Neither are illegal, you need version 4.9. For a rectangular Mat, you need to do rhs = P' * rhs2; and declare rhs before the multiplication.

Thanks. OK, maybe version is the problem. I will try to build the source code…

Dear Prof. Pierre Jolivet,

Sorry for bothering you again. I have now installed v4.9 for Windows, and all the operations can work now. However,

when I use one processor, everything is right: the results are the same as my serial code; but when I use two processors, I can see from the plot that interpolation is wrong.

Sorry for sending you the code (attached), but you may directly run it. The problem is a benchmark FSI problem: a vertical leaflet in channel flow. The channel flow is discretised by on a stationary mesh Th, and the vertical leaflet is discretised by a separate mesh Ths which is moving. The interaction is implemented by FEM interpolation, and I developed this method with Olivier Pironneau.

Thank you very much for all your help!

Best,
Yongxing.
fsi_2d.edp (4.0 KB)