deal.II version GIT relicensing-2233-g9bd70d8389 2024-12-08 16:10:00+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: 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// 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_solver_selector_h
16#define dealii_solver_selector_h
17
18
19#include <deal.II/base/config.h>
20
23
25#include <deal.II/lac/solver.h>
32#include <deal.II/lac/vector.h>
33
35
36
90template <typename VectorType = Vector<double>>
93{
94public:
98 using vector_type = VectorType;
99
103 SolverSelector() = default;
104
109 SolverSelector(const std::string &name, SolverControl &control);
110
114 virtual ~SolverSelector() override = default;
115
120 template <typename MatrixType, typename PreconditionerType>
124 void solve(const MatrixType &A,
125 VectorType &x,
126 const VectorType &b,
127 const PreconditionerType &precond) const;
128
133 void
134 select(const std::string &name);
135
139 void
140 set_control(SolverControl &ctrl);
141
145 void
146 set_data(const typename SolverRichardson<VectorType>::AdditionalData &data);
147
151 void
152 set_data(const typename SolverCG<VectorType>::AdditionalData &data);
153
157 void
158 set_data(const typename SolverMinRes<VectorType>::AdditionalData &data);
159
163 void
164 set_data(const typename SolverBicgstab<VectorType>::AdditionalData &data);
165
169 void
170 set_data(const typename SolverGMRES<VectorType>::AdditionalData &data);
171
175 void
176 set_data(const typename SolverFGMRES<VectorType>::AdditionalData &data);
177
190 static std::string
191 get_solver_names();
192
196 DeclException1(ExcSolverDoesNotExist,
197 std::string,
198 << "Solver " << arg1 << " does not exist. Use one of "
199 << std::endl
200 << get_solver_names());
201
202
203
204protected:
210
214 std::string solver_name;
215
216private:
221
226
231
236
241
246};
247
249/* --------------------- Inline and template functions ------------------- */
250
251
252template <typename VectorType>
254SolverSelector<VectorType>::SolverSelector(const std::string &name,
255 SolverControl &solver_control)
256 : solver_name(name)
257 , control(&solver_control)
258{}
259
260
261
262template <typename VectorType>
264void SolverSelector<VectorType>::select(const std::string &name)
265{
266 solver_name = name;
267}
268
269
270
271template <typename VectorType>
273template <typename MatrixType, typename PreconditionerType>
277void SolverSelector<VectorType>::solve(const MatrixType &A,
278 VectorType &x,
279 const VectorType &b,
280 const PreconditionerType &precond) const
281{
282 if (solver_name == "richardson")
283 {
284 SolverRichardson<VectorType> solver(*control, richardson_data);
285 solver.solve(A, x, b, precond);
286 }
287 else if (solver_name == "cg")
288 {
289 SolverCG<VectorType> solver(*control, cg_data);
290 solver.solve(A, x, b, precond);
291 }
292 else if (solver_name == "minres")
293 {
294 SolverMinRes<VectorType> solver(*control, minres_data);
295 solver.solve(A, x, b, precond);
296 }
297 else if (solver_name == "bicgstab")
298 {
299 SolverBicgstab<VectorType> solver(*control, bicgstab_data);
300 solver.solve(A, x, b, precond);
301 }
302 else if (solver_name == "gmres")
303 {
304 SolverGMRES<VectorType> solver(*control, gmres_data);
305 solver.solve(A, x, b, precond);
306 }
307 else if (solver_name == "fgmres")
308 {
309 SolverFGMRES<VectorType> solver(*control, fgmres_data);
310 solver.solve(A, x, b, precond);
311 }
312 else
313 Assert(false, ExcSolverDoesNotExist(solver_name));
314}
315
316
317
318template <typename VectorType>
320void SolverSelector<VectorType>::set_control(SolverControl &ctrl)
321{
322 control = &ctrl;
323}
324
325
326
327template <typename VectorType>
329std::string SolverSelector<VectorType>::get_solver_names()
330{
331 return "richardson|cg|bicgstab|gmres|fgmres|minres";
332}
333
334
335
336template <typename VectorType>
338void SolverSelector<VectorType>::set_data(
339 const typename SolverGMRES<VectorType>::AdditionalData &data)
340{
341 gmres_data = data;
342}
343
344
345
346template <typename VectorType>
348void SolverSelector<VectorType>::set_data(
349 const typename SolverFGMRES<VectorType>::AdditionalData &data)
350{
351 fgmres_data = data;
352}
353
354
355
356template <typename VectorType>
358void SolverSelector<VectorType>::set_data(
359 const typename SolverRichardson<VectorType>::AdditionalData &data)
360{
361 richardson_data = data;
362}
363
364
365
366template <typename VectorType>
368void SolverSelector<VectorType>::set_data(
369 const typename SolverCG<VectorType>::AdditionalData &data)
370{
371 cg_data = data;
372}
373
374
375
376template <typename VectorType>
378void SolverSelector<VectorType>::set_data(
379 const typename SolverMinRes<VectorType>::AdditionalData &data)
380{
381 minres_data = data;
382}
383
384
385
386template <typename VectorType>
388void SolverSelector<VectorType>::set_data(
389 const typename SolverBicgstab<VectorType>::AdditionalData &data)
390{
391 bicgstab_data = data;
392}
393
395
396#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 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)
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:498
#define DEAL_II_CXX20_REQUIRES(condition)
Definition config.h:175
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:499
#define Assert(cond, exc)
#define DeclException1(Exception1, type1, outsequence)
Definition exceptions.h:511
std::vector< index_type > data
Definition mpi.cc:735
STL namespace.