PISM, A Parallel Ice Sheet Model  stable v2.1-1-g6902d5502 committed by Ed Bueler on 2023-12-20 08:38:27 -0800
Scalar.hh
Go to the documentation of this file.
1 /* Copyright (C) 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 
20 #ifndef PISM_ARRAY_SCALAR_H
21 #define PISM_ARRAY_SCALAR_H
22 
23 #include <cmath> // floor()
24 
25 #include "pism/util/array/Array2D.hh"
26 
27 namespace pism {
28 namespace array {
29 
30 /** A class for storing and accessing scalar 2D fields. */
31 class Scalar : public Array2D<double> {
32 public:
33  Scalar(std::shared_ptr<const Grid> grid, const std::string &name);
34 
35  std::shared_ptr<Scalar> duplicate() const;
36 
37  inline int as_int(int i, int j) const;
38 
39 protected:
40  inline stencils::Star<int> star_int(int i, int j) const;
41  inline stencils::Box<int> box_int(int i, int j) const;
42  Scalar(std::shared_ptr<const Grid> grid, const std::string &name, int width);
43 };
44 
45 inline int Scalar::as_int(int i, int j) const {
46  const double &value = (*this)(i, j);
47  return static_cast<int>(floor(value + 0.5));
48 }
49 
50 /*!
51  * Scalar 2D array supporting width=1 stencil computations
52  */
53 class Scalar1 : public Scalar {
54 public:
55  Scalar1(std::shared_ptr<const Grid> grid, const std::string &name);
58  using Scalar::star_int;
59  using Scalar::box_int;
60 protected:
61  Scalar1(std::shared_ptr<const Grid> grid, const std::string &name, int width);
62 };
63 
64 /*!
65  * Scalar 2D array supporting width=2 stencil computations
66  */
67 class Scalar2 : public Scalar1 {
68 public:
69  Scalar2(std::shared_ptr<const Grid> grid, const std::string &name);
70 };
71 
72 inline stencils::Star<int> Scalar::star_int(int i, int j) const {
73  stencils::Star<int> result;
74 
75  result.c = as_int(i,j);
76  result.e = as_int(i+1,j);
77  result.w = as_int(i-1,j);
78  result.n = as_int(i,j+1);
79  result.s = as_int(i,j-1);
80 
81  return result;
82 }
83 
84 inline stencils::Box<int> Scalar::box_int(int i, int j) const {
85  const int
86  E = i + 1,
87  W = i - 1,
88  N = j + 1,
89  S = j - 1;
90 
91  return {as_int(i, j), as_int(i, N), as_int(W, N), as_int(W, j), as_int(W, S),
92  as_int(i, S), as_int(E, S), as_int(E, j), as_int(E, N)};
93 }
94 
95 // Finite-difference shortcuts. They may be slower than hard-coding FD approximations of x
96 // and y derivatives. Use with care.
97 double diff_x(const array::Scalar &array, int i, int j);
98 double diff_y(const array::Scalar &array, int i, int j);
99 
100 // These take grid periodicity into account and use one-sided differences at domain edges.
101 double diff_x_p(const array::Scalar &array, int i, int j);
102 double diff_y_p(const array::Scalar &array, int i, int j);
103 
104 double sum(const array::Scalar &input);
105 double min(const array::Scalar &input);
106 double max(const array::Scalar &input);
107 double absmax(const array::Scalar &input);
108 
109 void apply_mask(const array::Scalar &M, double fill, array::Scalar &result);
110 
111 } // end of namespace array
112 
113 } // end of namespace pism
114 
115 #endif /* PISM_ARRAY_SCALAR_H */
A storage vector combining related fields in a struct.
Definition: Array2D.hh:32
std::shared_ptr< const Grid > grid() const
Definition: Array.cc:132
Scalar1(std::shared_ptr< const Grid > grid, const std::string &name)
Definition: Scalar.cc:51
Scalar2(std::shared_ptr< const Grid > grid, const std::string &name)
Definition: Scalar.cc:61
Scalar(std::shared_ptr< const Grid > grid, const std::string &name)
Definition: Scalar.cc:39
int as_int(int i, int j) const
Definition: Scalar.hh:45
stencils::Box< int > box_int(int i, int j) const
Definition: Scalar.hh:84
stencils::Star< int > star_int(int i, int j) const
Definition: Scalar.hh:72
std::shared_ptr< Scalar > duplicate() const
Definition: Scalar.cc:44
void apply_mask(const array::Scalar &M, double fill, array::Scalar &result)
Masks out all the areas where by setting them to fill.
Definition: Scalar.cc:79
double diff_y(const array::Scalar &array, int i, int j)
Returns the y-derivative at i,j approximated using centered finite differences.
Definition: Scalar.cc:101
double max(const array::Scalar &input)
Finds maximum over all the values in an array::Scalar object. Ignores ghosts.
Definition: Scalar.cc:165
double diff_x(const array::Scalar &array, int i, int j)
Returns the x-derivative at i,j approximated using centered finite differences.
Definition: Scalar.cc:95
double sum(const array::Scalar &input)
Sums up all the values in an array::Scalar object. Ignores ghosts.
Definition: Scalar.cc:150
double diff_x_p(const array::Scalar &array, int i, int j)
Returns the x-derivative at i,j approximated using centered finite differences. Respects grid periodi...
Definition: Scalar.cc:108
double min(const array::Scalar &input)
Finds minimum over all the values in an array::Scalar object. Ignores ghosts.
Definition: Scalar.cc:193
double absmax(const array::Scalar &input)
Finds maximum over all the absolute values in an array::Scalar object. Ignores ghosts.
Definition: Scalar.cc:179
double diff_y_p(const array::Scalar &array, int i, int j)
Returns the y-derivative at i,j approximated using centered finite differences. Respects grid periodi...
Definition: Scalar.cc:129
Star stencil points (in the map-plane).
Definition: stencils.hh:30
static double S(unsigned n)
Definition: test_cube.c:58