ergo
sparse_pattern.h
Go to the documentation of this file.
1 /* Ergo, version 3.7, a program for linear scaling electronic structure
2  * calculations.
3  * Copyright (C) 2018 Elias Rudberg, Emanuel H. Rubensson, Pawel Salek,
4  * and Anastasia Kruchinina.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  *
19  * Primary academic reference:
20  * Ergo: An open-source program for linear-scaling electronic structure
21  * calculations,
22  * Elias Rudberg, Emanuel H. Rubensson, Pawel Salek, and Anastasia
23  * Kruchinina,
24  * SoftwareX 7, 107 (2018),
25  * <http://dx.doi.org/10.1016/j.softx.2018.03.005>
26  *
27  * For further information about Ergo, see <http://www.ergoscf.org>.
28  */
29 
37 #if !defined(_DFT_SPARSE_PATTERN_H_)
38 #define _DFT_SPARSE_PATTERN_H_ 1
39 
40 #if !defined(BEGIN_NAMESPACE)
41 #define BEGIN_NAMESPACE(x) namespace x {
42 #define END_NAMESPACE(x) } /* x */
43 #endif
44 
45 #include <vector>
46 #include <stdio.h>
47 
48 #include "basisinfo.h"
49 
51 
52 
54  public:
56  struct Interval {
57  int lo, hi;
58  Interval(int l_, int h_) : lo(l_), hi(h_){}
59  };
60  typedef std::vector<Interval> IntervalList;
61  struct Column {
63 
64  void addInterval(int lo, int hi);
65  void addIntervals(int nIntervals, int (*intervals)[2]);
66  struct Iterator {
67  IntervalList::const_iterator current, end;
68  int pos;
69  Iterator(const IntervalList::const_iterator& beg,
70  const IntervalList::const_iterator& end_, int p)
71  : current(beg), end(end_), pos(p)
72  {}
73 
75  ++pos;
76 #if 0
77  if(pos == current->hi)
78  printf("Iterator increased to %d current limit %d last? %s %s\n",
79  pos, current->hi,
80  & *current == & *end ? "YES" : "NO",
81  current == end ? "YES" : "NO");
82 #endif
83  if(pos >= current->hi) {
84  ++current;
85  if(current != end)
86  pos = current->lo;
87  else pos = 0;
88  }
89  return *this;
90  }
91  bool operator!=(const Iterator& other) const {
92  bool res = !(& *current == & *other.current && pos == other.pos);
93 #if 0
94  printf("Iterator::operator!=() compares %p with %p, returns %s \n",
95  & *current, & *other.current, res ? "TRUE" : "FALSE");
96 #endif
97  return res;
98  }
99  int operator*() const {
100  //printf("Iterator::operator*() returns %d\n", pos);
101  return pos;
102  }
103  const Interval* operator->() const {
104  return &(*current);
105  }
106 
107  };
108 
109  Iterator begin() const {
110  IntervalList::const_iterator a = list.begin();
111  IntervalList::const_iterator b = list.end();
112  return Iterator(a, b, a != list.end() ? a->lo : 0);
113  }
114 
115  Iterator end() const {
116  return Iterator(list.end(),list.end(),0);
117  }
118 
119  int size() const {
120  int result = 0;
121  for(IntervalList::const_iterator i = list.begin();
122  i != list.end(); ++i)
123  result += i->hi- i->lo;
124  return result;
125  }
126  };
127 
128  private:
131  public:
132  explicit SparsePattern(const BasisInfoStruct& bis_)
133  : bis(bis_), ranges(new Column[bis_.noOfBasisFuncs])
134  { }
135 
137  delete []ranges;
138  }
139 
142  void add(int nRanges, const int (*range)[2]);
143 
144  void save(FILE *f) const;
145  void load(FILE *f);
146  const Column& operator[](int column) const {
147  return ranges[column];
148  }
149 
151  int getColumnSize(int col) const {
152  return ranges[col].size();
153  }
154 
156  int size() const {
157  return bis.noOfBasisFuncs;
158  }
160  int sizeTotal() const;
161 };
162 
163 void setupShellMap(const BasisInfoStruct& bis, int *shellMap, int *aoMap);
164 
166 
167 #endif /* _DFT_SPARSE_PATTERN_H_ */
int pos
Definition: sparse_pattern.h:68
const BasisInfoStruct & bis
Definition: sparse_pattern.h:129
bool operator!=(const Iterator &other) const
Definition: sparse_pattern.h:91
Iterator(const IntervalList::const_iterator &beg, const IntervalList::const_iterator &end_, int p)
Definition: sparse_pattern.h:69
const Column & operator[](int column) const
Definition: sparse_pattern.h:146
#define END_NAMESPACE(x)
Definition: sparse_pattern.h:42
int size() const
Returns the dimension of the pattern.
Definition: sparse_pattern.h:156
int operator*() const
Definition: sparse_pattern.h:99
Definition: sparse_pattern.h:61
#define BEGIN_NAMESPACE(x)
Definition: sparse_pattern.h:41
A way to store sparse matrix patterns.
Definition: sparse_pattern.h:53
int lo
Definition: sparse_pattern.h:57
Column * ranges
Definition: sparse_pattern.h:130
std::vector< Interval > IntervalList
Definition: sparse_pattern.h:60
Code for setting up basis functions starting from shells.
Definition: sparse_pattern.h:66
IntervalList list
Definition: sparse_pattern.h:62
int getColumnSize(int col) const
returns the number of stored elements for specified column.
Definition: sparse_pattern.h:151
void setupShellMap(const BasisInfoStruct &bis, int *shellMap, int *aoMap)
Definition: sparse_pattern.cc:446
IntervalList::const_iterator end
Definition: sparse_pattern.h:67
ranges are upper-exclusive: involve i: lo <= i < hi.
Definition: sparse_pattern.h:56
int size() const
Definition: sparse_pattern.h:119
int noOfBasisFuncs
Definition: basisinfo.h:120
Iterator end() const
Definition: sparse_pattern.h:115
IntervalList::const_iterator current
Definition: sparse_pattern.h:67
const Interval * operator->() const
Definition: sparse_pattern.h:103
Definition: basisinfo.h:112
Iterator begin() const
Definition: sparse_pattern.h:109
SparsePattern(const BasisInfoStruct &bis_)
Definition: sparse_pattern.h:132
~SparsePattern()
Definition: sparse_pattern.h:136
Interval(int l_, int h_)
Definition: sparse_pattern.h:58
Definition: grid_matrix.h:42
Iterator & operator++()
Definition: sparse_pattern.h:74