Choral
elasticity_Neumann_2D.f90
Go to the documentation of this file.
1 
80 
82 
84  use choral
85 
86  implicit none
87 
88  !! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
89  !! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
90  !!
91  !! VARAIBLE DEFINITION : BEGIN
92  !!
93  !!
94 
95  !! verb = verbosity level
96  !!
97  integer , parameter :: verb = 2
98 
99 
100  !! SPACE DISCRETISATION
101  !!
102  !! fe_v = finite element method (volumic)
103  !! fe_s = finite element method (surfacic)
104  !! qd_v = quadrature method (volumic)
105  !! qd_s = quadrature method (surfacic)
106  !! mesh_idx = index for the mesh
107  !!
108  integer , parameter :: fe_v = fe_p2_2d
109  integer , parameter :: fe_s = fe_p2_1d
110  integer , parameter :: qd_v = quad_gauss_trg_12
111  integer , parameter :: qd_s = quad_gauss_edg_4
112  character(LEN=1), parameter :: mesh_idx = "2"
113  !!
114  !! m = mesh
115  !! X_h = finite element space
116  !! Y = [X_h]^dim
117  !! qdm = integration method
118  !!
119  type(mesh) :: msh
120  type(fespace) :: X_h
121  type(fespacexk) :: Y
122  type(quadmesh) :: qdm
123  !!
124  !! msh_file
125  character(len=100), parameter :: mesh_file= &
126  & trim(GMSH_DIR)//"square/square_"//mesh_idx//".msh"
127 
128 
129  !! LINEAR SYSTEM
130  !!
131  !! kry = krylov method def.
132  !! stiff = stiffness matrix
133  !! rhs = right hand side
134  !! u_h = numerical solution
135  !!
136  type(krylov) :: kry
137  type(csr) :: stiff
138  real(RP), dimension(:), allocatable :: rhs, u_h, aux
139  real(RP), dimension(:), allocatable :: u_h_1, u_h_2
140 
141 
142  !! NUMERICAL ERRORS
143  !!
144  real(RP) :: err_L2, err_H1
145 
146  character(LEN=300) :: shell_com
147 
148  !!
149  !! VARAIBLE DEFINITION : END
150  !!
151  !! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
152  !! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
153 
154 
155  !! !!!!!!!!!!!!!!!!!!!!! INITIALISATION
156  !!
157  call choral_init(verb=verb)
158  write(*,*)
159  write(*,*)'elasticity_Neumann_2D: start'
160  write(*,*)""
161  write(*,*)" EXAMPLE OF A LINEAR ELASTICITY PROBLEM"
162  write(*,*)""
163  write(*,*)" -div(A(x) e(u)) = f on \Omega"
164  write(*,*)""
165  write(*,*)" WITH A NEUMANN BOUNDARY CONDITION "
166  write(*,*)""
167  write(*,*)" A(x)e(u)) n = g on \partial\Omega"
168  write(*,*)""
169  write(*,*)" AND WITH NON CONSTANT LAME COEFFICIENTS:"
170  write(*,*)""
171  write(*,*)" A(x) \xi = lambda(x) Tr(\xi) Id + 2 mu(x) \si"
172  write(*,*)""
173  write(*,*)" The problem data 'lambda', 'mu', 'u', 'f' and 'g'"
174  write(*,*)" have been derived in "
175  write(*,*)" choral/maple/example_elasticity_Neumann.mw"
176  write(*,*)""
177  write(*,*)""
178  write(*,*)""
179 
180  write(*,*) ""
181  write(*,*)"================================== MESH ASSEMBLING"
182  msh = mesh(mesh_file, 'gmsh')
183 
184  call print(msh)
185 
186  write(*,*) ""
187  write(*,*)"================================== FINITE ELEMENT&
188  & SPACE ASSEMBLING"
189  x_h =fespace(msh)
190  call set(x_h, fe_v)
191  call set(x_h, fe_s)
192  call assemble(x_h)
193  call print(x_h)
194  y = fespacexk(x_h, 2)
195  call print(y)
196 
197  write(*,*) ""
198  write(*,*)"================================== INTEGRATION METHOD&
199  & ON \Omega ASSEMBLING"
200  qdm = quadmesh(msh)
201  call set(qdm, qd_v)
202  call assemble(qdm)
203  call print(qdm)
204 
205  write(*,*) ""
206  write(*,*)"================================== STIFFNESS&
207  & MATRIX ASSEMBLING"
208  call elasticity_stiffmat(stiff, lambda, mu, &
209  & y, qdm)
210 
211 
212  write(*,*) ""
213  write(*,*)"================================== RIGHT HAND SIDE"
214  write(*,*)" 1- COMPUTATION OF F"
215  !!
216  !! \int_\Omega f u_i dx
217  call l2_product(rhs, y, qdm, f_1, f_2)
218 
219 
220  write(*,*) ""
221  write(*,*)"================================== RIGHT HAND SIDE"
222  write(*,*)" 2- COMPUTATION OF G"
223  !!
224  !! \int_{Gamma, x=0} g u_i dx
225  call elasticity_neumann_rhs(aux, y, qd_s, &
226  & g_1_x_0, g_2_x_0, f=x_le_0)
227  rhs = rhs + aux
228 
229  !! \int_{Gamma, x=1} g u_i dx
230  call elasticity_neumann_rhs(aux, y, qd_s, &
231  & g_1_x_1, g_2_x_1, f=x_ge_1)
232  rhs = rhs + aux
233  !!
234  !! \int_{Gamma, y=0} g u_i dx
235  call elasticity_neumann_rhs(aux, y, qd_s, &
236  & g_1_y_0, g_2_y_0, f=y_le_0)
237  rhs = rhs + aux
238  !!
239  !! \int_{Gamma, y=1} g u_i dx
240  call elasticity_neumann_rhs(aux, y, qd_s, &
241  & g_1_y_1, g_2_y_1, f=y_ge_1)
242  rhs = rhs + aux
243 
244  write(*,*) ""
245  write(*,*)"================================== SOLVE LINEAR SYSTEM&
246  & Stiff U_h = RHS "
247  !!
248  !! Solver setting
249  kry = krylov(kry_gmres, tol=1e-6_rp, itmax=1000, restart=25, verb=2)
250  call print(kry)
251  !!
252  !! initial guess
253  call interp_vect_func(u_h, y, u_1, u_2)
254  !!
255  !! linear system inversion
256  call solve(u_h, kry, rhs, stiff)
257  !!
258  !! cleaning
259  call clear(stiff)
260  call freemem(rhs)
261 
262  write(*,*) ""
263  write(*,*)"================================== POST-TREATMENT"
264  !!
265  !! extract component
266  !!
267  call extract_component(u_h_2, u_h, y, 2)
268  call extract_component(u_h_1, u_h, y, 1)
269 
270  !!
271  !! graphical display
272  !!
273  !! output file def.
274  shell_com = 'elasticity_Neumann_u.msh'
275  !!
276  !! first write the 'finite element mesh'
277  call write(x_h, trim(shell_com), 'gmsh')
278  !!
279  !! second save the finite element solution u_h
280  !!
281  call gmsh_addview(x_h, u_h_1, &
282  & trim(shell_com), &
283  &'Elasticity problem : numerical solution', 0.0_rp, 1)
284  call gmsh_addview(x_h, u_h_2, &
285  & trim(shell_com), &
286  &'Elasticity problem : numerical solution', 1.0_rp, 2)
287  !!
288  !! shell command to visualise with gmsh
289  shell_com = 'gmsh -option '//trim(gmsh_dir)//'gmsh-options-view'&
290  & //' '//trim(shell_com)
291  call system(shell_com)
292 
293  !!
294  !! L2 and H1_0 numerical errors
295  write(*,*) ""
296  write(*,*) "Numerical errors"
297  err_l2 = l2_dist(u_h, y, qdm, u_1, u_2)
298  err_h1 = l2_dist_grad(u_h, y, qdm, grad_u1, grad_u2)
299  write(*,*)" Error | u - u_h|_L2 =", err_l2
300  write(*,*)" Error |\nabla (u - u_h)|_L2 =", err_h1
301 
302  call freemem(u_h)
303  call freemem(u_h_1)
304  call freemem(u_h_2)
305  call freemem(aux)
306 
307 contains
308 
311  function lambda(x) result(r)
312  real(RP) :: r
313  real(RP), dimension(3), intent(in) :: x
314 
315  r = 1.0_rp
316 
317  end function lambda
318 
321  function mu(x) result(r)
322  real(RP) :: r
323  real(RP), dimension(3), intent(in) :: x
324 
325  r = 1.0_rp
326 
327  end function mu
328 
331  function u_1(x) result(r)
332  real(RP) :: r
333  real(RP), dimension(3), intent(in) :: x
334 
335  r = cos(pi*x(1) ) * sin(pi*x(2))
336 
337  end function u_1
338  function u_2(x) result(r)
339  real(RP) :: r
340  real(RP), dimension(3), intent(in) :: x
341 
342  r = sin(pi*x(1) ) * cos(pi*x(2))
343 
344  end function u_2
345 
346 
347 
350  function grad_u1(x) result(grad)
351  real(RP), dimension(3) :: grad
352  real(RP), dimension(3), intent(in) :: x
353 
354  grad(1) = -pi * sin(pi*x(1)) * sin(pi*x(2))
355  grad(2) = pi * cos(pi*x(1)) * cos(pi*x(2))
356  grad(3) = 0._rp
357 
358  end function grad_u1
359  function grad_u2(x) result(grad)
360  real(RP), dimension(3) :: grad
361  real(RP), dimension(3), intent(in) :: x
362 
363  grad(1) = pi * cos(pi*x(1)) * cos(pi*x(2))
364  grad(2) = -pi * sin(pi*x(1)) * sin(pi*x(2))
365  grad(3) = 0._rp
366 
367  end function grad_u2
368 
369 
372  function f_1(x) result(r)
373  real(RP) :: r
374  real(RP), dimension(3), intent(in) :: x
375 
376  r = 6.0_rp * pi**2 * u_1(x)
377 
378  end function f_1
379  function f_2(x) result(r)
380  real(RP) :: r
381  real(RP), dimension(3), intent(in) :: x
382 
383  r = 6.0_rp * pi**2 * u_2(x)
384 
385  end function f_2
386 
387 
390  function x_le_0(x) result(r)
391  real(RP) :: r
392  real(RP), dimension(3), intent(in) :: x
393 
394  r = -x(1)
395 
396  end function x_le_0
397 
398 
401  function x_ge_1(x) result(r)
402  real(RP) :: r
403  real(RP), dimension(3), intent(in) :: x
404 
405  r = x(1) - 1.0_rp
406 
407  end function x_ge_1
408 
411  function y_le_0(x) result(r)
412  real(RP) :: r
413  real(RP), dimension(3), intent(in) :: x
414 
415  r = -x(2)
416 
417  end function y_le_0
418 
419 
422  function y_ge_1(x) result(r)
423  real(RP) :: r
424  real(RP), dimension(3), intent(in) :: x
425 
426  r = x(2) - 1.0_rp
427 
428  end function y_ge_1
429 
430 
433  function g_1_x_0(x) result(r)
434  real(RP) :: r
435  real(RP), dimension(3), intent(in) :: x
436 
437  r = 0.0_rp
438 
439  end function g_1_x_0
440 
443  function g_2_x_0(x) result(r)
444  real(RP) :: r
445  real(RP), dimension(3), intent(in) :: x
446 
447  r = -2.0_rp * cos(pi*x(2)) * pi
448 
449  end function g_2_x_0
450 
453  function g_1_x_1(x) result(r)
454  real(RP) :: r
455  real(RP), dimension(3), intent(in) :: x
456 
457  r = 0.0_rp
458 
459  end function g_1_x_1
460 
463  function g_2_x_1(x) result(r)
464  real(RP) :: r
465  real(RP), dimension(3), intent(in) :: x
466 
467  r = -2.0_rp * cos(pi*x(2)) * pi
468 
469  end function g_2_x_1
470 
473  function g_1_y_0(x) result(r)
474  real(RP) :: r
475  real(RP), dimension(3), intent(in) :: x
476 
477  r = -2.0_rp * cos(pi*x(1)) * pi
478 
479  end function g_1_y_0
480 
483  function g_2_y_0(x) result(r)
484  real(RP) :: r
485  real(RP), dimension(3), intent(in) :: x
486 
487  r = 0.0_rp
488 
489  end function g_2_y_0
490 
493  function g_1_y_1(x) result(r)
494  real(RP) :: r
495  real(RP), dimension(3), intent(in) :: x
496 
497  r = -2.0_rp * cos(pi*x(1)) * pi
498 
499  end function g_1_y_1
500 
503  function g_2_y_1(x) result(r)
504  real(RP) :: r
505  real(RP), dimension(3), intent(in) :: x
506 
507  r = 0.0_rp
508 
509  end function g_2_y_1
510 
511 
512 end program elasticity_neumann_2d
real(rp) function x_ge_1(x)
To characterise {x=1}.
real(rp) function g_1_y_0(x)
g(x) on {y=0}, first component
real(rp) function g_2_y_1(x)
g(x) on {y=1}, second component
real(rp) function, dimension(3) grad_u1(x)
gradient of the exact solution
subroutine, public choral_init(verb)
Initialisation for the CHORAL library
Definition: choral.F90:166
real(rp) function g_1_y_1(x)
g(x) on {y=1}, first component
TOP-LEVEL MODULE FOR THE LIBRARY CHORAL
Definition: choral.F90:27
integer, parameter fe_p2_2d
integer, parameter quad_gauss_edg_4
integer, parameter kry_gmres
GmRes linear solver.
program elasticity_neumann_2d
SOLVES THE LINEAR ELASTICITY PROBLEM with Neumann boundary conditions
real(rp) function f_2(x)
real(rp) function g_2_x_0(x)
g(x) on {x=0}, second component
real(rp) function g_2_y_0(x)
g(x) on {y=0}, second component
real(rp) function, dimension(3) grad_u2(x)
real(rp) function x_le_0(x)
To characterise {x=0}.
real(rp) function e(x, v1, v2)
real(rp) function y_le_0(x)
To characterise {x=0}.
CHORAL CONSTANTS
real(rp) function mu(x)
Lame coefficient 'mu'.
real(rp) function f(x)
right hand side 'f'
real(rp) function y_ge_1(x)
To characterise {x=1}.
real(rp) function g_1_x_1(x)
g(x) on {x=1}, first component
real(rp) function lambda(x)
Lame coefficient 'lambda'.
integer, parameter quad_gauss_trg_12
real(rp) function f_1(x)
right hand side 'f'= =(f_1, f_2)
integer, parameter fe_p2_1d
real(rp) function u_2(x)
real(rp) function g_1_x_0(x)
g(x) on {x=0}, first component
real(rp) function g_2_x_1(x)
g(x) on {x=1}, second component
real(rp) function u_1(x)
exact solution u=(u_1, u_2)