Skip to content

Commit

Permalink
Allow parameter modification from an input file for restarts (#1004)
Browse files Browse the repository at this point in the history
* Allow parameter modification from an input file for restarts

* Fix typos

* Test override from input for restart

* Fix path to restart override
  • Loading branch information
pgrete authored Apr 10, 2024
1 parent d3f5b6a commit ac561f7
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 20 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

### Changed (changing behavior/API/variables/...)
- [[PR 1024]](https://github.com/parthenon-hpc-lab/parthenon/pull/1024) Add .outN. to history output filenames

- [[PR 1004]](https://github.com/parthenon-hpc-lab/parthenon/pull/1004) Allow parameter modification from an input file for restarts

### Fixed (not changing behavior/API/variables/...)
- [[PR 1024]](https://github.com/parthenon-hpc-lab/parthenon/pull/1024) Add features to history output
Expand Down
9 changes: 7 additions & 2 deletions doc/sphinx/src/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ ones requires ``parthenon/mesh/pack_size=-1`` during initialization, i.e.,
all blocks on a rank need to be in a single pack. This allows to use MPI
reductions inside the function, for example, to globally normalize quantities.
The ``parthenon/mesh/pack_size=-1`` exists only during problem
inititalization, i.e., simulations can be restarted with an arbitrary
initialization, i.e., simulations can be restarted with an arbitrary
``pack_size``. For an example of the ``Mesh`` version, see the `Poisson
example <https://github.com/parthenon-hpc-lab/parthenon/blob/develop/example/poisson/parthenon_app_inputs.cpp>`__.

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

Global reductions
~~~~~~~~~~~~~~~~~
Expand Down
19 changes: 10 additions & 9 deletions doc/sphinx/src/outputs.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
Outputs
=======

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

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

In addition to time base outputs, two additional options to trigger
Expand Down Expand Up @@ -114,7 +114,7 @@ Restart Files

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

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

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

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

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

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

Expand Down
2 changes: 0 additions & 2 deletions src/argument_parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ class ArgParse {
case 'i': // -i <input_filename>
invalid = invalid_arg();
input_filename = argv[++i];
iarg_flag = 1;
break;
case 'r': // -r <restart_file>
invalid = invalid_arg();
Expand Down Expand Up @@ -121,7 +120,6 @@ class ArgParse {
char *prundir = nullptr;
int res_flag = 0;
int narg_flag = 0;
int iarg_flag = 0;
int mesh_flag = 0;
int wtlim = 0;
int exit_flag = 0;
Expand Down
21 changes: 17 additions & 4 deletions src/parthenon_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,9 @@ ParthenonStatus ParthenonManager::ParthenonInitEnv(int argc, char *argv[]) {
SignalHandler::SignalHandlerInit();
if (Globals::my_rank == 0 && arg.wtlim > 0) SignalHandler::SetWallTimeAlarm(arg.wtlim);

// Populate the ParameterInput object
if (arg.input_filename != nullptr) {
pinput = std::make_unique<ParameterInput>(arg.input_filename);
} else if (arg.res_flag != 0) {
// Populate the ParameterInput object.
// If restart, then ParameterInput in the restart file takes precedence.
if (arg.res_flag != 0) {
// Read input from restart file
if (fs::path(arg.restart_filename).extension() == ".rhdf") {
restartReader = std::make_unique<RestartReaderHDF5>(arg.restart_filename);
Expand All @@ -114,6 +113,20 @@ ParthenonStatus ParthenonManager::ParthenonInitEnv(int argc, char *argv[]) {
std::istringstream is(inputString);
pinput->LoadFromStream(is);
}
// If an input was provided
if (arg.input_filename != nullptr) {
// Modify info read from restart file
if (arg.res_flag != 0) {
IOWrapper infile;
infile.Open(arg.input_filename, IOWrapper::FileMode::read);
pinput->LoadFromFile(infile);
infile.Close();

// Populate new object for fresh simulation
} else {
pinput = std::make_unique<ParameterInput>(arg.input_filename);
}
}

// Modify based on command line inputs
pinput->ModifyFromCmdline(argc, argv);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# ========================================================================================
# Parthenon performance portable AMR framework
# Copyright(C) 2024 The Parthenon collaboration
# Licensed under the 3-clause BSD License, see LICENSE file for details
# ========================================================================================

# Testing to override parameters in a restart file from an input file
<parthenon/job>
problem_id=silver
5 changes: 3 additions & 2 deletions tst/regression/test_suites/restart/restart.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# ========================================================================================
# Parthenon performance portable AMR framework
# Copyright(C) 2020-2021 The Parthenon collaboration
# Copyright(C) 2020-2024 The Parthenon collaboration
# Licensed under the 3-clause BSD License, see LICENSE file for details
# ========================================================================================
# (C) (or copyright) 2020-2021. Triad National Security, LLC. All rights reserved.
Expand Down Expand Up @@ -38,7 +38,8 @@ def Prepare(self, parameters, step):
parameters.driver_cmd_line_args = [
"-r",
"gold.out0.00001.rhdf",
"parthenon/job/problem_id=silver",
"-i",
f"{parameters.parthenon_path}/tst/regression/test_suites/restart/parthinput_override.restart",
"-t",
"00:00:02",
]
Expand Down

0 comments on commit ac561f7

Please sign in to comment.