PISM, A Parallel Ice Sheet Model  stable v2.1-1-g6902d5502 committed by Ed Bueler on 2023-12-20 08:38:27 -0800
Component.hh
Go to the documentation of this file.
1 // Copyright (C) 2008-2018, 2020, 2021, 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 #ifndef PISM_COMPONENT_H
20 #define PISM_COMPONENT_H
21 
22 #include <string>
23 
24 #include "pism/util/ConfigInterface.hh"
25 #include "pism/util/Units.hh"
26 #include "pism/util/Logger.hh"
27 #include "pism/util/Diagnostic.hh"
28 
29 namespace pism {
30 
31 class MaxTimestep;
32 class File;
33 class Geometry;
34 class Time;
35 class Profiling;
36 class Grid;
37 
38 namespace array {
39 template<typename T> class Array2D;
40 class Array3D;
41 class Array;
42 class CellType1;
43 class CellType2;
44 class CellType;
45 class Forcing;
46 class Scalar1;
47 class Scalar2;
48 class Scalar;
49 class Staggered1;
50 class Staggered;
51 class Vector1;
52 class Vector2;
53 class Vector;
54 } // end of namespace array
55 
57 
58 struct InputOptions {
59  InputOptions(InitializationType t, const std::string &file, unsigned int index);
60  //! initialization type
62  //! name of the input file (if applicable)
63  std::string filename;
64  //! index of the record to re-start from
65  unsigned int record;
66 };
67 
69 
70 //! \brief A class defining a common interface for most PISM sub-models.
71 /*!
72  \section pism_components PISM's model components and their interface
73 
74  We've found that many sub-models in PISM share some tasks: they need to be
75  "initialized", "updated", asked for diagnostic quantities, asked to write the
76  model state...
77 
78  Component and its derived classes were created to have a common interface
79  for PISM sub-models, such as surface, atmosphere, ocean and bed deformation
80  models.
81 
82  \subsection pismcomponent_init Initialization
83 
84  Component::init() should contain all the initialization code,
85  excluding memory-allocation. (We might need to "re-initialize" a
86  component.)
87 
88  Many PISM sub-models read data from the same file the rest of PISM reads
89  from. Component::find_pism_input() checks options `-i` and `-bootstrap`
90  options to simplify finding this file.
91 
92  \subsection pismcomponent_output Writing to an output file
93 
94  A PISM component needs to implement the following I/O methods:
95 
96  - define_model_state_impl()
97  - write_model_state_impl()
98 
99  Why are all these methods needed? In PISM we separate defining and writing
100  NetCDF variables because defining all the NetCDF variables before writing
101  data is a lot faster than defining a variable, writing it, defining the
102  second variable, etc. (See http://www.unidata.ucar.edu/software/netcdf/docs/netcdf/Parts-of-a-NetCDF-Classic-File.html#Parts-of-a-NetCDF-Classic-File for a technical explanation.)
103 
104  Within IceModel the following steps are done to write 2D and 3D fields to an
105  output file:
106 
107  - Assemble the list of variables to be written (see
108  IceModel::output_variables()); calls add_vars_to_output()
109  - Create a NetCDF file
110  - Define all the variables in the file (see IceModel::write_variables());
111  calls define_variables()
112  - Write all the variables to the file (same method); calls write_variables().
113 
114  \subsection pismcomponent_timestep Restricting time-steps
115 
116  Implement Component::max_timestep() to affect PISM's adaptive time-stepping mechanism.
117 */
118 class Component {
119 public:
120  /** Create a Component instance given a grid. */
121  Component(std::shared_ptr<const Grid> grid);
122  virtual ~Component() = default;
123 
124  DiagnosticList diagnostics() const;
126 
127  std::shared_ptr<const Grid> grid() const;
128 
129  const Time &time() const;
130 
131  const Profiling &profiling() const;
132 
133  void define_model_state(const File &output) const;
134  void write_model_state(const File &output) const;
135 
136  //! Reports the maximum time-step the model can take at time t.
137  MaxTimestep max_timestep(double t) const;
138 
139 protected:
140  virtual MaxTimestep max_timestep_impl(double t) const;
141  virtual void define_model_state_impl(const File &output) const;
142  virtual void write_model_state_impl(const File &output) const;
143 
144  virtual DiagnosticList diagnostics_impl() const;
145  virtual TSDiagnosticList ts_diagnostics_impl() const;
146 
147  /** @brief This flag determines whether a variable is read from the
148  `-regrid_file` file even if it is not listed among variables in
149  `-regrid_vars`.
150  */
152  void regrid(const std::string &module_name, array::Array &variable,
154 
155  //! grid used by this component
156  const std::shared_ptr<const Grid> m_grid;
157  //! configuration database used by this component
159  //! unit system used by this component
161  //! logger (for easy access)
163 };
164 
165 } // end of namespace pism
166 
167 #endif // PISM_COMPONENT_H
const units::System::Ptr m_sys
unit system used by this component
Definition: Component.hh:160
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
@ NO_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
virtual ~Component()=default
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
A class defining a common interface for most PISM sub-models.
Definition: Component.hh:118
std::shared_ptr< const Config > ConstPtr
High-level PISM I/O class.
Definition: File.hh:56
std::shared_ptr< const Logger > ConstPtr
Definition: Logger.hh:46
Combines the max. time step with the flag indicating if a restriction is active. Makes is possible to...
Definition: MaxTimestep.hh:31
Time management class.
Definition: Time.hh:55
Abstract class for reading, writing, allocating, and accessing a DA-based PETSc Vec (2D and 3D fields...
Definition: Array.hh:208
std::shared_ptr< System > Ptr
Definition: Units.hh:47
InputOptions process_input_options(MPI_Comm com, Config::ConstPtr config)
Definition: Component.cc:43
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
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