PISM, A Parallel Ice Sheet Model 2.3.0-79cae578d committed by Constantine Khrulev on 2026-03-22
Loading...
Searching...
No Matches
Cache.cc
Go to the documentation of this file.
1/* Copyright (C) 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023, 2025 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#include "pism/util/Logger.hh"
30
31namespace pism {
32namespace surface {
33
34Cache::Cache(std::shared_ptr<const Grid> grid, std::shared_ptr<SurfaceModel> in)
35 : SurfaceModel(grid, in) {
36
38 m_update_interval_years = m_config->get_number("surface.cache.update_interval", "seconds");
39
40 // use the current year length (according to the selected calendar) to convert update
41 // interval length into years
43
44 if (m_update_interval_years <= 0) {
46 "surface.cache.update_interval has to be strictly positive.");
47 }
48
49 {
58 }
59}
60
61void Cache::init_impl(const Geometry &geometry) {
62 m_input_model->init(geometry);
63
64 m_log->message(2, "* Initializing the 'caching' surface model modifier...\n");
65
67}
68
69void Cache::update_impl(const Geometry &geometry, double t, double dt) {
70 // ignore dt and always use 1 year long time-steps when updating
71 // the input model
72 (void) dt;
73
74 double time_resolution = m_config->get_number("time_stepping.resolution", "seconds");
75 if (t >= m_next_update_time or fabs(t - m_next_update_time) < time_resolution) {
76
77 double
78 one_year_from_now = time().increment_date(t, 1.0),
79 update_dt = one_year_from_now - t;
80
81 assert(update_dt > 0.0);
82
83 m_input_model->update(geometry, t, update_dt);
84
87
88 // store outputs of the input model
89 m_mass_flux->copy_from(m_input_model->mass_flux());
90 m_temperature->copy_from(m_input_model->temperature());
91 m_liquid_water_fraction->copy_from(m_input_model->liquid_water_fraction());
92 m_layer_mass->copy_from(m_input_model->layer_mass());
93 m_layer_thickness->copy_from(m_input_model->layer_thickness());
94 m_accumulation->copy_from(m_input_model->accumulation());
95 m_melt->copy_from(m_input_model->melt());
96 m_runoff->copy_from(m_input_model->runoff());
97 }
98}
99
101 double dt = m_next_update_time - t;
102
103 double time_resolution = m_config->get_number("time_stepping.resolution", "seconds");
104
105 // if we got very close to the next update time, set time step
106 // length to the interval between updates
107 if (dt < time_resolution) {
108 double update_time_after_next = time().increment_date(m_next_update_time,
110
111 dt = update_time_after_next - m_next_update_time;
112 assert(dt > 0.0);
113 }
114
115 assert(m_input_model != NULL);
116
117 MaxTimestep cache_dt(dt, "surface cache");
118
119 MaxTimestep input_max_timestep = m_input_model->max_timestep(t);
120 if (input_max_timestep.finite()) {
121 return std::min(input_max_timestep, cache_dt);
122 }
123
124 return cache_dt;
125}
126
130
132 return *m_mass_flux;
133}
134
136 return *m_temperature;
137}
138
142
144 return *m_layer_mass;
145}
146
148 return *m_accumulation;
149}
150
152 return *m_melt;
153}
154
156 return *m_runoff;
157}
158
159} // end of namespace surface
160} // end of namespace pism
const Time & time() const
Definition Component.cc:111
std::shared_ptr< const Grid > grid() const
Definition Component.cc:107
std::shared_ptr< const Config > m_config
configuration database used by this component
Definition Component.hh:160
std::shared_ptr< const Logger > m_log
logger (for easy access)
Definition Component.hh:164
bool finite() const
Convert to bool to check if a time step restriction is "active".
Combines the max. time step with the flag indicating if a restriction is active. Makes is possible to...
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:867
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:694
double current() const
Current time, in seconds.
Definition Time.cc:461
virtual const array::Scalar & runoff_impl() const
Definition Cache.cc:155
std::shared_ptr< array::Scalar > m_temperature
Definition Cache.hh:50
const array::Scalar & layer_thickness_impl() const
Definition Cache.cc:127
void update_impl(const Geometry &geometry, double t, double dt)
Definition Cache.cc:69
MaxTimestep max_timestep_impl(double t) const
Definition Cache.cc:100
const array::Scalar & liquid_water_fraction_impl() const
Definition Cache.cc:139
const array::Scalar & layer_mass_impl() const
Definition Cache.cc:143
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:34
const array::Scalar & temperature_impl() const
Definition Cache.cc:135
double m_next_update_time
Definition Cache.hh:52
virtual const array::Scalar & melt_impl() const
Definition Cache.cc:151
const array::Scalar & mass_flux_impl() const
Definition Cache.cc:131
void init_impl(const Geometry &geometry)
Definition Cache.cc:61
virtual const array::Scalar & accumulation_impl() const
Definition Cache.cc:147
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)
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)
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)
std::shared_ptr< SurfaceModel > m_input_model
static std::shared_ptr< array::Scalar > allocate_layer_mass(std::shared_ptr< const Grid > grid)
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)
The interface of PISM's surface models.
#define PISM_ERROR_LOCATION