PISM, A Parallel Ice Sheet Model  stable v2.0.4 committed by Constantine Khrulev on 2022-05-25 12:02:27 -0800
Component.cc
Go to the documentation of this file.
1 // Copyright (C) 2008-2020 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 "Component.hh"
22 #include "pism/util/io/File.hh"
23 #include "IceGrid.hh"
24 #include "pism_utilities.hh"
25 #include "VariableMetadata.hh"
26 #include "iceModelVec.hh"
27 #include "pism_options.hh"
28 #include "error_handling.hh"
29 #include "ConfigInterface.hh"
30 #include "MaxTimestep.hh"
31 #include "pism/util/Time.hh"
32 #include "pism/util/Context.hh"
33 
34 namespace pism {
35 
36 InputOptions::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  */
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, PISM_NETCDF3, 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 
84  : m_grid(g), m_config(g->ctx()->config()), 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 
106  return m_grid;
107 }
108 
109 /*! @brief Define model state variables in an output file. */
110 /*!
111  * This is needed to allow defining all the variables in an output file before any data is written
112  * (an optimization needed to get decent performance writing NetCDF-3).
113  */
114 void Component::define_model_state(const File &output) const {
115  this->define_model_state_impl(output);
116 }
117 
118 /*! @brief Write model state variables to an output file. */
119 void Component::write_model_state(const File &output) const {
120  // define variables, if needed (this is a no-op if they are already defined)
121  this->define_model_state(output);
122 
123  this->write_model_state_impl(output);
124 }
125 
126 /*! @brief The default (empty implementation). */
127 void Component::define_model_state_impl(const File &output) const {
128  (void) output;
129 }
130 
131 /*! @brief The default (empty implementation). */
132 void Component::write_model_state_impl(const File &output) const {
133  (void) output;
134 }
135 
136 /**
137  * Regrid a variable by processing -regrid_file and -regrid_vars.
138  *
139  * @param[in] module_name Module name, used to annotate options when run with -help.
140  *
141  * @param[out] variable pointer to an IceModelVec; @c variable has to
142  * have metadata set for this to work.
143  *
144  * @param[in] flag Regridding flag. If set to
145  * REGRID_WITHOUT_REGRID_VARS, regrid this variable by
146  * default, if `-regrid_vars` was not set. Otherwise a
147  * variable is only regridded if both `-regrid_file` and
148  * `-regrid_vars` are set *and* the name of the variable is
149  * found in the set of names given with `-regrid_vars`.
150  */
151 void Component::regrid(const std::string &module_name, IceModelVec &variable,
152  RegriddingFlag flag) {
153 
154  auto regrid_file = m_config->get_string("input.regrid.file");
155  auto regrid_vars = set_split(m_config->get_string("input.regrid.vars"), ',');
156 
157  if (regrid_file.empty()) {
158  return;
159  }
160 
161  SpatialVariableMetadata &m = variable.metadata();
162 
163  if (((not regrid_vars.empty()) and member(m.get_string("short_name"), regrid_vars)) or
164  (regrid_vars.empty() and flag == REGRID_WITHOUT_REGRID_VARS)) {
165 
166  m_log->message(2,
167  " %s: regridding '%s' from file '%s' ...\n",
168  module_name.c_str(),
169  m.get_string("short_name").c_str(), regrid_file.c_str());
170 
171  variable.regrid(regrid_file, CRITICAL);
172  }
173 }
174 
176  return this->max_timestep_impl(t);
177 }
178 
180  (void) t;
181  return MaxTimestep();
182 }
183 
184 
185 } // end of namespace pism
IceGrid::ConstPtr grid() const
Definition: Component.cc:105
const Config::ConstPtr m_config
configuration database used by this component
Definition: Component.hh:138
const Logger::ConstPtr m_log
logger (for easy access)
Definition: Component.hh:142
Component(IceGrid::ConstPtr g)
Definition: Component.cc:83
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:131
@ REGRID_WITHOUT_REGRID_VARS
Definition: Component.hh:131
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:132
virtual void define_model_state_impl(const File &output) const
The default (empty implementation).
Definition: Component.cc:127
virtual void regrid(const std::string &module_name, IceModelVec &variable, RegriddingFlag flag=NO_REGRID_WITHOUT_REGRID_VARS)
Definition: Component.cc:151
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:114
void write_model_state(const File &output) const
Write model state variables to an output file.
Definition: Component.cc:119
const IceGrid::ConstPtr m_grid
grid used by this component
Definition: Component.hh:136
DiagnosticList diagnostics() const
Definition: Component.cc:89
virtual MaxTimestep max_timestep_impl(double t) const
Definition: Component.cc:179
MaxTimestep max_timestep(double t) const
Reports the maximum time-step the model can take at time t.
Definition: Component.cc:175
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:316
High-level PISM I/O class.
Definition: File.hh:51
std::shared_ptr< const IceGrid > ConstPtr
Definition: IceGrid.hh:233
SpatialVariableMetadata & metadata(unsigned int N=0)
Returns a reference to the SpatialVariableMetadata object containing metadata for the compoment N.
Definition: iceModelVec.cc:533
void regrid(const std::string &filename, RegriddingFlag flag, double default_value=0.0)
Definition: iceModelVec.cc:838
Abstract class for reading, writing, allocating, and accessing a DA-based PETSc Vec (2D and 3D fields...
Definition: iceModelVec.hh:202
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).
std::string get_string(const std::string &name) const
Get a string attribute.
InputOptions process_input_options(MPI_Comm com, Config::ConstPtr config)
Definition: Component.cc:45
static const double g
Definition: exactTestP.cc:39
InitializationType
Definition: Component.hh:39
@ INIT_BOOTSTRAP
Definition: Component.hh:39
@ INIT_OTHER
Definition: Component.hh:39
@ INIT_RESTART
Definition: Component.hh:39
std::map< std::string, TSDiagnostic::Ptr > TSDiagnosticList
Definition: Diagnostic.hh:346
std::map< std::string, Diagnostic::Ptr > DiagnosticList
Definition: Diagnostic.hh:117
std::set< std::string > set_split(const std::string &input, char separator)
Transform a separator-separated list (a string) into a set of strings.
@ CRITICAL
Definition: IO_Flags.hh:70
@ PISM_NETCDF3
Definition: IO_Flags.hh:41
bool member(const std::string &string, const std::set< std::string > &set)
@ PISM_READONLY
open an existing file for reading only
Definition: IO_Flags.hh:49
InitializationType type
initialization type
Definition: Component.hh:44
std::string filename
name of the input file (if applicable)
Definition: Component.hh:46
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:48