I am using Freefem++ to solve the poisson equation
Grad^2 u(x,y,z) = -f(x,y,z)
It works well when I have an analytical expression for f, but now I have an f numerically defined (i.e. a set of data defined on a mesh) and I am wondering if I can still use Freefem++.
I.e. typical code (for a 2D problem in this case), looks like the following
mesh Sh= square(10,10); // mesh generation of a square
fespace Vh(Sh,P1); // space of P1 Finite Elements
Vh u,v; // u and v belongs to Vh
func f=cos(x)*y; // analytical function
problem Poisson(u,v)= // Definition of the problem
int2d(Sh)(dx(u)*dx(v)+dy(u)*dy(v)) // bilinear form
-int2d(Sh)(f*v) // linear form
+on(1,2,3,4,u=0); // Dirichlet Conditions
Poisson; // Solve Poisson Equation
plot(u); // Plot the result
I am wondering if I can define f numerically, rather than analytically.
Mesh & space Definition
We define a square unit with Nx=10 mesh and Ny=10 this provides 11 nodes on x axis and the same for y axis.
Conditions and problem statement
We are not going to use solve but we ll handle matrix (a more sophisticated way to solve with FreeFem).
First we define CL for our problem (Dirichlet ones).
Then we define the problem. Instead of writing
dx(u)*dx(v)+dy(u)*dy(v)
I suggest to use macro, so we define div as following but pay attention macro finishes by//
NOT;
.So Poisson bilinear form becomes:
After we extract Stifness Matrix
We proceed for solving, UMFPACK is a solver in FreeFem no much attention to this.
And here what you need. You want to define a value of function f on some specific nodes. I'm going to give you the secret, the poisson linear form.
You define value of the function f at any node you want to do.
We solve our system.
Finally we get the plot.
I hope this will help you, thanks to my internship supervisor Olivier, he always gives to me tricks specially on FreeFem. I tested it, it works very well. Good luck.
The method by afaf works in the case when the function
f
is a free-standing one. For the terms likeint2d(Sh)(f*u*v)
, another solution is required. I propose (actually I have red it somewhere in Hecht's manual) an approach that covers both cases. However, it works only forP1
finite elements, for which the degrees of freedom are coincided with the mesh nodes.