PISM, A Parallel Ice Sheet Model 2.3.0-79cae578d committed by Constantine Khrulev on 2026-03-22
Loading...
Searching...
No Matches
Array3D.cc
Go to the documentation of this file.
1// Copyright (C) 2008--2018, 2020, 2021, 2022, 2023, 2024, 2025, 2026 Ed Bueler and Constantine Khroulev
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 <cstring>
20#include <cstdlib>
21#include <cassert>
22#include <memory>
23#include <petscvec.h>
24
25#include "pism/util/array/Array3D.hh"
26
27#include "pism/util/Config.hh"
28#include "pism/util/Context.hh"
29#include "pism/util/Grid.hh"
30#include "pism/util/VariableMetadata.hh"
31#include "pism/util/array/Array_impl.hh"
32#include "pism/util/array/Scalar.hh"
33#include "pism/util/error_handling.hh"
34#include "pism/util/io/IO_Flags.hh"
35#include "pism/util/io/io_helpers.hh"
36#include "pism/util/InputInterpolation.hh"
37
38namespace pism {
39namespace array {
40
41// this file contains method for derived class array::Array3D
42
43Array3D::Array3D(std::shared_ptr<const Grid> grid, const std::string &name, Kind ghostedp,
44 const std::vector<double> &levels, unsigned int stencil_width)
45 : Array(grid, name, ghostedp, 1, stencil_width, levels) {
47
48 if (levels.empty()) {
51 "pism::array::Array3D '%s' has to have at least one \"vertical\" level", name.c_str());
52 }
53}
54
55//! Set all values of scalar quantity to given a single value in a particular column.
56void Array3D::set_column(int i, int j, double c) {
57 PetscErrorCode ierr;
58#if (Pism_DEBUG == 1)
59 assert(m_array != NULL);
60 check_array_indices(i, j, 0);
61#endif
62
63 double ***arr = (double ***)m_array;
64
65 if (c == 0.0) {
66 ierr = PetscMemzero(arr[j][i], levels().size() * sizeof(double));
67 PISM_CHK(ierr, "PetscMemzero");
68 } else {
69 unsigned int nlevels = levels().size();
70 for (unsigned int k = 0; k < nlevels; k++) {
71 arr[j][i][k] = c;
72 }
73 }
74}
75
76void Array3D::set_column(int i, int j, const double *input) {
77#if (Pism_DEBUG == 1)
78 check_array_indices(i, j, 0);
79#endif
80 double ***arr = (double ***)m_array;
81 PetscErrorCode ierr = PetscMemcpy(arr[j][i], input, levels().size() * sizeof(double));
82 PISM_CHK(ierr, "PetscMemcpy");
83}
84
85#if (Pism_DEBUG == 1)
86static bool legal_level(const std::vector<double> &levels, double z) {
87 double z_min = levels.front();
88 double z_max = levels.back();
89 const double eps = 1.0e-6;
90 return not(z < z_min - eps || z > z_max + eps);
91}
92#endif
93
94//! Return value of scalar quantity at level z (m) above base of ice (by linear interpolation).
95double Array3D::interpolate(int i, int j, double z) const {
96 const auto &zs = levels();
97 auto N = zs.size();
98
99#if (Pism_DEBUG == 1)
100 assert(m_array != NULL);
101 check_array_indices(i, j, 0);
102
103 if (not legal_level(zs, z)) {
105 "Array3D interpolate(): level %f is not legal; name = %s", z,
106 m_impl->name.c_str());
107 }
108#endif
109
110 const auto *column = get_column(i, j);
111
112 if (z >= zs[N - 1]) {
113 return column[N - 1];
114 }
115
116 if (z <= zs[0]) {
117 return column[0];
118 }
119
120 auto mcurr = gsl_interp_accel_find(m_impl->bsearch_accel, zs.data(), N, z);
121
122 const double incr = (z - zs[mcurr]) / (zs[mcurr + 1] - zs[mcurr]);
123 const double valm = column[mcurr];
124 return valm + incr * (column[mcurr + 1] - valm);
125}
126
127double *Array3D::get_column(int i, int j) {
128#if (Pism_DEBUG == 1)
129 check_array_indices(i, j, 0);
130#endif
131 return ((double ***)m_array)[j][i];
132}
133
134const double *Array3D::get_column(int i, int j) const {
135#if (Pism_DEBUG == 1)
136 check_array_indices(i, j, 0);
137#endif
138 return ((double ***)m_array)[j][i];
139}
140
141//! Copies a horizontal slice at level z of an Array3D into `output`.
142void extract_surface(const Array3D &data, double z, Scalar &output) {
143 array::AccessScope list{ &data, &output };
144
145 ParallelSection loop(output.grid()->com);
146 try {
147 for (auto p : output.grid()->points()) {
148 const int i = p.i(), j = p.j();
149 output(i, j) = data.interpolate(i, j, z);
150 }
151 } catch (...) {
152 loop.failed();
153 }
154 loop.check();
155}
156
157
158//! Copies the values of an Array3D at the ice surface (specified by the level `z`) into
159//! `output`.
160void extract_surface(const Array3D &data, const Scalar &z, Scalar &output) {
161 array::AccessScope list{ &data, &output, &z };
162
163 ParallelSection loop(output.grid()->com);
164 try {
165 for (auto p : output.grid()->points()) {
166 const int i = p.i(), j = p.j();
167 output(i, j) = data.interpolate(i, j, z(i, j));
168 }
169 } catch (...) {
170 loop.failed();
171 }
172 loop.check();
173}
174
175/** Sum a 3-D vector in the Z direction to create a 2-D vector.
176
177Note that this sums up all the values in a column, including ones
178above the ice. This may or may not be what you need. Also, take a look
179at IceModel::compute_ice_enthalpy(PetscScalar &result) in iMreport.cc.
180
181As for the difference between array::Array2D and array::Scalar, the
182former can store fields with more than 1 "degree of freedom" per grid
183point (such as 2D fields on the "staggered" grid, with the first
184degree of freedom corresponding to the i-offset and second to
185j-offset).
186
187array::Scalar is just array::Array2D with "dof == 1", and
188array::Vector is array::Array2D with "dof == 2". (Plus some extra
189methods, of course.)
190
191Either one of array::Array2D and array::Scalar would work in this
192case.
193
194Computes output = A*output + B*sum_columns(input) + C
195
196@see https://github.com/pism/pism/issues/229 */
197void sum_columns(const Array3D &data, double A, double B, Scalar &output) {
198 const unsigned int Mz = data.grid()->Mz();
199
200 AccessScope access{ &data, &output };
201
202 for (auto p : data.grid()->points()) {
203 const int i = p.i(), j = p.j();
204
205 const double *column = data.get_column(i, j);
206
207 double scalar_sum = 0.0;
208 for (unsigned int k = 0; k < Mz; ++k) {
209 scalar_sum += column[k];
210 }
211 output(i, j) = A * output(i, j) + B * scalar_sum;
212 }
213}
214
215void Array3D::copy_from(const Array3D &input) {
216 array::AccessScope list{ this, &input };
217
218 assert(levels().size() == input.levels().size());
219 assert(ndof() == input.ndof());
220
221 // 3D arrays have at least one level and ndof() of 1, collections of fields have zero
222 // levels and ndof() > 1
223 auto N = std::max((size_t)ndof(), levels().size());
224
225 ParallelSection loop(m_impl->grid->com);
226 try {
227 for (auto p : m_impl->grid->points()) {
228 const int i = p.i(), j = p.j();
229
230#if PETSC_VERSION_LT(3, 12, 0)
231 PetscMemmove(this->get_column(i, j), const_cast<double *>(input.get_column(i, j)),
232 N * sizeof(double));
233#else
234 PetscArraymove(this->get_column(i, j), input.get_column(i, j), N);
235#endif
236 }
237 } catch (...) {
238 loop.failed();
239 }
240 loop.check();
241
243
245}
246
247std::shared_ptr<Array3D> Array3D::duplicate(Kind ghostedp) const {
248
249 auto result =
250 std::make_shared<Array3D>(this->grid(), this->get_name(), ghostedp, this->levels());
251
252 result->metadata() = this->metadata();
253
254 return result;
255}
256
257void Array3D::regrid_impl(const File &file, io::Default default_value) {
258
259 auto log = grid()->ctx()->log();
260
261 auto variable = metadata(0);
262
263 auto V = file.find_variable(variable.get_name(), variable["standard_name"]);
264
266
267 if (V.exists) {
268 auto interp = grid()->get_interpolation(levels(), file, V.name, m_impl->interpolation_type);
269
270 int last_record = -1;
271 interp->regrid(variable, file, last_record, *grid(), tmp);
272 } else {
273 set_default_value_or_stop(variable, default_value, *log, tmp);
274 }
275
276 if (m_impl->ghosted) {
277 global_to_local(*dm(), tmp, vec());
278 } else {
279 PetscErrorCode ierr = VecCopy(tmp, vec());
280 PISM_CHK(ierr, "VecCopy");
281 }
282}
283
284} // end of namespace array
285} // end of namespace pism
VariableLookupData find_variable(const std::string &short_name, const std::string &std_name) const
Find a variable using its standard name and/or short name.
Definition File.cc:328
std::string name() const
Definition File.cc:274
High-level PISM I/O class.
Definition File.hh:57
void failed()
Indicates a failure of a parallel section.
static RuntimeError formatted(const ErrorLocation &location, const char format[],...) __attribute__((format(printf
build a RuntimeError with a formatted message
Makes sure that we call begin_access() and end_access() for all accessed array::Arrays.
Definition Array.hh:66
void copy_from(const Array3D &input)
Definition Array3D.cc:215
double interpolate(int i, int j, double z) const
Return value of scalar quantity at level z (m) above base of ice (by linear interpolation).
Definition Array3D.cc:95
void set_column(int i, int j, double c)
Set all values of scalar quantity to given a single value in a particular column.
Definition Array3D.cc:56
void regrid_impl(const File &file, io::Default default_value)
Gets an Array from a file file, interpolating onto the current grid.
Definition Array3D.cc:257
Array3D(std::shared_ptr< const Grid > grid, const std::string &name, Kind ghostedp, const std::vector< double > &levels, unsigned int stencil_width=1)
Definition Array3D.cc:43
std::shared_ptr< Array3D > duplicate(Kind ghostedp=WITHOUT_GHOSTS) const
Definition Array3D.cc:247
double * get_column(int i, int j)
Definition Array3D.cc:127
A virtual class collecting methods common to ice and bedrock 3D fields.
Definition Array3D.hh:33
unsigned int ndof() const
Returns the number of degrees of freedom per grid point.
Definition Array.cc:138
const std::vector< double > & levels() const
Definition Array.cc:142
size_t size() const
Return the total number of elements in the owned part of an array.
Definition Array.cc:930
const std::string & get_name() const
Get the name of an Array object.
Definition Array.cc:357
petsc::Vec & vec() const
Definition Array.cc:313
std::shared_ptr< const Grid > grid() const
Definition Array.cc:134
void inc_state_counter()
Increment the object state counter.
Definition Array.cc:153
std::shared_ptr< petsc::DM > dm() const
Definition Array.cc:327
void set_begin_access_use_dof(bool flag)
Definition Array.cc:177
void update_ghosts()
Updates ghost points.
Definition Array.cc:645
VariableMetadata & metadata(unsigned int N=0)
Returns a reference to the VariableMetadata object containing metadata for the compoment N.
Definition Array.cc:467
void check_array_indices(int i, int j, unsigned int k) const
Check array indices and warn if they are out of range.
Definition Array.cc:666
Abstract class for reading, writing, allocating, and accessing a DA-based PETSc Vec (2D and 3D fields...
Definition Array.hh:209
#define PISM_CHK(errcode, name)
#define PISM_ERROR_LOCATION
void extract_surface(const Array3D &data, double z, Scalar &output)
Copies a horizontal slice at level z of an Array3D into output.
Definition Array3D.cc:142
void sum_columns(const Array3D &data, double A, double B, Scalar &output)
Definition Array3D.cc:197
void global_to_local(petsc::DM &dm, Vec source, Vec destination)
Definition Array.cc:56
void set_default_value_or_stop(const VariableMetadata &variable, io::Default default_value, const Logger &log, Vec output)
Definition Array.cc:361
Kind
What "kind" of a vector to create: with or without ghosts.
Definition Array.hh:63
static const double k
Definition exactTestP.cc:42
bool ghosted
true if this Array is ghosted
Definition Array_impl.hh:86
std::shared_ptr< const Grid > grid
The computational grid.
Definition Array_impl.hh:77
InterpolationType interpolation_type
gsl_interp_accel * bsearch_accel