Hamiltonians.hpp
1 #ifndef __HAMILTONIANS__
2 #define __HAMILTONIANS__
3 
4 #include "petscsys.h"
5 #include "DMRGBlock.hpp"
6 #include <vector>
7 #include <iostream>
8 
16 namespace Hamiltonians
17 {
19  struct Term
20  {
21  PetscScalar a;
23  PetscInt Isite;
25  PetscInt Jsite;
26  };
27 
29  typedef enum
30  {
31  OpenBC=0,
32  PeriodicBC=1
33  } BC_t;
34 
78  {
79 
80  public:
81 
84  {
85 
86  }
87 
89  PetscErrorCode SetFromOptions()
90  {
91  PetscErrorCode ierr;
92  /* Get values from command line options */
93  ierr = PetscOptionsGetReal(NULL,NULL,"-J1",&_J1,NULL); CHKERRQ(ierr);
94  ierr = PetscOptionsGetReal(NULL,NULL,"-J2",&_J2,NULL); CHKERRQ(ierr);
95  ierr = PetscOptionsGetReal(NULL,NULL,"-Jz1",&_Jz1,NULL); CHKERRQ(ierr);
96  ierr = PetscOptionsGetReal(NULL,NULL,"-Jz2",&_Jz2,NULL); CHKERRQ(ierr);
97  ierr = PetscOptionsGetInt(NULL,NULL,"-Lx",&_Lx,NULL); CHKERRQ(ierr);
98  ierr = PetscOptionsGetInt(NULL,NULL,"-Ly",&_Ly,NULL); CHKERRQ(ierr);
99  ierr = PetscOptionsGetBool(NULL,NULL,"-verbose",&verbose,NULL); CHKERRQ(ierr);
100 
101  /* One may also specify whether to do the NN Heisenberg model and specify only the anisotropy */
102  ierr = PetscOptionsGetReal(NULL,NULL,"-heisenberg",&_Jz1,&heisenberg); CHKERRQ(ierr);
103  if(heisenberg)
104  {
105  _J1 = 0.50;
106  _J2 = 0.0;
107  _Jz2= 0.0;
108  }
109 
110  PetscBool BCopen = PETSC_FALSE;
111  ierr = PetscOptionsGetBool(NULL,NULL,"-BCopen",&BCopen,NULL); CHKERRQ(ierr);
112  if(BCopen) { _BCx=OpenBC; _BCy=OpenBC; }
113  PetscBool BCperiodic = PETSC_FALSE;
114  ierr = PetscOptionsGetBool(NULL,NULL,"-BCperiodic",&BCperiodic,NULL); CHKERRQ(ierr);
115  if(BCperiodic) { _BCx=PeriodicBC; _BCy=PeriodicBC; }
116  set_from_options = PETSC_TRUE;
117  return(0);
118  }
119 
122  PetscErrorCode SaveAsOptions(const std::string& filename)
123  {
124  PetscErrorCode ierr;
125  char val[4096];
126  PetscBool set;
127  std::vector< std::string > keys = {
128  "-J1",
129  "-J2",
130  "-Jz1",
131  "-Jz2",
132  "-Lx",
133  "-Ly",
134  "-heisenberg",
135  "-BCopen",
136  "-BCperiodic"
137  };
138 
139  std::ostringstream oss;
140  for(std::string& key: keys) {
141  ierr = PetscOptionsGetString(NULL,NULL,key.c_str(),val,4096,&set); CHKERRQ(ierr);
142  if(set) {
143  std::string val_str(val);
144  if(val_str.empty()) val_str = "yes";
145  oss << key << " " << val_str << std::endl;
146  }
147  }
148 
149  FILE *fp;
150  ierr = PetscFOpen(PETSC_COMM_SELF,filename.c_str(),"w", &fp); CHKERRQ(ierr);
151  ierr = PetscFPrintf(PETSC_COMM_SELF,fp,"%s",oss.str().c_str()); CHKERRQ(ierr);
152  ierr = PetscFClose(PETSC_COMM_SELF,fp); CHKERRQ(ierr);
153 
154  return(0);
155  }
156 
158  PetscInt NumSites() const { return _Lx*_Ly; }
159 
162  PetscInt NumEnvSites() const { return _Ly; }
163 
165  std::vector< Term > H(
166  const PetscInt& nsites
167  );
168 
170  void PrintOut() const {
171  if(heisenberg)
172  {
173  printf( "HAMILTONIAN: HeisenbergModel_SquareLattice\n");
174  }
175  else
176  {
177  printf( "HAMILTONIAN: J1J2XXZModel_SquareLattice\n");
178  }
179  printf( " Lx : %lld\n", LLD(_Lx));
180  printf( " Ly : %lld\n", LLD(_Ly));
181  printf( " J1 : %g\n", _J1);
182  printf( " Jz1 : %g\n", _Jz1);
183  printf( " J2 : %g\n", _J2);
184  printf( " Jz2 : %g\n", _Jz2);
185  printf( " BCx : %s\n", _BCx?"Periodic":"Open");
186  printf( " BCy : %s\n", _BCy?"Periodic":"Open");
187  }
188 
190  void SaveOut(FILE *fp) const {
191  fprintf(fp, " \"Hamiltonian\": {\n");
192  if(heisenberg)
193  {
194  fprintf(fp, " \"label\":\"HeisenbergModel_SquareLattice\",\n");
195  }
196  else
197  {
198  fprintf(fp, " \"label\":\"J1J2XXZModel_SquareLattice\",\n");
199  }
200  fprintf(fp, " \"parameters\": {\n");
201  fprintf(fp, " \"Lx\" : %lld,\n", LLD(_Lx));
202  fprintf(fp, " \"Ly\" : %lld,\n", LLD(_Ly));
203  fprintf(fp, " \"J1\" : %g,\n", _J1);
204  fprintf(fp, " \"Jz1\" : %g,\n", _Jz1);
205  fprintf(fp, " \"J2\" : %g,\n", _J2);
206  fprintf(fp, " \"Jz2\" : %g,\n", _Jz2);
207  fprintf(fp, " \"BCx\" : \"%s\",\n", _BCx?"Periodic":"Open");
208  fprintf(fp, " \"BCy\" : \"%s\"\n", _BCy?"Periodic":"Open");
209  fprintf(fp, " }\n");
210  fprintf(fp, " }");
211  fflush(fp);
212  }
213 
214  PetscInt Lx() const { return _Lx; }
215 
216  PetscInt Ly() const { return _Ly; }
217 
218  PetscInt To1D(
219  const PetscInt ix,
220  const PetscInt jy
221  ) const;
222 
223  PetscErrorCode To2D(
224  const PetscInt idx,
225  PetscInt& ix,
226  PetscInt& jy
227  ) const;
228 
230  std::vector< std::vector< PetscInt > > NeighborPairs(const PetscInt d=1) const;
231 
232  private:
233 
235  PetscBool heisenberg = PETSC_FALSE;
236 
238  PetscBool set_from_options = PETSC_FALSE;
239 
241  PetscScalar _Jz1 = 0.0;
242 
244  PetscScalar _Jz2 = 0.0;
245 
247  PetscScalar _J1 = 1.0;
248 
250  PetscScalar _J2 = 1.0;
251 
253  PetscInt _Lx = 4;
254 
256  PetscInt _Ly = 4;
257 
259  PetscBool verbose = PETSC_FALSE;
260 
262  BC_t _BCx = OpenBC;
263 
265  BC_t _BCy = PeriodicBC;
266 
268  PetscErrorCode PrintInfo() const
269  {
270  SETERRQ1(PETSC_COMM_SELF,1,"Function %s not implemented.", __FUNCTION__);
271  }
272 
274  std::vector<PetscInt> GetNearestNeighbors(
275  const PetscInt& ix, const PetscInt& jy, const PetscInt& nsites_in
276  ) const;
277 
279  std::vector<PetscInt> GetNextNearestNeighbors(
280  const PetscInt& ix, const PetscInt& jy, const PetscInt& nsites_in
281  ) const;
282 
284  std::vector< Term > H_full;
285 
287  PetscBool H_full_filled = PETSC_FALSE;
288  };
289 }
290 
293 #endif // __HAMILTONIANS__
PetscInt NumSites() const
Returns the number of sites in the square lattice.
Op_t Iop
The operator type for the left side.
PetscScalar a
Constant coefficient.
PetscInt Isite
The index of the site for the left side.
Op_t
Identifies the three possible spin operators and also represents the shift associated to its action o...
Definition: DMRGBlock.hpp:21
PetscErrorCode SaveAsOptions(const std::string &filename)
Dumps options keys and values to file.
Implements the Hamiltonian for the J1-J2 XY model on the square lattice.
A single interaction term on a one-dimensional lattice.
std::vector< Term > H_full
Contains the Hamiltonian involving the entire lattice.
PetscErrorCode PrintInfo() const
Prints some information related to the parameters of the Hamiltonian.
Implementation of Hamiltonian classes.
PetscInt Jsite
The index of the site for the right side.
Op_t Jop
The operator type for the right side.
PetscErrorCode SetFromOptions()
Set attributes from command line arguments.
PetscInt NumEnvSites() const
Returns the number of sites in a single cluster/column of the environment block as suggested by Liang...
void SaveOut(FILE *fp) const
Writes out some JSON information to stdout.
BC_t
Identifies the possible boundary conditions at the edges of the lattice.
void PrintOut() const
Prints out some information to stdout.
This site was generated by Sphinx using Doxygen with a customized theme from doxygen-bootstrapped.