JDFTx  1.2.0
tensor3.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------
2 Copyright 2011 Ravishankar Sundararaman
3 
4 This file is part of JDFTx.
5 
6 JDFTx is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10 
11 JDFTx is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15 
16 You should have received a copy of the GNU General Public License
17 along with JDFTx. If not, see <http://www.gnu.org/licenses/>.
18 -------------------------------------------------------------------*/
19 
20 #ifndef JDFTX_CORE_TENSOR3_H
21 #define JDFTX_CORE_TENSOR3_H
22 
25 
26 #include <core/scalar.h>
27 #include <vector>
28 
29 #define LOOP5(code) { for(int k=0; k<5; k++) { code } }
30 
32 template<typename scalar=double> class tensor3
33 {
34  scalar v[5];
35 public:
36  //Accessors
37  __hostanddev__ scalar& operator[](int k) { return v[k]; }
38  __hostanddev__ const scalar& operator[](int k) const { return v[k]; }
39  __hostanddev__ scalar& xy() { return v[0]; }
40  __hostanddev__ scalar& yz() { return v[1]; }
41  __hostanddev__ scalar& zx() { return v[2]; }
42  __hostanddev__ scalar& xxr() { return v[3]; }
43  __hostanddev__ scalar& yyr() { return v[4]; }
44  __hostanddev__ const scalar& xy() const { return v[0]; }
45  __hostanddev__ const scalar& yz() const { return v[1]; }
46  __hostanddev__ const scalar& zx() const { return v[2]; }
47  __hostanddev__ const scalar& xxr() const { return v[3]; }
48  __hostanddev__ const scalar& yyr() const { return v[4]; }
49 
50  explicit __hostanddev__ tensor3(scalar a=0, scalar b=0, scalar c=0, scalar d=0, scalar e=0) { v[0]=a; v[1]=b; v[2]=c; v[3]=d; v[4]=e; }
51  tensor3(std::vector<scalar> a) { LOOP5( v[k]=a[k]; ) }
52 };
53 
55 template<typename scalar> __hostanddev__ tensor3<scalar> loadTensor(const tensor3<const scalar*>& tArr, int i)
56 { return tensor3<scalar>( tArr[0][i], tArr[1][i], tArr[2][i], tArr[3][i], tArr[4][i] );
57 }
59 template<typename scalar> __hostanddev__ tensor3<scalar> loadTensor(const tensor3<scalar*>& tArr, int i)
60 { return tensor3<scalar>( tArr[0][i], tArr[1][i], tArr[2][i], tArr[3][i], tArr[4][i] );
61 }
63 template<typename scalar> __hostanddev__ void storeTensor(const tensor3<scalar>& t, tensor3<scalar*>& tArr, int i)
64 { LOOP5( tArr[k][i] = t[k]; )
65 }
67 template<typename scalar> __hostanddev__ void accumTensor(const tensor3<scalar>& t, tensor3<scalar*>& tArr, int i)
68 { LOOP5( tArr[k][i] += t[k]; )
69 }
70 
71 #undef LOOP5
72 #endif // JDFTX_CORE_TENSOR3_H
__hostanddev__ tensor3< scalar > loadTensor(const tensor3< const scalar * > &tArr, int i)
Load tensor from a constant tensor field.
Definition: tensor3.h:55
__hostanddev__ scalar & xxr()
xxr = x^2 - r^2/3
Definition: tensor3.h:42
__hostanddev__ scalar & yyr()
yyr = y^2-r^2/3
Definition: tensor3.h:43
Symmetric traceless rank-2 tensor in 3D.
Definition: tensor3.h:32
__hostanddev__ void accumTensor(const tensor3< scalar > &t, tensor3< scalar * > &tArr, int i)
Accumulate tensor onto a tensor field.
Definition: tensor3.h:67
__hostanddev__ void storeTensor(const tensor3< scalar > &t, tensor3< scalar * > &tArr, int i)
Store tensor to a tensor field.
Definition: tensor3.h:63