PISM, A Parallel Ice Sheet Model  stable v2.1-1-g6902d5502 committed by Ed Bueler on 2023-12-20 08:38:27 -0800
Vector2d.hh
Go to the documentation of this file.
1 /* Copyright (C) 2015, 2016, 2020, 2021, 2022 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_VECTOR2D_HH
21 #define PISM_VECTOR2D_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 Vector2d {
30 public:
31  Vector2d() : u(0), v(0) {}
32  Vector2d(double a, double b) : u(a), v(b) {}
33  Vector2d(const Vector2d &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 Vector2d& operator=(const Vector2d &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 Vector2d& operator=(const double &a) {
54  u = a;
55  v = a;
56  return *this;
57  }
58 
59  inline Vector2d& operator+=(const Vector2d &other) {
60  u += other.u;
61  v += other.v;
62  return *this;
63  }
64 
65  inline Vector2d& operator-=(const Vector2d &other) {
66  u -= other.u;
67  v -= other.v;
68  return *this;
69  }
70 
71  inline Vector2d& operator*=(const double &a) {
72  u *= a;
73  v *= a;
74  return *this;
75  }
76 
77  inline Vector2d& operator/=(const double &a) {
78  u /= a;
79  v /= a;
80  return *this;
81  }
82 
83  //! \brief Adds two vectors.
84  inline Vector2d operator+(const Vector2d &other) const {
85  return Vector2d(u + other.u, v + other.v);
86  }
87 
88  //! \brief Substracts two vectors.
89  inline Vector2d operator-(const Vector2d &other) const {
90  return Vector2d(u - other.u, v - other.v);
91  }
92 
93  //! \brief Scales a vector.
94  inline Vector2d operator*(const double &a) const {
95  return Vector2d(u * a, v * a);
96  }
97 
98  //! \brief Scales a vector.
99  inline Vector2d operator/(const double &a) const {
100  return Vector2d(u / a, v / a);
101  }
102 
103  double u, v;
104 };
105 
106 // Multiplication of a vector by a constant is commutative.
107 inline Vector2d operator*(const double &a, const Vector2d &v) {
108  return v * a;
109 }
110 
111 } // end of namespace pism
112 
113 #endif /* PISM_VECTOR2D_HH */
Vector2d operator+(const Vector2d &other) const
Adds two vectors.
Definition: Vector2d.hh:84
Vector2d operator-(const Vector2d &other) const
Substracts two vectors.
Definition: Vector2d.hh:89
double magnitude() const
Magnitude.
Definition: Vector2d.hh:40
Vector2d & operator*=(const double &a)
Definition: Vector2d.hh:71
Vector2d & operator=(const Vector2d &other)
Definition: Vector2d.hh:44
Vector2d(double a, double b)
Definition: Vector2d.hh:32
Vector2d(const Vector2d &other)
Definition: Vector2d.hh:33
Vector2d & operator+=(const Vector2d &other)
Definition: Vector2d.hh:59
Vector2d & operator=(const double &a)
Set both components to the same number.
Definition: Vector2d.hh:53
Vector2d operator*(const double &a) const
Scales a vector.
Definition: Vector2d.hh:94
Vector2d & operator-=(const Vector2d &other)
Definition: Vector2d.hh:65
Vector2d operator/(const double &a) const
Scales a vector.
Definition: Vector2d.hh:99
Vector2d & operator/=(const double &a)
Definition: Vector2d.hh:77
double magnitude_squared() const
Magnitude squared.
Definition: Vector2d.hh:36
This class represents a 2D vector field (such as ice velocity) at a certain grid point.
Definition: Vector2d.hh:29
Vector2d operator*(const double &a, const Vector2d &v)
Definition: Vector2d.hh:107