PISM, A Parallel Ice Sheet Model 2.3.0-79cae578d committed by Constantine Khrulev on 2026-03-22
Loading...
Searching...
No Matches
Delta_P.cc
Go to the documentation of this file.
1// Copyright (C) 2011--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/Delta_P.hh"
20
21#include "pism/util/Config.hh"
22#include "pism/util/ScalarForcing.hh"
23#include "pism/coupler/util/options.hh"
24#include "pism/util/array/Forcing.hh"
25#include "pism/util/Logger.hh"
26#include "pism/util/io/IO_Flags.hh"
27
28namespace pism {
29namespace atmosphere {
30
31Delta_P::Delta_P(std::shared_ptr<const Grid> grid, std::shared_ptr<AtmosphereModel> in)
32 : AtmosphereModel(grid, std::move(in)) {
33
34 std::string
35 prefix = "atmosphere.delta_P",
36 variable_name = "delta_P",
37 long_name = "precipitation offsets",
38 units = "kg m^-2 second^-1",
39 external_units = "kg m^-2 year^-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_offsets.reset(new ScalarForcing(*grid->ctx(),
52 prefix,
53 variable_name,
54 units, external_units,
55 long_name));
56 } else {
57 auto buffer_size = m_config->get_number("input.forcing.buffer_size");
58
59 m_2d_offsets = std::make_shared<array::Forcing>(m_grid,
60 input,
61 variable_name,
62 "", // no standard name
63 static_cast<unsigned int>(buffer_size),
64 opt.periodic);
65
66 m_2d_offsets->metadata()
67 .long_name(long_name)
68 .units(units)
69 .output_units(external_units);
70 }
71
73}
74
75void Delta_P::init_impl(const Geometry &geometry) {
76 m_input_model->init(geometry);
77
78 m_log->message(2,
79 "* Initializing precipitation forcing using scalar offsets...\n");
80
81 if (m_2d_offsets) {
82 ForcingOptions opt(*m_grid->ctx(), "atmosphere.delta_P");
83 m_2d_offsets->init(opt.filename, opt.periodic);
84 }
85
86}
87
88void Delta_P::init_timeseries_impl(const std::vector<double> &ts) const {
90
91 m_offset_values.resize(ts.size());
92
93 if (m_1d_offsets) {
94 for (unsigned int k = 0; k < ts.size(); ++k) {
95 m_offset_values[k] = m_1d_offsets->value(ts[k]);
96 }
97 }
98
99 if (m_2d_offsets) {
100 m_2d_offsets->init_interpolation(ts);
101 }
102}
103
105 m_input_model->begin_pointwise_access();
106
107 if (m_2d_offsets) {
108 m_2d_offsets->begin_access();
109 }
110}
111
113 m_input_model->end_pointwise_access();
114
115 if (m_2d_offsets) {
116 m_2d_offsets->end_access();
117 }
118}
119
120void Delta_P::update_impl(const Geometry &geometry, double t, double dt) {
121 m_input_model->update(geometry, t, dt);
122 m_precipitation->copy_from(m_input_model->precipitation());
123
124 if (m_1d_offsets) {
125 m_precipitation->shift(m_1d_offsets->value(t + 0.5 * dt));
126 }
127
128 if (m_2d_offsets) {
129 m_2d_offsets->update(t, dt);
130 m_2d_offsets->average(t, dt);
131
132 m_precipitation->add(1.0, *m_2d_offsets);
133 }
134}
135
139
140void Delta_P::precip_time_series_impl(int i, int j, std::vector<double> &result) const {
141 m_input_model->precip_time_series(i, j, result);
142
143 if (m_2d_offsets) {
144 // m_offset_values was resized in init_interpolation and so it should have enough
145 // elements
146 m_2d_offsets->interp(i, j, m_offset_values);
147 } else if (m_1d_offsets) {
148 // empty: m_offset_values were set in init_timeseries_impl()
149 }
150
151 for (size_t k = 0; k < result.size(); ++k) {
152 result[k] += m_offset_values[k];
153 }
154}
155
156} // end of namespace atmosphere
157} // 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
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.
void precip_time_series_impl(int i, int j, std::vector< double > &result) const
Definition Delta_P.cc:140
void init_timeseries_impl(const std::vector< double > &ts) const
Definition Delta_P.cc:88
const array::Scalar & precipitation_impl() const
Definition Delta_P.cc:136
void end_pointwise_access_impl() const
Definition Delta_P.cc:112
void init_impl(const Geometry &geometry)
Definition Delta_P.cc:75
void begin_pointwise_access_impl() const
Definition Delta_P.cc:104
std::shared_ptr< ScalarForcing > m_1d_offsets
Definition Delta_P.hh:49
std::vector< double > m_offset_values
Definition Delta_P.hh:47
std::shared_ptr< array::Forcing > m_2d_offsets
Definition Delta_P.hh:51
void update_impl(const Geometry &geometry, double t, double dt)
Definition Delta_P.cc:120
Delta_P(std::shared_ptr< const Grid > g, std::shared_ptr< AtmosphereModel > in)
Definition Delta_P.cc:31
std::shared_ptr< array::Scalar > m_precipitation
Definition Delta_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