Loading [MathJax]/extensions/tex2jax.js
PISM, A Parallel Ice Sheet Model 2.2.2-d6b3a29ca committed by Constantine Khrulev on 2025-03-28
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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
27namespace pism {
28namespace array {
29
30/** A class for storing and accessing scalar 2D fields. */
31class Scalar : public Array2D<double> {
32public:
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
39protected:
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
45inline 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 */
53class Scalar1 : public Scalar {
54public:
55 Scalar1(std::shared_ptr<const Grid> grid, const std::string &name);
56 using Array2D<double>::star;
57 using Array2D<double>::box;
58 using Scalar::star_int;
59 using Scalar::box_int;
60protected:
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 */
67class Scalar2 : public Scalar1 {
68public:
69 Scalar2(std::shared_ptr<const Grid> grid, const std::string &name);
70};
71
72inline stencils::Star<int> Scalar::star_int(int i, int j) const {
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
84inline 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.
97double diff_x(const array::Scalar &array, int i, int j);
98double 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.
101double diff_x_p(const array::Scalar &array, int i, int j);
102double diff_y_p(const array::Scalar &array, int i, int j);
103
104double sum(const array::Scalar &input);
105double min(const array::Scalar &input);
106double max(const array::Scalar &input);
107double absmax(const array::Scalar &input);
108
109void 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 */
stencils::Star< double > star(int i, int j) const
Definition Array2D.hh:79
stencils::Box< double > box(int i, int j) const
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:131
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