PISM, A Parallel Ice Sheet Model  stable v2.1-1-g6902d5502 committed by Ed Bueler on 2023-12-20 08:38:27 -0800
PyOceanModel.hh
Go to the documentation of this file.
1 // Copyright (C) 2023 PISM Authors
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_PYOCEANMODEL_H
20 #define PISM_PYOCEANMODEL_H
21 
22 #include "pism/coupler/ocean/CompleteOceanModel.hh"
23 #include <memory>
24 
25 namespace pism {
26 namespace ocean {
27 
28 /*!
29  * The base class for ocean models that are implemented in Python
30  */
31 class PyOceanModel {
32 public:
33  virtual ~PyOceanModel() = default;
34 
35  std::shared_ptr<pism::array::Scalar> shelf_base_temperature;
36  std::shared_ptr<array::Scalar> shelf_base_mass_flux;
37  std::shared_ptr<array::Scalar> water_column_pressure;
38 
39  /*!
40  * Allocate data members. We need this to be able to test a Python implementation of an
41  * ocean model *without* the rest of PISM.
42  */
43  void allocate(std::shared_ptr<const Grid> grid);
44 
45  /*!
46  * Maximum time step the model can take at time `t` (in seconds)
47  */
48  virtual MaxTimestep max_timestep(double t) const;
49 
50  /*!
51  * Initialize the model state from formulas, by reading from an input file, etc.
52  */
53  virtual void init(const Geometry &geometry);
54 
55  /*!
56  * Update the state of the model by taking a time step from `t` to `t + dt` (in seconds).
57  *
58  * Assumes that the time step length `dt` is allowed at the time `t`.
59  */
60  virtual void update(const Geometry &geometry, double t, double dt);
61 
62  /*!
63  * Define model state variables and set their attributes
64  */
65  virtual void define_model_state(const File &output) const;
66 
67  /*!
68  * Write model state variables and set their attributes
69  */
70  virtual void write_model_state(const File &output) const;
71 };
72 
73 //! The adapter class for Python ocean models
74 /*!
75  * We need this class because SWIG cannot create a wrapper for the OceanModel class that
76  * can be used as a base class for Python classes. (Specifically: it does not support
77  * methods that return a reference, e.g. `const array::Scalar& shelf_base_mass_flux_impl()`.)
78  */
80 public:
81  PyOceanModelAdapter(std::shared_ptr<const Grid> grid, std::shared_ptr<PyOceanModel> implementation);
82  virtual ~PyOceanModelAdapter() = default;
83 
84 private:
85  MaxTimestep max_timestep_impl(double t) const;
86  void update_impl(const Geometry &geometry, double my_t, double my_dt);
87  void init_impl(const Geometry &geometry);
88 
89  void define_model_state_impl(const File &output) const;
90  void write_model_state_impl(const File &output) const;
91 
92  std::shared_ptr<PyOceanModel> m_impl;
93 };
94 
95 } // end of namespace ocean
96 } // end of namespace pism
97 #endif /* PISM_PYOCEANMODEL_H */
std::shared_ptr< const Grid > grid() const
Definition: Component.cc:105
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
virtual ~PyOceanModelAdapter()=default
void update_impl(const Geometry &geometry, double my_t, double my_dt)
Definition: PyOceanModel.cc:76
void init_impl(const Geometry &geometry)
Definition: PyOceanModel.cc:80
PyOceanModelAdapter(std::shared_ptr< const Grid > grid, std::shared_ptr< PyOceanModel > implementation)
Definition: PyOceanModel.cc:63
MaxTimestep max_timestep_impl(double t) const
Definition: PyOceanModel.cc:72
void write_model_state_impl(const File &output) const
The default (empty implementation).
Definition: PyOceanModel.cc:89
void define_model_state_impl(const File &output) const
The default (empty implementation).
Definition: PyOceanModel.cc:85
std::shared_ptr< PyOceanModel > m_impl
Definition: PyOceanModel.hh:92
The adapter class for Python ocean models.
Definition: PyOceanModel.hh:79
virtual ~PyOceanModel()=default
virtual void init(const Geometry &geometry)
Definition: PyOceanModel.cc:41
virtual void write_model_state(const File &output) const
Definition: PyOceanModel.cc:58
virtual void update(const Geometry &geometry, double t, double dt)
Definition: PyOceanModel.cc:46
virtual MaxTimestep max_timestep(double t) const
Definition: PyOceanModel.cc:36
std::shared_ptr< pism::array::Scalar > shelf_base_temperature
Definition: PyOceanModel.hh:35
virtual void define_model_state(const File &output) const
Definition: PyOceanModel.cc:53
std::shared_ptr< array::Scalar > water_column_pressure
Definition: PyOceanModel.hh:37
std::shared_ptr< array::Scalar > shelf_base_mass_flux
Definition: PyOceanModel.hh:36
void allocate(std::shared_ptr< const Grid > grid)
Definition: PyOceanModel.cc:30
bool ocean(int M)
An ocean cell (floating ice or ice-free).
Definition: Mask.hh:40