PISM, A Parallel Ice Sheet Model 2.3.0-79cae578d committed by Constantine Khrulev on 2026-03-22
Loading...
Searching...
No Matches
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, 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#include "pism/coupler/atmosphere/Frac_P.hh"
20
21#include "pism/util/Config.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#include "pism/util/Logger.hh"
27#include "pism/util/io/IO_Flags.hh"
28
29namespace pism {
30namespace atmosphere {
31
32Frac_P::Frac_P(std::shared_ptr<const Grid> grid, std::shared_ptr<AtmosphereModel> in)
33 : AtmosphereModel(grid, in) {
34
35 std::string
36 prefix = "atmosphere.frac_P",
37 variable_name = "frac_P",
38 long_name = "precipitation multiplier, pure fraction",
39 units = "1";
40
41 ForcingOptions opt(*m_grid->ctx(), prefix);
42
43 // will be closed at the end of scope
45
46 // Assume that we are expected to use 1D scaling if the input file contains a scalar
47 // time-series.
48 bool scalar = input.dimensions(variable_name).size() == 1;
49
50 if (scalar) {
51 m_1d_scaling.reset(new ScalarForcing(*grid->ctx(),
52 prefix,
53 variable_name,
54 units, units,
55 long_name));
56 } else {
57 unsigned int buffer_size = m_config->get_number("input.forcing.buffer_size");
58
59 m_2d_scaling = std::make_shared<array::Forcing>(m_grid,
60 input,
61 variable_name,
62 "", // no standard name
63 buffer_size,
64 opt.periodic);
65
66 m_2d_scaling->metadata().long_name(long_name).units(units);
67 }
68
70}
71
72void Frac_P::init_impl(const Geometry &geometry) {
73 m_input_model->init(geometry);
74
75 m_log->message(2, "* Initializing precipitation forcing using scalar multipliers...\n");
76
77 if (m_2d_scaling) {
78 ForcingOptions opt(*m_grid->ctx(), "atmosphere.frac_P");
79 m_2d_scaling->init(opt.filename, opt.periodic);
80 }
81}
82
83void Frac_P::init_timeseries_impl(const std::vector<double> &ts) const {
85
86 m_scaling_values.resize(ts.size());
87
88 if (m_1d_scaling) {
89 for (unsigned int k = 0; k < ts.size(); ++k) {
90 m_scaling_values[k] = m_1d_scaling->value(ts[k]);
91 }
92 }
93
94 if (m_2d_scaling) {
95 m_2d_scaling->init_interpolation(ts);
96 }
97}
98
100 m_input_model->begin_pointwise_access();
101
102 if (m_2d_scaling) {
103 m_2d_scaling->begin_access();
104 }
105}
106
108 m_input_model->end_pointwise_access();
109
110 if (m_2d_scaling) {
111 m_2d_scaling->end_access();
112 }
113}
114
115void Frac_P::update_impl(const Geometry &geometry, double t, double dt) {
116 m_input_model->update(geometry, t, dt);
117 m_precipitation->copy_from(m_input_model->precipitation());
118
119 if (m_1d_scaling) {
120 m_precipitation->scale(m_1d_scaling->value(t + 0.5 * dt));
121 }
122
123 if (m_2d_scaling) {
124 m_2d_scaling->update(t, dt);
125 m_2d_scaling->average(t, dt);
126
129
130 array::AccessScope list{&P, &S};
131
132 for (auto p : m_grid->points()) {
133 const int i = p.i(), j = p.j();
134
135 P(i, j) *= S(i, j);
136 }
137 }
138}
139
143
144void Frac_P::precip_time_series_impl(int i, int j, std::vector<double> &result) const {
145 m_input_model->precip_time_series(i, j, result);
146
147 if (m_2d_scaling) {
148 // m_scaling_values was resized in init_interpolation and so it should have enough
149 // elements
150 m_2d_scaling->interp(i, j, m_scaling_values);
151 } else if (m_1d_scaling) {
152 // empty: m_scaling_values were set in init_timeseries_impl()
153 }
154
155 for (size_t k = 0; k < result.size(); ++k) {
156 result[k] *= m_scaling_values[k];
157 }
158}
159
160} // end of namespace atmosphere
161} // end of namespace pism
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
const std::shared_ptr< const Grid > m_grid
grid used by this component
Definition Component.hh:158
std::shared_ptr< const Logger > m_log
logger (for easy access)
Definition Component.hh:164
std::vector< std::string > dimensions(const std::string &variable_name) const
Definition File.cc:390
High-level PISM I/O class.
Definition File.hh:57
Makes sure that we call begin_access() and end_access() for all accessed array::Arrays.
Definition Array.hh:66
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:32
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:144
void begin_pointwise_access_impl() const
Definition Frac_P.cc:99
void update_impl(const Geometry &geometry, double t, double dt)
Definition Frac_P.cc:115
std::shared_ptr< ScalarForcing > m_1d_scaling
Definition Frac_P.hh:49
void init_impl(const Geometry &geometry)
Definition Frac_P.cc:72
std::vector< double > m_scaling_values
Definition Frac_P.hh:47
const array::Scalar & precipitation_impl() const
Definition Frac_P.cc:140
void init_timeseries_impl(const std::vector< double > &ts) const
Definition Frac_P.cc:83
void end_pointwise_access_impl() const
Definition Frac_P.cc:107
std::shared_ptr< array::Scalar > m_precipitation
Definition Frac_P.hh:53
@ PISM_GUESS
Definition IO_Flags.hh:57
@ PISM_READONLY
open an existing file for reading only
Definition IO_Flags.hh:69
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