forked from xcompact3d/x3d2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmesh.f90
437 lines (351 loc) · 13.7 KB
/
mesh.f90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
module m_mesh
use iso_fortran_env, only: stderr => error_unit
use mpi
use m_common, only: dp, DIR_X, DIR_Y, DIR_Z, DIR_C, &
CELL, VERT, none, X_FACE, Y_FACE, Z_FACE, &
X_EDGE, Y_EDGE, Z_EDGE
use m_field, only: field_t
implicit none
! Stores geometry information
type :: geo_t
real(dp), dimension(3) :: d ! size of a cell in each direction (=edge length, distance between centers, distance between vertices)
real(dp), dimension(3) :: L ! Global dimensions of the domain in each direction
end type
! Stores parallel domain related information
type :: parallel_t
integer :: nrank ! local rank ID
integer :: nproc ! total number of ranks/proc participating in the domain decomposition
integer, dimension(3) :: nrank_dir ! local rank ID in each direction
integer, dimension(3) :: nproc_dir ! total number of proc in each direction
integer, dimension(3) :: n_offset ! number of cells offset in each direction due to domain decomposition
integer, dimension(3) :: pnext ! rank ID of the previous rank in each direction
integer, dimension(3) :: pprev ! rank ID of the next rank in each direction
contains
procedure :: is_root ! returns if the current rank is the root rank
end type
! The mesh class stores all the information about the global and local (due to domain decomposition) mesh
! It also includes getter functions to access some of its parameters
type :: mesh_t
integer, dimension(3), private :: global_vert_dims ! global number of vertices in each direction without padding (cartesian structure)
integer, dimension(3), private :: global_cell_dims ! global number of cells in each direction without padding (cartesian structure)
integer, dimension(3), private :: vert_dims_padded ! local domain size including padding (cartesian structure)
integer, dimension(3), private :: vert_dims ! local number of vertices in each direction without padding (cartesian structure)
integer, dimension(3), private :: cell_dims ! local number of cells in each direction without padding (cartesian structure)
logical, dimension(3), private :: periodic_BC ! Whether or not a direction has a periodic BC
integer, private :: sz
class(geo_t), allocatable :: geo ! object containing geometry information
class(parallel_t), allocatable :: par ! object containing parallel domain decomposition information
contains
procedure :: get_SZ
procedure :: get_dims
procedure :: get_global_dims
procedure :: get_n_groups_dir
procedure :: get_n_groups_phi
generic :: get_n_groups => get_n_groups_dir, get_n_groups_phi
procedure :: get_field_dims_dir
procedure :: get_field_dims_phi
procedure :: get_field_dims_phi_dataloc
generic :: get_field_dims => get_field_dims_dir, get_field_dims_phi, &
get_field_dims_phi_dataloc
procedure :: get_n_dir
procedure :: get_n_phi
generic :: get_n => get_n_dir, get_n_phi
procedure :: get_padded_dims_phi
procedure :: get_padded_dims_dir
generic :: get_padded_dims => get_padded_dims_dir, get_padded_dims_phi
procedure :: get_coordinates
procedure :: set_sz
procedure :: set_padded_dims
end type mesh_t
interface mesh_t
module procedure mesh_init
end interface mesh_t
contains
function mesh_init(dims_global, nproc_dir, L_global, &
periodic_BC) result(mesh)
!! Completely initialise the mesh object.
!! Upon initialisation the mesh object can be read-only and shouldn't be edited
!! Takes as argument global information about the mesh like its length, number of cells and decomposition in each direction
integer, dimension(3), intent(in) :: dims_global
integer, dimension(3), intent(in) :: nproc_dir ! Number of proc in each direction
real(dp), dimension(3), intent(in) :: L_global
logical, dimension(3), optional, intent(in) :: periodic_BC
type(mesh_t) :: mesh
integer :: dir
integer :: ierr
allocate (mesh%geo)
allocate (mesh%par)
mesh%global_vert_dims(:) = dims_global
if (present(periodic_BC)) then
mesh%periodic_BC(:) = periodic_BC
else
! Default to periodic BC
mesh%periodic_BC(:) = .true.
end if
do dir = 1, 3
if (mesh%periodic_BC(dir)) then
mesh%global_cell_dims(dir) = mesh%global_vert_dims(dir)
else
mesh%global_cell_dims(dir) = mesh%global_vert_dims(dir) - 1
end if
end do
! Geometry
mesh%geo%L = L_global
mesh%geo%d = mesh%geo%L/mesh%global_cell_dims
! Parallel domain decomposition
mesh%par%nproc_dir(:) = nproc_dir
mesh%par%nproc = product(nproc_dir(:))
call MPI_Comm_rank(MPI_COMM_WORLD, mesh%par%nrank, ierr)
call MPI_Comm_size(MPI_COMM_WORLD, mesh%par%nproc, ierr)
call domain_decomposition(mesh)
! Define default values
mesh%vert_dims_padded = mesh%vert_dims
mesh%sz = 1
end function mesh_init
subroutine set_padded_dims(self, vert_dims)
class(mesh_t), intent(inout) :: self
integer, dimension(3), intent(in) :: vert_dims
self%vert_dims_padded = vert_dims
end subroutine
subroutine set_sz(self, sz)
class(mesh_t), intent(inout) :: self
integer, intent(in) :: sz
self%sz = sz
end subroutine
subroutine domain_decomposition(mesh)
!! Supports 1D, 2D, and 3D domain decomposition.
!!
!! Current implementation allows only constant sub-domain size across a
!! given direction.
class(mesh_t), intent(inout) :: mesh
integer, allocatable, dimension(:, :, :) :: global_ranks
integer :: i, nproc_x, nproc_y, nproc_z, nproc
integer, dimension(3) :: subd_pos, subd_pos_prev, subd_pos_next
integer :: dir
logical :: is_last_domain
! Number of processes on a direction basis
nproc_x = mesh%par%nproc_dir(1)
nproc_y = mesh%par%nproc_dir(2)
nproc_z = mesh%par%nproc_dir(3)
! A 3D array corresponding to each region in the global domain
allocate (global_ranks(nproc_x, nproc_y, nproc_z))
! set the corresponding global rank for each sub-domain
global_ranks = reshape([(i, i=0, mesh%par%nproc - 1)], &
shape=[nproc_x, nproc_y, nproc_z])
! subdomain position in the global domain
subd_pos = findloc(global_ranks, mesh%par%nrank)
! local/directional position of the subdomain
mesh%par%nrank_dir(:) = subd_pos(:) - 1
! Define number of cells and vertices in each direction
mesh%vert_dims = mesh%global_vert_dims/mesh%par%nproc_dir
do dir = 1, 3
is_last_domain = (mesh%par%nrank_dir(dir) + 1 == mesh%par%nproc_dir(dir))
if (is_last_domain .and. (.not. mesh%periodic_BC(dir))) then
mesh%cell_dims(dir) = mesh%vert_dims(dir) - 1
else
mesh%cell_dims(dir) = mesh%vert_dims(dir)
end if
end do
mesh%par%n_offset(:) = mesh%vert_dims(:)*mesh%par%nrank_dir(:)
do dir = 1, 3
nproc = mesh%par%nproc_dir(dir)
subd_pos_prev(:) = subd_pos(:)
subd_pos_prev(dir) = modulo(subd_pos(dir) - 2, nproc) + 1
mesh%par%pprev(dir) = global_ranks(subd_pos_prev(1), &
subd_pos_prev(2), &
subd_pos_prev(3))
subd_pos_next(:) = subd_pos(:)
subd_pos_next(dir) = modulo(subd_pos(dir) - nproc, nproc) + 1
mesh%par%pnext(dir) = global_ranks(subd_pos_next(1), &
subd_pos_next(2), &
subd_pos_next(3))
end do
end subroutine domain_decomposition
pure function get_sz(self) result(sz)
!! Getter for parameter SZ
class(mesh_t), intent(in) :: self
integer :: sz
sz = self%sz
end function
pure function get_dims(self, data_loc) result(dims)
!! Getter for local domain dimensions
class(mesh_t), intent(in) :: self
integer, intent(in) :: data_loc
integer, dimension(3) :: dims
dims = get_dims_dataloc(data_loc, self%vert_dims, self%cell_dims)
end function
pure function get_global_dims(self, data_loc) result(dims)
!! Getter for local domain dimensions
class(mesh_t), intent(in) :: self
integer, intent(in) :: data_loc
integer, dimension(3) :: dims
dims = get_dims_dataloc(data_loc, self%global_vert_dims, &
self%global_cell_dims)
end function
pure function get_dims_dataloc(data_loc, vert_dims, cell_dims) result(dims)
!! Getter for domain dimensions
integer, intent(in) :: data_loc
integer, dimension(3), intent(in) :: vert_dims, cell_dims
integer, dimension(3) :: dims
select case (data_loc)
case (VERT)
dims = vert_dims
case (CELL)
dims = cell_dims
case (X_FACE)
dims(1) = vert_dims(1)
dims(2:3) = cell_dims(2:3)
case (Y_FACE)
dims(1) = cell_dims(1)
dims(2) = vert_dims(2)
dims(3) = cell_dims(3)
case (Z_FACE)
dims(1:2) = cell_dims(1:2)
dims(3) = vert_dims(3)
case (X_EDGE)
dims(1) = cell_dims(1)
dims(2:3) = vert_dims(2:3)
case (Y_EDGE)
dims(1) = vert_dims(1)
dims(2) = cell_dims(2)
dims(3) = vert_dims(3)
case (Z_EDGE)
dims(1:2) = vert_dims(1:2)
dims(3) = cell_dims(3)
case (none)
error stop
end select
end function
pure function get_padded_dims_dir(self, dir) result(dims_padded)
!! Getter for padded dimensions with structure in `dir` direction
class(mesh_t), intent(in) :: self
integer, intent(in) :: dir
integer, dimension(3) :: dims_padded
if (dir == DIR_C) then
dims_padded = self%vert_dims_padded
else
dims_padded(1) = self%sz
dims_padded(2) = self%vert_dims_padded(dir)
dims_padded(3) = self%get_n_groups(dir)
end if
end function
pure function get_padded_dims_phi(self, phi) result(dims_padded)
!! Getter for padded dimensions for field phi
!! Gets the field direction from the field itself
class(mesh_t), intent(in) :: self
class(field_t), intent(in) :: phi
integer, dimension(3) :: dims_padded
dims_padded = self%get_padded_dims(phi%dir)
end function
pure function get_n_groups_dir(self, dir) result(n_groups)
!! Getter for the number of groups for fields in direction `dir`
class(mesh_t), intent(in) :: self
integer, intent(in) :: dir
integer :: n_groups
n_groups = (product(self%vert_dims_padded(:))/ &
self%vert_dims_padded(dir))/self%sz
end function
pure function get_n_groups_phi(self, phi) result(n_groups)
!! Getter for the number of groups for fields phi
class(mesh_t), intent(in) :: self
class(field_t), intent(in) :: phi
integer :: n_groups
n_groups = self%get_n_groups(phi%dir)
end function
pure function get_field_dims_phi(self, phi) result(dims)
!! Getter for the dimensions of field phi
class(mesh_t), intent(in) :: self
class(field_t), intent(in) :: phi
integer, dimension(3) :: dims
dims = self%get_field_dims(phi%dir, phi%data_loc)
end function
pure function get_field_dims_phi_dataloc(self, phi, data_loc) result(dims)
!! Getter for the dimensions of field phi where data is located on `data_loc`
class(mesh_t), intent(in) :: self
class(field_t), intent(in) :: phi
integer, intent(in) :: data_loc
integer, dimension(3) :: dims
dims = self%get_field_dims(phi%dir, data_loc)
end function
pure function get_field_dims_dir(self, dir, data_loc) result(dims)
!! Getter for the dimensions of an array directed along `dir` where data would be located on `data_loc`
class(mesh_t), intent(in) :: self
integer, intent(in) :: dir
integer, intent(in) :: data_loc
integer, dimension(3) :: dims
if (dir == DIR_C) then
dims(1) = self%get_n(DIR_X, data_loc)
dims(2) = self%get_n(DIR_Y, data_loc)
dims(3) = self%get_n(DIR_Z, data_loc)
else
dims(1) = self%sz
dims(2) = self%get_n(dir, data_loc)
dims(3) = self%get_n_groups(dir)
end if
end function
pure function get_n_phi(self, phi) result(n)
!! Getter for the main dimension of field phi
class(mesh_t), intent(in) :: self
class(field_t), intent(in) :: phi
integer :: n
n = self%get_n(phi%dir, phi%data_loc)
end function
pure function get_n_dir(self, dir, data_loc) result(n)
!! Getter for the main dimension a field oriented along `dir` with data on `data_loc`
class(mesh_t), intent(in) :: self
integer, intent(in) :: dir
integer, intent(in) :: data_loc
integer :: n, n_cell, n_vert
n_cell = self%cell_dims(dir)
n_vert = self%vert_dims(dir)
! default to n_vert
n = n_vert
select case (data_loc)
case (CELL)
n = n_cell
case (VERT)
n = n_vert
case (X_FACE)
if (dir /= DIR_X) then
n = n_cell
end if
case (Y_FACE)
if (dir /= DIR_Y) then
n = n_cell
end if
case (Z_FACE)
if (dir /= DIR_Z) then
n = n_cell
end if
case (X_EDGE)
if (dir == DIR_X) then
n = n_cell
end if
case (Y_EDGE)
if (dir == DIR_Y) then
n = n_cell
end if
case (Z_EDGE)
if (dir == DIR_Z) then
n = n_cell
end if
case (none)
error stop
end select
end function
pure function get_coordinates(self, i, j, k) result(xloc)
!! Get the physical location of a cell center with i,j,k local indices
class(mesh_t), intent(in) :: self
integer, intent(in) :: i, j, k
real(dp), dimension(3) :: xloc
xloc(1) = (i - 1 + self%par%n_offset(1))*self%geo%d(1)
xloc(2) = (j - 1 + self%par%n_offset(2))*self%geo%d(2)
xloc(3) = (k - 1 + self%par%n_offset(3))*self%geo%d(3)
end function
pure function is_root(self) result(is_root_rank)
!! Returns whether or not the current rank is the root rank
class(parallel_t), intent(in) :: self
logical :: is_root_rank
is_root_rank = (self%nrank == 0)
end function
end module m_mesh