QuantumNumbers.hpp
1 #ifndef __QUANTUM_NUMBERS_HPP__
2 #define __QUANTUM_NUMBERS_HPP__
3 
12 #include <petscmat.h>
13 #include <vector>
14 #include <cassert>
15 
16 /* For back-compatibility with versions that use single precision integers */
17 #if !defined(LLD)
18  #if defined(PETSC_USE_64BIT_INDICES)
19  #define LLD(INT) (INT)
20  #else
21  #define LLD(INT) ((long long)(INT))
22  #endif
23 #endif
24 
31 {
32 
33 private:
34 
36  MPI_Comm mpi_comm = PETSC_COMM_SELF;
37 
39  PetscInt num_sectors;
40 
42  std::vector<PetscReal> qn_list;
43 
45  std::vector<PetscInt> qn_offset;
46 
48  std::vector<PetscInt> qn_size;
49 
51  PetscInt num_states;
52 
54  PetscBool initialized = PETSC_FALSE;
55 
56 public:
57 
60  PetscErrorCode Initialize(
61  const MPI_Comm& mpi_comm_in,
62  const std::vector<PetscReal>& qn_list_in,
63  const std::vector<PetscInt>& qn_size_in
64  );
65 
67  PetscErrorCode CheckInitialized() const
68  {
69  if(PetscUnlikely(!initialized))
70  SETERRQ(mpi_comm, PETSC_ERR_ARG_CORRUPT, "QuantumNumbers object not yet initialized.");
71  else
72  return 0;
73  }
74 
76  MPI_Comm MPIComm() const { return mpi_comm; }
77 
79  PetscInt NumSectors() const
80  {
81  assert(initialized);
82  return num_sectors;
83  }
84 
86  std::vector<PetscReal> List() const
87  {
88  assert(initialized);
89  return qn_list;
90  }
91 
93  const std::vector<PetscReal>& ListRef() const
94  {
95  assert(initialized);
96  return qn_list;
97  }
98 
100  PetscReal List(const PetscInt& idx) const
101  {
102  assert(initialized);
103  if(0 <= idx && idx < num_sectors)
104  return qn_list[idx];
105  else
106  return -1;
107  }
108 
110  std::vector<PetscInt> Offsets() const
111  {
112  assert(initialized);
113  return qn_offset;
114  }
115 
117  PetscInt Offsets(const PetscInt& idx) const
118  {
119  assert(initialized);
120  if(0 <= idx && idx < num_sectors)
121  return qn_offset[idx];
122  else
123  return -1;
124  }
125 
127  std::vector<PetscInt> Sizes() const
128  {
129  assert(initialized);
130  return qn_size;
131  }
132 
134  PetscInt Sizes(const PetscInt& idx) const
135  {
136  assert(initialized);
137  if(0 <= idx && idx < num_sectors)
138  return qn_size[idx];
139  else
140  return -1;
141  }
142 
144  PetscInt NumStates() const
145  {
146  assert(initialized);
147  return num_states;
148  }
149 
151  PetscErrorCode BlockIdxToGlobalRange(
152  const PetscInt& BlockIdx,
153  PetscInt& GlobIdxStart,
154  PetscInt& GlobIdxEnd
155  ) const;
156 
159  PetscErrorCode OpBlockToGlobalRange(
160  const PetscInt& BlockIdx,
161  const PetscInt& BlockShift,
162  PetscInt& GlobIdxStart,
163  PetscInt& GlobIdxEnd,
164  PetscBool& flg
165  ) const;
166 
170  const PetscInt& BlockIdx,
171  const PetscInt& BlockShift,
172  PetscBool& flg
173  ) const
174  {
175  PetscErrorCode ierr;
176  PetscInt GlobIdxStart, GlobIdxEnd;
177  ierr = OpBlockToGlobalRange(BlockIdx, BlockShift, GlobIdxStart, GlobIdxEnd, flg);
178  assert(!ierr);
179  return GlobIdxStart;
180  }
181 
183  PetscErrorCode QNToGlobalRange(
184  const PetscReal& QNValue,
185  PetscInt& GlobIdxStart,
186  PetscInt& GlobIdxEnd
187  ) const;
188 
190  PetscErrorCode GlobalIdxToBlockIdx(
191  const PetscInt& GlobIdx,
192  PetscInt& BlockIdx
193  ) const;
194 
196  PetscErrorCode GlobalIdxToBlockIdx(
197  const PetscInt& GlobIdx,
198  PetscInt& BlockIdx,
199  PetscInt& LocIdx
200  ) const;
201 
203  PetscErrorCode GlobalIdxToQN(
204  const PetscInt& GlobIdx,
205  PetscReal& QNValue
206  ) const;
207 
209  PetscErrorCode BlockIdxToGlobalIdx(
210  const PetscInt& BlockIdx,
211  const PetscInt& LocIdx,
212  PetscInt& GlobIdx
213  ) const;
214 
216  PetscInt BlockIdxToGlobalIdx(
217  const PetscInt& BlockIdx,
218  const PetscInt& LocIdx
219  ) const;
220 
222  PetscErrorCode PrintQNs()
223  {
224  PetscErrorCode ierr;
225  PetscMPIInt mpi_rank;
226  ierr = MPI_Comm_rank(mpi_comm, &mpi_rank); CHKERRQ(ierr);
227  if(!mpi_rank)
228  {
229  printf("[ ");
230  for(PetscInt i = 0; i < num_sectors; ++i)
231  {
232  for(PetscInt j = 0; j < qn_size[i]; ++j) printf("%g ", qn_list[i]);
233  }
234  printf(" ]\n");
235  }
236  return(0);
237  }
238 
239 };
240 
241 
244 {
245 private:
246 
249 
251  PetscInt istart_ = 0;
252 
254  PetscInt iend_ = 0;
255 
256  /* Stores the value of the current index */
257  PetscInt idx_ = 0;
258 
260  PetscInt blockidx_ = 0;
261 
263  PetscInt locidx_ = 0;
264 
265 
266 public:
267 
269 
272  const QuantumNumbers& QN_in
273  ):
274  QN(QN_in),
275  iend_(QN.NumStates())
276  {}
277 
280  const QuantumNumbers& QN_in,
281  const PetscInt& GlobIdxStart,
282  const PetscInt& GlobIdxEnd
283  ):
284  QN(QN_in),
285  istart_(GlobIdxStart),
286  iend_(GlobIdxEnd),
287  idx_(istart_)
288  {
289  PetscErrorCode ierr;
290  if(istart_==iend_) {}
291  else
292  {
293  ierr = QN.GlobalIdxToBlockIdx(istart_, blockidx_);
294  assert(!ierr);
295  }
296  }
297 
298  /* TODO: Initialize an iterator through a range of quantum number blocks */
299 
301  PetscInt Idx() const {return idx_;}
302 
304  PetscInt BlockIdx() const {return blockidx_;}
305 
307  PetscInt IdxStart() const {return istart_;}
308 
310  PetscInt IdxEnd() const {return iend_;}
311 
313  PetscInt LocIdx() const {return idx_ - QN.Offsets()[blockidx_];}
314 
316  bool Loop() const {return idx_ < iend_;}
317 
319  PetscInt Steps() const {return idx_-istart_;}
320 
322  Self_t operator++()
323  {
324  ++idx_;
325  if(idx_ >= QN.Offsets()[blockidx_+1]) ++blockidx_;
326  return *this;
327  }
328 
330  PetscErrorCode OpBlockToGlobalRange(
331  const PetscInt& BlockShift,
332  PetscInt& GlobIdxStart,
333  PetscInt& GlobIdxEnd,
334  PetscBool& flg
335  ) const
336  {
337  PetscInt ierr;
338  ierr = QN.OpBlockToGlobalRange(blockidx_, BlockShift, GlobIdxStart, GlobIdxEnd, flg); CHKERRQ(ierr);
339  return(0);
340  }
341 
342 };
343 
344 
349 #endif // __QUANTUM_NUMBERS_HPP__
PetscInt Offsets(const PetscInt &idx) const
Returns the offsets for the quantum number block with a specified index.
Contains information on quantum numbers and associated index ranges.
std::vector< PetscReal > qn_list
List of Sz quantum numbers.
PetscInt Steps() const
Gets the number of steps incremented from the starting index.
QuantumNumbersIterator(const QuantumNumbers &QN_in, const PetscInt &GlobIdxStart, const PetscInt &GlobIdxEnd)
Initialize an iterator through a range of indices.
const QuantumNumbers & QN
Reference to the Quantum Numbers object on which this iterator is based on.
PetscInt OpBlockToGlobalRangeStart(const PetscInt &BlockIdx, const PetscInt &BlockShift, PetscBool &flg) const
Maps the shifted quantum number block index to the global index start in the range [start...
const std::vector< PetscReal > & ListRef() const
Returns the list of quantum numbers as a const reference.
bool Loop() const
Determines whether the end of the range has not yet been reached.
MPI_Comm MPIComm() const
Gets the communicator associated to the block.
PetscBool initialized
Tells whether initialized previously.
PetscInt IdxEnd() const
Gets the first quantum number block index.
std::vector< PetscInt > qn_offset
Offset for each quantum number block.
PetscErrorCode QNToGlobalRange(const PetscReal &QNValue, PetscInt &GlobIdxStart, PetscInt &GlobIdxEnd) const
Maps the quantum number value to the global indices [start,end)
std::vector< PetscReal > List() const
Returns the list of quantum numbers.
PetscInt Sizes(const PetscInt &idx) const
Returns the number of basis states in each quantum number block.
PetscInt IdxStart() const
Gets the first quantum number block index.
PetscErrorCode BlockIdxToGlobalIdx(const PetscInt &BlockIdx, const PetscInt &LocIdx, PetscInt &GlobIdx) const
Maps the block index of a basis state to its global index.
PetscErrorCode OpBlockToGlobalRange(const PetscInt &BlockShift, PetscInt &GlobIdxStart, PetscInt &GlobIdxEnd, PetscBool &flg) const
Interfaced function OpBlockToGlobalRange from the Quantum Numbers class with current block index as i...
std::vector< PetscInt > Sizes() const
Returns the number of basis states in each quantum number block.
std::vector< PetscInt > Offsets() const
Returns the offsets for each quantum number block.
Iterates over a range of basis indices with information on each of its quantum number.
PetscInt Idx() const
Gets the current state index.
PetscInt BlockIdx() const
Gets the current quantum number block index.
PetscErrorCode BlockIdxToGlobalRange(const PetscInt &BlockIdx, PetscInt &GlobIdxStart, PetscInt &GlobIdxEnd) const
Maps the quantum number block index to the global indices [start,end)
std::vector< PetscInt > qn_size
Number of states in each quantum number block.
PetscErrorCode OpBlockToGlobalRange(const PetscInt &BlockIdx, const PetscInt &BlockShift, PetscInt &GlobIdxStart, PetscInt &GlobIdxEnd, PetscBool &flg) const
Maps the shifted quantum number block index to the global indices [start,end).
Self_t operator++()
Overloading the ++ increment.
PetscInt LocIdx() const
Gets the current local index in the quantum number block.
PetscErrorCode PrintQNs()
Prints out the full list of quantum numbers contained in the root process.
PetscInt NumSectors() const
Returns the number of quantum number sectors.
MPI_Comm mpi_comm
MPI Communicator.
PetscErrorCode GlobalIdxToBlockIdx(const PetscInt &GlobIdx, PetscInt &BlockIdx) const
Maps the global index of a basis state to its block index.
PetscErrorCode GlobalIdxToQN(const PetscInt &GlobIdx, PetscReal &QNValue) const
Maps the global index of a basis state to its quantum number.
PetscErrorCode Initialize(const MPI_Comm &mpi_comm_in, const std::vector< PetscReal > &qn_list_in, const std::vector< PetscInt > &qn_size_in)
Initializes the quantum number object.
PetscInt num_sectors
Number of Sz sectors in the Hilbert space.
PetscReal List(const PetscInt &idx) const
Returns the quantum number associated to the given index.
QuantumNumbersIterator(const QuantumNumbers &QN_in)
Initialize an iterator through all quantum numbers.
PetscErrorCode CheckInitialized() const
Checks whether quantum number object was properly initialized.
PetscInt num_states
Number of basis states in the Hilbert space.
PetscInt NumStates() const
Returns the total number of states.
This site was generated by Sphinx using Doxygen with a customized theme from doxygen-bootstrapped.