37 use num_types,
only: rp, dp, sp
38 use case,
only: case_t
43 use scalar_ic,
only: set_scalar_ic
44 use checkpoint,
only: chkp_t
45 use chkp_output,
only: chkp_output_t
46 use flow_ic,
only: set_flow_ic
47 use output_controller,
only: output_controller_t
48 use time_based_controller,
only: time_based_controller_t
49 use file,
only: file_t
50 use json_module,
only: json_file
51 use json_utils,
only: json_get, json_get_or_default, json_extract_item
54 use logger,
only : neko_log
55 use time_state,
only : time_state_t
56 use utils,
only: neko_error
75 adjoint_convection_term
76 type(case_t),
pointer :: case
77 type(time_state_t) :: time
79 type(chkp_output_t) :: chkp_out
83 type(output_controller_t) :: output_controller
84 type(time_based_controller_t) :: norm_output_ctrl
85 type(file_t) :: norm_output_file
86 logical :: norm_output_enabled = .false.
88 logical :: have_scalar = .false.
91 procedure, pass(this) :: init => adjoint_init_from_json
92 procedure, pass(this) :: free => adjoint_free
98 subroutine adjoint_init_from_json(this, neko_case)
100 type(case_t),
target,
intent(inout) :: neko_case
102 this%case => neko_case
103 call adjoint_case_init_common(this, neko_case)
105 end subroutine adjoint_init_from_json
108 subroutine adjoint_case_init_common(this, neko_case)
110 type(case_t),
intent(inout) :: neko_case
112 real(kind=rp) :: real_val = 0.0_rp
113 character(len=:),
allocatable :: string_val
114 character(len=:),
allocatable :: norm_control, norm_file
116 integer :: n_scalars_primal, n_scalars_adjoint, i
117 logical :: scalar = .false.
118 logical :: temperature_found = .false.
121 type(json_file) :: ic_json, numerics_params
122 type(json_file) :: scalar_params_primal, scalar_params_adjoint, json_subdict
123 character(len=:),
allocatable :: json_key
124 logical :: dealias_adjoint_scalar_convection
129 call json_get(neko_case%params,
'case.fluid.scheme', string_val)
132 call json_get(neko_case%params,
'case.numerics.polynomial_order', lx)
135 call this%chkp%init()
136 this%chkp%tlag => this%time%tlag
137 this%chkp%dtlag => this%time%dtlag
139 select type (f => this%fluid_adj)
141 call f%init(neko_case%msh, lx, neko_case%params, &
142 neko_case%user, this%chkp)
151 n_scalars_adjoint = 0
152 if (neko_case%params%valid_path(
'case.adjoint_scalar'))
then
153 call json_get_or_default(neko_case%params, &
154 'case.adjoint_scalar.enabled', scalar, .true.)
155 n_scalars_adjoint = 1
157 else if (neko_case%params%valid_path(
'case.adjoint_scalars'))
then
158 call neko_case%params%info(
'case.adjoint_scalars', &
159 n_children = n_scalars_adjoint)
160 call neko_case%params%info(
'case.scalars', n_children = n_scalars_primal)
161 if (n_scalars_adjoint > 0)
then
166 this%have_scalar = scalar
173 if (this%have_scalar)
then
174 allocate(this%adjoint_scalars)
175 call json_get(neko_case%params,
'case.numerics', &
177 if (neko_case%params%valid_path(
'case.adjoint_scalar'))
then
179 call json_get(neko_case%params,
'case.adjoint_scalar', &
180 scalar_params_adjoint)
181 call json_get(neko_case%params,
'case.scalar', &
182 scalar_params_primal)
183 call this%adjoint_scalars%init(neko_case%msh, neko_case%fluid%c_Xh, &
184 neko_case%fluid%gs_Xh, scalar_params_adjoint, &
185 scalar_params_primal, numerics_params, neko_case%user, &
186 neko_case%chkp, neko_case%fluid%ulag, neko_case%fluid%vlag, &
187 neko_case%fluid%wlag, neko_case%fluid%ext_bdf, &
190 allocate(this%adjoint_convection_term)
192 call json_get_or_default(neko_case%params, &
193 'case.adjoint_scalar.dealias_coupling_term', &
194 dealias_adjoint_scalar_convection, .true.)
195 call this%adjoint_convection_term%init_from_components( &
196 this%fluid_adj%f_adj_x, this%fluid_adj%f_adj_y, &
197 this%fluid_adj%f_adj_z, this%case%scalars%scalar_fields(1)%scalar%s, &
198 this%adjoint_scalars%adjoint_scalar_fields(1)%s_adj, &
199 this%fluid_adj%c_Xh, this%fluid_adj%c_Xh_GL, &
200 this%fluid_adj%GLL_to_GL, &
201 dealias_adjoint_scalar_convection, this%fluid_adj%scratch_GL)
203 select type (f => this%fluid_adj)
206 call f%source_term%add(this%adjoint_convection_term)
211 call json_get(this%case%params, &
212 'case.adjoint_scalars', scalar_params_adjoint)
213 call json_get(this%case%params, &
214 'case.scalars', scalar_params_primal)
215 call this%adjoint_scalars%init(n_scalars_adjoint, n_scalars_primal, &
216 neko_case%msh, neko_case%fluid%c_Xh, neko_case%fluid%gs_Xh, &
217 scalar_params_adjoint, scalar_params_primal, numerics_params, &
218 neko_case%user, neko_case%chkp, neko_case%fluid%ulag, &
219 neko_case%fluid%vlag, neko_case%fluid%wlag, &
220 neko_case%fluid%ext_bdf, neko_case%fluid%rho)
221 call neko_error(
'The adjoint scaling coupling term have not yet' // &
222 'been implemented for multiple scalars')
229 call json_get(this%case%params,
'case.time', json_subdict)
230 call this%time%init(json_subdict)
253 call neko_log%section(
"Adjoint initial condition")
255 'case.adjoint_fluid.initial_condition',
'case.fluid.initial_condition')
257 call json_get(neko_case%params, json_key, ic_json)
258 call json_get(ic_json,
'type', string_val)
260 if (trim(string_val) .ne.
'user')
then
262 this%fluid_adj%u_adj, this%fluid_adj%v_adj, this%fluid_adj%w_adj, &
263 this%fluid_adj%p_adj, this%fluid_adj%c_Xh, this%fluid_adj%gs_Xh, &
267 this%fluid_adj%u_adj, this%fluid_adj%v_adj, this%fluid_adj%w_adj, &
268 this%fluid_adj%p_adj, this%fluid_adj%c_Xh, this%fluid_adj%gs_Xh, &
269 neko_case%user%initial_conditions, neko_case%fluid%name)
272 call neko_log%end_section()
274 if (this%have_scalar)
then
276 if (neko_case%params%valid_path(
'case.adjoint_scalar'))
then
278 call json_get(neko_case%params, &
279 'case.adjoint_scalar.initial_condition.type', string_val)
280 call json_get(neko_case%params, &
281 'case.adjoint_scalar.initial_condition', ic_json)
285 if (trim(string_val) .ne.
'user')
then
286 if (trim(neko_case%scalars%scalar_fields(1)%scalar%name) .eq. &
289 this%adjoint_scalars%adjoint_scalar_fields(1)%s_adj, &
290 this%adjoint_scalars%adjoint_scalar_fields(1)%c_Xh, &
291 this%adjoint_scalars%adjoint_scalar_fields(1)%gs_Xh, &
292 string_val, ic_json, 0)
295 this%adjoint_scalars%adjoint_scalar_fields(1)%s_adj, &
296 this%adjoint_scalars%adjoint_scalar_fields(1)%c_Xh, &
297 this%adjoint_scalars%adjoint_scalar_fields(1)%gs_Xh, &
298 string_val, ic_json, 1)
301 call neko_error(
"user ICs not implemented for adjoint scalar")
311 do i = 1, n_scalars_adjoint
312 call json_extract_item(neko_case%params,
'case.adjoint_scalars', &
313 i, scalar_params_adjoint)
314 call json_get(scalar_params_adjoint, &
315 'initial_condition.type', string_val)
316 call json_get(scalar_params_adjoint, &
317 'initial_condition', json_subdict)
319 if (trim(string_val) .ne.
'user')
then
320 if (trim(neko_case%scalars%scalar_fields(i)%scalar%name) .eq. &
323 this%adjoint_scalars%adjoint_scalar_fields(i)%s_adj, &
324 this%adjoint_scalars%adjoint_scalar_fields(i)%c_Xh, &
325 this%adjoint_scalars%adjoint_scalar_fields(i)%gs_Xh, &
326 string_val, json_subdict, 0)
327 temperature_found = .true.
329 if (temperature_found)
then
332 this%adjoint_scalars%adjoint_scalar_fields(i)%s_adj, &
333 this%adjoint_scalars%adjoint_scalar_fields(i)%c_Xh, &
334 this%adjoint_scalars%adjoint_scalar_fields(i)%gs_Xh, &
335 string_val, json_subdict, i - 1)
339 this%adjoint_scalars%adjoint_scalar_fields(i)%s_adj, &
340 this%adjoint_scalars%adjoint_scalar_fields(i)%c_Xh, &
341 this%adjoint_scalars%adjoint_scalar_fields(i)%gs_Xh, &
342 string_val, json_subdict, i)
346 call neko_error(
"user ICs not implemented for adjoint scalar")
353 select type (f => this%fluid_adj)
355 call f%ulag%set(f%u_adj)
356 call f%vlag%set(f%v_adj)
357 call f%wlag%set(f%w_adj)
363 call this%fluid_adj%validate
365 if (this%have_scalar)
then
366 call this%adjoint_scalars%validate()
372 call json_get_or_default(neko_case%params,
'case.output_precision', &
373 string_val,
'single')
375 if (trim(string_val) .eq.
'double')
then
384 call this%output_controller%init(this%time%end_time)
385 if (this%have_scalar)
then
386 call this%f_out%init(precision, this%fluid_adj, &
387 this%adjoint_scalars, path = trim(neko_case%output_directory))
389 call this%f_out%init(precision, this%fluid_adj, &
390 path = trim(neko_case%output_directory))
393 call json_get_or_default(neko_case%params,
'case.fluid.output_control',&
396 if (trim(string_val) .eq.
'org')
then
398 call json_get(neko_case%params,
'case.nsamples', real_val)
399 call this%output_controller%add(this%f_out, real_val,
'nsamples')
400 else if (trim(string_val) .eq.
'never')
then
402 call json_get_or_default(neko_case%params,
'case.fluid.output_value', &
404 call this%output_controller%add(this%f_out, 0.0_rp, string_val)
406 call json_get(neko_case%params,
'case.fluid.output_value', real_val)
407 call this%output_controller%add(this%f_out, real_val, string_val)
413 call json_get_or_default(neko_case%params, &
414 'case.adjoint_fluid.norm_output_control', norm_control,
'never')
415 if (trim(norm_control) .ne.
'never')
then
416 call json_get_or_default(neko_case%params, &
417 'case.adjoint_fluid.norm_output_value', real_val, 1.0_rp)
418 call json_get_or_default(neko_case%params, &
419 'case.adjoint_fluid.norm_output_file', norm_file, &
421 call this%norm_output_file%init(trim(neko_case%output_directory) // &
423 call this%norm_output_file%set_header(
'Time, Norm')
424 call this%norm_output_file%set_overwrite(.true.)
425 call this%norm_output_ctrl%init(this%time%start_time, &
426 this%time%end_time, trim(norm_control), real_val)
427 this%norm_output_enabled = .true.
455 end subroutine adjoint_case_init_common
458 subroutine adjoint_free(this)
461 if (
allocated(this%fluid_adj))
then
462 call this%fluid_adj%free()
463 deallocate(this%fluid_adj)
466 if (
allocated(this%adjoint_scalars))
then
467 call this%adjoint_scalars%free()
468 deallocate(this%adjoint_scalars)
471 if (
allocated(this%adjoint_convection_term))
then
472 call this%adjoint_convection_term%free()
473 deallocate(this%adjoint_convection_term)
479 call this%chkp%free()
480 call this%chkp_out%free()
483 call this%f_out%free()
484 call this%output_controller%free()
485 call this%norm_output_ctrl%free()
486 call this%norm_output_file%free()
488 this%have_scalar = .false.
490 end subroutine adjoint_free
492end module adjoint_case
Factory for all adjoint fluid schemes.
subroutine, public adjoint_fluid_scheme_factory(object, type_name)
Initialise a adjoint fluid scheme.
Adjoint Pn/Pn formulation.
Defines an output for a adjoint.
Implements the adjoint_scalar_convection_source_term type.
Contains the adjoint_scalar_pnpn_t type.
Contains the adjoint_scalar_scheme_t type.
Contains the adjoint_scalars_t type that manages multiple scalar fields.
Adjoint case type. Todo: This should Ideally be a subclass of case_t, however, that is not yet suppor...
Base type of all fluid formulations.
Base type for a scalar advection-diffusion solver.
Type to manage multiple adjoint scalar transport equations.