forked from sanshar/Block
-
Notifications
You must be signed in to change notification settings - Fork 1
/
op_components.h
394 lines (339 loc) · 17 KB
/
op_components.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/*
Developed by Sandeep Sharma and Garnet K.-L. Chan, 2012
Copyright (c) 2012, Garnet K.-L. Chan
This program is integrated in Molpro with the permission of
Sandeep Sharma and Garnet K.-L. Chan
*/
#ifndef SPIN_OP_COMPONENTS_H
#define SPIN_OP_COMPONENTS_H
#include <boost/function.hpp>
#include <boost/functional.hpp>
#include <boost/format.hpp>
#include <para_array.h>
#include <boost/shared_ptr.hpp>
#include <list>
#include <boost/serialization/base_object.hpp>
#include <boost/serialization/serialization.hpp>
#include <boost/serialization/export.hpp>
#include "Operators.h"
#include "operatorloops.h"
#include <string>
#include "three_index_ops.h"
#include "four_index_ops.h"
#include "para_array_3d.h"
#include "para_array_4d.h"
namespace SpinAdapted{
class SpinBlock;
//===========================================================================================================================================================
//choose the type of array for different types of Operators
template <class T> struct ChooseArray {
typedef para_array_1d<std::vector<boost::shared_ptr<SparseMatrix> > > ArrayType;
};
template <> struct ChooseArray<Cre> {
typedef para_array_1d<std::vector<boost::shared_ptr<Cre> > > ArrayType; // Cre, CreDes, etc. are sparse matrices: <a|a_i^\dagger|b>
};
template <> struct ChooseArray<Des> {
typedef para_array_1d<std::vector<boost::shared_ptr<Des> > > ArrayType;
};
template <> struct ChooseArray<CreDes> {
typedef para_array_triang_2d<std::vector<boost::shared_ptr<CreDes> > > ArrayType;
};
template <> struct ChooseArray<DesCre> {
typedef para_array_triang_2d<std::vector<boost::shared_ptr<DesCre> > > ArrayType;
};
template <> struct ChooseArray<CreCre> {
typedef para_array_triang_2d<std::vector<boost::shared_ptr<CreCre> > > ArrayType;
};
template <> struct ChooseArray<DesDes> {
typedef para_array_triang_2d<std::vector<boost::shared_ptr<DesDes> > > ArrayType;
};
template <> struct ChooseArray<CreDesComp> {
typedef para_array_triang_2d<std::vector<boost::shared_ptr<CreDesComp> > > ArrayType;
};
template <> struct ChooseArray<DesCreComp> {
typedef para_array_triang_2d<std::vector<boost::shared_ptr<DesCreComp> > > ArrayType;
};
template <> struct ChooseArray<DesDesComp> {
typedef para_array_triang_2d<std::vector<boost::shared_ptr<DesDesComp> > > ArrayType;
};
template <> struct ChooseArray<CreCreComp> {
typedef para_array_triang_2d<std::vector<boost::shared_ptr<CreCreComp> > > ArrayType;
};
template <> struct ChooseArray<CreCreDesComp> {
typedef para_array_1d<std::vector<boost::shared_ptr<CreCreDesComp> > > ArrayType;
};
template <> struct ChooseArray<CreDesDesComp> {
typedef para_array_1d<std::vector<boost::shared_ptr<CreDesDesComp> > > ArrayType;
};
template <> struct ChooseArray<Ham> {
typedef para_array_0d<std::vector<boost::shared_ptr<Ham> > > ArrayType;
};
template <> struct ChooseArray<Overlap> {
typedef para_array_0d<std::vector<boost::shared_ptr<Overlap> > > ArrayType;
};
template <> struct ChooseArray<RI3index> {
typedef para_array_3d<std::vector<boost::shared_ptr<RI3index> > > ArrayType;
};
template <> struct ChooseArray<RI4index> {
typedef para_array_4d<std::vector<boost::shared_ptr<RI4index> > > ArrayType;
};
// 3PDM
template <> struct ChooseArray<CreCreDes> {
typedef para_array_3d<std::vector<boost::shared_ptr<CreCreDes> > > ArrayType;
};
template <> struct ChooseArray<CreDesDes> {
typedef para_array_3d<std::vector<boost::shared_ptr<CreDesDes> > > ArrayType;
};
template <> struct ChooseArray<CreDesCre> {
typedef para_array_3d<std::vector<boost::shared_ptr<CreDesCre> > > ArrayType;
};
template <> struct ChooseArray<CreCreCre> {
typedef para_array_3d<std::vector<boost::shared_ptr<CreCreCre> > > ArrayType;
};
// 4PDM
template <> struct ChooseArray<DesCreDes> {
typedef para_array_3d<std::vector<boost::shared_ptr<DesCreDes> > > ArrayType;
};
template <> struct ChooseArray<DesDesCre> {
typedef para_array_3d<std::vector<boost::shared_ptr<DesDesCre> > > ArrayType;
};
template <> struct ChooseArray<DesCreCre> {
typedef para_array_3d<std::vector<boost::shared_ptr<DesCreCre> > > ArrayType;
};
template <> struct ChooseArray<DesDesDes> {
typedef para_array_3d<std::vector<boost::shared_ptr<DesDesDes> > > ArrayType;
};
template <> struct ChooseArray<CreCreDesDes> {
typedef para_array_4d<std::vector<boost::shared_ptr<CreCreDesDes> > > ArrayType;
};
template <> struct ChooseArray<CreDesCreDes> {
typedef para_array_4d<std::vector<boost::shared_ptr<CreDesCreDes> > > ArrayType;
};
template <> struct ChooseArray<CreDesDesCre> {
typedef para_array_4d<std::vector<boost::shared_ptr<CreDesDesCre> > > ArrayType;
};
template <> struct ChooseArray<CreDesDesDes> {
typedef para_array_4d<std::vector<boost::shared_ptr<CreDesDesDes> > > ArrayType;
};
template <> struct ChooseArray<CreCreCreDes> {
typedef para_array_4d<std::vector<boost::shared_ptr<CreCreCreDes> > > ArrayType;
};
template <> struct ChooseArray<CreCreDesCre> {
typedef para_array_4d<std::vector<boost::shared_ptr<CreCreDesCre> > > ArrayType;
};
template <> struct ChooseArray<CreDesCreCre> {
typedef para_array_4d<std::vector<boost::shared_ptr<CreDesCreCre> > > ArrayType;
};
template <> struct ChooseArray<CreCreCreCre> {
typedef para_array_4d<std::vector<boost::shared_ptr<CreCreCreCre> > > ArrayType;
};
//===========================================================================================================================================================
class Op_component_base
{
private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & m_core & m_deriv;
}
protected:
bool m_core;
bool m_deriv;
public:
virtual void build_iterators(SpinBlock& b)=0;
virtual void build_operators(SpinBlock& b)=0;
virtual void build_csf_operators(std::vector< Csf >& dets, std::vector< std::vector<Csf> >& ladders, SpinBlock& b) = 0;
virtual void renormalise_transform(const std::vector<Matrix>& rotateMatrix, const StateInfo* s) =0;
virtual void renormalise_transform(const std::vector<Matrix>& leftMat, const StateInfo* bra, const std::vector<Matrix>& rightMat, const StateInfo* ket) =0;
virtual void build_and_renormalise_operators(SpinBlock&b, const opTypes &ot, const std::vector<Matrix>& rotateMatrix, const StateInfo *stateinfo) =0;
virtual void build_and_renormalise_operators(SpinBlock&b, const opTypes &ot, const std::vector<Matrix>& leftMat, const StateInfo *bra, const std::vector<Matrix>& rightMat, const StateInfo *ket) =0;
//virtual string type_name() = 0;
virtual int get_size() const =0;
virtual int size() const=0;
virtual void clear() =0;
virtual std::vector<boost::shared_ptr<SparseMatrix> > get_local_element(int i) =0;
virtual std::vector<boost::shared_ptr<SparseMatrix> > get_global_element(int i)=0;
const bool &is_core() const {return m_core;}
const bool &is_deriv() const {return m_deriv;}
void set_core(bool is_core) {m_core = is_core;}
virtual void add_local_indices(int i, int j=-1, int k=-1) {};
virtual bool is_local() const = 0;
virtual bool& set_local() = 0;
virtual std::vector< std::vector<int> > get_array() const =0;
virtual const std::vector<boost::shared_ptr<SparseMatrix> > get_element(int i, int j=-1, int k=-1, int l=-1) const = 0;
virtual std::vector<boost::shared_ptr<SparseMatrix> > get_element(int i, int j=-1, int k=-1, int l=-1) = 0;
virtual bool has(int i, int j=-1, int k=-1, int l=-1) const = 0;
virtual bool has_local_index(int i, int j=-1, int k=-1, int l=-1) const = 0;
virtual boost::shared_ptr<SparseMatrix> get_op_rep(const std::vector<SpinQuantum>& s, int i=-1, int j=-1, int k=-1, int l=-1) = 0;
virtual const boost::shared_ptr<SparseMatrix> get_op_rep(const std::vector<SpinQuantum>& s, int i=-1, int j=-1, int k=-1, int l=-1) const = 0;
virtual boost::shared_ptr<SparseMatrix> get_op_rep(const std::map< std::string, std::vector<SpinQuantum> >& s, int i=-1, int j=-1, int k=-1, int l=-1) = 0;
virtual const boost::shared_ptr<SparseMatrix> get_op_rep(const std::map< std::string, std::vector<SpinQuantum> >& s, int i=-1, int j=-1, int k=-1, int l=-1) const = 0;
virtual std::string get_op_string() const = 0;
virtual std::string get_filename() const = 0;
virtual ~Op_component_base() {}
};
//===========================================================================================================================================================
template <class Op> class Op_component : public Op_component_base
{
private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & boost::serialization::base_object<Op_component_base>(*this);
ar.register_type(static_cast<Op *>(NULL));
ar & m_op & uniqueID;
}
protected:
typedef typename ChooseArray<Op>::ArrayType paraarray;
typedef Op OpType;
paraarray m_op;
int uniqueID;
// Use for unique filename for NPDM disk-based operator storage
static int nIDgenerator;
public:
Op_component() { m_deriv=false; uniqueID = nIDgenerator++; }
Op_component(bool core) { m_core=core; m_deriv=false; uniqueID = nIDgenerator++; }
std::string get_op_string() const;
bool& set_local() {return m_op.set_local();}
bool is_local() const {return m_op.is_local();}
int get_size() const {return m_op.local_nnz();}
int size() const {return m_op.global_nnz();}
bool has(int i, int j=-1, int k=-1, int l=-1) const {return m_op.has(i, j, k, l);}
bool has_local_index(int i, int j=-1, int k=-1, int l=-1) const {return m_op.has_local_index(i, j, k, l);}
virtual void add_local_indices(int i, int j=-1, int k=-1);
void clear(){m_op.clear();}
void build_iterators(SpinBlock& b);
void build_operators(SpinBlock& b)
{ singlethread_build(*this, b); }
// Note for NPDM higher-index operators, there are template specializations for these functions
void build_and_renormalise_operators(SpinBlock&b, const opTypes &ot, const std::vector<Matrix>& rotateMatrix, const StateInfo *stateinfo)
{for_all_operators_multithread(*this, bind(&SparseMatrix::build_and_renormalise_transform, _1, &b, boost::ref(ot), boost::ref(rotateMatrix), stateinfo));}
void build_and_renormalise_operators(SpinBlock&b, const opTypes &ot, const std::vector<Matrix>& leftMat, const StateInfo *bra, const std::vector<Matrix>& rightMat, const StateInfo *ket)
{for_all_operators_multithread(*this, bind(&SparseMatrix::build_and_renormalise_transform, _1, &b, boost::ref(ot), boost::ref(leftMat), bra, boost::ref(rightMat), ket));}
//-----------------------------------------------------------------------------------------------------------------------------------------------------------
// Use for unique filename for NPDM disk-based operator storage
std::string get_filename() const
{
std::string file;
file = str( boost::format("%s%s%s%s%d%s%d%s") % dmrginp.load_prefix() % "/" % get_op_string() % "_" % uniqueID % "_p" % mpigetrank() % ".tmp" );
return file;
}
//-----------------------------------------------------------------------------------------------------------------------------------------------------------
void build_csf_operators(std::vector< Csf >& c, vector< vector<Csf> >& ladders, SpinBlock& b)
{
for_all_operators_multithread( *this, bind(&SparseMatrix::buildUsingCsf, _1, boost::ref(b), boost::ref(ladders), boost::ref(c)) );
}
//-----------------------------------------------------------------------------------------------------------------------------------------------------------
void renormalise_transform(const std::vector<Matrix>& rotateMatrix, const StateInfo* s)
{
if ( m_op.num_indices() > 2 ) {
// renormalise_transform_on_disk( rotateMatrix, s );
assert(false);
}
else {
// For operators built in core
for_all_operators_multithread( *this, bind(&SparseMatrix::renormalise_transform, _1, boost::ref(rotateMatrix), s) );
}
}
//-----------------------------------------------------------------------------------------------------------------------------------------------------------
void renormalise_transform( const std::vector<Matrix>& leftMat, const StateInfo* bra, const std::vector<Matrix>& rightMat, const StateInfo* ket)
{
if ( m_op.num_indices() > 2 ) {
// renormalise_transform_on_disk( ... )
assert(false);
}
else {
// For operators built in core
for_all_operators_multithread( *this, bind(&SparseMatrix::renormalise_transform, _1, boost::ref(leftMat), bra, boost::ref(rightMat), ket) );
}
}
//-----------------------------------------------------------------------------------------------------------------------------------------------------------
std::vector<boost::shared_ptr<SparseMatrix> > get_local_element(int i)
{
std::vector<boost::shared_ptr<SparseMatrix> > vec(m_op.get_local_element(i).size());
for (int l=0; l<vec.size(); l++)
vec[l] = m_op.get_local_element(i)[l];
return vec;
}
//-----------------------------------------------------------------------------------------------------------------------------------------------------------
std::vector<boost::shared_ptr<SparseMatrix> > get_global_element(int i)
{
std::vector<boost::shared_ptr<SparseMatrix> > vec(m_op.get_global_element(i).size());
for (int l=0; l<vec.size(); l++)
vec[l] = m_op.get_global_element(i)[l];
return vec;
}
//-----------------------------------------------------------------------------------------------------------------------------------------------------------
std::vector< std::vector<int> > get_array() const
{
std::vector<int> orbs(2);
std::vector< std::vector<int> > ret_val(m_op.local_nnz());
for (int i=0; i<m_op.local_nnz(); i++)
ret_val[i] = m_op.unmap_local_index(i);
return ret_val;
}
//-----------------------------------------------------------------------------------------------------------------------------------------------------------
const std::vector<boost::shared_ptr<SparseMatrix> > get_element(int i, int j=-1, int k=-1, int l=-1) const
{
std::vector<boost::shared_ptr<SparseMatrix> > vec(m_op(i,j,k,l).size());
for (int p=0; p<vec.size(); p++)
vec[p] = m_op(i,j,k,l)[p];
return vec;
}
//-----------------------------------------------------------------------------------------------------------------------------------------------------------
std::vector<boost::shared_ptr<SparseMatrix> > get_element(int i, int j=-1, int k=-1, int l=-1)
{
std::vector<boost::shared_ptr<SparseMatrix> > vec(m_op(i,j,k,l).size());
for (int p=0; p<vec.size(); p++)
vec[p] = m_op(i,j,k,l)[p];
return vec;
}
//-----------------------------------------------------------------------------------------------------------------------------------------------------------
boost::shared_ptr<SparseMatrix> get_op_rep(const std::vector<SpinQuantum>& s, int i=-1, int j=-1, int k=-1, int l=-1)
{
Op* o = 0;
std::vector<boost::shared_ptr<Op> >& vec = m_op(i,j,k,l);
for (int p=0; p<vec.size(); p++) {
if (s == vec[p]->get_deltaQuantum())
return m_op(i,j,k,l)[p];
}
return boost::shared_ptr<Op>(o);
}
//-----------------------------------------------------------------------------------------------------------------------------------------------------------
const boost::shared_ptr<SparseMatrix> get_op_rep(const std::vector<SpinQuantum>& s, int i=-1, int j=-1, int k=-1, int l=-1) const
{
Op* o = 0;
const std::vector<boost::shared_ptr<Op> >& vec = m_op(i,j,k,l);
for (int p=0; p<vec.size(); p++)
if (s == vec[p]->get_deltaQuantum())
return m_op(i,j,k,l)[p];
return boost::shared_ptr<Op>(o);
}
//-----------------------------------------------------------------------------------------------------------------------------------------------------------
boost::shared_ptr<SparseMatrix> get_op_rep(const std::map< std::string, std::vector<SpinQuantum> >& s, int i=-1, int j=-1, int k=-1, int l=-1)
{
Op* o = 0;
std::vector<boost::shared_ptr<Op> >& vec = m_op(i,j,k,l);
for (int p=0; p<vec.size(); p++) {
if (s == vec[p]->get_quantum_ladder())
return m_op(i,j,k,l)[p];
}
return boost::shared_ptr<Op>(o);
}
//-----------------------------------------------------------------------------------------------------------------------------------------------------------
const boost::shared_ptr<SparseMatrix> get_op_rep(const std::map< std::string, std::vector<SpinQuantum> >& s, int i=-1, int j=-1, int k=-1, int l=-1) const
{
Op* o = 0;
const std::vector<boost::shared_ptr<Op> >& vec = m_op(i,j,k,l);
for (int p=0; p<vec.size(); p++)
if (s == vec[p]->get_quantum_ladder())
return m_op(i,j,k,l)[p];
return boost::shared_ptr<Op>(o);
}
};
//===========================================================================================================================================================
template <class Op> int Op_component<Op>::nIDgenerator = 1;
}
#endif