-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathPoissonSolver.cpp
337 lines (299 loc) · 12 KB
/
PoissonSolver.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
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
// @author Wushi Dong
// PoissonSolver.cpp
#include "PoissonSolver.h"
PoissonSolver::~PoissonSolver(){}
void PoissonSolver::Run(PotentialProfile potential_profile, const ChargeProfile
charge_profile, const FixedChargeProfile fixed_charge_profile, bool
&rIsInnerConv)
{
// Initialization
double *pot = potential_profile.GetPotential();
double *charge = charge_profile.GetCharge();
double *charge_fixed = fixed_charge_profile.GetCharge();
int n_grid = charge_profile.GetNumGrid();
int n_grid_principal_layer = 6;
// Potential grid spacing
double DELTA_X_Gr = mLatticeConstantGraphene / sqrt(3.0) / 2.0;
double DELTA_X_MoS2 = mLatticeConstantMoS2 / sqrt(3.0) / 2.0;
// Jacobian
mat J(n_grid, n_grid);
vec F(n_grid);
// Potential changes
vec det_pot(n_grid);
// Local judge for possion solver convergence
int judge = 0;
// Count of iteration runs
int count = 0;
// double sign;
// Records initial potential
double pot_initial[n_grid];
for(int i = 0; i < n_grid; ++i)
pot_initial[i] = pot[i];
// if(mRank == 0)
// {
// cout << "Charge distribution (A^-1):" << endl;
// for(int i = 0; i < n_grid; ++i)
// {
// cout << charge_fixed[i] - charge[i] << " ";
// }
// cout << endl;
// }
while(judge == 0)
{
judge = 1;
count++;
if(count > mPoissonRunMax)
break;
J.zeros();
F.zeros();
det_pot.zeros();
if(mRank == 0)
{
cout << endl << "Inner cycle number "<< count << ":" << endl;
}
// if(mRank == 0)
// {
// cout << "Updated charge prediction:" << endl;
// for(int i = 0; i < n_grid; ++i)
// cout << i << '\t' << charge[i] * exp((pot[i] - pot_initial[i])) <<
// endl;;
// cout << endl;
// }
// Updates F (Neumman boundary condition) Graphene
F(0) = DELTA_X_Gr * DIL_Graphene * pot[1] - 2.0 * DELTA_X_Gr * DIL_Graphene
* pot[0] + DELTA_X_Gr * DIL_Graphene * pot[1] - DELTA_X_Gr * DELTA_X_Gr
* (charge[0] * exp((pot[0] - pot_initial[0]) / mKT) - charge_fixed[0]);
for(int i = 1; i < mNumPrincipalLayerGraphene * n_grid_principal_layer - 2;
++i) { F(i) = DELTA_X_Gr * DIL_Graphene * pot[i - 1] - 2.0 * DELTA_X_Gr
* DIL_Graphene * pot[i] + DELTA_X_Gr * DIL_Graphene * pot[i + 1]
- DELTA_X_Gr * DELTA_X_Gr * (charge[i] * exp((pot[i] - pot_initial[i])
/ mKT) - charge_fixed[i]); }
// The last graphene carbon slice
F(mNumPrincipalLayerGraphene * n_grid_principal_layer - 2) = 0.5
* mDistanceGrS2 * DIL_Graphene * pot[mNumPrincipalLayerGraphene
* n_grid_principal_layer - 3] - (0.5 * mDistanceGrS2 * DIL_Graphene
+ DELTA_X_Gr * DIL_Graphene) * pot[mNumPrincipalLayerGraphene
* n_grid_principal_layer - 2] + DELTA_X_Gr * DIL_Graphene
* pot[mNumPrincipalLayerGraphene * n_grid_principal_layer - 1]
- DELTA_X_Gr * 0.5 * mDistanceGrS2 * (charge[mNumPrincipalLayerGraphene
* n_grid_principal_layer - 2] * exp((pot[mNumPrincipalLayerGraphene
* n_grid_principal_layer - 2]
- pot_initial[mNumPrincipalLayerGraphene * n_grid_principal_layer
- 2]) / mKT) - charge_fixed[mNumPrincipalLayerGraphene
* n_grid_principal_layer - 2]);
// Gr - MoS2 interface
F(mNumPrincipalLayerGraphene * n_grid_principal_layer - 1) = 0.5
* mDistanceGrS2 * DIL_Graphene * pot[mNumPrincipalLayerGraphene
* n_grid_principal_layer - 2] - (0.5 * mDistanceGrS2 * DIL_Graphene + 0.5
* mDistanceGrS2 * DIL_MoS2) * pot[mNumPrincipalLayerGraphene
* n_grid_principal_layer - 1] + 0.5 * mDistanceGrS2 * DIL_MoS2
* pot[mNumPrincipalLayerGraphene * n_grid_principal_layer];
// S2
F(mNumPrincipalLayerGraphene * n_grid_principal_layer) = DELTA_X_MoS2
* DIL_MoS2 * pot[mNumPrincipalLayerGraphene * n_grid_principal_layer - 1]
- (DELTA_X_MoS2 * DIL_MoS2 + 0.5 * mDistanceGrS2 * DIL_MoS2)
* pot[mNumPrincipalLayerGraphene * n_grid_principal_layer] + 0.5
* mDistanceGrS2 * DIL_MoS2 * pot[mNumPrincipalLayerGraphene
* n_grid_principal_layer + 1] - 0.5 * mDistanceGrS2 * DELTA_X_MoS2
* (charge[mNumPrincipalLayerGraphene * n_grid_principal_layer]
* exp((pot[mNumPrincipalLayerGraphene * n_grid_principal_layer]
- pot_initial[mNumPrincipalLayerGraphene
* n_grid_principal_layer]) / mKT)
- charge_fixed[mNumPrincipalLayerGraphene
* n_grid_principal_layer]);
// MoS2
for(int i = mNumPrincipalLayerGraphene * n_grid_principal_layer + 1;
i < n_grid - 1; ++i) { F(i) = DELTA_X_MoS2 * DIL_MoS2 * pot[i - 1] - 2.0
* DELTA_X_MoS2 * DIL_MoS2 * pot[i] + DELTA_X_MoS2 * DIL_MoS2 * pot[i + 1]
- DELTA_X_MoS2 * DELTA_X_MoS2 * (charge[i] * exp((pot[i]
- pot_initial[i]) / mKT) - charge_fixed[i]); } F(n_grid - 1)
= DELTA_X_MoS2 * DIL_MoS2 * pot[n_grid - 2] - 2.0 * DELTA_X_MoS2
* DIL_MoS2 * pot[n_grid - 1] + DELTA_X_MoS2 * DIL_MoS2 * pot[n_grid - 2]
- DELTA_X_MoS2 * DELTA_X_MoS2 * (charge[n_grid - 1] * exp((pot[n_grid
- 1] - pot_initial[n_grid - 1]) / mKT) - charge_fixed[n_grid
- 1]);
// if(mRank == 0)
// {
// cout << "F:" << endl;
// for(int i = 0; i < n_grid; ++i)
// cout << i << '\t' << F(i) << endl;
// cout << endl;
// }
// Updates Jacobian
// Graphene
J(0, 0) = -2.0 * DELTA_X_Gr * DIL_Graphene;
J(0, 1) = DELTA_X_Gr * DIL_Graphene + DELTA_X_Gr * DIL_Graphene;
for(int i = 1; i < mNumPrincipalLayerGraphene * n_grid_principal_layer - 2;
++i)
{
J(i, i) = -2.0 * DELTA_X_Gr * DIL_Graphene - DELTA_X_Gr * DELTA_X_Gr
* charge[i - 1] * exp((pot[i] - pot_initial[i]) / mKT) / mKT;
J(i, i - 1) = DELTA_X_Gr * DIL_Graphene;
J(i, i + 1) = DELTA_X_Gr * DIL_Graphene;
}
// The last graphene carbon slice
J(mNumPrincipalLayerGraphene * n_grid_principal_layer - 2,
mNumPrincipalLayerGraphene * n_grid_principal_layer - 2) = -(0.5
* mDistanceGrS2 * DIL_Graphene + DELTA_X_Gr * DIL_Graphene) - DELTA_X_Gr
* 0.5 * mDistanceGrS2 * charge[mNumPrincipalLayerGraphene
* n_grid_principal_layer - 2] * exp((pot[mNumPrincipalLayerGraphene
* n_grid_principal_layer - 2]
- pot_initial[mNumPrincipalLayerGraphene
* n_grid_principal_layer - 2]) / mKT) / mKT;
J(mNumPrincipalLayerGraphene * n_grid_principal_layer - 2,
mNumPrincipalLayerGraphene * n_grid_principal_layer - 3) = 0.5
* mDistanceGrS2 * DIL_Graphene;
J(mNumPrincipalLayerGraphene * n_grid_principal_layer - 2,
mNumPrincipalLayerGraphene * n_grid_principal_layer - 1) = DELTA_X_Gr
* DIL_Graphene;
// Gr - MoS2 interface
J(mNumPrincipalLayerGraphene * n_grid_principal_layer - 1,
mNumPrincipalLayerGraphene * n_grid_principal_layer - 1) = -(0.5
* mDistanceGrS2 * DIL_Graphene + 0.5 * mDistanceGrS2 * DIL_MoS2);
J(mNumPrincipalLayerGraphene * n_grid_principal_layer - 1,
mNumPrincipalLayerGraphene * n_grid_principal_layer - 2) = 0.5
* mDistanceGrS2 * DIL_Graphene;
J(mNumPrincipalLayerGraphene * n_grid_principal_layer - 1,
mNumPrincipalLayerGraphene * n_grid_principal_layer) = 0.5
* mDistanceGrS2 * DIL_MoS2;
// S2
J(mNumPrincipalLayerGraphene * n_grid_principal_layer,
mNumPrincipalLayerGraphene * n_grid_principal_layer) = -(DELTA_X_MoS2
* DIL_MoS2 + 0.5 * mDistanceGrS2 * DIL_MoS2) - 0.5 * mDistanceGrS2
* DELTA_X_MoS2 * charge[mNumPrincipalLayerGraphene
* n_grid_principal_layer] * exp((pot[mNumPrincipalLayerGraphene
* n_grid_principal_layer]
- pot_initial[mNumPrincipalLayerGraphene
* n_grid_principal_layer]) / mKT) / mKT;
J(mNumPrincipalLayerGraphene * n_grid_principal_layer,
mNumPrincipalLayerGraphene * n_grid_principal_layer - 1) = DELTA_X_MoS2
* DIL_MoS2;
J(mNumPrincipalLayerGraphene * n_grid_principal_layer,
mNumPrincipalLayerGraphene * n_grid_principal_layer + 1) = 0.5
* mDistanceGrS2 * DIL_MoS2;
// MoS2
for(int i = mNumPrincipalLayerGraphene * n_grid_principal_layer + 1;
i < n_grid - 1; ++i)
{
J(i, i) = -2.0 * DELTA_X_MoS2 * DIL_MoS2 - DELTA_X_MoS2 * DELTA_X_MoS2
* charge[i] * exp((pot[i] - pot_initial[i]) / mKT) / mKT;
J(i, i - 1) = DELTA_X_MoS2 * DIL_MoS2;
J(i, i + 1) = DELTA_X_MoS2 * DIL_MoS2;
}
J(n_grid - 1, n_grid - 1) = -2.0 * DELTA_X_MoS2 * DIL_MoS2 - DELTA_X_MoS2
* DELTA_X_MoS2 * charge[n_grid - 1] * exp((pot[n_grid - 1]
- pot_initial[n_grid - 1]) / mKT) / mKT;
J(n_grid - 1, n_grid - 2) = DELTA_X_MoS2 * DIL_MoS2 + DELTA_X_MoS2
* DIL_MoS2; /* Neumman boundary condition */
// if(mRank == 0)
// {
// cout << "J:" << endl;
// cout << 0 << '\t' << J(0, 0) << '\t' << J(0, 1) << endl;
// for(int i = 1; i < n_grid - 1; ++i)
// cout << i << '\t' << J(i, i - 1) << '\t' << J(i, i) << '\t' << J(i, i + 1) << endl;
// cout << n_grid - 1 << '\t' << J(n_grid - 1,n_grid - 2) << '\t' << J(n_grid - 1, n_grid - 1) << endl;
// cout << endl;
// }
// Gets potential change
det_pot = -J.i() * F;
// if(mRank == 0)
// {
// cout << "Potential updates:" << endl;
// for(int i = 0; i < n_grid; ++i)
// cout << i << '\t' << det_pot(i) << endl;
// cout << endl;
// }
// // Brown and Lindsay fix if necessary
// for(int i = 0; i < n_grid; ++i)
// {
// if(abs(det_pot(i)) > 1)
// {
// if(mRank == 0)
// cout << "Fixed!" << endl;
// if(det_pot(i) > 0)
// sign = 1.0;
// else
// sign = -1.0;
// if(abs(det_pot(i)) < 3.7)
// det_pot(i) = sign * pow(abs(det_pot(i)), 0.2);
// else
// det_pot(i) = sign * log(abs(det_pot(i)));
// }
// }
// Updates potential with damping
for(int i = 0; i < n_grid; ++i)
{
pot[i] += (1.0 - mPotentialDampingPoisson) * det_pot(i);
}
// if(mRank == 0)
// {
// cout << "Updated potential at current iteration (V):" << endl;
// for (int i = 0; i < n_grid; ++i)
// cout << i << "\t" << setprecision(7) << pot[i] << endl;
// }
// Judges convergence
if(mRank == 0)
{
cout << "Max det_pot = " << (abs(det_pot)).max() << endl;
}
if(abs(det_pot).max() > mPotentialConv)
judge = 0;
}
if(count > mPoissonRunMax)
{
if(mRank == 0)
{
cout << endl << "... Inner Poisson iteration NOT converged!" << endl;
}
rIsInnerConv = false;
}
else
{
if(mRank == 0)
{
cout << "... converged at inner iteration number: " << count << endl << endl;
cout << "Converged potential from Poisson solver (V):" << endl;
for (int i = 0; i < n_grid; ++i)
cout << i << "\t" << std::setprecision(7) << pot[i] << endl;
}
}
}
//// Testing
//#include "ChargeSolver.h"
//int main()
//{
// // Input parameters
// Parameters parameters;
// parameters.ParseInputFile();
// parameters.Print();
//
// DeviceEdgeContact device_edge_contact(parameters);
//
// ChargeProfile charge_profile(30);
// PotentialProfile potential_profile(30);
// FixedChargeProfile fixed_charge(parameters);
// fixed_charge.SetCharge(charge_profile.GetCharge());
// double charge[30];
// charge[30] = 0.0001;
// charge_profile.SetCharge(charge);
//
// cout << "At the beginning ..." << endl;
// potential_profile.Print();
// charge_profile.Print();
// cout << endl;
//
// cout << "Attaching the PoissonSolver ... " << endl;
// PoissonSolver poisson_solver(parameters, device_edge_contact);
// cout << "Running the PoissonSolver ... " << endl;
// bool IsInnerConv = true;
// bool rIsInnerConv = &IsInnerConv;
// poisson_solver.Run(potential_profile, charge_profile, fixed_charge, pIsInnerConv);
//
// cout << "After running PoissonSolver ..." << endl;
// cout << "PoissonSolver converged? " << IsInnerConv << endl;
// potential_profile.Print();
// charge_profile.Print();
// cout << endl;
//
// return 0;
//}