PISM, A Parallel Ice Sheet Model  stable v2.0.5 committed by Constantine Khrulev on 2022-10-14 09:56:26 -0800
Cache.cc
Go to the documentation of this file.
1 /* Copyright (C) 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021 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 <cassert>
21 #include <algorithm> // for std::min
22 
23 #include "Cache.hh"
24 #include "pism/util/Time.hh"
25 #include "pism/util/pism_options.hh"
26 #include "pism/util/IceGrid.hh"
27 #include "pism/util/error_handling.hh"
28 #include "pism/util/pism_utilities.hh"
29 #include "pism/util/MaxTimestep.hh"
30 #include "pism/util/Context.hh"
31 
32 namespace pism {
33 namespace surface {
34 
35 Cache::Cache(IceGrid::ConstPtr grid, std::shared_ptr<SurfaceModel> in)
36  : SurfaceModel(grid, in) {
37 
38  auto time = m_grid->ctx()->time();
39 
40  m_next_update_time = time->current();
41  m_update_interval_years = m_config->get_number("surface.cache.update_interval", "seconds");
42 
43  // use the current year length (according to the selected calendar) to convert update
44  // interval length into years
45  m_update_interval_years = time->convert_time_interval(m_update_interval_years, "years");
46 
47  if (m_update_interval_years <= 0) {
49  "surface.cache.update_interval has to be strictly positive.");
50  }
51 
52  {
61  }
62 }
63 
64 void Cache::init_impl(const Geometry &geometry) {
65  m_input_model->init(geometry);
66 
67  m_log->message(2, "* Initializing the 'caching' surface model modifier...\n");
68 
69  m_next_update_time = m_grid->ctx()->time()->current();
70 }
71 
72 void Cache::update_impl(const Geometry &geometry, double t, double dt) {
73  // ignore dt and always use 1 year long time-steps when updating
74  // the input model
75  (void) dt;
76 
77  double time_resolution = m_config->get_number("time_stepping.resolution", "seconds");
78  if (t >= m_next_update_time or fabs(t - m_next_update_time) < time_resolution) {
79 
80  double
81  one_year_from_now = m_grid->ctx()->time()->increment_date(t, 1.0),
82  update_dt = one_year_from_now - t;
83 
84  assert(update_dt > 0.0);
85 
86  m_input_model->update(geometry, t, update_dt);
87 
88  m_next_update_time = m_grid->ctx()->time()->increment_date(m_next_update_time,
90 
91  // store outputs of the input model
92  m_mass_flux->copy_from(m_input_model->mass_flux());
93  m_temperature->copy_from(m_input_model->temperature());
94  m_liquid_water_fraction->copy_from(m_input_model->liquid_water_fraction());
95  m_layer_mass->copy_from(m_input_model->layer_mass());
96  m_layer_thickness->copy_from(m_input_model->layer_thickness());
97  m_accumulation->copy_from(m_input_model->accumulation());
98  m_melt->copy_from(m_input_model->melt());
99  m_runoff->copy_from(m_input_model->runoff());
100  }
101 }
102 
104  double dt = m_next_update_time - t;
105 
106  double time_resolution = m_config->get_number("time_stepping.resolution", "seconds");
107 
108  // if we got very close to the next update time, set time step
109  // length to the interval between updates
110  if (dt < time_resolution) {
111  double update_time_after_next = m_grid->ctx()->time()->increment_date(m_next_update_time,
113 
114  dt = update_time_after_next - m_next_update_time;
115  assert(dt > 0.0);
116  }
117 
118  assert(m_input_model != NULL);
119 
120  MaxTimestep cache_dt(dt, "surface cache");
121 
122  MaxTimestep input_max_timestep = m_input_model->max_timestep(t);
123  if (input_max_timestep.finite()) {
124  return std::min(input_max_timestep, cache_dt);
125  } else {
126  return cache_dt;
127  }
128 }
129 
131  return *m_layer_thickness;
132 }
133 
135  return *m_mass_flux;
136 }
137 
139  return *m_temperature;
140 }
141 
143  return *m_liquid_water_fraction;
144 }
145 
147  return *m_layer_mass;
148 }
149 
151  return *m_accumulation;
152 }
153 
155  return *m_melt;
156 }
157 
159  return *m_runoff;
160 }
161 
162 } // end of namespace surface
163 } // end of namespace pism
IceGrid::ConstPtr grid() const
Definition: Component.cc:105
const Config::ConstPtr m_config
configuration database used by this component
Definition: Component.hh:138
const Logger::ConstPtr m_log
logger (for easy access)
Definition: Component.hh:142
const IceGrid::ConstPtr m_grid
grid used by this component
Definition: Component.hh:136
std::shared_ptr< const IceGrid > ConstPtr
Definition: IceGrid.hh:233
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
const IceModelVec2S & liquid_water_fraction_impl() const
Definition: Cache.cc:142
const IceModelVec2S & layer_thickness_impl() const
Definition: Cache.cc:130
void update_impl(const Geometry &geometry, double t, double dt)
Definition: Cache.cc:72
MaxTimestep max_timestep_impl(double t) const
Definition: Cache.cc:103
virtual const IceModelVec2S & melt_impl() const
Definition: Cache.cc:154
Cache(IceGrid::ConstPtr g, std::shared_ptr< SurfaceModel > in)
Definition: Cache.cc:35
const IceModelVec2S & mass_flux_impl() const
Definition: Cache.cc:134
double m_update_interval_years
Definition: Cache.hh:53
IceModelVec2S::Ptr m_mass_flux
Definition: Cache.hh:49
virtual const IceModelVec2S & runoff_impl() const
Definition: Cache.cc:158
IceModelVec2S::Ptr m_temperature
Definition: Cache.hh:50
double m_next_update_time
Definition: Cache.hh:52
const IceModelVec2S & layer_mass_impl() const
Definition: Cache.cc:146
void init_impl(const Geometry &geometry)
Definition: Cache.cc:64
const IceModelVec2S & temperature_impl() const
Definition: Cache.cc:138
virtual const IceModelVec2S & accumulation_impl() const
Definition: Cache.cc:150
static IceModelVec2S::Ptr allocate_mass_flux(IceGrid::ConstPtr grid)
Definition: SurfaceModel.cc:76
IceModelVec2S::Ptr m_liquid_water_fraction
static IceModelVec2S::Ptr allocate_liquid_water_fraction(IceGrid::ConstPtr grid)
Definition: SurfaceModel.cc:62
IceModelVec2S::Ptr m_melt
static IceModelVec2S::Ptr allocate_melt(IceGrid::ConstPtr grid)
IceModelVec2S::Ptr m_layer_mass
IceModelVec2S::Ptr m_accumulation
static IceModelVec2S::Ptr allocate_temperature(IceGrid::ConstPtr grid)
Definition: SurfaceModel.cc:93
std::shared_ptr< SurfaceModel > m_input_model
static IceModelVec2S::Ptr allocate_accumulation(IceGrid::ConstPtr grid)
static IceModelVec2S::Ptr allocate_layer_mass(IceGrid::ConstPtr grid)
Definition: SurfaceModel.cc:38
static IceModelVec2S::Ptr allocate_runoff(IceGrid::ConstPtr grid)
IceModelVec2S::Ptr m_runoff
IceModelVec2S::Ptr m_layer_thickness
static IceModelVec2S::Ptr allocate_layer_thickness(IceGrid::ConstPtr grid)
Definition: SurfaceModel.cc:49
The interface of PISM's surface models.
Definition: SurfaceModel.hh:45
#define PISM_ERROR_LOCATION
double min(const IceModelVec2S &input)
Finds minimum over all the values in an IceModelVec2S object. Ignores ghosts.