Neko 1.99.11
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, output_boundary
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) :: fluid_params, 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 call json_get(this%params, 'case.fluid', fluid_params)
298 call json_get(this%params, 'case.numerics', numerics_params)
299 call json_get_or_default(this%params, 'case.output_boundary', &
300 output_boundary, .false.)
301 ! Set time lags in chkp
302 call this%chkp%init()
303 call this%chkp%add_time_state(this%time)
304 call this%fluid%init(this%msh, lx, fluid_params, numerics_params, &
305 this%user, this%chkp, output_boundary)
306
307
308 !
309 ! Setup scratch registry
310 !
311 call neko_scratch_registry%set_dofmap(this%fluid%dm_Xh)
312
313 !
314 ! Setup scalar scheme
315 !
316 ! @todo no scalar factory for now, probably not needed
317 scalar = .false.
318 n_scalars = 0
319 if (this%params%valid_path('case.scalar')) then
320 call json_get_or_default(this%params, 'case.scalar.enabled', scalar, &
321 .true.)
322 n_scalars = 1
323 else if (this%params%valid_path('case.scalars')) then
324 call this%params%info('case.scalars', n_children = n_scalars)
325 if (n_scalars > 0) then
326 scalar = .true.
327 end if
328 end if
329
330 if (scalar) then
331 allocate(this%scalars)
332 if (this%params%valid_path('case.scalar')) then
333 ! For backward compatibility
334 call json_get(this%params, 'case.scalar', scalar_params)
335 call this%scalars%init(this%msh, this%fluid%c_Xh, this%fluid%gs_Xh, &
336 scalar_params, numerics_params, this%user, this%chkp, &
337 this%fluid%ulag, this%fluid%vlag, this%fluid%wlag, &
338 this%fluid%ext_bdf, this%fluid%rho)
339 else
340 ! Multiple scalars
341 call json_get(this%params, 'case.scalars', json_subdict)
342 call this%scalars%init(n_scalars, this%msh, this%fluid%c_Xh, &
343 this%fluid%gs_Xh, json_subdict, numerics_params, this%user, &
344 this%chkp, this%fluid%ulag, this%fluid%vlag, this%fluid%wlag, &
345 this%fluid%ext_bdf, this%fluid%rho)
346 end if
347 end if
348
349 !
350 ! Setup initial conditions
351 !
352 call this%fluid%set_initial_condition(fluid_params, this%user, &
353 this%params%valid_path('case.restart_file'))
354
355 if (scalar) then
356 if (this%params%valid_path('case.scalar')) then
357 call this%scalars%set_initial_conditions(scalar_params, this%user, &
358 this%params%valid_path('case.restart_file'))
359 else
360 call this%scalars%set_initial_conditions(json_subdict, this%user, &
361 this%params%valid_path('case.restart_file'))
362 end if
363 end if
364
365 !
366 ! Validate that the case is properly setup for time-stepping
367 !
368 call this%fluid%validate
369
370 if (scalar) then
371 call this%scalars%validate()
372 end if
373
374 !
375 ! Get and process output directory
376 !
377 call json_get_or_default(this%params, 'case.output_directory',&
378 this%output_directory, '')
379
380 output_dir_len = len(trim(this%output_directory))
381 if (output_dir_len .gt. 0) then
382 if (this%output_directory(output_dir_len:output_dir_len) .ne. "/") then
383 this%output_directory = trim(this%output_directory)//"/"
384 end if
385 if (pe_rank .eq. 0) then
386 call mkdir(trim(this%output_directory))
387 end if
388 end if
389
390 !
391 ! Save mesh partitions (if requested)
392 !
393 call json_get_or_default(this%params, 'case.output_partitions',&
394 logical_val, .false.)
395 if (logical_val) then
396 call msh_part%init(this%msh, 'MPI_Rank')
397 msh_part%data = pe_rank
398 call part_file%init(trim(this%output_directory)//'partitions.vtk')
399 call part_file%write(msh_part)
400 call msh_part%free()
401 end if
402
403 !
404 ! Setup output precision of the field files
405 !
406 call json_get_or_default(this%params, 'case.output_precision', string_val,&
407 'single')
408
409 if (trim(string_val) .eq. 'double') then
410 precision = dp
411 else
412 precision = sp
413 end if
414
415 !
416 ! Setup output layout of the field bp file
417 !
418 call json_get_or_lookup_or_default(this%params, 'case.output_layout', &
419 layout, 1)
420
421 !
422 ! Setup output_controller
423 !
424 call json_get_or_default(this%params, 'case.fluid.output_filename', &
425 name, "field")
426 call json_get_or_default(this%params, 'case.fluid.output_format', &
427 file_format, 'fld')
428 call json_get_or_default(this%params, &
429 'case.fluid.output_mesh_in_all_files', &
430 logical_val, .false.)
431
432 ! To ensure we don't miss saving the mesh for ALE.
433 call json_get_or_default(this%params, 'case.fluid.ale.enabled', &
434 tmp_feature, .false.)
435 if (tmp_feature) logical_val = .true.
436
437 ! Whether the initial state of the simulation is written.
438 call json_get_or_default(this%params, 'case.output_at_start', &
439 write_at_start, .true.)
440
441 call this%output_controller%init(this%time%end_time, &
442 time_start = this%time%start_time, write_at_start = write_at_start)
443 if (scalar) then
444 call this%f_out%init(precision, this%fluid, this%scalars, name = name, &
445 path = trim(this%output_directory), &
446 fmt = trim(file_format), layout = layout, &
447 always_write_mesh = logical_val)
448 else
449 call this%f_out%init(precision, this%fluid, name = name, &
450 path = trim(this%output_directory), &
451 fmt = trim(file_format), layout = layout, &
452 always_write_mesh = logical_val)
453 end if
454
455 call json_get_or_default(this%params, 'case.fluid.output_subdivide', &
456 logical_val, .false.)
457 call this%f_out%file_%set_subdivide(logical_val)
458
459 call json_get(this%params, 'case.fluid.output_control', string_val)
460
461 if (trim(string_val) .eq. 'org') then
462 ! yes, it should be real_val below for type compatibility
463 call json_get_or_lookup(this%params, 'case.nsamples', integer_val)
464 double_val = real(integer_val, kind=dp)
465 call this%output_controller%add(this%f_out, double_val, 'nsamples')
466 else if (trim(string_val) .eq. 'never') then
467 call this%output_controller%add(this%f_out, 0.0_dp, 'never')
468 else if (trim(string_val) .eq. 'tsteps' .or. &
469 trim(string_val) .eq. 'nsamples') then
470 call json_get_or_lookup(this%params, 'case.fluid.output_value', &
471 integer_val)
472 double_val = real(integer_val, kind=dp)
473 call this%output_controller%add(this%f_out, double_val, string_val)
474 else if (trim(string_val) .eq. 'simulationtime') then
475 call json_get_or_lookup(this%params, 'case.fluid.output_value', &
476 double_val)
477 call this%output_controller%add(this%f_out, double_val, string_val)
478 else
479 call neko_log%error('Unknown output control type for the fluid: ' // &
480 trim(string_val))
481 end if
482
483 !
484 ! Save checkpoints (if nothing specified, default to saving at end of sim)
485 !
486 call json_get(this%params, 'case.output_checkpoints', logical_val)
487 if (logical_val) then
488 call json_get_or_default(this%params, 'case.checkpoint_filename', &
489 name, "fluid")
490 call json_get_or_default(this%params, 'case.checkpoint_format', &
491 string_val, "chkp")
492 call this%chkp_out%init(this%chkp, name = name,&
493 path = this%output_directory, fmt = trim(string_val))
494 call json_get(this%params, 'case.checkpoint_control', &
495 string_val)
496 if (trim(string_val) .eq. 'tsteps' .or. &
497 trim(string_val) .eq. 'nsamples') then
498 call json_get_or_lookup(this%params, 'case.checkpoint_value', &
499 integer_val)
500 double_val = real(integer_val, kind=dp)
501 else if (trim(string_val) .eq. 'simulationtime') then
502 call json_get_or_lookup(this%params, 'case.checkpoint_value', &
503 double_val)
504 else if (trim(string_val) .eq. 'never') then
505 double_val = 0.0_rp
506 end if
507
508 ! A checkpoint of the initial condition is of no use, so the first
509 ! point of the schedule is skipped.
510 call this%output_controller%add(this%chkp_out, double_val, string_val, &
511 write_at_start = .false.)
512 end if
513
514 !
515 ! Setup joblimit
516 !
517 if (this%params%valid_path('case.job_timelimit')) then
518 call json_get(this%params, 'case.job_timelimit', string_val)
519 call jobctrl_set_time_limit(string_val)
520 end if
521
522 call neko_log%end_section()
523
524 call scalar_params%destroy()
525 call numerics_params%destroy()
526 call json_subdict%destroy()
527
528 nullify(vec)
529
530 end subroutine case_init_common
531
532 !> Deallocate a case
533 subroutine case_free(this)
534 class(case_t), intent(inout) :: this
535
536 if (allocated(this%fluid)) then
537 call this%fluid%free()
538 deallocate(this%fluid)
539 end if
540
541 if (allocated(this%scalars)) then
542 call this%scalars%free()
543 deallocate(this%scalars)
544 end if
545
546 call this%msh%free()
547
548 call this%f_out%free()
549
550 call this%output_controller%free()
551
552 if (allocated(this%output_directory)) then
553 deallocate(this%output_directory)
554 end if
555
556 end subroutine case_free
557
558end 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:534
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:61
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...