PISM, A Parallel Ice Sheet Model  stable v2.1-1-g6902d5502 committed by Ed Bueler on 2023-12-20 08:38:27 -0800
Frac_P.cc
Go to the documentation of this file.
1 // Copyright (C) 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 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 #include "pism/coupler/atmosphere/Frac_P.hh"
20 
21 #include "pism/util/ConfigInterface.hh"
22 #include "pism/util/ScalarForcing.hh"
23 #include "pism/util/io/File.hh"
24 #include "pism/coupler/util/options.hh"
25 #include "pism/util/array/Forcing.hh"
26 
27 namespace pism {
28 namespace atmosphere {
29 
30 Frac_P::Frac_P(std::shared_ptr<const Grid> grid, std::shared_ptr<AtmosphereModel> in)
31  : AtmosphereModel(grid, in) {
32 
33  std::string
34  prefix = "atmosphere.frac_P",
35  variable_name = "frac_P",
36  long_name = "precipitation multiplier, pure fraction",
37  units = "1";
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_scaling.reset(new ScalarForcing(*grid->ctx(),
50  prefix,
51  variable_name,
52  units, units,
53  long_name));
54  } else {
55  unsigned int buffer_size = m_config->get_number("input.forcing.buffer_size");
56 
57  m_2d_scaling = std::make_shared<array::Forcing>(m_grid,
58  input,
59  variable_name,
60  "", // no standard name
61  buffer_size,
62  opt.periodic);
63 
64  m_2d_scaling->metadata().long_name(long_name).units(units);
65  }
66 
68 }
69 
70 void Frac_P::init_impl(const Geometry &geometry) {
71  m_input_model->init(geometry);
72 
73  m_log->message(2, "* Initializing precipitation forcing using scalar multipliers...\n");
74 
75  if (m_2d_scaling) {
76  ForcingOptions opt(*m_grid->ctx(), "atmosphere.frac_P");
77  m_2d_scaling->init(opt.filename, opt.periodic);
78  }
79 }
80 
81 void Frac_P::init_timeseries_impl(const std::vector<double> &ts) const {
83 
84  m_scaling_values.resize(ts.size());
85 
86  if (m_1d_scaling) {
87  for (unsigned int k = 0; k < ts.size(); ++k) {
88  m_scaling_values[k] = m_1d_scaling->value(ts[k]);
89  }
90  }
91 
92  if (m_2d_scaling) {
93  m_2d_scaling->init_interpolation(ts);
94  }
95 }
96 
98  m_input_model->begin_pointwise_access();
99 
100  if (m_2d_scaling) {
101  m_2d_scaling->begin_access();
102  }
103 }
104 
106  m_input_model->end_pointwise_access();
107 
108  if (m_2d_scaling) {
109  m_2d_scaling->end_access();
110  }
111 }
112 
113 void Frac_P::update_impl(const Geometry &geometry, double t, double dt) {
114  m_input_model->update(geometry, t, dt);
115  m_precipitation->copy_from(m_input_model->precipitation());
116 
117  if (m_1d_scaling) {
118  m_precipitation->scale(m_1d_scaling->value(t + 0.5 * dt));
119  }
120 
121  if (m_2d_scaling) {
122  m_2d_scaling->update(t, dt);
123  m_2d_scaling->average(t, dt);
124 
127 
128  array::AccessScope list{&P, &S};
129 
130  for (auto p = m_grid->points(); p; p.next()) {
131  const int i = p.i(), j = p.j();
132 
133  P(i, j) *= S(i, j);
134  }
135  }
136 }
137 
139  return *m_precipitation;
140 }
141 
142 void Frac_P::precip_time_series_impl(int i, int j, std::vector<double> &result) const {
143  m_input_model->precip_time_series(i, j, result);
144 
145  if (m_2d_scaling) {
146  // m_scaling_values was resized in init_interpolation and so it should have enough
147  // elements
148  m_2d_scaling->interp(i, j, m_scaling_values);
149  } else if (m_1d_scaling) {
150  // empty: m_scaling_values were set in init_timeseries_impl()
151  }
152 
153  for (size_t k = 0; k < result.size(); ++k) {
154  result[k] *= m_scaling_values[k];
155  }
156 }
157 
158 } // end of namespace atmosphere
159 } // 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
2D time-dependent inputs (for climate forcing, etc)
Definition: Forcing.hh:41
virtual void init_timeseries_impl(const std::vector< double > &ts) const
std::shared_ptr< AtmosphereModel > m_input_model
static std::shared_ptr< array::Scalar > allocate_precipitation(std::shared_ptr< const Grid > grid)
A purely virtual class defining the interface of a PISM Atmosphere Model.
Frac_P(std::shared_ptr< const Grid > g, std::shared_ptr< AtmosphereModel > in)
Definition: Frac_P.cc:30
std::shared_ptr< array::Forcing > m_2d_scaling
Definition: Frac_P.hh:51
void precip_time_series_impl(int i, int j, std::vector< double > &values) const
Definition: Frac_P.cc:142
void begin_pointwise_access_impl() const
Definition: Frac_P.cc:97
void update_impl(const Geometry &geometry, double t, double dt)
Definition: Frac_P.cc:113
std::shared_ptr< ScalarForcing > m_1d_scaling
Definition: Frac_P.hh:49
void init_impl(const Geometry &geometry)
Definition: Frac_P.cc:70
std::vector< double > m_scaling_values
Definition: Frac_P.hh:47
const array::Scalar & precipitation_impl() const
Definition: Frac_P.cc:138
void init_timeseries_impl(const std::vector< double > &ts) const
Definition: Frac_P.cc:81
void end_pointwise_access_impl() const
Definition: Frac_P.cc:105
std::shared_ptr< array::Scalar > m_precipitation
Definition: Frac_P.hh:53
@ 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
static double S(unsigned n)
Definition: test_cube.c:58