 # Automatic parallel

Hello, everyone! Is there some tricks on FreeFEM such that I write codes in serial, but it works in parallel.

There is very little to be done to get a sequential script working in parallel using the PETSc interface.
Here is an example for the Poisson equation.

``````mesh Th = square(100, 100);
fespace Vh(Th, P1);
varf vPb(u, v) = int2d(Th)(dx(u)*dx(v) + dy(u)*dy(v)) + int2d(Th)(v) + on(1, u = 0);
matrix A;
A = vPb(Vh, Vh);
real[int] rhs = vPb(0, Vh);
Vh sol;
sol[] = A^-1 * rhs;
plot(sol, cmm = "Sequential solution");
``````

And in parallel.

``````load "PETSc"
include "macro_ddm.idp"
mesh Th = square(100, 100);
fespace Vh(Th, P1);
varf vPb(u, v) = int2d(Th)(dx(u)*dx(v) + dy(u)*dy(v)) + int2d(Th)(v) + on(1, u = 0);
Mat A;
createMat(Th, A, P1);
real[int] rhs = vPb(0, Vh);
A = vPb(Vh, Vh);
Vh sol;
sol[] = A^-1 * rhs;
plotD(Th, sol, cmm = "Parallel solution");
``````

If you want to understand this better, you could have a look here and there.

Thanks for your reply. How about multiple variables such as Stokes equations, and do we have to use a matrix?

It works the same, see FreeFem-sources/stokes-2d-PETSc.edp at develop · FreeFem/FreeFem-sources · GitHub. By “do we have to use a matrix?”, are you asking if it works with the keyword `solve` instead of `varf`? If that is the case, then no, it does not work with `solve` and you have to assemble `varf` instead. Note that any `solve` can be converted into a `varf`.