PSCF v1.2
MeshIteratorFortran.tpp
1#ifndef PSCF_MESH_ITERATOR_FORTRAN_TPP
2#define PSCF_MESH_ITERATOR_FORTRAN_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 "MeshIteratorFortran.h"
12
13namespace Pscf
14{
15
16 /*
17 * Default constructor
18 */
19 template <int D>
21 : dimensions_(0),
22 offsets_(0),
23 position_(0),
24 rank_(0),
25 size_(0),
26 atEnd_(false)
27 {}
28
29 /*
30 * Constructor that sets mesh dimensions and initializes the iterator.
31 */
32 template <int D>
34 : dimensions_(0),
35 offsets_(0),
36 position_(0),
37 rank_(0),
38 size_(0),
39 atEnd_(false)
41
42 /*
43 * Set the dimensions and initialize the iterator.
44 */
45 template <int D>
47 {
48 // Require that all dimensions are positive
49 for (int i = 0; i < D; ++i) {
50 UTIL_CHECK(dimensions[i] > 0);
51 }
52
53 // Set dimensions_ and size_ member variables
54 dimensions_ = dimensions;
55 size_ = 1;
56 for (int i = 0; i < D; ++i) {
57 size_ *= dimensions_[i];
58 }
59
60 // Compute offsets_
61 offsets_[D - 1] = 1;
62 for (int i = D - 1 ; i > 0; --i ) {
63 offsets_[i - 1] = offsets_[i] * dimensions_[i];
64 }
65
66 // Initialize to first grid point
67 begin();
68 }
69
70 /*
71 * Initialize the iterator to the first grid point, with rank 0.
72 */
73 template <int D>
75 {
76 UTIL_CHECK(size_ > 0);
77 for (int i = 0; i < D; ++i) {
78 position_[i] = 0;
79 }
80 rank_ = 0;
81 atEnd_ = false;
82 }
83
84 /*
85 * Increment the iterator to the next grid point.
86 */
87 template <int D>
89 {
90 UTIL_CHECK(!atEnd_);
91
92 // Cartesian index of position component that is incremented
93 int k = 0;
94
95 // Increment grid position vector
96 bool next = true;
97 while (next) {
98 position_[k]++;
99 if (position_[k] == dimensions_[k]) {
100 position_[k] = 0;
101 if (k == D - 1) {
102 atEnd_ = true;
103 next = false;
104 } else {
105 k++;
106 }
107 } else {
108 next = false;
109 }
110 }
111
112 // Compute array rank from updated position vector
113 rank_ = 0;
114 for (int dim = 0; dim < D; ++dim) {
115 rank_ += offsets_[dim] * position_[dim];
116 }
117
118 }
119
120}
121#endif
An IntVec<D, T> is a D-component vector of elements of integer type T.
Definition IntVec.h:27
MeshIteratorFortran()
Default constructor.
void setDimensions(IntVec< D > const &dimensions)
Set or reset the mesh dimensions, and initialize iterator.
void begin()
Initialize the iterator to the first grid point.
IntVec< D > const & dimensions() const
Return the mesh dimensions as an vector of integers.
void operator++()
Increment the iterator to the next grid point.
#define UTIL_CHECK(condition)
Assertion macro suitable for serial or parallel production code.
Definition global.h:68
PSCF package top-level namespace.
Definition param_pc.dox:1