Debian Codegraph @

include/deal.II/algorithms/newton.templates.h

raw
1 // ------------------------------------------------------------------------
2 //
3 // SPDX-License-Identifier: LGPL-2.1-or-later
4 // Copyright (C) 2010 - 2024 by the deal.II authors
5 //
6 // This file is part of the deal.II library.
7 //
8 // Part of the source code is dual licensed under Apache-2.0 WITH
9 // LLVM-exception OR LGPL-2.1-or-later. Detailed license information
10 // governing the source code and code contributions can be found in
11 // LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II.
12 //
13 // ------------------------------------------------------------------------
14
15 #ifndef dealii_newton_templates_h
16 #define dealii_newton_templates_h
17
18
19 #include <deal.II/base/config.h>
20
21 #include <deal.II/algorithms/newton.h>
22
23 #include <deal.II/base/logstream.h>
24 #include <deal.II/base/parameter_handler.h>
25
26 #include <deal.II/lac/vector_memory.h>
27
28 #include <iomanip>
29
30
31 DEAL_II_NAMESPACE_OPEN
32
33 namespace Algorithms
34 {
35 template <typename VectorType>
36 Newton<VectorType>::Newton(OperatorBase &residual,
37 OperatorBase &inverse_derivative)
38 : residual(&residual)
39 , inverse_derivative(&inverse_derivative)
40 , assemble_now(false)
41 , n_stepsize_iterations(21)
42 , assemble_threshold(0.)
43 , debug_vectors(false)
44 , debug(0)
45 {}
46
47
48 template <typename VectorType>
49 void
50 Newton<VectorType>::declare_parameters(ParameterHandler &param)
51 {
52 param.enter_subsection("Newton");
53 ReductionControl::declare_parameters(param);
54 param.declare_entry("Assemble threshold", "0.", Patterns::Double(0.));
55 param.declare_entry("Stepsize iterations", "21", Patterns::Integer(0));
56 param.declare_entry("Debug level", "0", Patterns::Integer(0));
57 param.declare_entry("Debug vectors", "false", Patterns::Bool());
58 param.leave_subsection();
59 }
60
61 template <typename VectorType>
62 void
63 Newton<VectorType>::parse_parameters(ParameterHandler &param)
64 {
65 param.enter_subsection("Newton");
66 control.parse_parameters(param);
67 assemble_threshold = param.get_double("Assemble threshold");
68 n_stepsize_iterations = param.get_integer("Stepsize iterations");
69 debug = param.get_integer("Debug level");
70 debug_vectors = param.get_bool("Debug vectors");
71 param.leave_subsection();
72 }
73
74 template <typename VectorType>
75 void
76 Newton<VectorType>::initialize(OutputOperator<VectorType> &output)
77 {
78 data_out = &output;
79 }
80
81 template <typename VectorType>
82 void
83 Newton<VectorType>::notify(const Event &e)
84 {
85 residual->notify(e);
86 inverse_derivative->notify(e);
87 }
88
89
90 template <typename VectorType>
91 double
92 Newton<VectorType>::threshold(const double thr)
93 {
94 const double t = assemble_threshold;
95 assemble_threshold = thr;
96 return t;
97 }
98
99
100 template <typename VectorType>
101 void
102 Newton<VectorType>::operator()(AnyData &out, const AnyData &in)
103 {
104 Assert(out.size() == 1, ExcNotImplemented());
105 LogStream::Prefix prefix("Newton");
106
107 VectorType &u = *out.entry<VectorType *>(0);
108
109 if (debug > 2)
110 deallog << "u: " << u.l2_norm() << std::endl;
111
112 GrowingVectorMemory<VectorType> mem;
113 typename VectorMemory<VectorType>::Pointer Du(mem);
114 typename VectorMemory<VectorType>::Pointer res(mem);
115
116 Du->reinit(u);
117 res->reinit(u);
118 AnyData src1;
119 AnyData src2;
120 src1.add<const VectorType *>(&u, "Newton iterate");
121 src1.merge(in);
122 src2.add<const VectorType *>(res.get(), "Newton residual");
123 src2.merge(src1);
124 AnyData out1;
125 out1.add<VectorType *>(res.get(), "Residual");
126 AnyData out2;
127 out2.add<VectorType *>(Du.get(), "Update");
128
129 unsigned int step = 0;
130 // fill res with (f(u), v)
131 (*residual)(out1, src1);
132 double resnorm = res->l2_norm();
133 double old_residual = 0.;
134
135 if (debug_vectors)
136 {
137 AnyData tmp;
138 VectorType *p = &u;
139 tmp.add<const VectorType *>(p, "solution");
140 p = Du.get();
141 tmp.add<const VectorType *>(p, "update");
142 p = res.get();
143 tmp.add<const VectorType *>(p, "residual");
144 *data_out << step;
145 *data_out << tmp;
146 }
147
148 while (control.check(step++, resnorm) == SolverControl::iterate)
149 {
150 // assemble (Df(u), v)
151 if ((step > 1) && (resnorm / old_residual >= assemble_threshold))
152 inverse_derivative->notify(Events::bad_derivative);
153
154 Du->reinit(u);
155 try
156 {
157 (*inverse_derivative)(out2, src2);
158 }
159 catch (const SolverControl::NoConvergence &e)
160 {
161 deallog << "Inner iteration failed after " << e.last_step
162 << " steps with residual " << e.last_residual << std::endl;
163 }
164
165 if (debug_vectors)
166 {
167 AnyData tmp;
168 VectorType *p = &u;
169 tmp.add<const VectorType *>(p, "solution");
170 p = Du.get();
171 tmp.add<const VectorType *>(p, "update");
172 p = res.get();
173 tmp.add<const VectorType *>(p, "residual");
174 *data_out << step;
175 *data_out << tmp;
176 }
177
178 u.add(-1., *Du);
179 old_residual = resnorm;
180 (*residual)(out1, src1);
181 resnorm = res->l2_norm();
182
183 // Step size control
184 unsigned int step_size = 0;
185 while (resnorm >= old_residual)
186 {
187 ++step_size;
188 if (step_size > n_stepsize_iterations)
189 {
190 deallog << "No smaller stepsize allowed!";
191 break;
192 }
193 if (control.log_history())
194 deallog << "Trying step size: 1/" << (1 << step_size)
195 << " since residual was " << resnorm << std::endl;
196 u.add(1. / (1 << step_size), *Du);
197 (*residual)(out1, src1);
198 resnorm = res->l2_norm();
199 }
200 }
201
202 // in case of failure: throw exception
203 if (control.last_check() != SolverControl::success)
204 AssertThrow(false,
205 SolverControl::NoConvergence(control.last_step(),
206 control.last_value()));
207 // otherwise exit as normal
208 }
209 } // namespace Algorithms
210
211
212 DEAL_II_NAMESPACE_CLOSE
213
214 #endif