PISM, A Parallel Ice Sheet Model  stable v2.1-1-g6902d5502 committed by Ed Bueler on 2023-12-20 08:38:27 -0800
Array2D.hh
Go to the documentation of this file.
1 /* Copyright (C) 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 #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 
26 namespace pism {
27 
28 namespace array {
29 
30 //! A storage vector combining related fields in a struct
31 template<typename T>
32 class Array2D : public Array {
33 public:
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, {0.0}) {
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  void add(double alpha, const Array2D<T> &x) {
66  details::add(*this, alpha, x, *this);
67  }
68 
69  void add(double alpha, const Array2D<T> &x, Array2D<T> &result) const {
70  details::add(*this, alpha, x, result);
71  }
72 
73  void copy_from(const Array2D<T> &source) {
74  return details::copy(source, *this);
75  }
76 
77 protected:
78 
79  inline stencils::Star<T> star(int i, int j) const {
80  const auto &self = *this;
81 
82  stencils::Star<T> result;
83 
84  result.c = self(i,j);
85  result.e = self(i+1,j);
86  result.w = self(i-1,j);
87  result.n = self(i,j+1);
88  result.s = self(i,j-1);
89 
90  return result;
91  }
92 
93  inline stencils::Box<T> box(int i, int j) const {
94  const auto &x = *this;
95 
96  const int
97  E = i + 1,
98  W = i - 1,
99  N = j + 1,
100  S = j - 1;
101 
102  return {x(i, j), x(i, N), x(W, N), x(W, j), x(W, S),
103  x(i, S), x(E, S), x(E, j), x(E, N)};
104  }
105 };
106 
107 } // end of namespace array
108 } // end of namespace pism
109 
110 #endif /* PISM_ARRAY2D_H */
T const *const * array() const
Definition: Array2D.hh:47
const T & operator()(int i, int j) const
Definition: Array2D.hh:58
stencils::Box< T > box(int i, int j) const
Definition: Array2D.hh:93
Array2D(std::shared_ptr< const Grid > grid, const std::string &short_name, Kind ghostedp, unsigned int stencil_width=1)
Definition: Array2D.hh:36
void copy_from(const Array2D< T > &source)
Definition: Array2D.hh:73
stencils::Star< T > star(int i, int j) const
Definition: Array2D.hh:79
T & operator()(int i, int j)
Definition: Array2D.hh:51
void add(double alpha, const Array2D< T > &x, Array2D< T > &result) const
Definition: Array2D.hh:69
void add(double alpha, const Array2D< T > &x)
Definition: Array2D.hh:65
A storage vector combining related fields in a struct.
Definition: Array2D.hh:32
void * m_array
Definition: Array.hh:278
std::shared_ptr< const Grid > grid() const
Definition: Array.cc:132
void set_begin_access_use_dof(bool flag)
Definition: Array.cc:175
unsigned int stencil_width() const
Get the stencil width of the current Array. Returns 0 if ghosts are not available.
Definition: Array.cc:331
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:714
Abstract class for reading, writing, allocating, and accessing a DA-based PETSc Vec (2D and 3D fields...
Definition: Array.hh:208
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:62
Star stencil points (in the map-plane).
Definition: stencils.hh:30
static double S(unsigned n)
Definition: test_cube.c:58