PISM, A Parallel Ice Sheet Model 2.3.0-79cae578d committed by Constantine Khrulev on 2026-03-22
Loading...
Searching...
No Matches
Array2D.hh
Go to the documentation of this file.
1/* Copyright (C) 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#ifndef PISM_ARRAY2D_H
20#define PISM_ARRAY2D_H
21
22#include "pism/util/array/Array.hh"
23#include "pism/util/array/Array_helpers.hh"
24#include "pism/util/stencils.hh"
25
26namespace pism {
27
28namespace array {
29
30//! A storage vector combining related fields in a struct
31template<typename T>
32class Array2D : public Array {
33public:
34 using value_type = T;
35
36 Array2D(std::shared_ptr<const Grid> grid, const std::string &short_name,
37 Kind ghostedp, unsigned int stencil_width = 1)
38 : Array(grid, short_name, ghostedp,
39 sizeof(T) / sizeof(double), stencil_width, {}) {
41 }
42
43 T** array() {
44 return reinterpret_cast<T**>(m_array);
45 }
46
47 T const* const* array() const {
48 return reinterpret_cast<T const* const*>(m_array);
49 }
50
51 inline T& operator()(int i, int j) {
52#if (Pism_DEBUG==1)
53 check_array_indices(i, j, 0);
54#endif
55 return static_cast<T**>(m_array)[j][i];
56 }
57
58 inline const T& operator()(int i, int j) const {
59#if (Pism_DEBUG==1)
60 check_array_indices(i, j, 0);
61#endif
62 return static_cast<T**>(m_array)[j][i];
63 }
64
65 /*!
66 * Value at the north neighbor of (i, j)
67 */
68 inline const T& N(int i, int j) const {
69 return (*this)(i, j + 1);
70 }
71
72 /*!
73 * Value at the east neighbor of (i, j)
74 */
75 inline const T& E(int i, int j) const {
76 return (*this)(i + 1, j);
77 }
78
79 /*!
80 * Value at the south neighbor of (i, j)
81 */
82 inline const T& S(int i, int j) const {
83 return (*this)(i, j - 1);
84 }
85
86 /*!
87 * Value at the west neighbor of (i, j)
88 */
89 inline const T& W(int i, int j) const {
90 return (*this)(i - 1, j);
91 }
92
93 void add(double alpha, const Array2D<T> &x) {
94 details::add(*this, alpha, x, *this);
95 }
96
97 void add(double alpha, const Array2D<T> &x, Array2D<T> &result) const {
98 details::add(*this, alpha, x, result);
99 }
100
101 void copy_from(const Array2D<T> &source) {
102 return details::copy(source, *this);
103 }
104
105protected:
106
107 inline stencils::Star<T> star(int i, int j) const {
108 const auto &self = *this;
109
110 stencils::Star<T> result;
111
112 result.c = self(i,j);
113 result.e = E(i,j);
114 result.w = W(i,j);
115 result.n = N(i,j);
116 result.s = S(i,j);
117
118 return result;
119 }
120
121 inline stencils::Box<T> box(int i, int j) const {
122 const auto &x = *this;
123
124 const int
125 E = i + 1,
126 W = i - 1,
127 N = j + 1,
128 S = j - 1;
129
130 return {x(i, j), x(i, N), x(W, N), x(W, j), x(W, S),
131 x(i, S), x(E, S), x(E, j), x(E, N)};
132 }
133};
134
135} // end of namespace array
136} // end of namespace pism
137
138#endif /* PISM_ARRAY2D_H */
T const *const * array() const
Definition Array2D.hh:47
const T & S(int i, int j) const
Definition Array2D.hh:82
const T & operator()(int i, int j) const
Definition Array2D.hh:58
Array2D(std::shared_ptr< const Grid > grid, const std::string &short_name, Kind ghostedp, unsigned int stencil_width=1)
Definition Array2D.hh:36
const T & W(int i, int j) const
Definition Array2D.hh:89
T & operator()(int i, int j)
Definition Array2D.hh:51
const T & N(int i, int j) const
Definition Array2D.hh:68
void copy_from(const Array2D< T > &source)
Definition Array2D.hh:101
stencils::Star< T > star(int i, int j) const
Definition Array2D.hh:107
void add(double alpha, const Array2D< T > &x, Array2D< T > &result) const
Definition Array2D.hh:97
const T & E(int i, int j) const
Definition Array2D.hh:75
stencils::Box< T > box(int i, int j) const
Definition Array2D.hh:121
void add(double alpha, const Array2D< T > &x)
Definition Array2D.hh:93
A storage vector combining related fields in a struct.
Definition Array2D.hh:32
std::shared_ptr< const Grid > grid() const
Definition Array.cc:134
void set_begin_access_use_dof(bool flag)
Definition Array.cc:177
unsigned int stencil_width() const
Get the stencil width of the current Array. Returns 0 if ghosts are not available.
Definition Array.cc:305
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
void add(const V &x, double alpha, const V &y, V &result, bool scatter=true)
Computes result = x + alpha * y, where x, y, and z are 2D Arrays (scalar or vector).
void copy(const V &input, V &result, bool scatter=true)
Kind
What "kind" of a vector to create: with or without ghosts.
Definition Array.hh:63
Star stencil points (in the map-plane).
Definition stencils.hh:30