37 use num_types,
only: rp
38 use json_module,
only: json_file
39 use registry,
only: neko_registry
40 use field,
only: field_t
41 use coefs,
only: coef_t
42 use ax_product,
only: ax_t, ax_helm_allocator
43 use krylov,
only: ksp_t, ksp_monitor_t, krylov_solver_factory
44 use precon,
only: pc_t, precon_allocator, precon_destroy
45 use scalar_bc_projector,
only: scalar_bc_projector_t
46 use neumann,
only: neumann_t
47 use profiler,
only: profiler_start_region, profiler_end_region
48 use gather_scatter,
only: gs_t, gs_op_add
49 use pnpn_residual,
only: pnpn_prs_res_t
50 use mesh,
only: mesh_t, neko_msh_max_zlbls, neko_msh_max_zlbl_len
51 use registry,
only: neko_registry
53 use scratch_registry,
only: neko_scratch_registry
54 use field_math,
only: field_copy, field_add3
55 use coefs,
only: coef_t
56 use logger,
only: neko_log, log_size
57 use neko_config,
only: neko_bcknd_device
58 use dofmap,
only: dofmap_t
59 use jacobi,
only: jacobi_t
60 use device_jacobi,
only: device_jacobi_t
61 use sx_jacobi,
only: sx_jacobi_t
62 use utils,
only: neko_error
63 use device_math,
only: device_cfill, device_subcol3, device_cmult
64 use json_utils,
only: json_get, json_get_or_default
75 class(ax_t),
allocatable :: ax
77 type(ksp_monitor_t) :: ksp_results(1)
79 class(ksp_t),
allocatable :: ksp_filt
81 class(pc_t),
allocatable :: pc_filt
83 type(scalar_bc_projector_t) :: bc_projector_filt
89 real(kind=rp) :: abstol_filt
91 integer :: ksp_max_iter
93 character(len=:),
allocatable :: ksp_solver
95 character(len=:),
allocatable :: precon_type_filt
96 integer :: ksp_n, n, i
104 procedure, pass(this) :: init_from_attributes => &
105 pde_filter_init_from_attributes
107 procedure, pass(this) :: free => pde_filter_free
109 procedure, pass(this) :: forward_mapping => pde_filter_forward_mapping
119 procedure, pass(this) :: backward_mapping => pde_filter_backward_mapping
127 type(json_file),
intent(inout) :: json
128 type(coef_t),
intent(inout) :: coef
129 real(kind=rp) :: r, tol
131 character(len=:),
allocatable :: ksp_solver, precon_type
133 call nekotop_continuation%json_get_or_register(json,
'r', this%r, r)
134 call json_get_or_default(json,
'tol', tol, 0.0000000001_rp)
135 call json_get_or_default(json,
'max_iter', max_iter, 200)
136 call json_get_or_default(json,
'solver', ksp_solver,
"cg")
137 call json_get_or_default(json,
'preconditioner', precon_type,
"jacobi")
139 call this%init_base(json, coef,
"PDE_filter_mapping")
140 call this%init_from_attributes(coef, r, tol, max_iter, &
141 ksp_solver, precon_type)
146 subroutine pde_filter_init_from_attributes(this, coef, r, tol, max_iter, &
147 ksp_solver, precon_type)
149 type(coef_t),
intent(inout) :: coef
150 real(kind=rp),
intent(in) :: r, tol
151 integer,
intent(in) :: max_iter
152 character(len=*),
intent(in) :: ksp_solver, precon_type
156 this%abstol_filt = tol
157 this%ksp_max_iter = max_iter
158 this%ksp_solver = ksp_solver
159 this%precon_type_filt = precon_type
162 n = this%coef%dof%size()
165 call ax_helm_allocator(this%Ax, type_name =
"standard")
168 call krylov_solver_factory(this%ksp_filt, n, this%ksp_solver, &
169 this%ksp_max_iter, this%abstol_filt)
172 call filter_precon_factory(this%pc_filt, this%ksp_filt, &
173 this%coef, this%coef%dof, this%coef%gs_h, &
174 this%bc_projector_filt, this%precon_type_filt)
176 end subroutine pde_filter_init_from_attributes
179 subroutine pde_filter_free(this)
182 if (
allocated(this%Ax))
then
186 if (
allocated(this%ksp_filt))
then
187 call this%ksp_filt%free()
188 deallocate(this%ksp_filt)
191 if (
allocated(this%pc_filt))
then
192 call precon_destroy(this%pc_filt)
193 deallocate(this%pc_filt)
196 call this%bc_projector_filt%free()
198 call this%free_base()
200 end subroutine pde_filter_free
206 subroutine pde_filter_forward_mapping(this, X_out, X_in)
208 type(field_t),
intent(in) :: X_in
209 type(field_t),
intent(inout) :: X_out
211 type(field_t),
pointer :: RHS, d_X_out
212 character(len=LOG_SIZE) :: log_buf
213 integer :: temp_indices(2)
215 n = this%coef%dof%size()
216 call neko_scratch_registry%request_field(rhs, temp_indices(1), .false.)
217 call neko_scratch_registry%request_field(d_x_out, temp_indices(2), .false.)
230 if (neko_bcknd_device .eq. 1)
then
231 call device_cfill(this%coef%h1_d, (this%r / (2.0_rp * sqrt(3.0_rp)))**2, n)
232 call device_cfill(this%coef%h2_d, 1.0_rp, n)
235 this%coef%h1 = (this%r / (2.0_rp * sqrt(3.0_rp)))**2
237 this%coef%h2 = 1.0_rp
239 this%coef%ifh2 = .true.
244 call this%pc_filt%update()
249 call field_copy(d_x_out, x_in)
250 call this%Ax%compute(rhs%x, d_x_out%x, this%coef, this%coef%msh, &
253 if (neko_bcknd_device .eq. 1)
then
254 call device_subcol3(rhs%x_d, x_in%x_d, this%coef%B_d, n)
255 call device_cmult(rhs%x_d, -1.0_rp, n)
259 rhs%x(i,1,1,1) = x_in%x(i,1,1,1) * this%coef%B(i,1,1,1) &
265 call this%coef%gs_h%op(rhs, gs_op_add)
268 call this%bc_projector_filt%apply(rhs%x, n)
271 call profiler_start_region(
'filter solve')
272 this%ksp_results(1) = &
273 this%ksp_filt%solve(this%Ax, d_x_out, rhs%x, n, this%coef, &
274 this%bc_projector_filt, this%coef%gs_h)
276 call profiler_end_region
279 call field_add3(x_out, x_in, d_x_out)
282 call neko_log%message(
'Filter')
284 write(log_buf,
'(A,A,A)')
'Iterations: ',&
285 'Start residual: ',
'Final residual:'
286 call neko_log%message(log_buf)
287 write(log_buf,
'(I11,3x, E15.7,5x, E15.7)') this%ksp_results%iter, &
288 this%ksp_results%res_start, this%ksp_results%res_final
289 call neko_log%message(log_buf)
291 call neko_scratch_registry%relinquish_field(temp_indices)
295 end subroutine pde_filter_forward_mapping
316 subroutine pde_filter_backward_mapping(this, sens_out, sens_in, X_in)
318 type(field_t),
intent(in) :: X_in
319 type(field_t),
intent(in) :: sens_in
320 type(field_t),
intent(inout) :: sens_out
322 type(field_t),
pointer :: RHS, delta
323 integer :: temp_indices(2)
324 character(len=LOG_SIZE) :: log_buf
326 n = this%coef%dof%size()
328 call neko_scratch_registry%request_field(rhs, temp_indices(1), .false.)
329 call neko_scratch_registry%request_field(delta, temp_indices(2), .false.)
332 if (neko_bcknd_device .eq. 1)
then
333 call device_cfill(this%coef%h1_d, (this%r / (2.0_rp * sqrt(3.0_rp)))**2, n)
334 call device_cfill(this%coef%h2_d, 1.0_rp, n)
337 this%coef%h1 = (this%r / (2.0_rp * sqrt(3.0_rp)))**2
339 this%coef%h2 = 1.0_rp
341 this%coef%ifh2 = .true.
345 call this%pc_filt%update()
350 call field_copy(delta, sens_in)
351 call this%Ax%compute(rhs%x, delta%x, this%coef, this%coef%msh, &
354 if (neko_bcknd_device .eq. 1)
then
355 call device_subcol3(rhs%x_d, sens_in%x_d, this%coef%B_d, n)
356 call device_cmult(rhs%x_d, -1.0_rp, n)
360 rhs%x(i,1,1,1) = sens_in%x(i,1,1,1) * this%coef%B(i,1,1,1) &
366 call this%coef%gs_h%op(rhs, gs_op_add)
369 call this%bc_projector_filt%apply(rhs%x, n)
372 call profiler_start_region(
'filter solve')
373 this%ksp_results(1) = &
374 this%ksp_filt%solve(this%Ax, delta, rhs%x, n, this%coef, &
375 this%bc_projector_filt, this%coef%gs_h)
378 call field_add3(sens_out, sens_in, delta)
380 call profiler_end_region
383 call neko_log%message(
'Filter')
385 write(log_buf,
'(A,A,A)')
'Iterations: ',&
386 'Start residual: ',
'Final residual:'
387 call neko_log%message(log_buf)
388 write(log_buf,
'(I11,3x, E15.7,5x, E15.7)') this%ksp_results%iter, &
389 this%ksp_results%res_start, this%ksp_results%res_final
390 call neko_log%message(log_buf)
392 call neko_scratch_registry%relinquish_field(temp_indices)
394 end subroutine pde_filter_backward_mapping
396 subroutine filter_precon_factory(pc, ksp, coef, dof, gs, bc_projector, &
400 class(pc_t),
allocatable,
target,
intent(inout) :: pc
401 class(ksp_t),
target,
intent(inout) :: ksp
402 type(coef_t),
target,
intent(in) :: coef
403 type(dofmap_t),
target,
intent(in) :: dof
404 type(gs_t),
target,
intent(inout) :: gs
405 type(scalar_bc_projector_t),
target,
intent(inout) :: bc_projector
406 character(len=*) :: pctype
408 call precon_allocator(pc, pctype)
410 select type (pcp => pc)
412 call pcp%init(coef, dof, gs)
413 type is (sx_jacobi_t)
414 call pcp%init(coef, dof, gs)
415 type is (device_jacobi_t)
416 call pcp%init(coef, dof, gs)
421 end subroutine filter_precon_factory
Continuation scheduler for the optimization loop.
Mappings to be applied to a scalar field.
subroutine pde_filter_init_from_json(this, json, coef)
Constructor from json.
Base abstract class for mapping.
A PDE based filter mapping , see Lazarov & O. Sigmund 2010, by solving an equation of the form .