Neko 1.99.11
A portable framework for high-order spectral element flow simulations
Loading...
Searching...
No Matches
gradient_simcomp.f90
Go to the documentation of this file.
1! Copyright (c) 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!
34!> Implements the `gradient_t` type.
36 use num_types, only : rp, dp, sp
37 use json_module, only : json_file
39 use registry, only : neko_registry
40 use field, only : field_t
41 use operators, only : grad
42 use time_state, only : time_state_t
43 use case, only : case_t
47 use utils, only : neko_varname_len
48 implicit none
49 private
50
51 !> A simulation component that computes the gradient of a field.
52 !! Wraps the `gradient` operator.
53 type, public, extends(simulation_component_t) :: gradient_t
54 !> The scalar field to compute the weak gradient of.
55 type(field_t), pointer :: u
56 !> X weak gradient component.
57 type(field_t), pointer :: gradient_x
58 !> Y weak gradient component.
59 type(field_t), pointer :: gradient_y
60 !> Z weak gradient component.
61 type(field_t), pointer :: gradient_z
62 !> Output writer.
63 type(field_writer_t) :: writer
64
65 contains
66 !> Constructor from json, wrapping the actual constructor.
67 procedure, pass(this) :: init => gradient_init_from_json
68 !> Generic for constructing from components.
69 generic :: init_from_components => &
70 init_from_controllers, init_from_controllers_properties
71 !> Constructor from components, passing time_based_controllers.
72 procedure, pass(this) :: init_from_controllers => &
74 !> Constructor from components, passing the properties of
75 !! time_based_controllers.
76 procedure, pass(this) :: init_from_controllers_properties => &
78 !> Common part of both constructors.
79 procedure, private, pass(this) :: init_common => gradient_init_common
80 !> Destructor.
81 procedure, pass(this) :: free => gradient_free
82 !> Compute the gradient field.
83 procedure, pass(this) :: compute_ => gradient_compute
84 end type gradient_t
85
86contains
87
88 !> Constructor from json.
89 subroutine gradient_init_from_json(this, json, case)
90 class(gradient_t), intent(inout), target :: this
91 type(json_file), intent(inout) :: json
92 class(case_t), intent(inout), target :: case
93 character(len=:), allocatable :: field_name
94 character(len=:), allocatable :: name
95 character(len=NEKO_VARNAME_LEN) :: fields(3)
96 character(len=:), allocatable :: computed_field
97
98 call json_get_or_default(json, "name", name, "gradient")
99 call json_get(json, "field", field_name)
100
101 ! Add fields keyword to the json so that the field_writer picks it up.
102 ! Will also add fields to the registry.
103 call json_get_or_default(json, "computed_field", computed_field, &
104 "gradient_" // trim(field_name))
105
106 fields(1) = computed_field // "_x"
107 fields(2) = computed_field // "_y"
108 fields(3) = computed_field // "_z"
109
110 call json%add("fields", fields)
111
112 call this%init_base(json, case)
113 call this%writer%init(json, case)
114
115 call this%init_common(name, field_name, computed_field)
116 end subroutine gradient_init_from_json
117
118 !> Common part of the constructors.
119 !! @param name The unique name of the simcomp
120 subroutine gradient_init_common(this, name, field_name, computed_field)
121 class(gradient_t), intent(inout) :: this
122 character(len=*) :: name
123 character(len=*) :: field_name
124 character(len=*) :: computed_field
125
126 this%name = name
127 this%u => neko_registry%get_field_by_name(trim(field_name))
128
129 this%gradient_x => neko_registry%get_field_by_name( &
130 computed_field // "_x")
131 this%gradient_y => neko_registry%get_field_by_name( &
132 computed_field // "_y")
133 this%gradient_z => neko_registry%get_field_by_name( &
134 computed_field // "_z")
135
136
137 end subroutine gradient_init_common
138
139 !> Constructor from components, passing controllers.
140 !! @param name The unique name of the simcomp.
141 !! @param case The simulation case object.
142 !! @param order The execution oder priority of the simcomp.
143 !! @param preprocess_controller The controller for running preprocessing.
144 !! @param compute_controller The controller for running compute.
145 !! @param output_controller The controller for producing output.
146 !! @param field_name The name of the field to compute the gradient of.
147 !! @param computed_field The base name of the gradient field components.
148 !! @param filename The name of the file save the fields to. Optional, if not
149 !! @param precision The real precision of the output data. Optional, defaults
150 !! to single precision.
151 subroutine gradient_init_from_controllers(this, name, case, order, &
152 preprocess_controller, compute_controller, output_controller, &
153 field_name, computed_field, filename, precision)
154 class(gradient_t), intent(inout) :: this
155 character(len=*), intent(in) :: name
156 class(case_t), intent(inout), target :: case
157 integer :: order
158 type(time_based_controller_t), intent(in) :: preprocess_controller
159 type(time_based_controller_t), intent(in) :: compute_controller
160 type(time_based_controller_t), intent(in) :: output_controller
161 character(len=*) :: field_name
162 character(len=*) :: computed_field
163 character(len=*), intent(in), optional :: filename
164 integer, intent(in), optional :: precision
165
166 character(len=NEKO_VARNAME_LEN) :: fields(3)
167
168 fields(1) = trim(computed_field) // "_x"
169 fields(2) = trim(computed_field) // "_y"
170 fields(3) = trim(computed_field) // "_z"
171
172 call this%init_base_from_components(case, order, preprocess_controller, &
173 compute_controller, output_controller)
174 call this%writer%init_from_components("field_writer", case, order, &
175 preprocess_controller, compute_controller, output_controller, fields, &
176 filename, precision)
177 call this%init_common(name, field_name, computed_field)
178
179 end subroutine gradient_init_from_controllers
180
181 !> Constructor from components, passing properties to the
182 !! time_based_controller` components in the base type.
183 !! @param name The unique name of the simcomp.
184 !! @param case The simulation case object.
185 !! @param order The execution oder priority of the simcomp.
186 !! @param preprocess_controller Control mode for preprocessing.
187 !! @param preprocess_value Value parameter for preprocessing.
188 !! @param compute_controller Control mode for computing.
189 !! @param compute_value Value parameter for computing.
190 !! @param output_controller Control mode for output.
191 !! @param output_value Value parameter for output.
192 !! @param field_name The name of the field to compute the gradient of.
193 !! @param computed_field The base name of the gradient field components.
194 !! @param filename The name of the file save the fields to. Optional, if not
195 !! provided, fields are added to the main output file.
196 !! @param precision The real precision of the output data. Optional, defaults
197 !! to single precision.
199 case, order, preprocess_control, preprocess_value, compute_control, &
200 compute_value, output_control, output_value, field_name, &
201 computed_field, filename, precision)
202 class(gradient_t), intent(inout) :: this
203 character(len=*), intent(in) :: name
204 class(case_t), intent(inout), target :: case
205 integer :: order
206 character(len=*), intent(in) :: preprocess_control
207 real(kind=dp), intent(in) :: preprocess_value
208 character(len=*), intent(in) :: compute_control
209 real(kind=dp), intent(in) :: compute_value
210 character(len=*), intent(in) :: output_control
211 real(kind=dp), intent(in) :: output_value
212 character(len=*) :: field_name
213 character(len=*) :: computed_field
214 character(len=*), intent(in), optional :: filename
215 integer, intent(in), optional :: precision
216
217 character(len=NEKO_VARNAME_LEN) :: fields(3)
218
219 fields(1) = trim(computed_field) // "_x"
220 fields(2) = trim(computed_field) // "_y"
221 fields(3) = trim(computed_field) // "_z"
222
223 call this%init_base_from_components(case, order, preprocess_control, &
224 preprocess_value, compute_control, compute_value, output_control, &
225 output_value)
226 call this%writer%init_from_components("field_writer", case, order, &
227 preprocess_control, preprocess_value, compute_control, compute_value, &
228 output_control, output_value, fields, filename, precision)
229 call this%init_common(name, field_name, computed_field)
230
232
233 !> Destructor.
234 subroutine gradient_free(this)
235 class(gradient_t), intent(inout) :: this
236 call this%free_base()
237 call this%writer%free()
238 nullify(this%gradient_x)
239 nullify(this%gradient_y)
240 nullify(this%gradient_z)
241 nullify(this%u)
242 end subroutine gradient_free
243
244 !> Compute the gradient field.
245 !! @param time The current time value
246 subroutine gradient_compute(this, time)
247 class(gradient_t), intent(inout) :: this
248 type(time_state_t), intent(in) :: time
249
250 call grad(this%gradient_x%x, this%gradient_y%x, this%gradient_z%x, &
251 this%u%x, this%case%fluid%c_Xh)
252 end subroutine gradient_compute
253
254end module gradient_simcomp
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.
Compute the gradient of a scalar field, multiplied by the mass matrix.
Definition operators.f90:92
Defines a simulation case.
Definition case.f90:34
Implements the field_writer_t type.
Defines a field.
Definition field.f90:34
Implements the gradient_t type.
subroutine gradient_compute(this, time)
Compute the gradient field.
subroutine gradient_init_from_controllers_properties(this, name, case, order, preprocess_control, preprocess_value, compute_control, compute_value, output_control, output_value, field_name, computed_field, filename, precision)
Constructor from components, passing properties to the time_based_controller` components in the base ...
subroutine gradient_init_from_controllers(this, name, case, order, preprocess_controller, compute_controller, output_controller, field_name, computed_field, filename, precision)
Constructor from components, passing controllers.
subroutine gradient_init_from_json(this, json, case)
Constructor from json.
subroutine gradient_free(this)
Destructor.
subroutine gradient_init_common(this, name, field_name, computed_field)
Common part of the constructors.
Utilities for retrieving parameters from the case files.
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
Operators.
Definition operators.f90:34
Implements output_controller_t.
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
Simulation components are objects that encapsulate functionality that can be fit to a particular comp...
subroutine compute_(this, time)
Dummy compute function.
Contains the time_based_controller_t type.
Module with things related to the simulation time.
Utilities.
Definition utils.f90:35
integer, parameter, public neko_varname_len
Definition utils.f90:43
A simulation component that writes a 3d field to a file.
A simulation component that computes the gradient of a field. Wraps the gradient operator.
Base abstract class for simulation components.
A utility type for determining whether an action should be executed based on the current time value....
A struct that contains all info about the time, expand as needed.