29 const auto n_x = p.n_x;
30 const auto n_y = p.n_y;
41 auto i_from_xy = [&](
int x,
int y) ->
int {
return x + y*n_x; };
44 auto connect_cells = [&](
int i,
int j){
grid_wiring.at(i).push_back(j); };
47 auto wire_central_cell = [&](
int x,
int y)
50 auto i_cell = i_from_xy(x, y);
52 connect_cells(i_cell, i_cell+n_x);
53 connect_cells(i_cell, i_cell-n_x);
54 connect_cells(i_cell, i_cell+1);
55 connect_cells(i_cell, i_cell-1);
58 auto wire_central_cells = [&]()
60 for (
auto y=1; y<n_y-1; y++)
62 for (
auto x=1; x<n_x-1; x++)
64 wire_central_cell(x,y);
70 auto wire_periodic_edge_cell_yplus = [&](
int x,
int y)
72 auto i_cell = i_from_xy(x, y);
73 auto i_yplus = (y < n_y-1) ? i_cell + n_x : x;
74 connect_cells(i_cell, i_yplus);
76 auto wire_periodic_edge_cell_yminus = [&](
int x,
int y)
78 auto i_cell = i_from_xy(x, y);
79 auto i_yminus = (y > 0) ? i_cell - n_x : x + (n_y-1)*n_x;
80 connect_cells(i_cell, i_yminus);
82 auto wire_periodic_edge_cell_xplus = [&](
int x,
int y)
84 auto i_cell = i_from_xy(x, y);
85 auto i_xplus = (x < n_x-1) ? i_cell + 1 : 0 + y*n_x;
86 connect_cells(i_cell, i_xplus);
88 auto wire_periodic_edge_cell_xminus = [&](
int x,
int y)
90 auto i_cell = i_from_xy(x, y);
91 auto i_xminus = (x > 0) ? i_cell - 1 : n_x-1 + y*n_x;
92 connect_cells(i_cell, i_xminus);
94 auto wire_periodic_edge_cell = [&](
int x,
int y)
96 wire_periodic_edge_cell_yplus(x, y);
97 wire_periodic_edge_cell_yminus(x, y);
98 wire_periodic_edge_cell_xplus(x, y);
99 wire_periodic_edge_cell_xminus(x, y);
101 auto wire_periodic_y_edges = [&](
int x)
103 assert(x==0 or x==n_x-1);
104 for (
auto y=1; y<n_y-1; y++)
106 wire_periodic_edge_cell(x, y);
109 auto wire_periodic_x_edges = [&](
int y)
111 assert(y==0 or y==n_y-1);
112 for (
auto x=1; x<n_x-1; x++)
114 wire_periodic_edge_cell(x, y);
119 auto wire_bounded_y_edges = [&](
int x)
121 assert(x==0 or x==n_x-1);
122 auto plus_or_minus = (x==0) ? +1 : -1;
123 for (
auto y=1; y<n_y-1; y++)
125 auto i_cell = i_from_xy(x, y);
126 connect_cells(i_cell, i_cell + n_x*plus_or_minus);
127 connect_cells(i_cell, i_cell - n_x*plus_or_minus);
128 connect_cells(i_cell, i_cell + plus_or_minus);
131 auto wire_bounded_x_edges = [&](
int y)
133 assert(y==0 or y==n_y-1);
134 auto plus_or_minus = (y==0) ? +1 : -1;
135 for (
auto x=1; x<n_x-1; x++)
137 auto i_cell = i_from_xy(x, y);
138 connect_cells(i_cell, i_cell + n_x*plus_or_minus);
139 connect_cells(i_cell, i_cell - 1);
140 connect_cells(i_cell, i_cell + 1);
145 auto wire_x_edges = [&](
bool is_periodic_x_edge)
147 if (is_periodic_x_edge)
149 wire_periodic_x_edges(0);
150 wire_periodic_x_edges(n_y-1);
154 wire_bounded_x_edges(0);
155 wire_bounded_x_edges(n_y-1);
158 auto wire_y_edges = [&](
bool is_periodic_y_edge)
160 if (is_periodic_y_edge)
162 wire_periodic_y_edges(0);
163 wire_periodic_y_edges(n_x-1);
167 wire_bounded_y_edges(0);
168 wire_bounded_y_edges(n_x-1);
173 auto wire_corners = [&](
bool is_periodic_x_edge,
bool is_periodic_y_edge)
175 int x, y, i_cell, i_yminus, i_xminus, i_yplus, i_xplus;
180 i_cell = i_from_xy(x, y);
181 i_xplus = i_cell + 1;
182 i_xminus = n_x-1 + y*n_x;
183 i_yplus = i_cell + n_x;
184 i_yminus = x + (n_y-1)*n_x;
185 connect_cells(i_cell, i_xplus);
186 connect_cells(i_cell, i_yplus);
187 if (is_periodic_x_edge) { connect_cells(i_cell, i_yminus); }
188 if (is_periodic_y_edge) { connect_cells(i_cell, i_xminus); }
193 i_cell = i_from_xy(x, y);
194 i_xplus = i_cell + 1;
195 i_xminus = n_x-1 + y*n_x;
197 i_yminus = i_cell - n_x;
198 connect_cells(i_cell, i_xplus);
199 connect_cells(i_cell, i_yminus);
200 if (is_periodic_x_edge) { connect_cells(i_cell, i_yplus); }
201 if (is_periodic_y_edge) { connect_cells(i_cell, i_xminus); }
206 i_cell = i_from_xy(x, y);
208 i_xminus = i_cell - 1;
209 i_yplus = i_cell + n_x;
210 i_yminus = x + (n_y-1)*n_x;
211 connect_cells(i_cell, i_xminus);
212 connect_cells(i_cell, i_yplus);
213 if (is_periodic_x_edge) { connect_cells(i_cell, i_yminus); }
214 if (is_periodic_y_edge) { connect_cells(i_cell, i_xplus); }
219 i_cell = i_from_xy(x, y);
221 i_xminus = i_cell - 1;
223 i_yminus = i_cell - n_x;
224 connect_cells(i_cell, i_xminus);
225 connect_cells(i_cell, i_yminus);
226 if (is_periodic_x_edge) { connect_cells(i_cell, i_yplus); }
227 if (is_periodic_y_edge) { connect_cells(i_cell, i_xplus); }
231 auto wire_edge_cells = [&](
gt_vec_t grid_topologies)
233 auto is_periodic_x_edge = (grid_topologies[0]==GridTopology::PERIODIC);
234 auto is_periodic_y_edge = (grid_topologies[1]==GridTopology::PERIODIC);
235 wire_x_edges(is_periodic_x_edge);
236 wire_y_edges(is_periodic_y_edge);
237 wire_corners(is_periodic_x_edge, is_periodic_y_edge);
243 wire_central_cells();
246 wire_edge_cells(p.grid_topologies);