MADNESS  0.10.1
PNOStructures.h
Go to the documentation of this file.
1 /*
2  * PNOStructures.h
3  *
4  * Created on: Oct 24, 2018
5  * Author: kottmanj
6  */
7 
8 #ifndef PAPER_CODE_PNOSTRUCTURES_H_
9 #define PAPER_CODE_PNOSTRUCTURES_H_
10 
11 #include <valarray>
12 #include <string>
13 
17 #include <madness.h>
18 
19 /// this block needs to be known before nonlinsol.h is included (happens when nemo is included)
20 /// better move this to nonlinsol.h
21 namespace madness {
22 /// inner product for std::valarray
23 double inner(const std::valarray<double>& bra, const std::valarray<double>& ket);
24 // KAIN allocator for std::valarray
26  const size_t dim;
27 
28  valarray_allocator(const size_t& indim) : dim(indim) {};
29 
30  std::valarray<double> operator()(){
31  return std::valarray<double>(dim);
32  }
34  valarray_allocator tmp(other.dim);
35  return tmp;
36  }
37 
38 };
39 }
40 
41 
42 #include<madness/chem/nemo.h>
43 namespace madness {
44 
46 public:
47  ParametrizedExchange(World & world, const Nemo& nemo_, const std::string& type_):
48  world(world),
49  nemo(nemo_),
50  type(type_),
51  K(Exchange<double,3>(world, &nemo, 0))
52  {
53 
54  }
55 
57  const Nemo& nemo;
58  const std::string type;
60 
62  vecfuncT vket(1,ket);
63  vecfuncT vKket=this->operator()(vket);
64  return vKket[0];
65  }
66 
67  vecfuncT operator ()(const vecfuncT& vket,
68  const double& mul_tol = 0.0) const;
69 
70 
71 
72 };
73 
74 /// Timer structure
75 struct MyTimer{
77  mutable double wstart_, cstart_;
78  mutable double wstop_, cstop_;
79  mutable double wtime_, ctime_;
80  mutable std::string msg_;
81 
82  MyTimer(World& world): world(world), wstart_(-1.0),cstart_(-1.0),wstop_(-1.0),cstop_(-1.0),wtime_(-1.0),ctime_(-1.0){}
83  MyTimer start()const{
84  world.gop.fence();
85  wstart_ = wall_time();
86  cstart_ = cpu_time();
87  return *this;
88  }
90  wstop_ = wall_time();
91  cstop_ = cpu_time();
92  wtime_ = wstop_ - wstart_;
93  ctime_ = cstop_ - cstart_;
94  return *this;
95  }
96  MyTimer print(const std::string& msg)const{
97  msg_=msg;
98  if(world.rank()==0){
99  printf("timer: %40.40s %8.2fs %8.2fs \n", msg.c_str(), wtime_, ctime_);
100  }
101  return *this;
102  }
103  MyTimer print()const{
104  if(world.rank()==0){
105  printf("timer: %40.40s %8.2fs %8.2fs \n", msg_.c_str(), wtime_, ctime_);
106  }
107  return *this;
108  }
109 };
110 
111 /// POD structure for energies
114  PairEnergies(const size_t& npairs): eijs(npairs),eijt(npairs),eijs_f12(npairs),eijt_f12(npairs),eij(npairs),energy(0.0),energy_f12(0.0){}
115 
116  std::valarray<double> eijs; ///< singlet pair energies (for CIS(D) the GS Part)
117  std::valarray<double> eijt; ///< triplet pair energies (for CIS(D) the ES Part)
118  std::valarray<double> eijs_f12; ///< singlet f12 pair energies (for CIS(D) the GS Part)
119  std::valarray<double> eijt_f12; ///< triplet f12 pair energies (for CIS(D) the ES Part)
120  std::valarray<double> eij; ///< total pair energies
121  double energy=0.0; ///<total correlation energy (regularized Energy for f12 calculation)
122  double energy_f12=0.0; ///< total f12 correlation energy
123  double total_energy()const{return energy+energy_f12;}
124 
125  PairEnergies operator +=(const PairEnergies& right);
126 
127  PairEnergies operator +(const PairEnergies& right) const;
128 
129  void update(){
130  energy=0.0;
131  energy_f12=0.0;
132  for(size_t ij=0;ij<eij.size();++ij){
133  const double ereg_ij=eijs[ij]+eijt[ij];
134  const double ef12_ij=eijs_f12[ij]+eijt_f12[ij];
135  energy+=ereg_ij;
136  energy_f12+=ef12_ij;
137  eij[ij]=ereg_ij+ef12_ij;
138  }
139  }
140 };
141 
142 /// iterates the third index for pair coupling
144 private:
145  size_t i_;
146  const size_t start_;
147  const size_t stop_;
148  bool finished_;
149  const size_t freeze_;
150 public:
151  size_t i()const {return i_;}
152 
153  OrbitalIterator(const size_t &nocc, const size_t &freeze): i_(0), start_(0),stop_(nocc-freeze),finished_(false),freeze_(freeze) {}
154 
155  operator bool() const{ return !finished_;}
156 
158  if(i_<(stop_-1)) ++i_;
159  else finished_=true;
160  return *this;
161  }
162 
163  // gives real index (if some orbtials are frozen)
164  std::string name()const {return std::to_string(i_+start_);}
165 
166  size_t freeze() const {return freeze_;} // mostly to con the compiler that freeze_ is used
167 };
168 
169 /// Iterator over pairs
170 /// iterates (i,j) from i=start to i<stop and j=i to j<stop
171 /// iterates like the pno-mp2.cc code
172 /// For frozen orbitals iteration goes from 0 to nocc-freeze !!! (consistency with pno-mp2.cc code)
173 /// The name() function gives back the "real" pair number
175 
176  ElectronPairIterator(const size_t& nocc, const size_t& freeze):
177  stop_(nocc-freeze),
178  freeze_(freeze)
180 
181  /// check if iteration has to proceed
182  operator bool() const {
183  return !finished_;
184  }
185 
186  /// Gives the number of the pair in the valarray of pno-mp2.cc file
187  template <typename Int>
188  size_t tridx(Int row, Int col) {
189  size_t result= (row < col) ? (row + col * (col + 1) / 2)
190  : (col + row * (row + 1) / 2);
191  return result;//-offset_;
192  }
193 
194  /// pre-increment operator
196 
197  /// number of active occupied orbitals
198  size_t nocc()const{
199  return (stop_-start_);
200  }
201 
202  /// total number of pairs n * (n + 1) / 2;
203  size_t npairs()const{
204  const size_t n=stop_-start_;
205  return n*(n+1)/2;
206  }
207 
208  /// Gives back "pairij" (for frozen core i and j are the true indices corresponding to the reference with all orbitals)
209  std::string name()const{
210  std::string name="pair"+ std::to_string(i_+freeze_) + std::to_string(j_+freeze_);
211  return name;
212  }
213 
214  size_t i()const{ return i_;}
215  size_t j()const{ return j_;}
216  size_t start()const{ return start_;}
217  size_t stop()const{ return stop_;}
218  size_t ij()const{return ij_;}
219  bool finished()const{return finished_;}
220  bool diagonal()const{return (i_==j_);}
221 
222 private:
223  size_t i_=0; ///< current pair index i
224  size_t j_=0; ///< current pair index j
225  const size_t start_=0; ///< start value for i and j (usually 0)
226  const size_t stop_=0; ///< stop value for i and j (usually number of occ orbitals or occ-nfreeze for frozen core)
227  const size_t freeze_=0; ///< number of frozen orbitals (just to print the correct name)
228  size_t ij_=0; ///< pair number starting from 00 => 0
229  bool finished_=false; ///< true if all pairs where iterated
230 };
231 
232 /// POD for CIS excitation
233 struct CISData{
235  CISData(const size_t& n, const double& o, const vector_real_function_3d& f) : x(f),number(n),omega(o){}
239  int number=-1;
240  double omega=0.0;
241  bool initialized()const{return (number>=0 and omega>0.0 and x.size()>0);}
242 };
243 
244 /// POD for PNO code
245 struct PNOPairs{
246 
247  struct MemInfo{
248  double pno;
249  double Kpno;
250  double W;
251  friend std::ostream& operator <<(std::ostream& os, const MemInfo& mi){
252  os << "PNOPairs Memory Information\n";
253  os << "Total: " << std::fixed << mi.pno + mi.Kpno + mi.W << " GB \n";
254  os << "PNO : " << std::fixed << mi.pno << " GB \n";
255  os << "KPNO : " << std::fixed << mi.Kpno << " GB \n";
256  os << "W : " << std::fixed << mi.W << " GB \n";
257  return os;
258  }
259  };
260 
261  typedef std::valarray<vector_real_function_3d> vfT ;
262  PNOPairs(const PairType& t, const size_t& n) : type(t), nocc(n), npairs(n*(n+1)/2) , S_ij_ik(n), S_ij_kj(n) {initialize(n);}
263  PNOPairs(const PairType& t, const size_t& n, const CISData& c): type(t), nocc(n), npairs(n*(n+1)/2), cis(c), S_ij_ik(n), S_ij_kj(n)
264  {
265  initialize(n);
266  const size_t n2=cis.x.size();
267  MADNESS_ASSERT(nocc==n);
268  MADNESS_ASSERT(n==n2);
269  }
270 
271  void initialize(const std::size_t& nocc);
272 
273  const PairType type; ///< type (i.e. MP2_PAIRTYPE, CISPD_PAIRTYPE, later CCSD etc
274  const size_t nocc; ///< number of active occupied orbitals
275  const size_t npairs; ///< number of Pairs
276  CISData cis; ///< CIS excitation structure
277  vfT pno_ij; ///< the PNOs for all Pairs
278  std::valarray<Tensor<double> > rdm_evals_ij; ///< PNO eigenvalues
279  std::valarray<Tensor<double> > t_ij; ///< the amplitudes for all Pairs
280  std::valarray<Tensor<double> > F_ij; ///< Fock matrices of the PNOs
281  std::valarray<Tensor<double> > W_ij; ///< Fluctuation matrix
282  std::valarray<int> maxranks_ij; ///< maxranks for all pairs, negative->unlimited
283  std::valarray<bool> frozen_ij; ///< if true then pairs are frozen and not optimized (but still contribute to energy -> not frozen core)
284  PairEnergies energies; ///< all Pair Energies
285  vfT Kpno_ij; ///< Exchange Intermediate
286  vfT W_ij_i; ///< Fluctuation Potential
287  vfT W_ij_j; ///< Fluctuation Potential
290  mutable MemInfo meminfo; ///< information about the used memory
291 
292  /// check if all pairs are empty
293  bool empty()const{
294  bool empty=true;
295  for(const auto& p:pno_ij) empty=(empty and p.empty());
296  return empty;
297  }
298 
299  /// consistency check
300  bool is_consistent(std::string& errm) const;
301  /// throes exception if consistency check fails fails
302  void verify()const{
303  std::string errm="";
304  if(not is_consistent(errm)) MADNESS_EXCEPTION(errm.c_str(),1);
305  }
306  // need explicit assignment operator bc of const members
307  PNOPairs operator =(const PNOPairs& other);
308 
309  // name the pair to store and load on disc
310  std::string name(const ElectronPairIterator& it) const;
311 
312  // rearrange a valarray to a big vector according to the pair structure of this
313  // only return unfrozen pairs
314  vector_real_function_3d extract(const vfT& vf) const;
315  // reassemble big vector into valarray pair structure of this
316  // the inverted operation to extract
318  vfT result(npairs);
319  return reassemble(v,result);
320  }
321  vfT reassemble(const vector_real_function_3d& v, vfT& result) const;
322 
324 
325  MemInfo update_meminfo() const;
326 
327 
328 
329 };
330 
331 
332 } /* namespace madness */
333 
334 #endif /* PAPER_CODE_PNOSTRUCTURES_H_ */
Definition: SCFOperators.h:104
The Nemo class.
Definition: nemo.h:326
Definition: PNOStructures.h:45
Exchange< double, 3 > K
Definition: PNOStructures.h:59
real_function_3d operator()(const real_function_3d &ket) const
Definition: PNOStructures.h:61
ParametrizedExchange(World &world, const Nemo &nemo_, const std::string &type_)
Definition: PNOStructures.h:47
const Nemo & nemo
Definition: PNOStructures.h:57
World & world
Definition: PNOStructures.h:56
const std::string type
Definition: PNOStructures.h:58
void fence(bool debug=false)
Synchronizes all processes in communicator AND globally ensures no pending AM or tasks.
Definition: worldgop.cc:161
A parallel world class.
Definition: world.h:132
ProcessID rank() const
Returns the process rank in this World (same as MPI_Comm_rank()).
Definition: world.h:318
WorldGopInterface & gop
Global operations.
Definition: world.h:205
char * p(char *buf, const char *name, int k, int initial_level, double thresh, int order)
Definition: derivatives.cc:72
static const double v
Definition: hatom_sf_dirac.cc:20
General header file for using MADNESS.
Defines madness::MadnessException for exception handling.
#define MADNESS_EXCEPTION(msg, value)
Macro for throwing a MADNESS exception.
Definition: madness_exception.h:119
#define MADNESS_ASSERT(condition)
Assert a condition that should be free of side-effects since in release builds this might be a no-op.
Definition: madness_exception.h:134
File holds all helper structures necessary for the CC_Operator and CC2 class.
Definition: DFParameters.h:10
static double cpu_time()
Returns the cpu time in seconds relative to an arbitrary origin.
Definition: timers.h:127
std::vector< real_function_3d > vector_real_function_3d
Definition: functypedefs.h:79
NDIM & f
Definition: mra.h:2416
double wall_time()
Returns the wall time in seconds relative to an arbitrary origin.
Definition: timers.cc:48
double inner(response_space &a, response_space &b)
Definition: response_functions.h:442
vector< functionT > vecfuncT
Definition: corepotential.cc:58
PairType
Definition: PNOParameters.h:17
static const double c
Definition: relops.cc:10
POD for CIS excitation.
Definition: PNOStructures.h:233
bool initialized() const
Definition: PNOStructures.h:241
vector_real_function_3d Kx
Definition: PNOStructures.h:237
CISData(const size_t &n, const double &o, const vector_real_function_3d &f)
Definition: PNOStructures.h:235
int number
Definition: PNOStructures.h:239
CISData()
Definition: PNOStructures.h:234
vector_real_function_3d Vx
Definition: PNOStructures.h:238
vector_real_function_3d x
Definition: PNOStructures.h:236
double omega
Definition: PNOStructures.h:240
Definition: PNOStructures.h:174
const size_t stop_
stop value for i and j (usually number of occ orbitals or occ-nfreeze for frozen core)
Definition: PNOStructures.h:226
size_t j_
current pair index j
Definition: PNOStructures.h:224
size_t npairs() const
total number of pairs n * (n + 1) / 2;
Definition: PNOStructures.h:203
bool finished_
true if all pairs where iterated
Definition: PNOStructures.h:229
size_t ij_
pair number starting from 00 => 0
Definition: PNOStructures.h:228
const size_t freeze_
number of frozen orbitals (just to print the correct name)
Definition: PNOStructures.h:227
size_t start() const
Definition: PNOStructures.h:216
size_t i() const
Definition: PNOStructures.h:214
bool diagonal() const
Definition: PNOStructures.h:220
ElectronPairIterator & operator++()
pre-increment operator
Definition: PNOStructures.cpp:57
size_t j() const
Definition: PNOStructures.h:215
std::string name() const
Gives back "pairij" (for frozen core i and j are the true indices corresponding to the reference with...
Definition: PNOStructures.h:209
bool finished() const
Definition: PNOStructures.h:219
size_t i_
current pair index i
Definition: PNOStructures.h:223
size_t ij() const
Definition: PNOStructures.h:218
size_t nocc() const
number of active occupied orbitals
Definition: PNOStructures.h:198
size_t tridx(Int row, Int col)
Gives the number of the pair in the valarray of pno-mp2.cc file.
Definition: PNOStructures.h:188
size_t stop() const
Definition: PNOStructures.h:217
const size_t start_
start value for i and j (usually 0)
Definition: PNOStructures.h:225
ElectronPairIterator(const size_t &nocc, const size_t &freeze)
Definition: PNOStructures.h:176
Timer structure.
Definition: PNOStructures.h:75
double wstop_
Definition: PNOStructures.h:78
MyTimer stop()
Definition: PNOStructures.h:89
double cstart_
Definition: PNOStructures.h:77
double wtime_
Definition: PNOStructures.h:79
MyTimer print() const
Definition: PNOStructures.h:103
double wstart_
Definition: PNOStructures.h:77
double ctime_
Definition: PNOStructures.h:79
World & world
Definition: PNOStructures.h:76
MyTimer(World &world)
Definition: PNOStructures.h:82
MyTimer start() const
Definition: PNOStructures.h:83
std::string msg_
Definition: PNOStructures.h:80
double cstop_
Definition: PNOStructures.h:78
MyTimer print(const std::string &msg) const
Definition: PNOStructures.h:96
iterates the third index for pair coupling
Definition: PNOStructures.h:143
std::string name() const
Definition: PNOStructures.h:164
OrbitalIterator & operator++()
Definition: PNOStructures.h:157
const size_t stop_
Definition: PNOStructures.h:147
size_t i() const
Definition: PNOStructures.h:151
bool finished_
Definition: PNOStructures.h:148
size_t freeze() const
Definition: PNOStructures.h:166
const size_t freeze_
Definition: PNOStructures.h:149
size_t i_
Definition: PNOStructures.h:145
OrbitalIterator(const size_t &nocc, const size_t &freeze)
Definition: PNOStructures.h:153
const size_t start_
Definition: PNOStructures.h:146
Definition: PNOStructures.h:247
double W
Definition: PNOStructures.h:250
double pno
Definition: PNOStructures.h:248
friend std::ostream & operator<<(std::ostream &os, const MemInfo &mi)
Definition: PNOStructures.h:251
double Kpno
Definition: PNOStructures.h:249
POD for PNO code.
Definition: PNOStructures.h:245
vfT reassemble(const vector_real_function_3d &v)
Definition: PNOStructures.h:317
vfT W_ij_i
Fluctuation Potential.
Definition: PNOStructures.h:286
vfT Kpno_ij
Exchange Intermediate.
Definition: PNOStructures.h:285
vfT pno_ij
the PNOs for all Pairs
Definition: PNOStructures.h:277
PNOTensors::Tensor_IJ_KJ< double > S_ij_kj
PNO overlaps.
Definition: PNOStructures.h:289
vector_real_function_3d extract(const vfT &vf) const
Definition: PNOStructures.cpp:190
void initialize(const std::size_t &nocc)
Definition: PNOStructures.cpp:154
std::valarray< vector_real_function_3d > vfT
Definition: PNOStructures.h:261
void verify() const
throes exception if consistency check fails fails
Definition: PNOStructures.h:302
bool is_consistent(std::string &errm) const
consistency check
Definition: PNOStructures.cpp:77
PNOTensors::Tensor_IJ_IK< double > S_ij_ik
PNO overlaps.
Definition: PNOStructures.h:288
MemInfo meminfo
information about the used memory
Definition: PNOStructures.h:290
std::valarray< bool > frozen_ij
if true then pairs are frozen and not optimized (but still contribute to energy -> not frozen core)
Definition: PNOStructures.h:283
CISData cis
CIS excitation structure.
Definition: PNOStructures.h:276
PairEnergies energies
all Pair Energies
Definition: PNOStructures.h:284
const size_t nocc
number of active occupied orbitals
Definition: PNOStructures.h:274
bool empty() const
check if all pairs are empty
Definition: PNOStructures.h:293
std::valarray< Tensor< double > > t_ij
the amplitudes for all Pairs
Definition: PNOStructures.h:279
const size_t npairs
number of Pairs
Definition: PNOStructures.h:275
PNOPairs operator=(const PNOPairs &other)
Definition: PNOStructures.cpp:171
std::valarray< Tensor< double > > F_ij
Fock matrices of the PNOs.
Definition: PNOStructures.h:280
std::valarray< Tensor< double > > rdm_evals_ij
PNO eigenvalues.
Definition: PNOStructures.h:278
std::string name(const ElectronPairIterator &it) const
Definition: PNOStructures.cpp:181
void clear_intermediates(const ElectronPairIterator &it)
Definition: PNOStructures.cpp:221
std::valarray< int > maxranks_ij
maxranks for all pairs, negative->unlimited
Definition: PNOStructures.h:282
PNOPairs(const PairType &t, const size_t &n)
Definition: PNOStructures.h:262
MemInfo update_meminfo() const
Definition: PNOStructures.cpp:239
const PairType type
type (i.e. MP2_PAIRTYPE, CISPD_PAIRTYPE, later CCSD etc
Definition: PNOStructures.h:273
PNOPairs(const PairType &t, const size_t &n, const CISData &c)
Definition: PNOStructures.h:263
vfT W_ij_j
Fluctuation Potential.
Definition: PNOStructures.h:287
std::valarray< Tensor< double > > W_ij
Fluctuation matrix.
Definition: PNOStructures.h:281
POD structure for energies.
Definition: PNOStructures.h:112
PairEnergies(const size_t &npairs)
Definition: PNOStructures.h:114
PairEnergies operator+=(const PairEnergies &right)
Definition: PNOStructures.cpp:24
double total_energy() const
Definition: PNOStructures.h:123
PairEnergies()
Definition: PNOStructures.h:113
PairEnergies operator+(const PairEnergies &right) const
Definition: PNOStructures.cpp:40
std::valarray< double > eijs
singlet pair energies (for CIS(D) the GS Part)
Definition: PNOStructures.h:116
std::valarray< double > eijt_f12
triplet f12 pair energies (for CIS(D) the ES Part)
Definition: PNOStructures.h:119
std::valarray< double > eijs_f12
singlet f12 pair energies (for CIS(D) the GS Part)
Definition: PNOStructures.h:118
double energy
total correlation energy (regularized Energy for f12 calculation)
Definition: PNOStructures.h:121
std::valarray< double > eijt
triplet pair energies (for CIS(D) the ES Part)
Definition: PNOStructures.h:117
void update()
Definition: PNOStructures.h:129
double energy_f12
total f12 correlation energy
Definition: PNOStructures.h:122
std::valarray< double > eij
total pair energies
Definition: PNOStructures.h:120
Definition: PNOStructures.h:25
const size_t dim
Definition: PNOStructures.h:26
std::valarray< double > operator()()
Definition: PNOStructures.h:30
valarray_allocator operator=(const valarray_allocator &other)
Definition: PNOStructures.h:33
valarray_allocator(const size_t &indim)
Definition: PNOStructures.h:28
double ij(int64_t i, int64_t j)
Definition: test_distributed_matrix.cc:8