36 use case,
only: case_t
37 use neko,
only: neko_init, neko_finalize, neko_solve
39 use fluid_scheme_incompressible,
only: fluid_scheme_incompressible_t
42 use scalar_pnpn,
only: scalar_pnpn_t
45 use scalars,
only: scalars_t
46 use scalar_scheme,
only: scalar_scheme_t
47 use fluid_pnpn,
only: fluid_pnpn_t
48 use time_step_controller,
only: time_step_controller_t
49 use time_state,
only: time_state_t
50 use fld_file_output,
only: fld_file_output_t
51 use chkp_output,
only: chkp_output_t
52 use simcomp_executor,
only: neko_simcomps
54 use field,
only: field_t
55 use field_registry,
only: neko_field_registry
56 use field_math,
only: field_rzero, field_copy
57 use checkpoint,
only: chkp_t
58 use file,
only: file_t
59 use utils,
only: neko_warning, neko_error
60 use comm,
only: pe_rank
61 use json_file_module,
only: json_file
62 use json_utils,
only: json_extract_item, json_get_or_default
63 use num_types,
only: rp, sp, dp
64 use logger,
only: log_size, neko_log
65 use mpi_f08,
only: mpi_wtime
66 use jobctrl,
only: jobctrl_time_limit
67 use profiler,
only: profiler_start, profiler_stop
69 simulation_adjoint_step, simulation_adjoint_finalize
70 use simulation,
only: simulation_init, simulation_step, simulation_finalize, &
79 type(case_t),
public :: neko_case
83 class(fluid_scheme_incompressible_t),
public,
pointer :: fluid => null()
85 type(scalars_t),
public,
pointer :: scalars => null()
92 type(fld_file_output_t),
public :: output_forward
95 type(fld_file_output_t),
public :: output_adjoint
97 logical :: have_scalar = .false.
98 integer :: n_timesteps = 0
104 logical :: checkpoint_enable = .false.
110 procedure, pass(this) :: init => simulation_initialize
112 procedure, pass(this) :: free => simulation_free
114 procedure, pass(this) :: run_forward => simulation_run_forward
116 procedure, pass(this) :: run_backward => simulation_run_backward
118 procedure, pass(this) ::
reset => simulation_reset
120 procedure, pass(this) :: write => simulation_write
127 subroutine simulation_initialize(this, parameters)
129 type(json_file),
intent(inout) :: parameters
130 integer :: i, n_scalars
133 call neko_init(this%neko_case)
137 select type (fluid => this%neko_case%fluid)
138 type is (fluid_pnpn_t)
142 select type (adjoint_fluid => this%adjoint_case%fluid_adj)
144 this%adjoint_fluid => adjoint_fluid
147 if (
allocated(this%neko_case%scalars))
then
148 this%scalars => this%neko_case%scalars
151 if (
allocated(this%adjoint_case%adjoint_scalars))
then
152 this%adjoint_scalars => this%adjoint_case%adjoint_scalars
159 if (
allocated(this%neko_case%scalars))
then
160 n_scalars =
size(this%neko_case%scalars%scalar_fields)
162 call this%output_forward%init(sp,
'forward_fields', 4 + n_scalars)
164 call this%output_forward%fields%assign(1, this%fluid%p)
165 call this%output_forward%fields%assign(2, this%fluid%u)
166 call this%output_forward%fields%assign(3, this%fluid%v)
167 call this%output_forward%fields%assign(4, this%fluid%w)
170 if (
allocated(this%neko_case%scalars))
then
172 call this%output_forward%fields%assign(4 + i, &
173 this%scalars%scalar_fields(i)%s)
178 if (
allocated(this%adjoint_case%adjoint_scalars))
then
179 n_scalars =
size(this%adjoint_case%adjoint_scalars%adjoint_scalar_fields)
181 call this%output_adjoint%init(sp,
'adjoint_fields', 4 + n_scalars)
182 call this%output_adjoint%fields%assign(1, this%adjoint_fluid%p_adj)
183 call this%output_adjoint%fields%assign(2, this%adjoint_fluid%u_adj)
184 call this%output_adjoint%fields%assign(3, this%adjoint_fluid%v_adj)
185 call this%output_adjoint%fields%assign(4, this%adjoint_fluid%w_adj)
188 if (
allocated(this%adjoint_case%adjoint_scalars))
then
190 call this%output_adjoint%fields%assign(4 + i, &
191 this%adjoint_scalars%adjoint_scalar_fields(i)%s_adj)
195 call json_get_or_default(parameters,
"checkpoints.enable", &
196 this%checkpoint_enable, .false.)
197 if (this%checkpoint_enable)
then
198 call this%checkpoint%init(this%neko_case, parameters)
201 end subroutine simulation_initialize
204 subroutine simulation_free(this)
207 if (this%checkpoint_enable)
then
208 call this%checkpoint%free()
210 call adjoint_free(this%adjoint_case)
211 call neko_finalize(this%neko_case)
213 end subroutine simulation_free
216 subroutine simulation_run_forward(this)
218 type(time_step_controller_t) :: dt_controller
219 real(kind=dp) :: loop_start
221 call dt_controller%init(this%neko_case%params)
223 call simulation_init(this%neko_case, dt_controller)
226 loop_start = mpi_wtime()
227 do while (this%neko_case%time%t .lt. this%neko_case%time%end_time)
228 this%n_timesteps = this%n_timesteps + 1
230 call simulation_step(this%neko_case, dt_controller, loop_start)
232 if (this%checkpoint_enable)
then
233 call this%checkpoint%save(this%neko_case)
238 call simulation_finalize(this%neko_case)
240 end subroutine simulation_run_forward
243 subroutine simulation_run_backward(this)
245 type(time_step_controller_t) :: dt_controller
246 real(kind=dp) :: loop_start
250 call dt_controller%init(this%neko_case%params)
252 call simulation_adjoint_init(this%adjoint_case, dt_controller)
255 cfl = this%adjoint_case%fluid_adj%compute_cfl(this%adjoint_case%time%dt)
256 loop_start = mpi_wtime()
258 do i = this%n_timesteps, 1, -1
259 if (this%checkpoint_enable)
then
260 call this%checkpoint%restore(this%neko_case, i)
263 call simulation_adjoint_step(this%adjoint_case, dt_controller, cfl, &
268 call simulation_adjoint_finalize(this%adjoint_case)
270 end subroutine simulation_run_backward
273 subroutine simulation_reset(this)
275 integer :: i, n_scalars
277 call reset(this%neko_case)
282 this%adjoint_case%time%t = 0.0_rp
283 this%adjoint_case%time%tstep = 0
286 call field_rzero(this%adjoint_case%fluid_adj%u_adj)
287 call field_rzero(this%adjoint_case%fluid_adj%v_adj)
288 call field_rzero(this%adjoint_case%fluid_adj%w_adj)
290 if (
allocated(this%adjoint_case%adjoint_scalars))
then
291 n_scalars =
size(this%adjoint_case%adjoint_scalars%adjoint_scalar_fields)
294 this%adjoint_case%adjoint_scalars%adjoint_scalar_fields(i)%s_adj)
298 if (this%checkpoint_enable)
then
299 call this%checkpoint%reset()
301 end subroutine simulation_reset
304 subroutine simulation_write(this, idx)
306 integer,
intent(in) :: idx
308 call this%output_forward%sample(real(idx, kind=rp))
309 call this%output_adjoint%sample(real(idx, kind=rp))
311 end subroutine simulation_write
Adjoint Pn/Pn formulation.
Contains the adjoint_scalar_pnpn_t type.
Contains the adjoint_scalars_t type that manages multiple scalar fields.
Contains extensions to the neko library required to run the topology optimization code.
subroutine, public reset(neko_case)
Reset the case data structure.
Adjoint simulation driver.
Implements the steady_problem_t type.
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.
Type to manage multiple adjoint scalar transport equations.