283 subroutine adjoint_fluid_pnpn_init(this, msh, lx, params, user, time_scheme)
284 class(adjoint_fluid_pnpn_t),
target,
intent(inout) :: this
285 type(mesh_t),
target,
intent(inout) :: msh
286 integer,
intent(in) :: lx
287 type(json_file),
target,
intent(inout) :: params
288 type(user_t),
target,
intent(in) :: user
289 type(time_scheme_controller_t),
target,
intent(in) :: time_scheme
290 character(len=15),
parameter :: scheme =
'Adjoint (Pn/Pn)'
291 real(kind=rp) :: abs_tol
292 character(len=LOG_SIZE) :: log_buf
293 integer :: solver_maxiter
294 character(len=:),
allocatable :: solver_type, precon_type
295 logical :: monitor, found
299 character(len=:),
allocatable :: file_name
300 character(len=256) :: header_line
305 call this%init_base(msh, lx, params, scheme,
user, .true.)
309 call neko_field_registry%add_field(this%dm_Xh,
'p_adj')
310 this%p_adj => neko_field_registry%get_field(
'p_adj')
316 if (this%variable_material_properties .eqv. .true.)
then
318 call ax_helm_factory(this%Ax_vel, full_formulation = .true.)
321 call pnpn_prs_res_stress_factory(this%prs_res)
324 call pnpn_vel_res_stress_factory(this%vel_res)
327 call ax_helm_factory(this%Ax_vel, full_formulation = .false.)
330 call pnpn_prs_res_factory(this%prs_res)
333 call pnpn_vel_res_factory(this%vel_res)
337 call ax_helm_factory(this%Ax_prs, full_formulation = .false.)
341 call rhs_maker_sumab_fctry(this%sumab)
344 call rhs_maker_ext_fctry(this%makeabf)
347 call rhs_maker_bdf_fctry(this%makebdf)
350 call rhs_maker_oifs_fctry(this%makeoifs)
353 associate(xh_lx => this%Xh%lx, xh_ly => this%Xh%ly, xh_lz => this%Xh%lz, &
354 dm_xh => this%dm_Xh, nelv => this%msh%nelv)
356 call this%p_res%init(dm_xh,
"p_res")
357 call this%u_res%init(dm_xh,
"u_res")
358 call this%v_res%init(dm_xh,
"v_res")
359 call this%w_res%init(dm_xh,
"w_res")
360 call this%abx1%init(dm_xh,
"abx1")
361 call this%aby1%init(dm_xh,
"aby1")
362 call this%abz1%init(dm_xh,
"abz1")
363 call this%abx2%init(dm_xh,
"abx2")
364 call this%aby2%init(dm_xh,
"aby2")
365 call this%abz2%init(dm_xh,
"abz2")
366 call this%advx%init(dm_xh,
"advx")
367 call this%advy%init(dm_xh,
"advy")
368 call this%advz%init(dm_xh,
"advz")
371 call this%du%init(this%dm_Xh,
'du')
372 call this%dv%init(this%dm_Xh,
'dv')
373 call this%dw%init(this%dm_Xh,
'dw')
374 call this%dp%init(this%dm_Xh,
'dp')
377 call this%setup_bcs(
user, params)
380 call json_get_or_default(params,
'case.output_boundary', found, .false.)
381 if (found)
call this%write_boundary_conditions()
384 if (this%variable_material_properties .and. &
385 this%vel_projection_dim .gt. 0)
then
386 call neko_error(
"Velocity projection not available for full stress &
391 call this%proj_prs%init(this%dm_Xh%size(), this%pr_projection_dim, &
392 this%pr_projection_activ_step)
394 call this%proj_u%init(this%dm_Xh%size(), this%vel_projection_dim, &
395 this%vel_projection_activ_step)
396 call this%proj_v%init(this%dm_Xh%size(), this%vel_projection_dim, &
397 this%vel_projection_activ_step)
398 call this%proj_w%init(this%dm_Xh%size(), this%vel_projection_dim, &
399 this%vel_projection_activ_step)
403 call this%chkp%add_lag(this%ulag, this%vlag, this%wlag)
406 call json_get_or_default(params,
'case.numerics.oifs', this%oifs, .false.)
409 call json_get_or_default(params,
'case.fluid.advection', advection, .true.)
414 if (params%valid_path(
'case.fluid.flow_rate_force'))
then
415 call neko_error(
"Flow rate forcing not available for adjoint_fluid_pnpn")
420 call neko_log%section(
"Pressure solver")
422 call json_get_or_default(params, &
423 'case.fluid.pressure_solver.max_iterations', &
425 call json_get(params,
'case.fluid.pressure_solver.type', solver_type)
426 call json_get(params,
'case.fluid.pressure_solver.preconditioner', &
428 call json_get(params,
'case.fluid.pressure_solver.absolute_tolerance', &
430 call json_get_or_default(params,
'case.fluid.velocity_solver.monitor', &
432 call neko_log%message(
'Type : ('// trim(solver_type) // &
433 ', ' // trim(precon_type) //
')')
434 write(log_buf,
'(A,ES13.6)')
'Abs tol :', abs_tol
435 call neko_log%message(log_buf)
437 call this%solver_factory(this%ksp_prs, this%dm_Xh%size(), &
438 solver_type, solver_maxiter, abs_tol, monitor)
439 call this%precon_factory_(this%pc_prs, this%ksp_prs, &
440 this%c_Xh, this%dm_Xh, this%gs_Xh, this%bcs_prs, precon_type)
442 call neko_log%end_section()
445 if (neko_bcknd_device .eq. 1)
then
446 call device_event_create(this%event, 2)
453 call json_get_or_default(params,
'norm_scaling', &
454 this%norm_scaling, 0.5_rp)
462 this%u_b => neko_field_registry%get_field(
'u')
463 this%v_b => neko_field_registry%get_field(
'v')
464 this%w_b => neko_field_registry%get_field(
'w')
465 this%p_b => neko_field_registry%get_field(
'p')
468 call json_get_or_default(params,
'norm_target', &
469 this%norm_target, -1.0_rp)
470 call json_get_or_default(params,
'norm_tolerance', &
471 this%norm_tolerance, 10.0_rp)
474 call json_get_or_default(params,
'output_file', &
475 file_name,
'power_iterations.csv')
476 this%file_output = file_t(trim(file_name))
477 write(header_line,
'(A)')
'Time, Norm, Scaling'
478 call this%file_output%set_header(header_line)
482 subroutine adjoint_fluid_pnpn_restart(this, dtlag, tlag)
483 class(adjoint_fluid_pnpn_t),
target,
intent(inout) :: this
484 real(kind=rp) :: dtlag(10), tlag(10)
487 n = this%u_adj%dof%size()
488 if (
allocated(this%chkp%previous_mesh%elements) .or. &
489 this%chkp%previous_Xh%lx .ne. this%Xh%lx)
then
490 associate(u => this%u_adj, v => this%v_adj, w => this%w_adj, &
491 p => this%p_adj, c_xh => this%c_Xh, ulag => this%ulag, &
492 vlag => this%vlag, wlag => this%wlag)
493 do concurrent(j = 1:n)
494 u%x(j,1,1,1) = u%x(j,1,1,1) * c_xh%mult(j,1,1,1)
495 v%x(j,1,1,1) = v%x(j,1,1,1) * c_xh%mult(j,1,1,1)
496 w%x(j,1,1,1) = w%x(j,1,1,1) * c_xh%mult(j,1,1,1)
497 p%x(j,1,1,1) = p%x(j,1,1,1) * c_xh%mult(j,1,1,1)
499 do i = 1, this%ulag%size()
500 do concurrent(j = 1:n)
501 ulag%lf(i)%x(j,1,1,1) = ulag%lf(i)%x(j,1,1,1) &
503 vlag%lf(i)%x(j,1,1,1) = vlag%lf(i)%x(j,1,1,1) &
505 wlag%lf(i)%x(j,1,1,1) = wlag%lf(i)%x(j,1,1,1) &
512 if (neko_bcknd_device .eq. 1)
then
513 associate(u => this%u_adj, v => this%v_adj, w => this%w_adj, &
514 ulag => this%ulag, vlag => this%vlag, wlag => this%wlag,&
516 call device_memcpy(u%x, u%x_d, u%dof%size(), &
517 host_to_device, sync = .false.)
518 call device_memcpy(v%x, v%x_d, v%dof%size(), &
519 host_to_device, sync = .false.)
520 call device_memcpy(w%x, w%x_d, w%dof%size(), &
521 host_to_device, sync = .false.)
522 call device_memcpy(p%x, p%x_d, p%dof%size(), &
523 host_to_device, sync = .false.)
524 call device_memcpy(ulag%lf(1)%x, ulag%lf(1)%x_d, &
525 u%dof%size(), host_to_device, sync = .false.)
526 call device_memcpy(ulag%lf(2)%x, ulag%lf(2)%x_d, &
527 u%dof%size(), host_to_device, sync = .false.)
529 call device_memcpy(vlag%lf(1)%x, vlag%lf(1)%x_d, &
530 v%dof%size(), host_to_device, sync = .false.)
531 call device_memcpy(vlag%lf(2)%x, vlag%lf(2)%x_d, &
532 v%dof%size(), host_to_device, sync = .false.)
534 call device_memcpy(wlag%lf(1)%x, wlag%lf(1)%x_d, &
535 w%dof%size(), host_to_device, sync = .false.)
536 call device_memcpy(wlag%lf(2)%x, wlag%lf(2)%x_d, &
537 w%dof%size(), host_to_device, sync = .false.)
538 call device_memcpy(this%abx1%x, this%abx1%x_d, &
539 w%dof%size(), host_to_device, sync = .false.)
540 call device_memcpy(this%abx2%x, this%abx2%x_d, &
541 w%dof%size(), host_to_device, sync = .false.)
542 call device_memcpy(this%aby1%x, this%aby1%x_d, &
543 w%dof%size(), host_to_device, sync = .false.)
544 call device_memcpy(this%aby2%x, this%aby2%x_d, &
545 w%dof%size(), host_to_device, sync = .false.)
546 call device_memcpy(this%abz1%x, this%abz1%x_d, &
547 w%dof%size(), host_to_device, sync = .false.)
548 call device_memcpy(this%abz2%x, this%abz2%x_d, &
549 w%dof%size(), host_to_device, sync = .false.)
550 call device_memcpy(this%advx%x, this%advx%x_d, &
551 w%dof%size(), host_to_device, sync = .false.)
552 call device_memcpy(this%advy%x, this%advy%x_d, &
553 w%dof%size(), host_to_device, sync = .false.)
554 call device_memcpy(this%advz%x, this%advz%x_d, &
555 w%dof%size(), host_to_device, sync = .false.)
563 if (
allocated(this%chkp%previous_mesh%elements) &
564 .or. this%chkp%previous_Xh%lx .ne. this%Xh%lx)
then
565 call this%gs_Xh%op(this%u_adj, gs_op_add)
566 call this%gs_Xh%op(this%v_adj, gs_op_add)
567 call this%gs_Xh%op(this%w_adj, gs_op_add)
568 call this%gs_Xh%op(this%p_adj, gs_op_add)
570 do i = 1, this%ulag%size()
571 call this%gs_Xh%op(this%ulag%lf(i), gs_op_add)
572 call this%gs_Xh%op(this%vlag%lf(i), gs_op_add)
573 call this%gs_Xh%op(this%wlag%lf(i), gs_op_add)
719 subroutine adjoint_fluid_pnpn_step(this, t, tstep, dt, ext_bdf, dt_controller)
720 class(adjoint_fluid_pnpn_t),
target,
intent(inout) :: this
721 real(kind=rp),
intent(in) :: t
722 integer,
intent(in) :: tstep
723 real(kind=rp),
intent(in) :: dt
724 type(time_scheme_controller_t),
intent(in) :: ext_bdf
725 type(time_step_controller_t),
intent(in) :: dt_controller
729 type(ksp_monitor_t) :: ksp_results(4)
731 type(field_t),
pointer :: u_e, v_e, w_e
733 integer :: temp_indices(3)
735 if (this%freeze)
return
737 n = this%dm_Xh%size()
739 call profiler_start_region(
'Adjoint', 1)
740 associate(u => this%u_adj, v => this%v_adj, w => this%w_adj, &
742 du => this%du, dv => this%dv, dw => this%dw, dp => this%dp, &
743 u_b => this%u_b, v_b => this%v_b, w_b => this%w_b, &
744 u_res => this%u_res, v_res => this%v_res, w_res => this%w_res, &
745 p_res => this%p_res, ax_vel => this%Ax_vel, ax_prs => this%Ax_prs, &
747 c_xh => this%c_Xh, dm_xh => this%dm_Xh, gs_xh => this%gs_Xh, &
748 ulag => this%ulag, vlag => this%vlag, wlag => this%wlag, &
749 msh => this%msh, prs_res => this%prs_res, &
750 source_term => this%source_term, &
751 vel_res => this%vel_res, sumab => this%sumab, &
752 makeabf => this%makeabf, makebdf => this%makebdf, &
753 vel_projection_dim => this%vel_projection_dim, &
754 pr_projection_dim => this%pr_projection_dim, &
755 rho => this%rho, mu => this%mu, oifs => this%oifs, &
756 rho_field => this%rho_field, mu_field => this%mu_field, &
757 f_x => this%f_adj_x, f_y => this%f_adj_y, f_z => this%f_adj_z, &
758 if_variable_dt => dt_controller%if_variable_dt, &
759 dt_last_change => dt_controller%dt_last_change, &
763 call this%scratch%request_field(u_e, temp_indices(1))
764 call this%scratch%request_field(v_e, temp_indices(2))
765 call this%scratch%request_field(w_e, temp_indices(3))
766 call sumab%compute_fluid(u_e, v_e, w_e, u, v, w, &
767 ulag, vlag, wlag, ext_bdf%advection_coeffs, ext_bdf%nadv)
770 call this%source_term%compute(t, tstep)
773 call this%bcs_vel%apply_vector(f_x%x, f_y%x, f_z%x, &
774 this%dm_Xh%size(), t, tstep, strong = .false.)
777 if (this%if_gradient_jump_penalty .eqv. .true.)
then
778 call neko_error(
"Gradient jump penalty not implemented for adjoint")
788 call neko_error(
"OIFS not implemented for adjoint")
809 call this%adv%compute_adjoint(u, v, w, u_b, v_b, w_b, &
811 xh, this%c_Xh, dm_xh%size())
817 call makeabf%compute_fluid(this%abx1, this%aby1, this%abz1,&
818 this%abx2, this%aby2, this%abz2, &
819 f_x%x, f_y%x, f_z%x, &
820 rho, ext_bdf%advection_coeffs, n)
823 call makebdf%compute_fluid(ulag, vlag, wlag, f_x%x, f_y%x, f_z%x, &
824 u, v, w, c_xh%B, rho, dt, &
825 ext_bdf%diffusion_coeffs, ext_bdf%ndiff, n)
832 call this%bc_apply_vel(t, tstep, strong = .true.)
833 call this%bc_apply_prs(t, tstep)
836 call this%update_material_properties()
839 call profiler_start_region(
'Pressure_residual', 18)
841 call prs_res%compute(p, p_res,&
846 this%bc_prs_surface, this%bc_sym_surface,&
847 ax_prs, ext_bdf%diffusion_coeffs(1), dt, &
848 mu_field, rho_field, event)
851 if (.not. this%prs_dirichlet)
call ortho(p_res%x, this%glb_n_points, n)
853 call gs_xh%op(p_res, gs_op_add, event)
854 if (neko_bcknd_device .eq. 1)
then
855 call device_stream_wait_event(glb_cmd_queue, event, 0)
859 call this%bclst_dp%apply_scalar(p_res%x, p%dof%size(), t, tstep)
862 call profiler_end_region(
'Pressure_residual', 18)
865 call this%proj_prs%pre_solving(p_res%x, tstep, c_xh, n, dt_controller, &
868 call this%pc_prs%update()
870 call profiler_start_region(
'Pressure_solve', 3)
873 ksp_results(1) = this%ksp_prs%solve(ax_prs, dp, p_res%x, n, c_xh, &
874 this%bclst_dp, gs_xh)
877 call profiler_end_region(
'Pressure_solve', 3)
879 call this%proj_prs%post_solving(dp%x, ax_prs, c_xh, &
880 this%bclst_dp, gs_xh, n, tstep, dt_controller)
883 call field_add2(p, dp, n)
884 if (.not. this%prs_dirichlet)
call ortho(p%x, this%glb_n_points, n)
887 call profiler_start_region(
'Velocity_residual', 19)
888 call vel_res%compute(ax_vel, u, v, w, &
889 u_res, v_res, w_res, &
893 mu_field, rho_field, ext_bdf%diffusion_coeffs(1), &
896 call gs_xh%op(u_res, gs_op_add, event)
897 call gs_xh%op(v_res, gs_op_add, event)
898 call gs_xh%op(w_res, gs_op_add, event)
899 if (neko_bcknd_device .eq. 1)
then
900 call device_stream_wait_event(glb_cmd_queue, event, 0)
904 call this%bclst_vel_res%apply(u_res, v_res, w_res, t, tstep)
907 call profiler_end_region(
'Velocity_residual', 19)
909 call this%proj_u%pre_solving(u_res%x, tstep, c_xh, n, dt_controller)
910 call this%proj_v%pre_solving(v_res%x, tstep, c_xh, n, dt_controller)
911 call this%proj_w%pre_solving(w_res%x, tstep, c_xh, n, dt_controller)
913 call this%pc_vel%update()
915 call profiler_start_region(
"Velocity_solve", 4)
916 ksp_results(2:4) = this%ksp_vel%solve_coupled(ax_vel, du, dv, dw, &
917 u_res%x, v_res%x, w_res%x, n, c_xh, &
918 this%bclst_du, this%bclst_dv, this%bclst_dw, gs_xh, &
919 this%ksp_vel%max_iter)
920 call profiler_end_region(
"Velocity_solve", 4)
922 call this%proj_u%post_solving(du%x, ax_vel, c_xh, &
923 this%bclst_du, gs_xh, n, tstep, dt_controller)
924 call this%proj_v%post_solving(dv%x, ax_vel, c_xh, &
925 this%bclst_dv, gs_xh, n, tstep, dt_controller)
926 call this%proj_w%post_solving(dw%x, ax_vel, c_xh, &
927 this%bclst_dw, gs_xh, n, tstep, dt_controller)
929 if (neko_bcknd_device .eq. 1)
then
930 call device_opadd2cm(u%x_d, v%x_d, w%x_d, &
931 du%x_d, dv%x_d, dw%x_d, 1.0_rp, n, msh%gdim)
933 call opadd2cm(u%x, v%x, w%x, du%x, dv%x, dw%x, 1.0_rp, n, msh%gdim)
936 if (this%forced_flow_rate)
then
937 call neko_error(
'Forced flow rate is not implemented for the adjoint')
946 call fluid_step_info(tstep, t, dt, ksp_results, this%strict_convergence)
948 call this%scratch%relinquish_field(temp_indices)
951 call profiler_end_region(
'Adjoint', 1)
966 subroutine adjoint_fluid_pnpn_setup_bcs(this, user, params)
967 use mpi_f08,
only: mpi_in_place
969 class(adjoint_fluid_pnpn_t),
intent(inout) :: this
970 type(user_t),
target,
intent(in) :: user
971 type(json_file),
intent(inout) :: params
972 integer :: i, n_bcs, j, zone_size, global_zone_size, ierr
973 class(bc_t),
pointer :: bc_i
974 type(json_core) :: core
975 type(json_value),
pointer :: bc_object
976 type(json_file) :: bc_subdict
979 logical,
allocatable :: marked_zones(:)
980 integer,
allocatable :: zone_indices(:)
981 character(len=:),
allocatable :: json_key
984 call this%bclst_vel_res%init()
985 call this%bclst_du%init()
986 call this%bclst_dv%init()
987 call this%bclst_dw%init()
988 call this%bclst_dp%init()
990 call this%bc_vel_res%init_from_components(this%c_Xh)
991 call this%bc_du%init_from_components(this%c_Xh)
992 call this%bc_dv%init_from_components(this%c_Xh)
993 call this%bc_dw%init_from_components(this%c_Xh)
994 call this%bc_dp%init_from_components(this%c_Xh)
997 call this%bc_prs_surface%init_from_components(this%c_Xh)
998 call this%bc_sym_surface%init_from_components(this%c_Xh)
1000 json_key =
'case.adjoint_fluid.boundary_conditions'
1003 if (params%valid_path(json_key))
then
1004 call params%info(json_key, n_children = n_bcs)
1005 call params%get_core(core)
1006 call params%get(json_key, bc_object, found)
1011 call this%bcs_vel%init(n_bcs)
1013 allocate(marked_zones(
size(this%msh%labeled_zones)))
1014 marked_zones = .false.
1018 call json_extract_item(core, bc_object, i, bc_subdict)
1020 call json_get(bc_subdict,
"zone_indices", zone_indices)
1025 do j = 1,
size(zone_indices)
1026 zone_size = this%msh%labeled_zones(zone_indices(j))%size
1027 call mpi_allreduce(zone_size, global_zone_size, 1, &
1028 mpi_integer, mpi_max, neko_comm, ierr)
1030 if (global_zone_size .eq. 0)
then
1031 write(error_unit,
'(A, A, I0, A, A, I0, A)')
"*** ERROR ***: ",&
1032 "Zone index ", zone_indices(j), &
1033 " is invalid as this zone has 0 size, meaning it does ", &
1034 "not in the mesh. Check adjoint boundary condition ", &
1039 if (marked_zones(zone_indices(j)) .eqv. .true.)
then
1040 write(error_unit,
'(A, A, I0, A, A, A, A)')
"*** ERROR ***: ", &
1041 "Zone with index ", zone_indices(j), &
1042 " has already been assigned a boundary condition. ", &
1043 "Please check your boundary_conditions entry for the ", &
1044 "adjoint and make sure that each zone index appears ", &
1045 "only in a single boundary condition."
1048 marked_zones(zone_indices(j)) = .true.
1053 call velocity_bc_factory(bc_i, this, bc_subdict, this%c_Xh,
user)
1057 if (
associated(bc_i))
then
1063 type is (symmetry_t)
1071 call this%bclst_vel_res%append(bc_i)
1072 call this%bc_du%mark_facets(bc_i%bc_x%marked_facet)
1073 call this%bc_dv%mark_facets(bc_i%bc_y%marked_facet)
1074 call this%bc_dw%mark_facets(bc_i%bc_z%marked_facet)
1076 call this%bcs_vel%append(bc_i)
1078 call this%bc_sym_surface%mark_facets(bc_i%marked_facet)
1079 type is (non_normal_t)
1082 call this%bclst_vel_res%append(bc_i)
1083 call this%bc_du%mark_facets(bc_i%bc_x%marked_facet)
1084 call this%bc_dv%mark_facets(bc_i%bc_y%marked_facet)
1085 call this%bc_dw%mark_facets(bc_i%bc_z%marked_facet)
1086 type is (shear_stress_t)
1088 call this%bclst_vel_res%append(bc_i%symmetry)
1089 call this%bclst_du%append(bc_i%symmetry%bc_x)
1090 call this%bclst_dv%append(bc_i%symmetry%bc_y)
1091 call this%bclst_dw%append(bc_i%symmetry%bc_z)
1093 call this%bcs_vel%append(bc_i)
1094 type is (wall_model_bc_t)
1096 call this%bclst_vel_res%append(bc_i%symmetry)
1097 call this%bclst_du%append(bc_i%symmetry%bc_x)
1098 call this%bclst_dv%append(bc_i%symmetry%bc_y)
1099 call this%bclst_dw%append(bc_i%symmetry%bc_z)
1101 call this%bcs_vel%append(bc_i)
1108 if (bc_i%strong .eqv. .true.)
then
1109 call this%bc_vel_res%mark_facets(bc_i%marked_facet)
1110 call this%bc_du%mark_facets(bc_i%marked_facet)
1111 call this%bc_dv%mark_facets(bc_i%marked_facet)
1112 call this%bc_dw%mark_facets(bc_i%marked_facet)
1114 call this%bc_prs_surface%mark_facets(bc_i%marked_facet)
1117 call this%bcs_vel%append(bc_i)
1123 do i = 1,
size(this%msh%labeled_zones)
1124 if ((this%msh%labeled_zones(i)%size .gt. 0) .and. &
1125 (marked_zones(i) .eqv. .false.))
then
1126 write(error_unit,
'(A, A, I0)')
"*** ERROR ***: ", &
1127 "No adjoint boundary condition assigned to zone ", i
1135 call this%bcs_prs%init(n_bcs)
1139 call json_extract_item(core, bc_object, i, bc_subdict)
1141 call pressure_bc_factory(bc_i, this, bc_subdict, this%c_Xh,
user)
1145 if (
associated(bc_i))
then
1146 call this%bcs_prs%append(bc_i)
1149 if (bc_i%strong .eqv. .true.)
then
1150 call this%bc_dp%mark_facets(bc_i%marked_facet)
1158 call this%bc_prs_surface%finalize()
1159 call this%bc_sym_surface%finalize()
1161 call this%bc_vel_res%finalize()
1162 call this%bc_du%finalize()
1163 call this%bc_dv%finalize()
1164 call this%bc_dw%finalize()
1165 call this%bc_dp%finalize()
1167 call this%bclst_vel_res%append(this%bc_vel_res)
1168 call this%bclst_du%append(this%bc_du)
1169 call this%bclst_dv%append(this%bc_dv)
1170 call this%bclst_dw%append(this%bc_dw)
1171 call this%bclst_dp%append(this%bc_dp)
1174 this%prs_dirichlet = .not. this%bclst_dp%is_empty()
1175 call mpi_allreduce(mpi_in_place, this%prs_dirichlet, 1, &
1176 mpi_logical, mpi_lor, neko_comm)
1181 subroutine adjoint_fluid_pnpn_write_boundary_conditions(this)
1182 use inflow,
only: inflow_t
1183 use field_dirichlet,
only: field_dirichlet_t
1184 use blasius,
only: blasius_t
1185 use field_dirichlet_vector,
only: field_dirichlet_vector_t
1186 use usr_inflow,
only: usr_inflow_t
1187 use dong_outflow,
only: dong_outflow_t
1188 class(adjoint_fluid_pnpn_t),
target,
intent(inout) :: this
1189 type(dirichlet_t) :: bdry_mask
1190 type(field_t),
pointer :: bdry_field
1191 type(file_t) :: bdry_file
1192 integer :: temp_index, i
1193 class(bc_t),
pointer :: bci
1194 character(len=LOG_SIZE) :: log_buf
1196 call neko_log%section(
"Adjoint boundary conditions")
1197 write(log_buf,
'(A)') &
1198 'Marking using integer keys in boundary_adjoint0.f00000'
1199 call neko_log%message(log_buf)
1200 write(log_buf,
'(A)')
'Condition-value pairs: '
1201 call neko_log%message(log_buf)
1202 write(log_buf,
'(A)')
' no_slip = 1'
1203 call neko_log%message(log_buf)
1204 write(log_buf,
'(A)')
' velocity_value = 2'
1205 call neko_log%message(log_buf)
1206 write(log_buf,
'(A)')
' outflow, normal_outflow (+dong) = 3'
1207 call neko_log%message(log_buf)
1208 write(log_buf,
'(A)')
' symmetry = 4'
1209 call neko_log%message(log_buf)
1210 write(log_buf,
'(A)')
' user_velocity_pointwise = 5'
1211 call neko_log%message(log_buf)
1212 write(log_buf,
'(A)')
' periodic = 6'
1213 call neko_log%message(log_buf)
1214 write(log_buf,
'(A)')
' user_velocity = 7'
1215 call neko_log%message(log_buf)
1216 write(log_buf,
'(A)')
' user_pressure = 8'
1217 call neko_log%message(log_buf)
1218 write(log_buf,
'(A)')
' shear_stress = 9'
1219 call neko_log%message(log_buf)
1220 write(log_buf,
'(A)')
' wall_modelling = 10'
1221 call neko_log%message(log_buf)
1222 write(log_buf,
'(A)')
' blasius_profile = 11'
1223 call neko_log%message(log_buf)
1224 call neko_log%end_section()
1226 call this%scratch%request_field(bdry_field, temp_index)
1231 call bdry_mask%init_from_components(this%c_Xh, 6.0_rp)
1232 call bdry_mask%mark_zone(this%msh%periodic)
1233 call bdry_mask%finalize()
1234 call bdry_mask%apply_scalar(bdry_field%x, this%dm_Xh%size())
1235 call bdry_mask%free()
1237 do i = 1, this%bcs_prs%size()
1238 bci => this%bcs_prs%get(i)
1239 select type (bc => bci)
1240 type is (zero_dirichlet_t)
1241 call bdry_mask%init_from_components(this%c_Xh, 3.0_rp)
1242 call bdry_mask%mark_facets(bci%marked_facet)
1243 call bdry_mask%finalize()
1244 call bdry_mask%apply_scalar(bdry_field%x, this%dm_Xh%size())
1245 call bdry_mask%free()
1246 type is (dong_outflow_t)
1247 call bdry_mask%init_from_components(this%c_Xh, 3.0_rp)
1248 call bdry_mask%mark_facets(bci%marked_facet)
1249 call bdry_mask%finalize()
1250 call bdry_mask%apply_scalar(bdry_field%x, this%dm_Xh%size())
1251 call bdry_mask%free()
1252 type is (field_dirichlet_t)
1253 call bdry_mask%init_from_components(this%c_Xh, 8.0_rp)
1254 call bdry_mask%mark_facets(bci%marked_facet)
1255 call bdry_mask%finalize()
1256 call bdry_mask%apply_scalar(bdry_field%x, this%dm_Xh%size())
1257 call bdry_mask%free()
1261 do i = 1, this%bcs_vel%size()
1262 bci => this%bcs_vel%get(i)
1263 select type (bc => bci)
1264 type is (zero_dirichlet_t)
1265 call bdry_mask%init_from_components(this%c_Xh, 1.0_rp)
1266 call bdry_mask%mark_facets(bci%marked_facet)
1267 call bdry_mask%finalize()
1268 call bdry_mask%apply_scalar(bdry_field%x, this%dm_Xh%size())
1269 call bdry_mask%free()
1271 call bdry_mask%init_from_components(this%c_Xh, 2.0_rp)
1272 call bdry_mask%mark_facets(bci%marked_facet)
1273 call bdry_mask%finalize()
1274 call bdry_mask%apply_scalar(bdry_field%x, this%dm_Xh%size())
1275 call bdry_mask%free()
1276 type is (symmetry_t)
1277 call bdry_mask%init_from_components(this%c_Xh, 4.0_rp)
1278 call bdry_mask%mark_facets(bci%marked_facet)
1279 call bdry_mask%finalize()
1280 call bdry_mask%apply_scalar(bdry_field%x, this%dm_Xh%size())
1281 call bdry_mask%free()
1282 type is (usr_inflow_t)
1283 call bdry_mask%init_from_components(this%c_Xh, 5.0_rp)
1284 call bdry_mask%mark_facets(bci%marked_facet)
1285 call bdry_mask%finalize()
1286 call bdry_mask%apply_scalar(bdry_field%x, this%dm_Xh%size())
1287 call bdry_mask%free()
1288 type is (field_dirichlet_vector_t)
1289 call bdry_mask%init_from_components(this%c_Xh, 7.0_rp)
1290 call bdry_mask%mark_facets(bci%marked_facet)
1291 call bdry_mask%finalize()
1292 call bdry_mask%apply_scalar(bdry_field%x, this%dm_Xh%size())
1293 call bdry_mask%free()
1294 type is (shear_stress_t)
1295 call bdry_mask%init_from_components(this%c_Xh, 9.0_rp)
1296 call bdry_mask%mark_facets(bci%marked_facet)
1297 call bdry_mask%finalize()
1298 call bdry_mask%apply_scalar(bdry_field%x, this%dm_Xh%size())
1299 call bdry_mask%free()
1300 type is (wall_model_bc_t)
1301 call bdry_mask%init_from_components(this%c_Xh, 10.0_rp)
1302 call bdry_mask%mark_facets(bci%marked_facet)
1303 call bdry_mask%finalize()
1304 call bdry_mask%apply_scalar(bdry_field%x, this%dm_Xh%size())
1305 call bdry_mask%free()
1307 call bdry_mask%init_from_components(this%c_Xh, 11.0_rp)
1308 call bdry_mask%mark_facets(bci%marked_facet)
1309 call bdry_mask%finalize()
1310 call bdry_mask%apply_scalar(bdry_field%x, this%dm_Xh%size())
1311 call bdry_mask%free()
1316 bdry_file = file_t(
'boundary_adjoint.fld')
1317 call bdry_file%write(bdry_field)
1319 call this%scratch%relinquish_field(temp_index)
1325 subroutine rescale_fluid(fluid_data, scale)
1327 class(adjoint_fluid_pnpn_t),
intent(inout) :: fluid_data
1329 real(kind=rp),
intent(in) :: scale
1335 if (neko_bcknd_device .eq. 1)
then
1336 call device_cmult(fluid_data%u_adj%x_d, scale, fluid_data%u_adj%size())
1337 call device_cmult(fluid_data%v_adj%x_d, scale, fluid_data%v_adj%size())
1338 call device_cmult(fluid_data%w_adj%x_d, scale, fluid_data%w_adj%size())
1340 call cmult(fluid_data%u_adj%x, scale, fluid_data%u_adj%size())
1341 call cmult(fluid_data%v_adj%x, scale, fluid_data%v_adj%size())
1342 call cmult(fluid_data%w_adj%x, scale, fluid_data%w_adj%size())
1346 if (neko_bcknd_device .eq. 1)
then
1347 call device_cmult(fluid_data%f_adj_x%x_d, scale, &
1348 fluid_data%f_adj_x%size())
1349 call device_cmult(fluid_data%f_adj_y%x_d, scale, &
1350 fluid_data%f_adj_y%size())
1351 call device_cmult(fluid_data%f_adj_z%x_d, scale, &
1352 fluid_data%f_adj_z%size())
1355 call device_cmult(fluid_data%abx1%x_d, scale, fluid_data%abx1%size())
1356 call device_cmult(fluid_data%aby1%x_d, scale, fluid_data%aby1%size())
1357 call device_cmult(fluid_data%abz1%x_d, scale, fluid_data%abz1%size())
1358 call device_cmult(fluid_data%abx2%x_d, scale, fluid_data%abx2%size())
1359 call device_cmult(fluid_data%aby2%x_d, scale, fluid_data%aby2%size())
1360 call device_cmult(fluid_data%abz2%x_d, scale, fluid_data%abz2%size())
1363 call cmult(fluid_data%f_adj_x%x, scale, fluid_data%f_adj_x%size())
1364 call cmult(fluid_data%f_adj_y%x, scale, fluid_data%f_adj_y%size())
1365 call cmult(fluid_data%f_adj_z%x, scale, fluid_data%f_adj_z%size())
1367 call cmult(fluid_data%abx1%x, scale, fluid_data%abx1%size())
1368 call cmult(fluid_data%aby1%x, scale, fluid_data%aby1%size())
1369 call cmult(fluid_data%abz1%x, scale, fluid_data%abz1%size())
1371 call cmult(fluid_data%abx2%x, scale, fluid_data%abx2%size())
1372 call cmult(fluid_data%aby2%x, scale, fluid_data%aby2%size())
1373 call cmult(fluid_data%abz2%x, scale, fluid_data%abz2%size())
1377 if (neko_bcknd_device .eq. 1)
then
1378 do i = 1, fluid_data%ulag%size()
1379 call device_cmult(fluid_data%ulag%lf(i)%x_d, &
1380 scale, fluid_data%ulag%lf(i)%size())
1383 do i = 1, fluid_data%vlag%size()
1384 call device_cmult(fluid_data%vlag%lf(i)%x_d, &
1385 scale, fluid_data%vlag%lf(i)%size())
1388 do i = 1, fluid_data%wlag%size()
1389 call device_cmult(fluid_data%wlag%lf(i)%x_d, &
1390 scale, fluid_data%wlag%lf(i)%size())
1393 do i = 1, fluid_data%ulag%size()
1394 call cmult(fluid_data%ulag%lf(i)%x, &
1395 scale, fluid_data%ulag%lf(i)%size())
1398 do i = 1, fluid_data%vlag%size()
1399 call cmult(fluid_data%vlag%lf(i)%x, &
1400 scale, fluid_data%vlag%lf(i)%size())
1403 do i = 1, fluid_data%wlag%size()
1404 call cmult(fluid_data%wlag%lf(i)%x, &
1405 scale, fluid_data%wlag%lf(i)%size())
1453 subroutine power_iterations_compute(this, t, tstep)
1454 class(adjoint_fluid_pnpn_t),
target,
intent(inout) :: this
1455 real(kind=rp),
intent(in) :: t
1456 integer,
intent(in) :: tstep
1459 real(kind=rp) :: scaling_factor
1460 real(kind=rp) :: norm_l2, norm_l2_base
1461 character(len=256) :: log_message
1462 type(vector_t) :: data_line
1465 n = this%c_Xh%dof%size()
1466 if (tstep .eq. 1)
then
1467 if (neko_bcknd_device .eq. 1)
then
1468 norm_l2_base = device_norm(this%u_adj%x_d, this%v_adj%x_d, &
1470 this%c_Xh%B_d, this%c_Xh%volume, n)
1472 norm_l2_base = this%norm_scaling * norm(this%u_adj%x, this%v_adj%x, &
1474 this%c_Xh%B, this%c_Xh%volume, n)
1476 if (this%norm_target .lt. 0.0_rp)
then
1477 this%norm_target = norm_l2_base
1480 this%norm_l2_upper = this%norm_tolerance * this%norm_target
1481 this%norm_l2_lower = this%norm_target / this%norm_tolerance
1486 if (neko_bcknd_device .eq. 1)
then
1487 norm_l2 = device_norm(this%u_adj%x_d, this%v_adj%x_d, this%w_adj%x_d, &
1488 this%c_Xh%B_d, this%c_Xh%volume, n)
1490 norm_l2 = norm(this%u_adj%x, this%v_adj%x, this%w_adj%x, &
1491 this%c_Xh%B, this%c_Xh%volume, n)
1493 norm_l2 = sqrt(this%norm_scaling) * norm_l2
1494 scaling_factor = 1.0_rp
1497 if (norm_l2 .gt. this%norm_l2_upper &
1498 .or. norm_l2 .lt. this%norm_l2_lower)
then
1499 scaling_factor = this%norm_target / norm_l2
1500 call rescale_fluid(this, scaling_factor)
1501 norm_l2 = this%norm_target
1503 if (tstep .eq. 1)
then
1504 scaling_factor = 1.0_rp
1510 call neko_log%section(
'Power Iterations')
1512 write (log_message,
'(A7,E20.14)')
'Norm: ', norm_l2
1513 call neko_log%message(log_message, lvl = neko_log_debug)
1514 write (log_message,
'(A7,E20.14)')
'Scaling: ', scaling_factor
1515 call neko_log%message(log_message, lvl = neko_log_debug)
1518 call data_line%init(2)
1519 data_line%x = [norm_l2, scaling_factor]
1520 call this%file_output%write(data_line, t)
1523 call neko_log%end_section(
'Power Iterations')