PSCF v1.1
MeshIterator.tpp
1#ifndef PSCF_MESH_ITERATOR_TPP
2#define PSCF_MESH_ITERATOR_TPP
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 "MeshIterator.h"
12
13namespace Pscf
14{
15
16 /*
17 * Default constructor
18 */
19 template <int D>
21 : dimensions_(0),
22 position_(0),
23 rank_(0),
24 size_(0)
25 {}
26
27 /*
28 * Constructor
29 */
30 template <int D>
32 : dimensions_(0),
33 position_(0),
34 rank_(0),
35 size_(0)
36 { setDimensions(dimensions); }
37
38 /*
39 * Set the mesh dimensions.
40 */
41 template <int D>
43 {
44 for (int i = 0; i < D; ++i) {
45 if (dimensions[i] <= 0) {
46 UTIL_THROW("Mesh dimensions must be positive");
47 }
48 }
49
50 dimensions_ = dimensions;
51 size_ = 1;
52 for (int i = 0; i < D; ++i) {
53 size_ *= dimensions_[i];
54 }
55 }
56
57 /*
58 * Reset iterator to point to first element.
59 */
60 template <int D>
62 {
63 rank_ = 0;
64 for (int i = 0; i < D; ++i) {
65 position_[i] = 0;
66 }
67 }
68
69 /*
70 * Increment this iterator to the next mesh point (default templ.).
71 *
72 * Note: Explicit instantiations are defined for D = 1, 2 and 3.
73 * This default implementation should thus not normally be used.
74 */
75 template <int D>
77 {
78 position_[D-1]++;
79 if (position_[D-1] == dimensions_[D-1]) {
80 position_[D-1] = 0;
81 if (D > 1) {
82 increment(D-2);
83 }
84 }
85 rank_++;
86 }
87
88 /*
89 * Recursive increment function (private).
90 */
91 template <int D>
92 inline void MeshIterator<D>::increment(int i)
93 {
94 position_[i]++;
95 if (position_[i] == dimensions_[i]) {
96 position_[i] = 0;
97 if (i > 0) {
98 increment(i-1);
99 }
100 }
101 }
102
103}
104#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.
void begin()
Set iterator to the first point in the mesh.
MeshIterator()
Default constructor.
void setDimensions(const IntVec< D > &dimensions)
Set the grid dimensions in all directions.
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Definition: global.h:51
C++ namespace for polymer self-consistent field theory (PSCF).