37 use mpi_f08,
only: mpi_wtime
38 use neko_config,
only: neko_bcknd_device
39 use num_types,
only: rp, dp
40 use time_scheme_controller,
only: time_scheme_controller_t
41 use file,
only: file_t
42 use logger,
only: log_size, neko_log
43 use profiler,
only: profiler_start_region, profiler_end_region
44 use json_utils,
only: json_get_or_default
45 use time_state,
only : time_state_t
46 use time_step_controller,
only: time_step_controller_t
48 use device_math,
only: device_glsc3
50 use vector,
only: vector_t
63 type(time_step_controller_t),
intent(inout) :: dt_controller
64 character(len=LOG_SIZE) :: log_buf
67 call neko_log%section(
'Adjoint Starting simulation')
68 write(log_buf,
'(A, E15.7,A,E15.7,A)') &
69 'T : [', c%time%t,
',', c%time%end_time,
']'
70 call neko_log%message(log_buf)
71 if (.not. dt_controller%is_variable_dt)
then
72 write(log_buf,
'(A, E15.7)')
'dt : ', c%time%dt
74 write(log_buf,
'(A, E15.7)')
'CFL : ', dt_controller%cfl_trg
76 call neko_log%message(log_buf)
79 call neko_log%section(
'Postprocessing')
80 call c%output_controller%execute(c%time)
81 call simulation_adjoint_norm_output(c, c%time)
83 call c%case%user%initialize(c%time)
84 call neko_log%end_section()
85 call neko_log%newline()
92 logical :: output_at_end
95 call json_get_or_default(c%case%params,
'case.output_at_end', &
96 output_at_end, .true.)
97 call c%output_controller%execute(c%time, output_at_end)
99 if (.not. (output_at_end) .and. c%time%t .lt. c%time%end_time)
then
100 call simulation_adjoint_joblimit_chkp(c, c%time%t)
104 call c%case%user%finalize(c%time)
106 call neko_log%end_section(
'Normal end.')
112 tstep_loop_start_time, final_time)
114 real(kind=rp),
intent(inout) :: cfl
115 type(time_step_controller_t),
intent(inout) :: dt_controller
116 real(kind=dp),
intent(in) :: tstep_loop_start_time
117 real(kind=rp),
optional,
intent(in) :: final_time
118 real(kind=rp) :: t_bkp
119 real(kind=dp) :: start_time, end_time, tstep_start_time
120 character(len=LOG_SIZE) :: log_buf
123 call profiler_start_region(
'Time-Step Adjoint')
124 c%time%tstep = c%time%tstep + 1
125 start_time = mpi_wtime()
126 tstep_start_time = start_time
131 cfl = c%fluid_adj%compute_cfl(c%time%dt)
132 call dt_controller%set_dt(c%time, cfl)
133 if (dt_controller%is_variable_dt) cfl = c%fluid_adj%compute_cfl(c%time%dt)
139 call simulation_settime(c%time, c%fluid_adj%ext_bdf)
141 if (
present(final_time))
then
143 c%time%t = final_time - t_bkp
146 if (
present(final_time))
then
149 call neko_log%begin()
151 write(log_buf,
'(A,E15.7,1x,A,E15.7)')
'CFL:', cfl,
'dt:', c%time%dt
152 call neko_log%message(log_buf)
156 if (
allocated(c%adjoint_scalars))
then
157 start_time = mpi_wtime()
158 call neko_log%section(
'Adjoint scalar')
159 call c%adjoint_scalars%step(c%time, &
160 c%case%fluid%ext_bdf, dt_controller)
161 end_time = mpi_wtime()
162 write(log_buf,
'(A,E15.7)') &
163 'Scalar step time: ', end_time-start_time
164 call neko_log%end_section(log_buf)
168 call neko_log%section(
'Adjoint fluid')
169 call c%fluid_adj%step(c%time, dt_controller)
170 end_time = mpi_wtime()
171 write(log_buf,
'(A,E15.7)') &
172 'Fluid step time (s): ', end_time-start_time
173 call neko_log%end_section(log_buf)
176 call neko_log%section(
'Postprocessing')
179 call c%output_controller%execute(c%time)
180 call simulation_adjoint_norm_output(c, c%time)
182 call neko_log%end_section()
185 end_time = mpi_wtime()
186 call neko_log%section(
'Step summary')
187 write(log_buf,
'(A,I8,A,E15.7)') &
188 'Total time for step ', c%time%tstep,
' (s): ', &
189 end_time - tstep_start_time
190 call neko_log%message(log_buf)
191 write(log_buf,
'(A,E15.7)') &
192 'Total elapsed time (s): ', end_time-tstep_loop_start_time
193 call neko_log%message(log_buf)
195 call neko_log%end_section()
197 call profiler_end_region
199 if (
present(final_time))
then
205 subroutine simulation_settime(time, ext_bdf)
206 type(time_state_t),
intent(inout) :: time
207 type(time_scheme_controller_t),
intent(inout),
allocatable :: ext_bdf
210 if (
allocated(ext_bdf))
then
212 time%tlag(i) = time%tlag(i-1)
213 time%dtlag(i) = time%dtlag(i-1)
216 time%dtlag(1) = time%dt
217 time%tlag(1) = time%t
218 if (ext_bdf%ndiff .eq. 0)
then
219 time%dtlag(2) = time%dt
220 time%tlag(2) = time%t
223 call ext_bdf%set_coeffs(time%dtlag)
226 time%t = time%t + time%dt
228 end subroutine simulation_settime
234 type(file_t) :: chkpf, previous_meshf
235 character(len=LOG_SIZE) :: log_buf
236 character(len=:),
allocatable :: restart_file
237 character(len=:),
allocatable :: restart_mesh_file
241 call c%case%params%get(
'case.restart_file', restart_file, found)
242 call c%case%params%get(
'case.restart_mesh_file', restart_mesh_file, found)
245 call previous_meshf%init(trim(restart_mesh_file))
246 call previous_meshf%read(c%fluid_adj%chkp%previous_mesh)
249 call c%case%params%get(
'case.mesh2mesh_tolerance', tol, found)
251 if (found) c%case%fluid%chkp%mesh2mesh_tol = tol
253 call chkpf%init(trim(restart_file))
254 call chkpf%read(c%fluid_adj%chkp)
255 c%time%dtlag = c%fluid_adj%chkp%dtlag
256 c%time%tlag = c%fluid_adj%chkp%tlag
259 do i = 1,
size(c%time%dtlag)
260 call c%case%fluid%ext_bdf%set_coeffs(c%time%dtlag)
263 call c%fluid_adj%restart(c%case%chkp)
264 call c%case%fluid%chkp%previous_mesh%free()
265 if (
allocated(c%adjoint_scalars))
then
266 call c%adjoint_scalars%restart(c%case%chkp)
269 c%time%t = real(c%case%fluid%chkp%restart_time(), kind=rp)
270 call neko_log%section(
'Restarting from checkpoint')
271 write(log_buf,
'(A,A)')
'File : ', trim(restart_file)
272 call neko_log%message(log_buf)
273 write(log_buf,
'(A,E15.7)')
'Time : ', c%time%t
274 call neko_log%message(log_buf)
275 call neko_log%end_section()
277 call c%output_controller%set_counter(c%time)
278 if (c%norm_output_enabled)
then
279 call c%norm_output_ctrl%set_counter(c%time)
283 subroutine simulation_adjoint_norm_output(C, time_output)
285 type(time_state_t),
intent(in) :: time_output
286 type(vector_t) :: data_line
287 real(kind=rp) :: norm_l2
290 if (.not. c%norm_output_enabled)
return
291 if (.not. c%norm_output_ctrl%check(time_output))
return
293 n = c%fluid_adj%c_Xh%dof%size()
294 if (neko_bcknd_device .eq. 1)
then
295 norm_l2 = device_glsc3(c%fluid_adj%u_adj%x_d, &
296 c%fluid_adj%u_adj%x_d, c%fluid_adj%c_Xh%B_d, n) + &
297 device_glsc3(c%fluid_adj%v_adj%x_d, &
298 c%fluid_adj%v_adj%x_d, c%fluid_adj%c_Xh%B_d, n) + &
299 device_glsc3(c%fluid_adj%w_adj%x_d, &
300 c%fluid_adj%w_adj%x_d, c%fluid_adj%c_Xh%B_d, n)
302 norm_l2 = glsc3(c%fluid_adj%u_adj%x, c%fluid_adj%u_adj%x, &
303 c%fluid_adj%c_Xh%B, n) + &
304 glsc3(c%fluid_adj%v_adj%x, c%fluid_adj%v_adj%x, &
305 c%fluid_adj%c_Xh%B, n) + &
306 glsc3(c%fluid_adj%w_adj%x, c%fluid_adj%w_adj%x, &
307 c%fluid_adj%c_Xh%B, n)
310 norm_l2 = sqrt(norm_l2) / c%fluid_adj%c_Xh%volume
312 call data_line%init(1)
313 data_line%x = [norm_l2]
314 call c%norm_output_file%write(data_line, time_output%t)
315 call data_line%free()
316 call c%norm_output_ctrl%register_execution()
317 end subroutine simulation_adjoint_norm_output
320 subroutine simulation_adjoint_joblimit_chkp(C, t)
322 real(kind=rp),
intent(inout) :: t
323 type(file_t) :: chkpf
324 character(len=:),
allocatable :: chkp_format
325 character(len=LOG_SIZE) :: log_buf
326 character(len=10) :: format_str
329 call c%case%params%get(
'case.checkpoint_format', chkp_format, found)
330 call c%case%fluid%chkp%sync_host()
333 if (chkp_format .eq.
'hdf5')
then
337 call chkpf%init(c%case%output_directory //
'joblimit' // trim(format_str))
338 call chkpf%write(c%case%fluid%chkp, t)
339 write(log_buf,
'(A)')
'! saving checkpoint >>>'
340 call neko_log%message(log_buf)
342 end subroutine simulation_adjoint_joblimit_chkp
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_restart(c)
Restart a case C from a given checkpoint.
subroutine, public simulation_adjoint_finalize(c)
Finalize a simulation of a case.
Adjoint case type. Todo: This should Ideally be a subclass of case_t, however, that is not yet suppor...