Octopus
root_solver.F90
Go to the documentation of this file.
1!! Copyright (C) 2005-2006 Heiko Appel
2!!
3!! This program is free software; you can redistribute it and/or modify
4!! it under the terms of the GNU General Public License as published by
5!! the Free Software Foundation; either version 2, or (at your option)
6!! any later version.
7!!
8!! This program is distributed in the hope that it will be useful,
9!! but WITHOUT ANY WARRANTY; without even the implied warranty of
10!! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11!! GNU General Public License for more details.
12!!
13!! You should have received a copy of the GNU General Public License
14!! along with this program; if not, write to the Free Software
15!! Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16!! 02110-1301, USA.
17!!
18
19#include "global.h"
20
22 use debug_oct_m
23 use global_oct_m
24 use, intrinsic :: iso_fortran_env
28 use parser_oct_m
31
32 implicit none
33
34 private
35 public :: &
40
41 integer, public, parameter :: &
42 ROOT_NEWTON = 3
43
44 type root_solver_t
45 private
46 integer :: solver_type
47 integer :: dim
48 integer :: maxiter
49 integer :: usediter
50 real(real64) :: abs_tolerance
51 real(real64) :: rel_tolerance
52 end type root_solver_t
53
54contains
55
56 ! ---------------------------------------------------------
57 subroutine root_solver_init(rs, namespace, dimensionality, solver_type, maxiter, &
58 rel_tolerance, abs_tolerance)
59 type(root_solver_t), intent(out) :: rs
60 type(namespace_t), intent(in) :: namespace
61 integer, intent(in) :: dimensionality
62 integer, optional, intent(in) :: solver_type, maxiter
63 real(real64), optional, intent(in) :: rel_tolerance, abs_tolerance
64
65 ! no push_sub, called too often
66
67 ! Set dimension
68 rs%dim = dimensionality
69
70 ! Read config parameters for the solver
71 call root_solver_read(rs, namespace)
72
73 if (present(solver_type)) rs%solver_type = solver_type
74 if (present(maxiter)) rs%maxiter = maxiter
75 if (present(rel_tolerance)) rs%rel_tolerance = rel_tolerance
76 if (present(abs_tolerance)) rs%abs_tolerance = abs_tolerance
77
78 end subroutine root_solver_init
79
80
81 ! ---------------------------------------------------------
82 subroutine root_solver_read(rs, namespace)
83 type(root_solver_t), intent(inout) :: rs
84 type(namespace_t), intent(in) :: namespace
85
86 push_sub(root_solver_read)
87
88 !%Variable RootSolver
89 !%Type integer
90 !%Default root_newton
91 !%Section Math::RootSolver
92 !%Description
93 !% Specifies what kind of root solver will be used.
94 !%Option root_newton 3
95 !% Newton-Raphson solver with backtracking.
96 !%End
97 call parse_variable(namespace, 'RootSolver', root_newton, rs%solver_type)
98 if (.not. varinfo_valid_option('RootSolver', rs%solver_type)) then
99 call messages_input_error(namespace, 'RootSolver')
100 end if
101
102 !%Variable RootSolverMaxIter
103 !%Type integer
104 !%Default 500
105 !%Section Math::RootSolver
106 !%Description
107 !% In case of an iterative root solver, this variable determines the maximum number
108 !% of iteration steps.
109 !%End
110 call parse_variable(namespace, 'RootSolverMaxIter', 500, rs%maxiter)
111
112 !%Variable RootSolverAbsTolerance
113 !%Type float
114 !%Default 1e-10
115 !%Section Math::RootSolver
116 !%Description
117 !% Relative tolerance for the root-finding process.
118 !%End
119 call parse_variable(namespace, 'RootSolverAbsTolerance', 1e-10_real64, rs%abs_tolerance)
120
121 pop_sub(root_solver_read)
122 end subroutine root_solver_read
123
124 ! ---------------------------------------------------------
125 subroutine droot_solver_run(rs, func, root, success, startval)
126 type(root_solver_t), intent(in) :: rs
127 real(real64), intent(out) :: root(:)
128 logical, intent(out) :: success
129 real(real64), optional, intent(in) :: startval(:)
130 interface
131 subroutine func(z, f, jf)
132 import real64
133 implicit none
134 real(real64), intent(in) :: z(:)
135 real(real64), intent(out) :: f(:), jf(:, :)
136 end subroutine func
137 end interface
138
139 ! no push_sub, called too often
140
141 ! Initializations
142 root = m_zero
143 success = .false.
145 select case (rs%solver_type)
146 case (root_newton)
147 call droot_newton(rs, func, root, startval, success)
148 case default
149 write(message(1), '(a,i4,a)') "Error in droot_solver_run: '", rs%solver_type, "' is not a valid root solver"
150 call messages_fatal(1)
151 end select
153 end subroutine
154
155 ! ---------------------------------------------------------
159 subroutine droot_newton(rs, func, root, startval, success)
160 type(root_solver_t), intent(in) :: rs
161 real(real64), intent(out) :: root(:)
162 real(real64), optional, intent(in) :: startval(:)
163 logical, intent(out) :: success
164 interface
165 subroutine func(z, f, jf)
166 import real64
167 implicit none
168 real(real64), intent(in) :: z(:)
169 real(real64), intent(out) :: f(:), jf(:, :)
170 end subroutine func
171 end interface
172
173 integer :: iter
174 real(real64) :: err, prev_err
175 real(real64), allocatable :: f(:), jf(:, :), delta(:, :), rhs(:, :), root_trial(:)
176
177 real(real64) :: mm, prev_m, alpha, err_trial
178
179 ! no push_sub, called too often
180
181 safe_allocate(f(1:rs%dim))
182 safe_allocate(root_trial(1:rs%dim))
183 safe_allocate(jf(1:rs%dim, 1:rs%dim))
184 safe_allocate(delta(1:rs%dim, 1))
185 safe_allocate(rhs(1:rs%dim, 1))
186
187 if (present(startval)) then
188 root = startval
189 else
190 root = m_zero
191 end if
192
193 call func(root, f, jf)
194 err = norm2(f)
195 prev_err = err
196 mm = m_one
197 prev_m = m_one
198
199 success = .true.
200 iter = 0
201 do while(err > rs%abs_tolerance)
202 rhs(1:rs%dim, 1) = -mm * f(1:rs%dim)
203
204 call lalg_linsyssolve(rs%dim, 1, jf, rhs, delta)
205
206 ! Backtracking damping
207 alpha = 1.0_real64
208 do
209 root_trial = root + alpha * delta(:,1)
210 call func(root_trial, f, jf)
211 err_trial = norm2(f)
212
213 if (err_trial < err) exit ! accept
214 alpha = 0.5_real64 * alpha
215 if (alpha < 1e-12_real64) then
216 success = .false.
217 exit
218 end if
219 end do
220 if (.not. success) exit ! Damping failed
221
222 root = root_trial
223 prev_err = err
224 err = err_trial
225
226 iter = iter + 1
227 if (iter > rs%maxiter) then
228 success = .false.
229 exit
230 end if
231
232 ! Let us assume we have a linear convergence due to a multiplicity m of the solution,
233 ! Then we have |err_i+1| = (m-1)/m |err_i|
234 ! Or (m-1)/m = |err_i+1|/|err_i| = A, where A is the asymptotic error constant
235 ! for i sufficiently large.
236 !
237 ! Then we can estimate m from the first two iterations and use it to accelerate the convergence
238 !
239 ! See https://web.archive.org/web/20190524083302/http://mathfaculty.fullerton.edu/mathews/n2003/NewtonAccelerateMod.html
240 ! as well as
241 ! https://en.wikipedia.org/wiki/Newton%27s_method#Slow_convergence_for_roots_of_multiplicity_greater_than_1
242 ! for some more details
243 if (iter == 1) then
244 prev_m = floor(m_one / (m_one - err/prev_err))
245 end if
246 if (iter == 2) then
247 mm = floor(m_one / (m_one - err/prev_err))
248 ! If we get the same multiplicity, we use it for the next iterations
249 if (abs(mm - prev_m) > 0.5_real64) mm = m_one
250 ! If the multiplicity is going to infinity, we do not want to use this value
251 if ( mm > 10._real64 .or. mm < 1.0_real64) mm = m_one
252 end if
253
254 end do
255
256 safe_deallocate_a(f)
257 safe_deallocate_a(jf)
258 safe_deallocate_a(delta)
259 safe_deallocate_a(rhs)
260
261 end subroutine droot_newton
262
263end module root_solver_oct_m
264
265!! Local Variables:
266!! mode: f90
267!! coding: utf-8
268!! End:
double floor(double __x) __attribute__((__nothrow__
real(real64) function func(r1, rn, n, a)
Definition: logrid.F90:327
real(real64), parameter, public m_zero
Definition: global.F90:200
real(real64), parameter, public m_one
Definition: global.F90:201
character(len=256), dimension(max_lines), public message
to be output by fatal, warning
Definition: messages.F90:162
subroutine, public messages_fatal(no_lines, only_root_writes, namespace)
Definition: messages.F90:410
subroutine, public messages_input_error(namespace, var, details, row, column)
Definition: messages.F90:691
subroutine droot_newton(rs, func, root, startval, success)
Newton-Raphson scheme can only be used in the real case.
subroutine, public root_solver_init(rs, namespace, dimensionality, solver_type, maxiter, rel_tolerance, abs_tolerance)
subroutine, public droot_solver_run(rs, func, root, success, startval)
subroutine, public root_solver_read(rs, namespace)
static double f(double w, void *p)
int true(void)