deal.II version GIT relicensing-6834-g5b78e6bcdf 2026-10-01 11:20:01+00:00
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
Loading...
Searching...
No Matches
solver_selector.h
Go to the documentation of this file.
1// -----------------------------------------------------------------------------
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception OR LGPL-2.1-or-later
4// Copyright (C) 1999 - 2024 by the deal.II authors
5//
6// This file is part of the deal.II library.
7//
8// Detailed license information governing the source code and contributions
9// can be found in LICENSE.md and CONTRIBUTING.md at the top level directory.
10//
11// -----------------------------------------------------------------------------
12
13#ifndef dealii_solver_selector_h
14#define dealii_solver_selector_h
15
16
17#include <deal.II/base/config.h>
18
21
23#include <deal.II/lac/solver.h>
30#include <deal.II/lac/vector.h>
31
33
34
88template <typename VectorType = Vector<double>>
91{
92public:
96 using vector_type = VectorType;
97
101 SolverSelector() = default;
102
107 SolverSelector(const std::string &name, SolverControl &control);
108
112 virtual ~SolverSelector() override = default;
113
118 template <typename MatrixType, typename PreconditionerType>
122 void solve(const MatrixType &A,
123 VectorType &x,
124 const VectorType &b,
125 const PreconditionerType &precond) const;
126
131 void
132 select(const std::string &name);
133
137 void
138 set_control(SolverControl &ctrl);
139
143 void
144 set_data(const typename SolverRichardson<VectorType>::AdditionalData &data);
145
149 void
150 set_data(const typename SolverCG<VectorType>::AdditionalData &data);
151
155 void
156 set_data(const typename SolverMinRes<VectorType>::AdditionalData &data);
157
161 void
162 set_data(const typename SolverBicgstab<VectorType>::AdditionalData &data);
163
167 void
168 set_data(const typename SolverGMRES<VectorType>::AdditionalData &data);
169
173 void
174 set_data(const typename SolverFGMRES<VectorType>::AdditionalData &data);
175
188 static std::string
189 get_solver_names();
190
194 DeclException1(ExcSolverDoesNotExist,
195 std::string,
196 << "Solver " << arg1 << " does not exist. Use one of "
197 << std::endl
198 << get_solver_names());
199
200
201
202protected:
208
212 std::string solver_name;
213
214private:
219
224
229
234
239
244};
245
247/* --------------------- Inline and template functions ------------------- */
248
249
250template <typename VectorType>
252SolverSelector<VectorType>::SolverSelector(const std::string &name,
253 SolverControl &solver_control)
254 : solver_name(name)
255 , control(&solver_control)
256{}
257
258
259
260template <typename VectorType>
262void SolverSelector<VectorType>::select(const std::string &name)
263{
264 solver_name = name;
265}
266
267
268
269template <typename VectorType>
271template <typename MatrixType, typename PreconditionerType>
275void SolverSelector<VectorType>::solve(const MatrixType &A,
276 VectorType &x,
277 const VectorType &b,
278 const PreconditionerType &precond) const
279{
280 if (solver_name == "richardson")
281 {
282 SolverRichardson<VectorType> solver(*control, richardson_data);
283 solver.solve(A, x, b, precond);
284 }
285 else if (solver_name == "cg")
286 {
287 SolverCG<VectorType> solver(*control, cg_data);
288 solver.solve(A, x, b, precond);
289 }
290 else if (solver_name == "minres")
291 {
292 SolverMinRes<VectorType> solver(*control, minres_data);
293 solver.solve(A, x, b, precond);
294 }
295 else if (solver_name == "bicgstab")
296 {
297 SolverBicgstab<VectorType> solver(*control, bicgstab_data);
298 solver.solve(A, x, b, precond);
299 }
300 else if (solver_name == "gmres")
301 {
302 SolverGMRES<VectorType> solver(*control, gmres_data);
303 solver.solve(A, x, b, precond);
304 }
305 else if (solver_name == "fgmres")
306 {
307 SolverFGMRES<VectorType> solver(*control, fgmres_data);
308 solver.solve(A, x, b, precond);
309 }
310 else
311 Assert(false, ExcSolverDoesNotExist(solver_name));
312}
313
314
315
316template <typename VectorType>
318void SolverSelector<VectorType>::set_control(SolverControl &ctrl)
319{
320 control = &ctrl;
321}
322
323
324
325template <typename VectorType>
327std::string SolverSelector<VectorType>::get_solver_names()
328{
329 return "richardson|cg|bicgstab|gmres|fgmres|minres";
330}
331
332
333
334template <typename VectorType>
336void SolverSelector<VectorType>::set_data(
337 const typename SolverGMRES<VectorType>::AdditionalData &data)
338{
339 gmres_data = data;
340}
341
342
343
344template <typename VectorType>
346void SolverSelector<VectorType>::set_data(
347 const typename SolverFGMRES<VectorType>::AdditionalData &data)
348{
349 fgmres_data = data;
350}
351
352
353
354template <typename VectorType>
356void SolverSelector<VectorType>::set_data(
357 const typename SolverRichardson<VectorType>::AdditionalData &data)
358{
359 richardson_data = data;
360}
361
362
363
364template <typename VectorType>
366void SolverSelector<VectorType>::set_data(
367 const typename SolverCG<VectorType>::AdditionalData &data)
368{
369 cg_data = data;
370}
371
372
373
374template <typename VectorType>
376void SolverSelector<VectorType>::set_data(
377 const typename SolverMinRes<VectorType>::AdditionalData &data)
378{
379 minres_data = data;
380}
381
382
383
384template <typename VectorType>
386void SolverSelector<VectorType>::set_data(
387 const typename SolverBicgstab<VectorType>::AdditionalData &data)
388{
389 bicgstab_data = data;
390}
391
393
394#endif
void solve(const MatrixType &A, VectorType &x, const VectorType &b, const PreconditionerType &preconditioner)
void solve(const MatrixType &A, VectorType &x, const VectorType &b, const PreconditionerType &preconditioner)
void solve(const MatrixType &A, VectorType &x, const VectorType &b, const PreconditionerTypes &...preconditioners)
void solve(const MatrixType &A, VectorType &x, const VectorType &b, const PreconditionerType &preconditioner)
void solve(const MatrixType &A, VectorType &x, const VectorType &b, const PreconditionerType &preconditioner)
void solve(const MatrixType &A, VectorType &x, const VectorType &b, const PreconditionerType &preconditioner)
SolverGMRES< VectorType >::AdditionalData gmres_data
VectorType vector_type
virtual ~SolverSelector() override=default
SolverBicgstab< VectorType >::AdditionalData bicgstab_data
SolverFGMRES< VectorType >::AdditionalData fgmres_data
SolverMinRes< VectorType >::AdditionalData minres_data
ObserverPointer< SolverControl, SolverSelector< VectorType > > control
std::string solver_name
SolverSelector()=default
SolverCG< VectorType >::AdditionalData cg_data
SolverRichardson< VectorType >::AdditionalData richardson_data
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:38
#define DEAL_II_CXX20_REQUIRES(condition)
Definition config.h:249
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:39
#define Assert(cond, exc)
#define DeclException1(Exception1, type1, outsequence)
std::vector< index_type > data
Definition mpi.cc:734
STL namespace.