PISM, A Parallel Ice Sheet Model  stable v2.1-1-g6902d5502 committed by Ed Bueler on 2023-12-20 08:38:27 -0800
Component.cc
Go to the documentation of this file.
1 // Copyright (C) 2008-2020, 2022, 2023 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/ConfigInterface.hh"
28 #include "pism/util/MaxTimestep.hh"
29 #include "pism/util/Time.hh"
30 #include "pism/util/Context.hh"
31 
32 namespace pism {
33 
34 InputOptions::InputOptions(InitializationType t, const std::string &file, unsigned int index) {
35  type = t;
36  filename = file;
37  record = index;
38 }
39 
40 /*! Process command-line options -i and -bootstrap.
41  *
42  */
45  unsigned int record = 0;
46 
47  std::string input_filename = config->get_string("input.file");
48 
49  bool bootstrap = config->get_flag("input.bootstrap") and (not input_filename.empty());
50  bool restart = (not config->get_flag("input.bootstrap")) and (not input_filename.empty());
51 
52  if (restart) {
53  // re-start a run by initializing from an input file
54  type = INIT_RESTART;
55  } else if (bootstrap) {
56  // initialize from an input file using bootstrapping heuristics
57  type = INIT_BOOTSTRAP;
58  } else {
59  // other types of initialization (usually from formulas)
60  type = INIT_OTHER;
61  }
62 
63  // get the index of the last record in the input file
64  if (not input_filename.empty()) {
65  File input_file(com, input_filename, io::PISM_NETCDF3, io::PISM_READONLY);
66 
67  // Find the index of the last record in the input file.
68  unsigned int last_record = input_file.nrecords();
69  if (last_record > 0) {
70  last_record -= 1;
71  }
72 
73  record = last_record;
74  } else {
75  record = 0;
76  }
77 
78  return InputOptions(type, input_filename, record);
79 }
80 
81 Component::Component(std::shared_ptr<const Grid> g)
82  : m_grid(g),
83  m_config(g->ctx()->config()),
84  m_sys(g->ctx()->unit_system()),
85  m_log(g->ctx()->log()) {
86  // empty
87 }
88 
90  return this->diagnostics_impl();
91 }
92 
94  return this->ts_diagnostics_impl();
95 }
96 
98  return {};
99 }
100 
102  return {};
103 }
104 
105 std::shared_ptr<const Grid> Component::grid() const {
106  return m_grid;
107 }
108 
109 const Time &Component::time() const {
110  return *m_grid->ctx()->time();
111 }
112 
114  return m_grid->ctx()->profiling();
115 }
116 
117 /*! @brief Define model state variables in an output file. */
118 /*!
119  * This is needed to allow defining all the variables in an output file before any data is written
120  * (an optimization needed to get decent performance writing NetCDF-3).
121  */
122 void Component::define_model_state(const File &output) const {
123  this->define_model_state_impl(output);
124 }
125 
126 /*! @brief Write model state variables to an output file. */
127 void Component::write_model_state(const File &output) const {
128  // define variables, if needed (this is a no-op if they are already defined)
129  this->define_model_state(output);
130 
131  this->write_model_state_impl(output);
132 }
133 
134 /*! @brief The default (empty implementation). */
135 void Component::define_model_state_impl(const File &output) const {
136  (void) output;
137 }
138 
139 /*! @brief The default (empty implementation). */
140 void Component::write_model_state_impl(const File &output) const {
141  (void) output;
142 }
143 
144 /**
145  * Regrid a variable by processing -regrid_file and -regrid_vars.
146  *
147  * @param[in] module_name Module name, used to annotate options when run with -help.
148  *
149  * @param[out] variable pointer to an array::Array; @c variable has to
150  * have metadata set for this to work.
151  *
152  * @param[in] flag Regridding flag. If set to
153  * REGRID_WITHOUT_REGRID_VARS, regrid this variable by
154  * default, if `-regrid_vars` was not set. Otherwise a
155  * variable is only regridded if both `-regrid_file` and
156  * `-regrid_vars` are set *and* the name of the variable is
157  * found in the set of names given with `-regrid_vars`.
158  */
159 void Component::regrid(const std::string &module_name, array::Array &variable,
160  RegriddingFlag flag) {
161 
162  auto regrid_file = m_config->get_string("input.regrid.file");
163  auto regrid_vars = set_split(m_config->get_string("input.regrid.vars"), ',');
164 
165  if (regrid_file.empty()) {
166  return;
167  }
168 
169  SpatialVariableMetadata &m = variable.metadata();
170 
171  if (((not regrid_vars.empty()) and member(m["short_name"], regrid_vars)) or
172  (regrid_vars.empty() and flag == REGRID_WITHOUT_REGRID_VARS)) {
173 
174  m_log->message(2,
175  " %s: regridding '%s' from file '%s' ...\n",
176  module_name.c_str(),
177  m.get_string("short_name").c_str(), regrid_file.c_str());
178 
179  variable.regrid(regrid_file, io::Default::Nil());
180  }
181 }
182 
184  return this->max_timestep_impl(t);
185 }
186 
188  (void) t;
189  return MaxTimestep();
190 }
191 
192 
193 } // end of namespace pism
const Time & time() const
Definition: Component.cc:109
std::shared_ptr< const Grid > grid() const
Definition: Component.cc:105
const Config::ConstPtr m_config
configuration database used by this component
Definition: Component.hh:158
const Logger::ConstPtr m_log
logger (for easy access)
Definition: Component.hh:162
virtual TSDiagnosticList ts_diagnostics_impl() const
Definition: Component.cc:101
RegriddingFlag
This flag determines whether a variable is read from the -regrid_file file even if it is not listed a...
Definition: Component.hh:151
@ REGRID_WITHOUT_REGRID_VARS
Definition: Component.hh:151
TSDiagnosticList ts_diagnostics() const
Definition: Component.cc:93
virtual void write_model_state_impl(const File &output) const
The default (empty implementation).
Definition: Component.cc:140
const std::shared_ptr< const Grid > m_grid
grid used by this component
Definition: Component.hh:156
void regrid(const std::string &module_name, array::Array &variable, RegriddingFlag flag=NO_REGRID_WITHOUT_REGRID_VARS)
Definition: Component.cc:159
virtual void define_model_state_impl(const File &output) const
The default (empty implementation).
Definition: Component.cc:135
Component(std::shared_ptr< const Grid > grid)
Definition: Component.cc:81
virtual DiagnosticList diagnostics_impl() const
Definition: Component.cc:97
void define_model_state(const File &output) const
Define model state variables in an output file.
Definition: Component.cc:122
void write_model_state(const File &output) const
Write model state variables to an output file.
Definition: Component.cc:127
DiagnosticList diagnostics() const
Definition: Component.cc:89
const Profiling & profiling() const
Definition: Component.cc:113
virtual MaxTimestep max_timestep_impl(double t) const
Definition: Component.cc:187
MaxTimestep max_timestep(double t) const
Reports the maximum time-step the model can take at time t.
Definition: Component.cc:183
std::shared_ptr< const Config > ConstPtr
unsigned int nrecords() const
Get the number of records. Uses the length of an unlimited dimension.
Definition: File.cc:313
High-level PISM I/O class.
Definition: File.hh:56
Combines the max. time step with the flag indicating if a restriction is active. Makes is possible to...
Definition: MaxTimestep.hh:31
Spatial NetCDF variable (corresponding to a 2D or 3D scalar field).
Time management class.
Definition: Time.hh:55
std::string get_string(const std::string &name) const
Get a string attribute.
void regrid(const std::string &filename, io::Default default_value)
Definition: Array.cc:814
SpatialVariableMetadata & metadata(unsigned int N=0)
Returns a reference to the SpatialVariableMetadata object containing metadata for the compoment N.
Definition: Array.cc:553
Abstract class for reading, writing, allocating, and accessing a DA-based PETSc Vec (2D and 3D fields...
Definition: Array.hh:208
static Default Nil()
Definition: IO_Flags.hh:97
@ PISM_NETCDF3
Definition: IO_Flags.hh:57
@ PISM_READONLY
open an existing file for reading only
Definition: IO_Flags.hh:72
InputOptions process_input_options(MPI_Comm com, Config::ConstPtr config)
Definition: Component.cc:43
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
Definition: Diagnostic.hh:343
std::map< std::string, Diagnostic::Ptr > DiagnosticList
Definition: Diagnostic.hh:125
std::set< std::string > set_split(const std::string &input, char separator)
Transform a separator-separated list (a string) into a set of strings.
bool 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:34
unsigned int record
index of the record to re-start from
Definition: Component.hh:65