Theseus
Compressible flow solver
Loading...
Searching...
No Matches
CheckpointConfig.hpp
Go to the documentation of this file.
1// Copyright (c) 2025-2026 Board of Trustees of the University of Illinois
2//
3// This file is part of Theseus.
4//
5// SPDX-License-Identifier: BSD-3-Clause
6#pragma once
7
8#include "json.hpp"
9
10#include <filesystem>
11#include <iomanip>
12#include <sstream>
13#include <stdexcept>
14#include <string>
15
16namespace Theseus
17{
18
30
32 {
33 private:
34 bool load_ = false;
35 bool save_ = false;
36 int cycle_ = 0;
37 double interval_ = 0.01;
38 std::filesystem::path directory_;
39
40 public:
41 static CheckpointConfig FromRuntime(const nlohmann::json &runtime,
42 const std::filesystem::path &output_directory)
43 {
44 CheckpointConfig result;
45 result.load_ = runtime.value("checkpoint_load", false);
46 result.save_ = runtime.value("checkpoint_save", false);
47 result.cycle_ = runtime.value("checkpoint_cycle", 0);
48 result.interval_ = runtime.value("checkpoint_dt", 0.01);
49
50 const auto folder = runtime.value("checkpoints_folder", std::string("Checkpoints"));
51 if (folder.empty())
52 {
53 throw std::invalid_argument("runTime.checkpoints_folder must not be empty");
54 }
55 result.directory_ = output_directory / folder;
56
57 if ((result.load_ || result.save_) && result.interval_ <= 0.0)
58 {
59 throw std::invalid_argument("runTime.checkpoint_dt must be greater than zero");
60 }
61 if (result.load_ && result.cycle_ <= 0)
62 {
63 throw std::invalid_argument(
64 "runTime.checkpoint_cycle must be greater than zero when checkpoint_load is true");
65 }
66 return result;
67 }
68
69 bool LoadEnabled() const { return load_; }
70 bool SaveEnabled() const { return save_; }
71 int Cycle() const { return cycle_; }
72 double Interval() const { return interval_; }
73 const std::filesystem::path &Directory() const { return directory_; }
74
75 std::filesystem::path CycleDirectory(int cycle) const
76 {
77 return directory_ / ("Cycle" + std::to_string(cycle));
78 }
79
80 std::filesystem::path MetadataFile(int cycle) const
81 {
82 return CycleDirectory(cycle) /
83 ("checkpoint_cycle_" + std::to_string(cycle) + ".json");
84 }
85
86 std::filesystem::path RankFile(int cycle, int rank) const
87 {
88 std::ostringstream filename;
89 filename << "checkpoint_cycle_" << cycle << "."
90 << std::setw(8) << std::setfill('0') << rank << ".chk";
91 return CycleDirectory(cycle) / filename.str();
92 }
93
94 static nlohmann::json Metadata(double time, int cycle,
95 const CheckpointCompatibility &compatibility)
96 {
97 return {{"format_version", 2},
98 {"state_format", "raw_vector_v1"},
99 {"state_representation", "conservative"},
100 {"geometry", compatibility.axisymmetric ? "axisymmetric" : "cartesian"},
101 {"real_bytes", compatibility.real_bytes},
102 {"time", time},
103 {"cycle", cycle},
104 {"mpi_ranks", compatibility.mpi_ranks},
105 {"order", compatibility.order},
106 {"dimension", compatibility.dimension},
107 {"num_equations", compatibility.num_equations},
108 {"global_elements", compatibility.global_elements},
109 {"global_dofs", compatibility.global_dofs}};
110 }
111
112 // Returns false for legacy metadata, which contains only time and cycle.
113 static bool ValidateMetadata(const nlohmann::json &metadata,
114 const CheckpointCompatibility &expected)
115 {
116 if (!metadata.contains("format_version"))
117 {
118 if (expected.axisymmetric)
119 {
120 throw std::invalid_argument(
121 "Legacy checkpoint cannot establish axisymmetric conservative-state semantics");
122 }
123 return false;
124 }
125 const int format_version = metadata.value("format_version", 0);
126 if (format_version != 1 && format_version != 2)
127 {
128 throw std::invalid_argument("Unsupported checkpoint metadata format version");
129 }
130 if (metadata.value("state_format", std::string()) != "raw_vector_v1")
131 {
132 throw std::invalid_argument("Unsupported checkpoint state format");
133 }
134 if (format_version == 1 && expected.axisymmetric)
135 {
136 throw std::invalid_argument(
137 "Version 1 checkpoint cannot establish whether an axisymmetric state stores U or rU");
138 }
139 if (format_version == 2)
140 {
141 if (metadata.value("state_representation", std::string()) != "conservative")
142 {
143 throw std::invalid_argument("Unsupported checkpoint state representation");
144 }
145 const std::string expected_geometry =
146 expected.axisymmetric ? "axisymmetric" : "cartesian";
147 if (metadata.value("geometry", std::string()) != expected_geometry)
148 {
149 throw std::invalid_argument(
150 "Checkpoint is incompatible with the current run: geometry differs");
151 }
152 }
153 if (metadata.value("real_bytes", 0) != expected.real_bytes)
154 {
155 throw std::invalid_argument("Checkpoint floating-point representation differs");
156 }
157
158 const auto require_equal = [&metadata](const char *name, long long value)
159 {
160 if (!metadata.contains(name) || metadata.at(name).get<long long>() != value)
161 {
162 throw std::invalid_argument(
163 "Checkpoint is incompatible with the current run: " + std::string(name) +
164 " differs");
165 }
166 };
167 require_equal("mpi_ranks", expected.mpi_ranks);
168 require_equal("order", expected.order);
169 require_equal("dimension", expected.dimension);
170 require_equal("num_equations", expected.num_equations);
171 require_equal("global_elements", expected.global_elements);
172 require_equal("global_dofs", expected.global_dofs);
173 return true;
174 }
175 };
176
177}
Definition CheckpointConfig.hpp:32
static nlohmann::json Metadata(double time, int cycle, const CheckpointCompatibility &compatibility)
Definition CheckpointConfig.hpp:94
int Cycle() const
Definition CheckpointConfig.hpp:71
static CheckpointConfig FromRuntime(const nlohmann::json &runtime, const std::filesystem::path &output_directory)
Definition CheckpointConfig.hpp:41
bool LoadEnabled() const
Definition CheckpointConfig.hpp:69
double Interval() const
Definition CheckpointConfig.hpp:72
std::filesystem::path RankFile(int cycle, int rank) const
Definition CheckpointConfig.hpp:86
const std::filesystem::path & Directory() const
Definition CheckpointConfig.hpp:73
std::filesystem::path CycleDirectory(int cycle) const
Definition CheckpointConfig.hpp:75
bool SaveEnabled() const
Definition CheckpointConfig.hpp:70
std::filesystem::path MetadataFile(int cycle) const
Definition CheckpointConfig.hpp:80
static bool ValidateMetadata(const nlohmann::json &metadata, const CheckpointCompatibility &expected)
Definition CheckpointConfig.hpp:113
Definition AxisymmetricGeometry.hpp:15
Definition CheckpointConfig.hpp:20
int dimension
Definition CheckpointConfig.hpp:23
int num_equations
Definition CheckpointConfig.hpp:24
bool axisymmetric
Definition CheckpointConfig.hpp:28
int real_bytes
Definition CheckpointConfig.hpp:25
int order
Definition CheckpointConfig.hpp:22
long long global_elements
Definition CheckpointConfig.hpp:26
int mpi_ranks
Definition CheckpointConfig.hpp:21
long long global_dofs
Definition CheckpointConfig.hpp:27