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, 2019, 2020, 2021, 2022, 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 <cmath> // fabs()
21 #include <cassert>
22 #include <algorithm> // for std::min()
23 
24 #include "pism/coupler/surface/Cache.hh"
25 #include "pism/util/Time.hh"
26 #include "pism/util/Grid.hh"
27 #include "pism/util/error_handling.hh"
28 #include "pism/util/MaxTimestep.hh"
29 
30 namespace pism {
31 namespace surface {
32 
33 Cache::Cache(std::shared_ptr<const Grid> grid, std::shared_ptr<SurfaceModel> in)
34  : SurfaceModel(grid, in) {
35 
37  m_update_interval_years = m_config->get_number("surface.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) {
45  "surface.cache.update_interval has to be strictly positive.");
46  }
47 
48  {
57  }
58 }
59 
60 void Cache::init_impl(const Geometry &geometry) {
61  m_input_model->init(geometry);
62 
63  m_log->message(2, "* Initializing the 'caching' surface model modifier...\n");
64 
66 }
67 
68 void Cache::update_impl(const Geometry &geometry, double t, double dt) {
69  // ignore dt and always use 1 year long time-steps when updating
70  // the input model
71  (void) dt;
72 
73  double time_resolution = m_config->get_number("time_stepping.resolution", "seconds");
74  if (t >= m_next_update_time or fabs(t - m_next_update_time) < time_resolution) {
75 
76  double
77  one_year_from_now = time().increment_date(t, 1.0),
78  update_dt = one_year_from_now - t;
79 
80  assert(update_dt > 0.0);
81 
82  m_input_model->update(geometry, t, update_dt);
83 
86 
87  // store outputs of the input model
88  m_mass_flux->copy_from(m_input_model->mass_flux());
89  m_temperature->copy_from(m_input_model->temperature());
90  m_liquid_water_fraction->copy_from(m_input_model->liquid_water_fraction());
91  m_layer_mass->copy_from(m_input_model->layer_mass());
92  m_layer_thickness->copy_from(m_input_model->layer_thickness());
93  m_accumulation->copy_from(m_input_model->accumulation());
94  m_melt->copy_from(m_input_model->melt());
95  m_runoff->copy_from(m_input_model->runoff());
96  }
97 }
98 
100  double dt = m_next_update_time - t;
101 
102  double time_resolution = m_config->get_number("time_stepping.resolution", "seconds");
103 
104  // if we got very close to the next update time, set time step
105  // length to the interval between updates
106  if (dt < time_resolution) {
107  double update_time_after_next = time().increment_date(m_next_update_time,
109 
110  dt = update_time_after_next - m_next_update_time;
111  assert(dt > 0.0);
112  }
113 
114  assert(m_input_model != NULL);
115 
116  MaxTimestep cache_dt(dt, "surface cache");
117 
118  MaxTimestep input_max_timestep = m_input_model->max_timestep(t);
119  if (input_max_timestep.finite()) {
120  return std::min(input_max_timestep, cache_dt);
121  }
122 
123  return cache_dt;
124 }
125 
127  return *m_layer_thickness;
128 }
129 
131  return *m_mass_flux;
132 }
133 
135  return *m_temperature;
136 }
137 
139  return *m_liquid_water_fraction;
140 }
141 
143  return *m_layer_mass;
144 }
145 
147  return *m_accumulation;
148 }
149 
151  return *m_melt;
152 }
153 
155  return *m_runoff;
156 }
157 
158 } // end of namespace surface
159 } // end of namespace pism
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
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
virtual const array::Scalar & runoff_impl() const
Definition: Cache.cc:154
std::shared_ptr< array::Scalar > m_temperature
Definition: Cache.hh:50
const array::Scalar & layer_thickness_impl() const
Definition: Cache.cc:126
void update_impl(const Geometry &geometry, double t, double dt)
Definition: Cache.cc:68
MaxTimestep max_timestep_impl(double t) const
Definition: Cache.cc:99
const array::Scalar & liquid_water_fraction_impl() const
Definition: Cache.cc:138
const array::Scalar & layer_mass_impl() const
Definition: Cache.cc:142
double m_update_interval_years
Definition: Cache.hh:53
std::shared_ptr< array::Scalar > m_mass_flux
Definition: Cache.hh:49
Cache(std::shared_ptr< const Grid > g, std::shared_ptr< SurfaceModel > in)
Definition: Cache.cc:33
const array::Scalar & temperature_impl() const
Definition: Cache.cc:134
double m_next_update_time
Definition: Cache.hh:52
virtual const array::Scalar & melt_impl() const
Definition: Cache.cc:150
const array::Scalar & mass_flux_impl() const
Definition: Cache.cc:130
void init_impl(const Geometry &geometry)
Definition: Cache.cc:60
virtual const array::Scalar & accumulation_impl() const
Definition: Cache.cc:146
static std::shared_ptr< array::Scalar > allocate_runoff(std::shared_ptr< const Grid > grid)
static std::shared_ptr< array::Scalar > allocate_mass_flux(std::shared_ptr< const Grid > grid)
Definition: SurfaceModel.cc:73
std::shared_ptr< array::Scalar > m_melt
std::shared_ptr< array::Scalar > m_layer_thickness
static std::shared_ptr< array::Scalar > allocate_temperature(std::shared_ptr< const Grid > grid)
Definition: SurfaceModel.cc:92
static std::shared_ptr< array::Scalar > allocate_accumulation(std::shared_ptr< const Grid > grid)
static std::shared_ptr< array::Scalar > allocate_melt(std::shared_ptr< const Grid > grid)
std::shared_ptr< array::Scalar > m_runoff
static std::shared_ptr< array::Scalar > allocate_layer_thickness(std::shared_ptr< const Grid > grid)
Definition: SurfaceModel.cc:47
std::shared_ptr< SurfaceModel > m_input_model
static std::shared_ptr< array::Scalar > allocate_layer_mass(std::shared_ptr< const Grid > grid)
Definition: SurfaceModel.cc:35
std::shared_ptr< array::Scalar > m_layer_mass
std::shared_ptr< array::Scalar > m_accumulation
std::shared_ptr< array::Scalar > m_liquid_water_fraction
static std::shared_ptr< array::Scalar > allocate_liquid_water_fraction(std::shared_ptr< const Grid > grid)
Definition: SurfaceModel.cc:60
The interface of PISM's surface models.
Definition: SurfaceModel.hh:42
#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