PSCF v1.1
Vector.cpp
1/*
2* Util Package - C++ Utilities for Scientific Computation
3*
4* Copyright 2010 - 2017, The Regents of the University of Minnesota
5* Distributed under the terms of the GNU General Public License.
6*/
7
8#include "Vector.h"
9#include "Dimension.h"
10#include <util/global.h>
11
12#ifdef UTIL_MPI
13#include <util/mpi/MpiStructBuilder.h>
14#endif
15
16namespace Util
17{
18
19 // Define the Zero Vector
20 const Vector Vector::Zero = Vector(0.0);
21
22 // Equality operators
23
24 #define UTIL_VECTOR_EPSILON 1.0E-8
25
26 bool operator==(const Vector& v1, const Vector& v2)
27 {
28 for (int i=0; i < Dimension; ++i) {
29 if ( fabs(v1.elem_[i] - v2.elem_[i]) > UTIL_VECTOR_EPSILON) {
30 return false;
31 }
32 }
33 return true;
34 }
35
36 bool operator==(const Vector& v1, const double* v2)
37 {
38 for (int i=0; i < Dimension; ++i) {
39 if ( fabs(v1.elem_[i] - v2[i]) > UTIL_VECTOR_EPSILON) {
40 return false;
41 }
42 }
43 return true;
44 }
45
46 #undef UTIL_VECTOR_EPSILON
47
48 bool operator==(const double* v1, const Vector& v2)
49 { return (v2 == v1); }
50
51 // Inequality operators
52
53 bool operator!=(const Vector& v1, const Vector& v2)
54 { return !(v1 == v2); }
55
56 bool operator!=(const Vector& v1, const double* v2)
57 { return !(v1 == v2); }
58
59 bool operator!=(const double* v1, const Vector& v2)
60 { return !(v2 == v1); }
61
62 /*
63 * Extract a Vector from an istream.
64 */
65 std::istream& operator>>(std::istream& in, Vector &vector)
66 {
67 for (int i=0; i < Dimension; ++i) {
68 in >> vector.elem_[i];
69 }
70 return in;
71 }
72
73 /*
74 * Output a Vector to an ostream, without line breaks.
75 */
76 std::ostream& operator<<(std::ostream& out, const Vector &vector)
77 {
78 for (int i=0; i < Dimension; ++i) {
79 out.setf(std::ios::scientific);
80 out.width(Vector::Width);
81 out.precision(Vector::Precision);
82 out << vector.elem_[i];
83 }
84 return out;
85 }
86
87 #ifdef UTIL_MPI
91 MPI::Datatype MpiTraits<Vector>::type = MPI::BYTE;
92 bool MpiTraits<Vector>::hasType = false;
93
98 {
100 MpiStructBuilder builder;
101 Vector vector;
102
103 builder.setBase(&vector);
104 builder.addMember(&vector[0], MPI::DOUBLE);
105 builder.addMember(&vector[1], MPI::DOUBLE);
106 builder.addMember(&vector[2], MPI::DOUBLE);
109 }
110 }
111 #endif
112
113 /*
114 * This static method exists to guarantee initialization of the static
115 * constant Vector::Zero that is defined in this file. Call it once
116 * in the program to guarantee that the contents of this file will be
117 * linked, rather than optimized away.
118 */
120 {
121 static int nCall = 0;
122 ++nCall;
123 }
124
125}
A MpiStructBuilder objects is used to create an MPI Struct datatype.
void addMember(void *memberAddress, MPI::Datatype type, int count=1)
Add a new member variable to the type map.
void setBase(void *objectAddress)
Set address of an class instance.
void commit(MPI::Datatype &newType)
Build and commit a user-defined MPI Struct datatype.
Default MpiTraits class.
Definition: MpiTraits.h:40
A Vector is a Cartesian vector.
Definition: Vector.h:76
static const Vector Zero
Zero Vector = {0.0, 0.0, 0.0}.
Definition: Vector.h:369
static void commitMpiType()
Commit MPI datatype MpiTraits<Vector>::type.
Definition: Vector.cpp:97
static void initStatic()
Initialize Zero Vector.
Definition: Vector.cpp:119
File containing preprocessor macros for error handling.
const int Dimension
Dimensionality of space.
Definition: Dimension.h:19
Utility classes for scientific computation.
Definition: accumulators.mod:1
bool operator==(Polynomial< T > const &a, Polynomial< T > const &b)
Equality operator for polynomials.
Definition: Polynomial.h:675
std::istream & operator>>(std::istream &in, Pair< Data > &pair)
Input a Pair from an istream.
Definition: Pair.h:44
std::ostream & operator<<(std::ostream &out, const Pair< Data > &pair)
Output a Pair to an ostream, without line breaks.
Definition: Pair.h:57
bool operator!=(Polynomial< T > const &a, Polynomial< T > const &b)
Inequality operator for polynomials.
Definition: Polynomial.h:694