-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathpartition.h
193 lines (167 loc) · 3.58 KB
/
partition.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
//
// Distributed under the ITensor Library License, Version 1.1.
// (See accompanying LICENSE file.)
//
#ifndef __ITENSOR_PARTITION_H
#define __ITENSOR_PARTITION_H
//#include "itensor/global.h"
//#include "itensor/util/print_macro.h"
#include "itensor/all.h"
//#include "itensor/util/readwrite.h"
namespace itensor {
//
// Partition
//
// Represents a division of N sites
// into Nb blocks.
//
// Blocks are numbered 1,2,..,Nb.
//
//
class Partition
{
int N_ = 0;
int Nb_ = 0;
std::vector<int> bound_;
public:
Partition();
Partition(int N, int Nb);
Partition(int N, int Nb, int bound_size);
int
Nb() const { return Nb_; }
int
begin(int block) const;
int
end(int block) const;
int
size(int block) const;
bool
hasSite(int block, int s) const;
bool
hasBond(int block, int b) const;
void
read(std::istream & s);
void
write(std::ostream & s) const;
};
inline Partition::
Partition()
:
N_(0),
Nb_(0)
{ }
inline Partition::
Partition(int N, int Nb)
:
N_(N),
Nb_(Nb),
bound_(Nb_)
{
//Enlarge first block if N_ not a
//multiple of Nb_
int step = N_/Nb_,
extra = N_%Nb_/2;
bound_.at(0) = step+extra;
for(int cut_ = bound_.at(0), j = 1; j < Nb_-1; ++j)
{
cut_ += step;
bound_.at(j) = cut_;
}
}
inline Partition::
Partition(int N, int Nb, int bound_size)
:
N_(N),
Nb_(Nb),
bound_(Nb_)
{
if(Nb <= 2 && (bound_size * Nb) >= N)
{
Error("Cannot honor bound_size request");
}
//Set requested boundary size
bound_.at(0) = bound_size;
if(Nb == 2)
{
bound_.at(1) = N-bound_size;
}
else
{
//Use same algorithm as above but with
//the user-requested boundaries subtracted out
//(and 2 fewer blocks)
int Neff = N-2*bound_size,
step = Neff/(Nb_-2),
extra = Neff%(Nb_-2)/2;
if(step < 1 || Neff < 1)
{
print(Neff);
print(step);
print(extra);
Error("Ill-formed Partition");
}
bound_.at(1) = bound_size + step + extra;
for(int cut_ = bound_.at(1), j = 2; j < Nb_-1; ++j)
{
cut_ += step;
if(j == Nb_-2) cut_ += extra;
bound_.at(j) = cut_;
}
}
}
int inline Partition::
begin(int block) const
{
if(block == 1) return 1;
return bound_.at(block-2)+1;
}
int inline Partition::
end(int block) const
{
if(block == Nb_) return N_;
return bound_.at(block-1);
}
int inline Partition::
size(int block) const
{
return (end(block)-begin(block)+1);
}
bool inline Partition::
hasSite(int block, int s) const
{
return (s >= begin(block) && s <= end(block));
}
bool inline Partition::
hasBond(int block, int b) const
{
return (b >= begin(block) && b < end(block));
}
void inline Partition::
read(std::istream & s)
{
itensor::read(s,N_);
itensor::read(s,Nb_);
itensor::read(s,bound_);
}
void inline Partition::
write(std::ostream & s) const
{
itensor::write(s,N_);
itensor::write(s,Nb_);
itensor::write(s,bound_);
}
inline
std::ostream&
operator<<(std::ostream& s, const Partition& p)
{
s << "Partition:\n";
if(p.Nb() == 0) s << " (empty)\n";
for(int b = 1; b <= p.Nb(); ++b)
{
s << format("%d (%d,%d) %d\n",b,p.begin(b),p.end(b),p.size(b));
}
s << std::endl;
return s;
}
} //namespace itensor
#endif