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