Neko 1.99.11
A portable framework for high-order spectral element flow simulations
Loading...
Searching...
No Matches
scalars.f90
Go to the documentation of this file.
1! Copyright (c) 2022-2025, 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!> Contains the scalars_t type that manages multiple scalar fields.
34
35module scalars
36 use num_types, only : rp
39 use mesh, only : mesh_t
40 use space, only : space_t
41 use gather_scatter, only : gs_t
44 use json_module, only : json_file, json_array
46 use field, only : field_t
47 use field_list, only : field_list_t
49 use checkpoint, only : chkp_t
50 use krylov, only : ksp_t, ksp_monitor_t
52 use user_intf, only : user_t
53 use utils, only : neko_error
54 use coefs, only : coef_t
55 use time_state, only : time_state_t
57 implicit none
58 private
59
60 !> Type to manage multiple scalar transport equations
61 type, public :: scalars_t
62 !> The scalar fields
63 type(scalar_scheme_wrapper_t), allocatable :: scalar_fields(:)
64 !> Shared KSP solver for all scalar fields
65 class(ksp_t), allocatable :: shared_ksp
66 contains
67 !> Initialize the scalars container
69 procedure, private :: scalars_init
70 procedure, private :: scalars_init_single
71 !> Perform a time step for all scalar fields
72 procedure :: step => scalars_step
73 !> Restart from checkpoint data
74 procedure :: restart => scalars_restart
75 !> Set initial conditions for all scalar fields
76 procedure :: set_initial_conditions => scalars_set_initial_conditions
77 !> Check if the configuration is valid
78 procedure :: validate => scalars_validate
79 !> Clean up all resources
80 procedure :: free => scalars_free
81 end type scalars_t
82
83contains
84
85 !> Initialize the scalars container
86 subroutine scalars_init(this, n_scalars, msh, coef, gs, params, &
87 numerics_params, user, chkp, ulag, vlag, wlag, time_scheme, rho)
88 class(scalars_t), intent(inout) :: this
89 integer, intent(in) :: n_scalars
90 type(mesh_t), target, intent(in) :: msh
91 type(coef_t), target, intent(in) :: coef
92 type(gs_t), target, intent(inout) :: gs
93 type(json_file), target, intent(inout) :: params
94 type(json_file), target, intent(inout) :: numerics_params
95 type(user_t), target, intent(in) :: user
96 type(field_series_t), target, intent(in) :: ulag, vlag, wlag
97 type(time_scheme_controller_t), target, intent(in) :: time_scheme
98 TYPE(field_t), TARGET, INTENT(IN) :: rho
99 type(chkp_t), target, intent(inout) :: chkp
100 type(json_file) :: json_subdict
101 integer :: i, j
102 character(len=:), allocatable :: field_name
103 character(len=:), allocatable :: field_names(:)
104 character(len=256) :: error_msg, buffer
105
106 ! Allocate the array of the scalar scheme wrappers
107 allocate(this%scalar_fields(n_scalars))
108
109 ! Collect and validate field names for all scalars
110 allocate(character(len=256) :: field_names(n_scalars))
111
112 do i = 1, n_scalars
113 ! Extract element i from the "scalars" array
114 call json_extract_item(params, "", i, json_subdict)
115
116 ! Try to get name from JSON, generate one if not found or empty
117 call json_get_or_default(json_subdict, 'name', field_name, '')
118
119 ! If name is empty or not provided, generate a default one
120 if (len_trim(field_name) == 0) then
121 if (n_scalars == 1) then
122 field_name = 's' ! Single scalar gets default name 's'
123 else
124 write(buffer, '(A,I0)') 's_', i
125 field_name = trim(buffer)
126 end if
127 end if
128
129 field_names(i) = trim(field_name)
130
131 ! If there's a duplicate, append a number until unique
132 if (n_scalars > 1) then
133 j = 1
134 do while (j < i)
135 if (trim(field_names(i)) == trim(field_names(j))) then
136 call neko_error("Duplicate scalar field name detected: "// &
137 trim(field_names(i)) // &
138 ". Please provide unique names for each scalar field.")
139 else
140 j = j + 1
141 end if
142 end do
143 end if
144 end do
145
146 do i = 1, n_scalars
147 call json_extract_item(params, "", i, json_subdict)
148
149 ! Use the processed field names for all scalars
150 call json_subdict%add('name', trim(field_names(i)))
151
152 ! Allocate the scalar fields
153 call this%scalar_fields(i)%init(msh, coef, gs, json_subdict, &
154 numerics_params, user, chkp, ulag, vlag, wlag, time_scheme, rho)
155 end do
156 end subroutine scalars_init
157
158 subroutine scalars_init_single(this, msh, coef, gs, params, numerics_params, &
159 user, chkp, ulag, vlag, wlag, time_scheme, rho)
160 class(scalars_t), intent(inout) :: this
161 type(mesh_t), target, intent(in) :: msh
162 type(coef_t), target, intent(in) :: coef
163 type(gs_t), target, intent(inout) :: gs
164 type(json_file), target, intent(inout) :: params
165 type(json_file), target, intent(inout) :: numerics_params
166 type(user_t), target, intent(in) :: user
167 type(chkp_t), target, intent(inout) :: chkp
168 type(field_series_t), target, intent(in) :: ulag, vlag, wlag
169 type(time_scheme_controller_t), target, intent(in) :: time_scheme
170 TYPE(field_t), TARGET, INTENT(IN) :: rho
171
172 ! Allocate a single scalar field
173 allocate(this%scalar_fields(1))
174
175 ! Set the scalar name to "s"
176 if (.not. params%valid_path('name')) then
177 call params%add('name', 's')
178 end if
179
180 ! Initialize it directly with the params
181 call this%scalar_fields(1)%init(msh, coef, gs, params, numerics_params, &
182 user, chkp, ulag, vlag, wlag, time_scheme, rho)
183 end subroutine scalars_init_single
184
185 !> Perform a time step for all scalar fields
186 subroutine scalars_step(this, time, ext_bdf, dt_controller)
187 class(scalars_t), intent(inout) :: this
188 type(time_state_t), intent(in) :: time
189 type(time_scheme_controller_t), intent(inout) :: ext_bdf
190 type(time_step_controller_t), intent(inout) :: dt_controller
191 integer :: i
192 type(ksp_monitor_t), dimension(size(this%scalar_fields)) :: ksp_results
193 logical :: all_frozen
194
195 all_frozen = .true.
196
197 ! Iterate through all scalar fields, in one fixed-id region like the
198 ! fluid's, so all scalars are profiled together as well as one by one
199 call profiler_start_region('Scalar', 2)
200 do i = 1, size(this%scalar_fields)
201 all_frozen = all_frozen .and. this%scalar_fields(i)%scalar%freeze
202 call this%scalar_fields(i)%scalar%step(time, ext_bdf, dt_controller, &
203 ksp_results(i))
204 end do
205 call profiler_end_region('Scalar', 2)
206
207 if (.not. all_frozen) then
208 call ksp_results(i)%print_header()
209 end if
210
211 do i = 1, size(this%scalar_fields)
212 if (this%scalar_fields(i)%scalar%freeze) cycle
213 call scalar_step_info(time, ksp_results(i))
214 end do
215 end subroutine scalars_step
216
217 !> Restart from checkpoint data
218 subroutine scalars_restart(this, chkp)
219 class(scalars_t), intent(inout) :: this
220 type(chkp_t), intent(inout) :: chkp
221 integer :: i, n_scalars
222
223 n_scalars = size(this%scalar_fields)
224 do i = 1, size(this%scalar_fields)
225 call this%scalar_fields(i)%scalar%restart(chkp)
226 end do
227 end subroutine scalars_restart
228
229 !> Set initial conditions for all scalar fields.
230 !! @param params The scalar parameters passed to `init`, i.e. either the
231 !! `case.scalars` array or the single `case.scalar` object.
232 !! @param user Type with user-defined procedures.
233 !! @param is_restart Whether the case is restarting from a checkpoint.
234 subroutine scalars_set_initial_conditions(this, params, user, is_restart)
235 class(scalars_t), intent(inout) :: this
236 type(json_file), intent(inout) :: params
237 type(user_t), intent(in) :: user
238 logical, intent(in) :: is_restart
239 integer :: i, running_scalar_index, scalar_index, var_type
240 type(json_file) :: json_subdict
241
242 call neko_log%section("Scalar initial condition ")
243
244 if (is_restart) then
245 call neko_log%message("Restart file specified, " // &
246 "initial conditions ignored")
247 else
248 ! The parameters are an array for `case.scalars` and a single
249 ! object for `case.scalar`.
250 call params%info('', var_type = var_type)
251
252 running_scalar_index = 0
253 do i = 1, size(this%scalar_fields)
254 if (trim(this%scalar_fields(i)%scalar%name) .eq. 'temperature') then
255 scalar_index = 0
256 else
257 running_scalar_index = running_scalar_index + 1
258 scalar_index = running_scalar_index
259 end if
260
261 if (var_type .eq. json_array) then
262 call json_extract_item(params, "", i, json_subdict)
263 call this%scalar_fields(i)%scalar%set_initial_condition( &
264 json_subdict, user, scalar_index)
265 else
266 call this%scalar_fields(i)%scalar%set_initial_condition( &
267 params, user, scalar_index)
268 end if
269 end do
270 call json_subdict%destroy()
271 end if
272
273 call neko_log%end_section()
274
275 end subroutine scalars_set_initial_conditions
276
277 !> Check if the configuration is valid.
278 subroutine scalars_validate(this)
279 class(scalars_t), intent(inout) :: this
280 integer :: i
281 ! Iterate through all scalar fields
282 do i = 1, size(this%scalar_fields)
283 call this%scalar_fields(i)%scalar%slag%set( &
284 this%scalar_fields(i)%scalar%s)
285 call this%scalar_fields(i)%scalar%validate()
286 end do
287 end subroutine scalars_validate
288
289 !> Clean up all resources
290 subroutine scalars_free(this)
291 class(scalars_t), intent(inout) :: this
292 integer :: i
293
294 ! Iterate through all scalar fields
295 if (allocated(this%scalar_fields)) then
296 do i = 1, size(this%scalar_fields)
297 call this%scalar_fields(i)%free()
298 end do
299 deallocate(this%scalar_fields)
300 end if
301
302 if (allocated(this%shared_ksp)) then
303 call this%shared_ksp%free()
304 deallocate(this%shared_ksp)
305 end if
306 end subroutine scalars_free
307
308end module scalars
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.
Generic buffer that is extended with buffers of varying rank.
Definition buffer.F90:34
Defines format-independent checkpoint registration and restart state.
Coefficients.
Definition coef.f90:34
Contains the field_serties_t type.
Defines a field.
Definition field.f90:34
Gather-scatter.
Utilities for retrieving parameters from the case files.
Implements the base abstract type for Krylov solvers plus helper types.
Definition krylov.f90:34
Logging routines.
Definition log.f90:34
integer, parameter, public neko_log_verbose
Verbose.
Definition log.f90:54
type(log_t), public neko_log
Global log stream.
Definition log.f90:92
integer, parameter, public log_size
Definition log.f90:46
Defines a mesh.
Definition mesh.f90:34
integer, parameter, public rp
Global precision used in computations.
Definition num_types.f90:14
Profiling interface.
Definition profiler.F90:34
subroutine, public profiler_start_region(name, region_id)
Started a named (name) profiler region.
Definition profiler.F90:79
subroutine, public profiler_end_region(name, region_id)
End the most recently started profiler region.
Definition profiler.F90:116
Auxiliary routines for fluid solvers.
subroutine, public scalar_step_info(time, ksp_results, strict_convergence, allow_stabilization)
Prints for prs, velx, vely, velz the following: Number of iterations, start residual,...
Contains the scalar_scheme_t type.
Contains the scalars_t type that manages multiple scalar fields.
Definition scalars.f90:35
subroutine scalars_set_initial_conditions(this, params, user, is_restart)
Set initial conditions for all scalar fields.
Definition scalars.f90:235
subroutine scalars_validate(this)
Check if the configuration is valid.
Definition scalars.f90:279
subroutine scalars_init(this, n_scalars, msh, coef, gs, params, numerics_params, user, chkp, ulag, vlag, wlag, time_scheme, rho)
Initialize the scalars container.
Definition scalars.f90:88
subroutine scalars_init_single(this, msh, coef, gs, params, numerics_params, user, chkp, ulag, vlag, wlag, time_scheme, rho)
Definition scalars.f90:160
subroutine scalars_free(this)
Clean up all resources.
Definition scalars.f90:291
subroutine scalars_step(this, time, ext_bdf, dt_controller)
Perform a time step for all scalar fields.
Definition scalars.f90:187
subroutine scalars_restart(this, chkp)
Restart from checkpoint data.
Definition scalars.f90:219
Defines a function space.
Definition space.f90:34
Compound scheme for the advection and diffusion operators in a transport equation.
Base class for time integration schemes.
Module with things related to the simulation time.
Implements type time_step_controller.
Interfaces for user interaction with NEKO.
Definition user_intf.f90:34
Utilities.
Definition utils.f90:35
Collection of live simulation data registered for checkpointing.
Coefficients defined on a given (mesh, ) tuple. Arrays use indices (i,j,k,e): element e,...
Definition coef.f90:136
field_list_t, To be able to group fields together
Stores a series (sequence) of fields, logically connected to a base field, and arranged according to ...
Gather-scatter kernel.
Type for storing initial and final residuals in a Krylov solver.
Definition krylov.f90:60
Base abstract type for a canonical Krylov method, solving .
Definition krylov.f90:77
A helper type that is needed to have an array of polymorphic objects.
Type to manage multiple scalar transport equations.
Definition scalars.f90:61
The function space for the SEM solution fields.
Definition space.f90:64
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...