PISM, A Parallel Ice Sheet Model  stable v2.0.4 committed by Constantine Khrulev on 2022-05-25 12:02:27 -0800
Vector2.hh
Go to the documentation of this file.
1 /* Copyright (C) 2015, 2016, 2020, 2021 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_VECTOR2_HH
21 #define PISM_VECTOR2_HH
22 
23 #include <cmath> // sqrt
24 
25 namespace pism {
26 
27 //! @brief This class represents a 2D vector field (such as ice
28 //! velocity) at a certain grid point.
29 class Vector2 {
30 public:
31  Vector2() : u(0), v(0) {}
32  Vector2(double a, double b) : u(a), v(b) {}
33  Vector2(const Vector2 &other) : u(other.u), v(other.v) {}
34 
35  //! Magnitude squared.
36  inline double magnitude_squared() const {
37  return u*u + v*v;
38  }
39  //! Magnitude.
40  inline double magnitude() const {
41  return sqrt(magnitude_squared());
42  }
43 
44  inline Vector2& operator=(const Vector2 &other) {
45  // NOTE: we don't check for self-assignment because there is no memory
46  // (de-)allocation here.
47  u = other.u;
48  v = other.v;
49  return *this;
50  }
51 
52  //! Set both components to the same number.
53  inline Vector2& operator=(const double &a) {
54  u = a;
55  v = a;
56  return *this;
57  }
58 
59  inline Vector2& operator+=(const Vector2 &other) {
60  u += other.u;
61  v += other.v;
62  return *this;
63  }
64 
65  inline Vector2& operator-=(const Vector2 &other) {
66  u -= other.u;
67  v -= other.v;
68  return *this;
69  }
70 
71  inline Vector2& operator*=(const double &a) {
72  u *= a;
73  v *= a;
74  return *this;
75  }
76 
77  inline Vector2& operator/=(const double &a) {
78  u /= a;
79  v /= a;
80  return *this;
81  }
82 
83  //! \brief Adds two vectors.
84  inline Vector2 operator+(const Vector2 &other) const {
85  return Vector2(u + other.u, v + other.v);
86  }
87 
88  //! \brief Substracts two vectors.
89  inline Vector2 operator-(const Vector2 &other) const {
90  return Vector2(u - other.u, v - other.v);
91  }
92 
93  //! \brief Scales a vector.
94  inline Vector2 operator*(const double &a) const {
95  return Vector2(u * a, v * a);
96  }
97 
98  //! \brief Scales a vector.
99  inline Vector2 operator/(const double &a) const {
100  return Vector2(u / a, v / a);
101  }
102 
103  double u, v;
104 };
105 
106 // Multiplication of a vector by a constant is commutative.
107 inline Vector2 operator*(const double &a, const Vector2 &v) {
108  return v * a;
109 }
110 
111 inline bool operator==(const Vector2 &a, const Vector2 &b) {
112  return a.u == b.u and a.u == b.u;
113 }
114 
115 inline bool operator!=(const Vector2 &a, const Vector2 &b) {
116  return a.u != b.u or a.u != b.u;
117 }
118 
119 } // end of namespace pism
120 
121 #endif /* PISM_VECTOR2_HH */
Vector2 & operator+=(const Vector2 &other)
Definition: Vector2.hh:59
Vector2 operator+(const Vector2 &other) const
Adds two vectors.
Definition: Vector2.hh:84
Vector2 & operator-=(const Vector2 &other)
Definition: Vector2.hh:65
Vector2 operator*(const double &a) const
Scales a vector.
Definition: Vector2.hh:94
double magnitude() const
Magnitude.
Definition: Vector2.hh:40
Vector2 operator-(const Vector2 &other) const
Substracts two vectors.
Definition: Vector2.hh:89
Vector2 & operator=(const double &a)
Set both components to the same number.
Definition: Vector2.hh:53
Vector2 operator/(const double &a) const
Scales a vector.
Definition: Vector2.hh:99
Vector2(const Vector2 &other)
Definition: Vector2.hh:33
double v
Definition: Vector2.hh:103
Vector2 & operator/=(const double &a)
Definition: Vector2.hh:77
Vector2 & operator=(const Vector2 &other)
Definition: Vector2.hh:44
double magnitude_squared() const
Magnitude squared.
Definition: Vector2.hh:36
double u
Definition: Vector2.hh:103
Vector2 & operator*=(const double &a)
Definition: Vector2.hh:71
Vector2(double a, double b)
Definition: Vector2.hh:32
This class represents a 2D vector field (such as ice velocity) at a certain grid point.
Definition: Vector2.hh:29
bool operator!=(const Vector2 &a, const Vector2 &b)
Definition: Vector2.hh:115
bool operator==(const MaxTimestep &a, const MaxTimestep &b)
Equality operator for MaxTimestep.
Definition: MaxTimestep.cc:63
Vector2 operator*(const double &a, const Vector2 &v)
Definition: Vector2.hh:107