cppyabm  1.0.17
An agent-based library to integrate C++ and Python
mesh.h
Go to the documentation of this file.
1 
2 #pragma once
3 #include <vector>
4 #include <fstream>
5 #include "common.h"
6 using namespace std;
7 
8 
9 //! A class for mesh items
10 struct MESH_ITEM {
11  unsigned index;
12  vector<double> coords; //!< Coordinates
13  std::vector<unsigned> neighbors_indices; //!< Neighbor mesh indices
14  unsigned layer_index; //!< The layer in which the mesh resides
15  bool on_border = false; //!< Whether the mesh is located on the border
16 };
17 
18  namespace space{
19  //! A function to create 2D rectangular mesh
20  inline vector<MESH_ITEM> grid2(double length, double width, double mesh_length, bool share){
21  auto calculate_mesh_index = [](unsigned i, unsigned j,unsigned x_n) {
22  return (j*x_n + i);
23  };
24  auto find_neighborhood=[&](unsigned i, unsigned j,unsigned x_n, unsigned y_n){
25  vector<unsigned> neighbor_indices;
26  unsigned neighbor_numbers = 8;
27  int* x_index = new int [neighbor_numbers];
28  int* y_index = new int [neighbor_numbers];
29  int counter = 0;
30  // find the neighbors
31  for (int ii = -1; ii <= 1; ii++){
32  for (int jj = -1; jj <= 1; jj++ ){
33  if (ii == 0 & jj == 0) continue;
34  x_index[counter] = i + ii;
35  y_index[counter] = j + jj;
36  counter ++;
37  }
38  }
39 
40  for (unsigned iter = 0; iter < neighbor_numbers; iter++) {
41  // correct for the border elements
42  if (x_index[iter] < 0){
43  if (share){
44  x_index[iter] = x_n - 1;
45  } else continue;
46  }
47  if (y_index[iter] < 0){
48  if (share) {
49  y_index[iter] = y_n -1;
50  } else continue;
51  }
52  if (x_index[iter] >= x_n){
53  if (share){
54  x_index[iter] = 0;
55  } else continue;
56  }
57  if (y_index[iter] >= y_n){
58  if (share){
59  y_index[iter] = 0;
60  } else continue;
61  }
62  auto neighbor_index = calculate_mesh_index(x_index[iter],y_index[iter],x_n);
63 
64  neighbor_indices.push_back(neighbor_index);
65 
66  }
67  return neighbor_indices;
68  };
69 
70  vector<MESH_ITEM> meshes;
71  unsigned x_n = length/mesh_length;
72  unsigned y_n = width/mesh_length;
73  unsigned mesh_count =0;
74  for (unsigned j=0; j<y_n; j++){ //outer vector meshes
75  for (unsigned i=0; i<x_n; i++){ //inner vector meshe
76 
77  auto x = (mesh_length/2)+i*mesh_length;
78  auto y = (mesh_length/2)+j*mesh_length;
79  double z = 0;
80  vector<double> coords = {x,y,z};
81  auto mesh_index = calculate_mesh_index(i,j,x_n);
82  auto neighbor_indices = find_neighborhood(i,j,x_n,y_n);
83  MESH_ITEM mesh_item = {mesh_index,coords,neighbor_indices,0}; // 0 is for layer index
84  if (i==0 or i == x_n -1 or j ==0 or j == y_n-1){
85  mesh_item.on_border = true;
86  }
87  meshes.push_back(mesh_item);
88  mesh_count ++;
89  }
90 
91  }
92  auto message = to_string(mesh_count) + " mesh generated";
93  return meshes;
94  };
95  //! A function to create 3D rectangular mesh
96  inline vector<MESH_ITEM> grid3(double length, double width, double height, double mesh_length, bool share){
97  auto calculate_mesh_index = [](unsigned i, unsigned j, unsigned k, unsigned x_n, unsigned y_n) {
98  return (k*y_n*x_n + j*x_n + i);
99  };
100  auto find_neighborhood=[&](unsigned i, unsigned j, unsigned z, unsigned x_n, unsigned y_n, unsigned z_n){
101  vector<unsigned> neighbor_indices;
102  unsigned neighbor_numbers = 26;
103  int* x_index = new int [neighbor_numbers];
104  int* y_index = new int [neighbor_numbers];
105  int* z_index = new int [neighbor_numbers];
106  int counter = 0;
107  // find the neighbors
108  for (int ii = -1; ii <= 1; ii++){
109  for (int jj = -1; jj <= 1; jj++ ){
110  for (int kk = -1; kk <= 1; kk++ ){
111  if (ii == 0 & jj == 0 & kk == 0) continue;
112  x_index[counter] = i + ii;
113  y_index[counter] = j + jj;
114  z_index[counter] = z + kk;
115  counter ++;
116  }
117  }
118  }
119 
120  for (unsigned iter = 0; iter < neighbor_numbers; iter++) {
121  // correct for the border elements
122  if (x_index[iter] < 0){
123  if (share){
124  x_index[iter] = x_n - 1;
125  } else continue;
126  }
127  if (y_index[iter] < 0){
128  if (share) {
129  y_index[iter] = y_n -1;
130  } else continue;
131  }
132  if (x_index[iter] >= x_n){
133  if (share){
134  x_index[iter] = 0;
135  } else continue;
136  }
137  if (y_index[iter] >= y_n){
138  if (share){
139  y_index[iter] = 0;
140  } else continue;
141  }
142  if (z_index[iter] < 0){
143  if (share){
144  z_index[iter] = x_n - 1;
145  } else continue;
146  }
147  if (z_index[iter] >= z_n){
148  if (share){
149  z_index[iter] = 0;
150  } else continue;
151  }
152  // if (x_index[iter] < 0 or x_index[iter] >= x_n or
153  // y_index[iter] < 0 or y_index[iter] >= y_n or
154  // z_index[iter] < 0 or z_index[iter] >= z_n) continue;
155  auto neighbor_index = calculate_mesh_index(x_index[iter],y_index[iter],z_index[iter],x_n,y_n);
156 
157  neighbor_indices.push_back(neighbor_index);
158 
159  }
160  return neighbor_indices;
161  };
162 
163  vector<MESH_ITEM> meshes;
164 
165 
166  unsigned x_n = length/mesh_length;
167  unsigned y_n = width/mesh_length;
168  unsigned z_n = height/mesh_length;
169  unsigned mesh_count =0;
170  // ** loop over x,y,z and create meshes **//
171 
172  for (unsigned k=0; k<z_n; k++){
173  for (unsigned j=0; j<y_n; j++){ //outer vector meshes
174  for (unsigned i=0; i<x_n; i++){ //inner vector meshe
175  auto x = (mesh_length/2)+i*mesh_length;
176  auto y = (mesh_length/2)+j*mesh_length;
177  auto z = (mesh_length/2)+k*mesh_length;
178  vector<double> coords = {x,y,z};
179  auto mesh_index = calculate_mesh_index(i,j,k,x_n,y_n);
180  auto neighbor_indices = find_neighborhood(i,j,k,x_n,y_n,z_n);
181  MESH_ITEM mesh_item = {mesh_index,coords,neighbor_indices,k};
182  meshes.push_back(mesh_item);
183  mesh_count ++;
184  }
185 
186  }
187  }
188  auto message = to_string(mesh_count) + " mesh generated";
189  return meshes;
190  };
191 
192 }
193 
test_multiple_inheritance.i
i
Definition: test_multiple_inheritance.py:22
common.h
test_builtin_casters.x
x
Definition: test_builtin_casters.py:467
space
Definition: mesh.h:18
MESH_ITEM::coords
vector< double > coords
Coordinates.
Definition: mesh.h:12
space::grid3
vector< MESH_ITEM > grid3(double length, double width, double height, double mesh_length, bool share)
A function to create 3D rectangular mesh.
Definition: mesh.h:96
MESH_ITEM
A class for mesh items.
Definition: mesh.h:10
MESH_ITEM::on_border
bool on_border
Whether the mesh is located on the border.
Definition: mesh.h:15
iter
iterator iter(handle obj)
Definition: pytypes.h:1595
MESH_ITEM::neighbors_indices
std::vector< unsigned > neighbors_indices
Neighbor mesh indices.
Definition: mesh.h:13
space::grid2
vector< MESH_ITEM > grid2(double length, double width, double mesh_length, bool share)
A function to create 2D rectangular mesh.
Definition: mesh.h:20
MESH_ITEM::index
unsigned index
Definition: mesh.h:11
MESH_ITEM::layer_index
unsigned layer_index
The layer in which the mesh resides.
Definition: mesh.h:14