Skip to content

Commit

Permalink
Modified setup to provide default getter
Browse files Browse the repository at this point in the history
Use getter/setter to supply the required properties for the new reuse
capability.  Default to libmesh_not_implemented() in superclass,
implemented in the petsc solvers.
  • Loading branch information
reverendbedford committed Jun 14, 2022
1 parent 5e1849f commit c911e49
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 14 deletions.
37 changes: 29 additions & 8 deletions include/solvers/nonlinear_solver.h
Original file line number Diff line number Diff line change
Expand Up @@ -350,21 +350,42 @@ class NonlinearSolver : public ReferenceCountedObject<NonlinearSolver<T>>,
bool converged;

/**
* Whether we should reuse the linear preconditioner
* Set the solver configuration object.
*/
bool reuse_preconditioner;
void set_solver_configuration(SolverConfiguration & solver_configuration);

/**
* Number of linear iterations to retain the preconditioner
* Get the reuse_preconditioner flag
*/
unsigned int reuse_preconditioner_max_its;
virtual bool reuse_preconditioner() const;

/**
* Set the solver configuration object.
* Set the reuse preconditioner flag
*/
void set_solver_configuration(SolverConfiguration & solver_configuration);
virtual void set_reuse_preconditioner(bool reuse);

/**
* Get the reuse_preconditioner_max_its parameter
*/
virtual unsigned int reuse_preconditioner_max_its() const;

/**
* Set the reuse_preconditioner_max_its parameter
*/
virtual void set_reuse_preconditioner_max_its(unsigned int i);


protected:
/**
* Whether we should reuse the linear preconditioner
*/
bool _reuse_preconditioner;

/**
* Number of linear iterations to retain the preconditioner
*/
unsigned int _reuse_preconditioner_max_its;

/**
* A reference to the system we are solving.
*/
Expand Down Expand Up @@ -425,8 +446,8 @@ NonlinearSolver<T>::NonlinearSolver (sys_type & s) :
initial_linear_tolerance(0),
minimum_linear_tolerance(0),
converged(false),
reuse_preconditioner(false),
reuse_preconditioner_max_its(0),
_reuse_preconditioner(false),
_reuse_preconditioner_max_its(0),
_system(s),
_is_initialized (false),
_preconditioner (nullptr),
Expand Down
10 changes: 10 additions & 0 deletions include/solvers/petsc_nonlinear_solver.h
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,16 @@ class PetscNonlinearSolver : public NonlinearSolver<T>
*/
void setup_default_monitor();

/**
* Getter for preconditioner reuse
*/
virtual bool reuse_preconditioner() const;

/**
* Getter for the maximum iterations flag for preconditioner reuse
*/
virtual unsigned int reuse_preconditioner_max_its() const;

protected:

/**
Expand Down
23 changes: 23 additions & 0 deletions src/solvers/nonlinear_solver.C
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,29 @@ void NonlinearSolver<T>::set_solver_configuration(SolverConfiguration & solver_c
_solver_configuration = &solver_configuration;
}

template <typename T>
bool NonlinearSolver<T>::reuse_preconditioner() const
{
libmesh_not_implemented();
}

template <typename T>
void NonlinearSolver<T>::set_reuse_preconditioner(bool reuse)
{
_reuse_preconditioner = reuse;
}

template <typename T>
unsigned int NonlinearSolver<T>::reuse_preconditioner_max_its() const
{
libmesh_not_implemented();
}

template <typename T>
void NonlinearSolver<T>::set_reuse_preconditioner_max_its(unsigned int i)
{
_reuse_preconditioner_max_its = i;
}

//------------------------------------------------------------------
// Explicit instantiations
Expand Down
21 changes: 17 additions & 4 deletions src/solvers/petsc_nonlinear_solver.C
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ void PetscNonlinearSolver<T>::clear ()
// If we don't need the preconditioner next time
// retain the original behavior of clearing the data
// between solves.
if (!(this->reuse_preconditioner))
if (!(reuse_preconditioner()))
{
// SNESReset really ought to work but replacing destory() with
// SNESReset causes a very slight change in behavior that
Expand Down Expand Up @@ -871,7 +871,7 @@ PetscNonlinearSolver<T>::solve (SparseMatrix<T> & pre_in, // System Preconditi

// We don't want to do this twice because it resets
// SNESSetLagPreconditioner
if ((this->reuse_preconditioner) && (!_setup_reuse))
if ((reuse_preconditioner()) && (!_setup_reuse))
{
_setup_reuse = true;
ierr = SNESSetLagPreconditionerPersists(_snes, PETSC_TRUE);
Expand All @@ -882,13 +882,14 @@ PetscNonlinearSolver<T>::solve (SparseMatrix<T> & pre_in, // System Preconditi
LIBMESH_CHKERR(ierr);
// Add in our callback which will trigger recalculating
// the preconditioner when we hit reuse_preconditioner_max_its
unsigned int max_its = reuse_preconditioner_max_its();
ierr = SNESMonitorSet(_snes, &libmesh_petsc_recalculate_monitor,
(void*)
&(this->reuse_preconditioner_max_its),
&max_its,
NULL);
LIBMESH_CHKERR(ierr);
}
else if (!(this->reuse_preconditioner))
else if (!(reuse_preconditioner()))
// This covers the case where it was enabled but was then disabled
{
ierr = SNESSetLagPreconditionerPersists(_snes, PETSC_FALSE);
Expand Down Expand Up @@ -1126,6 +1127,18 @@ void PetscNonlinearSolver<T>::setup_default_monitor()
}
}

template <typename T>
bool PetscNonlinearSolver<T>::reuse_preconditioner() const
{
return this->_reuse_preconditioner;
}

template <typename T>
unsigned int PetscNonlinearSolver<T>::reuse_preconditioner_max_its() const
{
return this->_reuse_preconditioner_max_its;
}


//------------------------------------------------------------------
// Explicit instantiations
Expand Down
4 changes: 2 additions & 2 deletions src/systems/nonlinear_implicit_system.C
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ void NonlinearImplicitSystem::set_solver_parameters ()
nonlinear_solver->max_linear_iterations = maxlinearits;
nonlinear_solver->initial_linear_tolerance = linear_tol;
nonlinear_solver->minimum_linear_tolerance = linear_min_tol;
nonlinear_solver->reuse_preconditioner = reuse_preconditioner;
nonlinear_solver->reuse_preconditioner_max_its = reuse_preconditioner_max_its;
nonlinear_solver->set_reuse_preconditioner(reuse_preconditioner);
nonlinear_solver->set_reuse_preconditioner_max_its(reuse_preconditioner_max_its);

if (diff_solver.get())
{
Expand Down

0 comments on commit c911e49

Please sign in to comment.