Skip to content

Commit

Permalink
Add a parameter object on the system, use it for the nonlinear implic…
Browse files Browse the repository at this point in the history
…it system

refs libMesh#4006
  • Loading branch information
GiudGiud committed Nov 13, 2024
1 parent cd33969 commit dd939f0
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 36 deletions.
17 changes: 4 additions & 13 deletions include/systems/system.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "libmesh/fem_function_base.h"
#include "libmesh/libmesh_common.h"
#include "libmesh/parallel_object.h"
#include "libmesh/parameters.h"
#include "libmesh/qoi_set.h"
#include "libmesh/reference_counted_object.h"
#include "libmesh/tensor_value.h" // For point_hessian
Expand Down Expand Up @@ -60,7 +61,6 @@ class MeshBase;
class Xdr;
class DofMap;
template <typename Output> class FunctionBase;
class Parameters;
class ParameterVector;
class Point;
class SensitivityData;
Expand Down Expand Up @@ -510,8 +510,6 @@ class System : public ReferenceCountedObject<System>,
* user-provided cloneable functors.
* A gradient \p g is only required/used for projecting onto finite
* element spaces with continuous derivatives.
* If non-default \p Parameters are to be used, they can be provided
* in the \p parameters argument.
*/
void project_solution (FunctionBase<Number> * f,
FunctionBase<Gradient> * g = nullptr) const;
Expand All @@ -522,8 +520,6 @@ class System : public ReferenceCountedObject<System>,
* user-provided cloneable functors.
* A gradient \p g is only required/used for projecting onto finite
* element spaces with continuous derivatives.
* If non-default \p Parameters are to be used, they can be provided
* in the \p parameters argument.
*/
void project_solution (FEMFunctionBase<Number> * f,
FEMFunctionBase<Gradient> * g = nullptr) const;
Expand Down Expand Up @@ -554,8 +550,6 @@ class System : public ReferenceCountedObject<System>,
* user-provided cloneable functors.
* A gradient \p g is only required/used for projecting onto finite
* element spaces with continuous derivatives.
* If non-default \p Parameters are to be used, they can be provided
* in the \p parameters argument.
*
* Constrain the new vector using the requested adjoint rather than
* primal constraints if is_adjoint is non-negative.
Expand All @@ -572,8 +566,6 @@ class System : public ReferenceCountedObject<System>,
* user-provided cloneable functors.
* A gradient \p g is only required/used for projecting onto finite
* element spaces with continuous derivatives.
* If non-default \p Parameters are to be used, they can be provided
* in the \p parameters argument.
*
* Constrain the new vector using the requested adjoint rather than
* primal constraints if is_adjoint is non-negative.
Expand Down Expand Up @@ -611,8 +603,6 @@ class System : public ReferenceCountedObject<System>,
* user-provided cloneable functors.
* A gradient \p g is only required/used for projecting onto finite
* element spaces with continuous derivatives.
* If non-default \p Parameters are to be used, they can be provided
* in the \p parameters argument.
*/
void boundary_project_solution (const std::set<boundary_id_type> & b,
const std::vector<unsigned int> & variables,
Expand Down Expand Up @@ -648,8 +638,6 @@ class System : public ReferenceCountedObject<System>,
* user-provided cloneable functors.
* A gradient \p g is only required/used for projecting onto finite
* element spaces with continuous derivatives.
* If non-default \p Parameters are to be used, they can be provided
* in the \p parameters argument.
*
* Constrain the new vector using the requested adjoint rather than
* primal constraints if is_adjoint is non-negative.
Expand Down Expand Up @@ -1531,6 +1519,9 @@ class System : public ReferenceCountedObject<System>,
*/
virtual void prolong_vectors ();

/// Parameters for the system. If a parameter is not provided, it should be retrieved from the EquationSystems
Parameters parameters;

/**
* Flag which tells the system to whether or not to
* call the user assembly function during each call to solve().
Expand Down
12 changes: 8 additions & 4 deletions src/systems/eigen_system.C
Original file line number Diff line number Diff line change
Expand Up @@ -222,16 +222,20 @@ EigenSystem::solve_helper(SparseMatrix<Number> * const A,
// Get the tolerance for the solver and the maximum
// number of iterations. Here, we simply adopt the linear solver
// specific parameters.
const double tol =
const double tol = parameters.have_parameter<Real>("linear solver tolerance") ?
double(parameters.get<Real>("linear solver tolerance")) :
double(es.parameters.get<Real>("linear solver tolerance"));

const unsigned int maxits =
const unsigned int maxits = parameters.have_parameter<unsigned int>("linear solver maximum iterations") ?
parameters.get<unsigned int>("linear solver maximum iterations") :
es.parameters.get<unsigned int>("linear solver maximum iterations");

const unsigned int nev =
const unsigned int nev = parameters.have_parameter<unsigned int>("eigenpairs") ?
parameters.get<unsigned int>("eigenpairs") :
es.parameters.get<unsigned int>("eigenpairs");

const unsigned int ncv =
const unsigned int ncv = parameters.have_parameter<unsigned int>("basis vectors") ?
parameters.get<unsigned int>("basis vectors") :
es.parameters.get<unsigned int>("basis vectors");

std::pair<unsigned int, unsigned int> solve_data;
Expand Down
14 changes: 10 additions & 4 deletions src/systems/frequency_system.C
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,18 @@ void FrequencySystem::init_data ()
// make sure we have frequencies to solve for
if (!_finished_set_frequencies)
{
// not supported for now
if (parameters.have_parameter<unsigned int> ("n_frequencies"))
libmesh_error_msg("ERROR: Not supported");

// when this system was read from file, check
// if this has a "n_frequencies" parameter,
// and initialize us with these.
if (es.parameters.have_parameter<unsigned int> ("n_frequencies"))
{
#ifndef NDEBUG
const unsigned int n_freq =
es.parameters.get<unsigned int>("n_frequencies");
es.parameters:<unsigned int>("n_frequencies");

libmesh_assert_greater (n_freq, 0);
#endif
Expand Down Expand Up @@ -304,7 +308,7 @@ void FrequencySystem::set_frequencies (const std::vector<Number> & frequencies,
unsigned int FrequencySystem::n_frequencies () const
{
libmesh_assert(_finished_set_frequencies);
return this->get_equation_systems().parameters.get<unsigned int>("n_frequencies");
return this->get_equation_systems().parameters:<unsigned int>("n_frequencies");
}


Expand Down Expand Up @@ -339,9 +343,11 @@ void FrequencySystem::solve (const unsigned int n_start,
// Get the user-specified linear solver tolerance,
// the user-specified maximum # of linear solver iterations,
// the user-specified wave speed
const Real tol =
const Real tol = parameters.have_parameter<Real>("linear solver tolerance") ?
double(parameters.get<Real>("linear solver tolerance")) :
es.parameters.get<Real>("linear solver tolerance");
const unsigned int maxits =
const unsigned int maxits = parameters.have_parameter<unsigned int>("linear solver maximum iterations") ?
parameters.get<unsigned int>("linear solver maximum iterations") :
es.parameters.get<unsigned int>("linear solver maximum iterations");

// start solver loop
Expand Down
12 changes: 10 additions & 2 deletions src/systems/implicit_system.C
Original file line number Diff line number Diff line change
Expand Up @@ -1222,8 +1222,16 @@ LinearSolver<Number> * ImplicitSystem::get_linear_solver() const

std::pair<unsigned int, Real> ImplicitSystem::get_linear_solve_parameters() const
{
return std::make_pair(this->get_equation_systems().parameters.get<unsigned int>("linear solver maximum iterations"),
this->get_equation_systems().parameters.get<Real>("linear solver tolerance"));
if (parameters.have_parameter<unsigned int>("linear solver maximum iterations") &&
parameters.have_parameter<Real>("linear solver tolerance"))
return std::make_pair(parameters.get<unsigned int>("linear solver maximum iterations"),
parameters.get<Real>("linear solver tolerance"));
else if (!parameters.have_parameter<unsigned int>("linear solver maximum iterations") &&
!parameters.have_parameter<Real>("linear solver tolerance"))
return std::make_pair(this->get_equation_systems().parameters.get<unsigned int>("linear solver maximum iterations"),
this->get_equation_systems().parameters.get<Real>("linear solver tolerance"));
else
libmesh_error_msg("ERROR: Insufficient linear solver parameters");
}


Expand Down
6 changes: 4 additions & 2 deletions src/systems/linear_implicit_system.C
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,13 @@ void LinearImplicitSystem::solve ()
linear_solver->init_names(*this);

// Get the user-specified linear solver tolerance
const double tol =
const double tol = parameters.have_parameter<Real>("linear solver tolerance") ?
double(parameters.get<Real>("linear solver tolerance")) :
double(es.parameters.get<Real>("linear solver tolerance"));

// Get the user-specified maximum # of linear solver iterations
const unsigned int maxits =
const unsigned int maxits = parameters.have_parameter<unsigned int>("linear solver maximum iterations") ?
parameters.get<unsigned int>("linear solver maximum iterations") :
es.parameters.get<unsigned int>("linear solver maximum iterations");

if (_subset != nullptr)
Expand Down
35 changes: 24 additions & 11 deletions src/systems/nonlinear_implicit_system.C
Original file line number Diff line number Diff line change
Expand Up @@ -118,40 +118,53 @@ void NonlinearImplicitSystem::set_solver_parameters ()
this->get_equation_systems();

// Get the user-specified nonlinear solver tolerances
const unsigned int maxits =
const unsigned int maxits = parameters.have_parameter<unsigned int>("nonlinear solver maximum iterations") ?
parameters.get<unsigned int>("nonlinear solver maximum iterations") :
es.parameters.get<unsigned int>("nonlinear solver maximum iterations");

const unsigned int maxfuncs =
const unsigned int maxfuncs = parameters.have_parameter<unsigned int>("nonlinear solver maximum function evaluations") ?
parameters.get<unsigned int>("nonlinear solver maximum function evaluations") :
es.parameters.get<unsigned int>("nonlinear solver maximum function evaluations");

const double abs_resid_tol =
const double abs_resid_tol = parameters.have_parameter<Real>("nonlinear solver absolute residual tolerance") ?
double(parameters.get<Real>("nonlinear solver absolute residual tolerance")) :
double(es.parameters.get<Real>("nonlinear solver absolute residual tolerance"));

const double rel_resid_tol =
const double rel_resid_tol = parameters.have_parameter<Real>("nonlinear solver relative residual tolerance") ?
double(parameters.get<Real>("nonlinear solver relative residual tolerance")) :
double(es.parameters.get<Real>("nonlinear solver relative residual tolerance"));

const double div_tol =
const double div_tol = parameters.have_parameter<Real>("nonlinear solver divergence tolerance") ?
double(parameters.get<Real>("nonlinear solver divergence tolerance")) :
double(es.parameters.get<Real>("nonlinear solver divergence tolerance"));

const double abs_step_tol =
const double abs_step_tol = parameters.have_parameter<Real>("nonlinear solver absolute step tolerance") ?
double(parameters.get<Real>("nonlinear solver absolute step tolerance")) :
double(es.parameters.get<Real>("nonlinear solver absolute step tolerance"));

const double rel_step_tol =
const double rel_step_tol = parameters.have_parameter<Real>("nonlinear solver relative step tolerance")?
double(parameters.get<Real>("nonlinear solver relative step tolerance")) :
double(es.parameters.get<Real>("nonlinear solver relative step tolerance"));

// Get the user-specified linear solver tolerances
const unsigned int maxlinearits =
const unsigned int maxlinearits = parameters.have_parameter<unsigned int>("linear solver maximum iterations") ?
parameters.get<unsigned int>("linear solver maximum iterations") :
es.parameters.get<unsigned int>("linear solver maximum iterations");

const double linear_tol =
const double linear_tol = parameters.have_parameter<Real>("linear solver tolerance") ?
double(parameters.get<Real>("linear solver tolerance")) :
double(es.parameters.get<Real>("linear solver tolerance"));

const double linear_min_tol =
const double linear_min_tol = parameters.have_parameter<Real>("linear solver minimum tolerance") ?
double(parameters.get<Real>("linear solver minimum tolerance")) :
double(es.parameters.get<Real>("linear solver minimum tolerance"));

const bool reuse_preconditioner =
const bool reuse_preconditioner = parameters.have_parameter<unsigned int>("reuse preconditioner") ?
parameters.get<unsigned int>("reuse preconditioner") :
es.parameters.get<bool>("reuse preconditioner");
const unsigned int reuse_preconditioner_max_linear_its =
parameters.have_parameter<unsigned int>("reuse preconditioner maximum linear iterations") ?
parameters.get<unsigned int>("reuse preconditioner maximum linear iterations") :
es.parameters.get<unsigned int>("reuse preconditioner maximum linear iterations");

// Set all the parameters on the NonlinearSolver
Expand Down

0 comments on commit dd939f0

Please sign in to comment.