TBTK
HoppingAmplitudeSet.h
Go to the documentation of this file.
1 /* Copyright 2016 Kristofer Bj√∂rnson
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
23 #ifndef COM_DAFER45_TBTK_HOPPING_AMPLITUDE_SET
24 #define COM_DAFER45_TBTK_HOPPING_AMPLITUDE_SET
25 
26 #include "TBTK/HoppingAmplitude.h"
28 #include "TBTK/IndexTree.h"
29 #include "TBTK/Serializable.h"
30 #include "TBTK/SparseMatrix.h"
31 #include "TBTK/Streams.h"
32 #include "TBTK/TBTKMacros.h"
33 
34 #include <complex>
35 #include <vector>
36 
37 namespace TBTK{
38 
51  virtual public Serializable,
52  private HoppingAmplitudeTree
53 {
54 public:
64 
67 
76  HoppingAmplitudeSet(const std::vector<unsigned int> &capacity);
77 
81  HoppingAmplitudeSet(const HoppingAmplitudeSet &hoppingAmplitudeSet);
82 
86  HoppingAmplitudeSet(HoppingAmplitudeSet &&hoppingAmplitudeSet);
87 
95  HoppingAmplitudeSet(const std::string &serializeation, Mode mode);
96 
98  virtual ~HoppingAmplitudeSet();
99 
106 
113 
116  void construct();
117 
121  bool getIsConstructed() const;
122 
129  int getFirstIndexInBlock(const Index &blockIndex) const;
130 
137  int getLastIndexInBlock(const Index &blockIndex) const;
138 
144  IndexTree getIndexTree() const;
145 
155  IndexTree getIndexTree(const Index &subspace) const;
156 
158  void sort();
159 
161  void constructCOO();
162 
164  void destructCOO();
165 
172  void reconstructCOO();
173 
177  int getNumMatrixElements() const;
178 
180  const int* getCOORowIndices() const;
181 
183  const int* getCOOColIndices() const;
184 
186  const std::complex<double>* getCOOValues() const;
187 
194 
195  class Iterator;
196  class ConstIterator;
197 private:
200  template<bool isConstIterator>
201  class _Iterator{
202  public:
205  typedef typename std::conditional<
206  isConstIterator,
207  const HoppingAmplitude&,
208  HoppingAmplitude&
209  >::type HoppingAmplitudeReferenceType;
210 
212  void operator++();
213 
215  HoppingAmplitudeReferenceType operator*();
216 
218  bool operator==(const _Iterator &rhs) const;
219 
221  bool operator!=(const _Iterator &rhs) const;
222 
224  int getMinBasisIndex() const;
225 
227  int getMaxBasisIndex() const;
228 
230  int getNumBasisIndices() const;
231  private:
234  typedef typename std::conditional<
235  isConstIterator,
238  >::type HoppingAmplitudeTreeIteratorType;
239 
242  typedef typename std::conditional<
243  isConstIterator,
244  const HoppingAmplitudeTree*,
245  HoppingAmplitudeTree*
246  >::type HoppingAmplitudeTreePointerType;
247 
250  HoppingAmplitudeTreeIteratorType iterator;
251 
254  friend class Iterator;
255  friend class ConstIterator;
256 
259  _Iterator(
260  HoppingAmplitudeTreePointerType hoppingAmplitudeTree,
261  bool end = false
262  );
263  };
264 public:
267  class Iterator : public _Iterator<false>{
268  private:
269  Iterator(
270  HoppingAmplitudeTree *hoppingAmplitudeTree,
271  bool end = false
272  ) : _Iterator<false>(hoppingAmplitudeTree, end){};
273 
276  friend class HoppingAmplitudeSet;
277  };
278 
281  class ConstIterator : public _Iterator<true>{
282  private:
284  const HoppingAmplitudeTree *hoppingAmplitudeTree,
285  bool end = false
286  ) : _Iterator<true>(hoppingAmplitudeTree, end){};
287 
290  friend class HoppingAmplitudeSet;
291  };
292 
297  Iterator begin();
298 
303  ConstIterator begin() const;
304 
309  ConstIterator cbegin() const;
310 
317  Iterator begin(const Index &subspace);
318 
326  ConstIterator begin(const Index &subspace) const;
327 
335  ConstIterator cbegin(const Index &subspace) const;
336 
340  Iterator end();
341 
346  ConstIterator end() const;
347 
352  ConstIterator cend() const;
353 
359  Iterator end(const Index &subspace);
360 
368  ConstIterator end(const Index &subspace) const;
369 
377  ConstIterator cend(const Index &subspace) const;
378 
380  void print();
381 
406  void tabulate(
407  std::complex<double> **amplitudes,
408  int **indices,
409  int *numHoppingAmplitudes,
410  int *maxIndexSize
411  ) const;
412 
414  virtual std::string serialize(Mode mode) const;
415 
417  unsigned int getSizeInBytes() const;
418 private:
421  bool isConstructed;
422 
425  bool isSorted;
426 
429  int numMatrixElements;
430 
432  int *cooRowIndices;
433 
435  int *cooColIndices;
436 
438  std::complex<double> *cooValues;
439 };
440 
442  TBTKAssert(
443  !isConstructed,
444  "HoppingAmplitudeSet::construct()",
445  "HoppingAmplitudeSet is already constructed.",
446  ""
447  );
448 
450  isConstructed = true;
451 }
452 
454  return isConstructed;
455 }
456 
458  const Index &blockIndex
459 ) const{
461 }
462 
464  const Index &blockIndex
465 ) const{
467 }
468 
470  TBTKAssert(
471  isConstructed,
472  "HoppingAmplitudeSet::sort()",
473  "HoppingAmplitudeSet has to be constructed first.",
474  ""
475  );
476 
477  if(!isSorted){
479  isSorted = true;
480  }
481 }
482 
483 inline const int* HoppingAmplitudeSet::getCOORowIndices() const{
484  return cooRowIndices;
485 }
486 
487 inline const int* HoppingAmplitudeSet::getCOOColIndices() const{
488  return cooColIndices;
489 }
490 
491 inline const std::complex<double>* HoppingAmplitudeSet::getCOOValues() const{
492  return cooValues;
493 }
494 
496 ) const{
497  TBTKAssert(
498  isConstructed,
499  "HoppingAmplitudeSet::getSparseMatrix()",
500  "HoppingAmplitudeSet has to be constructed first.",
501  ""
502  );
503 
504  SparseMatrix<std::complex<double>> sparseMatrix(
505  SparseMatrix<std::complex<double>>::StorageFormat::CSC
506  );
507 
508  for(
509  ConstIterator iterator = begin();
510  iterator != end();
511  ++iterator
512  ){
513  sparseMatrix.add(
514  getBasisIndex((*iterator).getToIndex()),
515  getBasisIndex((*iterator).getFromIndex()),
516  (*iterator).getAmplitude()
517  );
518  }
519  sparseMatrix.construct();
520 
521  return sparseMatrix;
522 }
523 
525  return Iterator(this);
526 }
527 
529  return ConstIterator(this);
530 }
531 
533  return ConstIterator(this);
534 }
535 
537  const Index &subspace
538 ){
539  return Iterator(getSubTree(subspace));
540 }
541 
543  const Index &subspace
544 ) const{
545  return ConstIterator(getSubTree(subspace));
546 }
547 
549  const Index &subspace
550 ) const{
551  return ConstIterator(getSubTree(subspace));
552 }
553 
555  return Iterator(this, true);
556 }
557 
559  return ConstIterator(this, true);
560 }
561 
563  return ConstIterator(this, true);
564 }
565 
567  const Index &subspace
568 ){
569  return Iterator(getSubTree(subspace), true);
570 }
571 
573  const Index &subspace
574 ) const{
575  return ConstIterator(getSubTree(subspace), true);
576 }
577 
579  const Index &subspace
580 ) const{
581  return ConstIterator(getSubTree(subspace), true);
582 }
583 
584 template<bool isConstIterator>
585 inline bool HoppingAmplitudeSet::_Iterator<isConstIterator>::operator==(
586  const _Iterator &rhs
587 ) const{
588  return iterator == rhs.iterator;
589 }
590 
591 template<bool isConstIterator>
592 inline bool HoppingAmplitudeSet::_Iterator<isConstIterator>::operator!=(
593  const _Iterator &rhs
594 ) const{
595  return iterator != rhs.iterator;
596 }
597 
598 inline unsigned int HoppingAmplitudeSet::getSizeInBytes() const{
599  unsigned int size = sizeof(*this) - sizeof(HoppingAmplitudeTree);
601  if(numMatrixElements > 0){
602  size += numMatrixElements*(
603  sizeof(*cooRowIndices)
604  + sizeof(*cooColIndices)
605  + sizeof(*cooValues)
606  );
607  }
608 
609  return size;
610 }
611 
612 template<bool isConstIterator>
613 inline void HoppingAmplitudeSet::_Iterator<isConstIterator>::operator++(){
614  ++iterator;
615 }
616 
617 template<bool isConstIterator>
618 inline typename HoppingAmplitudeSet::_Iterator<
619  isConstIterator
620 >::HoppingAmplitudeReferenceType HoppingAmplitudeSet::_Iterator<
621  isConstIterator
622 >::operator*(){
623  return *iterator;
624 }
625 
626 template<bool isConstIterator>
627 inline int HoppingAmplitudeSet::_Iterator<isConstIterator>::getMinBasisIndex() const{
628  return iterator.getMinBasisIndex();
629 }
630 
631 template<bool isConstIterator>
632 inline int HoppingAmplitudeSet::_Iterator<isConstIterator>::getMaxBasisIndex() const{
633  return iterator.getMaxBasisIndex();
634 }
635 
636 template<bool isConstIterator>
637 inline int HoppingAmplitudeSet::_Iterator<isConstIterator>::getNumBasisIndices() const{
638  return iterator.getNumBasisIndices();
639 }
640 
641 template<bool isConstIterator>
642 inline HoppingAmplitudeSet::_Iterator<isConstIterator>::_Iterator(
643  HoppingAmplitudeTreePointerType hoppingAmplitudeTree,
644  bool end
645 ) :
646 // iterator(hoppingAmplitudeTree, end)
647  iterator(
648  (
649  end ?
650  hoppingAmplitudeTree->end()
651  : hoppingAmplitudeTree->begin()
652  )
653  )
654 {
655 }
656 
657 }; //End of namespace TBTK
658 
659 #endif
Node in tree used by HoppingAmplitudeSet to store HoppingAmplitudes .
void tabulate(std::complex< double > **amplitudes, int **indices, int *numHoppingAmplitudes, int *maxIndexSize) const
unsigned int getSizeInBytes() const
Definition: HoppingAmplitudeSet.h:598
FockStateRuleSet operator*(const LadderOperator< BitRegister > &ladderOperator, const FockStateRuleSet &fockStateRuleSet)
Definition: FockStateRuleSet.h:103
Node in tree used by HoppingAmplitudeSet to store HoppingAmplitudes .
Definition: HoppingAmplitudeTree.h:40
SparseMatrix< std::complex< double > > getSparseMatrix() const
Definition: HoppingAmplitudeSet.h:495
int getLastIndexInBlock(const Index &blockIndex) const
Definition: HoppingAmplitudeSet.h:463
const int * getCOOColIndices() const
Definition: HoppingAmplitudeSet.h:487
HoppingAmplitude container.
Definition: HoppingAmplitudeSet.h:50
std::vector< Index > getIndexList(const Index &pattern) const
ConstIterator cend() const
Definition: HoppingAmplitudeSet.h:562
int getLastIndexInSubspace(const Index &subspaceIndex) const
Definition: HoppingAmplitudeTree.h:503
Index getSubspaceIndex(const Index &index) const
Hopping amplitude from state &#39;from&#39; to state &#39;to&#39;.
Precompiler macros.
Definition: Serializable.h:40
int getFirstIndexInSubspace(const Index &subspaceIndex) const
Definition: HoppingAmplitudeTree.h:487
Data structure for mapping physical indices to a linear index.
void add(HoppingAmplitude ha)
int getBasisSize() const
Definition: HoppingAmplitudeTree.h:483
Iterator end()
Definition: HoppingAmplitudeSet.h:554
Index getPhysicalIndex(int basisIndex) const
Definition: HoppingAmplitudeSet.h:281
Sparse matrix.
HoppingAmplitudeSet & operator=(const HoppingAmplitudeSet &rhs)
const std::vector< HoppingAmplitude > & getHoppingAmplitudes(Index index) const
const std::complex< double > * getCOOValues() const
Definition: HoppingAmplitudeSet.h:491
void sort()
Definition: HoppingAmplitudeSet.h:469
bool getIsConstructed() const
Definition: HoppingAmplitudeSet.h:453
void construct()
Definition: SparseMatrix.h:678
Definition: HoppingAmplitudeSet.h:267
HoppingAmplitudeTree * getSubTree(const Index &subspace)
Definition: HoppingAmplitudeTree.h:300
unsigned int getSizeInBytes() const
Definition: HoppingAmplitudeTree.h:543
Hopping amplitude from state &#39;from&#39; to state &#39;to&#39;.
Definition: HoppingAmplitude.h:49
const int * getCOORowIndices() const
Definition: HoppingAmplitudeSet.h:483
bool isProperSubspace(const Index &subspace) const
virtual std::string serialize(Mode mode) const
ConstIterator cbegin() const
Definition: HoppingAmplitudeSet.h:532
Data structure for mapping physical indices to linear indices.
Definition: IndexTree.h:34
Flexible physical index.
Definition: Index.h:69
Definition: ModelFactory.h:35
Iterator begin()
Definition: HoppingAmplitudeSet.h:524
int getNumMatrixElements() const
Definition: SparseMatrix.h:35
Definition: HoppingAmplitudeTree.h:314
void sort(HoppingAmplitudeTree *rootNode)
Mode
Definition: Serializable.h:44
IndexTree getSubspaceIndices() const
int getBasisIndex(const Index &index) const
void add(unsigned int row, unsigned int col, const DataType &value)
Definition: SparseMatrix.h:461
Abstract base class for serializable objects.
Streams for TBTK output.
void construct()
Definition: HoppingAmplitudeSet.h:441
IndexTree getIndexTree() const
int getFirstIndexInBlock(const Index &blockIndex) const
Definition: HoppingAmplitudeSet.h:457