Neko 1.99.9
A portable framework for high-order spectral element flow simulations
Loading...
Searching...
No Matches
genmeshbox.f90
Go to the documentation of this file.
1
4program genmeshbox
5 use neko
6 implicit none
7
8 character(len=NEKO_FNAME_LEN) :: inputchar, fname = 'box.nmsh'
9 real(kind=dp) :: x0, x1
10 real(kind=dp) :: y0, y1
11 real(kind=dp) :: z0, z1
12 real(kind=dp) :: coord(3)
13 integer :: nelx, nely, nelz, nel
14 type(mesh_t) :: msh
15 type(file_t) :: nmsh_file
16 type(htable_pt_t) :: htable_pts
17 logical :: period_x, period_y, period_z
18 type(point_t) :: p(2,2,2)
19 type(tuple4_i4_t) :: facet_ord
20 integer :: argc, gdim = 3
21 integer :: el_idx, p_el_idx, pt_idx
22 integer :: i, zone_id, e_x, e_y, e_z, ix, iy, iz, e_id
23 real(kind=dp), allocatable :: el_len_x(:), el_len_y(:), el_len_z(:)
24 real(kind=dp), allocatable :: cumm_x(:), cumm_y(:), cumm_z(:)
25 type(vector_t) :: dist_x, dist_y, dist_z
26 type(file_t) :: dist_x_file, dist_y_file, dist_z_file
27 character(len = 80) :: dist_x_fname, dist_y_fname, dist_z_fname
28 character(len=80) :: log_fname = "genmeshbox.log"
29 integer :: log_unit
30 logical :: file_exists
31
32 argc = command_argument_count()
33
34 if ((argc .lt. 9) .or. (argc .gt. 15)) then
35 write(*,*) 'Usage: ./genmeshbox x0 x1 y0 y1 z0 z1 nel_x nel_y nel_z'
36 write(*,*) '**optional** periodic in x? (.true./.false.) y? ', &
37 '(.true./.false.) z? (.true./.false.)'
38 write(*,*) '**optional** Point distribution in x? (fname/uniform) y? ', &
39 '(fname/uniform) z? (fname/uniform)'
40 write(*,*)
41 write(*,*) 'Example command: ./genmeshbox 0 1 0 1 0 1 8 8 8 ', &
42 '.true. .true. .false.'
43 write(*,*) 'This examples generates a cube (box.nmsh) with side length ', &
44 '1 and with 8 elements in each spatial direction and periodic ', &
45 'boundaries in x-y.'
46 write(*,*) 'BCs for face 5,6 (z zones) can then be set by setting ', &
47 'boundary_conditions in the parameter file'
48 write(*,*) 'If you want a specific distribution of vertices in the ', &
49 'directions, give the filename where it is stored'
50 write(*,*) 'Example command: ./genmeshbox 0 1 0 1 0 1 8 8 8 ', &
51 '.false. .false. .false. uniform uniform dist.csv'
52 write(*,*) 'This example uses the vertex distribution in the z ', &
53 'direction given in dist.csv and keep the other'
54 write(*,*) 'directions uniform'
55 stop
56 end if
57
58 call neko_init
59 msh%lgenc = .false.
60 period_x = .false.
61 period_y = .false.
62 period_z = .false.
63 dist_x_fname = 'uniform'
64 dist_y_fname = 'uniform'
65 dist_z_fname = 'uniform'
66
67 call get_command_argument(1, inputchar)
68 read(inputchar, *) x0
69 call get_command_argument(2, inputchar)
70 read(inputchar, *) x1
71 call get_command_argument(3, inputchar)
72 read(inputchar, *) y0
73 call get_command_argument(4, inputchar)
74 read(inputchar, *) y1
75 call get_command_argument(5, inputchar)
76 read(inputchar, *) z0
77 call get_command_argument(6, inputchar)
78 read(inputchar, *) z1
79 call get_command_argument(7, inputchar)
80 read(inputchar, *) nelx
81 call get_command_argument(8, inputchar)
82 read(inputchar, *) nely
83 call get_command_argument(9, inputchar)
84 read(inputchar, *) nelz
85 if (argc .gt. 9) then
86 call get_command_argument(10, inputchar)
87 read(inputchar, *) period_x
88 call get_command_argument(11, inputchar)
89 read(inputchar, *) period_y
90 call get_command_argument(12, inputchar)
91 read(inputchar, *) period_z
92 end if
93 if (argc .gt. 12) then
94 call get_command_argument(13, inputchar)
95 read(inputchar, *) dist_x_fname
96 call get_command_argument(14, inputchar)
97 read(inputchar, *) dist_y_fname
98 call get_command_argument(15, inputchar)
99 read(inputchar, *) dist_z_fname
100 end if
101
102 ! Write a log of what parameters we used with genmeshbox
103 if (pe_rank .eq. 0) then
104
105 do i = 1, 1000
106
107 inquire(file = trim(log_fname), exist = file_exists)
108 if (.not. file_exists) then
109
110 open(newunit = log_unit, file = trim(log_fname), status = 'new', &
111 action = 'write')
112 write (log_unit, '(A,2(F12.6," "),I4,L2)') &
113 "xmin, xmax, Nel, periodic:", x0, x1, nelx, period_x
114 write (log_unit, '(A,2(F12.6," "),I4,L2)') &
115 "ymin, ymax, Nel, periodic:", y0, y1, nely, period_y
116 write (log_unit, '(A,2(F12.6," "),I4,L2)') &
117 "zmin, zmax, Nel, periodic:", z0, z1, nelz, period_z
118 close(log_unit)
119 exit
120
121 end if
122
123 ! if the original genmeshbox.log does not exist, we create new
124 ! files with genmeshbox.log.1, .2, .3, etc
125 if (i .lt. 10) then
126 write(log_fname, '(A,I1,A)') "genmeshbox_", i, ".log"
127 else if (i .lt. 100) then
128 write(log_fname, '(A,I2,A)') "genmeshbox_", i, ".log"
129 else if (i .lt. 1000) then
130 write(log_fname, '(A,I3,A)') "genmeshbox_", i, ".log"
131 else
132 write(log_fname, '(A,I4,A)') "genmeshbox_", i, ".log"
133 end if
134
135 end do
136 end if
137
138 nel = nelx*nely*nelz
139 call msh%init(gdim, nel)
140 call htable_pts%init( nel, gdim)
141
142 allocate(el_len_x(nelx), el_len_y(nely), el_len_z(nelz))
143 allocate(cumm_x(nelx+1), cumm_y(nely+1), cumm_z(nelz+1))
144
145 if (trim(dist_x_fname) .eq. 'uniform') then
146 el_len_x(:) = (x1 - x0)/nelx
147 else
148 call dist_x_file%init(trim(dist_x_fname))
149 call dist_x%init(nelx+1)
150 call dist_x_file%read(dist_x)
151 do i = 1, nelx
152 el_len_x(i) = dist_x%x(i+1) - dist_x%x(i)
153 end do
154 call file_free(dist_x_file)
155 call dist_x%free()
156 end if
157
158 if (trim(dist_y_fname) .eq. 'uniform') then
159 el_len_y(:) = (y1 - y0)/nely
160 else
161 call dist_y_file%init(trim(dist_y_fname))
162 call dist_y%init(nely+1)
163 call dist_y_file%read(dist_y)
164 do i = 1, nely
165 el_len_y(i) = dist_y%x(i+1) - dist_y%x(i)
166 end do
167 call file_free(dist_y_file)
168 call dist_y%free()
169 end if
170
171 if (trim(dist_z_fname) .eq. 'uniform') then
172 el_len_z(:) = (z1 - z0)/nelz
173 else
174 call dist_z_file%init(trim(dist_z_fname))
175 call dist_z%init(nelz+1)
176 call dist_z_file%read(dist_z)
177 do i = 1, nelz
178 el_len_z(i) = dist_z%x(i+1) - dist_z%x(i)
179 end do
180 call file_free(dist_z_file)
181 call dist_z%free()
182 end if
183
184 cumm_x(1) = x0
185 do i = 1, nelx
186 cumm_x(i+1) = cumm_x(i) + el_len_x(i)
187 end do
188
189 cumm_y(1) = y0
190 do i = 1, nely
191 cumm_y(i+1) = cumm_y(i) + el_len_y(i)
192 end do
193
194 cumm_z(1) = z0
195 do i = 1, nelz
196 cumm_z(i+1) = cumm_z(i) + el_len_z(i)
197 end do
198
199 i = 1
200 do e_z = 0, (nelz-1)
201 do e_y = 0, (nely-1)
202 do e_x = 0, (nelx-1)
203 do iz = 0,1
204 do iy = 0,1
205 do ix = 0,1
206 coord(1) = cumm_x(e_x + 1 + ix)
207 coord(2) = cumm_y(e_y + 1 + iy)
208 coord(3) = cumm_z(e_z + 1 + iz)
209 pt_idx = 1 + (ix + e_x) + (iy + e_y)*(nelx + 1) + &
210 (iz + e_z)*(nelx + 1)*(nely + 1)
211 call p(ix+1, iy+1, iz+1)%init(coord, pt_idx)
212 end do
213 end do
214 end do
215 call msh%add_element(i, i, p(1,1,1), p(2,1,1),p(1,2,1),p(2,2,1),&
216 p(1,1,2), p(2,1,2), p(1,2,2), p(2,2,2))
217 i = i + 1
218 end do
219 end do
220 end do
221
222 !Compute x zones
223 do zone_id = 1,2
224 do e_y = 0, (nely-1)
225 do e_z = 0, (nelz-1)
226 e_id = 1+e_y*nelx+e_z*nely*nelx
227 if (zone_id .eq. 1) then
228 el_idx = 1+e_y*nelx+e_z*nely*nelx
229 p_el_idx = 1+(nelx-1)+e_y*nelx+e_z*nely*nelx
230 else
231 p_el_idx = 1+e_y*nelx+e_z*nely*nelx
232 el_idx = 1+(nelx-1)+e_y*nelx+e_z*nely*nelx
233 end if
234 if (period_x) then
235 call msh%elements(e_id)%e%facet_order(facet_ord, zone_id)
236 call msh%mark_periodic_facet(zone_id, el_idx, &
237 1+mod(zone_id,2), p_el_idx, facet_ord%x)
238 else
239 call msh%mark_labeled_facet(zone_id, el_idx, zone_id)
240 end if
241 end do
242 end do
243 end do
244
245 !Compute y zones
246 do zone_id = 3,4
247 do e_x = 0, (nelx-1)
248 do e_z = 0, (nelz-1)
249 e_id = 1+e_x+e_z*nely*nelx
250 if (zone_id .eq. 3) then
251 el_idx = 1+e_x+e_z*nely*nelx
252 p_el_idx = 1+e_x+(nely-1)*nelx+e_z*nely*nelx
253 else
254 el_idx = 1+e_x+(nely-1)*nelx+e_z*nely*nelx
255 p_el_idx = 1+e_x+e_z*nely*nelx
256 end if
257 if (period_y) then
258 call msh%elements(e_id)%e%facet_order(facet_ord,3)
259 call msh%mark_periodic_facet(zone_id, el_idx, &
260 3+mod(zone_id,2), p_el_idx, facet_ord%x)
261 else
262 call msh%mark_labeled_facet(zone_id, el_idx, zone_id)
263 end if
264 end do
265 end do
266 end do
267
268 !Compute z zones
269 do zone_id = 5,6
270 do e_x = 0, (nelx-1)
271 do e_y = 0, (nely-1)
272 e_id = 1+e_x+e_y*nelx
273 if (zone_id .eq. 5) then
274 el_idx = 1+e_x+e_y*nelx
275 p_el_idx = 1+e_x+e_y*nelx+(nelz-1)*nely*nelx
276 else
277 p_el_idx = 1+e_x+e_y*nelx
278 el_idx = 1+e_x+e_y*nelx+(nelz-1)*nely*nelx
279 end if
280 if (period_z) then
281 call msh%elements(e_id)%e%facet_order(facet_ord,5)
282 call msh%mark_periodic_facet(zone_id, el_idx, &
283 5+mod(zone_id,2), p_el_idx, facet_ord%x)
284 else
285 call msh%mark_labeled_facet(zone_id, el_idx, zone_id)
286 end if
287 end do
288 end do
289 end do
290 ! No elegance just brute force...
291 ! To setup the correct global ids for the periodic points
292 if (period_x) then
293 do zone_id = 1,2
294 do e_y = 0, (nely-1)
295 do e_z = 0, (nelz-1)
296 e_id = 1+e_y*nelx+e_z*nely*nelx
297 if (zone_id .eq. 1) then
298 el_idx = 1 + e_y*nelx + e_z*nely*nelx
299 p_el_idx = 1 + (nelx-1) + e_y*nelx + e_z*nely*nelx
300 else
301 p_el_idx = 1 + e_y*nelx + e_z*nely*nelx
302 el_idx = 1 + (nelx-1) + e_y*nelx + e_z*nely*nelx
303 end if
304 call msh%create_periodic_ids(zone_id, el_idx, &
305 1+mod(zone_id,2), p_el_idx)
306 end do
307 end do
308 end do
309 end if
310
311 if (period_y) then
312 !Compute y zones
313 do zone_id = 3,4
314 do e_x = 0, (nelx-1)
315 do e_z = 0, (nelz-1)
316 e_id = 1+e_x+e_z*nely*nelx
317 if (zone_id .eq. 3) then
318 el_idx = 1+e_x+e_z*nely*nelx
319 p_el_idx = 1+e_x+(nely-1)*nelx+e_z*nely*nelx
320 else
321 el_idx = 1+e_x+(nely-1)*nelx+e_z*nely*nelx
322 p_el_idx = 1+e_x+e_z*nely*nelx
323 end if
324 call msh%create_periodic_ids(zone_id, el_idx, &
325 3+mod(zone_id,2), p_el_idx)
326 end do
327 end do
328 end do
329 end if
330
331 if (period_z) then
332 do zone_id = 5,6
333 do e_x = 0, (nelx-1)
334 do e_y = 0, (nely-1)
335 e_id = 1+e_x+e_y*nelx
336 if (zone_id .eq. 5) then
337 el_idx = 1+e_x+e_y*nelx
338 p_el_idx = 1+e_x+e_y*nelx+(nelz-1)*nely*nelx
339 else
340 p_el_idx = 1+e_x+e_y*nelx
341 el_idx = 1+e_x+e_y*nelx+(nelz-1)*nely*nelx
342 end if
343 call msh%create_periodic_ids(zone_id, el_idx, &
344 5+mod(zone_id,2), p_el_idx)
345 end do
346 end do
347 end do
348 end if
349
350 call msh%finalize()
351
352
353 call nmsh_file%init(fname)
354
355 call nmsh_file%write(msh)
356
357 call msh%free()
358
359 call neko_finalize
360
361end program genmeshbox
program genmeshbox
Simple program to generate a mesh for a box Gives some insight into how one can use built in function...
Definition genmeshbox.f90:4
Module for file I/O operations.
Definition file.f90:34
Master module.
Definition neko.f90:34
Neko binary mesh data.
Definition nmsh_file.f90:34
void neko_finalize()
void neko_init()