MADNESS  version 0.9
projector.h
Go to the documentation of this file.
1 /*
2  * projector.h
3  *
4  * Created on: Jan 24, 2014
5  * Author: fbischoff
6  */
7 
8 #ifndef MADNESS_CHEM_PROJECTOR_H__INCLUDED
9 #define MADNESS_CHEM_PROJECTOR_H__INCLUDED
10 
11 #include <madness/mra/mra.h>
12 
13 namespace madness {
14 
16  template<typename T, std::size_t NDIM>
17  class Projector {
18 
19  int particle_;
20  std::vector<Function<T,NDIM> > p_;
21 
22  public:
23 
24  Projector() : particle_(0), p_(std::vector<Function<T,NDIM> >()) {}
25 
27  Projector(const Function<T,NDIM>& p, const int particle=0)
28  : particle_(particle), p_(std::vector<Function<T,NDIM> >(1,p)) {
29  MADNESS_ASSERT(particle_==0 or particle_==1);
30  }
31 
33  Projector(const std::vector<Function<T,NDIM> >& p, const int particle=0)
34  : particle_(particle), p_(p) {
35  MADNESS_ASSERT(particle_==0 or particle_==1);
36  }
37 
38  int& particle() {return particle_;}
39  const int& particle() const {return particle_;}
40 
42  const std::vector<Function<T,NDIM> >& p() const {return p_;}
43 
45  template<std::size_t FDIM>
47  operator()(const Function<T,FDIM>& f) const {
48 
49  World& world=f.world();
50 
52 
53  compress(world, p_,false); // don't fence
54  sum.compress(false);
55  f.compress(); // fence
56 
57  // the overlap of all orbitals with the rhs
58  Tensor<double> ovlp=inner(world,f,p_);
59 
60  for (std::size_t i=0; i<p_.size(); ++i) {
61  if (ovlp(i) != T(0.0)) sum.gaxpy(1.0,p_[i],ovlp(i),false);
62  }
63  world.gop.fence();
64  sum.truncate();
65 
66  return sum;
67  }
68 
70  template<std::size_t FDIM>
72  operator()(const Function<T,FDIM>& f) const {
74  for (unsigned int i=0; i<p_.size(); ++i) {
75  const real_function_3d pf2=f.projfirect_out(p_[i],particle_);
76  real_function_6d tmp;
77  MADNESS_EXCEPTION("Projector class: the hartree product is inaccurate -- don't use it",1);
78  if (particle_==0) tmp=hartree_product(p_[i],pf2);
79  else tmp=hartree_product(pf2,p_[i]);
80  sum=(sum+tmp);
81  }
82  sum.truncate();
83  return sum;
84  }
85  };
86 
88 
95  template<typename T, std::size_t NDIM>
97 
98  typedef std::vector<Function<T,NDIM> > vecfuncT;
99 
100  public:
101 
103  StrongOrthogonalityProjector(World& world) : world(world) {}
104 
106  void set_spaces(const vecfuncT& p) {
107  ket1_=p;
108  bra1_=p;
109  ket2_=p;
110  bra2_=p;
111  }
112 
114 
120  void set_spaces(const vecfuncT& bra1, const vecfuncT& ket1,
121  const vecfuncT& bra2, const vecfuncT& ket2) {
122  ket1_=ket1;
123  bra1_=bra1;
124  ket2_=ket2;
125  bra2_=bra2;
126  }
127 
129  vecfuncT ket1() const {return ket1_;}
130 
132  vecfuncT bra1() const {return bra1_;}
133 
135  vecfuncT ket2() const {return ket2_;}
136 
138  vecfuncT bra2() const {return bra2_;}
139 
141 
145 
146  // simple and it works for higher accuracies, but might be
147  // imprecise for lower accuracies
148 // return (f-O1(f)-O2(f)+O1(O2(f))).truncate().reduce_rank();
149 
151  const double tight_thresh=FunctionDefaults<2*NDIM>::get_thresh()*0.1;
152 
153  // Eq. (A9): g_kl = < k(1) l(2) | f(1,2) >
154  // note no (kl) symmetry here!
155  Tensor<double> g_kl(bra1_.size(),bra2_.size());
156  for (size_t k=0; k<bra1_.size(); ++k) {
157  for (size_t l=0; l<bra2_.size(); ++l) {
159  .particle1(copy(bra1_[k])).particle2(copy(bra2_[l]));
160  g_kl(k,l)=inner(f,kl);
161  }
162  }
163 // if (world.rank()==0) {print(g_kl);};
164 
165  // Eq. (A12)
166  // project out the mainly first particle: O1 (1 - 1/2 O2)
167  Function<T,2*NDIM> r1=FunctionFactory<T,2*NDIM>(world).thresh(tight_thresh);
168  for (size_t k=0; k<bra1_.size(); ++k) {
169 
170  // Eq. (A10)
171  Function<T,NDIM> h2=f.project_out(bra1_[k],0);
172 
173  // Eq. (A12)
174  for (size_t l=0; l<ket2_.size(); ++l) {
175  h2-=0.5*g_kl(k,l)*ket2_[l];
176  }
177 
178  // Eq. (A7), second term rhs
179  // the hartree product tends to be inaccurate; tighten threshold
181  r1=(r1+hartree_product(ket1_[k],h2));
183  r1.set_thresh(thresh);
184  r1.print_size("r1"+stringify(k));
185  }
186 
187  // project out the mainly second particle: O2 (1 - 1/2 O1)
188  Function<T,2*NDIM> r2=FunctionFactory<T,2*NDIM>(world).thresh(tight_thresh);
189  for (size_t l=0; l<ket2_.size(); ++l) {
190 
191  // Eq. (A11)
192  Function<T,NDIM> h1=f.project_out(bra2_[l],1);
193 
194  // Eq. (A13)
195  for (size_t k=0; k<ket1_.size(); ++k) {
196  h1-=0.5*g_kl(k,l)*ket1_[k]; // ordering g(k,l) is correct
197  }
198 
199  // Eq. (A7), third term rhs
200  // the hartree product tends to be inaccurate; tighten threshold
202  r2=(r2+hartree_product(h1,ket2_[l]));
203  r2.set_thresh(thresh);
205  r2.print_size("r2"+stringify(l));
206  }
208  Function<T,2*NDIM> result=(f-r1-r2).truncate().reduce_rank();
210 
211 // // for debugging purposes only: check orthogonality
212 // for (size_t k=0; k<hf->nocc(); ++k) {
213 // for (size_t l=0; l<hf->nocc(); ++l) {
214 // real_function_6d kl=CompositeFactory<double,6,3>(world)
215 // .particle1(copy(O1_mos[k])).particle2(copy(O2_mos[l]));
216 // g_kl(k,l)=inner(result,kl);
217 // }
218 // }
219 // if (world.rank()==0) {print(g_kl);};
220 
221  return result;
222  }
223 
224  private:
225 
227  World& world;
228 
230  std::vector<Function<T,NDIM> > ket1_, bra1_, ket2_, bra2_;
231 
232  };
233 }
234 
235 #endif /* PROJECTOR_H_ */
const double thresh
Definition: dielectric.cc:185
WorldGopInterface & gop
Global operations.
Definition: worldfwd.h:462
Main include file for MADNESS and defines Function interface.
Function< T, NDIM > & truncate(double tol=0.0, bool fence=true)
Truncate the function with optional fence. Compresses with fence if not compressed.
Definition: mra.h:577
simple projector class for 1- and 2-particle projectors
Definition: projector.h:17
const int NDIM
Definition: tdse1.cc:44
vecfuncT bra1() const
return the orbital space for the bra of particle 1
Definition: projector.h:132
const Function< T, NDIM > & compress(bool fence=true) const
Compresses the function, transforming into wavelet basis. Possible non-blocking comm.
Definition: mra.h:683
FunctionFactory< double, 6 > real_factory_6d
Definition: functypedefs.h:96
void truncate(World &world, std::vector< Function< T, NDIM > > &v, double tol=0.0, bool fence=true)
Truncates a vector of functions.
Definition: vmra.h:194
int & particle()
Definition: projector.h:38
Definition: mpreal.h:3066
NDIM & f
Definition: mra.h:2179
Function< T, 2 *NDIM > operator()(const Function< T, 2 *NDIM > &f) const
apply the strong orthogonality operator Q12 on a function f
Definition: projector.h:144
static const double & get_thresh()
Returns the default threshold.
Definition: funcdefaults.h:225
enable_if_c< NDIM==FDIM, Function< T, FDIM > >::type operator()(const Function< T, FDIM > &f) const
project f on p: |result> = | p>
Definition: projector.h:47
Function< T, KDIM+LDIM > hartree_product(const Function< T, KDIM > &left2, const Function< T, LDIM > &right2)
Performs a Hartree product on the two given low-dimensional functions.
Definition: mra.h:1657
void set_thresh(double value, bool fence=true)
Sets the vaule of the truncation threshold. Optional global fence.
Definition: mra.h:552
a SO projector class
Definition: projector.h:96
Projector(const std::vector< Function< T, NDIM > > &p, const int particle=0)
constructor with a set of orbitals to project out
Definition: projector.h:33
enable_if_c< 2 *NDIM==FDIM, Function< T, FDIM > >::type operator()(const Function< T, FDIM > &f) const
project p out of f: |result(1,2)> = sum_p | p(1)>
Definition: projector.h:72
T inner(const vecfunc< T, NDIM > &a, const vecfunc< T, NDIM > &b)
the non-linear solver requires an inner product
Definition: nemo.h:112
const std::vector< Function< T, NDIM > > & p() const
get a const reference to the orbitals
Definition: projector.h:42
World & world() const
Returns the world.
Definition: mra.h:622
Factory for facile setup of a CompositeFunctorInterface and its FuncImpl.
Definition: function_factory.h:264
const T1 &f1 return GTEST_2_TUPLE_() T(f0, f1)
const int k
Definition: dielectric.cc:184
vecfuncT ket2() const
return the orbital space for the ket of particle 2
Definition: projector.h:135
Function< T, NDIM > copy(const Function< T, NDIM > &f, const std::shared_ptr< WorldDCPmapInterface< Key< NDIM > > > &pmap, bool fence=true)
Create a new copy of the function with different distribution and optional fence. ...
Definition: mra.h:1835
Projector()
Definition: projector.h:24
void set_spaces(const vecfuncT &bra1, const vecfuncT &ket1, const vecfuncT &bra2, const vecfuncT &ket2)
set different spaces for the projectors for particle 1 and 2
Definition: projector.h:120
Projector(const Function< T, NDIM > &p, const int particle=0)
simple constructor with only one orbital to project out
Definition: projector.h:27
enable_if_c from Boost for conditionally instantiating templates based on type
Definition: enable_if.h:46
A parallel world with full functionality wrapping an MPI communicator.
Definition: worldfwd.h:416
void fence()
Synchronizes all processes in communicator AND globally ensures no pending AM or tasks.
Definition: worldgop.cc:52
StrongOrthogonalityProjector(World &world)
default ctor
Definition: projector.h:103
A multiresolution adaptive numerical function.
Definition: derivative.h:61
void print_size(const std::string name) const
print some info about this
Definition: mra.h:478
static void set_thresh(double value)
Sets the default threshold.
Definition: funcdefaults.h:232
const mpreal sum(const mpreal tab[], unsigned long int n, mp_rnd_t rnd_mode)
Definition: mpreal.cc:241
const int & particle() const
Definition: projector.h:39
Function< T, NDIM > & gaxpy(const T &alpha, const Function< Q, NDIM > &other, const R &beta, bool fence=true)
Inplace, general bi-linear operation in wavelet basis. No communication except for optional fence...
Definition: mra.h:924
#define MADNESS_EXCEPTION(msg, value)
Definition: worldexc.h:88
vecfuncT ket1() const
return the orbital space for the ket of particle 1
Definition: projector.h:129
FunctionFactory implements the named-parameter idiom for Function.
Definition: funcimpl.h:70
void set_spaces(const vecfuncT &p)
set the same spaces for the projectors for particle 1 and 2
Definition: projector.h:106
Holds machinery to set up Functions/FuncImpls using various Factories and Interfaces.
Definition: chem/atomutil.cc:45
Function< TENSOR_RESULT_TYPE(T, R), NDIM-LDIM > project_out(const Function< R, LDIM > &g, const int dim) const
project this on the low-dim function g: h(x) =
Definition: mra.h:1294
vecfuncT bra2() const
return the orbital space for the bra of particle 2
Definition: projector.h:138
void compress(World &world, const std::vector< Function< T, NDIM > > &v, bool fence=true)
Compress a vector of functions.
Definition: vmra.h:130