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