PSCF v1.1
Tensor.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 "Tensor.h"
9#include <util/global.h>
10
11#ifdef UTIL_MPI
12#include <util/mpi/MpiStructBuilder.h>
13#endif
14
15namespace Util
16{
17
18 // Define the Zero Tensor constant
19 const Tensor Tensor::Zero = Tensor(0.0);
20
21 // Define the Identity Tensor constant
22 const Tensor Tensor::Identity = Tensor().identity();
23
28 {
29 int nCall = 0;
30 if (nCall > 0) {
31 UTIL_THROW("Tensor::initStatic called more than once");
32 }
33 ++nCall;
34 }
35
36 // Equality operators
37
38 #define UTIL_TENSOR_EPSILON 1.0E-8
39
40 /*
41 * Return true iff tensors t1 and t2 are equal
42 */
43 bool operator==(const Tensor& t1, const Tensor& t2)
44 {
45 for (int i = 0; i < DimensionSq; ++i) {
46 if ( fabs(t1.elem_[i] - t2.elem_[i]) > UTIL_TENSOR_EPSILON) {
47 return false;
48 }
49 }
50 return true;
51 }
52
53 /*
54 * Return true iff tensor t2 and built-in 2D array a2 are equal.
55 */
56 bool operator==(const Tensor& t1, const double a2[][Dimension])
57 {
58 for (int i = 0; i < Dimension; ++i) {
59 for (int j = 0; j < Dimension; ++j) {
60 if ( fabs(t1(i, j) - a2[i][j]) > UTIL_TENSOR_EPSILON) {
61 return false;
62 }
63 }
64 }
65 return true;
66 }
67
68 /*
69 * Return true iff a built-in 2D array and a tensor are equal.
70 */
71 bool operator==(const double a1[][Dimension], const Tensor& t2)
72 { return (t2 == a1); }
73
74 #undef UTIL_TENSOR_EPSILON
75
76 // Inequality operators
77
79 bool operator!=(const Tensor& t1, const Tensor& t2)
80 { return !(t1 == t2); }
81
83 bool operator!=(const Tensor& t1, const double a2[][Dimension])
84 { return !(t1 == a2); }
85
87 bool operator!=(const double a1[][Dimension], const Tensor& t2)
88 { return !(t2 == a1); }
89
90 /*
91 * Extract a Tensor from an istream.
92 */
93 std::istream& operator>>(std::istream& in, Tensor &tensor)
94 {
95 for (int i=0; i < DimensionSq; ++i) {
96 in >> tensor.elem_[i];
97 }
98 return in;
99 }
100
101 /*
102 * Output a Tensor to an ostream, without line breaks.
103 */
104 std::ostream& operator<<(std::ostream& out, const Tensor &tensor)
105 {
106 for (int i=0; i < DimensionSq; ++i) {
107 out.setf(std::ios::scientific);
108 out.width(Tensor::Width);
109 out.precision(Tensor::Precision);
110 out << tensor.elem_[i];
111 }
112 return out;
113 }
114
115 #ifdef UTIL_MPI
116 /*
117 * Initialize MPI Datatype.
118 */
119 MPI::Datatype MpiTraits<Tensor>::type = MPI::BYTE;
120 bool MpiTraits<Tensor>::hasType = false;
121
122 /*
123 * Commit MPI Datatype.
124 */
126 {
128 MpiStructBuilder builder;
129 Tensor tensor;
130 int i, j;
131
132 builder.setBase(&tensor);
133 for (i = 0; i < Dimension; ++i) {
134 for (j = 0; j < Dimension; ++j) {
135 builder.addMember(&tensor(i, j), MPI::DOUBLE);
136 }
137 }
140 }
141 }
142 #endif
143
144}
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 Tensor represents a Cartesian tensor.
Definition: Tensor.h:33
static const Tensor Zero
Constant Tensor with all zero elements.
Definition: Tensor.h:297
static void initStatic()
Call to guarantee initialization of Zero and Identity tensors.
Definition: Tensor.cpp:27
Tensor & identity()
Set this to the identity (unity) tensor.
Definition: Tensor.h:453
static void commitMpiType()
Commit MPI datatype MpiTraits<Tensor>::type.
Definition: Tensor.cpp:125
static const Tensor Identity
Constant idenity Tensor (diagonal diagonal elements all 1).
Definition: Tensor.h:302
File containing preprocessor macros for error handling.
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Definition: global.h:51
const int Dimension
Dimensionality of space.
Definition: Dimension.h:19
const int DimensionSq
Square of Dimensionality of space.
Definition: Dimension.h:26
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