Skip to content

Commit

Permalink
Merge pull request #27785 from lindsayad/strict-error-code-27784
Browse files Browse the repository at this point in the history
Make MOOSE compliant `--with-strict-petscerrorcode`
  • Loading branch information
lindsayad authored Jun 5, 2024
2 parents 08dd9ec + f75f15a commit fb1a448
Show file tree
Hide file tree
Showing 32 changed files with 640 additions and 406 deletions.
7 changes: 5 additions & 2 deletions framework/src/base/MooseApp.C
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
#include "libmesh/string_to_enum.h"
#include "libmesh/checkpoint_io.h"
#include "libmesh/mesh_base.h"
#include "libmesh/petsc_solver_exception.h"

// System include for dynamic library methods
#ifdef LIBMESH_HAVE_DLOPEN
Expand Down Expand Up @@ -1143,7 +1144,8 @@ MooseApp::executeExecutioner()
// run the simulation
if (_use_executor && _executor)
{
Moose::PetscSupport::petscSetupOutput(_command_line.get());
auto ierr = Moose::PetscSupport::petscSetupOutput(_command_line.get());
LIBMESH_CHKERR(ierr);

_executor->init();
errorCheck();
Expand All @@ -1153,7 +1155,8 @@ MooseApp::executeExecutioner()
}
else if (_executioner)
{
Moose::PetscSupport::petscSetupOutput(_command_line.get());
auto ierr = Moose::PetscSupport::petscSetupOutput(_command_line.get());
LIBMESH_CHKERR(ierr);
_executioner->init();
errorCheck();
_executioner->execute();
Expand Down
5 changes: 4 additions & 1 deletion framework/src/base/MooseInit.C
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#include "Executioner.h"
#include "MooseRandom.h"

#include "libmesh/petsc_solver_exception.h"

// PETSc
#include "petscsys.h"

Expand All @@ -40,7 +42,8 @@ RegisterSigHandler()
MooseInit::MooseInit(int argc, char * argv[], MPI_Comm COMM_WORLD_IN)
: LibMeshInit(argc, argv, COMM_WORLD_IN)
{
PetscPopSignalHandler(); // get rid of PETSc error handler
auto ierr = PetscPopSignalHandler(); // get rid of PETSc error handler
LIBMESH_CHKERR(ierr);

// Set the number of OpenMP threads to the same as the number of threads libMesh is going to use
#ifdef LIBMESH_HAVE_OPENMP
Expand Down
12 changes: 10 additions & 2 deletions framework/src/executioners/Eigenvalue.C
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#include "SlepcSupport.h"
#include "UserObject.h"

#include "libmesh/petsc_solver_exception.h"

registerMooseObject("MooseApp", Eigenvalue);

InputParameters
Expand Down Expand Up @@ -192,10 +194,16 @@ Eigenvalue::prepareSolverOptions()
{
// Master app has the default data base
if (!_app.isUltimateMaster())
PetscOptionsPush(_eigen_problem.petscOptionsDatabase());
{
auto ierr = PetscOptionsPush(_eigen_problem.petscOptionsDatabase());
LIBMESH_CHKERR(ierr);
}
Moose::SlepcSupport::slepcSetOptions(_eigen_problem, _pars);
if (!_app.isUltimateMaster())
PetscOptionsPop();
{
auto ierr = PetscOptionsPop();
LIBMESH_CHKERR(ierr);
}
_eigen_problem.petscOptionsInserted() = true;
}
#endif
Expand Down
15 changes: 8 additions & 7 deletions framework/src/outputs/PetscOutput.C
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ PetscOutputInterface::petscNonlinearOutput(SNES, PetscInt its, PetscReal norm, v
}

// Done
return 0;
return (PetscErrorCode)0;
}

PetscErrorCode
Expand Down Expand Up @@ -117,7 +117,7 @@ PetscOutputInterface::petscLinearOutput(KSP, PetscInt its, PetscReal norm, void
}

// Done
return 0;
return (PetscErrorCode)0;
}

InputParameters
Expand Down Expand Up @@ -238,16 +238,17 @@ PetscOutput::solveSetup()
NonlinearSystemBase & nl = _problem_ptr->currentNonlinearSystem();
SNES snes = nl.getSNES();
KSP ksp;
SNESGetKSP(snes, &ksp);
auto ierr = SNESGetKSP(snes, &ksp);
CHKERRABORT(_communicator.get(), ierr);

