18#include <libmesh/libmesh_config.h>
23#if defined(LIBMESH_HAVE_PETSC) || defined(LIBMESH_TRILINOS_HAVE_NOX)
25#include <libmesh/nonlinear_implicit_system.h>
26#include <libmesh/nonlinear_solver.h>
27#include <libmesh/sparse_matrix.h>
28#include <libmesh/libmesh_exceptions.h>
29#include <libmesh/equation_systems.h>
30#include <libmesh/mesh.h>
31#include <libmesh/mesh_generation.h>
53 virtual void init(
const char * =
nullptr)
override {}
55 virtual std::pair<unsigned int, Real>
60 const unsigned int)
override
62 solved_with = &jac_in;
63 ++single_matrix_calls;
71 unsigned int single_matrix_calls = 0;
79class TwoMatrixNonlinearSolver :
public SingleMatrixNonlinearSolver
86 using SingleMatrixNonlinearSolver::solve;
88 virtual std::pair<unsigned int, Real>
94 const unsigned int)
override
96 solved_with = &jac_in;
97 solved_with_pre = &pre_in;
103 unsigned int two_matrix_calls = 0;
114#ifdef LIBMESH_ENABLE_EXCEPTIONS
123 std::unique_ptr<EquationSystems>
es;
130 es = std::make_unique<EquationSystems>(*
mesh);
148 auto solver = std::make_unique<TwoMatrixNonlinearSolver>(*
sys);
149 auto * solver_ptr = solver.get();
154 CPPUNIT_ASSERT(solver_ptr->solved_with ==
sys->
matrix);
155 CPPUNIT_ASSERT_EQUAL(solver_ptr->single_matrix_calls, 1u);
156 CPPUNIT_ASSERT_EQUAL(solver_ptr->two_matrix_calls, 0u);
169 auto solver = std::make_unique<TwoMatrixNonlinearSolver>(*
sys);
170 auto * solver_ptr = solver.get();
175 CPPUNIT_ASSERT(solver_ptr->solved_with == amat.get());
176 CPPUNIT_ASSERT(solver_ptr->solved_with_pre ==
sys->
matrix);
177 CPPUNIT_ASSERT_EQUAL(solver_ptr->two_matrix_calls, 1u);
178 CPPUNIT_ASSERT_EQUAL(solver_ptr->single_matrix_calls, 0u);
181#ifdef LIBMESH_ENABLE_EXCEPTIONS
194 auto solver = std::make_unique<SingleMatrixNonlinearSolver>(*
sys);
std::unique_ptr< EquationSystems > es
CPPUNIT_TEST(testTwoMatrixSolveWithoutOverrideThrows)
void testOperatorMatrixUsesTwoMatrixSolve()
CPPUNIT_TEST(testOperatorMatrixUsesTwoMatrixSolve)
LIBMESH_CPPUNIT_TEST_SUITE(NonlinearImplicitSystemOperatorMatrixTest)
CPPUNIT_TEST(testDefaultUsesSingleMatrixSolve)
void testTwoMatrixSolveWithoutOverrideThrows()
void testDefaultUsesSingleMatrixSolve()
NonlinearImplicitSystem * sys
std::unique_ptr< Mesh > mesh
SparseMatrix< Number > * matrix
The system matrix.
Manages consistently variables, degrees of freedom, coefficient vectors, matrices and non-linear solv...
virtual void solve() override
Assembles & solves the nonlinear system R(x) = 0.
std::unique_ptr< NonlinearSolver< Number > > nonlinear_solver
The NonlinearSolver defines the default interface used to solve the nonlinear_implicit system.
void set_operator_matrix(SparseMatrix< Number > *mat)
Set a matrix to use as the actual Jacobian operator (Amat) on the next solve(), distinct from matrix,...
This base class can be inherited from to provide interfaces to nonlinear solvers from different packa...
virtual unsigned get_current_nonlinear_iteration_number() const =0
virtual std::pair< unsigned int, Real > solve(SparseMatrix< T > &, NumericVector< T > &, NumericVector< T > &, const double, const unsigned int)=0
Solves the nonlinear system.
virtual void init(const char *name=nullptr)=0
Initialize data structures if not done so already.
virtual int get_total_linear_iterations()=0
Get the total number of linear iterations done in the last solve.
A class to stub for features that should be in libMesh, but haven't been written yet,...
Provides a uniform interface to vector storage schemes for different linear algebra libraries.
static std::unique_ptr< SparseMatrix< T > > build(const Parallel::Communicator &comm, const SolverPackage solver_package=libMesh::default_solver_package(), const MatrixBuildType matrix_build_type=MatrixBuildType::AUTOMATIC)
Builds a SparseMatrix<T> using the linear solver package specified by solver_package.
void init()
Initializes degrees of freedom on the current mesh.
Communicator * TestCommWorld
The libMesh namespace provides an interface to certain functionality in the library.
CPPUNIT_TEST_SUITE_REGISTRATION(NonlinearImplicitSystemOperatorMatrixTest)