PISM, A Parallel Ice Sheet Model 2.3.0-79cae578d committed by Constantine Khrulev on 2026-03-22
Loading...
Searching...
No Matches
Component.cc
Go to the documentation of this file.
1// Copyright (C) 2008-2020, 2022, 2023, 2025, 2026 Ed Bueler and Constantine Khroulev
2//
3// This file is part of PISM.
4//
5// PISM is free software; you can redistribute it and/or modify it under the
6// terms of the GNU General Public License as published by the Free Software
7// Foundation; either version 3 of the License, or (at your option) any later
8// version.
9//
10// PISM is distributed in the hope that it will be useful, but WITHOUT ANY
11// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13// details.
14//
15// You should have received a copy of the GNU General Public License
16// along with PISM; if not, write to the Free Software
17// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
19#include <cassert>
20
21#include "pism/util/Component.hh"
22#include "pism/util/Profiling.hh"
23#include "pism/util/io/File.hh"
24#include "pism/util/Grid.hh"
25#include "pism/util/pism_utilities.hh"
26#include "pism/util/VariableMetadata.hh"
27#include "pism/util/Config.hh"
28#include "pism/util/MaxTimestep.hh"
29#include "pism/util/Time.hh"
30#include "pism/util/Context.hh"
31#include "pism/util/Logger.hh"
32#include "pism/util/io/IO_Flags.hh"
33
34namespace pism {
35
36InputOptions::InputOptions(InitializationType t, const std::string &file, unsigned int index) {
37 type = t;
38 filename = file;
39 record = index;
40}
41
42/*! Process command-line options -i and -bootstrap.
43 *
44 */
45InputOptions process_input_options(MPI_Comm com, std::shared_ptr<const Config> config) {
47 unsigned int record = 0;
48
49 std::string input_filename = config->get_string("input.file");
50
51 bool bootstrap = config->get_flag("input.bootstrap") and (not input_filename.empty());
52 bool restart = (not config->get_flag("input.bootstrap")) and (not input_filename.empty());
53
54 if (restart) {
55 // re-start a run by initializing from an input file
56 type = INIT_RESTART;
57 } else if (bootstrap) {
58 // initialize from an input file using bootstrapping heuristics
59 type = INIT_BOOTSTRAP;
60 } else {
61 // other types of initialization (usually from formulas)
62 type = INIT_OTHER;
63 }
64
65 // get the index of the last record in the input file
66 if (not input_filename.empty()) {
67 File input_file(com, input_filename, io::PISM_NETCDF3, io::PISM_READONLY);
68
69 // Find the index of the last record in the input file.
70 unsigned int last_record = input_file.nrecords();
71 if (last_record > 0) {
72 last_record -= 1;
73 }
74
75 record = last_record;
76 } else {
77 record = 0;
78 }
79
80 return InputOptions(type, input_filename, record);
81}
82
83Component::Component(std::shared_ptr<const Grid> g)
84 : m_grid(g),
85 m_config(g->ctx()->config()),
86 m_sys(g->ctx()->unit_system()),
87 m_log(g->ctx()->log()) {
88 // empty
89}
90
94
98
102
106
107std::shared_ptr<const Grid> Component::grid() const {
108 return m_grid;
109}
110
111const Time &Component::time() const {
112 return *m_grid->ctx()->time();
113}
114
116 return m_grid->ctx()->profiling();
117}
118
119/*! @brief Write model state variables to an output file. */
120void Component::write_state(const OutputFile &output) const {
121 this->write_state_impl(output);
122}
123
124std::set<VariableMetadata> Component::state() const {
125 return state_impl();
126}
127
128std::set<VariableMetadata> Component::state_impl() const {
129 return {};
130}
131
132/*! @brief The default (empty implementation). */
133void Component::write_state_impl(const OutputFile &output) const {
134 (void) output;
135}
136
137/**
138 * Regrid a variable by processing -regrid_file and -regrid_vars.
139 *
140 * @param[in] module_name Module name, used to annotate options when run with -help.
141 *
142 * @param[out] variable pointer to an array::Array; @c variable has to
143 * have metadata set for this to work.
144 *
145 * @param[in] flag Regridding flag. If set to
146 * REGRID_WITHOUT_REGRID_VARS, regrid this variable by
147 * default, if `-regrid_vars` was not set. Otherwise a
148 * variable is only regridded if both `-regrid_file` and
149 * `-regrid_vars` are set *and* the name of the variable is
150 * found in the set of names given with `-regrid_vars`.
151 */
152void Component::regrid(const std::string &module_name, array::Array &variable,
153 RegriddingFlag flag) {
154
155 auto regrid_file = m_config->get_string("input.regrid.file");
156 auto regrid_vars = set_split(m_config->get_string("input.regrid.vars"), ',');
157
158 if (regrid_file.empty()) {
159 return;
160 }
161
162 auto &m = variable.metadata();
163
164 if (((not regrid_vars.empty()) and set_member(m["short_name"], regrid_vars)) or
165 (regrid_vars.empty() and flag == REGRID_WITHOUT_REGRID_VARS)) {
166
167 m_log->message(2,
168 " %s: regridding '%s' from file '%s' ...\n",
169 module_name.c_str(),
170 m.get_string("short_name").c_str(), regrid_file.c_str());
171
172 variable.regrid(regrid_file, io::Default::Nil());
173 }
174}
175
177 return this->max_timestep_impl(t);
178}
179
181 (void) t;
182 return MaxTimestep();
183}
184
185
186} // end of namespace pism
const Time & time() const
Definition Component.cc:111
std::shared_ptr< const Grid > grid() const
Definition Component.cc:107
std::shared_ptr< const Config > m_config
configuration database used by this component
Definition Component.hh:160
RegriddingFlag
This flag determines whether a variable is read from the -regrid_file file even if it is not listed a...
Definition Component.hh:153
virtual DiagnosticList spatial_diagnostics_impl() const
Definition Component.cc:99
TSDiagnosticList scalar_diagnostics() const
Definition Component.cc:95
const std::shared_ptr< const Grid > m_grid
grid used by this component
Definition Component.hh:158
void regrid(const std::string &module_name, array::Array &variable, RegriddingFlag flag=NO_REGRID_WITHOUT_REGRID_VARS)
Definition Component.cc:152
DiagnosticList spatial_diagnostics() const
Definition Component.cc:91
virtual TSDiagnosticList scalar_diagnostics_impl() const
Definition Component.cc:103
Component(std::shared_ptr< const Grid > grid)
Definition Component.cc:83
virtual void write_state_impl(const OutputFile &output) const
The default (empty implementation).
Definition Component.cc:133
const Profiling & profiling() const
Definition Component.cc:115
std::set< VariableMetadata > state() const
Definition Component.cc:124
virtual MaxTimestep max_timestep_impl(double t) const
Definition Component.cc:180
MaxTimestep max_timestep(double t) const
Reports the maximum time-step the model can take at time t.
Definition Component.cc:176
void write_state(const OutputFile &output) const
Write model state variables to an output file.
Definition Component.cc:120
std::shared_ptr< const Logger > m_log
logger (for easy access)
Definition Component.hh:164
virtual std::set< VariableMetadata > state_impl() const
Definition Component.cc:128
unsigned int nrecords() const
Get the number of records. Uses the length of an unlimited dimension.
Definition File.cc:280
High-level PISM I/O class.
Definition File.hh:57
Combines the max. time step with the flag indicating if a restriction is active. Makes is possible to...
Time management class.
Definition Time.hh:56
void regrid(const std::string &filename, io::Default default_value)
Definition Array.cc:758
VariableMetadata & metadata(unsigned int N=0)
Returns a reference to the VariableMetadata object containing metadata for the compoment N.
Definition Array.cc:467
Abstract class for reading, writing, allocating, and accessing a DA-based PETSc Vec (2D and 3D fields...
Definition Array.hh:209
static Default Nil()
Definition IO_Flags.hh:94
@ PISM_NETCDF3
Definition IO_Flags.hh:58
@ PISM_READONLY
open an existing file for reading only
Definition IO_Flags.hh:69
static const double g
Definition exactTestP.cc:36
InitializationType
Definition Component.hh:56
@ INIT_BOOTSTRAP
Definition Component.hh:56
@ INIT_OTHER
Definition Component.hh:56
@ INIT_RESTART
Definition Component.hh:56
std::map< std::string, TSDiagnostic::Ptr > TSDiagnosticList
std::map< std::string, Diagnostic::Ptr > DiagnosticList
std::set< std::string > set_split(const std::string &input, char separator)
Transform a separator-separated list (a string) into a set of strings.
InputOptions process_input_options(MPI_Comm com, std::shared_ptr< const Config > config)
Definition Component.cc:45
bool set_member(const std::string &string, const std::set< std::string > &set)
InitializationType type
initialization type
Definition Component.hh:61
std::string filename
name of the input file (if applicable)
Definition Component.hh:63
InputOptions(InitializationType t, const std::string &file, unsigned int index)
Definition Component.cc:36
unsigned int record
index of the record to re-start from
Definition Component.hh:65