Langevin C++ source

Loading...
Searching...
No Matches
langevin_construct_grid2d.cpp
Go to the documentation of this file.
1
5
6#include "langevin_types.hpp"
7#include "langevin_base.hpp"
8
26 bool BaseLangevin::construct_2D_grid(const Parameters p)
27{
28 // Shorthand
29 const auto n_x = p.n_x;
30 const auto n_y = p.n_y;
31
32 // Flattened grid vector each with a vector of connection elements.
33 // Each connection element will link to between 2 and 4 neighbor locations.
34 // At central grid cell, there will be 4 connections.
35 // Along periodic edges there will be 3 connection elements.
36 // Along bounded edges there will be 2 connection elements.
37 // At corners these sets will be reduced to 2-3 elements.
38 grid_wiring = grid_wiring_t(n_x*n_y, neighborhood_t(0));
39
40 // Compute flattened grid vector index from coordinate
41 auto i_from_xy = [&](int x, int y) -> int { return x + y*n_x; };
42
43 // Connect two neighbor cells
44 auto connect_cells = [&](int i, int j){ grid_wiring.at(i).push_back(j); };
45
46 // Central grid cells
47 auto wire_central_cell = [&](int x, int y)
48 {
49 // i_cell is the index of the flattened grid
50 auto i_cell = i_from_xy(x, y);
51 // Each cell has 4 neighbors
52 connect_cells(i_cell, i_cell+n_x); // Up
53 connect_cells(i_cell, i_cell-n_x); // Down
54 connect_cells(i_cell, i_cell+1); // Right
55 connect_cells(i_cell, i_cell-1); // Left
56
57 };
58 auto wire_central_cells = [&]()
59 {
60 for (auto y=1; y<n_y-1; y++)
61 {
62 for (auto x=1; x<n_x-1; x++)
63 {
64 wire_central_cell(x,y);
65 }
66 }
67 };
68
69 // Periodic
70 auto wire_periodic_edge_cell_yplus = [&](int x, int y)
71 {
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); // Up
75 };
76 auto wire_periodic_edge_cell_yminus = [&](int x, int y)
77 {
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); // Down
81 };
82 auto wire_periodic_edge_cell_xplus = [&](int x, int y)
83 {
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); // Right (VMB: left)
87 };
88 auto wire_periodic_edge_cell_xminus = [&](int x, int y)
89 {
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); // Left (VMB: right)
93 };
94 auto wire_periodic_edge_cell = [&](int x, int y)
95 {
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);
100 };
101 auto wire_periodic_y_edges = [&](int x)
102 {
103 assert(x==0 or x==n_x-1);
104 for (auto y=1; y<n_y-1; y++)
105 {
106 wire_periodic_edge_cell(x, y);
107 }
108 };
109 auto wire_periodic_x_edges = [&](int y)
110 {
111 assert(y==0 or y==n_y-1);
112 for (auto x=1; x<n_x-1; x++)
113 {
114 wire_periodic_edge_cell(x, y);
115 }
116 };
117
118 // Bounded
119 auto wire_bounded_y_edges = [&](int x)
120 {
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++)
124 {
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);
129 }
130 };
131 auto wire_bounded_x_edges = [&](int y)
132 {
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++)
136 {
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);
141 }
142 };
143
144 // Any topology
145 auto wire_x_edges = [&](bool is_periodic_x_edge)
146 {
147 if (is_periodic_x_edge)
148 {
149 wire_periodic_x_edges(0); // Bottom row
150 wire_periodic_x_edges(n_y-1); // Top row
151 }
152 else
153 {
154 wire_bounded_x_edges(0); // Bottom row
155 wire_bounded_x_edges(n_y-1); // Top row
156 }
157 };
158 auto wire_y_edges = [&](bool is_periodic_y_edge)
159 {
160 if (is_periodic_y_edge)
161 {
162 wire_periodic_y_edges(0); // Left column
163 wire_periodic_y_edges(n_x-1); // Right column
164 }
165 else
166 {
167 wire_bounded_y_edges(0); // Left column
168 wire_bounded_y_edges(n_x-1); // Right column
169 }
170 };
171
172 // Corners
173 auto wire_corners = [&](bool is_periodic_x_edge, bool is_periodic_y_edge)
174 {
175 int x, y, i_cell, i_yminus, i_xminus, i_yplus, i_xplus;
176
177 // Bottom-left corner
178 x = 0;
179 y = 0;
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); }
189
190 // Top-left corner
191 x = 0;
192 y = n_y-1;
193 i_cell = i_from_xy(x, y);
194 i_xplus = i_cell + 1;
195 i_xminus = n_x-1 + y*n_x;
196 i_yplus = 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); }
202
203 // Bottom-right corner
204 x = n_x-1;
205 y = 0;
206 i_cell = i_from_xy(x, y);
207 i_xplus = 0 + y*n_x;
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); }
215
216 // Top-right corner
217 x = n_x-1;
218 y = n_y-1;
219 i_cell = i_from_xy(x, y);
220 i_xplus = 0 + y*n_x;
221 i_xminus = i_cell - 1;
222 i_yplus = x;
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); }
228 };
229
230 // Whole grid
231 auto wire_edge_cells = [&](gt_vec_t grid_topologies)
232 {
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);
238 };
239
241
242 // Step 1: Wire all the non-edge grid cells
243 wire_central_cells();
244
245 // Step 2: Wire grid edge cells according to topology specs
246 wire_edge_cells(p.grid_topologies);
247
249
250 return true;
251}
BaseLangevin::construct_2D_grid
bool construct_2D_grid(const Parameters parameters)
Build 2d Langevin density field grid & mixed topology.
Definition langevin_construct_grid2d.cpp:26
BaseLangevin::grid_wiring
grid_wiring_t grid_wiring
Neighorhood topology for all grid cells.
Definition langevin_base.hpp:23
langevin_base.hpp
Base class for Langevin equation integrator.
langevin_types.hpp
Type definitions for BaseLangevin integrator.
neighborhood_t
std::vector< int > neighborhood_t
Type for density grid wiring.
Definition langevin_types.hpp:28
gt_vec_t
std::vector< GridTopology > gt_vec_t
Type for specifying grid topology in each direction x, y, z...
Definition langevin_types.hpp:45
grid_wiring_t
std::vector< neighborhood_t > grid_wiring_t
Type for grid-cell neighborhood connections.
Definition langevin_types.hpp:30
Parameters
Container for BaseLangevin integrator parameters.
Definition langevin_parameters.hpp:21