MDStressLab++
Grid.cpp
Go to the documentation of this file.
1 /*
2  * Grid.cpp
3  *
4  * Created on: Nov 5, 2019
5  * Author: Nikhil
6  */
7 
8 #include "Grid.h"
9 #include <fstream>
10 #include "typedef.h"
11 #include "SpatialHash.h"
12 #include <iostream>
13 
14 template<ConfigType T>
15 Grid<T>::Grid(int _ngrid) : ngrid(_ngrid)
16 {
17  this->setCounter();
19  if (numberOfGrids == 1) MY_HEADING("Creating Grids");
20 
21  std::cout << "Grid " << numberOfGrids << ". Initializing a grid of size " << ngrid << " to the origin." << std::endl;
22  this->coordinates.resize(ngrid,Vector3d(0.0,0.0,0.0));
23 }
24 
25 template<ConfigType T>
27  Vector3d upperLimit,
28  int ngridx, int ngridy, int ngridz):ngrid(ngridx*ngridy*ngridz)
29 {
30  if ( !(lowerLimit.array() < upperLimit.array()).prod() )
31  MY_ERROR("ERROR: The coordinates of lowerLimit are not less than the upperLimit");
32 
33  this->setCounter();
35  if (numberOfGrids == 1) MY_HEADING("Creating Grids");
36 
37 
38  std::cout << "Grid " << numberOfGrids << ". Creating a uniform grid of " << ngrid << " points between (" << lowerLimit
39  << ") and (" << upperLimit << ")"<< std::endl;
40  std::cout << std::endl;
41  coordinates.resize(ngrid);
42 
43  int index= 0;
44  Vector3d coordinate;
45  coordinate= lowerLimit;
46  for (auto i : range<int>(0,ngridx))
47  {
48  coordinate(0) = lowerLimit(0) + i*(upperLimit(0)-lowerLimit(0))/ngridx;
49  for (auto j : range<int>(0,ngridy))
50  {
51  coordinate(1) = lowerLimit(1) + j*(upperLimit(1)-lowerLimit(1))/ngridy;
52  for (auto k : range<int>(0,ngridz))
53  {
54  coordinate(2)= lowerLimit(2) + k*(upperLimit(2)-lowerLimit(2))/ngridz;
55  coordinates[index]= coordinate;
56  index++;
57  }
58  }
59  }
60 }
61 
62 template<ConfigType T>
63 Grid<T>::Grid(std::string filename)
64 {
65  this->setCounter();
67  if (numberOfGrids == 1) MY_HEADING("Creating Grids");
68  std::cout << "Grid " << numberOfGrids << ". Reading grid from filename: " << filename << std::endl;
69  std::cout << std::endl;
70 
71  std::ifstream file(filename);
72 
73  file >> ngrid;
74  coordinates.resize(ngrid);
75  for(auto& position : coordinates)
76  for (int i_dim=0; i_dim<DIM; i_dim++)
77  if(!(file >> position(i_dim))) MY_ERROR("ERROR: Reading grid coordinates");
78 }
79 
80 // Function to calculate neighbor lists of grid points consisting of particles in subconfig within a distance of
81 // padding from the grid points
82 template<ConfigType T>
83 std::vector<std::set<int>> Grid<T>::getGridNeighborLists(const SubConfiguration& subconfig, const double& padding) const
84 {
85  std::vector<std::set<int>> gridNeighborLists;
86 
87  // Hash the coordinates
88  Vector3d origin,step;
89  origin.setConstant(0.0);
90  step.setConstant(padding);
91  ConstSpatialHash hashParticles(origin,step,subconfig.coordinates.at(T));
92  // Hash the grid points
93  ConstSpatialHash hashGrid(origin,step,coordinates);
94 
95 
96  int i_gridPoint= 0;
97  for(const auto& gridPoint : coordinates)
98  {
99  std::set<int> gridContributingList;
100  Triplet bin= hashGrid.hashFunction(i_gridPoint);
101  Triplet neighborBin;
102  // for each neighboring bin of a grid point's bin
103  for (const auto& neighborBin : bin.neighborList())
104  {
105  std::vector<int>& particleList= hashParticles.hashTable[neighborBin];
106  // for each particle in a neighboring bin
107  for(const auto& particle : particleList)
108  {
109  double distance= (subconfig.coordinates.at(T).row(particle)-gridPoint).squaredNorm();
110  if (distance<=pow(padding,2))
111  gridContributingList.insert(particle);
112  }
113  }
114  gridNeighborLists.push_back(gridContributingList);
115  i_gridPoint++;
116 
117  }
118  return gridNeighborLists;
119 }
120 
121 
122 template<ConfigType T>
123 void Grid<T>::write(std::string filename) const
124 {
125  std::ofstream file(filename+".grid");
126 
127  file << ngrid << "\n";
128  file << "\n";
129  for (const auto& coordinate : coordinates)
130  file << coordinate << std::endl;
131 }
132 
133 template<ConfigType T>
134 void Grid<T>::read(std::string filename)
135 {
136  std::cout << "Reading grid from file " << filename << "\n" << std::endl;
137 
138  std::ifstream file(filename);
139 
140  int ngridInFile;
141  file >> ngridInFile;
142  if (ngrid!= ngridInFile)
143  MY_ERROR("Error: Number of grid points in file" << " = " << ngridInFile <<
144  ", does not equal to " << ngrid << " in grid.");
145 
146  for(auto& position : coordinates)
147  for (int i_dim=0; i_dim<DIM; i_dim++)
148  if(!(file >> position(i_dim))) MY_ERROR("ERROR: Reading grid coordinates");
149 }
150 
151 template<ConfigType T>
153 {
155  else if (T == Current) GridBase::numberOfCurrentGrids+=1;
156  else MY_ERROR("Unrecognized grid type");
157 }
158 
159 
160 template<ConfigType T>
162  // TODO Auto-generated destructor stub
163 }
164 
#define MY_HEADING(heading)
Definition: typedef.h:36
void read(std::string)
Definition: Grid.cpp:134
void setCounter()
Definition: Grid.cpp:152
std::map< ConfigType, MatrixXd > coordinates
Definition: Configuration.h:24
std::vector< std::set< int > > getGridNeighborLists(const SubConfiguration &, const double &) const
Definition: Grid.cpp:83
std::vector< Vector3d > coordinates
Definition: Grid.h:18
Definition: range.h:11
void write(std::string) const
Definition: Grid.cpp:123
virtual ~Grid()
Definition: Grid.cpp:161
#define MY_ERROR(message)
Definition: typedef.h:17
Grid(int)
Definition: Grid.cpp:15
Eigen::Matrix< double, 1, DIM, Eigen::RowMajor > Vector3d
Definition: typedef.h:60
static int numberOfReferenceGrids
Definition: Grid.h:19
std::vector< Triplet > neighborList()
Definition: SpatialHash.h:42
const int DIM
Definition: typedef.h:51
int ngrid
Definition: Grid.h:39
static int numberOfCurrentGrids
Definition: Grid.h:20