PSCF v1.1
MeshIterator.h
1#ifndef PSCF_MESH_ITERATOR_H
2#define PSCF_MESH_ITERATOR_H
3
4/*
5* PSCF - Polymer Self-Consistent Field Theory
6*
7* Copyright 2016 - 2022, The Regents of the University of Minnesota
8* Distributed under the terms of the GNU General Public License.
9*/
10
11#include <pscf/math/IntVec.h> // member
12
13namespace Pscf
14{
15
16 using namespace Util;
17
27 template <int D>
29 {
30
31 public:
32
37
43 MeshIterator(const IntVec<D>& dimensions);
44
45 // Compiler copy constructor.
46
47 // Compiler default destructor.
48
54 void setDimensions(const IntVec<D>& dimensions);
55
59 void begin();
60
64 void operator ++();
65
69 bool atEnd() const;
70
74 IntVec<D> position() const;
75
81 int position(int i) const;
82
86 int rank() const;
87
88 private:
89
91 IntVec<D> dimensions_;
92
94 IntVec<D> position_;
95
97 int rank_;
98
100 int size_;
101
103 void increment(int i);
104
105 };
106
107 // Explicit specialization declarations
108
109 template<>
111
112 template<>
114
115 template<>
117
118 // Inline member functions
119
120 // Return the entire IntVec<D>
121 template <int D>
123 { return position_; }
124
125 // Return one component of the position IntVec<D>
126 template <int D>
127 inline int MeshIterator<D>::position(int i) const
128 {
129 assert(i >=0);
130 assert(i < D);
131 return position_[i];
132 }
133
134 // Return the rank
135 template <int D>
136 inline int MeshIterator<D>::rank() const
137 { return rank_; }
138
139 // Is this the end (i.e., one past the last point)?
140 template <int D>
141 inline bool MeshIterator<D>::atEnd() const
142 { return (bool)(rank_ == size_); }
143
144 // Inline explicit specializations
145
146 template <>
148 {
149 position_[0]++;
150 if (position_[0] == dimensions_[0]) {
151 position_[0] = 0;
152 }
153 rank_++;
154 }
155
156 template <>
158 {
159 position_[1]++;
160 if (position_[1] == dimensions_[1]) {
161 position_[1] = 0;
162 increment(0);
163 }
164 rank_++;
165 }
166
167 template <>
169 {
170 position_[2]++;
171 if (position_[2] == dimensions_[2]) {
172 position_[2] = 0;
173 increment(1);
174 }
175 rank_++;
176 }
177
178 #ifndef PSCF_MESH_ITERATOR_TPP
179 // Suppress implicit instantiation
180 extern template class MeshIterator<1>;
181 extern template class MeshIterator<2>;
182 extern template class MeshIterator<3>;
183 #endif
184
185}
186#endif
An IntVec<D, T> is a D-component vector of elements of integer type T.
Definition: IntVec.h:27
Iterator over points in a Mesh<D>.
Definition: MeshIterator.h:29
void operator++()
Increment iterator to next mesh point.
int rank() const
Get the rank of current element.
Definition: MeshIterator.h:136
void begin()
Set iterator to the first point in the mesh.
MeshIterator()
Default constructor.
bool atEnd() const
Is this the end (i.e., one past the last point)?
Definition: MeshIterator.h:141
void setDimensions(const IntVec< D > &dimensions)
Set the grid dimensions in all directions.
IntVec< D > position() const
Get current position in the grid, as integer vector.
Definition: MeshIterator.h:122
C++ namespace for polymer self-consistent field theory (PSCF).
Utility classes for scientific computation.
Definition: accumulators.mod:1