PISM, A Parallel Ice Sheet Model  stable v2.1-1-g6902d5502 committed by Ed Bueler on 2023-12-20 08:38:27 -0800
Cache.cc
Go to the documentation of this file.
1 /* Copyright (C) 2013, 2014, 2015, 2016, 2017, 2018, 2020, 2021, 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 
20 #include <algorithm> // std::min
21 #include <cassert>
22 #include <cmath>
23 
24 #include "pism/coupler/ocean/Cache.hh"
25 #include "pism/util/Grid.hh"
26 #include "pism/util/MaxTimestep.hh"
27 #include "pism/util/Time.hh"
28 #include "pism/util/error_handling.hh"
29 
30 namespace pism {
31 namespace ocean {
32 
33 Cache::Cache(std::shared_ptr<const Grid> g, std::shared_ptr<OceanModel> in)
34  : OceanModel(g, in) {
35 
37  m_update_interval_years = m_config->get_number("ocean.cache.update_interval", "seconds");
38 
39  // use the current year length (according to the selected calendar) to convert update
40  // interval length into years
42 
43  if (m_update_interval_years <= 0.0) {
45  "ocean.cache.update_interval has to be strictly positive (got %f)",
47  }
48 
49  {
53  }
54 }
55 
56 void Cache::init_impl(const Geometry &geometry) {
57  m_input_model->init(geometry);
58 
59  m_log->message(2,
60  "* Initializing the 'caching' ocean model modifier...\n");
61 
63 }
64 
65 void Cache::update_impl(const Geometry &geometry, double t, double dt) {
66  // ignore dt and always use 1 year long time-steps when updating
67  // an input model
68  (void) dt;
69 
70  double time_resolution = m_config->get_number("time_stepping.resolution", "seconds");
71 
72  if (t >= m_next_update_time or
73  fabs(t - m_next_update_time) < time_resolution) {
74 
75  double
76  one_year_from_now = time().increment_date(t, 1.0),
77  update_dt = one_year_from_now - t;
78 
79  assert(update_dt > 0.0);
80 
81  m_input_model->update(geometry, t, update_dt);
82 
85 
86  m_water_column_pressure->copy_from(m_input_model->average_water_column_pressure());
87 
88  m_shelf_base_temperature->copy_from(m_input_model->shelf_base_temperature());
89 
90  m_shelf_base_mass_flux->copy_from(m_input_model->shelf_base_mass_flux());
91  }
92 }
93 
95  double dt = m_next_update_time - t;
96 
97  double time_resolution = m_config->get_number("time_stepping.resolution", "seconds");
98 
99  // if we got very close to the next update time, set time step
100  // length to the interval between updates
101  if (dt < time_resolution) {
102  double update_time_after_next = time().increment_date(m_next_update_time,
104 
105  dt = update_time_after_next - m_next_update_time;
106  assert(dt > 0.0);
107  }
108 
109  MaxTimestep cache_dt(dt, "ocean cache");
110 
111  MaxTimestep input_max_timestep = m_input_model->max_timestep(t);
112  if (input_max_timestep.finite()) {
113  return std::min(input_max_timestep, cache_dt);
114  } else {
115  return cache_dt;
116  }
117 }
118 
120  return *m_shelf_base_temperature;
121 }
122 
124  return *m_shelf_base_mass_flux;
125 }
126 
128  return *m_water_column_pressure;
129 }
130 
131 
132 } // end of namespace ocean
133 } // end of namespace pism
const Time & time() const
Definition: Component.cc:109
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
bool finite() const
Convert to bool to check if a time step restriction is "active".
Definition: MaxTimestep.cc:47
Combines the max. time step with the flag indicating if a restriction is active. Makes is possible to...
Definition: MaxTimestep.hh:31
static RuntimeError formatted(const ErrorLocation &location, const char format[],...) __attribute__((format(printf
build a RuntimeError with a formatted message
double increment_date(double T, double years) const
Definition: Time.cc:852
double convert_time_interval(double T, const std::string &units) const
Convert time interval from seconds to given units. Handle 'years' using the year length corresponding...
Definition: Time.cc:674
double current() const
Current time, in seconds.
Definition: Time.cc:465
double m_update_interval_years
Definition: Cache.hh:44
void update_impl(const Geometry &geometry, double my_t, double my_dt)
Definition: Cache.cc:65
const array::Scalar & shelf_base_temperature_impl() const
Definition: Cache.cc:119
MaxTimestep max_timestep_impl(double t) const
Definition: Cache.cc:94
std::shared_ptr< array::Scalar > m_shelf_base_mass_flux
Definition: Cache.hh:48
void init_impl(const Geometry &geometry)
Definition: Cache.cc:56
const array::Scalar & shelf_base_mass_flux_impl() const
Definition: Cache.cc:123
double m_next_update_time
Definition: Cache.hh:43
std::shared_ptr< array::Scalar > m_shelf_base_temperature
Definition: Cache.hh:47
Cache(std::shared_ptr< const Grid > g, std::shared_ptr< OceanModel > in)
Definition: Cache.cc:33
const array::Scalar & average_water_column_pressure_impl() const
Definition: Cache.cc:127
std::shared_ptr< OceanModel > m_input_model
Definition: OceanModel.hh:72
std::shared_ptr< array::Scalar > m_water_column_pressure
Definition: OceanModel.hh:73
static std::shared_ptr< array::Scalar > allocate_shelf_base_temperature(std::shared_ptr< const Grid > g)
Definition: OceanModel.cc:31
static std::shared_ptr< array::Scalar > allocate_shelf_base_mass_flux(std::shared_ptr< const Grid > g)
Definition: OceanModel.cc:39
static std::shared_ptr< array::Scalar > allocate_water_column_pressure(std::shared_ptr< const Grid > g)
Definition: OceanModel.cc:49
A very rudimentary PISM ocean model.
Definition: OceanModel.hh:33
#define PISM_ERROR_LOCATION
double min(const array::Scalar &input)
Finds minimum over all the values in an array::Scalar object. Ignores ghosts.
Definition: Scalar.cc:193
bool ocean(int M)
An ocean cell (floating ice or ice-free).
Definition: Mask.hh:40
static const double g
Definition: exactTestP.cc:36