IMEX_SfloW2D  0.9
Shallowwatergranularflowmodel
geometry_2d.f90
Go to the documentation of this file.
1 !*********************************************************************
3 !
6 !*********************************************************************
7 MODULE geometry_2d
8 
9  USE parameters_2d, ONLY : verbose_level
10 
11  IMPLICIT NONE
12 
14  REAL*8, ALLOCATABLE :: x_comp(:)
15 
17  REAL*8, ALLOCATABLE :: x_stag(:)
18 
20  REAL*8, ALLOCATABLE :: y_comp(:)
21 
23  REAL*8, ALLOCATABLE :: y_stag(:)
24 
26  REAL*8, ALLOCATABLE :: b_stag_x(:,:)
27 
29  REAL*8, ALLOCATABLE :: b_stag_y(:,:)
30 
32  REAL*8, ALLOCATABLE :: b_ver(:,:)
33 
35  REAL*8, ALLOCATABLE :: b_cent(:,:)
36 
38  REAL*8, ALLOCATABLE :: b_prime_x(:,:)
39 
41  REAL*8, ALLOCATABLE :: b_prime_y(:,:)
42 
44  REAL*8, ALLOCATABLE :: grid_output(:,:)
45 
47  REAL*8, ALLOCATABLE :: grav_surf(:,:)
48 
50  REAL*8, ALLOCATABLE :: curv_xy(:,:)
51 
52  REAL*8, ALLOCATABLE :: topography_profile(:,:,:)
53 
55 
56  REAL*8 :: dx
57  REAL*8 :: x0
58  REAL*8 :: xn
59  REAL*8 :: dy
60  REAL*8 :: y0
61  REAL*8 :: yn
62  REAL*8 :: dx2
63  REAL*8 :: dy2
64  INTEGER :: comp_cells_x
65  INTEGER :: comp_interfaces_x
66  INTEGER :: comp_cells_y
67  INTEGER :: comp_interfaces_y
68  REAL*8 :: cell_size
69 
70 CONTAINS
71 
72  !*********************************************************************
74  !
77  !*********************************************************************
78 
79  SUBROUTINE init_grid
80 
82 
83  IMPLICIT none
84 
85  INTEGER j,k
86 
89 
90  ALLOCATE( x_comp(comp_cells_x) )
91  ALLOCATE( x_stag(comp_interfaces_x) )
92  ALLOCATE( y_comp(comp_cells_y) )
93  ALLOCATE( y_stag(comp_interfaces_y) )
94 
95  ALLOCATE( b_cent(comp_cells_x,comp_cells_y) )
101 
103 
104  ALLOCATE( grav_surf(comp_cells_x,comp_cells_y) )
105 
106  IF ( comp_cells_x .GT. 1 ) THEN
107 
108  dx = cell_size
109 
110  ELSE
111 
112  dx = 1.d0
113 
114  END IF
115 
116 
117  IF ( comp_cells_y .GT. 1 ) THEN
118 
119  dy = cell_size
120 
121  ELSE
122 
123  dy = 1.d0
124 
125  END IF
126 
127  xn = x0 + comp_cells_x * dx
128  yn = y0 + comp_cells_y * dy
129 
130  dx2 = dx / 2.d0
131  dy2 = dy / 2.d0
132 
133  ! eps_sing = MIN( dx ** 4.D0,dy ** 4.D0 )
134  eps_sing=min(min( dx ** 4.d0,dy ** 4.d0 ),1.d-20)
135 
136  IF ( verbose_level .GE. 1 ) WRITE(*,*) 'eps_sing = ',eps_sing
137 
138  x_comp(1) = x0 + 0.5d0 * dx
139  x_stag(1) = x0
140  y_comp(1) = y0 + 0.5d0 * dy
141  y_stag(1) = y0
142 
143  ! if topography is defined in file .inp we do a rescaling
144  IF ( .NOT.topography_demfile ) THEN
145 
146  topography_profile(1,:,:) = x0 + ( xn - x0 ) * topography_profile(1,:,:)
147 
148  topography_profile(2,:,:) = y0 + ( yn - y0 ) * topography_profile(2,:,:)
149 
150  b_ver(1,1) = topography_profile(3,1,1)
151 
152  ! bottom row
153  DO j=1,comp_cells_x
154 
155  x_stag(j+1) = x_stag(j) + dx
156 
157  IF(k.EQ.comp_cells_y)THEN
158 
159  ! right-bottom vertex
161 
162  ELSE
163 
164  CALL interp_1d_scalar( topography_profile(1,:,1) , &
165  topography_profile(3,:,1) , x_stag(j+1) , b_ver(j+1,1) )
166 
167  ENDIF
168 
169  b_stag_y(j,1)=0.5*(b_ver(j+1,1)+b_ver(j,1))
170 
171  ENDDO
172 
173  ! left column
174  DO k=1,comp_cells_y
175 
176  y_stag(k+1) = y_stag(k) + dy
177 
178  IF(k.EQ.comp_cells_y)THEN
179 
180  ! left-top vertex
182 
183  ELSE
184 
185  CALL interp_1d_scalar( topography_profile(2,1,:) , &
186  topography_profile(3,1,:) , y_stag(k+1) , b_ver(1,k+1) )
187 
188  ENDIF
189 
190  b_stag_x(1,k)=0.5*(b_ver(1,k+1)+b_ver(1,k))
191 
192  ENDDO
193 
194  ! all the other cells
195  DO j = 1,comp_cells_x
196 
197  x_comp(j) = 0.5 * ( x_stag(j) + x_stag(j+1) )
198 
199  DO k = 1,comp_cells_y
200 
201  y_comp(k) = 0.5 * ( y_stag(k) + y_stag(k+1) )
202 
203  ! right column
204  IF ( j.EQ.comp_cells_x .AND. k.NE.comp_cells_y ) THEN
205 
206  CALL interp_1d_scalar( &
209  y_stag(k+1) , b_ver(j+1,k+1) )
210 
211  ! top row
212  ELSEIF ( j.NE.comp_cells_x .AND. k.EQ.comp_cells_y ) THEN
213 
214  CALL interp_1d_scalar( &
217  x_stag(j+1) , b_ver(j+1,k+1) )
218 
219  ! right-top vertex
220  ELSEIF ( j.EQ.comp_cells_x .AND. k.EQ.comp_cells_y ) THEN
221 
224 
225  ! internal cells
226  ELSE
227 
228  CALL interp_2d_scalar( topography_profile(1,:,:) , &
229  & topography_profile(2,:,:), topography_profile(3,:,:) , &
230  & x_stag(j+1), y_stag(k+1) , b_ver(j+1,k+1) )
231 
232  ENDIF
233 
234  ! Eq. 3.12 K&P
235  b_cent(j,k) = 0.25 * ( b_ver(j,k) + b_ver(j+1,k) + b_ver(j,k+1) &
236  + b_ver(j+1,k+1) )
237 
238  ! Eq. 3.13 K&P
239  b_stag_x(j+1,k) = 0.5d0 * (b_ver(j+1,k+1)+b_ver(j+1,k))
240 
241  ! Eq. 3.14 K&P
242  b_stag_y(j,k+1) = 0.5d0 * (b_ver(j+1,k+1)+b_ver(j,k+1))
243 
244  ! Second factor in RHS 1st Eq. 3.16 K&P
245  b_prime_x(j,k) = ( b_stag_x(j+1,k) - b_stag_x(j,k) ) / &
246  ( x_stag(j+1) - x_stag(j) )
247 
248  ! Second factor in RHS 2nd Eq. 3.16 K&P
249  b_prime_y(j,k) = ( b_stag_y(j,k+1) - b_stag_y(j,k) ) / &
250  ( y_stag(k+1) - y_stag(k) )
251 
252  IF ( verbose_level .GE. 2 ) THEN
253 
254  WRITE(*,*) topography_profile(1,:,:)
255  WRITE(*,*) topography_profile(2,:,:)
256  WRITE(*,*) topography_profile(3,:,:)
257  WRITE(*,*) x_stag(j+1) , y_stag(k+1) , b_stag_x(j+1,k) , &
258  b_stag_y(j,k+1) , x_comp(j) , y_comp(k) , b_cent(j,k) , &
259  b_ver(j+1,k+1) , b_prime_x(j,k) , b_prime_y(j,k)
260  READ(*,*)
261 
262  END IF
263 
264  END DO
265 
266  ENDDO
267 
268  ! topography from larger dem
269  ELSE
270 
271  DO j=1,comp_interfaces_x
272 
273  x_stag(j) = x0 + (j-1) * dx
274 
275  END DO
276 
277  DO k=1,comp_interfaces_y
278 
279  y_stag(k) = y0 + (k-1) * dy
280 
281  END DO
282 
283  DO j=1,comp_cells_x
284 
285  x_comp(j) = 0.5d0 * ( x_stag(j) + x_stag(j+1) )
286 
287  END DO
288 
289  DO k=1,comp_cells_y
290 
291  y_comp(k) = 0.5d0 * ( y_stag(k) + y_stag(k+1) )
292 
293  END DO
294 
295  DO j=1,comp_interfaces_x
296 
297  DO k=1,comp_interfaces_y
298 
299  CALL interp_2d_scalar( topography_profile(1,:,:) , &
300  topography_profile(2,:,:), topography_profile(3,:,:) , &
301  x_stag(j), y_stag(k) , b_ver(j,k) )
302 
303  END DO
304 
305  END DO
306 
307  DO j=1,comp_cells_x
308 
309  DO k=1,comp_interfaces_y
310 
311  ! Eq. 3.14 K&P
312  b_stag_y(j,k) = 0.5d0 * ( b_ver(j+1,k) + b_ver(j,k) )
313 
314  END DO
315 
316  END DO
317 
318  DO j=1,comp_interfaces_x
319 
320  DO k=1,comp_cells_y
321 
322  ! Eq. 3.13 K&P
323  b_stag_x(j,k) = 0.5d0 * ( b_ver(j,k+1) + b_ver(j,k) )
324 
325  END DO
326 
327  END DO
328 
329  DO j=1,comp_cells_x
330 
331  DO k=1,comp_cells_y
332 
333  ! Eq. 3.12 K&P
334  b_cent(j,k) = 0.25d0 * ( b_ver(j,k) + b_ver(j+1,k) + b_ver(j,k+1) &
335  + b_ver(j+1,k+1) )
336 
337  ! Second factor in RHS 1st Eq. 3.16 K&P
338  b_prime_x(j,k) = ( b_stag_x(j+1,k) - b_stag_x(j,k) ) / &
339  ( x_stag(j+1) - x_stag(j) )
340 
341  ! Second factor in RHS 2nd Eq. 3.16 K&P
342  b_prime_y(j,k) = ( b_stag_y(j,k+1) - b_stag_y(j,k) ) / &
343  ( y_stag(k+1) - y_stag(k) )
344 
345  END DO
346 
347  ENDDO
348 
349  ENDIF
350 
351  ! this coefficient is used when the the scalar dot between the normal to the
352  ! topography and gravity is computed
353  DO j = 1,comp_cells_x
354 
355  DO k=1,comp_cells_y
356 
357  grav_surf(j,k) = - ( 1.d0/ dsqrt( 1.d0 + b_prime_x(j,k)**2 &
358  + b_prime_y(j,k)**2) )
359 
360  ENDDO
361 
362  ENDDO
363 
364  END SUBROUTINE init_grid
365 
366  !---------------------------------------------------------------------------
368  !
376  !---------------------------------------------------------------------------
377 
378  SUBROUTINE interp_1d_scalar(x1, f1, x2, f2)
379  IMPLICIT NONE
380 
381  REAL*8, INTENT(IN), DIMENSION(:) :: x1, f1
382  REAL*8, INTENT(IN) :: x2
383  REAL*8, INTENT(OUT) :: f2
384  INTEGER :: n, n1x, t
385  REAL*8 :: grad , rel_pos
386 
387  n1x = SIZE(x1)
388 
389  !
390  ! ... locate the grid points near the topographic points
391  ! ... and interpolate linearly the profile
392  !
393  t = 1
394 
395  search:DO n = 1, n1x-1
396 
397  rel_pos = ( x2 - x1(n) ) / ( x1(n+1) - x1(n) )
398 
399  IF ( ( rel_pos .GE. 0.d0 ) .AND. ( rel_pos .LE. 1.d0 ) ) THEN
400 
401  grad = ( f1(n+1)-f1(n) ) / ( x1(n+1)-x1(n) )
402  f2 = f1(n) + ( x2-x1(n) ) * grad
403 
404  EXIT search
405 
406  ELSEIF ( rel_pos .LT. 0.d0 ) THEN
407 
408  f2 = f1(n)
409 
410  ELSE
411 
412  f2 = f1(n+1)
413 
414  END IF
415 
416  END DO search
417 
418  RETURN
419 
420  END SUBROUTINE interp_1d_scalar
421 
422  !---------------------------------------------------------------------------
424  !
434  !---------------------------------------------------------------------------
435 
436  SUBROUTINE interp_2d_scalar(x1, y1, f1, x2, y2, f2)
437  IMPLICIT NONE
438 
439  REAL*8, INTENT(IN), DIMENSION(:,:) :: x1, y1, f1
440  REAL*8, INTENT(IN) :: x2, y2
441  REAL*8, INTENT(OUT) :: f2
442 
443  INTEGER :: ix , iy
444  REAL*8 :: alfa_x , alfa_y
445 
446  IF ( size(x1,1) .GT. 1 ) THEN
447 
448  ix = floor( ( x2 - x1(1,1) ) / ( x1(2,1) - x1(1,1) ) ) + 1
449  ix = min( ix , SIZE(x1,1)-1 )
450  alfa_x = ( x1(ix+1,1) - x2 ) / ( x1(ix+1,1) - x1(ix,1) )
451 
452  ELSE
453 
454  ix = 1
455  alfa_x = 0.d0
456 
457  END IF
458 
459  IF ( size(x1,2) .GT. 1 ) THEN
460 
461  iy = floor( ( y2 - y1(1,1) ) / ( y1(1,2) - y1(1,1) ) ) + 1
462  iy = min( iy , SIZE(x1,2)-1 )
463  alfa_y = ( y1(1,iy+1) - y2 ) / ( y1(1,iy+1) - y1(1,iy) )
464 
465  ELSE
466 
467  iy = 1
468  alfa_y = 0.d0
469 
470  END IF
471 
472  IF ( size(x1,1) .EQ. 1 ) THEN
473 
474  f2 = alfa_y * f1(ix,iy) + ( 1.d0 - alfa_y ) * f1(ix,iy+1)
475 
476  ELSEIF ( size(x1,2) .EQ. 1 ) THEN
477 
478  f2 = alfa_x * f1(ix,iy) + ( 1.d0 - alfa_x ) * f1(ix+1,iy)
479 
480  ELSE
481 
482  f2 = alfa_x * ( alfa_y * f1(ix,iy) + ( 1.d0 - alfa_y ) * f1(ix,iy+1) ) &
483  + ( 1.d0 - alfa_x ) * ( alfa_y * f1(ix+1,iy) + ( 1.d0 - alfa_y ) &
484  * f1(ix+1,iy+1) )
485 
486  END IF
487 
488  END SUBROUTINE interp_2d_scalar
489 
490 
491  !---------------------------------------------------------------------------
493  !
504  !---------------------------------------------------------------------------
505 
506  SUBROUTINE interp_2d_scalarb(x1, y1, f1, x2, y2, f2)
507  IMPLICIT NONE
508 
509  REAL*8, INTENT(IN), DIMENSION(:) :: x1, y1
510  REAL*8, INTENT(IN), DIMENSION(:,:) :: f1
511  REAL*8, INTENT(IN) :: x2, y2
512  REAL*8, INTENT(OUT) :: f2
513 
514  INTEGER :: ix , iy
515  REAL*8 :: alfa_x , alfa_y
516 
517  IF ( size(x1) .GT. 1 ) THEN
518 
519  ix = floor( ( x2 - x1(1) ) / ( x1(2) - x1(1) ) ) + 1
520  ix = min( ix , SIZE(x1)-1 )
521  alfa_x = ( x1(ix+1) - x2 ) / ( x1(ix+1) - x1(ix) )
522 
523  ELSE
524 
525  ix = 1
526  alfa_x = 0.d0
527 
528  END IF
529 
530  IF ( size(y1) .GT. 1 ) THEN
531 
532  iy = floor( ( y2 - y1(1) ) / ( y1(2) - y1(1) ) ) + 1
533  iy = min( iy , SIZE(x1)-1 )
534  alfa_y = ( y1(iy+1) - y2 ) / ( y1(iy+1) - y1(iy) )
535 
536  ELSE
537 
538  iy = 1
539  alfa_y = 0.d0
540 
541  END IF
542 
543  IF ( size(x1) .EQ. 1 ) THEN
544 
545  f2 = alfa_y * f1(ix,iy) + ( 1.d0 - alfa_y ) * f1(ix,iy+1)
546 
547  ELSEIF ( size(y1) .EQ. 1 ) THEN
548 
549  f2 = alfa_x * f1(ix,iy) + ( 1.d0 - alfa_x ) * f1(ix+1,iy)
550 
551  ELSE
552 
553  f2 = alfa_x * ( alfa_y * f1(ix,iy) + ( 1.d0 - alfa_y ) * f1(ix,iy+1) ) &
554  + ( 1.d0 - alfa_x ) * ( alfa_y * f1(ix+1,iy) + ( 1.d0 - alfa_y ) &
555  * f1(ix+1,iy+1) )
556 
557  END IF
558 
559  END SUBROUTINE interp_2d_scalarb
560 
561 
562  !------------------------------------------------------------------------------
564  !
570  !------------------------------------------------------------------------------
571  REAL*8 FUNCTION topography_function(x,y)
572  IMPLICIT NONE
573 
574  REAL*8, INTENT(IN) :: x,y
575 
576  REAL*8, PARAMETER :: pig = 4.0*atan(1.0)
577  REAL*8, PARAMETER :: eps_dis = 1.d-8
578  REAL*8 :: a
579 
580  ! example 1D from Kurganov and Petrova 2007
581  !IF(y.LT.0.0)THEN
582  !
583  ! topography_function = 1.d0
584  !
585  !ELSEIF(y.GE.0.0.AND.y.LE.0.4)THEN
586  !
587  ! topography_function = COS(pig*y)**2
588  !
589  !ELSEIF(y.GT.0.4.AND.y.LE.0.5)THEN
590  !
591  ! topography_function = COS(pig*y)**2+0.25*(COS(10.0*pig*(y-0.5))+1)
592  !
593  !ELSEIF(y.GT.0.5.AND.y.LE.0.6)THEN
594  !
595  ! topography_function = 0.5*COS(pig*y)**4+0.25*(COS(10.0*pig*(y-0.5))+1)
596  !
597  !ELSEIF(y.GT.0.6.AND.y.LT.1.0-eps_dis)THEN
598  !
599  ! topography_function = 0.5*COS(pig*y)**4
600  !
601  !ELSEIF(y.GE.1.0-eps_dis.AND.y.LE.1.0+eps_dis)THEN
602  !
603  ! topography_function = 0.25
604  !
605  !ELSEIF(y.GT.1.0+eps_dis.AND.y.LE.1.5)THEN
606  !
607  ! topography_function = 0.25*SIN(2*pig*(y-1))
608  !
609  !ELSE
610  !
611  ! topography_function = 0.d0
612  !
613  !ENDIF
614 
615 
616  ! example 2D from Kurganov and Petrova 2007
617  IF(abs(y).LE.0.5.AND.x.LE.(y-1.0)/2.0)THEN
618 
619  a=y**2
620 
621  ELSEIF(abs(y).GT.0.5.AND.x.LE.(y-1.0)/2.0)THEN
622 
623  a=y**2+0.1*sin(pig*x)
624 
625  ELSE
626 
627  a=max(0.125,y**2+0.1*sin(pig*x))
628 
629  ENDIF
630 
631  topography_function=7.0/32.0*exp(-8.0*(x-0.3)**2-60.0*(y-0.1)**2)- &
632  & 1.0/8.0*exp(-30.0*(x+0.1)**2-90.0*(y+0.2)**2) + a
633 
634  END FUNCTION topography_function
635 
636 
637 END MODULE geometry_2d
real *8, dimension(:,:), allocatable b_prime_x
Topography slope (x direction) at the centers of the control volumes.
Definition: geometry_2d.f90:38
logical topography_demfile
Flag for uploading topography from a different file (topography_dem.asc)
real *8 dy
Control volumes size.
Definition: geometry_2d.f90:59
real *8, dimension(:), allocatable x_comp
Location of the centers (x) of the control volume of the domain.
Definition: geometry_2d.f90:14
real *8 y0
Bottom of the physical domain.
Definition: geometry_2d.f90:60
real *8, dimension(:,:), allocatable b_cent
Topography at the centers of the control volumes.
Definition: geometry_2d.f90:35
integer comp_cells_x
Number of control volumes x in the comp. domain.
Definition: geometry_2d.f90:64
real *8, dimension(:,:), allocatable grav_surf
gravity vector wrt surface coordinates for each cell
Definition: geometry_2d.f90:47
real *8, dimension(:), allocatable y_comp
Location of the centers (y) of the control volume of the domain.
Definition: geometry_2d.f90:20
integer n_topography_profile_y
Definition: geometry_2d.f90:54
Parameters.
real *8 dx
Control volumes size.
Definition: geometry_2d.f90:56
integer comp_cells_y
Number of control volumes y in the comp. domain.
Definition: geometry_2d.f90:66
real *8, dimension(:,:), allocatable curv_xy
curvature wrt mixed directions for each cell
Definition: geometry_2d.f90:50
integer n_topography_profile_x
Definition: geometry_2d.f90:54
integer comp_interfaces_y
Number of interfaces (comp_cells_y+1)
Definition: geometry_2d.f90:67
real *8 xn
Right of the physical domain.
Definition: geometry_2d.f90:58
subroutine init_grid
Finite volume grid initialization.
Definition: geometry_2d.f90:80
real *8 yn
Top of the physical domain.
Definition: geometry_2d.f90:61
real *8 x0
Left of the physical domain.
Definition: geometry_2d.f90:57
real *8, dimension(:,:), allocatable b_ver
Topography at the vertices of the control volumes.
Definition: geometry_2d.f90:32
real *8, dimension(:,:), allocatable grid_output
Solution in ascii grid format (ESRI)
Definition: geometry_2d.f90:44
Grid module.
Definition: geometry_2d.f90:7
real *8 cell_size
Definition: geometry_2d.f90:68
real *8, dimension(:,:), allocatable b_stag_x
Topography at the boundaries (x) of the control volumes.
Definition: geometry_2d.f90:26
real *8 function topography_function(x, y)
Topography function.
integer verbose_level
real *8, dimension(:,:,:), allocatable topography_profile
Definition: geometry_2d.f90:52
real *8, dimension(:,:), allocatable b_prime_y
Topography slope (y direction) at the centers of the control volumes.
Definition: geometry_2d.f90:41
real *8, dimension(:,:), allocatable b_stag_y
Topography at the boundaries (y) of the control volumes.
Definition: geometry_2d.f90:29
real *8, dimension(:), allocatable x_stag
Location of the boundaries (x) of the control volumes of the domain.
Definition: geometry_2d.f90:17
subroutine interp_2d_scalarb(x1, y1, f1, x2, y2, f2)
Scalar interpolation (2D)
subroutine interp_2d_scalar(x1, y1, f1, x2, y2, f2)
Scalar interpolation (2D)
real *8 dx2
Half x Control volumes size.
Definition: geometry_2d.f90:62
integer comp_interfaces_x
Number of interfaces (comp_cells_x+1)
Definition: geometry_2d.f90:65
real *8 eps_sing
parameter for desingularization
real *8, dimension(:), allocatable y_stag
Location of the boundaries (x) of the control volumes of the domain.
Definition: geometry_2d.f90:23
subroutine interp_1d_scalar(x1, f1, x2, f2)
Scalar interpolation.
real *8 dy2
Half y Control volumes size.
Definition: geometry_2d.f90:63