-
Notifications
You must be signed in to change notification settings - Fork 0
/
nwnet.h
463 lines (400 loc) · 15.8 KB
/
nwnet.h
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/*
Random Nanowire Network Model Library
nwnet.h
Jeremy Smith
EECS, University of California Berkeley
Version 1.1
*/
#ifndef NWNET_H
#define NWNET_H
#include <iostream>
#include <mutex>
#include <string>
#include <vector>
#include <list>
#include <random>
#include <thread>
#include <Eigen/Dense>
using std::cout;
using std::endl;
using Eigen::Vector2d;
using Eigen::VectorXd;
using Eigen::ArrayXd;
using Eigen::ArrayXi;
using Eigen::ArrayXXd;
using Eigen::ArrayXXi;
using Eigen::MatrixXd;
typedef std::pair<int, double> enumerate;
std::mutex m;
// Comparison function for argsort
bool argsort_comp(const enumerate& left, const enumerate& right){
return left.second < right.second;
}
// Argsort function that returns index of the sorted Eigen array
template<typename Derived>
ArrayXi argsort(Eigen::ArrayBase<Derived>& array){
ArrayXi indices(array.size());
std::vector<enumerate> enum_array(array.size());
for(int i = 0; i < array.size(); i++){
enum_array[i].first = i;
enum_array[i].second = array(i);
}
std::sort(enum_array.begin(), enum_array.end(), argsort_comp);
for(int i = 0; i < array.size(); i++){
indices(i) = enum_array[i].first;
}
return indices;
}
// Function that checks for the intersection of two line segments given four coordinates
void intersectCheck(Vector2d& start1, Vector2d& end1, Vector2d& start2, Vector2d& end2, Vector2d& output, bool& intersect){
using std::min;
using std::max;
double pden = (start1(0) - end1(0))*(start2(1) - end2(1)) - (start1(1) - end1(1))*(start2(0) - end2(0));
double px = ((start1(0)*end1(1) - start1(1)*end1(0))*(start2(0) - end2(0)) - (start1(0) - end1(0))*(start2(0)*end2(1) - start2(1)*end2(0)))/pden;
double py = ((start1(0)*end1(1) - start1(1)*end1(0))*(start2(1) - end2(1)) - (start1(1) - end1(1))*(start2(0)*end2(1) - start2(1)*end2(0)))/pden;
intersect = ((px >= min(start1(0), end1(0))) & (px <= max(start1(0), end1(0)))) &
((px >= min(start2(0), end2(0))) & (px <= max(start2(0), end2(0)))) &
((py >= min(start1(1), end1(1))) & (py <= max(start1(1), end1(1)))) &
((py >= min(start2(1), end2(1))) & (py <= max(start2(1), end2(1))));
output << px, py;
}
// Calculates the resistance between 2 points using matrix solution
double two_point_resistance(VectorXd& val, MatrixXd& vec, int node1, int node2){
double r12 = 0;
int totalnodes = val.rows();
for(int i = 0; i < totalnodes; i++){
if(val(i) <= 0){
continue;
}else{
r12 += (1.0/val(i))*std::pow((vec(node1,i) - vec(node2,i)), 2);
}
}
return r12;
}
// Find the closest node to a particular (x,y) coordinate
int findnode(ArrayXXd& nodeslist, Vector2d& xycoord){
int totalnodes = nodeslist.rows();
ArrayXd distances(totalnodes);
ArrayXd::Index index;
for(int i = 0; i < totalnodes; i++){
double dx = nodeslist(i,0) - xycoord(0);
double dy = nodeslist(i,1) - xycoord(1);
distances(i) = std::sqrt(std::abs(dx*dx + dy*dy));
}
distances.minCoeff(&index);
return index;
}
// Function to generate random angle in range [0,2pi]
ArrayXd create_random_angles(int numwires, float& sparam, unsigned seed){
std::default_random_engine generator(seed);
std::uniform_real_distribution<double> distribution(0.0, 2*M_PI);
ArrayXd d(numwires);
for(int i = 0; i < numwires; i++){
d(i) = distribution(generator);
}
sparam = (2*(d.cos().square()) - 1).sum()/numwires;
return d;
}
// Function to generate random (x,y) positions
ArrayXXd create_random_positions(int numwires, double sampleDimension, unsigned seed){
std::default_random_engine generator(seed);
std::uniform_real_distribution<double> distribution(0.0, sampleDimension);
ArrayXXd d(numwires, 2);
for(int i = 0; i < numwires; i++){
d(i,0) = distribution(generator);
d(i,1) = distribution(generator);
}
return d;
}
// Function to generate random nanowire lengths using normal distribution
ArrayXd create_random_lengths(int numwires, double lav, double lstd, unsigned seed){
std::default_random_engine generator(seed);
std::normal_distribution<double> distribution(lav, lstd);
ArrayXd d(numwires);
for(int i = 0; i < numwires; i++){
d(i) = std::abs(distribution(generator));
}
return d;
}
// Class definition of nanowire network array object
class WireNet{
public:
WireNet(int, double, double, double, double, double, double, unsigned, bool);
std::vector<std::string> parameterList;
double areal_coverage(double);
void parameters();
void indexwires();
void intersections();
void solve();
void output_files(std::string);
VectorXd eigenvalues;
MatrixXd eigenvectors;
MatrixXd laplacian;
MatrixXd cmatrix;
ArrayXXd nodeCoords;
ArrayXXi nodeWireIndices;
int number_of_nodes;
bool intersections_calculated;
bool solve_calculated;
private:
void conductance_matrix();
int numwires;
double lav;
double lstd;
double sampleDimension;
double wireRes;
double intersectRes;
double sheetRes;
double wireDensity;
float sparam;
unsigned seed;
ArrayXd wireLengths;
ArrayXXd startCoords;
ArrayXXd endCoords;
ArrayXd wireAngles;
ArrayXXd allxCoords;
};
// Class constructor
WireNet::WireNet(int n, double l, double st, double d, double wr, double ir, double rsh, unsigned s, bool debug = false){
numwires = n;
lav = l;
lstd = st;
sampleDimension = d;
wireRes = wr;
intersectRes = ir;
sheetRes = rsh;
wireDensity = numwires/(sampleDimension*sampleDimension);
if(debug == true){
seed = 293423;
}else{
seed = s;
}
wireLengths = create_random_lengths(numwires, lav, lstd, seed);
startCoords = create_random_positions(numwires, sampleDimension, seed);
wireAngles = create_random_angles(numwires, sparam, seed);
endCoords.resize(numwires, 2);
endCoords << wireLengths*wireAngles.sin(), wireLengths*wireAngles.cos();
endCoords = endCoords + startCoords;
allxCoords.resize(numwires, 2);
allxCoords << startCoords.col(0), endCoords.col(0);
nodeCoords.resize(1e7*wireDensity*wireDensity + 2*numwires, 2); // Initial node arrays are larger than the number of wires to make sure there is enough space
nodeWireIndices.resize(1e7*wireDensity*wireDensity + 2*numwires, 2);
intersections_calculated = false;
solve_calculated = false;
using std::to_string;
parameterList.push_back("Number of wires: " + to_string(numwires));
parameterList.push_back("Wire lengths: " + to_string(lav) + " with standard deviation: " + to_string(lstd));
parameterList.push_back("Sample size: " + to_string(sampleDimension));
parameterList.push_back("Wire areal density: " + to_string(wireDensity));
parameterList.push_back("Calculated S-parameter: " + to_string(sparam));
parameterList.push_back("Wire resistance per length: " + to_string(wireRes));
parameterList.push_back("Wire interconnect resistance: " + to_string(intersectRes));
parameterList.push_back("Matrix sheet resistance: " + to_string(sheetRes));
parameterList.push_back("Random seed: " + to_string(seed));
}
// Calculates areal coverage of nanowires
double WireNet::areal_coverage(double wirediameter){
return (wireLengths*wirediameter).sum()/(sampleDimension*sampleDimension);
}
// Displays the parameters for the WireNet
void WireNet::parameters(){
m.lock();
for(int i = 0; i < parameterList.size(); i++){
cout << parameterList[i] << endl;
}
m.unlock();
}
// Lists all wires sorted by their wire index
void WireNet::indexwires(){
for(int i = 0; i < numwires; i++){
cout << "[" << i << "] "
<< startCoords(i,0) << ", " << startCoords(i,1) << " "
<< endCoords(i,0) << ", " << endCoords(i,1) << " L = "
<< wireLengths(i) << endl;
}
}
// Calculates all nodes (intersections and wire end points). Returns coordinates, wire indices and node count.
void WireNet::intersections(){
if(intersections_calculated == true){
m.lock();
cout << "Intersections already calculated!";
m.unlock();
return;
}
ArrayXd xs_st = allxCoords.rowwise().minCoeff(); // Lowest x coordinate of wire i.e. start of wire
ArrayXd xs_en = allxCoords.rowwise().maxCoeff(); // Highest x coordinate of wire i.e. end of wire
ArrayXd xs_all(2*numwires); // Combined list of all x coordinates (first half of list is start coordinates, second is end coordinates)
xs_all << xs_st, xs_en;
ArrayXi i_xs_sort = argsort(xs_all);
std::list<int> searchList; // Search list for temp storage of x indices
m.lock();
cout << "[" << std::this_thread::get_id() << "] Finding intersections...\n";
m.unlock();
int node_count = 0;
for(int j = 0; j < 2*numwires; j++){
int i_wireA = i_xs_sort(j);
if(i_wireA < numwires){
// First include wire start point as additional node
nodeCoords.row(node_count) = startCoords.row(i_wireA);
nodeWireIndices(node_count, 0) = i_wireA;
nodeWireIndices(node_count, 1) = i_wireA;
node_count++;
// Starts of wires
if(searchList.empty()){
searchList.push_back(i_wireA); // Adds current wire index to searchList if empty and moves to next wire
continue;
}
for(std::list<int>::iterator i = searchList.begin(); i != searchList.end(); ++i){
int i_wireB = *i;
bool intersect = false;
Vector2d st_A = startCoords.row(i_wireA);
Vector2d en_A = endCoords.row(i_wireA);
Vector2d st_B = startCoords.row(i_wireB);
Vector2d en_B = endCoords.row(i_wireB);
Vector2d intersectxy;
intersectCheck(st_A, en_A, st_B, en_B, intersectxy, intersect); // Checks for intersection between wireA and wireB
if(intersect == true){
nodeCoords.row(node_count) = intersectxy;
nodeWireIndices(node_count, 0) = i_wireA;
nodeWireIndices(node_count, 1) = i_wireB;
node_count++;
}
}
searchList.push_back(i_wireA); // Adds current wire index to searchList
}else{
// First include wire end point as additional node
nodeCoords.row(node_count) = endCoords.row(i_wireA - numwires);
nodeWireIndices(node_count, 0) = i_wireA - numwires;
nodeWireIndices(node_count, 1) = i_wireA - numwires;
node_count++;
// Ends of wires
searchList.remove(i_wireA - numwires); // Removes current wire index from searchList
}
}
nodeCoords.conservativeResize(node_count, Eigen::NoChange);
nodeWireIndices.conservativeResize(node_count, Eigen::NoChange);
intersections_calculated = true;
m.lock();
cout << "[" << std::this_thread::get_id()
<< "] There are " << node_count
<< " nodes from " << numwires
<< " wires of which " << node_count - 2*numwires
<< " are intersections " << endl;
m.unlock();
number_of_nodes = node_count;
}
// Calculates the adjacency conductance matrix
void WireNet::conductance_matrix(){
if(intersections_calculated == false){
intersections();
}
m.lock();
cout << "[" << std::this_thread::get_id() << "] Calculating conductance matrix..." << endl;
m.unlock();
cmatrix = MatrixXd::Ones(number_of_nodes, number_of_nodes) - MatrixXd::Identity(number_of_nodes, number_of_nodes);
cmatrix *= 1.0/(sheetRes*0.5*number_of_nodes);
for(int w = 0; w < numwires; w++){
ArrayXi i_inter(100); // Array of node indices that are on wire w
ArrayXXd xy_inter(100, 2); // Array of x,y positions of nodes on wire w
int online_count = 0;
// Finds nodes for wire w
for(int i = 0; i < number_of_nodes; i++){
if((nodeWireIndices(i, 0) == w) || (nodeWireIndices(i, 1) == w)){
i_inter(online_count) = i;
xy_inter.row(online_count) = nodeCoords.row(i);
online_count++;
}
}
i_inter.conservativeResize(online_count);
xy_inter.conservativeResize(online_count, Eigen::NoChange);
ArrayXd x = xy_inter.col(0);
ArrayXi xy_inter_sort = argsort(x); // Sort nodes by x-coordinate
for(int k = 0; k < i_inter.size() - 1; k++){
double dx = xy_inter(xy_inter_sort(k), 0) - xy_inter(xy_inter_sort(k+1), 0);
double dy = xy_inter(xy_inter_sort(k), 1) - xy_inter(xy_inter_sort(k+1), 1);
double intLength = std::sqrt(std::abs(dx*dx + dy*dy)); // Distance between adjacent nodes (k and k+1)
if(intLength == 0){
continue;
}
double c = 1.0/(intLength*wireRes + intersectRes); // Conductance between node k and k+1
// Update conductance matrix (Hermitian i.e. c_ij=c_ji)
cmatrix(i_inter(xy_inter_sort(k)), i_inter(xy_inter_sort(k+1))) = c;
cmatrix(i_inter(xy_inter_sort(k+1)), i_inter(xy_inter_sort(k))) = c;
}
}
}
// Solves the resistor network
void WireNet::solve(){
if(solve_calculated == true){
m.lock();
cout << "Already solved!" << endl;
m.unlock();
return;
}
conductance_matrix();
VectorXd c_i = cmatrix.rowwise().sum();
laplacian = c_i.asDiagonal();
laplacian -= cmatrix;
m.lock();
cout << "[" << std::this_thread::get_id() << "] Solving..." << endl;
m.unlock();
Eigen::SelfAdjointEigenSolver<MatrixXd> eigensolver(laplacian);
if(eigensolver.info() != Eigen::Success) abort();
m.lock();
cout << "[" << std::this_thread::get_id() << "] Saving eigenvalues and eigenvctors..." << endl;
m.unlock();
eigenvalues = eigensolver.eigenvalues();
eigenvectors = eigensolver.eigenvectors();
m.lock();
cout << "[" << std::this_thread::get_id() << "] Done" << endl;
m.unlock();
solve_calculated = true;
}
// Output files for resistor network
void WireNet::output_files(std::string filename){
if(solve_calculated == false){
m.lock();
cout << "Network nor solved!" << endl;
m.unlock();
return;
}
m.lock();
cout << "[" << filename << "] Writing files out..." << endl;
std::ofstream outfile;
outfile.open(filename + "_eigvals.dat");
for(int i = 0; i < parameterList.size(); i++){
outfile << parameterList[i] << '\n';
}
outfile << "EIGENVALUES\n";
for(int i = 0; i < eigenvalues.size(); i++){
outfile << eigenvalues(i) << '\n';
}
outfile.close();
outfile.open(filename + "_eigvecs.dat");
for(int i = 0; i < parameterList.size(); i++){
outfile << parameterList[i] << '\n';
}
outfile << "EIGENVECTORS\n";
for(int i = 0; i < eigenvectors.rows(); i++){
for(int j = 0; j < eigenvectors.cols(); j++){
outfile << eigenvectors(i,j) << '\t';
}
outfile << '\n';
}
outfile.close();
outfile.open(filename + "_cmatrix.dat");
for(int i = 0; i < parameterList.size(); i++){
outfile << parameterList[i] << '\n';
}
outfile << "CONDUCTANCE MATRIX\n";
for(int i = 0; i < cmatrix.rows(); i++){
for(int j = 0; j < cmatrix.cols(); j++){
outfile << cmatrix(i,j) << '\t';
}
outfile << '\n';
}
outfile.close();
m.unlock();
}
#endif