Choral
poisson_2d.f90
Go to the documentation of this file.
1 
61 
62 program poisson_2d
63 
64  use choral
66 
67  implicit none
68 
69  !! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
70  !! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
71  !!
72  !! VARAIBLE DEFINITION : BEGIN
73  !!
74  !!
75 
76  !! verb = verbosity level
77  !!
78  integer, parameter :: verb = 2
79 
80  !! SPACE DISCRETISATION
81  !!
82  !! fe = finite element method
83  !! quad = quadrature method
84  !!
85  integer, parameter :: fe = fe_p2_2d
86  integer, parameter :: quad = quad_gauss_trg_12
87  !!
88  !! msh = mesh
89  !! X_h = finite element space
90  !! qdm = integration method
91  !!
92  type(mesh) :: msh
93  type(fespace) :: X_h
94  type(quadmesh) :: qdm
95  !!
96  !! msh_file = mesh file
97  character(len=100), parameter :: &
98  & mesh_file= trim(GMSH_DIR)//"square/square_3.msh"
99 
100 
101  !! LINEAR SYSTEM
102  !!
103  !! kry = krylov method def.
104  !! mass = mass matrix
105  !! stiff = stiffness matrix
106  !! K = linear system matrix ( = mass + stiff )
107  !! rhs = right hand side
108  !! u_h = numerical solution
109  !!
110  type(krylov) :: kry
111  type(csr) :: mass, stiff, K
112  real(RP), dimension(:), allocatable :: rhs, u_h
113 
114 
115  !! NUMERICAL ERRORS
116  !!
117  real(RP) :: err_L2, err_H1
118 
119  character(LEN=300) :: shell_com
120 
121  !!
122  !! VARAIBLE DEFINITION : END
123  !!
124  !! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
125  !! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
126 
127 
128  !! !!!!!!!!!!!!!!!!!!!!! INITIALISATION
129  !!
130  call choral_init(verb=verb)
131  write(*,*)
132  write(*,*)'poisson_2d: start'
133  write(*,*)""
134  write(*,*)" RESOLUTION OF THE POISSON PROBLEM"
135  write(*,*)""
136  write(*,*)" -Delta u + u = f on \Omega"
137  write(*,*)""
138  write(*,*)" WITH A HOMOGENEOUS NEUMANN BOUNDARY CONDITION "
139  write(*,*)""
140  write(*,*)" grad u . n = 0 on \partial\Omega"
141  write(*,*)""
142 
143  write(*,*) ""
144  write(*,*)"================================== ASSEMBLING THE MESH "
145  msh = mesh(mesh_file, 'gmsh')
146  if (verb>1) call print(msh)
147 
148  write(*,*) ""
149  write(*,*)"================================== ASSEMBLING THE&
150  & FINITE ELEMENT SPACE"
151  x_h = fespace(msh)
152  call set(x_h, fe)
153  call assemble(x_h)
154  if (verb>1) call print(x_h)
155 
156  write(*,*) ""
157  write(*,*)"================================== ASSEMBLING THE&
158  & INTEGRATION METHOD ON \Omega"
159  qdm = quadmesh(msh)
160  call set(qdm, quad)
161  call assemble(qdm)
162  if (verb>1) call print(qdm)
163 
164  write(*,*) ""
165  write(*,*)"================================== ASSEMBLING THE&
166  & MASS AND STIFFNESS MATRICES"
167  !!
168  !! 'one_R3' is the density function equal to 1 on \Omega
169  call diffusion_massmat(mass , one_r3, x_h, qdm)
170  !!
171  !! 'EMetric' is the Euclidian metric on \Omega
172  call diffusion_stiffmat(stiff, emetric, x_h, qdm)
173  !!
174  !! K = mass + stiff
175  call add(k, stiff, 1._rp, mass, 1._rp)
176 
177  write(*,*) ""
178  write(*,*)"================================== RIGHT HAND SIDE 'F'"
179  !!
180  !! \int_\Omega f u_i dx
181  !! The function 'f' here is defined below after the 'contains' keyword
182  !!
183  call l2_product(rhs, f, x_h, qdm)
184 
185  write(*,*) ""
186  write(*,*)"================================== SOLVE THE LINEAR SYSTEM&
187  & K U = F "
188  !!
189  !! Solver setting
190  kry = krylov(kry_cg, tol=1e-6_rp, itmax=1000, verb=verb)
191  if (verb>1) call print(kry)
192  !!
193  !! allocate memory for the solution
194  call allocmem(u_h, x_h%nbDof)
195  u_h = 0.0_rp
196  !!
197  !! linear system inversion
198  call solve(u_h, kry, rhs, k)
199 
200  write(*,*) ""
201  write(*,*)"================================== POST-TREATMENT"
202  !!
203  !! L2 and H1_0 numerical errors
204  !! The exact solution 'u' and its gradient 'grad_y'
205  !! are defined below after the 'contains' keyword
206  !!
207  write(*,*) ""
208  write(*,*) "Numerical errors"
209  err_l2 = l2_dist(u, u_h, x_h, qdm)
210  err_h1 = l2_dist_grad(grad_u, u_h, x_h, qdm)
211  write(*,*)" Error | u - u_h|_L2 =", err_l2
212  write(*,*)" Error |\nabla (u - u_h)|_L2 =", err_h1
213 
214  if (verb>=1) then
215  !!
216  !! graphical display
217  !!
218  write(*,*) ""
219  write(*,*) "Graphical display"
220  !! output file def.
221  shell_com = 'poisson_2d_u.msh'
222  !!
223  !! first write the 'finite element mesh'
224  call write(x_h, trim(shell_com), 'gmsh')
225  !!
226  !! second save the finite element solution u_h
227  call gmsh_addview(x_h, u_h, &
228  & trim(shell_com), &
229  & 'Poisson problem: numerical solution', 0.0_rp, 1)
230  !!
231  !! shell command to visualise with gmsh
232  shell_com = 'gmsh -option '//trim(gmsh_dir)//'gmsh-options-view'&
233  & //' '//trim(shell_com)
234  call system(shell_com)
235 
236  end if
237 
238  call freemem(u_h)
239  call freemem(rhs)
240 
241 contains
242 
243  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
244  !!
247  function f(x)
248  real(RP) :: f
249  real(RP), dimension(3), intent(in) :: x
250 
251  f = (2._rp * pi**2 + 1._rp) * cos(pi*x(1)) * cos(pi*x(2))
252 
253  end function f
254 
255 
256  !! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
257  !!
260  function u(x)
261  real(RP) :: u
262  real(RP), dimension(3), intent(in) :: x
263 
264  u = cos(pi*x(1)) * cos(pi*x(2))
265 
266  end function u
267 
268  !! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
269  !!
272  function grad_u(x)
273  real(RP), dimension(3) :: grad_u
274  real(RP), dimension(3), intent(in) :: x
275 
276  grad_u(1) =-pi * sin(pi*x(1)) * cos(pi*x(2))
277  grad_u(2) =-pi * cos(pi*x(1)) * sin(pi*x(2))
278  grad_u(3) = 0._rp
279 
280  end function grad_u
281 
282 
283 end program poisson_2d
subroutine, public choral_init(verb)
Initialisation for the CHORAL library
Definition: choral.F90:166
TOP-LEVEL MODULE FOR THE LIBRARY CHORAL
Definition: choral.F90:27
integer, parameter fe_p2_2d
real(rp) function e(x, v1, v2)
real(rp) function, dimension(3) grad_u(x)
gradient of the exact solution
CHORAL CONSTANTS
real(rp) function f(x)
right hand side 'f'
real(rp) function u(x)
exact solution 'u'
program poisson_2d
SOLVES THE POISSON PROBLEM with homogeneous Neumann boundary conditions
Definition: poisson_2d.f90:62
integer, parameter quad_gauss_trg_12
integer, parameter kry_cg
CG linear solver.