Skip to content

Commit

Permalink
Add reload functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
davidscn committed Mar 8, 2022
1 parent 1bb4878 commit 01cae15
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 36 deletions.
44 changes: 22 additions & 22 deletions include/base/checkpoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,28 +62,28 @@ namespace Utilities
* @param name
* @param t
*/
// template <int dim, typename VectorType>
// void
// load_checkpoint(
// const dealii::parallel::distributed::Triangulation<dim> &triangulation,
// const dealii::DoFHandler<dim> & dof_handler,
// const std::vector<VectorType *> & vectors,
// const std::string & name,
// const double t)
// {
// parallel::distributed::SolutionTransfer<dim, VectorType> solution_transfer(
// dof_handler);

// solution_transfer.deserialize(vectors);

// for (auto &it : vectors)
// it.update_ghost_values();

// std::ifstream file(name + "-checkpoint.metadata", std::ios::binary);

// boost::archive::binary_iarchive ia(file);
// ia >> t;
// }
template <int dim, typename VectorType>
void
load_checkpoint(
const dealii::DoFHandler<dim> & dof_handler,
std::vector<VectorType *> & vectors,
const std::string & name,
double & t)
{
// triangulation.load(name + "-checkpoint.mesh");
dealii::parallel::distributed::SolutionTransfer<dim, VectorType>
solution_transfer(dof_handler);

solution_transfer.deserialize(vectors);

for (auto &it : vectors)
it->update_ghost_values();

std::ifstream file(name + "-checkpoint.metadata", std::ios::binary);

boost::archive::binary_iarchive ia(file);
ia >> t;
}
} // namespace Utilities

DEAL_II_NAMESPACE_CLOSE
9 changes: 7 additions & 2 deletions include/parameter/parameter_handling.h
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,9 @@ namespace Parameters
// Set the timestep size $ \varDelta t $ and the simulation end-time.
struct Time
{
double delta_t = 0.1;
double end_time = 1.;
double delta_t = 0.1;
double end_time = 1.;
bool read_checkpoint = false;

void
add_parameters(ParameterHandler &prm);
Expand All @@ -329,6 +330,10 @@ namespace Parameters
delta_t,
"Time step size",
Patterns::Double(0.));
prm.add_parameter("Resume checkpoint",
read_checkpoint,
"Resume from a checkpoint",
Patterns::Bool());
}
prm.leave_subsection();
}
Expand Down
83 changes: 71 additions & 12 deletions include/solid_mechanics/mf_elasticity.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ namespace FSI
void
write_checkpoint();


void
load_checkpoint();

// Set up an Additional data object
template <typename AdditionalData>
void
Expand Down Expand Up @@ -576,7 +580,10 @@ namespace FSI
mf_data_reference);
precice_adapter->initialize(total_displacement);

write_checkpoint();
if (parameters.read_checkpoint)
load_checkpoint();
else
write_checkpoint();

// At the beginning, we reset the solution update for this time step...
while (precice_adapter->is_coupling_ongoing())
Expand Down Expand Up @@ -619,6 +626,8 @@ namespace FSI
time.increment();
}
}
write_checkpoint();


// for post-processing, print average CG iterations over the whole run:
timer_out << std::endl
Expand All @@ -640,7 +649,11 @@ namespace FSI
{
Assert(testcase.get() != nullptr, ExcInternalError());
testcase->make_coarse_grid_and_bcs(triangulation);
triangulation.refine_global(parameters.n_global_refinement);

if (parameters.read_checkpoint)
triangulation.load("test-checkpoint.mesh");
else
triangulation.refine_global(parameters.n_global_refinement);

if (testcase->body_force.get() == nullptr)
testcase->body_force =
Expand Down Expand Up @@ -2036,16 +2049,62 @@ namespace FSI
void
Solid<dim, Number>::write_checkpoint()
{
// pcout << "checkpoint computation";

Utilities::create_checkpoint<dim, VectorType>(triangulation,
dof_handler,
{&total_displacement,
&velocity},
"test",
time.current()
);
// pcout << "resume interrupted computation";
pcout << "checkpoint computation"
<< "\n";
std::vector<const VectorType *> in_vectors({&total_displacement,
&velocity,
&acceleration,
&old_displacement,
&velocity_old,
&acceleration_old});

Utilities::create_checkpoint<dim, VectorType>(
triangulation, dof_handler, in_vectors, "test", time.current());
}



template <int dim, typename Number>
void
Solid<dim, Number>::load_checkpoint()
{
std::vector<VectorType *> in_vectors({&total_displacement,
&velocity,
&acceleration,
&old_displacement,
&velocity_old,
&acceleration_old});

auto partitioner = std::make_shared<Utilities::MPI::Partitioner>(
dof_handler.locally_owned_dofs(),
locally_relevant_dofs,
mpi_communicator);

// We need something temporarily because the reloading doesn't allow for
// ghosted vectors
std::vector<VectorType> tmp_vectors(in_vectors.size());
std::vector<VectorType *> tmp_vectors_ptr;

for (auto &vec : tmp_vectors)
{
vec.reinit(partitioner);
tmp_vectors_ptr.emplace_back(&vec);
}

double new_time = 0;
Utilities::load_checkpoint<dim, VectorType>(dof_handler,
tmp_vectors_ptr,
"test",
new_time);


for (unsigned int i = 0; i < tmp_vectors_ptr.size(); ++i)
{
in_vectors[i]->copy_locally_owned_data_from(*(tmp_vectors_ptr[i]));
in_vectors[i]->update_ghost_values();
}

pcout << "resume interrupted computation at t = " << new_time << "\n";
}


Expand Down

0 comments on commit 01cae15

Please sign in to comment.