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 file,
only: file_t
49 use json_module,
only: json_file
50 use json_utils,
only: json_get, json_get_or_default, json_extract_item
53 use logger,
only : neko_log
54 use time_state,
only : time_state_t
55 use utils,
only: neko_error
74 adjoint_convection_term
75 type(case_t),
pointer :: case
76 type(time_state_t) :: time
78 type(chkp_output_t) :: chkp_out
83 type(output_controller_t) :: output_controller
85 logical :: have_scalar = .false.
90 module procedure adjoint_init_from_json
96 subroutine adjoint_init_from_json(this, neko_case)
97 class(adjoint_case_t),
intent(inout) :: this
98 type(case_t),
target,
intent(inout) :: neko_case
100 this%case => neko_case
101 call adjoint_case_init_common(this, neko_case)
103 end subroutine adjoint_init_from_json
106 subroutine adjoint_case_init_common(this, neko_case)
107 class(adjoint_case_t),
target,
intent(inout) :: this
108 type(case_t),
intent(inout) :: neko_case
110 real(kind=rp) :: real_val = 0.0_rp
111 character(len=:),
allocatable :: string_val
113 integer :: n_scalars_primal, n_scalars_adjoint, i
114 logical :: scalar = .false.
115 logical :: temperature_found = .false.
118 type(json_file) :: ic_json, numerics_params
119 type(json_file) :: scalar_params_primal, scalar_params_adjoint, json_subdict
120 character(len=:),
allocatable :: json_key
121 logical :: dealias_adjoint_scalar_convection
126 call json_get(neko_case%params,
'case.fluid.scheme', string_val)
129 call json_get(neko_case%params,
'case.numerics.polynomial_order', lx)
132 this%chkp%tlag => this%time%tlag
133 this%chkp%dtlag => this%time%dtlag
135 select type (f => this%fluid_adj)
137 call f%init(neko_case%msh, lx, neko_case%params, &
138 neko_case%user, this%chkp)
147 n_scalars_adjoint = 0
148 if (neko_case%params%valid_path(
'case.adjoint_scalar'))
then
149 call json_get_or_default(neko_case%params, &
150 'case.adjoint_scalar.enabled', scalar, .true.)
151 n_scalars_adjoint = 1
153 else if (neko_case%params%valid_path(
'case.adjoint_scalars'))
then
154 call neko_case%params%info(
'case.adjoint_scalars', &
155 n_children = n_scalars_adjoint)
156 call neko_case%params%info(
'case.scalars', n_children = n_scalars_primal)
157 if (n_scalars_adjoint > 0)
then
162 this%have_scalar = scalar
169 if (this%have_scalar)
then
170 allocate(this%adjoint_scalars)
171 call json_get(neko_case%params,
'case.numerics', &
173 if (neko_case%params%valid_path(
'case.adjoint_scalar'))
then
175 call json_get(neko_case%params,
'case.adjoint_scalar', &
176 scalar_params_adjoint)
177 call json_get(neko_case%params,
'case.scalar', &
178 scalar_params_primal)
179 call this%adjoint_scalars%init(neko_case%msh, neko_case%fluid%c_Xh, &
180 neko_case%fluid%gs_Xh, scalar_params_adjoint, &
181 scalar_params_primal, numerics_params, neko_case%user, &
182 neko_case%chkp, neko_case%fluid%ulag, neko_case%fluid%vlag, &
183 neko_case%fluid%wlag, neko_case%fluid%ext_bdf, &
186 allocate(this%adjoint_convection_term)
188 call json_get_or_default(neko_case%params, &
189 'case.adjoint_scalar.dealias_coupling_term', &
190 dealias_adjoint_scalar_convection, .true.)
191 call this%adjoint_convection_term%init_from_components( &
192 this%fluid_adj%f_adj_x, this%fluid_adj%f_adj_y, &
193 this%fluid_adj%f_adj_z, this%case%scalars%scalar_fields(1)%s, &
194 this%adjoint_scalars%adjoint_scalar_fields(1)%s_adj, &
195 this%fluid_adj%c_Xh, this%fluid_adj%c_Xh_GL, &
196 this%fluid_adj%GLL_to_GL, &
197 dealias_adjoint_scalar_convection, this%fluid_adj%scratch_GL)
199 select type (f => this%fluid_adj)
202 call f%source_term%add(this%adjoint_convection_term)
207 call json_get(this%case%params, &
208 'case.adjoint_scalars', scalar_params_adjoint)
209 call json_get(this%case%params, &
210 'case.scalars', scalar_params_primal)
211 call this%adjoint_scalars%init(n_scalars_adjoint, n_scalars_primal, &
212 neko_case%msh, neko_case%fluid%c_Xh, neko_case%fluid%gs_Xh, &
213 scalar_params_adjoint, scalar_params_primal, numerics_params, &
214 neko_case%user, neko_case%chkp, neko_case%fluid%ulag, &
215 neko_case%fluid%vlag, neko_case%fluid%wlag, &
216 neko_case%fluid%ext_bdf, neko_case%fluid%rho)
217 call neko_error(
'The adjoint scaling coupling term have not yet' // &
218 'been implemented for multiple scalars')
225 call json_get(this%case%params,
'case.time', json_subdict)
226 call this%time%init(json_subdict)
249 call neko_log%section(
"Adjoint initial condition")
251 'case.adjoint_fluid.initial_condition',
'case.fluid.initial_condition')
253 call json_get(neko_case%params, json_key, ic_json)
254 call json_get(ic_json,
'type', string_val)
256 if (trim(string_val) .ne.
'user')
then
258 this%fluid_adj%u_adj, this%fluid_adj%v_adj, this%fluid_adj%w_adj, &
259 this%fluid_adj%p_adj, this%fluid_adj%c_Xh, this%fluid_adj%gs_Xh, &
263 this%fluid_adj%u_adj, this%fluid_adj%v_adj, this%fluid_adj%w_adj, &
264 this%fluid_adj%p_adj, this%fluid_adj%c_Xh, this%fluid_adj%gs_Xh, &
265 neko_case%user%initial_conditions, neko_case%fluid%name)
268 call neko_log%end_section()
270 if (this%have_scalar)
then
272 if (neko_case%params%valid_path(
'case.adjoint_scalar'))
then
274 call json_get(neko_case%params, &
275 'case.adjoint_scalar.initial_condition.type', string_val)
276 call json_get(neko_case%params, &
277 'case.adjoint_scalar.initial_condition', ic_json)
281 if (trim(string_val) .ne.
'user')
then
282 if (trim(neko_case%scalars%scalar_fields(1)%name) .eq. &
285 this%adjoint_scalars%adjoint_scalar_fields(1)%s_adj, &
286 this%adjoint_scalars%adjoint_scalar_fields(1)%c_Xh, &
287 this%adjoint_scalars%adjoint_scalar_fields(1)%gs_Xh, &
288 string_val, ic_json, 0)
291 this%adjoint_scalars%adjoint_scalar_fields(1)%s_adj, &
292 this%adjoint_scalars%adjoint_scalar_fields(1)%c_Xh, &
293 this%adjoint_scalars%adjoint_scalar_fields(1)%gs_Xh, &
294 string_val, ic_json, 1)
297 call neko_error(
"user ICs not implemented for adjoint scalar")
307 do i = 1, n_scalars_adjoint
308 call json_extract_item(neko_case%params,
'case.adjoint_scalars', &
309 i, scalar_params_adjoint)
310 call json_get(scalar_params_adjoint, &
311 'initial_condition.type', string_val)
312 call json_get(scalar_params_adjoint, &
313 'initial_condition', json_subdict)
315 if (trim(string_val) .ne.
'user')
then
316 if (trim(neko_case%scalars%scalar_fields(i)%name) .eq. &
319 this%adjoint_scalars%adjoint_scalar_fields(i)%s_adj, &
320 this%adjoint_scalars%adjoint_scalar_fields(i)%c_Xh, &
321 this%adjoint_scalars%adjoint_scalar_fields(i)%gs_Xh, &
322 string_val, json_subdict, 0)
323 temperature_found = .true.
325 if (temperature_found)
then
328 this%adjoint_scalars%adjoint_scalar_fields(i)%s_adj, &
329 this%adjoint_scalars%adjoint_scalar_fields(i)%c_Xh, &
330 this%adjoint_scalars%adjoint_scalar_fields(i)%gs_Xh, &
331 string_val, json_subdict, i - 1)
335 this%adjoint_scalars%adjoint_scalar_fields(i)%s_adj, &
336 this%adjoint_scalars%adjoint_scalar_fields(i)%c_Xh, &
337 this%adjoint_scalars%adjoint_scalar_fields(i)%gs_Xh, &
338 string_val, json_subdict, i)
342 call neko_error(
"user ICs not implemented for adjoint scalar")
349 select type (f => this%fluid_adj)
351 call f%ulag%set(f%u_adj)
352 call f%vlag%set(f%v_adj)
353 call f%wlag%set(f%w_adj)
359 call this%fluid_adj%validate
361 if (this%have_scalar)
then
362 call this%adjoint_scalars%validate()
368 call json_get_or_default(neko_case%params,
'case.output_precision', &
369 string_val,
'single')
371 if (trim(string_val) .eq.
'double')
then
380 call this%output_controller%init(this%time%end_time)
381 if (this%have_scalar)
then
383 this%adjoint_scalars, path = trim(neko_case%output_directory))
386 path = trim(neko_case%output_directory))
389 call json_get_or_default(neko_case%params,
'case.fluid.output_control',&
392 if (trim(string_val) .eq.
'org')
then
394 call json_get(neko_case%params,
'case.nsamples', real_val)
395 call this%output_controller%add(this%f_out, real_val,
'nsamples')
396 else if (trim(string_val) .eq.
'never')
then
398 call json_get_or_default(neko_case%params,
'case.fluid.output_value', &
400 call this%output_controller%add(this%f_out, 0.0_rp, string_val)
402 call json_get(neko_case%params,
'case.fluid.output_value', real_val)
403 call this%output_controller%add(this%f_out, real_val, string_val)
431 end subroutine adjoint_case_init_common
434 subroutine adjoint_free(this)
435 class(adjoint_case_t),
intent(inout) :: this
438 if (
allocated(this%adjoint_scalars))
then
439 call this%adjoint_scalars%free()
440 deallocate(this%adjoint_scalars)
443 if (
allocated(this%fluid_adj))
then
444 call this%fluid_adj%free()
445 deallocate(this%fluid_adj)
447 call this%output_controller%free()
449 end subroutine adjoint_free
451end 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.