// Set the PETSc monitor functions (register the nonlinear callback so that linear outputs
// get an updated nonlinear iteration number)
// Not every Output should register its own DM monitor! Just register one each of nonlinear
// and linear and dispatch all Outputs from there!
auto ierr1 = SNESMonitorSet(snes, petscNonlinearOutput, this, LIBMESH_PETSC_NULLPTR);
CHKERRABORT(_communicator.get(), ierr1);
auto ierr2 = KSPMonitorSet(ksp, petscLinearOutput, this, LIBMESH_PETSC_NULLPTR);
CHKERRABORT(_communicator.get(), ierr2);
ierr = SNESMonitorSet(snes, petscNonlinearOutput, this, LIBMESH_PETSC_NULLPTR);
CHKERRABORT(_communicator.get(), ierr);
ierr = KSPMonitorSet(ksp, petscLinearOutput, this, LIBMESH_PETSC_NULLPTR);
CHKERRABORT(_communicator.get(), ierr);
}

Real
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ VariableCondensationPreconditioner::init()
void
VariableCondensationPreconditioner::condenseSystem()
{
PetscErrorCode ierr = 0;
PetscErrorCode ierr = (PetscErrorCode)0;

// extract _M from the original matrix
_matrix->create_submatrix(*_M, _rows, _lm_dofs);
Expand Down Expand Up @@ -403,7 +403,7 @@ VariableCondensationPreconditioner::computeCondensedJacobian(PetscMatrix<Number>
const std::vector<dof_id_type> & grows,
PetscMatrix<Number> & block_mat)
{
PetscErrorCode ierr = 0;
PetscErrorCode ierr = (PetscErrorCode)0;

// obtain entries from the original matrix
PetscInt pc_ncols = 0, block_ncols = 0;
Expand Down Expand Up @@ -505,7 +505,7 @@ VariableCondensationPreconditioner::preallocateCondensedJacobian(
// condensed matrix
std::vector<dof_id_type> n_nz, n_oz;

PetscErrorCode ierr = 0;
PetscErrorCode ierr = (PetscErrorCode)0;

// Get number of nonzeros from original_mat and block_mat for each row
for (const auto & row_id : _rows)
Expand Down Expand Up @@ -652,7 +652,7 @@ VariableCondensationPreconditioner::getCondensedXY(const NumericVector<Number> &
NumericVector<Number> & x)
{
Mat mdinv;
PetscErrorCode ierr = 0;
PetscErrorCode ierr = (PetscErrorCode)0;
// calculate mdinv
ierr = MatMatMult(_M->mat(), _dinv, MAT_INITIAL_MATRIX, PETSC_DEFAULT, &mdinv);
LIBMESH_CHKERR(ierr);
Expand Down Expand Up @@ -755,7 +755,7 @@ VariableCondensationPreconditioner::findZeroDiagonals(SparseMatrix<Number> & mat
for (PetscInt i = 0; i < nrows; ++i)
indices.push_back(petsc_idx[i]);

ISRestoreIndices(zerodiags_all, &petsc_idx);
ierr = ISRestoreIndices(zerodiags_all, &petsc_idx);
LIBMESH_CHKERR(ierr);
ierr = ISDestroy(&zerodiags);
LIBMESH_CHKERR(ierr);
Expand Down
10 changes: 8 additions & 2 deletions framework/src/problems/EigenProblem.C
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,10 @@ EigenProblem::solve(const unsigned int nl_sys_num)
#if !PETSC_RELEASE_LESS_THAN(3, 12, 0)
// Master has the default database
if (!_app.isUltimateMaster())
PetscOptionsPush(_petsc_option_data_base);
{
auto ierr = PetscOptionsPush(_petsc_option_data_base);
LIBMESH_CHKERR(ierr);
}
#endif

setCurrentNonlinearSystem(nl_sys_num);
Expand Down Expand Up @@ -594,7 +597,10 @@ EigenProblem::solve(const unsigned int nl_sys_num)

#if !PETSC_RELEASE_LESS_THAN(3, 12, 0)
if (!_app.isUltimateMaster())
PetscOptionsPop();
{
auto ierr = PetscOptionsPop();
LIBMESH_CHKERR(ierr);
}
#endif

// sync solutions in displaced problem
Expand Down
34 changes: 27 additions & 7 deletions framework/src/problems/FEProblemBase.C
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,10 @@ FEProblemBase::FEProblemBase(const InputParameters & parameters)
#if !PETSC_RELEASE_LESS_THAN(3, 12, 0)
// Main app should hold the default database to handle system petsc options
if (!_app.isUltimateMaster())
PetscOptionsCreate(&_petsc_option_data_base);
{
auto ierr = PetscOptionsCreate(&_petsc_option_data_base);
LIBMESH_CHKERR(ierr);
}
#endif

if (!_solve)
Expand Down Expand Up @@ -683,7 +686,11 @@ FEProblemBase::~FEProblemBase()

#if !PETSC_RELEASE_LESS_THAN(3, 12, 0)
if (!_app.isUltimateMaster())
PetscOptionsDestroy(&_petsc_option_data_base);
{
auto ierr = PetscOptionsDestroy(&_petsc_option_data_base);
// Don't throw on destruction
CHKERRABORT(this->comm().get(), ierr);
}
#endif
}

Expand Down Expand Up @@ -5937,7 +5944,10 @@ FEProblemBase::solve(const unsigned int nl_sys_num)
// Now this database will be the default
// Each app should have only one database
if (!_app.isUltimateMaster())
PetscOptionsPush(_petsc_option_data_base);
{
auto ierr = PetscOptionsPush(_petsc_option_data_base);
LIBMESH_CHKERR(ierr);
}
// We did not add PETSc options to database yet
if (!_is_petsc_options_inserted)
{
Expand Down Expand Up @@ -5974,7 +5984,10 @@ FEProblemBase::solve(const unsigned int nl_sys_num)

#if !PETSC_RELEASE_LESS_THAN(3, 12, 0)
if (!_app.isUltimateMaster())
PetscOptionsPop();
{
auto ierr = PetscOptionsPop();
LIBMESH_CHKERR(ierr);
}
#endif
}

Expand Down Expand Up @@ -6087,13 +6100,17 @@ FEProblemBase::solveLinearSystem(const unsigned int linear_sys_num,
solver_params._line_search = Moose::LineSearchType::LS_NONE;

#if PETSC_RELEASE_LESS_THAN(3, 12, 0)
Moose::PetscSupport::petscSetOptions(
auto ierr = Moose::PetscSupport::petscSetOptions(
options, solver_params); // Make sure the PETSc options are setup for this app
LIBMESH_CHKERR(ierr);
#else
// Now this database will be the default
// Each app should have only one database
if (!_app.isUltimateMaster())
PetscOptionsPush(_petsc_option_data_base);
{
auto ierr = PetscOptionsPush(_petsc_option_data_base);
LIBMESH_CHKERR(ierr);
}
// We did not add PETSc options to database yet
if (!_is_petsc_options_inserted)
{
Expand All @@ -6107,7 +6124,10 @@ FEProblemBase::solveLinearSystem(const unsigned int linear_sys_num,

#if !PETSC_RELEASE_LESS_THAN(3, 12, 0)
if (!_app.isUltimateMaster())
PetscOptionsPop();
{
auto ierr = PetscOptionsPop();
LIBMESH_CHKERR(ierr);
}
#endif
}

Expand Down
19 changes: 13 additions & 6 deletions framework/src/restart/DataIO.C
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "libmesh/elem.h"
#include "libmesh/petsc_vector.h"
#include "libmesh/enum_solver_package.h"
#include "libmesh/petsc_solver_exception.h"

template <>
void
Expand Down Expand Up @@ -696,25 +697,31 @@ void
dataLoad(std::istream & stream, Vec & v, void * context)
{
PetscInt local_size;
VecGetLocalSize(v, &local_size);
auto ierr = VecGetLocalSize(v, &local_size);
LIBMESH_CHKERR(ierr);
PetscScalar * array;
VecGetArray(v, &array);
ierr = VecGetArray(v, &array);
LIBMESH_CHKERR(ierr);
for (PetscInt i = 0; i < local_size; i++)
dataLoad(stream, array[i], context);

VecRestoreArray(v, &array);
ierr = VecRestoreArray(v, &array);
LIBMESH_CHKERR(ierr);
}

template <>
void
dataStore(std::ostream & stream, Vec & v, void * context)
{
PetscInt local_size;
VecGetLocalSize(v, &local_size);
auto ierr = VecGetLocalSize(v, &local_size);
LIBMESH_CHKERR(ierr);
PetscScalar * array;
VecGetArray(v, &array);
ierr = VecGetArray(v, &array);
LIBMESH_CHKERR(ierr);
for (PetscInt i = 0; i < local_size; i++)
dataStore(stream, array[i], context);

VecRestoreArray(v, &array);
ierr = VecRestoreArray(v, &array);
LIBMESH_CHKERR(ierr);
}
13 changes: 9 additions & 4 deletions framework/src/systems/LinearSystem.C
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
#include "libmesh/petsc_matrix.h"
#include "libmesh/default_coupling.h"
#include "libmesh/diagonal_matrix.h"
#include "libmesh/petsc_solver_exception.h"

#include <ios>

Expand Down Expand Up @@ -196,11 +197,15 @@ LinearSystem::computeLinearSystemInternal(const std::set<TagID> & vector_tags,
// Necessary for speed
if (auto petsc_matrix = dynamic_cast<PetscMatrix<Number> *>(&matrix))
{
MatSetOption(petsc_matrix->mat(),
MAT_KEEP_NONZERO_PATTERN, // This is changed in 3.1
PETSC_TRUE);
auto ierr = MatSetOption(petsc_matrix->mat(),
MAT_KEEP_NONZERO_PATTERN, // This is changed in 3.1
PETSC_TRUE);
LIBMESH_CHKERR(ierr);
if (!_fe_problem.errorOnJacobianNonzeroReallocation())
MatSetOption(petsc_matrix->mat(), MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_FALSE);
{
ierr = MatSetOption(petsc_matrix->mat(), MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_FALSE);
LIBMESH_CHKERR(ierr);
}
}
}

Expand Down
13 changes: 10 additions & 3 deletions framework/src/systems/NonlinearEigenSystem.C
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "libmesh/sparse_matrix.h"
#include "libmesh/diagonal_matrix.h"
#include "libmesh/petsc_shell_matrix.h"
#include "libmesh/petsc_solver_exception.h"

#ifdef LIBMESH_HAVE_SLEPC

Expand Down Expand Up @@ -82,7 +83,11 @@ assemble_matrix(EquationSystems & es, const std::string & system_name)
eigen_nl.eigenMatrixTag());
#if LIBMESH_HAVE_SLEPC
if (p->negativeSignEigenKernel())
MatScale(static_cast<PetscMatrix<Number> &>(eigen_system.get_matrix_B()).mat(), -1.0);
{
auto ierr =
MatScale(static_cast<PetscMatrix<Number> &>(eigen_system.get_matrix_B()).mat(), -1.0);
LIBMESH_CHKERR(ierr);
}
#endif
return;
}
Expand Down Expand Up @@ -341,7 +346,8 @@ NonlinearEigenSystem::getSNES()
if (_eigen_problem.isNonlinearEigenvalueSolver())
{
SNES snes = nullptr;
Moose::SlepcSupport::mooseSlepcEPSGetSNES(eps, &snes);
auto ierr = Moose::SlepcSupport::mooseSlepcEPSGetSNES(eps, &snes);
LIBMESH_CHKERR(ierr);
return snes;
}
else
Expand Down Expand Up @@ -427,7 +433,8 @@ NonlinearEigenSystem::attachPreconditioner(Preconditioner<Number> * precondition
// We need to let PETSc know that
if (_preconditioner)
{
Moose::SlepcSupport::registerPCToPETSc();
auto ierr = Moose::SlepcSupport::registerPCToPETSc();
LIBMESH_CHKERR(ierr);
// Mark this, and then we can setup correct petsc options
_eigen_problem.solverParams()._customized_pc_for_eigen = true;
_eigen_problem.solverParams()._type = Moose::ST_JFNK;
Expand Down
Loading

0 comments on commit fb1a448

Please sign in to comment.