File: SyBookkeeper.cpp

package info (click to toggle)
chemps2 1.8.12-5
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 5,432 kB
  • sloc: cpp: 38,521; python: 1,116; f90: 215; makefile: 45; sh: 23
file content (325 lines) | stat: -rw-r--r-- 13,289 bytes parent folder | download | duplicates (4)
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
/*
   CheMPS2: a spin-adapted implementation of DMRG for ab initio quantum chemistry
   Copyright (C) 2013-2018 Sebastian Wouters

   This program 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 2 of the License, or
   (at your option) any later version.

   This program 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 this program; if not, write to the Free Software Foundation, Inc.,
   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include <algorithm>
#include <stdlib.h>
#include <assert.h>
#include <math.h>

#include "SyBookkeeper.h"
#include "Irreps.h"
#include "Options.h"

CheMPS2::SyBookkeeper::SyBookkeeper( const Problem * Prob, const int D ){

   this->Prob = Prob;
   Irreps temp( Prob->gSy() );
   this->num_irreps = temp.getNumberOfIrreps();

   // Allocate the arrays
   allocate_arrays();

   // Fill FCIdim
   fillFCIdim();

   // Copy FCIdim to CURdim
   CopyDim( FCIdim, CURdim );

   // Scale the CURdim
   ScaleCURdim( D, 1, gL() - 1 );

   assert( IsPossible() );

}

CheMPS2::SyBookkeeper::SyBookkeeper( const SyBookkeeper & tocopy ){

   this->Prob = tocopy.gProb();
   Irreps temp( Prob->gSy() );
   this->num_irreps = temp.getNumberOfIrreps();

   // Allocate the arrays
   allocate_arrays();

   // Fill FCIdim
   fillFCIdim();

   // Copy the CURdim
   for ( int boundary = 0; boundary <= gL(); boundary++ ){
      for ( int N = gNmin( boundary ); N <= gNmax( boundary ); N++ ){
         for ( int TwoS = gTwoSmin( boundary, N ); TwoS <= gTwoSmax( boundary, N ); TwoS += 2 ){
            for ( int irrep = 0; irrep < num_irreps; irrep++ ){
               CURdim[ boundary ][ N - gNmin( boundary ) ][ ( TwoS - gTwoSmin( boundary, N ) ) / 2 ][ irrep ] = tocopy.gCurrentDim( boundary, N, TwoS, irrep );
            }
         }
      }
   }

}

void CheMPS2::SyBookkeeper::allocate_arrays(){

   // Set the min and max particle number and spin
   Nmin = new int[ gL() + 1 ];
   Nmax = new int[ gL() + 1 ];
   TwoSmin = new int*[ gL() + 1 ];
   TwoSmax = new int*[ gL() + 1 ];
   for ( int boundary = 0; boundary <= gL(); boundary++ ){
      Nmin[ boundary ] = std::max( std::max( 0, gN() + 2 * ( boundary - gL() ) ), boundary - gL() + ( gN() + gTwoS() ) / 2 );
      Nmax[ boundary ] = std::min( std::min( 2 * boundary, gN() ), boundary + ( gN() - gTwoS() ) / 2 );
      TwoSmin[ boundary ] = new int[ Nmax[ boundary ] - Nmin[ boundary ] + 1 ];
      TwoSmax[ boundary ] = new int[ Nmax[ boundary ] - Nmin[ boundary ] + 1 ];
      for ( int N = Nmin[ boundary ]; N <= Nmax[ boundary ]; N++ ){
         const int temporary = gL() - boundary - abs( gN() - N - gL() + boundary );
         TwoSmin[ boundary ][ N - Nmin[ boundary ] ] = std::max( N % 2, gTwoS() - temporary );
         TwoSmax[ boundary ][ N - Nmin[ boundary ] ] = std::min( boundary - abs( boundary - N ), gTwoS() + temporary );
      }
   }

   // FCIdim & CURdim memory allocation
   FCIdim = new int***[ gL() + 1 ];
   CURdim = new int***[ gL() + 1 ];
   for ( int boundary = 0; boundary <= gL(); boundary++ ){
      FCIdim[ boundary ] = new int**[ gNmax( boundary ) - gNmin( boundary ) + 1 ];
      CURdim[ boundary ] = new int**[ gNmax( boundary ) - gNmin( boundary ) + 1 ];
      for ( int N = gNmin( boundary ); N <= gNmax( boundary ); N++ ){
         FCIdim[ boundary ][ N - gNmin( boundary ) ] = new int*[ ( gTwoSmax( boundary, N ) - gTwoSmin( boundary, N ) ) / 2 + 1 ];
         CURdim[ boundary ][ N - gNmin( boundary ) ] = new int*[ ( gTwoSmax( boundary, N ) - gTwoSmin( boundary, N ) ) / 2 + 1 ];
         for ( int TwoS = gTwoSmin( boundary, N ); TwoS <= gTwoSmax( boundary, N ); TwoS += 2 ){
            FCIdim[ boundary ][ N - gNmin( boundary ) ][ ( TwoS - gTwoSmin( boundary, N ) ) / 2 ] = new int[ num_irreps ];
            CURdim[ boundary ][ N - gNmin( boundary ) ][ ( TwoS - gTwoSmin( boundary, N ) ) / 2 ] = new int[ num_irreps ];
         }
      }
   }

}

CheMPS2::SyBookkeeper::~SyBookkeeper(){

   for ( int boundary = 0; boundary <= gL(); boundary++ ){
      for ( int N = gNmin( boundary ); N <= gNmax( boundary ); N++ ){
         for ( int TwoS = gTwoSmin( boundary, N ); TwoS <= gTwoSmax( boundary, N ); TwoS += 2 ){
            delete [] FCIdim[ boundary ][ N - gNmin( boundary ) ][ ( TwoS - gTwoSmin( boundary, N ) ) / 2 ];
            delete [] CURdim[ boundary ][ N - gNmin( boundary ) ][ ( TwoS - gTwoSmin( boundary, N ) ) / 2 ];
         }
         delete [] FCIdim[ boundary ][ N - gNmin( boundary ) ];
         delete [] CURdim[ boundary ][ N - gNmin( boundary ) ];
      }
      delete [] FCIdim[ boundary ];
      delete [] CURdim[ boundary ];
   }
   delete [] FCIdim;
   delete [] CURdim;

   for ( int boundary = 0; boundary <= gL(); boundary++ ){
      delete [] TwoSmin[ boundary ];
      delete [] TwoSmax[ boundary ];
   }
   delete [] TwoSmin;
   delete [] TwoSmax;
   delete [] Nmin;
   delete [] Nmax;

}

const CheMPS2::Problem * CheMPS2::SyBookkeeper::gProb() const{ return Prob; }

int CheMPS2::SyBookkeeper::gL() const{ return Prob->gL(); }

int CheMPS2::SyBookkeeper::gIrrep( const int orbital ) const{ return Prob->gIrrep( orbital ); }

int CheMPS2::SyBookkeeper::gTwoS() const{ return Prob->gTwoS(); }

int CheMPS2::SyBookkeeper::gN() const{ return Prob->gN(); }

int CheMPS2::SyBookkeeper::gIrrep() const{ return Prob->gIrrep(); }

int CheMPS2::SyBookkeeper::getNumberOfIrreps() const{ return num_irreps; }

int CheMPS2::SyBookkeeper::gNmin( const int boundary ) const{ return Nmin[ boundary ]; }

int CheMPS2::SyBookkeeper::gNmax( const int boundary ) const{ return Nmax[ boundary ]; }

int CheMPS2::SyBookkeeper::gTwoSmin( const int boundary, const int N) const{ return TwoSmin[ boundary ][ N - Nmin[ boundary ] ]; }

int CheMPS2::SyBookkeeper::gTwoSmax( const int boundary, const int N) const{ return TwoSmax[ boundary ][ N - Nmin[ boundary ] ]; }

int CheMPS2::SyBookkeeper::gFCIdim( const int boundary, const int N, const int TwoS, const int irrep ) const{ return gDimPrivate( FCIdim, boundary, N, TwoS, irrep ); }

int CheMPS2::SyBookkeeper::gCurrentDim( const int boundary, const int N, const int TwoS, const int irrep ) const{ return gDimPrivate( CURdim, boundary, N, TwoS, irrep ); }

void CheMPS2::SyBookkeeper::SetDim( const int boundary, const int N, const int TwoS, const int irrep, const int value ){

   if ( gFCIdim( boundary, N, TwoS, irrep ) != 0 ){
      CURdim[ boundary ][ N - gNmin( boundary ) ][ ( TwoS - gTwoSmin( boundary, N ) ) / 2 ][ irrep ] = value;
   }

}

void CheMPS2::SyBookkeeper::fillFCIdim(){

   // On the left-hand side only the trivial symmetry sector is allowed
   for ( int irrep = 0; irrep < num_irreps; irrep++ ){ FCIdim[ 0 ][ 0 ][ 0 ][ irrep ] = 0; }
   FCIdim[ 0 ][ 0 ][ 0 ][ 0 ] = 1;

   // Fill boundaries 1 to L from left to right
   fill_fci_dim_right( FCIdim, 1, gL() );

   // Remember the FCI virtual dimension at the RHS
   const int rhs = FCIdim[ gL() ][ 0 ][ 0 ][ gIrrep() ];

   // On the right-hand side only the targeted symmetry sector is allowed
   for ( int irrep = 0; irrep < num_irreps; irrep++ ){ FCIdim[ gL() ][ 0 ][ 0 ][ irrep ] = 0; }
   FCIdim[ gL() ][ 0 ][ 0 ][ gIrrep() ] = std::min( 1, rhs );

   // Fill boundarties 0 to L - 1 from right to left
   fill_fci_dim_left( FCIdim, 0, gL() - 1 );

}

void CheMPS2::SyBookkeeper::fill_fci_dim_right( int **** storage, const int start, const int stop ){

   for ( int boundary = start; boundary <= stop; boundary++ ){
      for ( int N = gNmin( boundary ); N <= gNmax( boundary ); N++ ){
         for ( int TwoS = gTwoSmin( boundary, N ); TwoS <= gTwoSmax( boundary, N ); TwoS += 2 ){
            for ( int irrep = 0; irrep < num_irreps; irrep++ ){
               const int value = std::min( CheMPS2::SYBK_dimensionCutoff,
                                           gDimPrivate( storage, boundary - 1, N,     TwoS    , irrep )
                                         + gDimPrivate( storage, boundary - 1, N - 2, TwoS    , irrep )
                                         + gDimPrivate( storage, boundary - 1, N - 1, TwoS + 1, Irreps::directProd( irrep, gIrrep( boundary - 1 ) ) )
                                         + gDimPrivate( storage, boundary - 1, N - 1, TwoS - 1, Irreps::directProd( irrep, gIrrep( boundary - 1 ) ) ) );
               storage[ boundary ][ N - gNmin( boundary ) ][ ( TwoS - gTwoSmin( boundary, N ) ) / 2 ][ irrep ] = value;
            }
         }
      }
   }

}

void CheMPS2::SyBookkeeper::fill_fci_dim_left( int **** storage, const int start, const int stop ){

   for ( int boundary = stop; boundary >= start; boundary-- ){
      for ( int N = gNmin( boundary ); N <= gNmax( boundary ); N++ ){
         for ( int TwoS = gTwoSmin( boundary, N ); TwoS <= gTwoSmax( boundary, N ); TwoS += 2 ){
            for ( int irrep = 0; irrep < num_irreps; irrep++ ){
               const int value = std::min( gDimPrivate( storage, boundary, N, TwoS, irrep ),
                                 std::min( CheMPS2::SYBK_dimensionCutoff,
                                           gDimPrivate( storage, boundary + 1, N,     TwoS    , irrep )
                                         + gDimPrivate( storage, boundary + 1, N + 2, TwoS    , irrep )
                                         + gDimPrivate( storage, boundary + 1, N + 1, TwoS + 1, Irreps::directProd( irrep, gIrrep( boundary ) ) )
                                         + gDimPrivate( storage, boundary + 1, N + 1, TwoS - 1, Irreps::directProd( irrep, gIrrep( boundary ) ) ) ) );
               storage[ boundary ][ N - gNmin( boundary ) ][ ( TwoS - gTwoSmin( boundary, N ) ) / 2 ][ irrep ] = value;
            }
         }
      }
   }

}

void CheMPS2::SyBookkeeper::CopyDim( int **** origin, int **** target ){

   for ( int boundary = 0; boundary <= gL(); boundary++ ){
      for ( int N = gNmin( boundary ); N <= gNmax( boundary ); N++ ){
         for ( int TwoS = gTwoSmin( boundary, N ); TwoS <= gTwoSmax( boundary, N ); TwoS += 2 ){
            for ( int irrep = 0; irrep < num_irreps; irrep++ ){
                 target[ boundary ][ N - gNmin( boundary ) ][ ( TwoS - gTwoSmin( boundary, N ) ) / 2 ][ irrep ]
               = origin[ boundary ][ N - gNmin( boundary ) ][ ( TwoS - gTwoSmin( boundary, N ) ) / 2 ][ irrep ];
            }
         }
      }
   }

}

void CheMPS2::SyBookkeeper::ScaleCURdim( const int virtual_dim, const int start, const int stop ){

   for ( int boundary = start; boundary <= stop; boundary++ ){

      const int totaldim = gTotDimAtBound( boundary );

      if ( totaldim > virtual_dim ){
         double factor = ( 1.0 * virtual_dim ) / totaldim;
         for ( int N = gNmin( boundary ); N <= gNmax( boundary ); N++ ){
            for ( int TwoS = gTwoSmin( boundary, N ); TwoS <= gTwoSmax( boundary, N ); TwoS += 2 ){
               for ( int irrep = 0; irrep < num_irreps; irrep++ ){
                  const int value = ( int )( ceil( factor * gCurrentDim( boundary, N, TwoS, irrep ) ) + 0.1 );
                  SetDim( boundary, N, TwoS, irrep, value );
               }
            }
         }
      }
   }

}

int CheMPS2::SyBookkeeper::gDimPrivate( int **** storage, const int boundary, const int N, const int TwoS, const int irrep ) const{

   if (( boundary < 0 ) || ( boundary > gL() )){ return 0; }
   if (( N > gNmax( boundary ) ) || ( N < gNmin( boundary ) )){ return 0; }
   if (( TwoS % 2 ) != ( gTwoSmin( boundary, N ) % 2 )){ return 0; }
   if (( TwoS < gTwoSmin( boundary, N ) ) || ( TwoS > gTwoSmax( boundary, N ) )){ return 0; }
   if (( irrep < 0 ) || ( irrep >= num_irreps )){ return 0; }
   return storage[ boundary ][ N - gNmin( boundary ) ][ ( TwoS - gTwoSmin( boundary, N ) ) / 2 ][ irrep ];

}

int CheMPS2::SyBookkeeper::gMaxDimAtBound( const int boundary ) const{

   int max_dim = 0;
   for ( int N = gNmin( boundary ); N <= gNmax( boundary ); N++ ){
      for ( int TwoS = gTwoSmin( boundary, N ); TwoS <= gTwoSmax( boundary, N ); TwoS += 2 ){
         for ( int irrep = 0; irrep < num_irreps; irrep++ ){
            const int dim = gCurrentDim( boundary, N, TwoS, irrep );
            if ( dim > max_dim ){ max_dim = dim; }
         }
      }
   }
   return max_dim;

}

int CheMPS2::SyBookkeeper::gTotDimAtBound( const int boundary ) const{

   int tot_dim = 0;
   for ( int N = gNmin( boundary ); N <= gNmax( boundary ); N++ ){
      for ( int TwoS = gTwoSmin( boundary, N ); TwoS <= gTwoSmax( boundary, N ); TwoS += 2 ){
         for ( int irrep = 0; irrep < num_irreps; irrep++ ){
            tot_dim += gCurrentDim( boundary, N, TwoS, irrep );
         }
      }
   }
   return tot_dim;

}

void CheMPS2::SyBookkeeper::restart( const int start, const int stop, const int virtual_dim ){

   fill_fci_dim_right( CURdim, start, stop );
   fill_fci_dim_left(  CURdim, start, stop );
     ScaleCURdim( virtual_dim, start, stop );

}

bool CheMPS2::SyBookkeeper::IsPossible() const{

   return ( gCurrentDim( gL(), gN(), gTwoS(), gIrrep() ) == 1 );

}