libMesh
Loading...
Searching...
No Matches
nonlinear_solver.C
Go to the documentation of this file.
1// The libMesh Finite Element Library.
2// Copyright (C) 2002-2026 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3
4// This library is free software; you can redistribute it and/or
5// modify it under the terms of the GNU Lesser General Public
6// License as published by the Free Software Foundation; either
7// version 2.1 of the License, or (at your option) any later version.
8
9// This library 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 GNU
12// Lesser General Public License for more details.
13
14// You should have received a copy of the GNU Lesser General Public
15// License along with this library; if not, write to the Free Software
16// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
18
19
20// Local Includes
21#include "libmesh/nonlinear_solver.h"
22#include "libmesh/petsc_nonlinear_solver.h"
23#include "libmesh/trilinos_nox_nonlinear_solver.h"
24#include "libmesh/solver_configuration.h"
25#include "libmesh/enum_solver_package.h"
26
27// C++ Includes
28#include <memory>
29
30
31namespace libMesh
32{
33
34
35//------------------------------------------------------------------
36// NonlinearSolver members
37#if defined(LIBMESH_HAVE_PETSC) || defined(LIBMESH_TRILINOS_HAVE_NOX)
38template <typename T>
39std::unique_ptr<NonlinearSolver<T>>
41{
42 // Build the appropriate solver
43 switch (solver_package)
44 {
45
46#ifdef LIBMESH_HAVE_PETSC
47 case PETSC_SOLVERS:
48 return std::make_unique<PetscNonlinearSolver<T>>(s);
49#endif // LIBMESH_HAVE_PETSC
50
51#if defined(LIBMESH_TRILINOS_HAVE_NOX) && defined(LIBMESH_TRILINOS_HAVE_EPETRA)
53 return std::make_unique<NoxNonlinearSolver<T>>(s);
54#endif
55
56 default:
57 libmesh_error_msg("ERROR: Unrecognized solver package: " << solver_package);
58 }
59}
60
61#else // LIBMESH_HAVE_PETSC || LIBMESH_TRILINOS_HAVE_NOX
62
63template <typename T>
64std::unique_ptr<NonlinearSolver<T>>
66{
67 libmesh_not_implemented_msg("ERROR: libMesh was compiled without nonlinear solver support");
68}
69#endif
70
71
72template <typename T>
73void
75{
76 libmesh_error_msg_if(this->_is_initialized, "Preconditioner must be attached before the solver is initialized!");
77
78 _preconditioner = preconditioner;
79}
80
81template <typename T>
83{
84 _solver_configuration = &solver_configuration;
85}
86
87template <typename T>
89{
90 libmesh_not_implemented();
91}
92
93template <typename T>
95{
96 _reuse_preconditioner = reuse;
97}
98
99template <typename T>
101{
102 libmesh_not_implemented();
103}
104
105template <typename T>
107{
108 _reuse_preconditioner_max_linear_its = i;
109}
110
111//------------------------------------------------------------------
112// Explicit instantiations
113template class LIBMESH_EXPORT NonlinearSolver<Number>;
114
115} // namespace libMesh
Manages consistently variables, degrees of freedom, coefficient vectors, matrices and non-linear solv...
This base class can be inherited from to provide interfaces to nonlinear solvers from different packa...
virtual void set_reuse_preconditioner(bool reuse)
Set the reuse preconditioner flag.
void attach_preconditioner(Preconditioner< T > *preconditioner)
Attaches a Preconditioner object to be used during the linear solves.
static std::unique_ptr< NonlinearSolver< T > > build(sys_type &s, const SolverPackage solver_package=libMesh::default_solver_package())
Builds a NonlinearSolver using the nonlinear solver package specified by solver_package.
virtual bool reuse_preconditioner() const
Get the reuse_preconditioner flag.
void set_solver_configuration(SolverConfiguration &solver_configuration)
Set the solver configuration object.
virtual void set_reuse_preconditioner_max_linear_its(unsigned int i)
Set the reuse_preconditioner_max_linear_its parameter.
virtual unsigned int reuse_preconditioner_max_linear_its() const
Get the reuse_preconditioner_max_linear_its parameter.
This class provides a uniform interface for preconditioners.
This class stores solver configuration data, e.g.
The libMesh namespace provides an interface to certain functionality in the library.
SolverPackage
Defines an enum for various linear solver packages.