37 use simulation_component,
only: simulation_component_t
38 use num_types,
only: rp, dp
39 use field,
only: field_t
40 use json_module,
only: json_file
41 use json_utils,
only: json_get_or_default
42 use case,
only: case_t
43 use field_math,
only: field_sub2, field_copy
45 use device_math,
only: device_glsc3
46 use coefs,
only: coef_t
47 use neko_config,
only : neko_bcknd_device
48 use csv_file,
only : csv_file_t
49 use vector,
only: vector_t
50 use time_state,
only: time_state_t
51 use utils,
only: neko_error
63 type(field_t),
pointer :: u, v, w, p, s
66 type(field_t) :: u_old, v_old, w_old, p_old, s_old
72 logical :: scalar_coupled = .false.
75 type(csv_file_t) :: logger
77 integer :: log_frequency
79 type(vector_t) :: log_data
83 procedure,
public, pass(this) :: init => steady_simcomp_init_from_json
85 procedure,
public, pass(this) :: init_from_attributes => &
86 steady_simcomp_init_from_attributes
88 procedure,
public, pass(this) :: free => steady_simcomp_free
90 procedure,
public, pass(this) :: compute_ => steady_simcomp_compute
97 class(simulation_component_t),
allocatable,
intent(inout) :: obj
102 subroutine steady_simcomp_init_from_json(this, json, case)
104 type(json_file),
intent(inout) :: json
105 class(case_t),
intent(inout),
target :: case
107 integer :: log_frequency
109 call this%init_base(json, case)
112 call json_get_or_default(json,
"tol", tol, 1.0e-6_dp)
114 call json_get_or_default(json,
"log_frequency", log_frequency, 50)
115 call json_get_or_default(json,
"scalar_coupled", this%scalar_coupled, .false.)
117 call this%init_from_attributes(tol, log_frequency)
119 end subroutine steady_simcomp_init_from_json
122 subroutine steady_simcomp_init_from_attributes(this, tol, log_frequency)
124 real(kind=dp),
intent(in) :: tol
125 integer,
intent(in) :: log_frequency
128 this%log_frequency = log_frequency
130 this%u => this%case%fluid%u
131 this%v => this%case%fluid%v
132 this%w => this%case%fluid%w
133 this%p => this%case%fluid%p
134 if (
allocated(this%case%scalars))
then
135 if (
size(this%case%scalars%scalar_fields) .gt. 1)
then
136 call neko_error(
'steady simcomp only works for a single scalar')
138 this%s => this%case%scalars%scalar_fields(1)%scalar%s
144 call this%logger%init(
'steady_state_data.csv')
145 call this%logger%set_header(
'iter,time,u,v,w,p,scalar')
146 call this%log_data%init(7)
149 call this%u_old%init(this%u%dof)
150 call this%v_old%init(this%v%dof)
151 call this%w_old%init(this%w%dof)
152 call this%p_old%init(this%p%dof)
155 if (
associated(this%s))
then
156 call this%s_old%init(this%s%dof)
159 end subroutine steady_simcomp_init_from_attributes
162 subroutine steady_simcomp_free(this)
165 if (
associated(this%u))
nullify(this%u)
166 if (
associated(this%v))
nullify(this%v)
167 if (
associated(this%w))
nullify(this%w)
168 if (
associated(this%p))
nullify(this%p)
169 if (
associated(this%s))
nullify(this%s)
171 call this%u_old%free()
172 call this%v_old%free()
173 call this%w_old%free()
174 call this%p_old%free()
175 call this%s_old%free()
176 call this%log_data%free()
178 call this%free_base()
180 end subroutine steady_simcomp_free
183 subroutine steady_simcomp_compute(this, time)
185 type(time_state_t),
intent(in) :: time
190 real(kind=rp),
dimension(4) :: normed_diff_fluid
191 real(kind=rp),
dimension(1) :: normed_diff_scalar
192 logical :: converged_fluid, converged_scalar, converged
195 if (this%case%fluid%freeze)
return
200 dt = this%case%time%dt
209 call field_sub2(this%u_old, this%u)
210 call field_sub2(this%v_old, this%v)
211 call field_sub2(this%w_old, this%w)
212 call field_sub2(this%p_old, this%p)
213 if (
associated(this%s))
then
214 call field_sub2(this%s_old, this%s)
219 normed_diff_fluid(1) = energy_norm(this%u_old, this%case%fluid%C_Xh, dt)
220 normed_diff_fluid(2) = energy_norm(this%v_old, this%case%fluid%C_Xh, dt)
221 normed_diff_fluid(3) = energy_norm(this%w_old, this%case%fluid%C_Xh, dt)
222 normed_diff_fluid(4) = energy_norm(this%p_old, this%case%fluid%C_Xh, dt)
223 if (
associated(this%s))
then
224 normed_diff_scalar(1) = energy_norm(this%s_old, this%case%fluid%C_Xh, dt)
226 normed_diff_scalar(1) = 0.0_rp
229 converged_fluid = maxval(normed_diff_fluid) .le. this%tol
230 if (
associated(this%s))
then
231 converged_scalar = maxval(normed_diff_scalar) .le. this%tol
233 converged_scalar = .true.
235 converged = converged_fluid .and. converged_scalar
237 if (this%scalar_coupled)
then
238 converged_fluid = converged
239 converged_scalar = converged
245 if (.not. converged)
then
246 call field_copy(this%u_old, this%u)
247 call field_copy(this%v_old, this%v)
248 call field_copy(this%w_old, this%w)
249 call field_copy(this%p_old, this%p)
250 if (
associated(this%s))
then
251 call field_copy(this%s_old, this%s)
256 if (mod(tstep, this%log_frequency) .eq. 0)
then
257 this%log_data%x(1) = real(tstep, kind=rp)
258 this%log_data%x(2) = t
259 this%log_data%x(3) = normed_diff_fluid(1)
260 this%log_data%x(4) = normed_diff_fluid(2)
261 this%log_data%x(5) = normed_diff_fluid(3)
262 this%log_data%x(6) = normed_diff_fluid(4)
263 this%log_data%x(7) = normed_diff_scalar(1)
264 call this%logger%write(this%log_data)
268 if (converged_fluid)
then
269 this%case%fluid%freeze = .true.
276 end subroutine steady_simcomp_compute
282 function energy_norm(delta_fld, coef, dt)
283 type(field_t),
intent(in) :: delta_fld
284 type(coef_t),
intent(in) :: coef
285 real(kind=rp),
intent(in) :: dt
286 real(kind=rp) :: energy_norm, tmp
290 if (neko_bcknd_device .eq. 1)
then
291 tmp = device_glsc3(delta_fld%x_d, delta_fld%x_d, coef%B_d, n)
293 tmp = glsc3(delta_fld%x, delta_fld%x, coef%B, n)
295 energy_norm = sqrt(tmp) / dt / coef%volume
297 end function energy_norm
Implements the steady_simcomp_t type.
subroutine, public steady_simcomp_allocate(obj)
Allocator for the steady simulation component.
The steady_simcomp_t type is a simulation component that terminates a simulation when the normed diff...