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 50
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', 50, 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 !% unless curvilinear coordinates are used, in which case the default is <tt>gauss_jacobi</tt>.
144 !%Option gauss_seidel 1
145 !% Gauss-Seidel.
146 !%Option weighted_jacobi 2
147 !% Jacobi relaxation with a weight. The weight is determined by by MultigridRelaxationFactor.
148 !%End
149 if (mesh%use_curvilinear) then
150 call parse_variable(namespace, 'MultigridRelaxationMethod', weighted_jacobi, this%relaxation_method)
151 else
152 call parse_variable(namespace, 'MultigridRelaxationMethod', gauss_seidel, this%relaxation_method)
153 end if
155 if (.not. varinfo_valid_option('MultigridRelaxationMethod', this%relaxation_method)) then
156 call messages_input_error(namespace, 'MultigridRelaxationMethod')
157 end if
158 call messages_print_var_option("MultigridRelaxationMethod", this%relaxation_method, namespace=namespace)
160 if (this%relaxation_method == weighted_jacobi) then
161 !%Variable MultigridRelaxationFactor
162 !%Type float
163 !%Section Hamiltonian::Poisson::Multigrid
164 !%Description
165 !% Relaxation factor of the relaxation operator used for the
166 !% multigrid method. Only used for the <tt>gauss_jacobi</tt> method.
167 !% The default is 0.6666 for the <tt>gauss_jacobi</tt> method.
168 !%End
169 call parse_variable(namespace, 'MultigridRelaxationFactor', 0.6666_real64, this%relax_factor)
170 end if
171
173 end subroutine multigrid_solver_init
174
175
176 ! ---------------------------------------------------------
180 recursive subroutine multigrid_solver_v_cycle(this, der, op, sol, rhs)
181 type(mg_solver_t), intent(in) :: this
182 type(derivatives_t), intent(in) :: der
183 type(nl_operator_t), intent(in) :: op
184 real(real64), contiguous, intent(inout) :: sol(:)
185 real(real64), contiguous, intent(in) :: rhs(:)
186
187 real(real64), allocatable :: residue(:), coarse_residue(:), correction(:), coarse_correction(:)
188
190
191 safe_allocate(residue(1:der%mesh%np_part))
192
193 if (associated(der%coarser)) then
194 assert(associated(op%coarser))
195
196 safe_allocate(correction(1:der%mesh%np_part))
197 safe_allocate(coarse_residue(1:der%coarser%mesh%np_part))
198 safe_allocate(coarse_correction(1:der%coarser%mesh%np_part))
199
200 ! Pre-Smoothing
201 call multigrid_relax(this, der%mesh, der, op, sol, rhs, this%presteps)
202
203 ! Compute the residual error
204 call get_residual(op, der, sol, rhs, residue)
205
206 ! Restriction of the residual is the next r.h.s
207 message(1) = "Debug: Multigrid restriction"
208 call messages_info(1, debug_only=.true.)
209
210 call dmultigrid_fine2coarse(der%to_coarser, der, der%coarser%mesh, residue, coarse_residue, this%restriction_method)
211
212 ! Recursive call for the coarse-grid correction
213 coarse_correction = m_zero
214 call multigrid_solver_v_cycle(this, der%coarser, op%coarser, coarse_correction, coarse_residue)
215
216 !Prolongation
217 message(1) = "Debug: Multigrid prolongation"
218 call messages_info(1, debug_only=.true.)
219
220 correction = m_zero
221 call dmultigrid_coarse2fine(der%to_coarser, der%coarser, der%mesh, coarse_correction, correction)
222
223 ! Correction
224 call lalg_axpy(der%mesh%np, m_one, correction, sol)
225
226 safe_deallocate_a(correction)
227 safe_deallocate_a(coarse_residue)
228 safe_deallocate_a(coarse_correction)
229
230 ! Post-Smoothing
231 call multigrid_relax(this, der%mesh, der, op, sol, rhs, this%poststeps)
232
233 else ! Coarsest grid
234
235 call multigrid_solver_solve_coarsest(this, der, op, sol, rhs, residue)
236
237 end if
238
239 safe_deallocate_a(residue)
240
242 end subroutine multigrid_solver_v_cycle
243
244 ! ---------------------------------------------------------
248 recursive subroutine multigrid_solver_w_cycle(this, der, op, sol, rhs)
249 type(mg_solver_t), intent(in) :: this
250 type(derivatives_t), intent(in) :: der
251 type(nl_operator_t), intent(in) :: op
252 real(real64), contiguous, intent(inout) :: sol(:)
253 real(real64), contiguous, intent(in) :: rhs(:)
254
255 real(real64), allocatable :: residue(:), coarse_residue(:), correction(:), coarse_correction(:)
256
258
259 safe_allocate(residue(1:der%mesh%np_part))
260
261 if (associated(der%coarser)) then
262 assert(associated(op%coarser))
263
264 safe_allocate(correction(1:der%mesh%np_part))
265 safe_allocate(coarse_residue(1:der%coarser%mesh%np_part))
266 safe_allocate(coarse_correction(1:der%coarser%mesh%np_part))
267
268 ! Pre-Smoothing
269 call multigrid_relax(this, der%mesh, der, op, sol, rhs, this%presteps)
270
271 ! Compute the residual error
272 call get_residual(op, der, sol, rhs, residue)
274 ! Restriction of the residual is the next r.h.s
275 message(1) = "Debug: Multigrid restriction"
276 call messages_info(1, debug_only=.true.)
277
278 call dmultigrid_fine2coarse(der%to_coarser, der, der%coarser%mesh, residue, coarse_residue, this%restriction_method)
279
280 ! Recursive call for the coarse-grid correction
281 coarse_correction = m_zero
282 call multigrid_solver_w_cycle(this, der%coarser, op%coarser, coarse_correction, coarse_residue)
283
284 !Prolongation
285 message(1) = "Debug: Multigrid prolongation"
286 call messages_info(1, debug_only=.true.)
287
288 correction = m_zero
289 call dmultigrid_coarse2fine(der%to_coarser, der%coarser, der%mesh, coarse_correction, correction)
290
291 ! Correction
292 call lalg_axpy(der%mesh%np, m_one, correction, sol)
293
294 ! Re-Smoothing
295 call multigrid_relax(this, der%mesh, der, op, sol, rhs, this%presteps)
296
297 ! Compute the residual error
298 call get_residual(op, der, sol, rhs, residue)
299
300 ! Restriction of the residual is the next r.h.s
301 message(1) = "Debug: Multigrid restriction"
302 call messages_info(1, debug_only=.true.)
303
304 call dmultigrid_fine2coarse(der%to_coarser, der, der%coarser%mesh, residue, coarse_residue, this%restriction_method)
305
306 ! Recursive call for the coarse-grid correction
307 coarse_correction = m_zero
308 call multigrid_solver_w_cycle(this, der%coarser, op%coarser, coarse_correction, coarse_residue)
309
310 !Prolongation
311 message(1) = "Debug: Multigrid prolongation"
312 call messages_info(1, debug_only=.true.)
313
314 correction = m_zero
315 call dmultigrid_coarse2fine(der%to_coarser, der%coarser, der%mesh, coarse_correction, correction)
316
317 ! Correction
318 call lalg_axpy(der%mesh%np, m_one, correction, sol)
319
320 safe_deallocate_a(correction)
321 safe_deallocate_a(coarse_residue)
322 safe_deallocate_a(coarse_correction)
323
324 ! Post-Smoothing
325 call multigrid_relax(this, der%mesh, der, op, sol, rhs, this%poststeps)
326
327 else ! Coarsest grid
328
329 call multigrid_solver_solve_coarsest(this, der, op, sol, rhs, residue)
330
331 end if
332
333 safe_deallocate_a(residue)
334
336
337 end subroutine multigrid_solver_w_cycle
338
339 ! ---------------------------------------------------------
343 subroutine multigrid_iterative_solver(this, namespace, der, op, sol, rhs, multigrid_shape)
344 type(mg_solver_t), intent(in) :: this
345 type(namespace_t), intent(in) :: namespace
346 type(derivatives_t), intent(in) :: der
347 type(nl_operator_t), intent(in) :: op
348 real(real64), contiguous, intent(inout) :: sol(:)
349 real(real64), contiguous, intent(inout) :: rhs(:)
350 integer, intent(in) :: multigrid_shape
351
352 integer :: iter
353 real(real64) :: resnorm
354 real(real64), allocatable :: err(:)
355
357
358 safe_allocate(err(1:der%mesh%np))
359
360 do iter = 1, this%maxcycles
361
362 select case (multigrid_shape)
363 case(mg_v_shape)
364 call multigrid_solver_v_cycle(this, der, op, sol, rhs)
365 case(mg_w_shape)
366 call multigrid_solver_w_cycle(this, der, op, sol, rhs)
367 case default
368 assert(.false.)
369 end select
370 ! Compute the residual
371 call dderivatives_lapl(der, sol, err)
372 call lalg_axpy(der%mesh%np, -m_one, rhs, err)
373 resnorm = dmf_nrm2(der%mesh, err)
374
375 if (resnorm < this%threshold) exit
376
377 write(message(1), '(a,i5,a,e13.6)') "Multigrid: base level: iter ", iter, " res ", resnorm
378 call messages_info(1, namespace=namespace, debug_only=.true.)
379
380 end do
381
382 if (resnorm >= this%threshold) then
383 message(1) = 'Multigrid Poisson solver did not converge.'
384 write(message(2), '(a,e14.6)') ' Abs. norm of the residue = ', resnorm
385 call messages_warning(2, namespace=namespace)
386 else
387 write(message(1), '(a,i4,a)') "Multigrid Poisson solver converged in ", iter, " iterations."
388 write(message(2), '(a,e14.6)') ' Abs. norm of the residue = ', resnorm
389 call messages_info(2, namespace=namespace, debug_only=.true.)
390 end if
391
392 safe_deallocate_a(err)
393
395 end subroutine multigrid_iterative_solver
396
397 ! ---------------------------------------------------------
402 recursive subroutine multigrid_fmg_solver(this, namespace, der, op, sol, rhs)
403 type(mg_solver_t), intent(in) :: this
404 type(namespace_t), intent(in) :: namespace
405 type(derivatives_t), intent(in) :: der
406 type(nl_operator_t), intent(in) :: op
407 real(real64), contiguous, intent(inout) :: sol(:)
408 real(real64), contiguous, intent(inout) :: rhs(:)
409
410 real(real64), allocatable :: coarse_solution(:), coarse_rhs(:), residue(:)
411
412 push_sub(multigrid_fmg_solver)
413
414 if (associated(der%coarser)) then
415 assert(associated(op%coarser))
416
417 safe_allocate(coarse_rhs(1:der%coarser%mesh%np_part))
418 safe_allocate(coarse_solution(1:der%coarser%mesh%np_part))
419
420 ! Restriction of the r.h.s
421 message(1) = "Debug: Full Multigrid restriction"
422 call messages_info(1, debug_only=.true.)
423
424 call dmultigrid_fine2coarse(der%to_coarser, der, der%coarser%mesh, rhs, coarse_rhs, this%restriction_method)
425
426 ! Recursive call for the coarse-grid correction
427 coarse_solution = m_zero
428 call multigrid_fmg_solver(this, namespace, der%coarser, op%coarser, coarse_solution, coarse_rhs)
429
430 !Prolongation
431 message(1) = "Debug: Full Multigrid prolongation"
432 call messages_info(1, debug_only=.true.)
433
434 sol = m_zero
435 call dmultigrid_coarse2fine(der%to_coarser, der%coarser, der%mesh, coarse_solution, sol)
437 ! Perform N times a V cycle, up to convergence at each step
438 call multigrid_iterative_solver(this, namespace, der, op, sol, rhs, mg_v_shape)
439
440 safe_deallocate_a(coarse_solution)
441 safe_deallocate_a(coarse_rhs)
442
443 else ! Coarsest grid - solve the problem
444
445 safe_allocate(residue(1:der%mesh%np))
446 call multigrid_solver_solve_coarsest(this, der, op, sol, rhs, residue)
447 safe_deallocate_a(residue)
448 end if
449
450 pop_sub(multigrid_fmg_solver)
451 end subroutine multigrid_fmg_solver
452
453
454 ! ---------------------------------------------------------
456 subroutine multigrid_solver_solve_coarsest(this, der, op, sol, rhs, residue)
457 type(mg_solver_t), intent(in) :: this
458 type(derivatives_t), intent(in) :: der
459 type(nl_operator_t), intent(in) :: op
460 real(real64), contiguous, intent(inout) :: sol(:)
461 real(real64), contiguous, intent(in) :: rhs(:)
462 real(real64), contiguous, intent(inout) :: residue(:)
463
464 integer :: iter
465 real(real64) :: resnorm
466
468
469 ! Solution of the problem, i.e., multiple call to multigrid_relax up to convergence
470 do iter = 1, this%maxcycles
471
472 call multigrid_relax(this, der%mesh, der, op, sol, rhs, 1)
473
474 call get_residual(op, der, sol, rhs, residue)
475 resnorm = dmf_nrm2(der%mesh, residue)
476 if (resnorm < this%threshold) exit
477
478 end do
479
480 write(message(1), '(a,i4,a)') "Debug: Multigrid coarsest grid solver converged in ", iter, " iterations."
481 write(message(2), '(a,es18.6)') " Residue norm is ", resnorm
482 call messages_info(2, debug_only=.true.)
483
485 end subroutine
486
487 ! ---------------------------------------------------------
489 subroutine get_residual(op, der, sol, rhs, residue)
490 type(nl_operator_t), intent(in) :: op
491 type(derivatives_t), intent(in) :: der
492 real(real64), contiguous, intent(inout) :: sol(:)
493 real(real64), contiguous, intent(in) :: rhs(:)
494 real(real64), contiguous, intent(inout) :: residue(:)
496 integer :: ip
497
498 ! Compute the residue
499 call dderivatives_perform(op, der, sol, residue)
500 !$omp parallel do
501 do ip = 1, der%mesh%np
502 residue(ip) = rhs(ip) - residue(ip)
503 end do
504 end subroutine get_residual
505
506
507 ! ---------------------------------------------------------
511 subroutine multigrid_relax(this, mesh, der, op, sol, rhs, steps)
512 type(mg_solver_t), intent(in) :: this
513 type(mesh_t), intent(in) :: mesh
514 type(derivatives_t), intent(in) :: der
515 type(nl_operator_t), intent(in) :: op
516 real(real64), contiguous, intent(inout) :: sol(:)
517 real(real64), contiguous, intent(in) :: rhs(:)
518 integer, intent(in) :: steps
519
520 integer :: istep, index
521 integer :: ip, nn, is
522 real(real64) :: point, factor
523 real(real64), allocatable :: op_sol(:), diag(:)
524
525 push_sub(multigrid_relax)
526 call profiling_in("MG_GAUSS_SEIDEL")
527
528 select case (this%relaxation_method)
529
530 case (gauss_seidel)
531
532 do istep = 1, steps
533
534 call boundaries_set(der%boundaries, der%mesh, sol)
535
536 if (mesh%parallel_in_domains) then
537 call dpar_vec_ghost_update(mesh%pv, sol)
538 end if
539
540 nn = op%stencil%size
541
542 if (op%const_w) then
543 factor = -m_one/op%w(op%stencil%center, 1)
544 call dgauss_seidel(op%stencil%size, op%w(1, 1), op%nri, &
545 op%ri(1, 1), op%rimap_inv(1), op%rimap_inv(2), factor, sol(1), rhs(1))
546 else
547 !$omp parallel do private(point, index)
548 do ip = 1, mesh%np
549 point = m_zero
550 do is = 1, nn
551 index = nl_operator_get_index(op, is, ip)
552 point = point + op%w(is, ip)*sol(index)
553 end do
554 sol(ip) = sol(ip) - (point-rhs(ip))/op%w(op%stencil%center, ip)
555 end do
556 end if
557
558 end do
559 call profiling_count_operations(mesh%np*(steps + 1)*(2*nn + 3))
560
561 case (weighted_jacobi)
562
563 safe_allocate(op_sol(1:mesh%np))
564 safe_allocate(diag(1:mesh%np))
565
566 call dnl_operator_operate_diag(op, diag)
567 !$omp parallel do
568 do ip = 1, mesh%np
569 diag(ip) = this%relax_factor/diag(ip)
570 end do
571
572 do istep = 1, steps
573 call dderivatives_perform(op, der, sol, op_sol)
574 !$omp parallel do
575 do ip = 1, mesh%np
576 sol(ip) = sol(ip) - diag(ip)*(op_sol(ip) - rhs(ip))
577 end do
578 end do
579
580 safe_deallocate_a(diag)
581 safe_deallocate_a(op_sol)
583 end select
584
585 call profiling_out("MG_GAUSS_SEIDEL")
586 pop_sub(multigrid_relax)
587
588 end subroutine multigrid_relax
589
590end module multigrid_solver_oct_m
591
592!! Local Variables:
593!! mode: f90
594!! coding: utf-8
595!! End:
constant times a vector plus a vector
Definition: lalg_basic.F90:170
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:187
real(real64), parameter, public m_one
Definition: global.F90:188
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:543
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:723
subroutine, public messages_info(no_lines, iunit, debug_only, stress, all_nodes, namespace)
Definition: messages.F90:624
subroutine, public dmultigrid_coarse2fine(tt, coarse_der, fine_mesh, f_coarse, f_fine, set_bc)
Definition: multigrid.F90:674
subroutine, public dmultigrid_fine2coarse(tt, fine_der, coarse_mesh, f_fine, f_coarse, method_p)
Definition: multigrid.F90:753
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)