Neko-TOP
A portable framework for high-order spectral element flow toplogy optimization.
Loading...
Searching...
No Matches
adjoint_scalar_scheme.f90
Go to the documentation of this file.
1
34!
36
38 use gather_scatter, only : gs_t
39 use checkpoint, only : chkp_t
40 use num_types, only: rp
41 use field, only : field_t
42 use field_list, only: field_list_t
43 use space, only : space_t
44 use dofmap, only : dofmap_t
45 use krylov, only : ksp_t, krylov_solver_factory, ksp_max_iter, ksp_monitor_t
46 use coefs, only : coef_t
47 use dirichlet, only : dirichlet_t
48 use neumann, only : neumann_t
49 use jacobi, only : jacobi_t
50 use device_jacobi, only : device_jacobi_t
51 use sx_jacobi, only : sx_jacobi_t
52 use hsmg, only : hsmg_t
53 use bc, only : bc_t
54 use bc_list, only : bc_list_t
55 use precon, only : pc_t, precon_factory, precon_destroy
56 use field_dirichlet, only: field_dirichlet_t, field_dirichlet_update
57 use mesh, only : mesh_t, neko_msh_max_zlbls, neko_msh_max_zlbl_len
58 use facet_zone, only : facet_zone_t
59 use time_scheme_controller, only : time_scheme_controller_t
60 use logger, only : neko_log, log_size, neko_log_verbose
61 use registry, only : neko_registry
62 use json_utils, only : json_get, json_get_or_default, json_extract_item
63 use json_module, only : json_file
64 use user_intf, only : user_t, dummy_user_material_properties, &
65 user_material_properties_intf
66 use utils, only : neko_error, neko_warning
67 use comm, only: neko_comm
68 use scalar_source_term, only : scalar_source_term_t
69 use field_series, only : field_series_t
70 use math, only : cfill, add2s2
71 use field_math, only : field_cmult, field_col3, field_cfill, field_add2, &
72 field_col2
73 use device_math, only : device_cfill, device_add2s2
74 use neko_config, only : neko_bcknd_device
75 use field_series, only : field_series_t
76 use time_step_controller, only : time_step_controller_t
77 use json_utils_ext, only: json_key_fallback
78 use scalar_scheme, only: scalar_scheme_precon_factory, &
79 scalar_scheme_solver_factory
80 use scratch_registry, only : neko_scratch_registry
81 use time_state, only : time_state_t
82 use device, only : device_memcpy, device_to_host
83 use field_math, only : field_col3, field_cmult2, field_add2, field_cfill
84 use mpi_f08, only: mpi_integer, mpi_sum
85 implicit none
86
88 type, abstract :: adjoint_scalar_scheme_t
90 character(len=:), allocatable :: name
92 character(len=:), allocatable :: primal_name
94 type(field_t), pointer :: u
96 type(field_t), pointer :: v
98 type(field_t), pointer :: w
100 type(field_t), pointer :: s
102 type(field_t), pointer :: s_adj
104 type(field_series_t) :: s_adj_lag
106 type(space_t), pointer :: xh
108 type(dofmap_t), pointer :: dm_xh
110 type(gs_t), pointer :: gs_xh
112 type(coef_t), pointer :: c_xh
114 type(field_t), pointer :: f_xh => null()
116 type(scalar_source_term_t) :: source_term
118 class(ksp_t), allocatable :: ksp
120 integer :: ksp_maxiter
122 integer :: projection_dim
123
124 integer :: projection_activ_step
126 class(pc_t), allocatable :: pc
128 type(bc_list_t) :: bcs
130 type(json_file), pointer :: params
132 type(mesh_t), pointer :: msh => null()
134 type(chkp_t), pointer :: chkp => null()
136 character(len=:), allocatable :: nut_field_name
138 type(field_t), pointer :: rho => null()
140 type(field_t) :: lambda
142 type(field_t) :: cp
144 real(kind=rp) :: pr_turb
146 type(field_list_t) :: material_properties
148 logical :: variable_material_properties = .false.
149 ! Lag arrays for the RHS.
150 type(field_t) :: abx1, abx2
151 procedure(user_material_properties_intf), nopass, pointer :: &
152 user_material_properties => null()
154 logical :: freeze = .false.
155 contains
157 procedure, pass(this) :: scheme_init => adjoint_scalar_scheme_init
159 procedure, pass(this) :: scheme_free => adjoint_scalar_scheme_free
161 procedure, pass(this) :: validate => adjoint_scalar_scheme_validate
163 procedure, pass(this) :: set_material_properties => &
166 procedure, pass(this) :: update_material_properties => &
169 procedure(adjoint_scalar_scheme_init_intrf), pass(this), deferred :: init
171 procedure(adjoint_scalar_scheme_free_intrf), pass(this), deferred :: free
173 procedure(adjoint_scalar_scheme_step_intrf), pass(this), deferred :: step
175 procedure(adjoint_scalar_scheme_restart_intrf), pass(this), deferred :: &
176 restart
178
180 abstract interface
181 subroutine adjoint_scalar_scheme_init_intrf(this, msh, coef, gs, &
182 params_adjoint, params_primal, numerics_params, user, chkp, ulag, &
183 vlag, wlag, time_scheme, rho)
185 import json_file
186 import coef_t
187 import gs_t
188 import mesh_t
189 import user_t
190 import field_series_t, field_t
191 import time_scheme_controller_t
192 import rp
193 import chkp_t
194 class(adjoint_scalar_scheme_t), target, intent(inout) :: this
195 type(mesh_t), target, intent(in) :: msh
196 type(coef_t), target, intent(in) :: coef
197 type(gs_t), target, intent(inout) :: gs
198 type(json_file), target, intent(inout) :: params_adjoint
199 type(json_file), target, intent(inout) :: params_primal
200 type(json_file), target, intent(inout) :: numerics_params
201 type(user_t), target, intent(in) :: user
202 type(chkp_t), target, intent(inout) :: chkp
203 type(field_series_t), target, intent(in) :: ulag, vlag, wlag
204 type(time_scheme_controller_t), target, intent(in) :: time_scheme
205 type(field_t), target, intent(in) :: rho
207 end interface
208
210 abstract interface
213 import chkp_t
214 import rp
215 class(adjoint_scalar_scheme_t), target, intent(inout) :: this
216 type(chkp_t), intent(inout) :: chkp
218 end interface
219
221 abstract interface
224 class(adjoint_scalar_scheme_t), intent(inout) :: this
226 end interface
227
229 abstract interface
230 subroutine adjoint_scalar_scheme_step_intrf(this, time, ext_bdf, &
231 dt_controller, ksp_results)
233 import time_state_t
234 import time_scheme_controller_t
235 import time_step_controller_t
236 import ksp_monitor_t
237 class(adjoint_scalar_scheme_t), intent(inout) :: this
238 type(time_state_t), intent(in) :: time
239 type(time_scheme_controller_t), intent(in) :: ext_bdf
240 type(time_step_controller_t), intent(in) :: dt_controller
241 type(ksp_monitor_t), intent(inout) :: ksp_results
243 end interface
244
245contains
246
257 subroutine adjoint_scalar_scheme_init(this, msh, c_Xh, gs_Xh, &
258 params_adjoint, params_primal, scheme, user, rho)
259 class(adjoint_scalar_scheme_t), target, intent(inout) :: this
260 type(mesh_t), target, intent(in) :: msh
261 type(coef_t), target, intent(in) :: c_Xh
262 type(gs_t), target, intent(inout) :: gs_Xh
263 type(json_file), target, intent(inout) :: params_primal, params_adjoint
264 character(len=*), intent(in) :: scheme
265 type(user_t), target, intent(in) :: user
266 type(field_t), target, intent(in) :: rho
267 type(json_file), pointer :: params_selected
268 ! IO buffer for log output
269 character(len=LOG_SIZE) :: log_buf
270 ! Variables for retrieving json parameters
271 logical :: logical_val
272 real(kind=rp) :: solver_abstol
273 integer :: integer_val
274 character(len=:), allocatable :: solver_type, solver_precon
275 type(json_file) :: precon_params
276
277 this%u => neko_registry%get_field('u')
278 this%v => neko_registry%get_field('v')
279 this%w => neko_registry%get_field('w')
280 this%rho => rho
281
282 ! get the primal adjoint's name
283 call json_get(params_adjoint, 'primal_name', this%primal_name)
284 ! Assign a name
285 call json_get_or_default(params_adjoint, 'name', this%name, &
286 'scalar adjoint')
287
288 call neko_log%section('Adjoint scalar')
289 params_selected => json_key_fallback(params_adjoint, params_primal, &
290 'solver.type')
291 call json_get(params_selected, 'solver.type', solver_type)
292
293 params_selected => json_key_fallback(params_adjoint, params_primal, &
294 'solver.preconditioner.type')
295 call json_get(params_selected, 'solver.preconditioner.type', solver_precon)
296
297 params_selected => json_key_fallback(params_adjoint, params_primal, &
298 'solver.preconditioner')
299 call json_get(params_selected, 'solver.preconditioner', &
300 precon_params)
301
302 params_selected => json_key_fallback(params_adjoint, params_primal, &
303 'solver.absolute_tolerance')
304 call json_get(params_selected, 'solver.absolute_tolerance', &
305 solver_abstol)
306
307 params_selected => json_key_fallback(params_adjoint, params_primal, &
308 'solver.projection_space_size')
309 call json_get_or_default(params_selected, &
310 'solver.projection_space_size', &
311 this%projection_dim, 0)
312
313 params_selected => json_key_fallback(params_adjoint, params_primal, &
314 'solver.projection_hold_steps')
315 call json_get_or_default(params_selected, &
316 'solver.projection_hold_steps', &
317 this%projection_activ_step, 5)
318
319
320 write(log_buf, '(A, A)') 'Type : ', trim(scheme)
321 call neko_log%message(log_buf)
322 write(log_buf, '(A, A)') 'Name : ', trim(this%name)
323 call neko_log%message(log_buf)
324 call neko_log%message('Ksp adjoint scalar : ('// trim(solver_type) // &
325 ', ' // trim(solver_precon) // ')')
326 write(log_buf, '(A,ES13.6)') ' `-abs tol :', solver_abstol
327 call neko_log%message(log_buf)
328
329 this%Xh => this%u%Xh
330 this%dm_Xh => this%u%dof
331 this%params => params_adjoint
332 this%msh => msh
333
334 if (.not. neko_registry%field_exists(this%name)) then
335 call neko_registry%add_field(this%dm_Xh, this%name)
336 end if
337
338 this%s_adj => neko_registry%get_field(this%name)
339
340 call this%s_adj_lag%init(this%s_adj, 2)
341
342 this%s => neko_registry%get_field(this%primal_name)
343
344 this%gs_Xh => gs_xh
345 this%c_Xh => c_xh
346
347 !
348 ! Material properties
349 !
350 call this%set_material_properties(params_primal, user)
351
352
353 !
354 ! Turbulence modelling and variable material properties
355 !
356 params_selected => json_key_fallback(params_adjoint, params_primal, &
357 'variable_material_properties')
358 if (params_selected%valid_path('variable_material_properties')) then
359 call neko_error('variable material properties no supported for adjoint')
360 end if
361
362 write(log_buf, '(A,L1)') 'LES : ', this%variable_material_properties
363 call neko_log%message(log_buf)
364
365 !
366 ! Setup right-hand side field.
367 !
368 allocate(this%f_Xh)
369 call this%f_Xh%init(this%dm_Xh, fld_name = "adjoint_scalar_rhs")
370
371 ! Initialize the source term
372 call this%source_term%init(this%f_Xh, this%c_Xh, user, this%name)
373 ! We should ONLY read the adjoint
374 call this%source_term%add(params_primal, 'source_terms')
375
376 ! todo parameter file ksp tol should be added
377 params_selected => json_key_fallback(params_adjoint, params_primal, &
378 'solver.max_iterations')
379 call json_get_or_default(params_selected, &
380 'solver.max_iterations', &
381 integer_val, ksp_max_iter)
382 params_selected => json_key_fallback(params_adjoint, params_primal, &
383 'solver.monitor')
384 call json_get_or_default(params_selected, &
385 'solver.monitor', &
386 logical_val, .false.)
387 call adjoint_scalar_scheme_solver_factory(this%ksp, this%dm_Xh%size(), &
388 solver_type, integer_val, solver_abstol, logical_val)
389 call scalar_scheme_precon_factory(this%pc, this%ksp, &
390 this%c_Xh, this%dm_Xh, this%gs_Xh, this%bcs, &
391 solver_precon, precon_params)
392
393 call neko_log%end_section()
394
395 end subroutine adjoint_scalar_scheme_init
396
397
400 class(adjoint_scalar_scheme_t), intent(inout) :: this
401
402 nullify(this%Xh)
403 nullify(this%dm_Xh)
404 nullify(this%gs_Xh)
405 nullify(this%c_Xh)
406 nullify(this%params)
407
408 if (allocated(this%ksp)) then
409 call this%ksp%free()
410 deallocate(this%ksp)
411 end if
412
413 if (allocated(this%pc)) then
414 call precon_destroy(this%pc)
415 deallocate(this%pc)
416 end if
417
418 call this%source_term%free()
419
420 call this%bcs%free()
421
422 call this%cp%free()
423 call this%lambda%free()
424 call this%s_adj_lag%free()
425
426 end subroutine adjoint_scalar_scheme_free
427
431 class(adjoint_scalar_scheme_t), target, intent(inout) :: this
432
433 if ( (.not. allocated(this%u%x)) .or. &
434 (.not. allocated(this%v%x)) .or. &
435 (.not. allocated(this%w%x)) .or. &
436 (.not. allocated(this%s%x)) .or. &
437 (.not. allocated(this%s_adj%x))) then
438 call neko_error('Fields are not allocated')
439 end if
440
441 if (.not. allocated(this%ksp)) then
442 call neko_error('No Krylov solver for velocity defined')
443 end if
444
445 if (.not. associated(this%Xh)) then
446 call neko_error('No function space defined')
447 end if
448
449 if (.not. associated(this%dm_Xh)) then
450 call neko_error('No dofmap defined')
451 end if
452
453 if (.not. associated(this%c_Xh)) then
454 call neko_error('No coefficients defined')
455 end if
456
457 if (.not. associated(this%f_Xh)) then
458 call neko_error('No rhs allocated')
459 end if
460
461 if (.not. associated(this%params)) then
462 call neko_error('No parameters defined')
463 end if
464
465 if (.not. associated(this%rho)) then
466 call neko_error('No density field defined')
467 end if
468
469 end subroutine adjoint_scalar_scheme_validate
470
473 subroutine adjoint_scalar_scheme_solver_factory(ksp, n, solver, max_iter, &
474 abstol, monitor)
475 class(ksp_t), allocatable, target, intent(inout) :: ksp
476 integer, intent(in), value :: n
477 integer, intent(in) :: max_iter
478 character(len=*), intent(in) :: solver
479 real(kind=rp) :: abstol
480 logical, intent(in) :: monitor
481
482 call krylov_solver_factory(ksp, n, solver, max_iter, &
483 abstol, monitor = monitor)
484
486
488 subroutine adjoint_scalar_scheme_precon_factory(pc, ksp, coef, dof, gs, &
489 bclst, pctype, pcparams)
490 class(pc_t), allocatable, target, intent(inout) :: pc
491 class(ksp_t), target, intent(inout) :: ksp
492 type(coef_t), target, intent(in) :: coef
493 type(dofmap_t), target, intent(in) :: dof
494 type(gs_t), target, intent(inout) :: gs
495 type(bc_list_t), target, intent(inout) :: bclst
496 character(len=*) :: pctype
497 type(json_file), intent(inout) :: pcparams
498
499 call precon_factory(pc, pctype)
500
501 select type (pcp => pc)
502 type is (jacobi_t)
503 call pcp%init(coef, dof, gs)
504 type is (sx_jacobi_t)
505 call pcp%init(coef, dof, gs)
506 type is (device_jacobi_t)
507 call pcp%init(coef, dof, gs)
508 type is (hsmg_t)
509 call pcp%init(coef, bclst, pcparams)
510 end select
511
512 call ksp%set_pc(pc)
513
515
521 class(adjoint_scalar_scheme_t), intent(inout) :: this
522 type(time_state_t), intent(in) :: time
523 type(field_t), pointer :: nut
524 integer :: index
525 ! Factor to transform nu_t to lambda_t
526 type(field_t), pointer :: lambda_factor
527
528 call this%user_material_properties(this%name, this%material_properties, &
529 time)
530
531 ! factor = rho * cp / pr_turb
532 if (this%variable_material_properties .and. &
533 len(trim(this%nut_field_name)) > 0) then
534 nut => neko_registry%get_field(this%nut_field_name)
535
536 ! lambda = lambda + rho * cp * nut / pr_turb
537 call neko_scratch_registry%request_field(lambda_factor, index, .false.)
538
539 call field_col3(lambda_factor, this%cp, this%rho)
540 call field_col2(lambda_factor, nut)
541 call field_cmult(lambda_factor, 1.0_rp / this%pr_turb)
542 call field_add2(this%lambda, lambda_factor)
543 call neko_scratch_registry%relinquish_field(index)
544 end if
545
546 ! Since cp is a fields and we use the %x(1,1,1,1) of the
547 ! host array data to pass constant material properties
548 ! to some routines, we need to make sure that the host
549 ! values are also filled
550 if (neko_bcknd_device .eq. 1) then
551 call device_memcpy(this%cp%x, this%cp%x_d, this%cp%size(), &
552 device_to_host, sync=.false.)
553 end if
554
556
562 params_primal, user)
563 class(adjoint_scalar_scheme_t), intent(inout) :: this
564 type(json_file), intent(inout) :: params_primal
565 type(user_t), target, intent(in) :: user
566 character(len=LOG_SIZE) :: log_buf
567 ! A local pointer that is needed to make Intel happy
568 procedure(user_material_properties_intf), pointer :: dummy_mp_ptr
569 real(kind=rp) :: const_cp, const_lambda
570 ! Dummy time state set to 0
571 type(time_state_t) :: time
572
573 dummy_mp_ptr => dummy_user_material_properties
574
575 ! Fill lambda field with the physical value
576 call this%lambda%init(this%dm_Xh, "lambda")
577 call this%cp%init(this%dm_Xh, "cp")
578
579 call this%material_properties%init(2)
580 call this%material_properties%assign_to_field(1, this%cp)
581 call this%material_properties%assign_to_field(2, this%lambda)
582
583 if (.not. associated(user%material_properties, dummy_mp_ptr)) then
584
585 write(log_buf, '(A)') "Material properties must be set in the user " // &
586 "file!"
587 call neko_log%message(log_buf)
588 this%user_material_properties => user%material_properties
589 call user%material_properties(this%name, this%material_properties, time)
590 else
591 this%user_material_properties => dummy_user_material_properties
592 if (params_primal%valid_path('Pe') .and. &
593 (params_primal%valid_path('lambda') .or. &
594 params_primal%valid_path('cp'))) then
595 call neko_error("To set the material properties for the scalar, " // &
596 "either provide Pe OR lambda and cp in the case file.")
597 ! Non-dimensional case
598 else if (params_primal%valid_path('Pe')) then
599 write(log_buf, '(A)') 'Non-dimensional scalar material properties' //&
600 ' input.'
601 call neko_log%message(log_buf, lvl = neko_log_verbose)
602 write(log_buf, '(A)') 'Specific heat capacity will be set to 1,'
603 call neko_log%message(log_buf, lvl = neko_log_verbose)
604 write(log_buf, '(A)') 'conductivity to 1/Pe. Assumes density is 1.'
605 call neko_log%message(log_buf, lvl = neko_log_verbose)
606
607 ! Read Pe into lambda for further manipulation.
608 call json_get(params_primal, 'Pe', const_lambda)
609 write(log_buf, '(A,ES13.6)') 'Pe :', const_lambda
610 call neko_log%message(log_buf)
611
612 ! Set cp and rho to 1 since the setup is non-dimensional.
613 const_cp = 1.0_rp
614 ! Invert the Pe to get conductivity
615 const_lambda = 1.0_rp/const_lambda
616 ! Dimensional case
617 else
618 call json_get(params_primal, 'lambda', const_lambda)
619 call json_get(params_primal, 'cp', const_cp)
620 end if
621 end if
622 ! We need to fill the fields based on the parsed const values
623 ! if the user routine is not used.
624 if (associated(user%material_properties, dummy_mp_ptr)) then
625 ! Fill mu and rho field with the physical value
626 call field_cfill(this%lambda, const_lambda)
627 call field_cfill(this%cp, const_cp)
628
629 write(log_buf, '(A,ES13.6)') 'lambda :', const_lambda
630 call neko_log%message(log_buf)
631 write(log_buf, '(A,ES13.6)') 'cp :', const_cp
632 call neko_log%message(log_buf)
633 end if
634
635 ! Since cp is a field and we use the %x(1,1,1,1) of the
636 ! host array data to pass constant material properties
637 ! to some routines, we need to make sure that the host
638 ! values are also filled
639 if (neko_bcknd_device .eq. 1) then
640 call device_memcpy(this%cp%x, this%cp%x_d, this%cp%size(), &
641 device_to_host, sync=.false.)
642 end if
644
645end module adjoint_scalar_scheme
Abstract interface to dealocate a scalar formulation.
Abstract interface to initialize a scalar formulation.
Abstract interface to restart a scalar formulation.
Contains the adjoint_scalar_scheme_t type.
subroutine adjoint_scalar_scheme_set_material_properties(this, params_primal, user)
Set lamdba and cp.
subroutine adjoint_scalar_scheme_init(this, msh, c_xh, gs_xh, params_adjoint, params_primal, scheme, user, rho)
Initialize all related components of the current scheme.
subroutine adjoint_scalar_scheme_solver_factory(ksp, n, solver, max_iter, abstol, monitor)
Initialize a linear solver.
subroutine adjoint_scalar_scheme_precon_factory(pc, ksp, coef, dof, gs, bclst, pctype, pcparams)
Initialize a Krylov preconditioner.
subroutine adjoint_scalar_scheme_update_material_properties(this, time)
Call user material properties routine and update the values of lambda if necessary.
subroutine adjoint_scalar_scheme_free(this)
Deallocate a scalar formulation.
subroutine adjoint_scalar_scheme_validate(this)
Validate that all fields, solvers etc necessary for performing time-stepping are defined.
Base type for a scalar advection-diffusion solver.