20 #ifndef JDFTX_CORE_OPERATORS_H 21 #define JDFTX_CORE_OPERATORS_H 33 #include <core/LoopMacros.h> 109 #define Tptr std::shared_ptr<T> 111 template<
class T>
Tptr clone(
const Tptr& X) {
if(X)
return X->clone();
else return 0; }
115 template<
class T>
Tptr&
operator*=(
Tptr& in,
double scaleFac) {
if(in) in->scale *= scaleFac;
return in; }
116 template<
class T>
Tptr operator*(
const Tptr& in,
double scaleFac) {
Tptr out(in->clone());
return out *= scaleFac; }
117 template<
class T>
Tptr operator*(
double scaleFac,
const Tptr& in) {
Tptr out(in->clone());
return out *= scaleFac; }
130 { in->scale *= other->scale;
161 {
if(Y->scale == 0.0) { Y = X * alpha; }
162 else callPref(
eblas_zaxpy)(X->nElem, alpha*X->scale/Y->scale, X->dataPref(
false), 1, Y->dataPref(
false), 1);
203 {
FieldData dataOne(X->gInfo,
"complexScalarField", 1, 2,
false); *((
complex*)dataOne.
data()) = 1.0;
229 template<
typename T>
void initZero(
Tptr& X) { X->zero(); }
230 template<
typename T>
void initZero(
Tptr& X,
const GridInfo& gInfo) {
if(X) X->zero();
else nullToZero(X, gInfo); }
231 template<
typename T>
void nullToZero(
Tptr& X,
const GridInfo& gInfo) {
if(!X) { X=T::alloc(gInfo,isGpuEnabled()); initZero(X); } }
240 template<
typename Func,
typename... Args>
void applyFuncGsq(
const GridInfo& gInfo,
const Func& f, Args... args);
243 template<
typename Func,
typename... Args>
void applyFunc_r(
const GridInfo& gInfo,
const Func& f, Args... args);
253 template<
typename Callable,
typename Vec>
void checkSymmetry(Callable* func,
const Vec& v1,
const Vec& v2,
const char* funcName)
254 {
double dot1 =
dot(v1, (*func)(v2));
255 double dot2 =
dot(v2, (*func)(v1));
256 double dotDiff = fabs(dot1-dot2);
257 logPrintf(
"Relative error in symmetry of %s: %le\n", funcName, dotDiff/
sqrt(fabs(dot1)*fabs(dot2)));
267 template<
typename Func,
typename... Args>
268 void applyFuncGsq_sub(
size_t iStart,
size_t iStop,
const vector3<int> S,
const matrix3<> GGT,
const Func* f, Args... args)
269 { THREAD_halfGspaceLoop( (*f)(i, GGT.metric_length_squared(iG), args...); )
271 template<typename Func, typename... Args>
void applyFuncGsq(
const GridInfo& gInfo,
const Func& f, Args... args)
272 {
threadLaunch(applyFuncGsq_sub<Func,Args...>, gInfo.
nG, gInfo.
S, gInfo.GGT, &f, args...);
275 template<
typename Func,
typename... Args>
276 void applyFunc_r_sub(
size_t iStart,
size_t iStop,
const vector3<int> S,
const vector3<> h[3],
const Func* f, Args... args)
278 (
vector3<> ri = iv[0]*h[0] + iv[1]*h[1] + iv[2]*h[2];
279 (*f)(i, ri, args...);
282 template<typename Func, typename... Args>
void applyFunc_r(
const GridInfo& gInfo,
const Func& f, Args... args)
283 {
threadLaunch(applyFunc_r_sub<Func,Args...>, gInfo.
nr, gInfo.
S, gInfo.
h, f, args...);
286 #endif //JDFTX_CORE_OPERATORS_H ScalarFieldTilde Idag(const ScalarField &, int nThreads=0)
Forward transform transpose: Real space -> PW basis.
complex sum(const Tptr &X)
Definition: Operators.h:202
void checkSymmetry(Callable *func, const Vec &v1, const Vec &v2, const char *funcName)
Definition: Operators.h:253
ScalarField pow(const ScalarField &, double alpha)
Elementwise power (preserve input)
#define logPrintf(...)
printf() for log files
Definition: Util.h:110
ScalarField Imag(const complexScalarField &)
imaginary part of a complex scalar field (real-space)
void applyFunc_r(const GridInfo &gInfo, const Func &f, Args...args)
Evaluate a function f(i, r, args...) at each point in real space index by i.
Tptr operator*(const Tptr &in, double scaleFac)
Scalar multiply (preserve input)
Definition: Operators.h:116
ScalarField sqrt(const ScalarField &)
Elementwise square root (preserve input)
Simulation grid descriptor.
Definition: GridInfo.h:45
STL strings and streams with case insensitive comparison.
ScalarFieldTilde J(const ScalarField &, int nThreads=0)
Inverse transform: Real space -> PW basis.
void initRandom(ScalarField &, double cap=0.0)
initialize element-wise with a unit-normal random number (with a cap if cap>0)
Tptr conj(Tptr &&in)
Generic elementwise conjugate for complex data:
Definition: Operators.h:122
complex dot(const Tptr &X, const Tptr &Y)
Definition: Operators.h:196
ScalarField Jdag(const ScalarFieldTilde &, bool compat=false, int nThreads=0)
Inverse transform transpose: PW basis -> real space (preserve input)
double integral(const ScalarField &)
Integral in the unit cell (dV times sum())
void initRandomFlat(ScalarField &)
initialize element-wise with a unit-flat [0:1) random number
complexScalarField Complex(const ScalarField &)
convert real to complex scalar field with zero imaginary part (real-space)
std::shared_ptr< ScalarFieldTildeData > ScalarFieldTilde
A smart reference-counting pointer to ScalarFieldTildeData.
Definition: ScalarField.h:45
int nr
position space grid count = S[0]*S[1]*S[2]
Definition: GridInfo.h:99
vector3 h[3]
real space sample vectors
Definition: GridInfo.h:98
void * data(bool shouldAbsorbScale=true)
get a pointer to the actual data (after absorbing the scale factor, unless otherwise specified) ...
void threadLaunch(int nThreads, Callable *func, size_t nJobs, Args...args)
A simple utility for running muliple threads.
Tptr & operator-=(Tptr &in, const Tptr &other)
Decrement.
Definition: Operators.h:170
ScalarFieldTilde Linv(const ScalarFieldTilde &)
Inverse Laplacian.
void applyFuncGsq(const GridInfo &gInfo, const Func &f, Args...args)
Evaluate a function f(i, Gsq, args...) at each point in reciprocal space indexed by i...
void zeroNyquist(RealKernel &Gdata)
zeros out all the nyquist components of a real G-kernel
void nullToZero(Tptr &X, const GridInfo &gInfo)
If X is null, allocate and initialize to 0.
Definition: Operators.h:231
ScalarField Real(const complexScalarField &)
real part of a complex scalar field (real-space)
Real and complex scalar fields in real and reciprocal space.
int nG
reciprocal lattice count = S[0]*S[1]*(S[2]/2+1) [on account of using r2c and c2r ffts] ...
Definition: GridInfo.h:100
ScalarField I(const ScalarFieldTilde &, bool compat=false, int nThreads=0)
Forward transform: PW basis -> real space (preserve input)
std::shared_ptr< complexScalarFieldTildeData > complexScalarFieldTilde
A smart reference-counting pointer to complexScalarFieldTildeData.
Definition: ScalarField.h:47
Tptr clone(const Tptr &X)
Clone (NOTE: operator= is by reference for the ScalarField classes)
Definition: Operators.h:111
ScalarField log(const ScalarField &)
Elementwise logarithm (preserve input)
ScalarFieldTilde O(const ScalarFieldTilde &)
Inner product operator (diagonal in PW basis)
ScalarField JdagOJ(const ScalarField &)
Evaluate Jdag(O(J())), which avoids 2 fourier transforms in PW basis (preserve input) ...
vector3< int > S
sample points in each dimension (if 0, will be determined automatically based on Gmax) ...
Definition: GridInfo.h:85
Tptr operator+(const Tptr &in1, const Tptr &in2)
Add (preserve inputs)
Definition: Operators.h:171
ScalarFieldTilde L(const ScalarFieldTilde &)
Laplacian.
void printStats(const ScalarField &X, const char *name, FILE *fp=stdout)
Print mean and standard deviation of array with specified name (debug utility)
void initGaussianKernel(RealKernel &, double x0)
initialize to gaussian kernel exp(-(G x0/2)^2)
ScalarFieldTilde gaussConvolve(const ScalarFieldTilde &, double sigma)
convolve with a gaussian
std::shared_ptr< complexScalarFieldData > complexScalarField
A smart reference-counting pointer to complexScalarFieldData.
Definition: ScalarField.h:46
ScalarField inv(const ScalarField &)
Elementwise reciprocal (preserve input)
ScalarField exp(const ScalarField &)
Elementwise exponential (preserve input)
Geometry of the simulation grid.
Tptr & operator*=(Tptr &in, double scaleFac)
Scale.
Definition: Operators.h:115
#define Tptr
shorthand for writing the template operators (undef'd at end of header)
Definition: Operators.h:109
Base class for ScalarFieldData and ScalarFieldTildeData.
Definition: ScalarField.h:61
Special class for storing real reciprocal-space kernels encountered ever so often for convolutions...
Definition: ScalarField.h:180
std::shared_ptr< ScalarFieldData > ScalarField
A smart reference-counting pointer to ScalarFieldData.
Definition: ScalarField.h:41
Complex number (need to define our own because we need operators for gpu code as well) ...
Definition: scalar.h:49
void axpy(double alpha, const Tptr &X, Tptr &Y)
Generic axpy for complex data types (Note: null pointers are treated as zero)
Definition: Operators.h:158
void initTranslation(ScalarFieldTilde &, const vector3<> &r)
initialize to translation operator exp(-i G.r)
Tptr & operator+=(Tptr &in, const Tptr &other)
Increment.
Definition: Operators.h:169
Tptr operator-(const Tptr &in1, const Tptr &in2)
Subtract (preserve inputs)
Definition: Operators.h:175
double nrm2(const Tptr &X)
Definition: Operators.h:199