-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChannelAnalyzer.cpp
287 lines (230 loc) · 7.61 KB
/
ChannelAnalyzer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#include "ChannelAnalyzer.hpp"
//#include <set>
#include <unordered_set>
#include <stack>
#include <vector>
#include <array>
#include <limits>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstdint>
// Hash function for unordered_set
namespace std
{
template<typename T, size_t N>
struct hash< array<T, N> >
{
typedef array<T, N> argument_type;
typedef size_t result_type;
result_type operator()(const argument_type& a) const
{
hash<T> hasher;
result_type h = 0;
for (result_type i = 0; i < N; ++i)
{
h = h * 31 + hasher(a[i]);
}
return h;
}
};
}
ChannelAnalyzer::ChannelAnalyzer(EnergyGrid& energyGrid, GReal maxE, GReal dr) :
mEnergyGrid {energyGrid}
{
this->analyze(maxE, dr);
}
int
ChannelAnalyzer::getChannelDimension()
{
return mChannelDimension;
}
void
ChannelAnalyzer::analyze(GReal maxE, GReal dr)
{
using namespace std;
// Ref energy grid.
// Will be changed to copy if needed.
EnergyGrid& eGrid = mEnergyGrid;
//bool isVerbose = true;
bool isVerbose = false;
mChannelDimension = 0;
GIndex invalid = {GIndex {0} - 1};
// 0: yz plane
// 1: zx plane
// 2: xy palne
// Get minimum rectangle cell.
Cell box = eGrid.getContainingBox();
Cell invBox = inverse(box);
vector<GIndex> maxNs {static_cast<GIndex>(ceil(box.a[0] / dr)),
static_cast<GIndex>(ceil(box.b[1] / dr)),
static_cast<GIndex>(ceil(box.c[2] / dr))};
vector<GIndex3> minIndices = eGrid.getLocalMinimumIndices(maxE);
auto pbc = [maxNs, invalid](GIndex3& idx)
{
for (int i = 0; i < 3; ++i)
{
if (idx[i] == maxNs[i])
idx[i] = 0;
if (idx[i] == invalid)
idx[i] = maxNs[i] - 1;
}
};
auto idx2pos = [maxNs, box](const GIndex3& idx)
{
Vector s {static_cast<GReal>(idx[0]) / maxNs[0],
static_cast<GReal>(idx[1]) / maxNs[1],
static_cast<GReal>(idx[2]) / maxNs[2]};
return box * s;
};
// Reduce start points
auto dist = [this](const GIndex3& n1, const GIndex3& n2)
{
Cell cell = mEnergyGrid.getCell();
GReal maxNx = mEnergyGrid.getMaxNx();
GReal maxNy = mEnergyGrid.getMaxNy();
GReal maxNz = mEnergyGrid.getMaxNz();
Vector s1 {static_cast<GReal>(n1[0]) / maxNx,
static_cast<GReal>(n1[1]) / maxNy,
static_cast<GReal>(n1[2]) / maxNz};
Vector s2 {static_cast<GReal>(n2[0]) / maxNx,
static_cast<GReal>(n2[1]) / maxNy,
static_cast<GReal>(n2[2]) / maxNz};
Vector s = s2 - s1;
for (auto& si : s)
{
if (si < -0.5)
si += 1.0;
if (si > 0.5)
si -= 1.0;
}
return norm(cell * s);
};
if (minIndices.size() >= 2)
{
unordered_set<GIndex> nearList;
for (GIndex i = 0; i < minIndices.size() - 1; ++i)
for (GIndex j = i + 1; j < minIndices.size(); ++j)
{
if (dist(minIndices[i], minIndices[j]) < 2.0)
nearList.insert(j);
}
vector<GIndex3> dummy;
for (GIndex i = 0; i < minIndices.size(); ++i)
if (nearList.count(i) == 0)
dummy.push_back(minIndices[i]);
minIndices = dummy;
}
else
{
minIndices.resize(1);
minIndices[0] = mEnergyGrid.getMinimumEnergyIndex3();
}
if (isVerbose)
cout << "# of startring point = " << minIndices.size() << endl;
auto tri2rec = [this, maxNs, invBox](const GIndex3& tri)
{
GReal tiny = numeric_limits<GReal>::epsilon();
Cell cell = mEnergyGrid.getCell();
GReal maxNx = mEnergyGrid.getMaxNx();
GReal maxNy = mEnergyGrid.getMaxNy();
GReal maxNz = mEnergyGrid.getMaxNz();
Vector s {static_cast<GReal>(tri[0]) / maxNx,
static_cast<GReal>(tri[1]) / maxNy,
static_cast<GReal>(tri[2]) / maxNz};
Vector r = cell * s;
s = invBox * r;
for (auto& si : s)
{
if (si < 0.0)
si += 1.0;
if (si > 1.0)
si -= 1.0;
}
GIndex rec0 = floor(s[0] * maxNs[0] + tiny);
GIndex rec1 = floor(s[1] * maxNs[1] + tiny);
GIndex rec2 = floor(s[2] * maxNs[2] + tiny);
GIndex3 rec = {rec0, rec1, rec2};
return rec;
};
vector<string> directionStr =
{"X Direction", "Y Direction", "Z Direction"};
for (GIndex direction : {0, 1, 2})
{
if (isVerbose)
cout << directionStr[direction] << " Searching start" << endl;
for (const auto& minIndex : minIndices)
{
GIndex3 start = tri2rec(minIndex);
GIndex3 point = start;
int64_t counter = 0;
unordered_set<GIndex3> visited {point};
stack<GIndex3> possibleWays;
stack<int64_t> counterStack;
bool channelFound = false;
while (not channelFound)
{
vector<GIndex3> trials (6, point);
// Impose directional priority
// Up direction 5 = first (stack)
trials[5][direction]++; pbc(trials[5]);
trials[0][direction]--; pbc(trials[0]);
GIndex idx = 1;
for (GIndex i = 0; i < 3; ++i)
{
if (i == direction)
continue;
trials[idx][i]++; pbc(trials[idx]); idx++;
trials[idx][i]--; pbc(trials[idx]); idx++;
}
for (GIndex i = 0; i < 6; ++i)
{
auto& trial = trials[i];
if (visited.count(trial) == 0)
if (eGrid.interpolate(idx2pos(trial)) < maxE)
{
possibleWays.push(trial);
if (i == 0)
counterStack.push(counter - 1);
else if (i == 5)
counterStack.push(counter + 1);
else
counterStack.push(counter);
}
}
if (possibleWays.empty())
{
if (isVerbose)
cout << "Nowhere to go" << endl;
break;
}
else
{
point = possibleWays.top();
possibleWays.pop();
visited.insert(point);
counter = counterStack.top();
counterStack.pop();
}
if (isVerbose)
cout << "Current position = " << idx2pos(point) <<
setw(20) << "Index = " <<
setw(6) << point[0] <<
setw(6) << point[1] <<
setw(6) << point[2] <<
setw(20) << "Counter = " << counter <<
endl;
if (static_cast<GIndex>(counter) == maxNs[direction])
{
if (isVerbose)
cout << directionStr[direction] << " Found" << endl;
channelFound = true;
mChannelDimension++;
break;
}
}
if (channelFound)
break;
} // minIndex loop
} // direction
}