Skip to content

Commit

Permalink
Add a SolverConfiguration to override command-line options.
Browse files Browse the repository at this point in the history
  • Loading branch information
BalticPinguin committed Aug 10, 2023
1 parent 6620cc1 commit 6f6a14f
Showing 1 changed file with 34 additions and 8 deletions.
42 changes: 34 additions & 8 deletions tests/numerics/shell_matrix.C
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "libmesh/shell_matrix.h"
#include "libmesh/enum_solver_type.h"
#include "libmesh/sparse_shell_matrix.h"
#include "libmesh/solver_configuration.h"
#ifdef LIBMESH_HAVE_PETSC
#include "libmesh/petsc_linear_solver.h"
#include "libmesh/petsc_vector.h"
Expand Down Expand Up @@ -37,10 +38,8 @@ public:
// Depending on the config: Use different vector-types here!
PetscLinearSolver<Number> linear_solver(*_comm);
linear_solver.init();
auto ierr = KSPSetType (linear_solver.ksp(), const_cast<KSPType>(KSPGMRES));
CHKERRABORT(linear_solver.comm().get(), ierr);
ierr = PCSetType (linear_solver.pc(), const_cast<PCType>(PCNONE));
CHKERRABORT(linear_solver.comm().get(), ierr);
NoPcSolverConfiguration solver_conf(linear_solver);
linear_solver.set_solver_configuration(solver_conf);
KSPSetInitialGuessNonzero(linear_solver.ksp(), PETSC_TRUE);

unsigned int maxits=20;
Expand Down Expand Up @@ -68,10 +67,8 @@ public:
// Depending on the config: Use different vector-types here!
PetscLinearSolver<Number> linear_solver(*_comm);
linear_solver.init();
auto ierr = KSPSetType (linear_solver.ksp(), const_cast<KSPType>(KSPGMRES));
CHKERRABORT(linear_solver.comm().get(), ierr);
ierr = PCSetType (linear_solver.pc(), const_cast<PCType>(PCNONE));
CHKERRABORT(linear_solver.comm().get(), ierr);
NoPcSolverConfiguration solver_conf(linear_solver);
linear_solver.set_solver_configuration(solver_conf);
KSPSetInitialGuessNonzero(linear_solver.ksp(), PETSC_TRUE);

PetscMatrix<Number> petsc_mat(*_comm);
Expand Down Expand Up @@ -99,11 +96,13 @@ public:
// a) set an irrelevant element to '0':
solution->set(7,2.);
petsc_mat.set(2,2,0.);
petsc_mat.close();
auto rval = linear_solver.solve(mat, *solution, rhs, tol, maxits);
// In this case, we can solve the equation.
CPPUNIT_ASSERT_EQUAL(rval.second, 0.00);
// b) set the relevant element to '0':
petsc_mat.set(5,5,0.);
petsc_mat.close();
rval = linear_solver.solve(mat, *solution, rhs, tol, maxits);
// In this case, we can not solve the equation.
// The best solution is the 0-vector and we have an error of 2.0
Expand All @@ -120,6 +119,33 @@ private:
M.set(n,n,1.);
}


class NoPcSolverConfiguration : public libMesh::SolverConfiguration
{
public:

NoPcSolverConfiguration(libMesh::PetscLinearSolver<libMesh::Number> & petsc_linear_solver) :
_petsc_linear_solver(petsc_linear_solver)
{
}

// Shell-matrices are implemented only for few solvers/preconditioners.
// The SolverConfiguration overrides input-arguments, so here we force GMRES without precond.
virtual void configure_solver()
{
PetscErrorCode ierr = 0;
ierr = KSPSetType (_petsc_linear_solver.ksp(), const_cast<KSPType>(KSPGMRES));
CHKERRABORT(_petsc_linear_solver.comm().get(), ierr);
ierr = PCSetType (_petsc_linear_solver.pc(), const_cast<PCType>(PCNONE));
CHKERRABORT(_petsc_linear_solver.comm().get(), ierr);
}

// The linear solver object that we are configuring
libMesh::PetscLinearSolver<libMesh::Number> & _petsc_linear_solver;

};


class UnityShellMat : public libMesh::ShellMatrix<libMesh::Number>
{
public:
Expand Down

0 comments on commit 6f6a14f

Please sign in to comment.