35 use num_types,
only: rp, dp, sp
36 use case,
only: case_t
41 use scalar_ic,
only: set_scalar_ic
42 use checkpoint,
only: chkp_t
43 use chkp_output,
only: chkp_output_t
44 use flow_ic,
only: set_flow_ic
45 use output_controller,
only: output_controller_t
46 use file,
only: file_t
47 use json_module,
only: json_file
48 use json_utils,
only: json_get, json_get_or_default, json_extract_object, &
52 use logger,
only : neko_log
53 use time_state,
only : time_state_t
54 use utils,
only: neko_error
73 adjoint_convection_term
74 type(case_t),
pointer :: case
75 type(time_state_t) :: time
77 type(chkp_output_t) :: chkp_out
82 type(output_controller_t) :: output_controller
84 logical :: have_scalar = .false.
89 module procedure adjoint_init_from_json
95 subroutine adjoint_init_from_json(this, neko_case)
96 class(adjoint_case_t),
intent(inout) :: this
97 type(case_t),
target,
intent(inout) :: neko_case
99 this%case => neko_case
100 call adjoint_case_init_common(this, neko_case)
102 end subroutine adjoint_init_from_json
105 subroutine adjoint_case_init_common(this, neko_case)
106 class(adjoint_case_t),
target,
intent(inout) :: this
107 type(case_t),
intent(inout) :: neko_case
109 real(kind=rp) :: real_val = 0.0_rp
110 character(len=:),
allocatable :: string_val
112 integer :: n_scalars_primal, n_scalars_adjoint, i
113 logical :: scalar = .false.
116 type(json_file) :: ic_json, numerics_params
117 type(json_file) :: scalar_params_primal, scalar_params_adjoint, json_subdict
118 character(len=:),
allocatable :: json_key
123 call json_get(neko_case%params,
'case.fluid.scheme', string_val)
124 call adjoint_fluid_scheme_factory(this%fluid_adj, trim(string_val))
126 call json_get(neko_case%params,
'case.numerics.polynomial_order', lx)
129 this%chkp%tlag => this%time%tlag
130 this%chkp%dtlag => this%time%dtlag
132 select type (f => this%fluid_adj)
134 call f%init(neko_case%msh, lx, neko_case%params, &
135 neko_case%user, this%chkp)
144 n_scalars_adjoint = 0
145 if (neko_case%params%valid_path(
'case.adjoint_scalar'))
then
146 call json_get_or_default(neko_case%params, &
147 'case.adjoint_scalar.enabled', scalar, .true.)
148 n_scalars_adjoint = 1
150 else if (neko_case%params%valid_path(
'case.adjoint_scalars'))
then
151 call neko_case%params%info(
'case.adjoint_scalars', &
152 n_children = n_scalars_adjoint)
153 call neko_case%params%info(
'case.scalars', n_children = n_scalars_primal)
154 if (n_scalars_adjoint > 0)
then
159 this%have_scalar = scalar
166 if (this%have_scalar)
then
167 allocate(this%adjoint_scalars)
168 call json_extract_object(neko_case%params,
'case.numerics', &
170 if (neko_case%params%valid_path(
'case.adjoint_scalar'))
then
172 call json_extract_object(neko_case%params,
'case.adjoint_scalar', &
173 scalar_params_adjoint)
174 call json_extract_object(neko_case%params,
'case.scalar', &
175 scalar_params_primal)
176 call this%adjoint_scalars%init(neko_case%msh, neko_case%fluid%c_Xh, &
177 neko_case%fluid%gs_Xh, scalar_params_adjoint, &
178 scalar_params_primal, numerics_params, neko_case%user, &
179 neko_case%chkp, neko_case%fluid%ulag, neko_case%fluid%vlag, &
180 neko_case%fluid%wlag, neko_case%fluid%ext_bdf, &
183 allocate(this%adjoint_convection_term)
185 call this%adjoint_convection_term%init_from_components( &
186 this%fluid_adj%f_adj_x, this%fluid_adj%f_adj_y, &
187 this%fluid_adj%f_adj_z, this%case%scalars%scalar_fields(1)%s, &
188 this%adjoint_scalars%adjoint_scalar_fields(1)%s_adj, &
191 select type (f => this%fluid_adj)
194 call f%source_term%add(this%adjoint_convection_term)
199 call json_extract_object(this%case%params, &
200 'case.adjoint_scalars', scalar_params_adjoint)
201 call json_extract_object(this%case%params, &
202 'case.scalars', scalar_params_primal)
203 call this%adjoint_scalars%init(n_scalars_adjoint, n_scalars_primal, &
204 neko_case%msh, neko_case%fluid%c_Xh, neko_case%fluid%gs_Xh, &
205 scalar_params_adjoint, scalar_params_primal, numerics_params, &
206 neko_case%user, neko_case%chkp, neko_case%fluid%ulag, &
207 neko_case%fluid%vlag, neko_case%fluid%wlag, &
208 neko_case%fluid%ext_bdf, neko_case%fluid%rho)
209 call neko_error(
'The adjoint scaling coupling term have not yet' // &
210 'been implemented for multiple scalars')
217 call json_extract_object(this%case%params,
'case.time', json_subdict)
218 call this%time%init(json_subdict)
241 call neko_log%section(
"Adjoint initial condition")
243 'case.adjoint_fluid.initial_condition',
'case.fluid.initial_condition')
245 call json_extract_object(neko_case%params, json_key, ic_json)
246 call json_get(ic_json,
'type', string_val)
248 if (trim(string_val) .ne.
'user')
then
250 this%fluid_adj%u_adj, this%fluid_adj%v_adj, this%fluid_adj%w_adj, &
251 this%fluid_adj%p_adj, this%fluid_adj%c_Xh, this%fluid_adj%gs_Xh, &
255 this%fluid_adj%u_adj, this%fluid_adj%v_adj, this%fluid_adj%w_adj, &
256 this%fluid_adj%p_adj, this%fluid_adj%c_Xh, this%fluid_adj%gs_Xh, &
257 neko_case%user%initial_conditions, neko_case%fluid%name)
260 call neko_log%end_section()
262 if (this%have_scalar)
then
264 if (neko_case%params%valid_path(
'case.adjoint_scalar'))
then
266 call json_get(neko_case%params, &
267 'case.adjoint_scalar.initial_condition.type', string_val)
268 call json_extract_object(neko_case%params, &
269 'case.adjoint_scalar.initial_condition', ic_json)
273 if (trim(string_val) .ne.
'user')
then
275 this%adjoint_scalars%adjoint_scalar_fields(1)%s_adj, &
276 this%adjoint_scalars%adjoint_scalar_fields(1)%c_Xh, &
277 this%adjoint_scalars%adjoint_scalar_fields(1)%gs_Xh, &
280 call neko_error(
"user ICs not implemented for adjoint scalar")
290 do i = 1, n_scalars_adjoint
291 call json_extract_item(neko_case%params,
'case.adjoint_scalars', &
292 i, scalar_params_adjoint)
293 call json_get(scalar_params_adjoint, &
294 'initial_condition.type', string_val)
295 call json_extract_object(scalar_params_adjoint, &
296 'initial_condition', json_subdict)
298 if (trim(string_val) .ne.
'user')
then
300 this%adjoint_scalars%adjoint_scalar_fields(i)%s_adj, &
301 this%adjoint_scalars%adjoint_scalar_fields(i)%c_Xh, &
302 this%adjoint_scalars%adjoint_scalar_fields(i)%gs_Xh, &
303 string_val, json_subdict)
305 call neko_error(
"user ICs not implemented for adjoint scalar")
312 select type (f => this%fluid_adj)
314 call f%ulag%set(f%u_adj)
315 call f%vlag%set(f%v_adj)
316 call f%wlag%set(f%w_adj)
322 call this%fluid_adj%validate
324 if (this%have_scalar)
then
325 call this%adjoint_scalars%validate()
331 call json_get_or_default(neko_case%params,
'case.output_precision', &
332 string_val,
'single')
334 if (trim(string_val) .eq.
'double')
then
343 call this%output_controller%init(this%time%end_time)
344 if (this%have_scalar)
then
346 this%adjoint_scalars, path = trim(neko_case%output_directory))
349 path = trim(neko_case%output_directory))
352 call json_get_or_default(neko_case%params,
'case.fluid.output_control',&
355 if (trim(string_val) .eq.
'org')
then
357 call json_get(neko_case%params,
'case.nsamples', real_val)
358 call this%output_controller%add(this%f_out, real_val,
'nsamples')
359 else if (trim(string_val) .eq.
'never')
then
361 call json_get_or_default(neko_case%params,
'case.fluid.output_value', &
363 call this%output_controller%add(this%f_out, 0.0_rp, string_val)
365 call json_get(neko_case%params,
'case.fluid.output_value', real_val)
366 call this%output_controller%add(this%f_out, real_val, string_val)
394 end subroutine adjoint_case_init_common
397 subroutine adjoint_free(this)
398 class(adjoint_case_t),
intent(inout) :: this
401 if (
allocated(this%adjoint_scalars))
then
402 call this%adjoint_scalars%free()
403 deallocate(this%adjoint_scalars)
406 if (
allocated(this%fluid_adj))
then
407 call this%fluid_adj%free()
408 deallocate(this%fluid_adj)
410 call this%output_controller%free()
412 end subroutine adjoint_free
414end module adjoint_case
Factory for all adjoint fluid schemes.
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.