Neko-TOP
A portable framework for high-order spectral element flow toplogy optimization.
Loading...
Searching...
No Matches
simulation_adjoint.f90
Go to the documentation of this file.
1
34!
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
47 use adjoint_case, only: adjoint_case_t
48 use device_math, only: device_glsc3
49 use math, only: glsc3
50 use vector, only: vector_t
51 implicit none
52 private
53
56
57contains
58
59
61 subroutine simulation_adjoint_init(C, dt_controller)
62 type(adjoint_case_t), intent(inout) :: c
63 type(time_step_controller_t), intent(inout) :: dt_controller
64 character(len=LOG_SIZE) :: log_buf
65
66 ! Write the initial logging message
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
73 else
74 write(log_buf, '(A, E15.7)') 'CFL : ', dt_controller%cfl_trg
75 end if
76 call neko_log%message(log_buf)
77
78 ! Call stats, samplers and user-init before time loop
79 call neko_log%section('Postprocessing')
80 call c%output_controller%execute(c%time)
81 call simulation_adjoint_norm_output(c, c%time)
82
83 call c%case%user%initialize(c%time)
84 call neko_log%end_section()
85 call neko_log%newline()
86
87 end subroutine simulation_adjoint_init
88
91 type(adjoint_case_t), intent(inout) :: c
92 logical :: output_at_end
93
94 ! Run a final output if specified in the json
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)
98
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)
101 end if
102
103 ! Finalize the user modules
104 call c%case%user%finalize(c%time)
105
106 call neko_log%end_section('Normal end.')
107
108 end subroutine simulation_adjoint_finalize
109
111 subroutine simulation_adjoint_step(C, dt_controller, cfl, &
112 tstep_loop_start_time, final_time)
113 type(adjoint_case_t), intent(inout) :: c
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
121
122 ! Setup the time step, and start time
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
127
128 ! Compute the next time step
129 ! NOTE. we should be wary here since CFL is based on the convective velocity
130 ! not the adjoint velocity
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)
134
135 ! Calculate the cfl after the possibly varied dt
136 ! cfl = C%fluid_adj%compute_cfl(C%time%dt)
137
138 ! Advance time step from t to t+dt and print the status
139 call simulation_settime(c%time, c%fluid_adj%ext_bdf)
140 ! for cosmetic reasons we want the simulation to run backwards
141 if (present(final_time)) then
142 t_bkp = c%time%t
143 c%time%t = final_time - t_bkp
144 end if
145 call c%time%status()
146 if (present(final_time)) then
147 c%time%t = t_bkp
148 end if
149 call neko_log%begin()
150
151 write(log_buf, '(A,E15.7,1x,A,E15.7)') 'CFL:', cfl, 'dt:', c%time%dt
152 call neko_log%message(log_buf)
153
154 ! Scalar step
155 ! (Note that for the adjoint we should the adjoint_scalars first)
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)
165 end if
166
167 ! Fluid step
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)
174
175 ! Postprocessing
176 call neko_log%section('Postprocessing')
177
178 ! Run any IO needed.
179 call c%output_controller%execute(c%time)
180 call simulation_adjoint_norm_output(c, c%time)
181
182 call neko_log%end_section()
183
184 ! End the step and print summary
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)
194
195 call neko_log%end_section()
196 call neko_log%end()
197 call profiler_end_region
198
199 if (present(final_time)) then
200 c%time%t = t_bkp
201 end if
202
203 end subroutine simulation_adjoint_step
204
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
208 integer :: i
209
210 if (allocated(ext_bdf)) then
211 do i = 10, 2, -1
212 time%tlag(i) = time%tlag(i-1)
213 time%dtlag(i) = time%dtlag(i-1)
214 end do
215
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
221 end if
222
223 call ext_bdf%set_coeffs(time%dtlag)
224 end if
225
226 time%t = time%t + time%dt
227
228 end subroutine simulation_settime
229
232 type(adjoint_case_t), intent(inout) :: c
233 integer :: i
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
238 real(kind=rp) :: tol
239 logical :: found
240
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)
243
244 if (found) then
245 call previous_meshf%init(trim(restart_mesh_file))
246 call previous_meshf%read(c%fluid_adj%chkp%previous_mesh)
247 end if
248
249 call c%case%params%get('case.mesh2mesh_tolerance', tol, found)
250
251 if (found) c%case%fluid%chkp%mesh2mesh_tol = tol
252
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
257
258 ! Free the previous mesh, dont need it anymore
259 do i = 1, size(c%time%dtlag)
260 call c%case%fluid%ext_bdf%set_coeffs(c%time%dtlag)
261 end do
262
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)
267 end if
268
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()
276
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)
280 end if
281 end subroutine simulation_adjoint_restart
282
283 subroutine simulation_adjoint_norm_output(C, time_output)
284 type(adjoint_case_t), intent(inout) :: c
285 type(time_state_t), intent(in) :: time_output
286 type(vector_t) :: data_line
287 real(kind=rp) :: norm_l2
288 integer :: n
289
290 if (.not. c%norm_output_enabled) return
291 if (.not. c%norm_output_ctrl%check(time_output)) return
292
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)
301 else
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)
308 end if
309
310 norm_l2 = sqrt(norm_l2) / c%fluid_adj%c_Xh%volume
311
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
318
320 subroutine simulation_adjoint_joblimit_chkp(C, t)
321 type(adjoint_case_t), intent(inout) :: c
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
327 logical :: found
328
329 call c%case%params%get('case.checkpoint_format', chkp_format, found)
330 call c%case%fluid%chkp%sync_host()
331 format_str = '.chkp'
332 if (found) then
333 if (chkp_format .eq. 'hdf5') then
334 format_str = '.h5'
335 end if
336 end if
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)
341
342 end subroutine simulation_adjoint_joblimit_chkp
343
344end module simulation_adjoint
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...