Skip to content

Commit

Permalink
Address reviewer's comments
Browse files Browse the repository at this point in the history
  • Loading branch information
fdkong committed May 20, 2020
1 parent 408907d commit ddb7174
Show file tree
Hide file tree
Showing 14 changed files with 48 additions and 74 deletions.
6 changes: 5 additions & 1 deletion framework/doc/content/source/bcs/EigenArrayDirichletBC.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# EigenArrayDirichletBC

It is an array version of EigenDirichletBC provided for Eigenvalue solvers to handle Dirichlet boundary conditions for the right-hand side. It is different from regular DirichletBC. EigenArrayDirichletBC will always return 0 regardless of the residual. The corresponding rows of the matrix are zeroed out without adding ones to the diagonal.
It is an array version of EigenDirichletBC provided for Eigenvalue solvers to
handle Dirichlet boundary conditions for the right-hand side. It is different
from regular DirichletBC. EigenArrayDirichletBC will always return 0
regardless of the residual. The corresponding rows of the matrix are zeroed
out without adding ones to the diagonal.

!syntax description /BCs/EigenDirichletBC

Expand Down
2 changes: 1 addition & 1 deletion framework/include/base/Assembly.h
Original file line number Diff line number Diff line change
Expand Up @@ -768,7 +768,7 @@ class Assembly
TagID tag = 0);

/**
* Adds element matrix for ivar rows and jvar columns to the global Jacobian matrix for given
* Add element matrix for ivar rows and jvar columns to the global Jacobian matrix for given
* tags.
*/
void addJacobianBlockTags(SparseMatrix<Number> & jacobian,
Expand Down
2 changes: 1 addition & 1 deletion framework/include/systems/DumpObjectsNonlinearSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class DumpObjectsNonlinearSystem : public NonlinearSystemBase

virtual unsigned int getCurrentNonlinearIterationNumber() override { return 0; }
virtual void setupFiniteDifferencedPreconditioner() override {}
virtual void attachMoosePreconditioner(Preconditioner<Number> * /* preconditioner */) override {}
virtual void attachPreconditioner(Preconditioner<Number> * /* preconditioner */) override {}

protected:
NumericVector<Number> * _dummy;
Expand Down
21 changes: 8 additions & 13 deletions framework/include/systems/NonlinearEigenSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ class NonlinearEigenSystem : public NonlinearSystemBase
* is the real and the imaginary part of
* the eigenvalue, respectively.
*/
virtual const std::pair<Real, Real> getConvergedEigenvalue(dof_id_type n);
virtual std::pair<Real, Real> getConvergedEigenvalue(dof_id_type n) const;

/**
* Return the Nth converged eigenvalue and copies the respective eigen vector to the solution
Expand All @@ -132,7 +132,7 @@ class NonlinearEigenSystem : public NonlinearSystemBase
* is the real and the imaginary part of
* the eigenvalue, respectively.
*/
virtual const std::pair<Real, Real> getConvergedEigenpair(dof_id_type n);
virtual std::pair<Real, Real> getConvergedEigenpair(dof_id_type n) const;

/**
* Get the number of converged eigenvalues
Expand Down Expand Up @@ -172,21 +172,16 @@ class NonlinearEigenSystem : public NonlinearSystemBase
_precond_matrix_includes_eigen = precond_matrix_includes_eigen;
}

bool precondMatrixIncludesEigenKernels() { return _precond_matrix_includes_eigen; }
bool precondMatrixIncludesEigenKernels() const { return _precond_matrix_includes_eigen; }

TagID precondMatrixTag() { return _precond_tag; }
TagID precondMatrixTag() const { return _precond_tag; }

virtual void attachMoosePreconditioner(Preconditioner<Number> * preconditioner) override;
virtual void attachPreconditioner(Preconditioner<Number> * preconditioner) override;

Preconditioner<Number> * moosePreconditioner() { return _moose_preconditioner; }
Preconditioner<Number> * preconditioner() const { return _preconditioner; }

virtual void turnOffJacobian() override;

/**
* Turn off or on eigen nodal BCs
*/
void turnOffOnEigenNodalBCs(bool on);

protected:
TransientEigenSystem & _transient_sys;
EigenProblem & _eigen_problem;
Expand All @@ -200,8 +195,8 @@ class NonlinearEigenSystem : public NonlinearSystemBase
TagID _B_tag;
TagID _precond_tag;
bool _precond_matrix_includes_eigen;
// Tags for computing preconditioning matrix
Preconditioner<Number> * _moose_preconditioner;
// Libmesh preconditioner
Preconditioner<Number> * _preconditioner;
};

