tVector2.hh - pism - [fork] customized build of PISM, the parallel ice sheet model (tillflux branch)
HTML git clone git://src.adamsgaard.dk/pism
DIR Log
DIR Files
DIR Refs
DIR LICENSE
---
tVector2.hh (2835B)
---
1 /* Copyright (C) 2015, 2016, 2020 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 _VECTOR2_H_
21 #define _VECTOR2_H_
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 } // end of namespace pism
112
113 #endif /* _VECTOR2_H_ */