Report Typos and Errors    
Semi-Lagrangian Library
Modular library for kinetic and gyrokinetic simulations of plasmas in fusion energy devices.
aligned_translation_2d_spaghetti.F90
Go to the documentation of this file.
1 !**************************************************************
2 ! Copyright INRIA
3 ! Authors :
4 ! CALVI project team
5 !
6 ! This code SeLaLib (for Semi-Lagrangian-Library)
7 ! is a parallel library for simulating the plasma turbulence
8 ! in a tokamak.
9 !
10 ! This software is governed by the CeCILL-B license
11 ! under French law and abiding by the rules of distribution
12 ! of free software. You can use, modify and redistribute
13 ! the software under the terms of the CeCILL-B license as
14 ! circulated by CEA, CNRS and INRIA at the following URL
15 ! "http://www.cecill.info".
16 !**************************************************************
17 
18 !we take an initial function that is constant for displacement
19 ! X1'(t) = A1_0
20 ! X2'(t) = A2_0
21 !we work on periodic [0,1]^2 => we assume that A1_0 and A2_0 are integers
22 !we then consider a displacement
23 ! X1'(t) = A1
24 ! X2'(t) = A2
25 !where A1 and A2 are not so far from A1_0 and A2_0
26 !we use a lot of points in x1 and few points in x2
27 
29 #include "sll_working_precision.h"
30 #include "sll_assert.h"
31 #include "sll_memory.h"
35  use sll_m_fcisl
37 
38  implicit none
39 
40  class(sll_advection_1d_base), pointer :: adv_x1
41  class(sll_advection_1d_base), pointer :: adv_x2
42  sll_int32 :: i1
43  sll_int32 :: i2
44  sll_int32 :: nc_x1
45  sll_int32 :: nc_x2
46  sll_real64 :: a1
47  sll_real64 :: a2
48  sll_real64 :: a1_0
49  sll_real64 :: a2_0
50  sll_int32 :: k_mode
51  sll_real64, dimension(:, :), allocatable :: f
52  sll_real64, dimension(:, :), allocatable :: f_init
53  sll_real64, dimension(:, :), allocatable :: f_exact
54  !sll_real64, dimension(:) :: buf_x1
55  !sll_real64, dimension(:) :: buf_x2
56  sll_int32 :: ierr
57  sll_real64 :: x1
58  sll_real64 :: x2
59  sll_real64 :: x1_min
60  sll_real64 :: x1_max
61  sll_real64 :: x2_min
62  sll_real64 :: x2_max
63  sll_real64 :: delta_x1
64  sll_real64 :: delta_x2
65  sll_real64 :: dt
66  sll_int32 :: nb_step
67  sll_int32 :: step
68  sll_real64 :: err
69  sll_real64 :: alpha
70  sll_int32 :: i0
71  sll_real64 :: dt_loc
72  sll_real64, dimension(:, :), allocatable :: buf
73  sll_real64, dimension(:, :), allocatable :: f_new
74  sll_int32 :: d
75  sll_int32 :: r
76  sll_int32 :: s
77  sll_real64, dimension(:), allocatable :: xx
78  sll_real64, dimension(:), allocatable :: x1_array
79  sll_real64, dimension(:), allocatable :: x2_array
80  sll_int32 :: ell
81  sll_int32 :: i2_loc
82  character(len=256) :: filename
83  sll_int32 :: io_stat
84  sll_int32, parameter :: input_file = 99
85  sll_int32 :: i
86 
87  ! namelists for data input
88  namelist /params/ &
89  k_mode, &
90  nc_x1, &
91  nc_x2, &
92  x1_min, &
93  x1_max, &
94  x2_min, &
95  x2_max, &
96  dt, &
97  nb_step, &
98  d, &
99  a1_0, &
100  a2_0, &
101  a1, &
102  a2
103 
104  !initialization of default parameters
105  k_mode = 3
106  nc_x1 = 512
107  nc_x2 = 16
108  dt = 0.1_f64
109  nb_step = 10
110  d = 5
111 
112  a1_0 = 3._f64
113  a2_0 = 7._f64 ! we should assume A2>A1>=0
114 
115  a1 = 2.8357_f64
116  a2 = 7.18459_f64
117 
118  x1_min = 0._f64
119  x1_max = 1._f64
120  x2_min = 0._f64
121  x2_max = 1._f64
122 
123  call get_command_argument(1, filename)
124  if (len_trim(filename) .ne. 0) then
125  print *, '#read namelist'
126  open (unit=input_file, file=trim(filename)//'.nml', iostat=io_stat)
127  if (io_stat /= 0) then
128  print *, '#aligned_translation_2d failed to open file ', trim(filename)//'.nml'
129  stop
130  end if
131  read (input_file, params)
132  close (input_file)
133  else
134  print *, '#use default parameters'
135  end if
136  print *, '#k_mode=', k_mode
137  print *, '#Nc_x1=', nc_x1
138  print *, '#Nc_x2=', nc_x2
139  print *, '#x1_min, x1_max=', x1_min, x1_max
140  print *, '#x2_min, x2_max=', x2_min, x2_max
141  print *, '#nb_step=', nb_step
142  print *, '#dt=', dt
143  print *, '#d=', d
144  print *, '#A1_0', a1_0
145  print *, '#A2_0', a2_0
146  print *, '#A1=', a1
147  print *, '#A2=', a2
148 
149  delta_x1 = (x1_max - x1_min)/real(nc_x1, f64)
150  delta_x2 = (x2_max - x2_min)/real(nc_x2, f64)
151  r = -(d - 1)/2
152  s = (d + 1)/2
153  sll_allocate(xx(r:s), ierr)
154  sll_allocate(buf(r:s, nc_x1 + 1), ierr)
155  sll_allocate(f(nc_x1 + 1, nc_x2 + 1), ierr)
156  sll_allocate(f_init(nc_x1 + 1, nc_x2 + 1), ierr)
157  sll_allocate(f_exact(nc_x1 + 1, nc_x2 + 1), ierr)
158  sll_allocate(f_new(nc_x1 + 1, nc_x2 + 1), ierr)
159  sll_allocate(x1_array(nc_x1 + 1), ierr)
160  sll_allocate(x2_array(nc_x2 + 1), ierr)
161 
162  do i = 1, nc_x1 + 1
163  x1_array(i) = x1_min + real(i - 1, f64)*delta_x1
164  end do
165  do i = 1, nc_x2 + 1
166  x2_array(i) = x2_min + real(i - 1, f64)*delta_x2
167  end do
168 
169  do i1 = r, s
170  xx(i1) = real(i1, f64)
171  end do
172 
173  adv_x1 => new_periodic_1d_advector( &
174  nc_x1, &
175  x1_min, &
176  x1_max, &
177  ! LAGRANGE, &
178  spline, &
179  4)
180  adv_x2 => new_periodic_1d_advector( &
181  nc_x2, &
182  x2_min, &
183  x2_max, &
184  ! LAGRANGE, &
185  spline, &
186  4)
187 
188  do i2 = 1, nc_x2 + 1
189  x2 = x2_min + real(i2 - 1, f64)*delta_x2
190  do i1 = 1, nc_x1 + 1
191  x1 = x1_min + real(i1 - 1, f64)*delta_x1
192  x2 = x2_min + real(i2 - 1, f64)*delta_x2
193  f_init(i1, i2) = sin(2._f64*sll_p_pi*real(k_mode, f64)*(-a2_0*x1 + a1_0*x2))
194  x1 = x1 - a1*real(nb_step, f64)*dt
195  x2 = x2 - a2*real(nb_step, f64)*dt
196  f_exact(i1, i2) = sin(2._f64*sll_p_pi*real(k_mode, f64)*(-a2_0*x1 + a1_0*x2))
197  end do
198  end do
199 
200  !classical method with splitting
201  f = f_init
202  err = 0._f64
203 
204  do step = 1, nb_step
205  !advection in x1
206  do i2 = 1, nc_x2 + 1
207  call adv_x1%advect_1d_constant(a1, dt, f(1:nc_x1 + 1, i2), f(1:nc_x1 + 1, i2))
208  end do
209  !advection in x2
210  do i1 = 1, nc_x1 + 1
211  call adv_x2%advect_1d_constant(a2, dt, f(i1, 1:nc_x2 + 1), f(i1, 1:nc_x2 + 1))
212  end do
213  end do
214  err = maxval(abs(f - f_exact))
215  print *, '#err for classical method=', err
216 
217 !
218 !#ifndef NOHDF5
219 ! call plot_f_cartesian( &
220 ! 0, &
221 ! f, &
222 ! x1_array, &
223 ! Nc_x1+1, &
224 ! x2_array, &
225 ! Nc_x2+1, &
226 ! 'fold', 0._f64 )
227 !! call plot_f_cartesian( &
228 !! iplot, &
229 !! f_visu_light, &
230 !! sim%x1_array_light, &
231 !! np_x1_light, &
232 !! node_positions_x2_light, &
233 !! sim%num_dof_x2_light, &
234 !! 'light_f', time_init )
235 !#endif
236 
237  !new method
238  !first iota_modif
239  !then modif
240 
241  !new method
242  f = f_init
243  err = 0._f64
244  alpha = a2*dt/delta_x2
245  i0 = floor(alpha)
246  alpha = alpha - i0
247  print *, '#i0=', i0, alpha
248 
249  do step = 1, nb_step
250  do i2 = 1, nc_x2 + 1
251  !choose several dt_loc so that advection in x2 is exact
252  do ell = r, s
253  dt_loc = real(ell + i0, f64)*delta_x2/a2
254  i2_loc = modulo(i2 - ell - i0 - 1, nc_x2) + 1
255  call adv_x1%advect_1d_constant( &
256  a1, &
257  dt_loc, &
258  f(1:nc_x1 + 1, i2_loc), &
259  buf(ell, 1:nc_x1 + 1))
260  end do
261  ! interpolate between these values
262  do i1 = 1, nc_x1 + 1
263  f_new(i1, i2) = lagrange_interpolate(alpha, d, xx, buf(r:s, i1))
264  end do
265  end do
266  f = f_new
267  end do
268  err = maxval(abs(f - f_exact))
269  print *, '#err with new method=', err
270 !
271 !#ifndef NOHDF5
272 ! call plot_f_cartesian( &
273 ! 0, &
274 ! f, &
275 ! x1_array, &
276 ! Nc_x1+1, &
277 ! x2_array, &
278 ! Nc_x2+1, &
279 ! 'fnew', 0._f64 )
280 !! call plot_f_cartesian( &
281 !! iplot, &
282 !! f_visu_light, &
283 !! sim%x1_array_light, &
284 !! np_x1_light, &
285 !! node_positions_x2_light, &
286 !! sim%num_dof_x2_light, &
287 !! 'light_f', time_init )
288 !#endif
289 
290 !
291 !
292 !contains
293 !
294 !#ifndef NOHDF5
295 !!*********************
296 !!*********************
297 !
298 ! !---------------------------------------------------
299 ! ! Save the mesh structure
300 ! !---------------------------------------------------
301 ! subroutine plot_f_cartesian( &
302 ! iplot, &
303 ! f, &
304 ! node_positions_x1, &
305 ! nnodes_x1, &
306 ! node_positions_x2, &
307 ! nnodes_x2, &
308 ! array_name, time)
309 ! !mesh_2d)
310 ! use sll_m_xdmf
311 ! use sll_hdf5_io
312 ! sll_int32 :: file_id
313 ! sll_int32 :: error
314 ! sll_real64, dimension(:), intent(in) :: node_positions_x1
315 ! sll_real64, dimension(:), intent(in) :: node_positions_x2
316 ! character(len=*), intent(in) :: array_name !< field name
317 ! sll_real64, dimension(:,:), allocatable :: x1
318 ! sll_real64, dimension(:,:), allocatable :: x2
319 ! sll_int32, intent(in) :: nnodes_x1
320 ! sll_int32, intent(in) :: nnodes_x2
321 ! sll_int32 :: i, j
322 ! sll_int32, intent(in) :: iplot
323 ! character(len=4) :: cplot
324 ! sll_real64, dimension(:,:), intent(in) :: f
325 ! sll_real64 :: time
326 !
327 ! if (iplot == 1) then
328 !
329 ! SLL_ALLOCATE(x1(nnodes_x1,nnodes_x2), error)
330 ! SLL_ALLOCATE(x2(nnodes_x1,nnodes_x2), error)
331 ! do j = 1,nnodes_x2
332 ! do i = 1,nnodes_x1
333 ! x1(i,j) = node_positions_x1(i) !x1_min+real(i-1,f32)*dx1
334 ! x2(i,j) = node_positions_x2(j) !x2_min+real(j-1,f32)*dx2
335 ! end do
336 ! end do
337 ! call sll_hdf5_file_create("cartesian_mesh-x1.h5",file_id,error)
338 ! call sll_hdf5_write_array(file_id,x1,"/x1",error)
339 ! call sll_hdf5_file_close(file_id, error)
340 ! call sll_hdf5_file_create("cartesian_mesh-x2.h5",file_id,error)
341 ! call sll_hdf5_write_array(file_id,x2,"/x2",error)
342 ! call sll_hdf5_file_close(file_id, error)
343 ! deallocate(x1)
344 ! deallocate(x2)
345 !
346 ! end if
347 !
348 ! call int2string(iplot,cplot)
349 ! call sll_xdmf_open(trim(array_name)//cplot//".xmf","cartesian_mesh", &
350 ! nnodes_x1,nnodes_x2,file_id,error)
351 ! write(file_id,"(a,f8.3,a)") "<Time Value='",time,"'/>"
352 ! call sll_xdmf_write_array(trim(array_name)//cplot,f,"values", &
353 ! error,file_id,"Node")
354 ! call sll_xdmf_close(file_id,error)
355 ! end subroutine plot_f_cartesian
356 !
357 !#endif
358 
359 end program
program aligned_translation_2d
Abstract class for advection.
    Report Typos and Errors