Simpatico  v1.10
Grid.cpp
1 /*
2 * Util Package - C++ Utilities for Scientific Computation
3 *
4 * Copyright 2010 - 2017, The Regents of the University of Minnesota
5 * Distributed under the terms of the GNU General Public License.
6 */
7 
8 #include "Grid.h"
9 #include "Dimension.h"
10 #include <util/global.h>
11 
12 namespace Util
13 {
14 
16  : dimensions_(0),
17  offsets_(0),
18  size_(0)
19  {
21  setDimensions(dimensions);
22  }
23 
25  : dimensions_(0),
26  offsets_(0),
27  size_(0)
28  {
29  setDimensions(dimensions);
30  }
31 
33  {
34  int i;
35  for (i = 0; i < Dimension; ++i) {
36  if (dimensions[i] <= 0) {
37  UTIL_THROW("Grid dimensions must be positive");
38  }
39  }
40 
41  dimensions_ = dimensions;
42  offsets_[Dimension -1] = 1;
43  for (i = Dimension - 1; i > 0; --i) {
44  offsets_[i-1] = offsets_[i]*dimensions_[i];
45  }
46  size_ = offsets_[0]*dimensions_[0];
47  }
48 
49  int Grid::rank(const IntVector& position) const
50  {
51  int i;
52  int result = 0;
53  for (i = 0; i < Dimension - 1; ++i) {
54  assert(position[i] >= 0);
55  assert(position[i] < dimensions_[i]);
56  result += position[i]*offsets_[i];
57  }
58  assert(position[i] >= 0);
59  assert(position[i] < dimensions_[i]);
60  result += position[i];
61  return result;
62  }
63 
64  IntVector Grid::position(int id) const
65  {
67  int remainder = id;
68 
69  int i;
70  for (i = 0; i < Dimension - 1; ++i) {
71  position[i] = remainder/offsets_[i];
72  remainder -= position[i]*offsets_[i];
73  }
74  position[i] = remainder;
75  return position;
76  }
77 
78  bool Grid::isInGrid(int coordinate, int i) const
79  {
80  bool result = true;
81  if (coordinate < 0)
82  result = false;
83  if (coordinate >= dimensions_[i])
84  result = false;
85  return result;
86  }
87 
89  {
90  bool result = true;
91  for (int i = 0; i < Dimension; ++i) {
92  if (position[i] < 0)
93  result = false;
94  if (position[i] >= dimensions_[i])
95  result = false;
96  }
97  return result;
98  }
99 
100  int Grid::shift(int& coordinate, int i) const
101  {
102  int shift;
103  if (coordinate >= 0) {
104  shift = coordinate/dimensions_[i];
105  } else {
106  shift = -1 + ((coordinate+1)/dimensions_[i]);
107  }
108  coordinate -= shift*dimensions_[i];
109  return shift;
110  }
111 
113  {
114  IntVector shifts;
115  for (int i = 0; i < Dimension; ++i) {
116  shifts[i] = shift(position[i], i);
117  }
118  return shifts;
119  }
120 
121 }
const int Dimension
Dimensionality of space.
Definition: Dimension.h:19
IntVector dimensions() const
Get an IntVector of the grid dimensions.
Definition: Grid.h:156
Grid()
Default constructor.
Definition: Grid.cpp:15
File containing preprocessor macros for error handling.
IntVector position(int rank) const
Get the position IntVector of a grid point with a specified rank.
Definition: Grid.cpp:64
bool isInGrid(int coordinate, int i) const
Is this coordinate in range?
Definition: Grid.cpp:78
int shift(int &coordinate, int i) const
Shift a periodic coordinate into range.
Definition: Grid.cpp:100
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Definition: global.h:51
Utility classes for scientific computation.
Definition: accumulators.mod:1
int rank(const IntVector &position) const
Get the rank of a grid point with specified position.
Definition: Grid.cpp:49
An IntVector is an integer Cartesian vector.
Definition: IntVector.h:73
void setDimensions(const IntVector &dimensions)
Set the grid dimensions in all directions.
Definition: Grid.cpp:32