-
Notifications
You must be signed in to change notification settings - Fork 4
/
lattice_2dsqr.cpp
375 lines (305 loc) · 8.14 KB
/
lattice_2dsqr.cpp
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
/*
* Copyright (c) 2013, Robert Rueger <[email protected]>
*
* This file is part of hVMC.
*
* hVMC is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* hVMC is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with hVMC. If not, see <http://www.gnu.org/licenses/>.
*/
#include "lattice_2dsqr.hpp"
#include <algorithm>
#if VERBOSE >= 1
# include <iostream>
#endif
# include "utils.hpp"
using namespace std;
Lattice2DSquare::Lattice2DSquare( unsigned int L_init )
: Lattice( L_init ), S( uintsqrt( L_init ) )
{
assert( is_perfect_square( L_init ) );
}
int Lattice2DSquare::x( Lattice::spindex l ) const
{
assert( l < 2 * L );
// we don't have to convert the spindex to an index first, because
// it does not make any difference for the position in x
return l % S;
}
int Lattice2DSquare::y( Lattice::spindex l ) const
{
assert( l < 2 * L );
return get_index_from_spindex( l ) / S;
}
int Lattice2DSquare::d( int p1, int p2 ) const
{
assert( p1 >= 0 && p2 >= 0 );
int dist = p2 - p1;
// wrap large d around the boundaries
if ( dist > static_cast<int>( S ) / 2 ) {
dist -= S ;
}
if ( dist < -1 * static_cast<int>( S ) / 2 ) {
dist += S;
}
return dist;
}
void Lattice2DSquare::get_Xnn(
Lattice::spindex l, unsigned int X, std::vector<Lattice::spindex>* outbuf ) const
{
assert( l < 2 * L );
assert( X == 1 || X == 2 || X == 3 );
if ( X == 1 ) {
get_1nn( l, outbuf );
} else if ( X == 2 ) {
get_2nn( l, outbuf );
} else { /* X == 3 */
assert( X == 3 );
get_3nn( l, outbuf );
}
}
void Lattice2DSquare::get_1nn(
Lattice::spindex l, vector<Lattice::spindex>* outbuf ) const
{
outbuf->resize( 4 );
const unsigned int xl = x( l );
const unsigned int yl = y( l );
// add left neighbor
if ( xl == 0 ) {
( *outbuf )[0] = l + S - 1;
} else {
( *outbuf )[0] = l - 1;
}
// add right neighbor
if ( xl == S - 1 ) {
( *outbuf )[1] = l + 1 - S;
} else {
( *outbuf )[1] = l + 1;
}
// add bottom neighbor
if ( yl == 0 ) {
( *outbuf )[2] = l + L - S;
} else {
( *outbuf )[2] = l - S;
}
// add top neighbor
if ( yl == S - 1 ) {
( *outbuf )[3] = l + S - L;
} else {
( *outbuf )[3] = l + S;
}
}
void Lattice2DSquare::get_2nn(
Lattice::spindex l, vector<Lattice::spindex>* outbuf ) const
{
outbuf->resize( 4 );
const unsigned int xl = x( l );
const unsigned int yl = y( l );
// add bottom left neighbor
if ( xl == 0 ) {
// in left column
if ( yl == 0 ) {
// bottom left corner
( *outbuf )[0] = l + L - 1;
} else {
( *outbuf )[0] = l - 1;
}
} else if ( yl == 0 ) {
// in bottom row
// (but NOT in bottom left corner!)
( *outbuf )[0] = l + L - S - 1;
} else {
// in the center
( *outbuf )[0] = l - S - 1;
}
// add bottom right neighbor
if ( xl == S - 1 ) {
// in right column
if ( yl == 0 ) {
// bottom right corner
( *outbuf )[1] = l + L + 1 - 2 * S;
} else {
( *outbuf )[1] = l + 1 - 2 * S;
}
} else if ( yl == 0 ) {
// in bottom row
// (but NOT in bottom right corner!)
( *outbuf )[1] = l + L + 1 - S;
} else {
// in the center
( *outbuf )[1] = l + 1 - S;
}
// add top right neighbor
if ( xl == S - 1 ) {
// in right column
if ( yl == S - 1 ) {
// top right corner
( *outbuf )[2] = l + 1 - L;
} else {
( *outbuf )[2] = l + 1;
}
} else if ( yl == S - 1 ) {
// in top row
// (but NOT in top right corner!)
( *outbuf )[2] = l + S + 1 - L;
} else {
// in the center
( *outbuf )[2] = l + S + 1;
}
// add top left neighbor
if ( xl == 0 ) {
// in left column
if ( yl == S - 1 ) {
// top left corner
( *outbuf )[3] = l + 2 * S - 1 - L ;
} else {
( *outbuf )[3] = l + 2 * S - 1;
}
} else if ( yl == S - 1 ) {
// in top row
// (but NOT in top left corner!)
( *outbuf )[3] = l + S - 1 - L;
} else {
// in the center
( *outbuf )[3] = l + S - 1;
}
}
void Lattice2DSquare::get_3nn(
Lattice::spindex l, vector<Lattice::spindex>* outbuf ) const
{
outbuf->resize( 4 );
const unsigned int xl = x( l );
const unsigned int yl = y( l );
// add left neighbor
if ( xl <= 1 ) {
( *outbuf )[0] = l + S - 2;
} else {
( *outbuf )[0] = l - 2;
}
// add right neighbor
if ( xl >= S - 2 ) {
( *outbuf )[1] = l + 2 - S;
} else {
( *outbuf )[1] = l + 2;
}
// add bottom neighbor
if ( yl <= 1 ) {
( *outbuf )[2] = l + L - 2 * S;
} else {
( *outbuf )[2] = l - 2 * S;
}
// add top neighbor
if ( yl >= S - 2 ) {
( *outbuf )[3] = l + 2 * S - L;
} else {
( *outbuf )[3] = l + 2 * S;
}
}
Lattice::irridxrel Lattice2DSquare::reduce_idxrel(
Lattice::spindex i, Lattice::spindex j ) const
{
assert( i < 2 * L );
assert( j < 2 * L );
assert( get_spindex_type( i ) == get_spindex_type( j ) );
// calculate the absolute values of the position differences
unsigned int dx = std::abs( d( x( i ), x( j ) ) );
unsigned int dy = std::abs( d( y( i ), y( j ) ) );
// dx should be larger than dy
if ( dy > dx ) {
swap( dx, dy );
}
return dx + S * dy;
}
set<Lattice::irridxrel> Lattice2DSquare::get_all_irridxrels() const
{
set<Lattice::irridxrel> allrels;
for ( Lattice::index i = 0; i < L; ++i ) {
allrels.insert( reduce_idxrel( 0, i ) );
}
#if VERBOSE >= 1
cout << "Lattice2DSquare::irreducible_idxrel_list() : "
<< "list of irreducible index relations =" << endl;
for ( auto it = allrels.begin(); it != allrels.end(); ++it ) {
cout << *it << endl;
}
#endif
return allrels;
}
Lattice::irridxrel Lattice2DSquare::get_maxdist_irridxrel() const
{
if ( S % 2 == 0 ) {
return L / 2 + S / 2;
} else {
return L / 2;
}
}
Eigen::VectorXd Lattice2DSquare::r( Lattice::index i, Lattice::index j ) const
{
assert( i < L );
assert( j < L );
Eigen::VectorXd result = Eigen::VectorXd::Zero( 2 );
result( 0 ) = x( j ) - x( i );
result( 1 ) = y( j ) - y( i );
return result;
}
vector<Eigen::VectorXd> Lattice2DSquare::get_qvectors() const
{
vector<Eigen::VectorXd> allq;
allq.reserve( S * S / 4 );
for ( unsigned int i = 0; i <= S / 2; ++i ) {
for ( unsigned int l = 0; l <= S / 2; ++l ) {
if ( i == 0 && l == 0 ) {
continue;
}
Eigen::VectorXd q( 2 );
q[0] = i * 2.0 * M_PI / static_cast<double>( S );
q[1] = l * 2.0 * M_PI / static_cast<double>( S );
allq.push_back( q );
}
}
return allq;
}
double Lattice2DSquare::pairsym_modifier(
optpairsym_t sym, Lattice::spindex i, Lattice::spindex j ) const
{
assert( get_spindex_type( i ) == get_spindex_type( j ) );
if ( sym == OPTION_PAIRING_SYMMETRY_SWAVE ) {
return 1.0;
} else { // dwave or twisted dwave (doesn't make a difference for 1 plane)
int dx = d( x( i ), x( j ) );
int dy = d( y( i ), y( j ) );
if ( ( dx == 1 || dx == -1 ) && dy == 0 ) {
// nearest neighbors along x-axis
return 1.0;
} else if ( dx == 0 && ( dy == 1 || dy == -1 ) ) {
// nearest neighbors along y-axis
return -1.0;
} else if ( ( dx == 1 || dx == -1 ) && ( dy == 1 || dy == -1 ) ) {
// second nearest neighbors along diagonal
return 0.0;
} else if ( ( dx == 2 || dx == -2 ) && dy == 0 ) {
// third nearest neighbors along x-axis
return 1.0;
} else if ( dx == 0 && ( dy == 2 || dy == -2 ) ) {
// third nearest neighbors along y-axis
return -1.0;
}
}
// still here? no meaningful decision yet??? --> this is a bug ...
assert( false );
return 0.0; // <-- should never be reached; only to suppress compiler warning
}
unsigned int Lattice2DSquare::get_index_sublattice( Lattice::index i ) const
{
assert( i < L );
return ( x( i ) + y( i ) ) % 2;
}