38 use case,
only: case_t
39 use neko,
only: neko_solve
41 use fluid_scheme_incompressible,
only: fluid_scheme_incompressible_t
44 use scalar_pnpn,
only: scalar_pnpn_t
47 use scalars,
only: scalars_t
48 use scalar_scheme,
only: scalar_scheme_t
49 use fluid_pnpn,
only: fluid_pnpn_t
50 use time_step_controller,
only: time_step_controller_t
51 use time_state,
only: time_state_t
52 use fld_file_output,
only: fld_file_output_t
53 use chkp_output,
only: chkp_output_t
54 use simcomp_executor,
only: neko_simcomps
56 use field,
only: field_t
57 use registry,
only: neko_registry
58 use field_math,
only: field_rzero, field_copy
59 use checkpoint,
only: chkp_t
60 use file,
only: file_t
61 use utils,
only: neko_warning, neko_error
62 use comm,
only: pe_rank
63 use json_file_module,
only: json_file
64 use json_utils,
only: json_get, json_get_or_default
65 use num_types,
only: rp, sp, dp
66 use logger,
only: log_size, neko_log
67 use mpi_f08,
only: mpi_wtime
68 use jobctrl,
only: jobctrl_time_limit
69 use profiler,
only: profiler_start, profiler_stop, &
70 profiler_start_region, profiler_end_region
73 use simulation,
only: simulation_init, simulation_step, simulation_finalize, &
76 use runtime_stats,
only: neko_rt_stats
77 use scratch_registry,
only: neko_scratch_registry
78 use registry,
only: neko_registry
85 type(case_t),
public :: neko_case
89 class(fluid_scheme_incompressible_t),
public,
pointer :: fluid => null()
91 type(scalars_t),
public,
pointer :: scalars => null()
98 type(fld_file_output_t),
public :: output_forward
101 type(fld_file_output_t),
public :: output_adjoint
103 logical :: unsteady = .false.
105 logical :: have_scalar = .false.
106 integer :: n_timesteps = 0
118 procedure, pass(this) :: free => simulation_free
120 procedure, pass(this) :: run_forward => simulation_run_forward
122 procedure, pass(this) :: run_backward => simulation_run_backward
124 procedure, pass(this) ::
reset => simulation_reset
126 procedure, pass(this) :: set_output_counter => &
127 simulation_set_output_counter
129 procedure, pass(this) :: write => simulation_write
131 procedure, pass(this) :: write_forward => simulation_write_forward
133 procedure, pass(this) :: write_adjoint => simulation_write_adjoint
142 type(json_file),
intent(inout) :: parameters
143 type(json_file) :: checkpoint_params
144 integer :: i, n_scalars, unsteady_support
148 call this%neko_case%init(parameters)
149 call neko_rt_stats%init(parameters)
150 call neko_simcomps%init(this%neko_case)
153 call this%adjoint_case%init(this%neko_case)
158 select type (fluid => this%neko_case%fluid)
159 type is (fluid_pnpn_t)
163 select type (adjoint_fluid => this%adjoint_case%fluid_adj)
165 this%adjoint_fluid => adjoint_fluid
168 if (
allocated(this%neko_case%scalars))
then
169 this%scalars => this%neko_case%scalars
172 if (
allocated(this%adjoint_case%adjoint_scalars))
then
173 this%adjoint_scalars => this%adjoint_case%adjoint_scalars
180 if (
allocated(this%neko_case%scalars))
then
181 n_scalars =
size(this%neko_case%scalars%scalar_fields)
183 call this%output_forward%init(sp,
'forward_fields', 4 + n_scalars)
185 call this%output_forward%fields%assign(1, this%fluid%p)
186 call this%output_forward%fields%assign(2, this%fluid%u)
187 call this%output_forward%fields%assign(3, this%fluid%v)
188 call this%output_forward%fields%assign(4, this%fluid%w)
191 if (
allocated(this%neko_case%scalars))
then
193 call this%output_forward%fields%assign(4 + i, &
194 this%scalars%scalar_fields(i)%scalar%s)
199 if (
allocated(this%adjoint_case%adjoint_scalars))
then
200 n_scalars =
size(this%adjoint_case%adjoint_scalars%adjoint_scalar_fields)
202 call this%output_adjoint%init(sp,
'adjoint_fields', 4 + n_scalars)
203 call this%output_adjoint%fields%assign(1, this%adjoint_fluid%p_adj)
204 call this%output_adjoint%fields%assign(2, this%adjoint_fluid%u_adj)
205 call this%output_adjoint%fields%assign(3, this%adjoint_fluid%v_adj)
206 call this%output_adjoint%fields%assign(4, this%adjoint_fluid%w_adj)
209 if (
allocated(this%adjoint_case%adjoint_scalars))
then
211 call this%output_adjoint%fields%assign(4 + i, &
212 this%adjoint_scalars%adjoint_scalar_fields(i)%s_adj)
217 call json_get_or_default(parameters,
"unsteady", unsteady, .false.)
218 this%unsteady = unsteady
221 if (this%unsteady)
then
223 if (
"checkpoints" .in. parameters)
then
224 unsteady_support = unsteady_support + 1
227 if (unsteady_support .eq. 0)
then
228 call neko_error(
"No support for unsteady simulation provided, \\ &
229 & \\ current options include enabling checkpoints.")
232 if (unsteady_support .gt. 1)
then
233 call neko_error(
"Too many supports for unsteady simulation \\ &
234 & \\ provided, please select one.")
238 if (
"checkpoints" .in. parameters)
then
239 call json_get(parameters,
'checkpoints', checkpoint_params)
240 call this%checkpoint%init(this%neko_case, checkpoint_params)
246 subroutine simulation_free(this)
253 call this%neko_case%free()
254 call this%adjoint_case%free()
255 call this%output_forward%free()
256 call this%output_adjoint%free()
257 call this%checkpoint%free()
261 nullify(this%scalars)
262 nullify(this%adjoint_fluid)
263 nullify(this%adjoint_scalars)
266 this%unsteady = .false.
267 this%have_scalar = .false.
271 call neko_simcomps%free()
273 end subroutine simulation_free
276 subroutine simulation_run_forward(this)
278 type(time_step_controller_t) :: dt_controller
279 real(kind=dp) :: loop_start
281 call dt_controller%init(this%neko_case%params)
283 call simulation_init(this%neko_case, dt_controller)
285 call profiler_start_region(
"Forward simulation")
286 loop_start = mpi_wtime()
288 do while (this%neko_case%time%t .lt. this%neko_case%time%end_time)
289 this%n_timesteps = this%n_timesteps + 1
291 call simulation_step(this%neko_case, dt_controller, loop_start)
293 call this%checkpoint%save(this%neko_case)
295 call profiler_end_region(
"Forward simulation")
297 call simulation_finalize(this%neko_case)
299 end subroutine simulation_run_forward
302 subroutine simulation_run_backward(this)
304 type(time_step_controller_t) :: dt_controller
305 real(kind=dp) :: loop_start
309 call dt_controller%init(this%neko_case%params)
313 call profiler_start_region(
"Adjoint simulation")
314 cfl = this%adjoint_case%fluid_adj%compute_cfl(this%adjoint_case%time%dt)
315 loop_start = mpi_wtime()
316 do i = this%n_timesteps, 1, -1
317 call this%checkpoint%restore(this%neko_case, i)
322 call profiler_end_region(
"Adjoint simulation")
326 end subroutine simulation_run_backward
329 subroutine simulation_reset(this)
332 call reset(this%neko_case)
334 call this%checkpoint%reset()
336 end subroutine simulation_reset
338 subroutine simulation_set_output_counter(this, idx)
340 integer,
intent(in) :: idx
342 call this%output_forward%set_counter(idx)
343 call this%output_adjoint%set_counter(idx)
345 end subroutine simulation_set_output_counter
348 subroutine simulation_write(this, idx)
350 integer,
intent(in) :: idx
352 call this%output_forward%sample(real(idx, kind=rp))
353 call this%output_adjoint%sample(real(idx, kind=rp))
355 end subroutine simulation_write
358 subroutine simulation_write_forward(this, idx)
360 integer,
intent(in) :: idx
362 call this%output_forward%sample(real(idx, kind=rp))
364 end subroutine simulation_write_forward
367 subroutine simulation_write_adjoint(this, idx)
369 integer,
intent(in) :: idx
371 call this%output_adjoint%sample(real(idx, kind=rp))
373 end subroutine simulation_write_adjoint
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.
subroutine, public reset_adjoint(adjoint_case, neko_case)
Reset the adjoint case data structure.
Adjoint simulation driver.
subroutine, public simulation_adjoint_init(c, dt_controller)
Initialise a simulation_adjoint of a case.
subroutine, public simulation_adjoint_step(c, dt_controller, cfl, tstep_loop_start_time, final_time)
Compute a single time-step of an adjoint case.
subroutine, public simulation_adjoint_finalize(c)
Finalize a simulation of a case.
Implements the steady_problem_t type.
subroutine simulation_initialize(this, parameters)
Initialize the simulation.
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.