DMRGBlock.hpp
1 #ifndef __DMRG_BLOCK_HPP__
2 #define __DMRG_BLOCK_HPP__
3 
11 #include <petscmat.h>
12 #include "QuantumNumbers.hpp"
13 #include "MiscTools.hpp"
14 #include "kron.hpp"
15 #include "linalg_tools.hpp"
16 #include <string>
17 #include <map>
18 
21 typedef enum
22 {
23  OpSm = -1,
24  OpSz = 0,
25  OpSp = +1,
26  OpEye= +2
27 } Op_t;
28 
30 static const std::map<Op_t, std::string> OpString =
31 {
32  {OpSm, "Sm"},
33  {OpSz, "Sz"},
34  {OpSp, "Sp"}
35 };
36 
38 #define OpToCStr(OP) ((OpString.find(OP)->second).c_str())
39 
41 #define OpToStr(OP) (OpString.find(OP)->second)
42 
44 typedef enum
45 {
46  SideLeft = 0,
47  SideRight = 1
48 } Side_t;
49 
51 typedef enum
52 {
53  SpinOneHalf = 102,
54  SpinOne = 101,
55  SpinNull = -2
56 } Spin_t;
57 
59 static const std::map<std::string,Spin_t> SpinTypes = {
60  {"1/2", SpinOneHalf },
61  {"1", SpinOne }
62 };
63 
65 static const std::map<Spin_t,std::string> SpinTypesString = {
66  {SpinOneHalf, "1/2" },
67  {SpinOne, "1" }
68 };
69 
70 static const std::vector<Op_t> BasicOpTypes = { OpSz, OpSp };
71 static const std::vector<Side_t> SideTypes = { SideLeft, SideRight };
72 
76 namespace Block {
77 
79  class SpinBase
80  {
81 
82  private:
83 
85  MPI_Comm mpi_comm = PETSC_COMM_SELF;
86 
88  PetscMPIInt mpi_rank;
89 
91  PetscMPIInt mpi_size;
92 
95 
97  std::string spin_type_str = "1/2";
98 
100  PetscInt _loc_dim = 2;
101 
103  std::vector<PetscScalar> _loc_qn_list = {+0.5, -0.5};
104 
106  std::vector<PetscInt> _loc_qn_size = {1, 1};
107 
109  PetscBool init = PETSC_FALSE;
110 
112  PetscBool init_once = PETSC_FALSE;
113 
115  PetscBool mpi_init = PETSC_FALSE;
116 
118  PetscBool verbose = PETSC_FALSE;
119 
121  PetscBool log_io = PETSC_FALSE;
122 
124  PetscInt num_sites;
125 
127  PetscInt num_states;
128 
130  PetscBool init_Sm = PETSC_FALSE;
131 
133  std::vector<Mat> SzData;
134 
136  std::vector<Mat> SpData;
137 
139  std::vector<Mat> SmData;
140 
142  PetscBool init_save = PETSC_FALSE;
143 
145  PetscBool disk_set = PETSC_FALSE;
146 
148  PetscBool saved = PETSC_FALSE;
149 
151  PetscBool retrieved = PETSC_FALSE;
152 
154  std::string save_dir;
155 
157  std::string read_dir;
158 
160  std::string write_dir;
161 
167  PetscInt num_reads = 0;
168 
170  PetscErrorCode SaveOperator(
171  const std::string& OpName,
172  const size_t& isite,
173  Mat& Op,
174  const MPI_Comm& comm_in
175  );
176 
178  PetscErrorCode Retrieve_NoChecks();
179 
180  /* Number of subcommunicators to be used when performing the rotation. */
181  PetscInt nsubcomm = 1;
182 
183  /* Tells the subcommunicator to use for rotating this site */
184  std::vector< PetscMPIInt > site_color;
185 
187  enum RotMethod { mmmmult=0, matptap=1 };
188 
192 
193  protected:
194 
196  PetscBool MPIInitialized() const { return mpi_init; }
197 
198  public:
199 
201  virtual PetscInt loc_dim() const
202  {
203  return _loc_dim;
204  }
205 
207  virtual std::vector<PetscScalar> loc_qn_list() const
208  {
209  return _loc_qn_list;
210  }
211 
213  virtual std::vector<PetscInt> loc_qn_size() const
214  {
215  return _loc_qn_size;
216  }
217 
219  virtual PetscErrorCode MatSpinSzCreate(Mat& Sz);
220 
223  virtual PetscErrorCode MatSpinSpCreate(Mat& Sp);
224 
226  PetscErrorCode Initialize(
227  const MPI_Comm& comm_in
228  );
229 
238  PetscErrorCode Initialize(
239  const MPI_Comm& comm_in,
240  const PetscInt& num_sites_in,
241  const PetscInt& num_states_in,
242  const PetscBool& init_ops = PETSC_TRUE
244  );
245 
251  PetscErrorCode Initialize(
252  const MPI_Comm& comm_in,
253  const PetscInt& num_sites_in,
254  const std::vector<PetscReal>& qn_list_in,
255  const std::vector<PetscInt>& qn_size_in,
256  const PetscBool& init_ops = PETSC_TRUE
258  );
259 
261  PetscErrorCode Initialize(
262  const PetscInt& num_sites_in,
263  const QuantumNumbers& qn_in
264  );
265 
267  PetscErrorCode InitializeFromDisk(
268  const MPI_Comm& comm_in,
269  const std::string& block_path
270  );
271 
276  PetscErrorCode InitializeSave(
277  const std::string& save_dir_in
278  );
279 
281  PetscErrorCode SetDiskStorage(
282  const std::string& read_dir_in,
283  const std::string& write_dir_in
284  );
285 
287  std::string SaveDir() const { return save_dir; }
288 
290  PetscBool SaveInitialized() const { return init_save; }
291 
293  PetscErrorCode SaveAndDestroy();
294 
302  PetscErrorCode SaveBlockInfo();
303 
305  PetscErrorCode RetrieveOperator(
306  const std::string& OpName,
307  const size_t& isite,
308  Mat& Op,
309  const MPI_Comm& comm_in=MPI_COMM_NULL
310  );
311 
313  PetscErrorCode Retrieve();
314 
316  PetscErrorCode EnsureSaved();
317 
319  PetscErrorCode EnsureRetrieved();
320 
323  PetscErrorCode Destroy();
324 
327 
330  PetscErrorCode CheckOperatorArray(
331  const Op_t& OpType
332  ) const;
333 
335  PetscBool Initialized() const { return init; }
336 
338  PetscBool Saved() const { return saved; }
339 
341  MPI_Comm MPIComm() const { return mpi_comm; }
342 
344  PetscInt NumSites() const {return num_sites; }
345 
347  PetscInt NumStates() const {return num_states; }
348 
350  Mat H = nullptr;
351 
353  Mat Sz(const PetscInt& Isite) const {
354  if(Isite >= num_sites) throw std::runtime_error("Attempted to access non-existent site.");
355  return SzData[Isite];
356  }
357 
359  Mat Sp(const PetscInt& Isite) const {
360  if(Isite >= num_sites) throw std::runtime_error("Attempted to access non-existent site.");
361  return SpData[Isite];
362  }
363 
365  Mat Sm(const PetscInt& Isite) const {
366  if(Isite >= num_sites) throw std::runtime_error("Attempted to access non-existent site.");
367  if(!init_Sm) throw std::runtime_error("Sm matrices were not initialized on this block.");
368  return SmData[Isite];
369  }
370 
372  const std::vector<Mat>& Sz() const { return SzData; }
373 
375  const std::vector<Mat>& Sp() const { return SpData; }
376 
378  const std::vector<Mat>& Sm() const { return SmData; }
379 
381  PetscErrorCode CheckOperators() const;
382 
384  PetscErrorCode CheckSectors() const;
385 
387  PetscErrorCode MatOpGetNNZs(
388  const Op_t& OpType,
389  const PetscInt& isite,
390  std::vector<PetscInt>& nnzs
391  ) const;
392 
394  PetscErrorCode MatGetNNZs(
395  const Mat& matin,
396  std::vector<PetscInt>& nnzs
397  ) const;
398 
401  PetscErrorCode MatOpCheckOperatorBlocks(
402  const Op_t& op_t,
403  const PetscInt& isite
404  ) const;
405 
408  PetscErrorCode MatCheckOperatorBlocks(
409  const Op_t& op_t,
410  const Mat& matin
411  ) const;
412 
414  PetscErrorCode CheckOperatorBlocks() const;
415 
418  PetscErrorCode GetOperatorBlocks(Op_t Operator);
419 
421  PetscErrorCode CreateSm();
422 
424  PetscErrorCode DestroySm();
425 
427  PetscErrorCode RotateOperators(
428  SpinBase& Source,
429  const Mat& RotMatT
430  );
431 
433  PetscErrorCode AssembleOperators();
434  };
435 
436 }
437 
442 #endif // __DMRG_BLOCK_HPP__
PetscErrorCode EnsureRetrieved()
Ensures that the block matrices have been retrieved if the block is initialized, otherwise does nothi...
Definition: DMRGBlock.cpp:1098
std::vector< Mat > SzData
Array of matrices representing operators.
Definition: DMRGBlock.hpp:133
Contains information on quantum numbers and associated index ranges.
PetscErrorCode InitializeSave(const std::string &save_dir_in)
Initializes the writing of the block matrices to file.
Definition: DMRGBlock.cpp:835
Mat Sm(const PetscInt &Isite) const
Returns the matrix pointer to the operator at site Isite, if available.
Definition: DMRGBlock.hpp:365
PetscBool retrieved
Whether the block matrices have been retrieved.
Definition: DMRGBlock.hpp:151
PetscErrorCode GetOperatorBlocks(Op_t Operator)
Extracts the block structure for each operator.
RotMethod rot_method
Method to use for performing basis transformation.
Definition: DMRGBlock.hpp:191
Spin_t
Identifies the spin types implemented for this block.
Definition: DMRGBlock.hpp:51
PetscBool init_save
Whether saving the block matrices to file has been initialized correctly.
Definition: DMRGBlock.hpp:142
MPI_Comm mpi_comm
MPI Communicator.
Definition: DMRGBlock.hpp:85
virtual std::vector< PetscScalar > loc_qn_list() const
Sz sectors of a single site.
Definition: DMRGBlock.hpp:207
std::vector< Mat > SpData
Array of matrices representing operators.
Definition: DMRGBlock.hpp:136
std::string SaveDir() const
Returns the value of save_dir where the block data will be read from/written.
Definition: DMRGBlock.hpp:287
Base class for the implementation of a block of spin sites.
Definition: DMRGBlock.hpp:79
PetscBool SaveInitialized() const
Tells whether InitializeSave() has been properly called.
Definition: DMRGBlock.hpp:290
Spin one.
Definition: DMRGBlock.hpp:54
operator
Definition: DMRGBlock.hpp:25
const std::vector< Mat > & Sp() const
Returns the list of matrix pointer to the operators.
Definition: DMRGBlock.hpp:375
PetscInt num_reads
Number of reads from file made for this block.
Definition: DMRGBlock.hpp:167
Null input spin.
Definition: DMRGBlock.hpp:55
std::string read_dir
Root directory to read the matrix blocks from during the first access.
Definition: DMRGBlock.hpp:157
std::string write_dir
Root directory to write the matrix blocks into.
Definition: DMRGBlock.hpp:160
PetscErrorCode AssembleOperators()
Ensures that all operators are assembled.
Definition: DMRGBlock.cpp:825
PetscErrorCode EnsureSaved()
Ensures that the block matrices have been saved if the block is initialized, otherwise does nothing...
Definition: DMRGBlock.cpp:1090
left block
Definition: DMRGBlock.hpp:46
PetscBool log_io
Tells whether to printout IO functions.
Definition: DMRGBlock.hpp:121
PetscErrorCode SetDiskStorage(const std::string &read_dir_in, const std::string &write_dir_in)
Tells where to read from and save the operators and data about the block.
Definition: DMRGBlock.cpp:857
Op_t
Identifies the three possible spin operators and also represents the shift associated to its action o...
Definition: DMRGBlock.hpp:21
PetscBool MPIInitialized() const
Returns PETSC_TRUE if the MPI communicator was initialized properly.
Definition: DMRGBlock.hpp:196
PetscErrorCode CheckOperatorBlocks() const
Checks whether all matrix blocks follow the correct sector indices using MatCheckOperatorBlocks() ...
Definition: DMRGBlock.cpp:601
PetscBool Saved() const
Returns the saved state of all operators.
Definition: DMRGBlock.hpp:338
std::vector< Mat > SmData
Array of matrices representing operators.
Definition: DMRGBlock.hpp:139
PetscBool verbose
Tells whether to printout info during certain function calls.
Definition: DMRGBlock.hpp:118
Mat H
Matrix representation of the Hamiltonian operator.
Definition: DMRGBlock.hpp:350
virtual std::vector< PetscInt > loc_qn_size() const
Number of states in each sector in a single site.
Definition: DMRGBlock.hpp:213
PetscErrorCode RotateOperators(SpinBase &Source, const Mat &RotMatT)
Rotates all operators from a source block using the given transposed rotation matrix.
Definition: DMRGBlock.cpp:677
PetscBool init_Sm
Tells whether the Sm matrices have been initialized.
Definition: DMRGBlock.hpp:130
PetscErrorCode InitializeFromDisk(const MPI_Comm &comm_in, const std::string &block_path)
Initializes block object from data located in the directory block_path.
Definition: DMRGBlock.cpp:214
Contains the definitions for blocks of spin sites.
Definition: DMRGBlock.hpp:76
PetscErrorCode MatOpGetNNZs(const Op_t &OpType, const PetscInt &isite, std::vector< PetscInt > &nnzs) const
Returns the number of non-zeros in each row for an operator acting on a given site.
Definition: DMRGBlock.cpp:450
PetscErrorCode DestroySm()
Destroys the Sm matrices on the fly.
Definition: DMRGBlock.cpp:639
Spin_t spin_type
Type of spin contained in the block.
Definition: DMRGBlock.hpp:94
virtual PetscErrorCode MatSpinSzCreate(Mat &Sz)
Creates the single-site operator.
Definition: DMRGBlock.cpp:1106
PetscErrorCode CheckOperators() const
Checks whether all operators have been initialized and have correct dimensions.
Definition: DMRGBlock.cpp:413
PetscErrorCode CheckSectors() const
Checks whether sector indexing was done properly.
Definition: DMRGBlock.cpp:429
PetscInt num_sites
Number of sites in the block.
Definition: DMRGBlock.hpp:124
operator
Definition: DMRGBlock.hpp:23
PetscErrorCode SaveOperator(const std::string &OpName, const size_t &isite, Mat &Op, const MPI_Comm &comm_in)
Saves a single operator.
Definition: DMRGBlock.cpp:896
std::string spin_type_str
Type of spin contained in the block expressed as a string.
Definition: DMRGBlock.hpp:97
const std::vector< Mat > & Sz() const
Returns the list of matrix pointer to the operators.
Definition: DMRGBlock.hpp:372
PetscMPIInt mpi_size
MPI size of mpi_comm.
Definition: DMRGBlock.hpp:91
PetscBool init_once
Tells whether the block was initialized at least once before.
Definition: DMRGBlock.hpp:112
std::vector< PetscInt > _loc_qn_size
Stores the number of states in each sector in a single site.
Definition: DMRGBlock.hpp:106
PetscErrorCode Retrieve()
Retrieve all the matrix operators that were written to file by SaveAndDestroy()
Definition: DMRGBlock.cpp:1048
virtual PetscInt loc_dim() const
Local dimension of a single site.
Definition: DMRGBlock.hpp:201
PetscErrorCode RetrieveOperator(const std::string &OpName, const size_t &isite, Mat &Op, const MPI_Comm &comm_in=MPI_COMM_NULL)
Retrieves a single operator.
Definition: DMRGBlock.cpp:974
virtual PetscErrorCode MatSpinSpCreate(Mat &Sp)
Creates the single-site raising operator , from which we can define .
Definition: DMRGBlock.cpp:1169
RotMethod
List of possible methods for performing basis transformation.
Definition: DMRGBlock.hpp:187
PetscErrorCode CheckOperatorArray(const Op_t &OpType) const
Determines whether the operator arrays have been successfully filled with matrices.
Definition: DMRGBlock.cpp:375
Mat Sz(const PetscInt &Isite) const
Returns the matrix pointer to the operator at site Isite.
Definition: DMRGBlock.hpp:353
PetscBool mpi_init
Tells whether the block&#39;s MPI attributes were initialized.
Definition: DMRGBlock.hpp:115
PetscMPIInt mpi_rank
MPI rank in mpi_comm.
Definition: DMRGBlock.hpp:88
PetscBool Initialized() const
Indicates whether block has been initialized before using it.
Definition: DMRGBlock.hpp:335
PetscErrorCode SaveAndDestroy()
Save all the matrix operators to file and destroy the current storage.
Definition: DMRGBlock.cpp:1014
PetscErrorCode SaveBlockInfo()
Save some information about the block that could be used to reconstruct it later. ...
Definition: DMRGBlock.cpp:924
PetscErrorCode CreateSm()
Creates the Sm matrices on the fly.
Definition: DMRGBlock.cpp:623
Identity operator.
Definition: DMRGBlock.hpp:26
PetscErrorCode Destroy()
Destroys all operator matrices and frees memory.
Definition: DMRGBlock.cpp:655
operator
Definition: DMRGBlock.hpp:24
right block
Definition: DMRGBlock.hpp:47
PetscErrorCode MatOpCheckOperatorBlocks(const Op_t &op_t, const PetscInt &isite) const
Checks the block indexing in the matrix operator op_t on site isite.
Definition: DMRGBlock.cpp:498
PetscErrorCode Initialize(const MPI_Comm &comm_in)
Initializes block object&#39;s MPI attributes.
Definition: DMRGBlock.cpp:31
PetscBool disk_set
Whether SetDiskStorage() has properly set the block storage locations.
Definition: DMRGBlock.hpp:145
std::string save_dir
Root directory to read from and write the matrix blocks into.
Definition: DMRGBlock.hpp:154
PetscBool saved
Whether the block matrices have been saved.
Definition: DMRGBlock.hpp:148
QuantumNumbers Magnetization
Stores the information on the magnetization Sectors.
Definition: DMRGBlock.hpp:326
PetscInt _loc_dim
Stores the local dimension of a single site.
Definition: DMRGBlock.hpp:100
PetscInt num_states
Number of basis states in the Hilbert space.
Definition: DMRGBlock.hpp:127
Spin one-half.
Definition: DMRGBlock.hpp:53
PetscErrorCode MatGetNNZs(const Mat &matin, std::vector< PetscInt > &nnzs) const
Returns the number of non-zeros in each row for a given matrix.
Definition: DMRGBlock.cpp:477
PetscInt NumSites() const
Gets the number of sites that are currently initialized.
Definition: DMRGBlock.hpp:344
PetscErrorCode MatCheckOperatorBlocks(const Op_t &op_t, const Mat &matin) const
Checks the block indexing in the matrix operator op_t on specified matrix matin.
Definition: DMRGBlock.cpp:520
Mat Sp(const PetscInt &Isite) const
Returns the matrix pointer to the operator at site Isite.
Definition: DMRGBlock.hpp:359
MPI_Comm MPIComm() const
Gets the communicator associated to the block.
Definition: DMRGBlock.hpp:341
Defines a single operator to be used for performing measurements.
PetscBool init
Tells whether the block was initialized.
Definition: DMRGBlock.hpp:109
std::vector< PetscScalar > _loc_qn_list
Stores the Sz sectors of a single site.
Definition: DMRGBlock.hpp:103
const std::vector< Mat > & Sm() const
Returns the list of matrix pointer to the operators.
Definition: DMRGBlock.hpp:378
PetscInt NumStates() const
Gets the number of states that are currently used.
Definition: DMRGBlock.hpp:347
PetscErrorCode Retrieve_NoChecks()
Private function to retrieve operators without checking for save initialization.
Definition: DMRGBlock.cpp:1063
Side_t
Identifies the sides of the DMRG block.
Definition: DMRGBlock.hpp:44
This site was generated by Sphinx using Doxygen with a customized theme from doxygen-bootstrapped.