JDFTx  1.2.0
scalar.h
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_SCALAR_H
21 #define JDFTX_CORE_SCALAR_H
22 
23 #include <cmath>
24 
25 #ifndef __device__ //in .cpp files
26  #define __hostanddev__ inline
27 #else //in .cu files
28  #define __hostanddev__ inline __device__ __host__
29  #define __in_a_cu_file__
30  #include <cuda_runtime.h>
31 #endif
32 
34 template<class T> T ceildiv(T num, T den) { return (num + den - 1)/den; }
35 
37 template<class T> T floorMultiple(T num, T den) { return (num/den)*den; }
38 
39 #ifdef __APPLE__
40 #ifndef __in_a_cu_file__
41 inline void sincos(double x, double* s, double* c)
42 { *s = sin(x);
43  *c = cos(x);
44 }
45 #endif
46 #endif
47 
49 struct complex
50 { double x, y;
51 
52  //Accessors
53  __hostanddev__ double& real() { return x; }
54  __hostanddev__ double& imag() { return y; }
55  __hostanddev__ const double& real() const { return x; }
56  __hostanddev__ const double& imag() const { return y; }
57 
58  //Constructors
59  __hostanddev__ complex(double x=0, double y=0) : x(x), y(y) {}
60  #ifdef __in_a_cu_file__
61  __hostanddev__ complex(const double2& c) : x(c.x), y(c.y) {}
62  __hostanddev__ operator double2() const { double2 ret; ret.x=x; ret.y=y; return ret;}
63  #endif
64 
65  //Arithmetic
66  __hostanddev__ complex& operator+=(const complex& c) { x+=c.x; y+=c.y; return *this; }
67  __hostanddev__ complex& operator+=(double r) { x+=r; return *this; }
68  __hostanddev__ complex operator+(const complex& c) const { return complex(x+c.x, y+c.y); }
69  __hostanddev__ complex operator+(double r) const { return complex(x+r, y); }
70  __hostanddev__ complex& operator-=(const complex& c) { x-=c.x; y-=c.y; return *this; }
71  __hostanddev__ complex& operator-=(double r) { x-=r; return *this; }
72  __hostanddev__ complex operator-(const complex& c) const { return complex(x-c.x, y-c.y); }
73  __hostanddev__ complex operator-(double r) const { return complex(x-r, y); }
74  __hostanddev__ complex operator-() const { return complex(-x, -y); }
75  __hostanddev__ complex& operator*=(const complex& c) { return (*this = *this * c); }
76  __hostanddev__ complex& operator*=(double r) { x*=r; y*=r; return *this; }
77  __hostanddev__ complex operator*(const complex& c) const { return complex(x*c.x-y*c.y, y*c.x+x*c.y); }
78  __hostanddev__ complex operator*(double r) const { return complex(x*r, y*r); }
79  __hostanddev__ complex& operator/=(const complex& c) { return (*this = *this / c); }
80  __hostanddev__ complex& operator/=(double r) { return (*this *= 1.0/r); }
81  __hostanddev__ complex operator/(const complex& c) const { return complex(x*c.x+y*c.y, y*c.x-x*c.y) / c.norm(); }
82  __hostanddev__ complex operator/(double r) const { return *this * (1.0/r); }
83 
84  __hostanddev__ double norm() const { return x*x + y*y; }
85  __hostanddev__ double abs() const { return sqrt(norm()); }
86  __hostanddev__ double arg() const { return atan2(y,x); }
87  __hostanddev__ complex conj() const { return complex(x,-y); }
88 };
89 
90 __hostanddev__ double real(const complex& c) { return c.real(); }
91 __hostanddev__ double imag(const complex& c) { return c.imag(); }
92 __hostanddev__ double norm(const complex& c) { return c.norm(); }
93 __hostanddev__ double abs(const complex& c) { return c.abs(); }
94 __hostanddev__ double arg(const complex& c) { return c.arg(); }
95 __hostanddev__ complex conj(const complex& c) { return c.conj(); }
96 __hostanddev__ double conj(const double& c) { return c; } //provided to ease templating over complex and double
97 
98 __hostanddev__ complex operator+(double r, const complex& c) { return c+r; }
99 __hostanddev__ complex operator-(double r, const complex& c) { return -c+r; }
100 __hostanddev__ complex operator*(double r, const complex& c) { return c*r; }
101 
102 
104 __hostanddev__ complex cis(double x)
105 { double s, c; sincos(x, &s, &c);
106  return complex(c, s);
107 }
108 
109 #endif // JDFTX_CORE_SCALAR_H
ScalarField sqrt(const ScalarField &)
Elementwise square root (preserve input)
Complex number (need to define our own because we need operators for gpu code as well) ...
Definition: scalar.h:49