Octopus
multigrid_solver.F90
Go to the documentation of this file.
1!! Copyright (C) 2005-2006 M. Marques, A. Castro, A. Rubio, G. Bertsch, X. Andrade
2!! Copyright (C) 2024 N. Tancogne-Dejean
3!!
4!! This program is free software; you can redistribute it and/or modify
5!! it under the terms of the GNU General Public License as published by
6!! the Free Software Foundation; either version 2, or (at your option)
7!! any later version.
8!!
9!! This program is distributed in the hope that it will be useful,
10!! but WITHOUT ANY WARRANTY; without even the implied warranty of
11!! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12!! GNU General Public License for more details.
13!!
14!! You should have received a copy of the GNU General Public License
15!! along with this program; if not, write to the Free Software
16!! Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17!! 02110-1301, USA.
18!!
19
20#include "global.h"
21
25 use debug_oct_m
27 use global_oct_m
28 use, intrinsic :: iso_fortran_env
30 use mesh_oct_m
37 use parser_oct_m
40 use space_oct_m
42
43 implicit none
44
45 integer, parameter :: &
46 GAUSS_SEIDEL = 1, &
48
49 private
50 public :: &
57
58 type mg_solver_t
59 private
60
61 real(real64), public :: threshold
62 real(real64) :: relax_factor
63
64 integer, public :: maxcycles = 0
65 integer :: presteps
66 integer :: poststeps
67 integer :: restriction_method
68 integer :: relaxation_method
69 end type mg_solver_t
70
71 integer, parameter, public :: &
72 MG_V_SHAPE = 1, &
73 mg_w_shape = 2, &
74 mg_fmg = 3
75
76contains
77
78 ! ---------------------------------------------------------
79 subroutine multigrid_solver_init(this, namespace, space, mesh, thr)
80 type(mg_solver_t), intent(out) :: this
81 type(namespace_t), intent(in) :: namespace
82 class(space_t), intent(in) :: space
83 type(mesh_t), intent(inout) :: mesh
84 real(real64), intent(in) :: thr
85
86 push_sub(multigrid_solver_init)
87
88 this%threshold = thr
89
90 !%Variable MultigridPresmoothingSteps
91 !%Type integer
92 !%Default 1
93 !%Section Hamiltonian::Poisson::Multigrid
94 !%Description
95 !% Number of Gauss-Seidel smoothing steps before coarse-level
96 !% correction in the multigrid solver.
97 !%End
98 call parse_variable(namespace, 'MultigridPresmoothingSteps', 1, this%presteps)
99
100 !%Variable MultigridPostsmoothingSteps
101 !%Type integer
102 !%Default 4
103 !%Section Hamiltonian::Poisson::Multigrid
104 !%Description
105 !% Number of Gauss-Seidel smoothing steps after coarse-level
106 !% correction in the multigrid solver.
107 !%End
108 call parse_variable(namespace, 'MultigridPostsmoothingSteps', 4, this%poststeps)
109
110 !%Variable MultigridMaxCycles
111 !%Type integer
112 !%Default 100
113 !%Section Hamiltonian::Poisson::Multigrid
114 !%Description
115 !% Maximum number of multigrid cycles that are performed if
116 !% convergence is not achieved.
117 !%End
118 call parse_variable(namespace, 'MultigridMaxCycles', 100, this%maxcycles)
119
120 !%Variable MultigridRestrictionMethod
121 !%Type integer
122 !%Default fullweight
123 !%Section Hamiltonian::Poisson::Multigrid
124 !%Description
125 !% Method used from fine-to-coarse grid transfer.
126 !%Option injection 1
127 !% Injection
128 !%Option fullweight 2
129 !% Fullweight restriction
130 !%End
131 call parse_variable(namespace, 'MultigridRestrictionMethod', 2, this%restriction_method)
132 if (.not. varinfo_valid_option('MultigridRestrictionMethod', this%restriction_method)) then
133 call messages_input_error(namespace, 'MultigridRestrictionMethod')
134 end if
135 call messages_print_var_option("MultigridRestrictionMethod", this%restriction_method, namespace=namespace)
136
137 !%Variable MultigridRelaxationMethod
138 !%Type integer
139 !%Section Hamiltonian::Poisson::Multigrid
140 !%Description
141 !% Method used to solve the linear system approximately in each grid for the
142 !% multigrid procedure that solves a linear equation like the Poisson equation. Default is <tt>gauss_seidel</tt>.
143 !%Option gauss_seidel 1
144 !% Gauss-Seidel.
145 !%Option weighted_jacobi 2
146 !% Jacobi relaxation with a weight. The weight is determined by by MultigridRelaxationFactor.
147 !%End
148 call parse_variable(namespace, 'MultigridRelaxationMethod', gauss_seidel, this%relaxation_method)
149
150 if (.not. varinfo_valid_option('MultigridRelaxationMethod', this%relaxation_method)) then
151 call messages_input_error(namespace, 'MultigridRelaxationMethod')
152 end if
153 call messages_print_var_option("MultigridRelaxationMethod", this%relaxation_method, namespace=namespace)
155 if (this%relaxation_method == weighted_jacobi) then
156 !%Variable MultigridRelaxationFactor
157 !%Type float
158 !%Section Hamiltonian::Poisson::Multigrid
159 !%Description
160 !% Relaxation factor of the relaxation operator used for the
161 !% multigrid method. Only used for the <tt>gauss_jacobi</tt> method.
162 !% The default is 0.6666 for the <tt>gauss_jacobi</tt> method.
163 !%End
164 call parse_variable(namespace, 'MultigridRelaxationFactor', 0.6666_real64, this%relax_factor)
165 end if
166
167 pop_sub(multigrid_solver_init)
168 end subroutine multigrid_solver_init
169
170
171 ! ---------------------------------------------------------
175 recursive subroutine multigrid_solver_v_cycle(this, der, op, sol, rhs)
176 type(mg_solver_t), intent(in) :: this
177 type(derivatives_t), intent(in) :: der
178 type(nl_operator_t), intent(in) :: op
179 real(real64), contiguous, intent(inout) :: sol(:)
180 real(real64), contiguous, intent(in) :: rhs(:)
181
182 real(real64), allocatable :: residue(:), coarse_residue(:), correction(:), coarse_correction(:)
183
185
186 safe_allocate(residue(1:der%mesh%np_part))
187
188 if (associated(der%coarser)) then
189 assert(associated(op%coarser))
190
191 safe_allocate(correction(1:der%mesh%np_part))
192 safe_allocate(coarse_residue(1:der%coarser%mesh%np_part))
193 safe_allocate(coarse_correction(1:der%coarser%mesh%np_part))
194
195 ! Pre-Smoothing
196 call multigrid_relax(this, der%mesh, der, op, sol, rhs, this%presteps)
197
198 ! Compute the residual error
199 call get_residual(op, der, sol, rhs, residue)
200
201 ! Restriction of the residual is the next r.h.s
202 message(1) = "Debug: Multigrid restriction"
203 call messages_info(1, debug_only=.true.)
204
205 call dmultigrid_fine2coarse(der%to_coarser, der, der%coarser%mesh, residue, coarse_residue, this%restriction_method)
206
207 ! Recursive call for the coarse-grid correction
208 coarse_correction = m_zero
209 call multigrid_solver_v_cycle(this, der%coarser, op%coarser, coarse_correction, coarse_residue)
210
211 !Prolongation
212 message(1) = "Debug: Multigrid prolongation"
213 call messages_info(1, debug_only=.true.)
214
215 correction = m_zero
216 call dmultigrid_coarse2fine(der%to_coarser, der%coarser, der%mesh, coarse_correction, correction)
217
218 ! Correction
219 call lalg_axpy(der%mesh%np, m_one, correction, sol)
220
221 safe_deallocate_a(correction)
222 safe_deallocate_a(coarse_residue)
223 safe_deallocate_a(coarse_correction)
224
225 ! Post-Smoothing
226 call multigrid_relax(this, der%mesh, der, op, sol, rhs, this%poststeps)
227
228 else ! Coarsest grid
229
230 call multigrid_solver_solve_coarsest(this, der, op, sol, rhs, residue)
231
232 end if
233
234 safe_deallocate_a(residue)
235
237 end subroutine multigrid_solver_v_cycle
238
239 ! ---------------------------------------------------------
243 recursive subroutine multigrid_solver_w_cycle(this, der, op, sol, rhs)
244 type(mg_solver_t), intent(in) :: this
245 type(derivatives_t), intent(in) :: der
246 type(nl_operator_t), intent(in) :: op
247 real(real64), contiguous, intent(inout) :: sol(:)
248 real(real64), contiguous, intent(in) :: rhs(:)
249
250 real(real64), allocatable :: residue(:), coarse_residue(:), correction(:), coarse_correction(:)
251
253
254 safe_allocate(residue(1:der%mesh%np_part))
255
256 if (associated(der%coarser)) then
257 assert(associated(op%coarser))
258
259 safe_allocate(correction(1:der%mesh%np_part))
260 safe_allocate(coarse_residue(1:der%coarser%mesh%np_part))
261 safe_allocate(coarse_correction(1:der%coarser%mesh%np_part))
262
263 ! Pre-Smoothing
264 call multigrid_relax(this, der%mesh, der, op, sol, rhs, this%presteps)
265
266 ! Compute the residual error
267 call get_residual(op, der, sol, rhs, residue)
269 ! Restriction of the residual is the next r.h.s
270 message(1) = "Debug: Multigrid restriction"
271 call messages_info(1, debug_only=.true.)
272
273 call dmultigrid_fine2coarse(der%to_coarser, der, der%coarser%mesh, residue, coarse_residue, this%restriction_method)
274
275 ! Recursive call for the coarse-grid correction
276 coarse_correction = m_zero
277 call multigrid_solver_w_cycle(this, der%coarser, op%coarser, coarse_correction, coarse_residue)
278
279 !Prolongation
280 message(1) = "Debug: Multigrid prolongation"
281 call messages_info(1, debug_only=.true.)
282
283 correction = m_zero
284 call dmultigrid_coarse2fine(der%to_coarser, der%coarser, der%mesh, coarse_correction, correction)
285
286 ! Correction
287 call lalg_axpy(der%mesh%np, m_one, correction, sol)
288
289 ! Re-Smoothing
290 call multigrid_relax(this, der%mesh, der, op, sol, rhs, this%presteps)
291
292 ! Compute the residual error
293 call get_residual(op, der, sol, rhs, residue)
294
295 ! Restriction of the residual is the next r.h.s
296 message(1) = "Debug: Multigrid restriction"
297 call messages_info(1, debug_only=.true.)
298
299 call dmultigrid_fine2coarse(der%to_coarser, der, der%coarser%mesh, residue, coarse_residue, this%restriction_method)
300
301 ! Recursive call for the coarse-grid correction
302 coarse_correction = m_zero
303 call multigrid_solver_w_cycle(this, der%coarser, op%coarser, coarse_correction, coarse_residue)
304
305 !Prolongation
306 message(1) = "Debug: Multigrid prolongation"
307 call messages_info(1, debug_only=.true.)
308
309 correction = m_zero
310 call dmultigrid_coarse2fine(der%to_coarser, der%coarser, der%mesh, coarse_correction, correction)
311
312 ! Correction
313 call lalg_axpy(der%mesh%np, m_one, correction, sol)
314
315 safe_deallocate_a(correction)
316 safe_deallocate_a(coarse_residue)
317 safe_deallocate_a(coarse_correction)
318
319 ! Post-Smoothing
320 call multigrid_relax(this, der%mesh, der, op, sol, rhs, this%poststeps)
321
322 else ! Coarsest grid
323
324 call multigrid_solver_solve_coarsest(this, der, op, sol, rhs, residue)
325
326 end if
327
328 safe_deallocate_a(residue)
329
331
332 end subroutine multigrid_solver_w_cycle
333
334 ! ---------------------------------------------------------
338 subroutine multigrid_iterative_solver(this, namespace, der, op, sol, rhs, multigrid_shape)
339 type(mg_solver_t), intent(in) :: this
340 type(namespace_t), intent(in) :: namespace
341 type(derivatives_t), intent(in) :: der
342 type(nl_operator_t), intent(in) :: op
343 real(real64), contiguous, intent(inout) :: sol(:)
344 real(real64), contiguous, intent(inout) :: rhs(:)
345 integer, intent(in) :: multigrid_shape
346
347 integer :: iter
348 real(real64) :: resnorm
349 real(real64), allocatable :: err(:)
350
352
353 safe_allocate(err(1:der%mesh%np))
354
355 do iter = 1, this%maxcycles
356
357 select case (multigrid_shape)
358 case(mg_v_shape)
359 call multigrid_solver_v_cycle(this, der, op, sol, rhs)
360 case(mg_w_shape)
361 call multigrid_solver_w_cycle(this, der, op, sol, rhs)
362 case default
363 assert(.false.)
364 end select
365 ! Compute the residual
366 call dderivatives_lapl(der, sol, err)
367 call lalg_axpy(der%mesh%np, -m_one, rhs, err)
368 resnorm = dmf_nrm2(der%mesh, err)
369
370 if (resnorm < this%threshold) exit
371
372 write(message(1), '(a,i5,a,e13.6)') "Multigrid: base level: iter ", iter, " res ", resnorm
373 call messages_info(1, namespace=namespace, debug_only=.true.)
374
375 end do
376
377 if (resnorm >= this%threshold) then
378 message(1) = 'Multigrid Poisson solver did not converge.'
379 write(message(2), '(a,e14.6)') ' Abs. norm of the residue = ', resnorm
380 call messages_warning(2, namespace=namespace)
381 else
382 write(message(1), '(a,i4,a)') "Multigrid Poisson solver converged in ", iter, " iterations."
383 write(message(2), '(a,e14.6)') ' Abs. norm of the residue = ', resnorm
384 call messages_info(2, namespace=namespace, debug_only=.true.)
385 end if
386
387 safe_deallocate_a(err)
388
390 end subroutine multigrid_iterative_solver
391
392 ! ---------------------------------------------------------
397 recursive subroutine multigrid_fmg_solver(this, namespace, der, op, sol, rhs)
398 type(mg_solver_t), intent(in) :: this
399 type(namespace_t), intent(in) :: namespace
400 type(derivatives_t), intent(in) :: der
401 type(nl_operator_t), intent(in) :: op
402 real(real64), contiguous, intent(inout) :: sol(:)
403 real(real64), contiguous, intent(inout) :: rhs(:)
404
405 real(real64), allocatable :: coarse_solution(:), coarse_rhs(:), residue(:)
406
407 push_sub(multigrid_fmg_solver)
408
409 if (associated(der%coarser)) then
410 assert(associated(op%coarser))
411
412 safe_allocate(coarse_rhs(1:der%coarser%mesh%np_part))
413 safe_allocate(coarse_solution(1:der%coarser%mesh%np_part))
414
415 ! Restriction of the r.h.s
416 message(1) = "Debug: Full Multigrid restriction"
417 call messages_info(1, debug_only=.true.)
418
419 call dmultigrid_fine2coarse(der%to_coarser, der, der%coarser%mesh, rhs, coarse_rhs, this%restriction_method)
420
421 ! Recursive call for the coarse-grid correction
422 coarse_solution = m_zero
423 call multigrid_fmg_solver(this, namespace, der%coarser, op%coarser, coarse_solution, coarse_rhs)
424
425 !Prolongation
426 message(1) = "Debug: Full Multigrid prolongation"
427 call messages_info(1, debug_only=.true.)
428
429 sol = m_zero
430 call dmultigrid_coarse2fine(der%to_coarser, der%coarser, der%mesh, coarse_solution, sol)
432 ! Perform N times a V cycle, up to convergence at each step
433 call multigrid_iterative_solver(this, namespace, der, op, sol, rhs, mg_v_shape)
434
435 safe_deallocate_a(coarse_solution)
436 safe_deallocate_a(coarse_rhs)
437
438 else ! Coarsest grid - solve the problem
439
440 safe_allocate(residue(1:der%mesh%np))
441 call multigrid_solver_solve_coarsest(this, der, op, sol, rhs, residue)
442 safe_deallocate_a(residue)
443 end if
444
445 pop_sub(multigrid_fmg_solver)
446 end subroutine multigrid_fmg_solver
447
448
449 ! ---------------------------------------------------------
451 subroutine multigrid_solver_solve_coarsest(this, der, op, sol, rhs, residue)
452 type(mg_solver_t), intent(in) :: this
453 type(derivatives_t), intent(in) :: der
454 type(nl_operator_t), intent(in) :: op
455 real(real64), contiguous, intent(inout) :: sol(:)
456 real(real64), contiguous, intent(in) :: rhs(:)
457 real(real64), contiguous, intent(inout) :: residue(:)
458
459 integer :: iter
460 real(real64) :: resnorm
461
463
464 ! Solution of the problem, i.e., multiple call to multigrid_relax up to convergence
465 do iter = 1, this%maxcycles
466
467 call multigrid_relax(this, der%mesh, der, op, sol, rhs, 1)
468
469 call get_residual(op, der, sol, rhs, residue)
470 resnorm = dmf_nrm2(der%mesh, residue)
471 if (resnorm < this%threshold) exit
472
473 end do
474
475 write(message(1), '(a,i4,a)') "Debug: Multigrid coarsest grid solver converged in ", iter, " iterations."
476 write(message(2), '(a,es18.6)') " Residue norm is ", resnorm
477 call messages_info(2, debug_only=.true.)
478
480 end subroutine
481
482 ! ---------------------------------------------------------
484 subroutine get_residual(op, der, sol, rhs, residue)
485 type(nl_operator_t), intent(in) :: op
486 type(derivatives_t), intent(in) :: der
487 real(real64), contiguous, intent(inout) :: sol(:)
488 real(real64), contiguous, intent(in) :: rhs(:)
489 real(real64), contiguous, intent(inout) :: residue(:)
491 integer :: ip
492
493 ! Compute the residue
494 call dderivatives_perform(op, der, sol, residue)
495 !$omp parallel do
496 do ip = 1, der%mesh%np
497 residue(ip) = rhs(ip) - residue(ip)
498 end do
499 end subroutine get_residual
500
501
502 ! ---------------------------------------------------------
506 subroutine multigrid_relax(this, mesh, der, op, sol, rhs, steps)
507 type(mg_solver_t), intent(in) :: this
508 type(mesh_t), intent(in) :: mesh
509 type(derivatives_t), intent(in) :: der
510 type(nl_operator_t), intent(in) :: op
511 real(real64), contiguous, intent(inout) :: sol(:)
512 real(real64), contiguous, intent(in) :: rhs(:)
513 integer, intent(in) :: steps
514
515 integer :: istep, index
516 integer :: ip, nn, is
517 real(real64) :: point, factor
518 real(real64), allocatable :: op_sol(:), diag(:)
519
520 push_sub(multigrid_relax)
521 call profiling_in("MG_GAUSS_SEIDEL")
522
523 select case (this%relaxation_method)
524
525 case (gauss_seidel)
526
527 do istep = 1, steps
528
529 call boundaries_set(der%boundaries, der%mesh, sol)
530
531 if (mesh%parallel_in_domains) then
532 call dpar_vec_ghost_update(mesh%pv, sol)
533 end if
534
535 nn = op%stencil%size
536
537 if (op%const_w) then
538 factor = -m_one/op%w(op%stencil%center, 1)
539 call dgauss_seidel(op%stencil%size, op%w(1, 1), op%nri, &
540 op%ri(1, 1), op%rimap_inv(1), op%rimap_inv(2), factor, sol(1), rhs(1))
541 else
542 !$omp parallel do private(point, index)
543 do ip = 1, mesh%np
544 point = m_zero
545 do is = 1, nn
546 index = nl_operator_get_index(op, is, ip)
547 point = point + op%w(is, ip)*sol(index)
548 end do
549 sol(ip) = sol(ip) - (point-rhs(ip))/op%w(op%stencil%center, ip)
550 end do
551 end if
552
553 end do
554 call profiling_count_operations(mesh%np*(steps + 1)*(2*nn + 3))
555
556 case (weighted_jacobi)
557
558 safe_allocate(op_sol(1:mesh%np))
559 safe_allocate(diag(1:mesh%np))
560
561 call dnl_operator_operate_diag(op, diag)
562 !$omp parallel do
563 do ip = 1, mesh%np
564 diag(ip) = this%relax_factor/diag(ip)
565 end do
566
567 do istep = 1, steps
568 call dderivatives_perform(op, der, sol, op_sol)
569 !$omp parallel do
570 do ip = 1, mesh%np
571 sol(ip) = sol(ip) - diag(ip)*(op_sol(ip) - rhs(ip))
572 end do
573 end do
574
575 safe_deallocate_a(diag)
576 safe_deallocate_a(op_sol)
578 end select
579
580 call profiling_out("MG_GAUSS_SEIDEL")
581 pop_sub(multigrid_relax)
582
583 end subroutine multigrid_relax
584
585end module multigrid_solver_oct_m
586
587!! Local Variables:
588!! mode: f90
589!! coding: utf-8
590!! End:
constant times a vector plus a vector
Definition: lalg_basic.F90:171
Module implementing boundary conditions in Octopus.
Definition: boundaries.F90:122
subroutine, public dpar_vec_ghost_update(pv, v_local)
Updates ghost points of every node.
This module calculates the derivatives (gradients, Laplacians, etc.) of a function.
subroutine, public dderivatives_perform(op, der, ff, op_ff, ghost_update, set_bc, factor)
apply a nl_operator to a mesh function
subroutine, public dderivatives_lapl(der, ff, op_ff, ghost_update, set_bc, factor)
apply the Laplacian to a mesh function
real(real64), parameter, public m_zero
Definition: global.F90:188
real(real64), parameter, public m_one
Definition: global.F90:189
This module defines various routines, operating on mesh functions.
This module defines the meshes, which are used in Octopus.
Definition: mesh.F90:118
subroutine, public messages_warning(no_lines, all_nodes, namespace)
Definition: messages.F90:537
character(len=256), dimension(max_lines), public message
to be output by fatal, warning
Definition: messages.F90:160
subroutine, public messages_input_error(namespace, var, details, row, column)
Definition: messages.F90:713
subroutine, public messages_info(no_lines, iunit, debug_only, stress, all_nodes, namespace)
Definition: messages.F90:616
subroutine, public dmultigrid_coarse2fine(tt, coarse_der, fine_mesh, f_coarse, f_fine, set_bc)
Definition: multigrid.F90:686
subroutine, public dmultigrid_fine2coarse(tt, fine_der, coarse_mesh, f_fine, f_coarse, method_p)
Definition: multigrid.F90:765
This modules provides the routines for solving Ax=b using the V-shaped multigrid method.
recursive subroutine, public multigrid_fmg_solver(this, namespace, der, op, sol, rhs)
Full multigrid (FMG) solver.
recursive subroutine, public multigrid_solver_v_cycle(this, der, op, sol, rhs)
Performs one cycle of a V-shaped multigrid solver.
recursive subroutine, public multigrid_solver_w_cycle(this, der, op, sol, rhs)
Performs one cycle of a W-shaped multigrid solver.
subroutine multigrid_solver_solve_coarsest(this, der, op, sol, rhs, residue)
Computes the solution on the coarsest grid.
subroutine, public multigrid_iterative_solver(this, namespace, der, op, sol, rhs, multigrid_shape)
An iterative multigrid solver.
integer, parameter weighted_jacobi
subroutine, public multigrid_solver_init(this, namespace, space, mesh, thr)
integer, parameter, public mg_fmg
subroutine get_residual(op, der, sol, rhs, residue)
Computes the residual.
subroutine multigrid_relax(this, mesh, der, op, sol, rhs, steps)
Given a nonlocal operator op, perform the relaxation operator.
integer, parameter, public mg_w_shape
This module defines non-local operators.
subroutine, public dnl_operator_operate_diag(op, fo)
integer pure function, public nl_operator_get_index(op, is, ip)
This module contains interfaces for routines in operate.c.
Definition: operate_f.F90:117
Some general things and nomenclature:
Definition: par_vec.F90:171
subroutine, public profiling_out(label)
Increment out counter and sum up difference between entry and exit time.
Definition: profiling.F90:623
subroutine, public profiling_in(label, exclude)
Increment in counter and save entry time.
Definition: profiling.F90:552
class representing derivatives
Describes mesh distribution to nodes.
Definition: mesh.F90:186
data type for non local operators
int true(void)