Skip to content

Commit

Permalink
Address Roy's review:
Browse files Browse the repository at this point in the history
- centralize change in get_linear_solve_parameters
- enable the test
- use the number of iterations for the test
  • Loading branch information
GiudGiud committed Nov 28, 2024
1 parent f3746dc commit 862bcb5
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 46 deletions.
7 changes: 1 addition & 6 deletions src/systems/frequency_system.C
Original file line number Diff line number Diff line change
Expand Up @@ -343,12 +343,7 @@ 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 = 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 = 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 auto [maxits, tol] = this->get_linear_solve_parameters();

// start solver loop
for (unsigned int n=n_start; n<= n_stop; n++)
Expand Down
18 changes: 8 additions & 10 deletions src/systems/implicit_system.C
Original file line number Diff line number Diff line change
Expand Up @@ -1222,16 +1222,14 @@ LinearSolver<Number> * ImplicitSystem::get_linear_solver() const

std::pair<unsigned int, Real> ImplicitSystem::get_linear_solve_parameters() const
{
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");
return std::make_pair(
parameters.have_parameter<unsigned int>("linear solver maximum iterations")
? parameters.get<unsigned int>("linear solver maximum iterations")
: this->get_equation_systems().parameters.get<unsigned int>(
"linear solver maximum iterations"),
parameters.have_parameter<Real>("linear solver tolerance")
? parameters.get<Real>("linear solver tolerance")
: this->get_equation_systems().parameters.get<Real>("linear solver tolerance"));
}


Expand Down
13 changes: 1 addition & 12 deletions src/systems/linear_implicit_system.C
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,6 @@ void LinearImplicitSystem::solve ()
// Assemble the linear system
this->assemble ();

// Get a reference to the EquationSystems
const EquationSystems & es =
this->get_equation_systems();

// If the linear solver hasn't been initialized, we do so here.
if (libMesh::on_command_line("--solver-system-names"))
linear_solver->init((this->name()+"_").c_str());
Expand All @@ -131,14 +127,7 @@ void LinearImplicitSystem::solve ()
linear_solver->init_names(*this);

// Get the user-specified linear solver tolerance
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 = 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 auto [maxits, tol] = this->get_linear_solve_parameters();

if (_subset != nullptr)
linear_solver->restrict_solve_to(&_subset->dof_ids(),_subset_solve_mode);
Expand Down
8 changes: 1 addition & 7 deletions src/systems/nonlinear_implicit_system.C
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,7 @@ void NonlinearImplicitSystem::set_solver_parameters ()
double(es.parameters.get<Real>("nonlinear solver relative step tolerance"));

// Get the user-specified linear solver tolerances
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 = parameters.have_parameter<Real>("linear solver tolerance") ?
double(parameters.get<Real>("linear solver tolerance")) :
double(es.parameters.get<Real>("linear solver tolerance"));
const auto [maxlinearits, linear_tol] = this->get_linear_solve_parameters();

const double linear_min_tol = parameters.have_parameter<Real>("linear solver minimum tolerance") ?
double(parameters.get<Real>("linear solver minimum tolerance")) :
Expand Down
22 changes: 11 additions & 11 deletions tests/systems/systems_test.C
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,7 @@ public:
CPPUNIT_TEST( test3DProjectVectorFEHex20 );
CPPUNIT_TEST( test3DProjectVectorFEHex27 );
#ifdef LIBMESH_HAVE_SOLVER
CPPUNIT_TEST( testSetSystemParameterOverEquationSystem);
CPPUNIT_TEST( testAssemblyWithDgFemContext );
#endif
#endif // LIBMESH_DIM > 2
Expand Down Expand Up @@ -1289,8 +1290,9 @@ public:
// Create an equation systems object.
EquationSystems equation_systems (mesh);

// Set some parameters to the equation system that would cause a failed solve
// Set some parameters to the equation system that would cause a failed test
equation_systems.parameters.set<unsigned int>("linear solver maximum iterations") = 0;
equation_systems.parameters.set<unsigned int>("nonlinear solver absolute residual tolerance") = 1e8;

// Setup Linear Implicit system
LinearImplicitSystem & li_system =
Expand All @@ -1309,25 +1311,23 @@ public:
nli_system.add_variable ("v", libMesh::FIRST);
nli_system.add_variable ("w", libMesh::FIRST);
nli_system.attach_assemble_function (assemble_matrix_and_rhs);
nli_system.get_linear_solver()->set_solver_type(JACOBI);
nli_system.get_linear_solver()->set_preconditioner_type(IDENTITY_PRECOND);

// Set some parameters to the system that work for the solve
li_system.parameters.set<unsigned int>("linear solver maximum iterations") = 100;
nli_system.parameters.set<unsigned int>("linear solver maximum iterations") = 100;
li_system.parameters.set<unsigned int>("linear solver maximum iterations") = 5;
nli_system.parameters.set<unsigned int>("nonlinear solver absolute residual tolerance") = 1e-10;

// See the solve pass, indicating system parameters are used over equation system parameters
equation_systems.init ();
li_system.solve();
nli_system.solve();

// We set the solution to be 1 everywhere, so the final l1 norm of the
// solution is the product of the number of variables and number of nodes.
Real ref_l1_norm = static_cast<Real>(mesh.n_nodes() * li_system.n_vars());
// Check that the number of iterations from the systems got obeyed
CPPUNIT_ASSERT_EQUAL(li_system.n_linear_iterations(), 5u);

LIBMESH_ASSERT_FP_EQUAL(li_system.solution->l1_norm(), ref_l1_norm, TOLERANCE*TOLERANCE);
LIBMESH_ASSERT_FP_EQUAL(nli_system.solution->l1_norm(), ref_l1_norm, TOLERANCE*TOLERANCE);
}
// Check that the solution for the nonlinear system is converged
Real ref_l1_norm = static_cast<Real>(mesh.n_nodes() * li_system.n_vars());
LIBMESH_ASSERT_FP_EQUAL(nli_system.solution->l1_norm(), ref_l1_norm, TOLERANCE);
}

void testAssemblyWithDgFemContext()
{
Expand Down

0 comments on commit 862bcb5

Please sign in to comment.