Neko 1.99.10
A portable framework for high-order spectral element flow simulations
Loading...
Searching...
No Matches
case.f90
Go to the documentation of this file.
1! Copyright (c) 2020-2026, The Neko Authors
2! All rights reserved.
3!
4! Redistribution and use in source and binary forms, with or without
5! modification, are permitted provided that the following conditions
6! are met:
7!
8! * Redistributions of source code must retain the above copyright
9! notice, this list of conditions and the following disclaimer.
10!
11! * Redistributions in binary form must reproduce the above
12! copyright notice, this list of conditions and the following
13! disclaimer in the documentation and/or other materials provided
14! with the distribution.
15!
16! * Neither the name of the authors nor the names of its
17! contributors may be used to endorse or promote products derived
18! from this software without specific prior written permission.
19!
20! THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21! "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22! LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23! FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24! COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25! INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26! BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27! LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28! CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29! LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30! ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31! POSSIBILITY OF SUCH DAMAGE.
32!
33!> Defines a simulation case
34module case
35 use num_types, only : rp, sp, dp
36 use fluid_scheme_base, only: fluid_scheme_base_t, fluid_scheme_base_factory
38 use chkp_output, only : chkp_output_t
39 use mesh_field, only : mesh_fld_t
41 use redist, only : redist_mesh
43 use file, only : file_t
45 use mesh, only : mesh_t
46 use checkpoint, only: chkp_t
48 use logger, only : neko_log, neko_log_quiet
50 use user_intf, only : user_t
51 use scalar_pnpn, only : scalar_pnpn_t
53 use time_state, only : time_state_t
54 use json_module, only : json_file
59 use scalars, only : scalars_t
60 use comm, only : neko_comm, pe_rank, pe_size
61 use mpi_f08, only : mpi_bcast, mpi_character, mpi_integer, mpi_logical
63 use vector, only : vector_t
64
65 implicit none
66 private
67
68 type, public :: case_t
69 type(mesh_t) :: msh
70 type(json_file) :: params
71 character(len=:), allocatable :: output_directory
73 type(fluid_output_t) :: f_out
74 type(time_state_t) :: time
75 type(chkp_output_t) :: chkp_out
76 type(chkp_t) :: chkp
77 type(user_t) :: user
78 class(fluid_scheme_base_t), allocatable :: fluid
79 type(scalars_t), allocatable :: scalars
80 contains
81 procedure, private, pass(this) :: case_init_from_file
82 procedure, private, pass(this) :: case_init_from_json
83 procedure, pass(this) :: free => case_free
85 end type case_t
86
87contains
88
89 !> Initialize a case from an input file @a case_file
90 subroutine case_init_from_file(this, case_file)
91 class(case_t), target, intent(inout) :: this
92 character(len=*), intent(in) :: case_file
93 integer :: ierr, integer_val
94 character(len=:), allocatable :: json_buffer
95 logical :: exist
96
97 ! Check if the file exists
98 inquire(file = trim(case_file), exist = exist)
99 if (.not. exist) then
100 call neko_error('The case file '//trim(case_file)//' does not exist.')
101 end if
102
103 call neko_log%section('Case')
104 call neko_log%message('Reading case file ' // trim(case_file), &
106
107 call this%params%initialize()
108 if (pe_rank .eq. 0) then
109 call this%params%load_file(filename = trim(case_file))
110 call this%params%print_to_string(json_buffer)
111 integer_val = len(json_buffer)
112 end if
113
114 call mpi_bcast(integer_val, 1, mpi_integer, 0, neko_comm, ierr)
115 if (pe_rank .ne. 0) allocate(character(len = integer_val) :: json_buffer)
116 call mpi_bcast(json_buffer, integer_val, mpi_character, 0, neko_comm, ierr)
117 call this%params%load_from_string(json_buffer)
118
119 deallocate(json_buffer)
120
121 call case_init_common(this)
122
123 end subroutine case_init_from_file
124
125 !> Initialize a case from a JSON object describing a case
126 subroutine case_init_from_json(this, case_json)
127 class(case_t), target, intent(inout) :: this
128 type(json_file), intent(in) :: case_json
129
130 call neko_log%section('Case')
131 call neko_log%message('Creating case from JSON object', neko_log_quiet)
132
133 this%params = case_json
134
135 call case_init_common(this)
136
137 end subroutine case_init_from_json
138
139 !> Initialize a case from its (loaded) params object
140 subroutine case_init_common(this)
141 type(case_t), target, intent(inout) :: this
142 integer :: lx = 0
143 logical :: scalar = .false.
144 type(file_t) :: msh_file, bdry_file, part_file
145 type(mesh_fld_t) :: msh_part, parts
146 logical :: found, logical_val, load_balance
147 logical :: write_at_start
148 integer :: integer_val, var_type
149 real(kind=rp) :: real_val
150 real(kind=dp) :: double_val
151 real(kind=rp), allocatable :: real_vals(:)
152 type(vector_t), pointer :: vec
153 character(len=:), allocatable :: string_val, name, file_format
154 character(len=NEKO_FNAME_LEN) :: lb_file, lb_name, lb_path, lb_ext
155 integer :: output_dir_len
156 integer :: precision, layout
157 type(json_file) :: scalar_params, numerics_params
158 type(json_file) :: json_subdict
159 integer :: n_scalars, i
160 logical :: tmp_feature
161
162 !
163 ! Setup user defined functions
164 !
165 call this%user%init()
166
167 ! Run user startup routine
168 call this%user%startup(this%params)
169
170 ! Check if default value fill-in is allowed
171 if (this%params%valid_path('case.no_defaults')) then
172 call json_get(this%params, 'case.no_defaults', json_no_defaults)
173 end if
174
175
176 !
177 ! Populate const registry with global data from the case file
178 !
179 if (this%params%valid_path('case.constants')) then
180 call this%params%info('case.constants', &
181 n_children = integer_val)
182 do i = 1, integer_val
183 call json_extract_item(this%params, &
184 'case.constants', i, json_subdict)
185 call json_get(json_subdict, 'name', string_val)
186
187 call json_subdict%info('value', found = found, var_type = var_type)
188
189 select case (var_type)
190 case (5) ! integer
191 call json_get(json_subdict, 'value', integer_val)
192 call neko_const_registry%add_integer_scalar(integer_val, &
193 trim(string_val))
194 case (6) ! real
195 call json_get(json_subdict, 'value', real_val)
196 call neko_const_registry%add_real_scalar(real_val, &
197 trim(string_val))
198 case (3) ! array
199 call json_get(json_subdict, 'value', real_vals)
200 call neko_const_registry%add_vector(size(real_vals), &
201 trim(string_val))
202 vec => neko_const_registry%get_vector(trim(string_val))
203 vec%x = real_vals
204 case default
205 call neko_error('case_init_common: Unsupported constant ' // &
206 'type in case.constants for entry '//trim(string_val)//'.')
207 end select
208 end do
209 end if
210
211 !
212 ! Load mesh and perform load balancing if requested
213 !
214 call json_get_or_default(this%params, 'case.mesh_file', string_val, &
215 'no mesh')
216 call json_get_or_default(this%params, 'case.load_balancing', load_balance, &
217 .false.)
218
219 if (trim(string_val) .eq. 'no mesh') then
220 call neko_error('The mesh_file keyword could not be found in the .' // &
221 'case file. Often caused by incorrectly formatted json.')
222 end if
223
224 if (pe_rank .eq. 0) then
225 inquire(file = trim(string_val), exist = found)
226 end if
227 call mpi_bcast(found, 1, mpi_logical, 0, neko_comm)
228
229 if (.not. found) then
230 call neko_error('The mesh file ' // trim(string_val) // &
231 ' does not exist.')
232 end if
233
234 if (.not. load_balance .or. pe_size .eq. 1) then
235 if (load_balance) then
236 call neko_log%message('Load balancing requested but only one ' // &
237 'MPI rank found, ignoring.')
238 end if
239
240 call msh_file%init(string_val)
241 call msh_file%read(this%msh)
242
243 else if (load_balance) then
244 call neko_log%section('Load Balancing')
245
246 call filename_split(trim(string_val), lb_path, lb_name, lb_ext)
247 write(lb_file, '(A,A,A,I0,A)') &
248 trim(lb_path), trim(lb_name), "_lb_", pe_size, trim(lb_ext)
249
250 if (pe_rank .eq. 0) then
251 inquire(file = trim(lb_file), exist = found)
252 end if
253 call mpi_bcast(found, 1, mpi_logical, 0, neko_comm)
254
255 if (found) then
256 call neko_log%message('Reading balanced mesh')
257 call msh_file%init(lb_file)
258 call msh_file%read(this%msh)
259 else
260 call msh_file%init(string_val)
261 call msh_file%read(this%msh)
262
263 call neko_log%message('Performing load balancing with ParMETIS')
264 call parmetis_partmeshkway(this%msh, parts)
265 call redist_mesh(this%msh, parts)
266
267 ! store the balanced mesh (for e.g. restarts)
268 call msh_file%init(lb_file)
269 call msh_file%write(this%msh)
270 end if
271
272 call neko_log%end_section()
273 end if
274
275 ! Run user mesh motion routine
276 call this%user%mesh_setup(this%msh, this%time)
277
278 !
279 ! Time control
280 !
281 call json_get(this%params, 'case.time', json_subdict)
282 call this%time%init(json_subdict)
283
284 !
285 ! Initialize point_zones registry
286 !
287 call neko_point_zone_registry%init(this%params, this%msh)
288
289 !
290 ! Setup fluid scheme
291 !
292 call json_get(this%params, 'case.fluid.scheme', string_val)
293 call fluid_scheme_base_factory(this%fluid, trim(string_val))
294
295 call json_get_or_lookup(this%params, 'case.numerics.polynomial_order', lx)
296 lx = lx + 1 ! add 1 to get number of gll points
297 ! Set time lags in chkp
298 call this%chkp%init()
299 call this%chkp%add_time_state(this%time)
300 call this%fluid%init(this%msh, lx, this%params, this%user, this%chkp)
301
302
303 !
304 ! Setup scratch registry
305 !
306 call neko_scratch_registry%set_dofmap(this%fluid%dm_Xh)
307
308 !
309 ! Setup scalar scheme
310 !
311 ! @todo no scalar factory for now, probably not needed
312 scalar = .false.
313 n_scalars = 0
314 if (this%params%valid_path('case.scalar')) then
315 call json_get_or_default(this%params, 'case.scalar.enabled', scalar, &
316 .true.)
317 n_scalars = 1
318 else if (this%params%valid_path('case.scalars')) then
319 call this%params%info('case.scalars', n_children = n_scalars)
320 if (n_scalars > 0) then
321 scalar = .true.
322 end if
323 end if
324
325 if (scalar) then
326 allocate(this%scalars)
327 call json_get(this%params, 'case.numerics', numerics_params)
328 if (this%params%valid_path('case.scalar')) then
329 ! For backward compatibility
330 call json_get(this%params, 'case.scalar', scalar_params)
331 call this%scalars%init(this%msh, this%fluid%c_Xh, this%fluid%gs_Xh, &
332 scalar_params, numerics_params, this%user, this%chkp, &
333 this%fluid%ulag, this%fluid%vlag, this%fluid%wlag, &
334 this%fluid%ext_bdf, this%fluid%rho)
335 else
336 ! Multiple scalars
337 call json_get(this%params, 'case.scalars', json_subdict)
338 call this%scalars%init(n_scalars, this%msh, this%fluid%c_Xh, &
339 this%fluid%gs_Xh, json_subdict, numerics_params, this%user, &
340 this%chkp, this%fluid%ulag, this%fluid%vlag, this%fluid%wlag, &
341 this%fluid%ext_bdf, this%fluid%rho)
342 end if
343 end if
344
345 !
346 ! Setup initial conditions
347 !
348 call this%fluid%set_initial_condition(this%user, &
349 this%params%valid_path('case.restart_file'))
350
351 if (scalar) then
352 call this%scalars%set_initial_conditions(this%user, &
353 this%params%valid_path('case.restart_file'))
354 end if
355
356 !
357 ! Validate that the case is properly setup for time-stepping
358 !
359 call this%fluid%validate
360
361 if (scalar) then
362 call this%scalars%validate()
363 end if
364
365 !
366 ! Get and process output directory
367 !
368 call json_get_or_default(this%params, 'case.output_directory',&
369 this%output_directory, '')
370
371 output_dir_len = len(trim(this%output_directory))
372 if (output_dir_len .gt. 0) then
373 if (this%output_directory(output_dir_len:output_dir_len) .ne. "/") then
374 this%output_directory = trim(this%output_directory)//"/"
375 end if
376 if (pe_rank .eq. 0) then
377 call mkdir(trim(this%output_directory))
378 end if
379 end if
380
381 !
382 ! Save mesh partitions (if requested)
383 !
384 call json_get_or_default(this%params, 'case.output_partitions',&
385 logical_val, .false.)
386 if (logical_val) then
387 call msh_part%init(this%msh, 'MPI_Rank')
388 msh_part%data = pe_rank
389 call part_file%init(trim(this%output_directory)//'partitions.vtk')
390 call part_file%write(msh_part)
391 call msh_part%free()
392 end if
393
394 !
395 ! Setup output precision of the field files
396 !
397 call json_get_or_default(this%params, 'case.output_precision', string_val,&
398 'single')
399
400 if (trim(string_val) .eq. 'double') then
401 precision = dp
402 else
403 precision = sp
404 end if
405
406 !
407 ! Setup output layout of the field bp file
408 !
409 call json_get_or_lookup_or_default(this%params, 'case.output_layout', &
410 layout, 1)
411
412 !
413 ! Setup output_controller
414 !
415 call json_get_or_default(this%params, 'case.fluid.output_filename', &
416 name, "field")
417 call json_get_or_default(this%params, 'case.fluid.output_format', &
418 file_format, 'fld')
419 call json_get_or_default(this%params, &
420 'case.fluid.output_mesh_in_all_files', &
421 logical_val, .false.)
422
423 ! To ensure we don't miss saving the mesh for ALE.
424 call json_get_or_default(this%params, 'case.fluid.ale.enabled', &
425 tmp_feature, .false.)
426 if (tmp_feature) logical_val = .true.
427
428 ! Whether the initial state of the simulation is written.
429 call json_get_or_default(this%params, 'case.output_at_start', &
430 write_at_start, .true.)
431
432 call this%output_controller%init(this%time%end_time, &
433 time_start = this%time%start_time, write_at_start = write_at_start)
434 if (scalar) then
435 call this%f_out%init(precision, this%fluid, this%scalars, name = name, &
436 path = trim(this%output_directory), &
437 fmt = trim(file_format), layout = layout, &
438 always_write_mesh = logical_val)
439 else
440 call this%f_out%init(precision, this%fluid, name = name, &
441 path = trim(this%output_directory), &
442 fmt = trim(file_format), layout = layout, &
443 always_write_mesh = logical_val)
444 end if
445
446 call json_get_or_default(this%params, 'case.fluid.output_subdivide', &
447 logical_val, .false.)
448 call this%f_out%file_%set_subdivide(logical_val)
449
450 call json_get(this%params, 'case.fluid.output_control', string_val)
451
452 if (trim(string_val) .eq. 'org') then
453 ! yes, it should be real_val below for type compatibility
454 call json_get_or_lookup(this%params, 'case.nsamples', integer_val)
455 double_val = real(integer_val, kind=dp)
456 call this%output_controller%add(this%f_out, double_val, 'nsamples')
457 else if (trim(string_val) .eq. 'never') then
458 call this%output_controller%add(this%f_out, 0.0_dp, 'never')
459 else if (trim(string_val) .eq. 'tsteps' .or. &
460 trim(string_val) .eq. 'nsamples') then
461 call json_get_or_lookup(this%params, 'case.fluid.output_value', &
462 integer_val)
463 double_val = real(integer_val, kind=dp)
464 call this%output_controller%add(this%f_out, double_val, string_val)
465 else if (trim(string_val) .eq. 'simulationtime') then
466 call json_get_or_lookup(this%params, 'case.fluid.output_value', &
467 double_val)
468 call this%output_controller%add(this%f_out, double_val, string_val)
469 else
470 call neko_log%error('Unknown output control type for the fluid: ' // &
471 trim(string_val))
472 end if
473
474 !
475 ! Save checkpoints (if nothing specified, default to saving at end of sim)
476 !
477 call json_get(this%params, 'case.output_checkpoints', logical_val)
478 if (logical_val) then
479 call json_get_or_default(this%params, 'case.checkpoint_filename', &
480 name, "fluid")
481 call json_get_or_default(this%params, 'case.checkpoint_format', &
482 string_val, "chkp")
483 call this%chkp_out%init(this%chkp, name = name,&
484 path = this%output_directory, fmt = trim(string_val))
485 call json_get(this%params, 'case.checkpoint_control', &
486 string_val)
487 if (trim(string_val) .eq. 'tsteps' .or. &
488 trim(string_val) .eq. 'nsamples') then
489 call json_get_or_lookup(this%params, 'case.checkpoint_value', &
490 integer_val)
491 double_val = real(integer_val, kind=dp)
492 else if (trim(string_val) .eq. 'simulationtime') then
493 call json_get_or_lookup(this%params, 'case.checkpoint_value', &
494 double_val)
495 else if (trim(string_val) .eq. 'never') then
496 double_val = 0.0_rp
497 end if
498
499 ! A checkpoint of the initial condition is of no use, so the first
500 ! point of the schedule is skipped.
501 call this%output_controller%add(this%chkp_out, double_val, string_val, &
502 write_at_start = .false.)
503 end if
504
505 !
506 ! Setup joblimit
507 !
508 if (this%params%valid_path('case.job_timelimit')) then
509 call json_get(this%params, 'case.job_timelimit', string_val)
510 call jobctrl_set_time_limit(string_val)
511 end if
512
513 call neko_log%end_section()
514
515 call scalar_params%destroy()
516 call numerics_params%destroy()
517 call json_subdict%destroy()
518
519 nullify(vec)
520
521 end subroutine case_init_common
522
523 !> Deallocate a case
524 subroutine case_free(this)
525 class(case_t), intent(inout) :: this
526
527 if (allocated(this%fluid)) then
528 call this%fluid%free()
529 deallocate(this%fluid)
530 end if
531
532 if (allocated(this%scalars)) then
533 call this%scalars%free()
534 deallocate(this%scalars)
535 end if
536
537 call this%msh%free()
538
539 call this%f_out%free()
540
541 call this%output_controller%free()
542
543 if (allocated(this%output_directory)) then
544 deallocate(this%output_directory)
545 end if
546
547 end subroutine case_free
548
549end module case
double real
Retrieves a parameter by name or assigns a provided default value. In the latter case also adds the m...
Retrieves a parameter by name or throws an error.
Defines a simulation case.
Definition case.f90:34
subroutine case_init_from_file(this, case_file)
Initialize a case from an input file case_file.
Definition case.f90:91
subroutine case_free(this)
Deallocate a case.
Definition case.f90:525
subroutine case_init_from_json(this, case_json)
Initialize a case from a JSON object describing a case.
Definition case.f90:127
subroutine case_init_common(this)
Initialize a case from its (loaded) params object.
Definition case.f90:141
Defines format-independent checkpoint registration and restart state.
Defines an output for a checkpoint.
Definition comm.F90:1
integer, public pe_size
MPI size of communicator.
Definition comm.F90:62
integer, public pe_rank
MPI rank.
Definition comm.F90:59
type(mpi_comm), public neko_comm
MPI communicator.
Definition comm.F90:46
Module for file I/O operations.
Definition file.f90:34
Defines an output for a fluid.
Job control.
Definition jobctrl.f90:34
Utilities for retrieving parameters from the case files.
logical, public json_no_defaults
If true, the json_get_or_default routines will not add missing parameters.
Logging routines.
Definition log.f90:34
integer, parameter, public neko_log_quiet
Always.
Definition log.f90:52
type(log_t), public neko_log
Global log stream.
Definition log.f90:92
Defines a mesh field.
Defines a mesh.
Definition mesh.f90:34
integer, parameter, public dp
Definition num_types.f90:10
integer, parameter, public sp
Definition num_types.f90:8
integer, parameter, public rp
Global precision used in computations.
Definition num_types.f90:14
Implements output_controller_t.
Interface to ParMETIS.
Definition parmetis.F90:34
subroutine, public parmetis_partmeshkway(msh, parts, weights, nprts)
Compute a k-way partitioning of a mesh msh.
Definition parmetis.F90:112
type(point_zone_registry_t), target, public neko_point_zone_registry
Global point_zone registry.
Redistribution routines.
Definition redist.f90:34
subroutine, public redist_mesh(msh, parts)
Redistribute a mesh msh according to new partitions.
Definition redist.f90:59
Defines a registry for storing solution fields.
Definition registry.f90:34
type(registry_t), target, public neko_registry
Global field registry.
Definition registry.f90:158
type(registry_t), target, public neko_const_registry
This registry is used to store user-defined scalars and vectors, provided under the constants section...
Definition registry.f90:164
Contains the scalar_pnpn_t type.
Contains the scalar_scheme_t type.
Contains the scalars_t type that manages multiple scalar fields.
Definition scalars.f90:35
Defines a registry for storing and requesting temporary objects This can be used when you have a func...
type(scratch_registry_t), target, public neko_scratch_registry
Global scratch registry.
Compound scheme for the advection and diffusion operators in a transport equation.
Module with things related to the simulation time.
Interfaces for user interaction with NEKO.
Definition user_intf.f90:34
Utilities.
Definition utils.f90:35
integer, parameter, public neko_fname_len
Definition utils.f90:42
subroutine, public filename_split(fname, path, name, suffix)
Extract file name components.
Definition utils.f90:170
recursive subroutine, public mkdir(path, mode)
Recursively create a directory and all parent directories if they do not exist. This should be safer ...
Definition utils.f90:213
Defines a vector.
Definition vector.f90:34
Collection of live simulation data registered for checkpointing.
A wrapper around a polymorphic generic_file_t that handles its init. This is essentially a factory fo...
Definition file.f90:56
Base type of all fluid formulations.
Centralized controller for a list of outputs.
Base type for a scalar advection-diffusion solver.
Type to manage multiple scalar transport equations.
Definition scalars.f90:60
Implements the logic to compute the time coefficients for the advection and diffusion operators in a ...
A struct that contains all info about the time, expand as needed.
A type collecting all the overridable user routines and flag to suppress type injection from custom m...