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