JDFTx  1.2.0
ColumnBundle.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_ELECTRONIC_COLUMNBUNDLE_H
21 #define JDFTX_ELECTRONIC_COLUMNBUNDLE_H
22 
23 #include <electronic/common.h>
24 #include <core/ManagedMemory.h>
25 #include <electronic/Basis.h>
26 #include <electronic/matrix.h>
27 #include <core/ScalarField.h>
28 #include <core/scaled.h>
29 
31 {
32  int ncols;
33  size_t col_length; //typically equal to either basis->nbasis (or 2*basis->nbasis for spinors in noncollinear mode)
34 
35 public:
36  int nCols() const { return ncols; }
37  size_t colLength() const { return col_length; }
38  explicit operator bool() const { return ncols*col_length; }
39 
41  size_t index(int i, size_t j) const { return i*col_length+j; }
42 
43  bool isSpinor() const { return basis && (col_length==2*basis->nbasis); }
44  int spinorLength() const { return isSpinor() ? 2 : 1; }
45 
46  const QuantumNumber *qnum;
47  const Basis *basis;
48 
49  void init(int nc, size_t len, const Basis* b, const QuantumNumber* q, bool onGpu=false);
50  void free();
51  ColumnBundle(int nc=0, size_t len=0, const Basis* b=NULL, const QuantumNumber* q=NULL, bool onGpu=false);
52  ColumnBundle(const ColumnBundle&);
54 
57  ColumnBundle similar(int ncOverride=-1) const;
58 
61 
62  // Get/set columns
63  ColumnBundle getSub(int colStart, int colStop) const;
64  void setSub(int colStart, const ColumnBundle&);
65 
66  complexScalarFieldTilde getColumn(int i, int s) const;
67  void setColumn(int i, int s, const complexScalarFieldTilde&);
68  void accumColumn(int i, int s, const complexScalarFieldTilde&);
69 
70  void randomize(int colStart, int colStop);
71 };
72 
74 void init(std::vector<ColumnBundle>&, int nbundles, int ncols=0, const Basis* basis=0, const ElecInfo* eInfo=0);
75 
76 void randomize(std::vector<ColumnBundle>&, const ElecInfo& eInfo);
77 void write(const std::vector<ColumnBundle>&, const char *fname, const ElecInfo& eInfo);
78 
80 { bool realSpace;
81  int nBandsOld;
82  double Ecut, EcutOld;
84 
86 };
87 void read(std::vector<ColumnBundle>&, const char *fname, const ElecInfo& eInfo, const ColumnBundleReadConversion* conversion=0);
88 
89 // Used in the CG template Minimize.h
91 void randomize(ColumnBundle& x);
92 double dot(const ColumnBundle& x, const ColumnBundle& y);
93 
94 
95 //----------- Arithmetic ------------
96 
101 
102 ColumnBundle& operator*=(ColumnBundle& X, double s);
103 ColumnBundle operator*(double s, ColumnBundle&& Y);
104 ColumnBundle operator*(ColumnBundle&& Y, double s);
105 scaled<ColumnBundle> operator*(double s, const ColumnBundle &Y);
106 scaled<ColumnBundle> operator*(const ColumnBundle &Y, double s);
111 
114 { const ColumnBundle& Y;
116  double scale;
117 
118  int nCols() const { return Mst.nCols(); }
119  size_t colLength() const { return Y.colLength(); }
120 
121  //Scaling:
122  ColumnBundleMatrixProduct& operator*=(double s) { scale *= s; return *this; }
123  ColumnBundleMatrixProduct operator*(double s) const { return ColumnBundleMatrixProduct(Y,Mst,scale*s); }
124  friend ColumnBundleMatrixProduct operator*(double s, const ColumnBundleMatrixProduct& A) { return A * s; }
125 
126  operator ColumnBundle() const;
127  void scaleAccumulate(double alpha, double beta, ColumnBundle& YM) const;
128 private:
130  ColumnBundleMatrixProduct(const ColumnBundle& Y, const matrixScaledTransOp& Mst, double scale=1.) : Y(Y), Mst(Mst), scale(scale) {}
132 };
133 
134 //Delay ColumnBundle * matrix and combine it with ColumnBundle accumulate operations when possible:
140 
142 matrix operator^(const scaled<ColumnBundle>&, const scaled<ColumnBundle>&);
143 
144 #endif // JDFTX_ELECTRONIC_COLUMNBUNDLE_H
Definition: scaled.h:25
Tptr operator*(const Tptr &in, double scaleFac)
Scalar multiply (preserve input)
Definition: Operators.h:116
complex dot(const Tptr &X, const Tptr &Y)
Definition: Operators.h:196
ColumnBundle similar(int ncOverride=-1) const
General complex matrix.
Definition: matrix.h:58
Template to avoid (delay) scaling operations on linear objects.
void read(const char *fname)
binary read from a file
int nCols() const
number of columns accessor
Definition: ColumnBundle.h:118
int nBandsOld
nBands for the input wavefunction
Definition: ColumnBundle.h:81
const ColumnBundle & Y
the ColumnBundle in the product
Definition: ColumnBundle.h:114
void free()
Force cleanup.
Base class for managed-memory objects (that could potentially live on GPUs as well) ...
Definition: ManagedMemory.h:26
Real diagonal matrix.
Definition: matrix.h:31
Tptr & operator-=(Tptr &in, const Tptr &other)
Decrement.
Definition: Operators.h:170
size_t colLength() const
column length accessor
Definition: ColumnBundle.h:37
Definition: ElecInfo.h:35
ColumnBundle(int nc=0, size_t len=0, const Basis *b=NULL, const QuantumNumber *q=NULL, bool onGpu=false)
onGpu MUST remain false if no GPU_ENABLED!
Real and complex scalar fields in real and reciprocal space.
double EcutOld
Ecut for the current calculation and input wavefunction in fourier space.
Definition: ColumnBundle.h:82
bool realSpace
whether to read realspace wavefunctions
Definition: ColumnBundle.h:80
int nCols() const
number of columns accessor
Definition: ColumnBundle.h:36
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
vector3< int > S_old
fftbox size for the input wavefunction in double space
Definition: ColumnBundle.h:83
double scale
additional scale factor
Definition: ColumnBundle.h:116
void randomize(int colStart, int colStop)
randomize a selected range of columns
const matrixScaledTransOp & Mst
the matrix in the product (along with scale and transpose operations, if any)
Definition: ColumnBundle.h:115
Definition: Basis.h:29
ColumnBundle & operator=(const ColumnBundle &)
copy-assignment
complexScalarFieldTilde getColumn(int i, int s) const
Expand the i&#39;th column and s&#39;th spinor component from reduced to full G-space.
size_t index(int i, size_t j) const
Index of the i&#39;th column j&#39;th point into the data array.
Definition: ColumnBundle.h:41
void setSub(int colStart, const ColumnBundle &)
set columns (starting at colStart) from a ColumnBundle, ignoring columns that would go beyond nCols()...
size_t colLength() const
column length accessor
Definition: ColumnBundle.h:119
Tptr operator+(const Tptr &in1, const Tptr &in2)
Add (preserve inputs)
Definition: Operators.h:171
size_t nbasis
number of basis elements (i.e. G-vectors)
Definition: Basis.h:35
void accumColumn(int i, int s, const complexScalarFieldTilde &)
Redeuce a full G-space vector and accumulate onto the i&#39;th column and s&#39;th spinor component...
Tptr & operator*=(Tptr &in, double scaleFac)
Scale.
Definition: Operators.h:115
ColumnBundle getSub(int colStart, int colStop) const
get a range of columns as a ColumnBundle
Definition: ColumnBundle.h:79
Definition: ElecInfo.h:49
Matrix with a pending scale and transpose operation.
Definition: matrix.h:115
ColumnBundle with a pending matrix multiply (on the right side)
Definition: ColumnBundle.h:113
void write(const char *fname) const
binary-write to a file
Complex number (need to define our own because we need operators for gpu code as well) ...
Definition: scalar.h:49
void setColumn(int i, int s, const complexScalarFieldTilde &)
Redeuce a full G-space vector and store it as the i&#39;th column and s&#39;th spinor component.
Definition: ColumnBundle.h:30
Tptr & operator+=(Tptr &in, const Tptr &other)
Increment.
Definition: Operators.h:169
void init(int nc, size_t len, const Basis *b, const QuantumNumber *q, bool onGpu=false)
onGpu MUST remain false if no GPU_ENABLED!
Tptr operator-(const Tptr &in1, const Tptr &in2)
Subtract (preserve inputs)
Definition: Operators.h:175