Skip to content

Commit ac561f7

Browse files
authored
Allow parameter modification from an input file for restarts (#1004)
* Allow parameter modification from an input file for restarts * Fix typos * Test override from input for restart * Fix path to restart override
1 parent d3f5b6a commit ac561f7

File tree

7 files changed

+47
-20
lines changed

7 files changed

+47
-20
lines changed

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
### Changed (changing behavior/API/variables/...)
99
- [[PR 1024]](https://github.com/parthenon-hpc-lab/parthenon/pull/1024) Add .outN. to history output filenames
10-
10+
- [[PR 1004]](https://github.com/parthenon-hpc-lab/parthenon/pull/1004) Allow parameter modification from an input file for restarts
1111

1212
### Fixed (not changing behavior/API/variables/...)
1313
- [[PR 1024]](https://github.com/parthenon-hpc-lab/parthenon/pull/1024) Add features to history output

doc/sphinx/src/README.rst

+7-2
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ ones requires ``parthenon/mesh/pack_size=-1`` during initialization, i.e.,
205205
all blocks on a rank need to be in a single pack. This allows to use MPI
206206
reductions inside the function, for example, to globally normalize quantities.
207207
The ``parthenon/mesh/pack_size=-1`` exists only during problem
208-
inititalization, i.e., simulations can be restarted with an arbitrary
208+
initialization, i.e., simulations can be restarted with an arbitrary
209209
``pack_size``. For an example of the ``Mesh`` version, see the `Poisson
210210
example <https://github.com/parthenon-hpc-lab/parthenon/blob/develop/example/poisson/parthenon_app_inputs.cpp>`__.
211211

@@ -307,7 +307,12 @@ command. For example, the ``refine_tol`` parameter in the
307307
appending ``parthenon/refinement0/refine_tol=my_new_value`` to the
308308
launch command (e.g.,
309309
``srun ./myapp -i my_input.file parthenon/refinement0/refine_tol=my_new_value``).
310-
This similarly applies to simulations that are restarted.
310+
This similarly applies to simulations that are restarted, where modifications
311+
from the command line and through an input file are possible, e.g.,
312+
``srun ./myapp -r out0.rhdf -i modified_input.in parthenon/refinement0/refine_tol=my_new_value``.
313+
In the latter case, the input stored in the restart file will be read first,
314+
then updated from the content in the input file, and finally modified from the
315+
parameters provided on the command line.
311316

312317
Global reductions
313318
~~~~~~~~~~~~~~~~~

doc/sphinx/src/outputs.rst

+10-9
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
Outputs
44
=======
55

6-
Outputs from Parthenon are controled via ``<parthenon/output*>`` blocks,
6+
Outputs from Parthenon are controlled via ``<parthenon/output*>`` blocks,
77
where ``*`` should be replaced by a unique integer for each block.
88

9-
To disable an output block without removing it from the intput file set
9+
To disable an output block without removing it from the input file set
1010
the block's ``dt < 0.0``.
1111

1212
In addition to time base outputs, two additional options to trigger
@@ -114,7 +114,7 @@ Restart Files
114114

115115
Parthenon allows users to output restart files for restarting a
116116
simulation. The restart file captures the input file, so no input file
117-
is required to be specified. Parameters for the input can be overriden
117+
is required to be specified. Parameters for the input can be overridden
118118
in the usual way from the command line. At a future date we will allow
119119
for users the ability to extensively edit the parameters stored within
120120
the restart file.
@@ -134,8 +134,9 @@ This will produce an hdf5 (``.rhdf``) output file every 1 units of
134134
simulation time that can be used for restarting the simulation.
135135

136136
To use this restart file, simply specify the restart file with a
137-
``-r <restart.rhdf>`` at the command line. It is an error to specify an
138-
input file with the ``-i`` flag when using the restart option.
137+
``-r <restart.rhdf>`` at the command line. If both ``-r <restart.rhdf>``
138+
and ``-i <input.in>`` are specified, the simulation will be restarted from
139+
the restart file with input parameters updated (or added) from the input file.
139140

140141
For physics developers: The fields to be output are automatically
141142
selected as all the variables that have either the ``Independent`` or
@@ -179,7 +180,7 @@ Currently supported are
179180
- weighting by volume and/or variable
180181

181182
The output format follows ``numpy`` convention, so that plotting data
182-
with Python based machinery should be straightfoward (see example below).
183+
with Python based machinery should be straightforward (see example below).
183184
In other words, 2D histograms use C-ordering corresponding to ``[x,y]``
184185
indexing with ``y`` being the fast index.
185186
In general, histograms are calculated using inclusive left bin edges and
@@ -222,7 +223,7 @@ with the following parameters
222223

223224
- ``hist_names=STRING, STRING, STRING, ...`` (comma separated names)
224225
The names of the histograms in this block.
225-
Will be used as preifx in the block as well as in the output file.
226+
Will be used as prefix in the block as well as in the output file.
226227
All histograms will be written to the same output file with the "group" in the
227228
output corresponding to the histogram name.
228229
- ``NAME_ndim=INT`` (either ``1`` or ``2``)
@@ -338,7 +339,7 @@ Support for Ascent is disabled by default and must be enabled via ``PARTHENON_EN
338339
In the input file, include a ``<parthenon/output*>`` block and specify ``file_type = ascent``.
339340
A ``dt`` parameter controls the frequency of outputs for simulations involving evolution.
340341
*Note* that in principle Ascent can control its own output cadence (including
341-
automated tiggers).
342+
automated triggers).
342343
If you want to call Ascent on every cycle, set ``dt`` to a value smaller than the actual simulation ``dt``.
343344
The mandatory ``actions_file`` parameter points to a separate file that defines
344345
Ascent actions in ``.yaml`` or ``.json`` format, see
@@ -352,7 +353,7 @@ If component label(s) are provided, they will be added as a suffix, e.g,.
352353
Otherwise, an integer index is added for vectors/tensors with more than one component, i.e.,
353354
vectors/tensors with a single component and without component labels will not contain a suffix.
354355
The definition of component labels for variables is typically done by downstream codes
355-
so that the downstream documention should be consulted for more specific information.
356+
so that the downstream documentation should be consulted for more specific information.
356357

357358
A ``<parthenon/output*>`` block might look like::
358359

src/argument_parser.hpp

-2
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ class ArgParse {
4747
case 'i': // -i <input_filename>
4848
invalid = invalid_arg();
4949
input_filename = argv[++i];
50-
iarg_flag = 1;
5150
break;
5251
case 'r': // -r <restart_file>
5352
invalid = invalid_arg();
@@ -121,7 +120,6 @@ class ArgParse {
121120
char *prundir = nullptr;
122121
int res_flag = 0;
123122
int narg_flag = 0;
124-
int iarg_flag = 0;
125123
int mesh_flag = 0;
126124
int wtlim = 0;
127125
int exit_flag = 0;

src/parthenon_manager.cpp

+17-4
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,9 @@ ParthenonStatus ParthenonManager::ParthenonInitEnv(int argc, char *argv[]) {
9797
SignalHandler::SignalHandlerInit();
9898
if (Globals::my_rank == 0 && arg.wtlim > 0) SignalHandler::SetWallTimeAlarm(arg.wtlim);
9999

100-
// Populate the ParameterInput object
101-
if (arg.input_filename != nullptr) {
102-
pinput = std::make_unique<ParameterInput>(arg.input_filename);
103-
} else if (arg.res_flag != 0) {
100+
// Populate the ParameterInput object.
101+
// If restart, then ParameterInput in the restart file takes precedence.
102+
if (arg.res_flag != 0) {
104103
// Read input from restart file
105104
if (fs::path(arg.restart_filename).extension() == ".rhdf") {
106105
restartReader = std::make_unique<RestartReaderHDF5>(arg.restart_filename);
@@ -114,6 +113,20 @@ ParthenonStatus ParthenonManager::ParthenonInitEnv(int argc, char *argv[]) {
114113
std::istringstream is(inputString);
115114
pinput->LoadFromStream(is);
116115
}
116+
// If an input was provided
117+
if (arg.input_filename != nullptr) {
118+
// Modify info read from restart file
119+
if (arg.res_flag != 0) {
120+
IOWrapper infile;
121+
infile.Open(arg.input_filename, IOWrapper::FileMode::read);
122+
pinput->LoadFromFile(infile);
123+
infile.Close();
124+
125+
// Populate new object for fresh simulation
126+
} else {
127+
pinput = std::make_unique<ParameterInput>(arg.input_filename);
128+
}
129+
}
117130

118131
// Modify based on command line inputs
119132
pinput->ModifyFromCmdline(argc, argv);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# ========================================================================================
2+
# Parthenon performance portable AMR framework
3+
# Copyright(C) 2024 The Parthenon collaboration
4+
# Licensed under the 3-clause BSD License, see LICENSE file for details
5+
# ========================================================================================
6+
7+
# Testing to override parameters in a restart file from an input file
8+
<parthenon/job>
9+
problem_id=silver

tst/regression/test_suites/restart/restart.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# ========================================================================================
22
# Parthenon performance portable AMR framework
3-
# Copyright(C) 2020-2021 The Parthenon collaboration
3+
# Copyright(C) 2020-2024 The Parthenon collaboration
44
# Licensed under the 3-clause BSD License, see LICENSE file for details
55
# ========================================================================================
66
# (C) (or copyright) 2020-2021. Triad National Security, LLC. All rights reserved.
@@ -38,7 +38,8 @@ def Prepare(self, parameters, step):
3838
parameters.driver_cmd_line_args = [
3939
"-r",
4040
"gold.out0.00001.rhdf",
41-
"parthenon/job/problem_id=silver",
41+
"-i",
42+
f"{parameters.parthenon_path}/tst/regression/test_suites/restart/parthinput_override.restart",
4243
"-t",
4344
"00:00:02",
4445
]

0 commit comments

Comments
 (0)