-
Notifications
You must be signed in to change notification settings - Fork 7
/
FiniteDifferences_ShortleyWeller_SquareGrid_extrapolation.py
466 lines (374 loc) · 17.9 KB
/
FiniteDifferences_ShortleyWeller_SquareGrid_extrapolation.py
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
464
465
466
#----------------------------------------------------------------------
#
# CERN
#
# European Organization for Nuclear Research
#
#
# This file is part of the code:
#
#
# PyPIC Version 2.4.5
#
#
# Author and contact: Giovanni IADAROLA
# BE-ABP Group
# CERN
# CH-1211 GENEVA 23
# SWITZERLAND
# giovanni.iadarola@cern.ch
#
# contact: Giovanni RUMOLO
# BE-ABP Group
# CERN
# CH-1211 GENEVA 23
# SWITZERLAND
# giovanni.rumolo@cern.ch
#
#
#
# Copyright CERN, Geneva 2011 - Copyright and any other
# appropriate legal protection of this computer program and
# associated documentation reserved in all countries of the
# world.
#
# Organizations collaborating with CERN may receive this program
# and documentation freely and without charge.
#
# CERN undertakes no obligation for the maintenance of this
# program, nor responsibility for its correctness, and accepts
# no liability whatsoever resulting from its use.
#
# Program and documentation are provided solely for the use of
# the organization to which they are distributed.
#
# This program may not be copied or otherwise distributed
# without permission. This message must be retained on this and
# any other authorized copies.
#
# The material cannot be sold. CERN should be given credit in
# all references.
#----------------------------------------------------------------------
import numpy as np
import scipy.sparse as scsp
from scipy.sparse.linalg import spsolve
import scipy.sparse.linalg as ssl
from .PyPIC_Scatter_Gather import PyPIC_Scatter_Gather
from scipy.constants import e, epsilon_0
na = lambda x:np.array([x])
qe = e
eps0 = epsilon_0
class FiniteDifferences_ShortleyWeller_SquareGrid(PyPIC_Scatter_Gather):
#@profile
def __init__(self,chamb, Dh, sparse_solver = 'scipy_slu'):
raise ValueError('This module has been discontinued') # All the state stuff has not been implemented
print('Start PIC init.:')
print('Finite Differences, Shortley-Weller, Square Grid')
print('Using Shortley-Weller boundary approx.')
self.Dh = Dh
super(FiniteDifferences_ShortleyWeller_SquareGrid, self).__init__(chamb.x_aper, chamb.y_aper, self.Dh, self.Dh)
Nyg, Nxg = self.Nyg, self.Nxg
[xn, yn]=np.meshgrid(self.xg,self.yg)
xn=xn.T
xn=xn.flatten()
yn=yn.T
yn=yn.flatten()
#% xn and yn are stored such that the external index is on x
flag_outside_n=chamb.is_outside(xn,yn)
flag_inside_n=~(flag_outside_n)
#flag_inside_n=(((xn/x_aper)**2 + (yn/y_aper)**2)<1);
#flag_outside_n= ~(flag_inside_n);
flag_outside_n_mat=np.reshape(flag_outside_n,(Nyg,Nxg),'F');
flag_outside_n_mat=flag_outside_n_mat.T
[gx,gy]=np.gradient(np.double(flag_outside_n_mat));
gradmod=abs(gx)+abs(gy);
flag_border_mat=np.logical_and((gradmod>0), flag_outside_n_mat);
flag_border_n = flag_border_mat.flatten()
A=scsp.lil_matrix((Nxg*Nyg,Nxg*Nyg)); #allocate a sparse matrix
Dx=scsp.lil_matrix((Nxg*Nyg,Nxg*Nyg)); #allocate a sparse matrix
Dy=scsp.lil_matrix((Nxg*Nyg,Nxg*Nyg)); #allocate a sparse matrix
list_internal_force_zero = []
# Build A Dx Dy matrices
for u in range(0,Nxg*Nyg):
if np.mod(u, Nxg*Nyg//20)==0:
print(('Mat. assembly %.0f'%(float(u)/ float(Nxg*Nyg)*100)+"""%"""))
if flag_inside_n[u]:
#Compute Shortley-Weller coefficients
if flag_inside_n[u-1]: #phi(i-1,j)
hw = Dh
else:
x_int,y_int,z_int,Nx_int,Ny_int, i_found_int = chamb.impact_point_and_normal(na(xn[u]), na(yn[u]), na(0.), na(xn[u-1]), na(yn[u-1]), na(0.), resc_fac=.995, flag_robust=False)
hw = np.abs(y_int[0]-yn[u])
if flag_inside_n[u+1]: #phi(i+1,j)
he = Dh
else:
x_int,y_int,z_int,Nx_int,Ny_int, i_found_int = chamb.impact_point_and_normal(na(xn[u]), na(yn[u]), na(0.), na(xn[u+1]), na(yn[u+1]), na(0.), resc_fac=.995, flag_robust=False)
he = np.abs(y_int[0]-yn[u])
if flag_inside_n[u-Nyg]: #phi(i,j-1)
hs = Dh
else:
x_int,y_int,z_int,Nx_int,Ny_int, i_found_int = chamb.impact_point_and_normal(na(xn[u]), na(yn[u]), na(0.), na(xn[u-Nyg]), na(yn[u-Nyg]), na(0.), resc_fac=.995, flag_robust=False)
hs = np.abs(x_int[0]-xn[u])
#~ print hs
if flag_inside_n[u+Nyg]: #phi(i,j+1)
hn = Dh
else:
x_int,y_int,z_int,Nx_int,Ny_int, i_found_int = chamb.impact_point_and_normal(na(xn[u]), na(yn[u]), na(0.), na(xn[u+Nyg]), na(yn[u+Nyg]), na(0.), resc_fac=.995, flag_robust=False)
hn = np.abs(x_int[0]-xn[u])
#~ print hn
# Build A matrix
if hn<Dh/100. or hs<Dh/100. or hw<Dh/100. or he<Dh/100.: # nodes very close to the bounday
A[u,u] =1.
list_internal_force_zero.append(u)
#print u, xn[u], yn[u]
else:
A[u,u] = -(2./(he*hw)+2/(hs*hn))
A[u,u-1]=2./(hw*(hw+he)); #phi(i-1,j)nx
A[u,u+1]=2./(he*(hw+he)); #phi(i+1,j)
A[u,u-Nyg]=2./(hs*(hs+hn)); #phi(i,j-1)
A[u,u+Nyg]=2./(hn*(hs+hn)); #phi(i,j+1)
# Build Dx matrix
if hn<Dh/100.:
if hs>=Dh/100.:
Dx[u,u] = -1./hs
Dx[u,u-Nyg]=1./hs
elif hs<Dh/100.:
if hn>=Dh/100.:
Dx[u,u] = 1./hn
Dx[u,u+Nyg]=-1./hn
else:
Dx[u,u] = (1./(2*hn)-1./(2*hs))
Dx[u,u-Nyg]=1./(2*hs)
Dx[u,u+Nyg]=-1./(2*hn)
# Build Dy matrix
if he<Dh/100.:
if hw>=Dh/100.:
Dy[u,u] = -1./hw
Dy[u,u-1]=1./hw
elif hw<Dh/100.:
if he>=Dh/100.:
Dy[u,u] = 1./he
Dy[u,u+1]=-1./(he)
else:
Dy[u,u] = (1./(2*he)-1./(2*hw))
Dy[u,u-1]=1./(2*hw)
Dy[u,u+1]=-1./(2*he)
else:
# external nodes
A[u,u]=1.
if flag_border_n[u]:
handle_border(u, flag_inside_n, Nxg, Nyg, xn, yn, chamb, Dh, Dx, Dy)
for u in list_internal_force_zero:
handle_border(u, flag_inside_n, Nxg, Nyg, xn, yn, chamb, Dh, Dx, Dy)
#~ A = A.tocsc()
#~ Dx = Dx.tocsc()
#~ Dy = Dy.tocsc()
flag_force_zero = flag_outside_n.copy()
for ind in list_internal_force_zero:
flag_force_zero[ind] = True
flag_force_zero_mat=np.reshape(flag_force_zero,(Nyg,Nxg),'F');
flag_force_zero_mat=flag_force_zero_mat.T
[gxc,gyc]=np.gradient(np.double(flag_force_zero_mat));
gradmodc=abs(gxc)+abs(gyc);
flag_border_mat_c=np.logical_and((gradmodc>0), flag_force_zero_mat);
sumcurr = np.sum(flag_border_mat_c, axis=0)
jj_max_border = np.max((np.where(sumcurr>0))[0])
jj_min_border = np.min((np.where(sumcurr>0))[0])
sumcurr = np.sum(flag_border_mat_c, axis=1)# corrected in version 4.05. I it was: sumcurr = np.sum(flag_border_mat_c, axis=1)
ii_max_border = np.max((np.where(sumcurr>0))[0])
ii_min_border = np.min((np.where(sumcurr>0))[0])
print('Internal nodes with 0 potential')
print(list_internal_force_zero)
A=A.tocsr() #convert to csr format
#Remove trivial equtions
diagonal = A.diagonal()
N_full = len(diagonal)
indices_non_id = np.where(diagonal!=1.)[0]
N_sel = len(indices_non_id)
Msel = scsp.lil_matrix((N_full, N_sel))
for ii, ind in enumerate(indices_non_id):
Msel[ind, ii] =1.
Msel = Msel.tocsc()
Asel = Msel.T*A*Msel
Asel=Asel.tocsc()
if sparse_solver == 'scipy_slu':
print("Using scipy superlu solver...")
luobj = ssl.splu(Asel.tocsc())
elif sparse_solver == 'PyKLU':
print("Using klu solver...")
try:
import PyKLU.klu as klu
luobj = klu.Klu(Asel.tocsc())
except Exception as e:
print("Got exception: ", e)
print("Falling back on scipy superlu solver:")
luobj = ssl.splu(Asel.tocsc())
else:
raise ValueError('Solver not recognized!!!!\nsparse_solver must be "scipy_klu" or "PyKLU"\n')
self.xn = xn
self.yn = yn
self.flag_inside_n = flag_inside_n
self.flag_outside_n = flag_outside_n
self.flag_outside_n_mat = flag_outside_n_mat
self.flag_inside_n_mat = np.logical_not(flag_outside_n_mat)
self.flag_border_mat = flag_border_mat
self.flag_force_zero = flag_force_zero
self.Asel = Asel
self.luobj = luobj
self.Dx = Dx.tocsc()
self.Dy = Dy.tocsc()
self.ii_max_border = ii_max_border
self.ii_min_border = ii_min_border
self.jj_max_border = jj_max_border
self.jj_min_border = jj_min_border
self.rho = np.zeros((self.Nxg,self.Nyg));
self.phi = np.zeros((self.Nxg,self.Nyg));
self.efx = np.zeros((self.Nxg,self.Nyg));
self.efy = np.zeros((self.Nxg,self.Nyg));
self.U_sc_eV_stp=0.;
self.Msel = Msel.tocsc()
self.Msel_T = (Msel.T).tocsc()
self.chamb = chamb
print('Done PIC init.')
#@profile
def solve(self, rho = None, flag_verbose = False):
if rho == None:
rho = self.rho
b=-rho.flatten()/eps0;
b[(self.flag_force_zero)]=0; #boundary condition
if flag_verbose:
print('Start Linear System Solution.')
b_sel = self.Msel_T*b
phi_sel = self.luobj.solve(b_sel)
phi = self.Msel*phi_sel
U_sc_eV_stp = -0.5*eps0*np.sum(b*phi)*self.Dh*self.Dh/qe
if flag_verbose:
print('Start field computation.')
efx = self.Dx*phi
efy = self.Dy*phi
phi=np.reshape(phi,(self.Nxg,self.Nyg))
efx=np.reshape(efx,(self.Nxg,self.Nyg))
efy=np.reshape(efy,(self.Nxg,self.Nyg))
for jj in range(self.jj_max_border, self.Nyg):
efx[:, jj]=efx[:, self.jj_max_border-1]
for jj in range(0, self.jj_min_border+1):
efx[:, jj]=efx[:, self.jj_min_border+1]
for ii in range(self.ii_max_border, self.Nxg):
efy[ii, :]=efy[self.ii_max_border-1, :]
for ii in range(0, self.ii_min_border+1):
efy[ii,:]=efy[self.ii_min_border+1,:]
self.rho = rho
self.b = b
self.phi = phi
self.efx = efx
self.efy = efy
self.U_sc_eV_stp = U_sc_eV_stp
def handle_border(u, flag_inside_n, Nxg, Nyg, xn, yn, chamb, Dh, Dx, Dy):
#print u
jjj = np.floor(u/Nyg)
if flag_inside_n[u+Nyg]:
if not flag_inside_n[u]:
x_int,y_int,z_int,Nx_int,Ny_int, i_found_int = chamb.impact_point_and_normal( na(xn[u+Nyg]), na(yn[u+Nyg]), na(0.),
na(xn[u]), na(yn[u]), na(0.), resc_fac=.995, flag_robust=False)
hs = np.abs(x_int[0]-xn[u+Nyg])
else: #this is the case for internal nodes with zero potential (very close to the boundary)
hs = Dh
hn = Dh
if hs<Dh/100.:
Dx[u,u+Nyg] = (1./(hn))
Dx[u,u+Nyg+Nyg]=-1./(hn)
nnn=1
while u-nnn*Nyg>=0:
Dx[u-nnn*Nyg,u+Nyg] = (1./(hn))
Dx[u-nnn*Nyg,u+Nyg+Nyg]=-1./(hn)
nnn+=1
else:
Dx[u,u+Nyg] = (1./(2*hn)-1./(2*hs))
Dx[u,u-Nyg+Nyg] = 1./(2*hs)
Dx[u,u+Nyg+Nyg] = -1./(2*hn)
nnn=1
while u-nnn*Nyg>=0:
Dx[u-nnn*Nyg,u+Nyg] = Dx[u,u+Nyg]
Dx[u-nnn*Nyg,u-Nyg+Nyg] = Dx[u,u-Nyg+Nyg]
Dx[u-nnn*Nyg,u+Nyg+Nyg] = Dx[u,u+Nyg+Nyg]
nnn+=1
elif flag_inside_n[u-Nyg]:
if not flag_inside_n[u]:
x_int,y_int,z_int,Nx_int,Ny_int, i_found_int = chamb.impact_point_and_normal( na(xn[u-Nyg]), na(yn[u-Nyg]), na(0.),
na(xn[u]), na(yn[u]), na(0.), resc_fac=.995, flag_robust=False)
hn = np.abs(x_int[0]-xn[u-Nyg])
else:#this is the case for internal nodes with zero potential (very close to the boundary)
hn = Dh
hs = Dh
if hn<Dh/100.:
Dx[u,u-Nyg] = -1./(hs)
Dx[u,u-Nyg-Nyg]=1./(hs)
nnn=1
while u+nnn*Nyg<Nxg*Nyg:
Dx[u+nnn*Nyg,u-Nyg] = -1./(hs)
Dx[u+nnn*Nyg,u-Nyg-Nyg]=1./(hs)
nnn+=1
else:
Dx[u,u-Nyg] = (1./(2*hn)-1./(2*hs))
Dx[u,u-Nyg-Nyg]=1./(2*hs)
Dx[u,u+Nyg-Nyg]=-1./(2*hn)
nnn=1
while u+nnn*Nyg<Nxg*Nyg:
Dx[u+nnn*Nyg,u-Nyg] = Dx[u,u-Nyg]
Dx[u+nnn*Nyg,u-Nyg-Nyg] = Dx[u,u-Nyg-Nyg]
Dx[u+nnn*Nyg,u+Nyg-Nyg] = Dx[u,u+Nyg-Nyg]
nnn+=1
if flag_inside_n[u+1]:
if not flag_inside_n[u]:
x_int,y_int,z_int,Nx_int,Ny_int, i_found_int = chamb.impact_point_and_normal( na(xn[u+1]), na(yn[u+1]), na(0.),
na(xn[u]), na(yn[u]),na(0.), resc_fac=.995, flag_robust=False)
hw = np.abs(y_int[0]-yn[u+1])
else:#this is the case for internal nodes with zero potential (very close to the boundary)
hw = Dh
he = Dh
if hw<Dh/100.:
Dy[u,u+1] = (1./(he))
Dy[u,u+1+1]=-1./(he)
nnn=1
while u-nnn>=(jjj)*Nyg:
Dy[u-nnn*1,u+1] = (1./(he))
Dy[u-nnn*1,u+1+1]=-1./(he)
nnn+=1
else:
Dy[u,u+1] = (1./(2*he)-1./(2*hw))
Dy[u,u-1+1] = 1./(2*hw)
Dy[u,u+1+1] = -1./(2*he)
nnn=1
while u-nnn>=(jjj)*Nyg:
#print nnn
Dy[u-nnn,u+1] = Dy[u,u+1]
Dy[u-nnn,u-1+1] = Dy[u,u-1+1]
Dy[u-nnn,u+1+1] = Dy[u,u+1+1]
nnn += 1
elif flag_inside_n[u-1]:
if not flag_inside_n[u]:
x_int,y_int,z_int,Nx_int,Ny_int, i_found_int = chamb.impact_point_and_normal( na(xn[u-1]), na(yn[u-1]), na(0.),
na(xn[u]), na(yn[u]), na(0.), resc_fac=.995, flag_robust=False)
he = np.abs(y_int[0]-yn[u-1])
else:#this is the case for internal nodes with zero potential (very close to the boundary)
he=Dh
hw = Dh
if he<Dh/100.:
Dy[u,u-1] = -1./(hw)
Dy[u,u-1-1]=1./(hw)
nnn=1
while u+nnn<(jjj+1)*Nyg:
Dy[u+nnn,u-1] = -1./(hw)
Dy[u+nnn,u-1-1]=1./(hw)
nnn+=1
else:
Dy[u,u-1] = (1./(2*he)-1./(2*hw))
Dy[u,u-1-1]=1./(2*hw)
Dy[u,u+1-1]=-1./(2*he)
nnn=1
while u+nnn<(jjj+1)*Nyg:
Dy[u+nnn,u-1] = Dy[u,u-1]
Dy[u+nnn,u-1-1] = Dy[u,u-1-1]
Dy[u+nnn,u+1-1] = Dy[u,u+1-1]
nnn+=1
return Dx, Dy