| 1 |
/* |
| 2 |
* SPDX-FileCopyrightText: 2026 Oak Ridge National Laboratory and Contributors |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: Apache-2.0 |
| 5 |
*/ |
| 6 |
|
| 7 |
#include "restart.h" |
| 8 |
|
| 9 |
#include <iostream> |
| 10 |
|
| 11 |
static bool firstCkpt = true; |
| 12 |
|
| 13 |
void WriteCkpt(MPI_Comm comm, const int step, const Settings &settings, const GrayScott &sim, |
| 14 |
adios2::IO io) |
| 15 |
{ |
| 16 |
int rank, nproc; |
| 17 |
MPI_Comm_rank(comm, &rank); |
| 18 |
MPI_Comm_size(comm, &nproc); |
| 19 |
std::cout << "checkpoint at step " << step << " create file " << settings.checkpoint_output |
| 20 |
<< std::endl; |
| 21 |
adios2::Engine writer = io.Open(settings.checkpoint_output, adios2::Mode::Write); |
| 22 |
if (writer) |
| 23 |
{ |
| 24 |
adios2::Variable<double> var_u; |
| 25 |
adios2::Variable<double> var_v; |
| 26 |
adios2::Variable<int> var_step; |
| 27 |
|
| 28 |
if (firstCkpt) |
| 29 |
{ |
| 30 |
size_t X = sim.size_x + 2; |
| 31 |
size_t Y = sim.size_y + 2; |
| 32 |
size_t Z = sim.size_z + 2; |
| 33 |
size_t R = static_cast<size_t>(rank); |
| 34 |
size_t N = static_cast<size_t>(nproc); |
| 35 |
|
| 36 |
var_u = io.DefineVariable<double>("U", {N, X, Y, Z}, {R, 0, 0, 0}, {1, X, Y, Z}); |
| 37 |
var_v = io.DefineVariable<double>("V", {N, X, Y, Z}, {R, 0, 0, 0}, {1, X, Y, Z}); |
| 38 |
|
| 39 |
var_step = io.DefineVariable<int>("step"); |
| 40 |
firstCkpt = false; |
| 41 |
} |
| 42 |
else |
| 43 |
{ |
| 44 |
var_u = io.InquireVariable<double>("U"); |
| 45 |
var_v = io.InquireVariable<double>("V"); |
| 46 |
var_step = io.InquireVariable<int>("step"); |
| 47 |
} |
| 48 |
|
| 49 |
writer.Put<int>(var_step, &step); |
| 50 |
writer.Put<double>(var_u, sim.u_ghost().data()); |
| 51 |
writer.Put<double>(var_v, sim.v_ghost().data()); |
| 52 |
|
| 53 |
writer.Close(); |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
int ReadRestart(MPI_Comm comm, const Settings &settings, GrayScott &sim, adios2::IO io) |
| 58 |
{ |
| 59 |
int step = 0; |
| 60 |
int rank, nproc; |
| 61 |
MPI_Comm_rank(comm, &rank); |
| 62 |
MPI_Comm_size(comm, &nproc); |
| 63 |
if (!rank) |
| 64 |
{ |
| 65 |
std::cout << "restart from file " << settings.restart_input << std::endl; |
| 66 |
} |
| 67 |
adios2::Engine reader = io.Open(settings.restart_input, adios2::Mode::ReadRandomAccess); |
| 68 |
if (reader) |
| 69 |
{ |
| 70 |
adios2::Variable<int> var_step = io.InquireVariable<int>("step"); |
| 71 |
adios2::Variable<double> var_u = io.InquireVariable<double>("U"); |
| 72 |
adios2::Variable<double> var_v = io.InquireVariable<double>("V"); |
| 73 |
size_t X = sim.size_x + 2; |
| 74 |
size_t Y = sim.size_y + 2; |
| 75 |
size_t Z = sim.size_z + 2; |
| 76 |
size_t R = static_cast<size_t>(rank); |
| 77 |
std::vector<double> u, v; |
| 78 |
|
| 79 |
var_u.SetSelection({{R, 0, 0, 0}, {1, X, Y, Z}}); |
| 80 |
var_v.SetSelection({{R, 0, 0, 0}, {1, X, Y, Z}}); |
| 81 |
reader.Get<int>(var_step, step); |
| 82 |
reader.Get<double>(var_u, u); |
| 83 |
reader.Get<double>(var_v, v); |
| 84 |
reader.Close(); |
| 85 |
|
| 86 |
if (!rank) |
| 87 |
{ |
| 88 |
std::cout << "restart from step " << step << std::endl; |
| 89 |
} |
| 90 |
sim.restart(u, v); |
| 91 |
} |
| 92 |
else |
| 93 |
{ |
| 94 |
std::cout << " failed to open file " << std::endl; |
| 95 |
} |
| 96 |
return step; |
| 97 |
} |