/** # The poisson equation The poisson equation is a diffusion Fick equation of a specie $c$ $$ \nabla^2 c = s $$ we solve it in 2D. we compare the solution with the analytical solution of equation $$ \nabla^2 c = 1 $$ with boundary condition dirichlet 1 at every boundary the solution is $$ ce(x,y) = \sum\limits_{l=1}^{\infty}\sum\limits_{m=1}^{\infty}U_{lm}sin((2l-1)\pi x)sin((2m-1)\pi y) $$ with $$ U_{lm}=\frac{-16}{((2l-1)^2+(2m-1)^2)\pi ^4 (2l-1)(2m-1)} $$ This system can be solved with the poisson solver. */ #include "grid/multigrid.h" #include "poisson.h" #include "utils.h" #define pi 3.1415926 /** Concentration source and flux, and some obvious variables */ scalar c[],s[],flux[]; double dx,dy; /** the exact solution without mask */ double cexact(double x, double y) { double ce; ce =0.; //ce = (bi*y+1)/(bi*L0+1); for (double l = 1 ; l <= 200; l += 1){ for (double m= 1 ; m<= 200; m += 1){ ce += -16*sin((2*l-1)*pi*x)*sin((2*m-1)*pi*y)/(((2*l-1)*(2*l-1)+(2*m-1)*(2*m-1))*pi*pi*pi*pi*(2*l-1)*(2*m-1)); } } return ce; } /** Boundary conditions: dirichlet 1 */ c[top] = dirichlet(0); c[bottom] = dirichlet(0); c[right] = dirichlet(0); c[left] = dirichlet(0); /** ## Parameters The size of the domain `L0`. $0 abscemax) abscemax = cemax; csmax = fabs( interpolate (c, x, y)); if(csmax > abscsmax) abscsmax = csmax; }} fprintf (fpe, " %g \n", fabs((abscemax-abscsmax)/abscemax)); fclose(fpe); fprintf(stdout,"Err = "); fprintf(stdout," %g\n", fabs((abscemax-abscsmax)/abscemax)); fprintf(stdout," end\n"); } /** ##Run Then compile and run: ~~~bash qcc -O2 -Wall -o Poisson2D Poisson2D.c -lm; ./Poisson2D ~~~ ##Results ~~~gnuplot 3D plot of analycal solution and numerical solution set hidden3d sp[][][:] 'Fcs.txt' , 'Fce.txt' ~~~ ##Bibliography * http://www.ufrmeca.univ-lyon1.fr/~buffat/COURS/COURSDF_HTML/node32.html Version 1: december 2015 */