#else
Expand Down
2 changes: 1 addition & 1 deletion framework/include/systems/NonlinearSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class NonlinearSystem : public NonlinearSystemBase

void computeScaling() override;

virtual void attachMoosePreconditioner(Preconditioner<Number> * preconditioner) override;
virtual void attachPreconditioner(Preconditioner<Number> * preconditioner) override;

protected:
TransientNonlinearImplicitSystem & _transient_sys;
Expand Down
6 changes: 3 additions & 3 deletions framework/include/systems/NonlinearSystemBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -465,10 +465,10 @@ class NonlinearSystemBase : public SystemBase, public PerfGraphInterface
}

/**
* Attache a customized moose preconditioner that requires physics knowledge.
* For generic preconditioners, they should be implemented at the PETSC side.
* Attach a customized preconditioner that requires physics knowledge.
* Generic preconditioners should be implemented in PETSc, instead.
*/
virtual void attachMoosePreconditioner(Preconditioner<Number> * preconditioner) = 0;
virtual void attachPreconditioner(Preconditioner<Number> * preconditioner) = 0;

/**
* Setup damping stuff (called before we actually start)
Expand Down
5 changes: 2 additions & 3 deletions framework/src/base/Assembly.C
Original file line number Diff line number Diff line change
Expand Up @@ -3934,9 +3934,8 @@ Assembly::setCachedJacobianContributions()
{
// First zero the rows (including the diagonals) to prepare for
// setting the cached values.
// If we did not cache anything yet, we should skip
// if (_cached_jacobian_contribution_rows[tag].size())
_sys.getMatrix(tag).zero_rows(_cached_jacobian_contribution_rows[tag], 0.0);
if (_cached_jacobian_contribution_rows[tag].size())
_sys.getMatrix(tag).zero_rows(_cached_jacobian_contribution_rows[tag], 0.0);

// TODO: Use SparseMatrix::set_values() for efficiency
for (MooseIndex(_cached_jacobian_contribution_vals) i = 0;
Expand Down
9 changes: 5 additions & 4 deletions framework/src/executioners/Eigenvalue.C
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ Eigenvalue::validParams()
"If true, shell matrices will be used and meanwhile a preconditioning matrix"
"may be formed as well.");

params.addParam<bool>("precond_matrix_free",
false,
"Whether or not to use a matrix free fashion to form preconditioner. "
"If true, a shell matrix will be used for preconditioning.");
params.addParam<bool>(
"precond_matrix_free",
false,
"Whether or not to use a matrix free fashion for forming the preconditioning matrix. "
"If true, a shell matrix will be used for preconditioner.");

params.addParam<bool>("precond_matrix_includes_eigen",
false,
Expand Down
1 change: 1 addition & 0 deletions framework/src/preconditioners/MoosePreconditioner.C
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//* This file is part of the MOOSE framework
//* https://www.mooseframework.org
//*
//* All rights reserved, see COPYRIGHT for full restrictions
Expand Down
2 changes: 1 addition & 1 deletion framework/src/preconditioners/PhysicsBasedPreconditioner.C
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ PhysicsBasedPreconditioner::PhysicsBasedPreconditioner(const InputParameters & p
for (unsigned int var = 0; var < n_vars; var++)
addSystem(var, off_diag[var], _pre_type[var]);

_nl.attachMoosePreconditioner(this);
_nl.attachPreconditioner(this);

if (_fe_problem.solverParams()._type != Moose::ST_JFNK)
mooseError("PBP must be used with JFNK solve type");
Expand Down
7 changes: 2 additions & 5 deletions framework/src/problems/EigenProblem.C
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,7 @@ EigenProblem::computeJacobianBlocks(std::vector<JacobianBlock *> & blocks)
TIME_SECTION(_compute_jacobian_blocks_timer);

if (_displaced_problem)
{
_aux->compute(EXEC_PRE_DISPLACE);
_displaced_problem->updateMesh();
}

_aux->compute(EXEC_NONLINEAR);

Expand Down Expand Up @@ -332,7 +329,7 @@ void
EigenProblem::solve()
{
#if LIBMESH_HAVE_SLEPC
// Set proper operations
// Set necessary slepc callbacks
// We delay this call as much as possible because libmesh
// could rebuild matrices due to mesh changes or something else.
_nl_eigen->attachSLEPcCallbacks();
Expand Down Expand Up @@ -365,7 +362,7 @@ void
EigenProblem::init()
{
#if !PETSC_RELEASE_LESS_THAN(3, 13, 0)
// If matrix_free=true, this set Libmesh to use shell matrices
// If matrix_free=true, this tells Libmesh to use shell matrices
_nl_eigen->sys().use_shell_matrices(solverParams()._eigen_matrix_free);
// We need to tell libMesh if we are using a shell preconditioning matrix
_nl_eigen->sys().use_shell_precond_matrix(solverParams()._precond_matrix_free);
Expand Down
44 changes: 11 additions & 33 deletions framework/src/systems/NonlinearEigenSystem.C
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ assemble_matrix(EquationSystems & es, const std::string & system_name)
NonlinearEigenSystem & eigen_nl = p->getNonlinearEigenSystem();

// If this is a nonlinear eigenvalue problem,
// we do not need to assembly anything
// we do not need to assemble anything
if (p->isNonlinearEigenvalueSolver())
{
// If you want an efficient eigensolver,
// please use 3.13 or newer.
// please use PETSc 3.13 or newer.
// We need to do an unnecessary assembly,
// if you use PETSc that is older than 3.13.
#if PETSC_RELEASE_LESS_THAN(3, 13, 0)
Expand Down Expand Up @@ -138,7 +138,7 @@ NonlinearEigenSystem::initialSetup()
addEigenTagToMooseObjects(_integrated_bcs);
// If the precond matrix needs to include eigen kernels,
// we assign precond tag to all objects except nodal BCs.
// Mathematically speaking, we can not add eigen BCs in
// Mathematically speaking, we can not add eigen nodal BCs
// since they will overwrite non-eigen nodal BCs contributions.
if (_precond_matrix_includes_eigen)
{
Expand Down Expand Up @@ -202,28 +202,6 @@ NonlinearEigenSystem::addEigenTagToMooseObjects(MooseObjectTagWarehouse<T> & war
}
}

void
NonlinearEigenSystem::turnOffOnEigenNodalBCs(bool on)
{
for (THREAD_ID tid = 0; tid < _nodal_bcs.numThreads(); tid++)
{
auto & objects = _nodal_bcs.getObjects(tid);

for (auto & object : objects)
{
auto & mtags = object->getMatrixTags();
// Current object is an eigen nodal bc
if (mtags.find(_B_tag) != mtags.end())
{
// Currently, there does not exist an interfance for me,
// so I just drop const anyway.
auto & params = const_cast<InputParameters &>(object->parameters());
params.set<bool>("enable", true) = on;
}
}
}
}

void
NonlinearEigenSystem::solve()
{
Expand Down Expand Up @@ -278,7 +256,7 @@ NonlinearEigenSystem::attachSLEPcCallbacks()

Moose::SlepcSupport::attachCallbacksToMat(_eigen_problem, mat, false);

// Let libmesh do not close matrices before solve
// Tell libmesh not close matrices before solve
_transient_sys.eigen_solver->set_close_matrix_before_solve(false);
}

Expand Down Expand Up @@ -420,17 +398,17 @@ NonlinearEigenSystem::checkIntegrity()
}
}

const std::pair<Real, Real>
NonlinearEigenSystem::getConvergedEigenvalue(dof_id_type n)
std::pair<Real, Real>
NonlinearEigenSystem::getConvergedEigenvalue(dof_id_type n) const
{
unsigned int n_converged_eigenvalues = getNumConvergedEigenvalues();
if (n >= n_converged_eigenvalues)
mooseError(n, " not in [0, ", n_converged_eigenvalues, ")");
return _transient_sys.get_eigenvalue(n);
}

const std::pair<Real, Real>
NonlinearEigenSystem::getConvergedEigenpair(dof_id_type n)
std::pair<Real, Real>
NonlinearEigenSystem::getConvergedEigenpair(dof_id_type n) const
{
unsigned int n_converged_eigenvalues = getNumConvergedEigenvalues();
if (n >= n_converged_eigenvalues)
Expand All @@ -439,13 +417,13 @@ NonlinearEigenSystem::getConvergedEigenpair(dof_id_type n)
}

void
NonlinearEigenSystem::attachMoosePreconditioner(Preconditioner<Number> * preconditioner)
NonlinearEigenSystem::attachPreconditioner(Preconditioner<Number> * preconditioner)
{
_moose_preconditioner = preconditioner;
_preconditioner = preconditioner;

// If we have a customized preconditioner,
// We need to let PETSc know that
if (_moose_preconditioner)
if (_preconditioner)
{
Moose::SlepcSupport::registerPCToPETSc();
// Mark this, and then we can setup correct petsc options
Expand Down
2 changes: 1 addition & 1 deletion framework/src/systems/NonlinearSystem.C
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ NonlinearSystem::computeScaling()
}

void
NonlinearSystem::attachMoosePreconditioner(Preconditioner<Number> * preconditioner)
NonlinearSystem::attachPreconditioner(Preconditioner<Number> * preconditioner)
{
nonlinearSolver()->attach_preconditioner(preconditioner);
}
13 changes: 6 additions & 7 deletions framework/src/utils/SlepcSupport.C
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ mooseSlepcEigenFormJacobianA(SNES snes, Vec x, Mat jac, Mat pc, void * ctx)
NonlinearEigenSystem & eigen_nl = eigen_problem->getNonlinearEigenSystem();

// If both jacobian and preconditioning are shell matrices,
// and then assembly them and return
// and then assemble them and return
ierr = PetscObjectTypeCompare((PetscObject)jac, MATSHELL, &jissell);
CHKERRQ(ierr);
ierr = PetscObjectTypeCompare((PetscObject)jac, MATMFFD, &jismffd);
Expand All @@ -509,7 +509,7 @@ mooseSlepcEigenFormJacobianA(SNES snes, Vec x, Mat jac, Mat pc, void * ctx)
CHKERRQ(ierr);
if ((jissell || jismffd) && pissell)
{
// Just assembly matrices and return
// Just assemble matrices and return
ierr = MatAssemblyBegin(jac, MAT_FINAL_ASSEMBLY);
CHKERRQ(ierr);
ierr = MatAssemblyBegin(pc, MAT_FINAL_ASSEMBLY);
Expand Down Expand Up @@ -574,7 +574,7 @@ mooseSlepcEigenFormJacobianB(SNES snes, Vec x, Mat jac, Mat pc, void * ctx)
NonlinearEigenSystem & eigen_nl = eigen_problem->getNonlinearEigenSystem();

// If both jacobian and preconditioning are shell matrices,
// and then assembly them and return
// and then assemble them and return
ierr = PetscObjectTypeCompare((PetscObject)jac, MATSHELL, &jsell);
CHKERRQ(ierr);
ierr = PetscObjectTypeCompare((PetscObject)jac, MATMFFD, &jismffd);
Expand All @@ -583,7 +583,7 @@ mooseSlepcEigenFormJacobianB(SNES snes, Vec x, Mat jac, Mat pc, void * ctx)
CHKERRQ(ierr);
if ((jsell || jismffd) && psell)
{
// Just assembly matrices and return
// Just assemble matrices and return
ierr = MatAssemblyBegin(jac, MAT_FINAL_ASSEMBLY);
CHKERRQ(ierr);
ierr = MatAssemblyBegin(pc, MAT_FINAL_ASSEMBLY);
Expand Down Expand Up @@ -869,7 +869,7 @@ PCApply_MoosePC(PC pc, Vec x, Vec y)

EigenProblem * eigen_problem = static_cast<EigenProblem *>(ctx);
NonlinearEigenSystem & nl_eigen = eigen_problem->getNonlinearEigenSystem();
Preconditioner<Number> * preconditioner = nl_eigen.moosePreconditioner();
auto preconditioner = nl_eigen.preconditioner();

if (!preconditioner)
mooseError("There is no moose preconditioner in nonlinear eigen system \n");
Expand Down Expand Up @@ -905,14 +905,13 @@ PCSetUp_MoosePC(PC pc)
}
EigenProblem * eigen_problem = static_cast<EigenProblem *>(ctx);
NonlinearEigenSystem & nl_eigen = eigen_problem->getNonlinearEigenSystem();
Preconditioner<Number> * preconditioner = nl_eigen.moosePreconditioner();
Preconditioner<Number> * preconditioner = nl_eigen.preconditioner();

if (!preconditioner)
mooseError("There is no moose preconditioner in nonlinear eigen system \n");

if (!preconditioner->initialized())
preconditioner->init();
// libmesh_error_msg("Preconditioner not initialized! Make sure you call init() before solve!");

preconditioner->setup();

Expand Down

0 comments on commit ddb7174

Please sign in to comment.