Skip to content

Commit

Permalink
Make subvectors restartable so recover works
Browse files Browse the repository at this point in the history
  • Loading branch information
lindsayad committed Oct 25, 2024
1 parent 8722f8b commit 9aca6c7
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 7 deletions.
3 changes: 3 additions & 0 deletions framework/include/outputs/JsonIO.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ template <typename T>
class DenseVector;
template <typename T>
class DenseMatrix;
template <typename T>
class NumericVector;
}

// Overloads for to_json, which _must_ be overloaded in the namespace
Expand All @@ -38,6 +40,7 @@ namespace libMesh
void to_json(nlohmann::json & json, const Point & p);
void to_json(nlohmann::json & json, const DenseVector<Real> & vector);
void to_json(nlohmann::json & json, const DenseMatrix<Real> & matrix);
void to_json(nlohmann::json & json, const std::unique_ptr<NumericVector<Number>> & vector);
}

namespace nlohmann
Expand Down
1 change: 1 addition & 0 deletions framework/include/restart/DataIO.h
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@ template <>
void dataStore(std::ostream & stream, RealEigenMatrix & v, void * context);
template <>
void dataStore(std::ostream & stream, libMesh::Parameters & p, void * context);

template <>
/**
* Stores an owned numeric vector.
Expand Down
10 changes: 5 additions & 5 deletions framework/include/timeintegrators/TimeIntegrator.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,8 @@ class TimeIntegrator : public MooseObject, public Restartable
/// solution vectors
const NumericVector<Number> * const & _solution;
const NumericVector<Number> & _solution_old;
std::unique_ptr<NumericVector<Number>> _solution_sub;
std::unique_ptr<NumericVector<Number>> _solution_old_sub;
std::unique_ptr<NumericVector<Number>> & _solution_sub;
std::unique_ptr<NumericVector<Number>> & _solution_old_sub;
//
int & _t_step;
//
Expand All @@ -227,12 +227,12 @@ class TimeIntegrator : public MooseObject, public Restartable
const TagID _u_dotdot_factor_tag;

/// Whether the user has requested that the time integrator be applied to a subset of variables
bool _var_restriction;
bool & _var_restriction;

/// The local degree of freedom indices this time integrator is being applied to. If this
/// container is empty then the time integrator is applied to all indices
std::vector<dof_id_type> _local_indices;
std::vector<dof_id_type> & _local_indices;

/// The variables that this time integrator integrates
std::unordered_set<unsigned int> _vars;
std::unordered_set<unsigned int> & _vars;
};
11 changes: 11 additions & 0 deletions framework/src/outputs/JsonIO.C
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,15 @@ to_json(nlohmann::json & json, const DenseMatrix<Real> & matrix)
values[i][j] = matrix(i, j);
nlohmann::to_json(json, values);
}

void
to_json(nlohmann::json & json, const std::unique_ptr<NumericVector<Number>> & vector)
{
mooseAssert(vector, "vector must be non-null");
std::vector<Number> local_values;
local_values.reserve(vector->local_size());
for (const auto i : make_range(vector->first_local_index(), vector->last_local_index()))
local_values.push_back((*vector)(i));
nlohmann::to_json(json, local_values);
}
}
12 changes: 11 additions & 1 deletion framework/src/restart/DataIO.C
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,11 @@ dataStore(std::ostream & stream,
std::unique_ptr<libMesh::NumericVector<Number>> & v,
void * context)
{
mooseAssert(v, "Null vector");
bool have_vector = v.get();
dataStore(stream, have_vector, context);
if (!have_vector)
return;

mooseAssert(context, "Needs a context of the communicator");
const auto & comm = *static_cast<const libMesh::Parallel::Communicator *>(context);
mooseAssert(&comm == &v->comm(), "Inconsistent communicator");
Expand Down Expand Up @@ -669,6 +673,12 @@ template <>
void
dataLoad(std::istream & stream, std::unique_ptr<libMesh::NumericVector<Number>> & v, void * context)
{
bool have_vector;
dataLoad(stream, have_vector, context);

if (!have_vector)
return;

mooseAssert(context, "Needs a context of the communicator");
const auto & comm = *static_cast<const libMesh::Parallel::Communicator *>(context);
if (v)
Expand Down
9 changes: 8 additions & 1 deletion framework/src/timeintegrators/TimeIntegrator.C
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ TimeIntegrator::TimeIntegrator(const InputParameters & parameters)
_du_dot_du(_sys.duDotDu()),
_solution(_sys.currentSolution()),
_solution_old(_sys.solutionState(1)),
_solution_sub(declareRestartableDataWithContext<std::unique_ptr<NumericVector<Number>>>(
"solution_sub", &const_cast<libMesh::Parallel::Communicator &>(this->comm()))),
_solution_old_sub(declareRestartableDataWithContext<std::unique_ptr<NumericVector<Number>>>(
"solution_old_sub", &const_cast<libMesh::Parallel::Communicator &>(this->comm()))),
_t_step(_fe_problem.timeStep()),
_dt(_fe_problem.dt()),
_dt_old(_fe_problem.dtOld()),
Expand All @@ -46,7 +50,10 @@ TimeIntegrator::TimeIntegrator(const InputParameters & parameters)
_is_lumped(false),
_u_dot_factor_tag(_fe_problem.addVectorTag("u_dot_factor", Moose::VECTOR_TAG_SOLUTION)),
_u_dotdot_factor_tag(_fe_problem.addVectorTag("u_dotdot_factor", Moose::VECTOR_TAG_SOLUTION)),
_var_restriction(!getParam<std::vector<VariableName>>("variables").empty())
_var_restriction(declareRestartableData<bool>(
"var_restriction", !getParam<std::vector<VariableName>>("variables").empty())),
_local_indices(declareRestartableData<std::vector<dof_id_type>>("local_indices")),
_vars(declareRestartableData<std::unordered_set<unsigned int>>("vars"))
{
_fe_problem.setUDotRequested(true);
}
Expand Down

0 comments on commit 9aca6c7

Please sign in to comment.