PISM, A Parallel Ice Sheet Model  stable v2.1-1-g6902d5502 committed by Ed Bueler on 2023-12-20 08:38:27 -0800
Delta_T.cc
Go to the documentation of this file.
1 // Copyright (C) 2011--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 #include "pism/coupler/atmosphere/Delta_T.hh"
20 
21 #include "pism/util/ConfigInterface.hh"
22 #include "pism/util/ScalarForcing.hh"
23 #include "pism/coupler/util/options.hh"
24 #include "pism/util/array/Forcing.hh"
25 
26 namespace pism {
27 namespace atmosphere {
28 
29 Delta_T::Delta_T(std::shared_ptr<const Grid> grid, std::shared_ptr<AtmosphereModel> in)
30  : AtmosphereModel(grid, in) {
31 
32  std::string
33  prefix = "atmosphere.delta_T",
34  variable_name = "delta_T",
35  long_name = "near-surface air temperature offsets",
36  units = "Kelvin",
37  external_units = "Kelvin";
38 
39  ForcingOptions opt(*m_grid->ctx(), prefix);
40 
41  // will be closed at the end of scope
43 
44  // Assume that we are expected to use 1D scaling if the input file contains a scalar
45  // time-series.
46  bool scalar = input.dimensions(variable_name).size() == 1;
47 
48  if (scalar) {
49  m_1d_offsets.reset(new ScalarForcing(*grid->ctx(),
50  prefix,
51  variable_name,
52  units, external_units,
53  long_name));
54  } else {
55  unsigned int buffer_size = m_config->get_number("input.forcing.buffer_size");
56 
57  m_2d_offsets = std::make_shared<array::Forcing>(m_grid,
58  input,
59  variable_name,
60  "", // no standard name
61  buffer_size,
62  opt.periodic);
63  m_2d_offsets->metadata()
64  .long_name(long_name)
65  .units(units)
66  .output_units(external_units);
67  }
68 
70 }
71 
72 void Delta_T::init_impl(const Geometry &geometry) {
73  m_input_model->init(geometry);
74 
75  m_log->message(2,
76  "* Initializing near-surface air temperature offsets...\n");
77 
78  if (m_2d_offsets) {
79  ForcingOptions opt(*m_grid->ctx(), "atmosphere.delta_T");
80  m_2d_offsets->init(opt.filename, opt.periodic);
81  }
82 }
83 
84 void Delta_T::init_timeseries_impl(const std::vector<double> &ts) const {
86 
87  m_offset_values.resize(ts.size());
88 
89  if (m_1d_offsets) {
90  for (unsigned int k = 0; k < ts.size(); ++k) {
91  m_offset_values[k] = m_1d_offsets->value(ts[k]);
92  }
93  }
94 
95  if (m_2d_offsets) {
96  m_2d_offsets->init_interpolation(ts);
97  }
98 }
99 
101  m_input_model->begin_pointwise_access();
102 
103  if (m_2d_offsets) {
104  m_2d_offsets->begin_access();
105  }
106 }
107 
109  m_input_model->end_pointwise_access();
110 
111  if (m_2d_offsets) {
112  m_2d_offsets->end_access();
113  }
114 }
115 
116 void Delta_T::update_impl(const Geometry &geometry, double t, double dt) {
117  m_input_model->update(geometry, t, dt);
118  m_temperature->copy_from(m_input_model->air_temperature());
119 
120  if (m_1d_offsets) {
121  m_temperature->shift(m_1d_offsets->value(t + 0.5 * dt));
122  }
123 
124  if (m_2d_offsets) {
125  m_2d_offsets->update(t, dt);
126  m_2d_offsets->average(t, dt);
127 
128  auto &T = *m_temperature;
129  const auto &delta = *m_2d_offsets;
130 
131  array::AccessScope list{&T, &delta};
132 
133  for (auto p = m_grid->points(); p; p.next()) {
134  const int i = p.i(), j = p.j();
135 
136  T(i, j) += delta(i, j);
137  }
138  }
139 }
140 
142  return *m_temperature;
143 }
144 
145 void Delta_T::temp_time_series_impl(int i, int j, std::vector<double> &result) const {
146  m_input_model->temp_time_series(i, j, result);
147 
148  if (m_2d_offsets) {
149  // m_offset_values was resized in init_interpolation and so it should have enough
150  // elements
151  m_2d_offsets->interp(i, j, m_offset_values);
152  } else if (m_1d_offsets) {
153  // empty: m_offset_values were set in init_timeseries_impl()
154  }
155 
156  for (size_t k = 0; k < result.size(); ++k) {
157  result[k] += m_offset_values[k];
158  }
159 }
160 
161 } // end of namespace atmosphere
162 } // end of namespace pism
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
const std::shared_ptr< const Grid > m_grid
grid used by this component
Definition: Component.hh:156
std::vector< std::string > dimensions(const std::string &variable_name) const
Definition: File.cc:425
High-level PISM I/O class.
Definition: File.hh:56
Makes sure that we call begin_access() and end_access() for all accessed array::Arrays.
Definition: Array.hh:65
virtual void init_timeseries_impl(const std::vector< double > &ts) const
static std::shared_ptr< array::Scalar > allocate_temperature(std::shared_ptr< const Grid > grid)
std::shared_ptr< AtmosphereModel > m_input_model
A purely virtual class defining the interface of a PISM Atmosphere Model.
const array::Scalar & air_temperature_impl() const
Definition: Delta_T.cc:141
std::shared_ptr< array::Scalar > m_temperature
Definition: Delta_T.hh:53
void temp_time_series_impl(int i, int j, std::vector< double > &values) const
Definition: Delta_T.cc:145
std::shared_ptr< ScalarForcing > m_1d_offsets
Definition: Delta_T.hh:49
void init_impl(const Geometry &geometry)
Definition: Delta_T.cc:72
void end_pointwise_access_impl() const
Definition: Delta_T.cc:108
void begin_pointwise_access_impl() const
Definition: Delta_T.cc:100
std::shared_ptr< array::Forcing > m_2d_offsets
Definition: Delta_T.hh:51
void update_impl(const Geometry &geometry, double t, double dt)
Definition: Delta_T.cc:116
std::vector< double > m_offset_values
Definition: Delta_T.hh:47
void init_timeseries_impl(const std::vector< double > &ts) const
Definition: Delta_T.cc:84
Delta_T(std::shared_ptr< const Grid > g, std::shared_ptr< AtmosphereModel > in)
Definition: Delta_T.cc:29
@ PISM_GUESS
Definition: IO_Flags.hh:56
@ PISM_READONLY
open an existing file for reading only
Definition: IO_Flags.hh:72
static const double k
Definition: exactTestP.cc:42
std::string filename
Definition: options.hh:33