forked from sanshar/Block
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spinblock.C
487 lines (382 loc) · 15.6 KB
/
spinblock.C
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
/*
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
*/
#include "spinblock.h"
#include "op_components.h"
#include "operatorfunctions.h"
#include "opxop.h"
#include "wavefunction.h"
#include <boost/format.hpp>
#include "distribute.h"
#ifndef SERIAL
#include <boost/mpi.hpp>
#endif
#ifdef MOLPRO
#include "global/CxOutputStream.h"
#define pout if (dmrginp.outputlevel() != 0) xout
#define cout xout
#endif
namespace SpinAdapted{
using namespace operatorfunctions;
void SpinBlock::printOperatorSummary()
{
#ifndef SERIAL
mpi::communicator world;
if (mpigetrank() != 0) {
for (std::map<opTypes, boost::shared_ptr< Op_component_base> >::const_iterator it = ops.begin(); it != ops.end(); ++it)
sendobject(it->second->get_size(), 0);
}
else {
for (std::map<opTypes, boost::shared_ptr< Op_component_base> >::const_iterator it = ops.begin(); it != ops.end(); ++it)
{
if(it->second->is_core())
cout << it->second->size()<<" : "<<it->second->get_op_string()<<" Core Operators ";
else
cout << it->second->size()<<" : "<<it->second->get_op_string()<<" Virtual Operators ";
vector<int> numops(world.size(), 0);
for (int proc = 0; proc <world.size(); proc++) {
if (proc != 0)
receiveobject(numops[proc],proc);
else
numops[proc] = it->second->get_size();
cout <<numops[proc]<<" ";
}
cout << endl;
}
}
#else
for (std::map<opTypes, boost::shared_ptr< Op_component_base> >::const_iterator it = ops.begin(); it != ops.end(); ++it)
{
if(it->second->is_core())
cout << it->second->size()<<" : "<<it->second->get_op_string()<<" Core Operators ";
else
cout << it->second->size()<<" : "<<it->second->get_op_string()<<" Virtual Operators ";
cout << endl;
}
#endif
}
ostream& operator<< (ostream& os, const SpinBlock& b)
{
os << " Sites :: ";
for (int i = 0; i < b.sites.size(); ++i) { os << b.sites[i] << " "; }
if (dmrginp.outputlevel() > 0) {
os << endl;
os << b.stateInfo;
}
else {
os <<" # states: "<<b.stateInfo.totalStates<<endl;
}
return os;
}
SpinBlock::SpinBlock () :
localstorage(false),
name (rand()),
hasMemoryAllocated (false),
direct(false), complementary(false), normal(true), leftBlock(0), rightBlock(0) { }
SpinBlock::SpinBlock(int start, int finish, bool is_complement) :
name (rand()),
hasMemoryAllocated (false),
direct(false), leftBlock(0), rightBlock(0)
{
complementary = is_complement;
normal = !is_complement;
default_op_components(is_complement);
std::vector<int> sites;
if (dmrginp.use_partial_two_integrals()) {
if (start != finish) {
cout << "Cannot use partial two electron integrals, when making spin block with more than two orbitals"<<endl;
abort();
}
std::vector<int> o;
for (int i=dmrginp.spatial_to_spin()[start]; i<dmrginp.spatial_to_spin()[start+1]; i+=2)
o.push_back(i/2);
twoInt = boost::shared_ptr<PartialTwoElectronArray> (new PartialTwoElectronArray(o));
twoInt->Load(dmrginp.load_prefix());
#ifndef SERIAL
mpi::communicator world;
PartialTwoElectronArray& ar = dynamic_cast<PartialTwoElectronArray&>(*twoInt.get());
mpi::broadcast(world, ar, 0);
#endif
}
else
twoInt = boost::shared_ptr<TwoElectronArray>( &v_2, boostutils::null_deleter());
int lower = min(start, finish);
int higher = max(start, finish);
sites.resize(higher - lower + 1);
for (int i=0; i < sites.size(); i++)
sites[i] = lower + i;
BuildTensorProductBlock(sites);
}
SpinBlock::SpinBlock (const SpinBlock& b) { *this = b; }
SpinBlock::SpinBlock(const StateInfo& s)
{
stateInfo = s;
sites.resize(0);
}
void SpinBlock::BuildTensorProductBlock(std::vector<int>& new_sites)
{
if (twoInt.get() == 0 && dmrginp.use_partial_two_integrals()) { //this is when dummy block is being added for non zero spin
std::vector<int> o;
for (int i=dmrginp.spatial_to_spin()[new_sites[0]]; i<dmrginp.spatial_to_spin()[new_sites[new_sites.size()-1]+1]; i+=2)
o.push_back(i/2);
twoInt = boost::shared_ptr<PartialTwoElectronArray> (new PartialTwoElectronArray(o));
twoInt->Load(dmrginp.load_prefix());
#ifndef SERIAL
mpi::communicator world;
PartialTwoElectronArray& ar = dynamic_cast<PartialTwoElectronArray&>(*twoInt.get());
mpi::broadcast(world, ar, 0);
//world.broadcast(twoInt);
#endif
}
else if (twoInt.get() == 0)
twoInt = boost::shared_ptr<TwoElectronArray>( &v_2, boostutils::null_deleter());
name = get_name();
sites = new_sites;
std::vector< Csf > dets = CSFUTIL::spinfockstrings(new_sites);
stateInfo = StateInfo(dets);
setstoragetype(LOCAL_STORAGE);
complementary_sites = make_complement(sites);
build_iterators();
std::vector< std::vector<Csf> > ladders; ladders.resize(dets.size());
for (int i=0; i< dets.size(); i++)
ladders[i] = dets[i].spinLadder(min(2,dets[i].S));
build_operators(dets, ladders);
}
std::vector<int> SpinBlock::make_complement(const std::vector<int>& sites)
{
std::vector<int> complementary_sites;
for (int i=0; i<dmrginp.last_site(); ++i)
if (find(sites.begin(), sites.end(), i) == sites.end())
complementary_sites.push_back(i);
return complementary_sites;
}
void SpinBlock::build_iterators()
{
for (std::map<opTypes, boost::shared_ptr< Op_component_base> >::iterator it = ops.begin(); it != ops.end(); ++it)
{
it->second->build_iterators(*this);
}
}
void SpinBlock::build_operators(std::vector< Csf >& dets, std::vector< std::vector<Csf> >& ladders)
{
for (std::map<opTypes, boost::shared_ptr< Op_component_base> >::iterator it = ops.begin(); it != ops.end(); ++it)
{
if(it->second->is_core())
it->second->build_csf_operators(dets, ladders, *this);
}
}
void SpinBlock::build_operators()
{
for (std::map<opTypes, boost::shared_ptr< Op_component_base> >::iterator it = ops.begin(); it != ops.end(); ++it)
{
if(it->second->is_core()) {
it->second->build_operators(*this);
}
}
}
void SpinBlock::clear()
{
for (std::map<opTypes, boost::shared_ptr< Op_component_base> >::iterator it = ops.begin(); it != ops.end(); ++it)
it->second->clear();
}
void SpinBlock::BuildSumBlockSkeleton(int condition, SpinBlock& lBlock, SpinBlock& rBlock, StateInfo* compState)
{
name = get_name();
if (dmrginp.outputlevel() > 0)
pout << "\t\t\t Building Sum Block " << name << endl;
leftBlock = &lBlock;
rightBlock = &rBlock;
sites.reserve (lBlock.sites.size () + rBlock.sites.size ());
if (dmrginp.use_partial_two_integrals()) {
if (rBlock.sites.size() == 1) {
std::vector<int> o;
for (int i=dmrginp.spatial_to_spin().at(rBlock.sites[0]); i<dmrginp.spatial_to_spin().at(rBlock.sites[0]+1); i+=2)
o.push_back(i/2);
twoInt = boost::shared_ptr<PartialTwoElectronArray> (new PartialTwoElectronArray(o));
twoInt->Load(dmrginp.load_prefix());
#ifndef SERIAL
mpi::communicator world;
PartialTwoElectronArray& ar = dynamic_cast<PartialTwoElectronArray&>(*twoInt.get());
mpi::broadcast(world, ar, 0);
#endif
}
//cout << "Cannot use partial two electron integrals, when the dot block has more than one orbital"<<endl;
//abort();
}
else
twoInt = boost::shared_ptr<TwoElectronArray>( &v_2, boostutils::null_deleter());
sites = lBlock.sites;
copy (rBlock.sites.begin(), rBlock.sites.end (), back_inserter (sites));
sort(sites.begin(), sites.end());
complementary_sites = make_complement(sites);
if (dmrginp.outputlevel() > 0) {
pout << "\t\t\t ";
for (int i = 0; i < sites.size(); ++i) pout << sites[i] << " ";
pout << endl;
}
TensorProduct (lBlock.stateInfo, rBlock.stateInfo, stateInfo, condition, compState);
if (condition != PARTICLE_SPIN_NUMBER_CONSTRAINT)
stateInfo.CollectQuanta();
}
void SpinBlock::BuildSumBlock(int condition, SpinBlock& lBlock, SpinBlock& rBlock, StateInfo* compState)
{
dmrginp.buildsumblock -> start();
BuildSumBlockSkeleton(condition, lBlock, rBlock, compState);
build_iterators();
dmrginp.buildblockops -> start();
build_operators();
dmrginp.buildblockops -> stop();
dmrginp.buildsumblock -> stop();
}
void SpinBlock::operator= (const SpinBlock& b)
{
localstorage = b.localstorage;
name = b.name;
complementary = b.is_complementary();
normal = b.is_normal();
loopblock = b.is_loopblock();
hasMemoryAllocated = b.hasMemoryAllocated;
sites = b.sites;
complementary_sites = b.complementary_sites;
direct = b.is_direct();
stateInfo = b.stateInfo;
leftBlock = b.leftBlock;
rightBlock = b.rightBlock;
twoInt = b.twoInt;
ops = b.ops;
}
void SpinBlock::multiplyH(Wavefunction& c, Wavefunction* v, int num_threads) const
{
SpinBlock* loopBlock=(leftBlock->is_loopblock()) ? leftBlock : rightBlock;
SpinBlock* otherBlock = loopBlock == leftBlock ? rightBlock : leftBlock;
Wavefunction *v_array=0, *v_distributed=0, *v_add=0;
int maxt = 1;
initiateMultiThread(v, v_array, v_distributed, MAX_THRD);
dmrginp.oneelecT -> start();
dmrginp.s0time -> start();
boost::shared_ptr<SparseMatrix> op = leftBlock->get_op_array(HAM).get_local_element(0)[0];
TensorMultiply(leftBlock, *op, this, c, *v, dmrginp.effective_molecule_quantum() ,1.0, MAX_THRD);
op = rightBlock->get_op_array(HAM).get_local_element(0)[0];
TensorMultiply(rightBlock, *op, this, c, *v, dmrginp.effective_molecule_quantum(), 1.0, MAX_THRD);
dmrginp.s0time -> stop();
#ifndef SERIAL
boost::mpi::communicator world;
int size = world.size();
#endif
dmrginp.s1time -> start();
v_add = leftBlock->get_op_array(CRE_CRE_DESCOMP).is_local() ? v_array : v_distributed;
Functor f = boost::bind(&opxop::cxcddcomp, leftBlock, _1, this, ref(c), v_add, dmrginp.effective_molecule_quantum() );
for_all_multithread(rightBlock->get_op_array(CRE), f);
v_add = rightBlock->get_op_array(CRE_CRE_DESCOMP).is_local() ? v_array : v_distributed;
f = boost::bind(&opxop::cxcddcomp, rightBlock, _1, this, ref(c), v_add, dmrginp.effective_molecule_quantum() );
for_all_multithread(leftBlock->get_op_array(CRE), f);
dmrginp.s1time -> stop();
dmrginp.oneelecT -> stop();
dmrginp.twoelecT -> start();
if (dmrginp.hamiltonian() == QUANTUM_CHEMISTRY) {
dmrginp.s0time -> start();
v_add = otherBlock->get_op_array(CRE_DESCOMP).is_local() ? v_array : v_distributed;
f = boost::bind(&opxop::cdxcdcomp, otherBlock, _1, this, ref(c), v_add, dmrginp.effective_molecule_quantum() );
for_all_multithread(loopBlock->get_op_array(CRE_DES), f);
v_add = otherBlock->get_op_array(DES_DESCOMP).is_local() ? v_array : v_distributed;
f = boost::bind(&opxop::ddxcccomp, otherBlock, _1, this, ref(c), v_add, dmrginp.effective_molecule_quantum() );
for_all_multithread(loopBlock->get_op_array(CRE_CRE), f);
dmrginp.s0time -> stop();
}
dmrginp.twoelecT -> stop();
accumulateMultiThread(v, v_array, v_distributed, MAX_THRD);
}
void SpinBlock::diagonalH(DiagonalMatrix& e) const
{
SpinBlock* loopBlock=(leftBlock->is_loopblock()) ? leftBlock : rightBlock;
SpinBlock* otherBlock = loopBlock == leftBlock ? rightBlock : leftBlock;
DiagonalMatrix *e_array=0, *e_distributed=0, *e_add=0;
initiateMultiThread(&e, e_array, e_distributed, MAX_THRD);
boost::shared_ptr<SparseMatrix> op =leftBlock->get_op_array(HAM).get_local_element(0)[0]->getworkingrepresentation(this);
TensorTrace(leftBlock, *op, this, &(get_stateInfo()), e, 1.0);
op = rightBlock->get_op_array(HAM).get_local_element(0)[0]->getworkingrepresentation(this);
TensorTrace(rightBlock, *op, this, &(get_stateInfo()), e, 1.0);
#ifndef SERIAL
boost::mpi::communicator world;
int size = world.size();
#endif
std::vector< std::vector<int> > indices;
e_add = leftBlock->get_op_array(CRE_CRE_DESCOMP).is_local() ? e_array : e_distributed;
indices = rightBlock->get_op_array(CRE).get_array();
Functor f = boost::bind(&opxop::cxcddcomp_d, leftBlock, _1, this, e_add);
//for_all_multithread(rightBlock->get_op_array(CRE), f); //not needed in diagonal
e_add = rightBlock->get_op_array(CRE_CRE_DESCOMP).is_local() ? e_array : e_distributed;
indices = leftBlock->get_op_array(CRE).get_array();
f = boost::bind(&opxop::cxcddcomp_d, rightBlock, _1, this, e_add);
//for_all_multithread(leftBlock->get_op_array(CRE), f); //not needed in diagonal
if (dmrginp.hamiltonian() == QUANTUM_CHEMISTRY) {
e_add = otherBlock->get_op_array(CRE_DESCOMP).is_local() ? e_array : e_distributed;
indices = loopBlock->get_op_array(CRE_DES).get_array();
f = boost::bind(&opxop::cdxcdcomp_d, otherBlock, _1, this, e_add);
for_all_multithread(loopBlock->get_op_array(CRE_DES), f);
e_add = otherBlock->get_op_array(DES_DESCOMP).is_local() ? e_array : e_distributed;
indices = loopBlock->get_op_array(CRE_CRE).get_array();
f = boost::bind(&opxop::ddxcccomp_d, otherBlock, _1, this, e_add);
//for_all_multithread(loopBlock->get_op_array(CRE_CRE), f); //not needed in diagonal
}
accumulateMultiThread(&e, e_array, e_distributed, MAX_THRD);
}
void SpinBlock::BuildSlaterBlock (std::vector<int> sts, std::vector<SpinQuantum> qnumbers, std::vector<int> distribution,
bool random, const bool haveNormops)
{
name = get_name();
sites = sts;
complementary_sites = make_complement(sites);
assert (sites.size () > 0);
sort (sites.begin (), sites.end ());
default_op_components(!haveNormops);
setstoragetype(DISTRIBUTED_STORAGE);
std::vector< Csf > dets;
std::vector< Csf > det_ex;
Timer slatertimer;
for (int i = 0; i < qnumbers.size (); ++i)
{
if (distribution [i] == 0 || (qnumbers [i].get_n() > 2*sites.size ())) continue;
det_ex = Csf::distribute (qnumbers [i].get_n(), qnumbers [i].get_s(), IrrepVector(qnumbers [i].get_symm().getirrep(), 0) , sites [0],
sites [0] + sites.size (), dmrginp.last_site());
multimap <double, Csf > slater_emap;
for (int j = 0; j < det_ex.size(); ++j) {
slater_emap.insert (pair <double, Csf > (csf_energy (det_ex[j]), det_ex[j]));
}
multimap <double, Csf >::iterator m = slater_emap.begin();
int sz = det_ex.size();
det_ex.resize (min (distribution [i], sz));
for (int j = 0; j < det_ex.size(); ++j)
{
det_ex[j] = m->second;
++m;
}
copy (det_ex.begin(), det_ex.end(), back_inserter (dets));
}
std::multimap<SpinQuantum, Csf > tmp;
for (int i = 0; i < dets.size (); ++i) {
tmp.insert(pair<SpinQuantum, Csf > (SpinQuantum ( (dets [i]).n, (dets [i]).S, (dets[i]).sym_is()), dets[i]));
}
std::multimap<SpinQuantum, Csf >::iterator tmpiter = tmp.begin();
dets.clear();
for (; tmpiter != tmp.end();)
{
dets.push_back(tmpiter->second);
tmpiter++;
}
stateInfo = StateInfo (dets);
twoInt = boost::shared_ptr<TwoElectronArray>( &v_2, boostutils::null_deleter());
build_iterators();
if (dmrginp.outputlevel() > 0)
pout << "\t\t\t time in slater distribution " << slatertimer.elapsedwalltime() << " " << slatertimer.elapsedcputime() << endl;
std::vector< std::vector<Csf> > ladders; ladders.resize(dets.size());
for (int i=0; i< dets.size(); i++)
ladders[i] = dets[i].spinLadder(min(2, dets[i].S));
build_operators(dets, ladders);
if (dmrginp.outputlevel() > 0)
pout << "\t\t\t time in slater operator build " << slatertimer.elapsedwalltime() << " " << slatertimer.elapsedcputime() << endl;
}
}