PSCF v1.1
Mesh.tpp
1#ifndef PSCF_MESH_TPP
2#define PSCF_MESH_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 "Mesh.h"
12#include <util/global.h>
13
14namespace Pscf
15{
16
17 using namespace Util;
18
19 // Default constructor
20 template <int D>
22 : dimensions_(0),
23 offsets_(0),
24 size_(0)
25 {}
26
27 // Copy constructor
28 template <int D>
29 Mesh<D>::Mesh(Mesh<D> const & other)
30 : dimensions_(other.dimensions_),
31 offsets_(other.offsets_),
32 size_(other.size_)
33 {}
34
35 // Construct from dimensions
36 template <int D>
37 Mesh<D>::Mesh(IntVec<D> const & dimensions)
38 : dimensions_(0),
39 offsets_(0),
40 size_(0)
42
43 // Assignment operator.
44 template <int D>
46 {
47 setDimensions(other.dimensions_);
48 return *this;
49 }
50
51 // Set or reset the state of existing Mesh<D>
52 template <int D>
53 void Mesh<D>::setDimensions(IntVec<D> const & dimensions)
54 {
55 int i;
56 for (i = 0; i < D; ++i) {
57 if (dimensions[i] <= 0) {
58 UTIL_THROW("Mesh dimensions must be positive");
59 }
60 }
61
62 dimensions_ = dimensions;
63 offsets_[D -1] = 1;
64 for (i = D - 1; i > 0; --i) {
65 offsets_[i-1] = offsets_[i]*dimensions_[i];
66 }
67 size_ = offsets_[0]*dimensions_[0];
68 }
69
70 // Return rank of IntVec position
71 template <int D>
72 int Mesh<D>::rank(IntVec<D> const & position) const
73 {
74 int i;
75 int result = 0;
76 for (i = 0; i < D - 1; ++i) {
77 assert(position[i] >= 0);
78 assert(position[i] < dimensions_[i]);
79 result += position[i]*offsets_[i];
80 }
81 assert(position[i] >= 0);
82 assert(position[i] < dimensions_[i]);
83 result += position[i];
84 return result;
85 }
86
87 // Return IntVec vector corresponding to rank id
88 template <int D>
90 {
91 IntVec<D> position;
92 int remainder = id;
93
94 int i;
95 for (i = 0; i < D - 1; ++i) {
96 position[i] = remainder/offsets_[i];
97 remainder -= position[i]*offsets_[i];
98 }
99 position[i] = remainder;
100 return position;
101 }
102
103 // Is a single coordinate in the primary range?
104 template <int D>
105 bool Mesh<D>::isInMesh(int coordinate, int i) const
106 {
107 bool result = true;
108 if (coordinate < 0)
109 result = false;
110 if (coordinate >= dimensions_[i])
111 result = false;
112 return result;
113 }
114
115 // Is IntVec<D> argument in the primary cell of this mesh?
116 template <int D>
117 bool Mesh<D>::isInMesh(IntVec<D> const & position) const
118 {
119 bool result = true;
120 for (int i = 0; i < D; ++i) {
121 if (position[i] < 0)
122 result = false;
123 if (position[i] >= dimensions_[i])
124 result = false;
125 }
126 return result;
127 }
128
129 // Shift a single coordinate to the primary range
130 template <int D>
131 int Mesh<D>::shift(int& coordinate, int i) const
132 {
133 int shift;
134 if (coordinate >= 0) {
135 shift = coordinate/dimensions_[i];
136 } else {
137 shift = -1 + ((coordinate+1)/dimensions_[i]);
138 }
139 coordinate -= shift*dimensions_[i];
140 return shift;
141 }
142
143 // Shift the vector to the primary cell
144 template <int D>
146 {
147 IntVec<D> shifts;
148 for (int i = 0; i < D; ++i) {
149 shifts[i] = shift(position[i], i);
150 }
151 return shifts;
152 }
153
154}
155#endif
An IntVec<D, T> is a D-component vector of elements of integer type T.
Definition: IntVec.h:27
Description of a regular grid of points in a periodic domain.
Definition: Mesh.h:61
IntVec< D > dimensions() const
Get an IntVec<D> of the grid dimensions.
Definition: Mesh.h:217
Mesh< D > & operator=(Mesh< D > const &other)
Assignment operator.
Definition: Mesh.tpp:45
int rank(IntVec< D > const &position) const
Get the rank of a grid point with specified position.
Definition: Mesh.tpp:72
bool isInMesh(int coordinate, int i) const
Is this coordinate in range?
Definition: Mesh.tpp:105
IntVec< D > position(int rank) const
Get the position IntVec<D> of a grid point with a specified rank.
Definition: Mesh.tpp:89
int shift(int &coordinate, int i) const
Shift a periodic coordinate into range.
Definition: Mesh.tpp:131
void setDimensions(IntVec< D > const &dimensions)
Set the grid dimensions for an existing mesh.
Definition: Mesh.tpp:53
Mesh()
Default constructor.
Definition: Mesh.tpp:21
File containing preprocessor macros for error handling.
#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).
Utility classes for scientific computation.
Definition: accumulators.mod:1