35 use num_types,
only: rp, dp, sp
36 use case,
only: case_t
41 use scalar_ic,
only: set_scalar_ic
42 use flow_ic,
only: set_flow_ic
43 use output_controller,
only: output_controller_t
44 use file,
only: file_t
45 use json_module,
only: json_file
46 use json_utils,
only: json_get, json_get_or_default, json_extract_object, &
50 use logger,
only : neko_log
51 use time_state,
only : time_state_t
52 use utils,
only: neko_error
71 adjoint_convection_term
72 type(case_t),
pointer :: case
73 type(time_state_t) :: time
78 type(output_controller_t) :: output_controller
80 logical :: have_scalar = .false.
85 module procedure adjoint_init_from_json
91 subroutine adjoint_init_from_json(this, neko_case)
92 class(adjoint_case_t),
intent(inout) :: this
93 type(case_t),
target,
intent(inout) :: neko_case
95 this%case => neko_case
96 call adjoint_case_init_common(this, neko_case)
98 end subroutine adjoint_init_from_json
101 subroutine adjoint_case_init_common(this, neko_case)
102 class(adjoint_case_t),
intent(inout) :: this
103 type(case_t),
intent(inout) :: neko_case
105 real(kind=rp) :: real_val = 0.0_rp
106 character(len=:),
allocatable :: string_val
108 integer :: n_scalars_primal, n_scalars_adjoint, i
109 logical :: scalar = .false.
112 type(json_file) :: ic_json, numerics_params
113 type(json_file) :: scalar_params_primal, scalar_params_adjoint, json_subdict
114 character(len=:),
allocatable :: json_key
119 call json_get(neko_case%params,
'case.fluid.scheme', string_val)
120 call adjoint_fluid_scheme_factory(this%fluid_adj, trim(string_val))
122 call json_get(neko_case%params,
'case.numerics.polynomial_order', lx)
125 select type (f => this%fluid_adj)
127 call f%init(neko_case%msh, lx, neko_case%params, &
128 neko_case%user, neko_case%chkp)
137 n_scalars_adjoint = 0
138 if (neko_case%params%valid_path(
'case.adjoint_scalar'))
then
139 call json_get_or_default(neko_case%params, &
140 'case.adjoint_scalar.enabled', scalar, .true.)
141 n_scalars_adjoint = 1
143 else if (neko_case%params%valid_path(
'case.adjoint_scalars'))
then
144 call neko_case%params%info(
'case.adjoint_scalars', &
145 n_children = n_scalars_adjoint)
146 call neko_case%params%info(
'case.scalars', n_children = n_scalars_primal)
147 if (n_scalars_adjoint > 0)
then
152 this%have_scalar = scalar
159 if (this%have_scalar)
then
160 allocate(this%adjoint_scalars)
161 call json_extract_object(neko_case%params,
'case.numerics', &
163 if (neko_case%params%valid_path(
'case.adjoint_scalar'))
then
165 call json_extract_object(neko_case%params,
'case.adjoint_scalar', &
166 scalar_params_adjoint)
167 call json_extract_object(neko_case%params,
'case.scalar', &
168 scalar_params_primal)
169 call this%adjoint_scalars%init(neko_case%msh, neko_case%fluid%c_Xh, &
170 neko_case%fluid%gs_Xh, scalar_params_adjoint, &
171 scalar_params_primal, numerics_params, neko_case%user, &
172 neko_case%chkp, neko_case%fluid%ulag, neko_case%fluid%vlag, &
173 neko_case%fluid%wlag, neko_case%fluid%ext_bdf, &
176 allocate(this%adjoint_convection_term)
178 call this%adjoint_convection_term%init_from_components( &
179 this%fluid_adj%f_adj_x, this%fluid_adj%f_adj_y, &
180 this%fluid_adj%f_adj_z, this%case%scalars%scalar_fields(1)%s, &
181 this%adjoint_scalars%adjoint_scalar_fields(1)%s_adj, &
184 select type (f => this%fluid_adj)
187 call f%source_term%add(this%adjoint_convection_term)
192 call json_extract_object(this%case%params, &
193 'case.adjoint_scalars', scalar_params_adjoint)
194 call json_extract_object(this%case%params, &
195 'case.scalars', scalar_params_primal)
196 call this%adjoint_scalars%init(n_scalars_adjoint, n_scalars_primal, &
197 neko_case%msh, neko_case%fluid%c_Xh, neko_case%fluid%gs_Xh, &
198 scalar_params_adjoint, scalar_params_primal, numerics_params, &
199 neko_case%user, neko_case%chkp, neko_case%fluid%ulag, &
200 neko_case%fluid%vlag, neko_case%fluid%wlag, &
201 neko_case%fluid%ext_bdf, neko_case%fluid%rho)
202 call neko_error(
'The adjoint scaling coupling term have not yet' // &
203 'been implemented for multiple scalars')
210 call json_extract_object(this%case%params,
'case.time', json_subdict)
211 call this%time%init(json_subdict)
234 call neko_log%section(
"Adjoint initial condition")
236 'case.adjoint_fluid.initial_condition',
'case.fluid.initial_condition')
238 call json_extract_object(neko_case%params, json_key, ic_json)
239 call json_get(ic_json,
'type', string_val)
241 if (trim(string_val) .ne.
'user')
then
243 this%fluid_adj%u_adj, this%fluid_adj%v_adj, this%fluid_adj%w_adj, &
244 this%fluid_adj%p_adj, this%fluid_adj%c_Xh, this%fluid_adj%gs_Xh, &
248 this%fluid_adj%u_adj, this%fluid_adj%v_adj, this%fluid_adj%w_adj, &
249 this%fluid_adj%p_adj, this%fluid_adj%c_Xh, this%fluid_adj%gs_Xh, &
250 neko_case%user%fluid_user_ic, neko_case%params)
253 call neko_log%end_section()
255 if (this%have_scalar)
then
257 if (neko_case%params%valid_path(
'case.adjoint_scalar'))
then
259 call json_get(neko_case%params, &
260 'case.adjoint_scalar.initial_condition.type', string_val)
261 call json_extract_object(neko_case%params, &
262 'case.adjoint_scalar.initial_condition', ic_json)
266 if (trim(string_val) .ne.
'user')
then
268 this%adjoint_scalars%adjoint_scalar_fields(1)%s_adj, &
269 this%adjoint_scalars%adjoint_scalar_fields(1)%c_Xh, &
270 this%adjoint_scalars%adjoint_scalar_fields(1)%gs_Xh, &
273 call neko_error(
"user ICs not implemented for adjoint scalar")
283 do i = 1, n_scalars_adjoint
284 call json_extract_item(neko_case%params,
'case.adjoint_scalars', &
285 i, scalar_params_adjoint)
286 call json_get(scalar_params_adjoint, &
287 'initial_condition.type', string_val)
288 call json_extract_object(scalar_params_adjoint, &
289 'initial_condition', json_subdict)
291 if (trim(string_val) .ne.
'user')
then
293 this%adjoint_scalars%adjoint_scalar_fields(i)%s_adj, &
294 this%adjoint_scalars%adjoint_scalar_fields(i)%c_Xh, &
295 this%adjoint_scalars%adjoint_scalar_fields(i)%gs_Xh, &
296 string_val, json_subdict)
298 call neko_error(
"user ICs not implemented for adjoint scalar")
305 select type (f => this%fluid_adj)
307 call f%ulag%set(f%u_adj)
308 call f%vlag%set(f%v_adj)
309 call f%wlag%set(f%w_adj)
315 call this%fluid_adj%validate
317 if (this%have_scalar)
then
318 call this%adjoint_scalars%validate()
324 call json_get_or_default(neko_case%params,
'case.output_precision', &
325 string_val,
'single')
327 if (trim(string_val) .eq.
'double')
then
336 call this%output_controller%init(neko_case%time%end_time)
337 if (this%have_scalar)
then
339 this%adjoint_scalars, path = trim(neko_case%output_directory))
342 path = trim(neko_case%output_directory))
345 call json_get_or_default(neko_case%params,
'case.fluid.output_control',&
348 if (trim(string_val) .eq.
'org')
then
350 call json_get(neko_case%params,
'case.nsamples', real_val)
351 call this%output_controller%add(this%f_out, real_val,
'nsamples')
352 else if (trim(string_val) .eq.
'never')
then
354 call json_get_or_default(neko_case%params,
'case.fluid.output_value', &
356 call this%output_controller%add(this%f_out, 0.0_rp, string_val)
358 call json_get(neko_case%params,
'case.fluid.output_value', real_val)
359 call this%output_controller%add(this%f_out, real_val, string_val)
387 end subroutine adjoint_case_init_common
390 subroutine adjoint_free(this)
391 class(adjoint_case_t),
intent(inout) :: this
394 if (
allocated(this%adjoint_scalars))
then
395 call this%adjoint_scalars%free()
396 deallocate(this%adjoint_scalars)
399 if (
allocated(this%fluid_adj))
then
400 call this%fluid_adj%free()
401 deallocate(this%fluid_adj)
403 call this%output_controller%free()
405 end subroutine adjoint_free
407end 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.