forked from openecloud/openecloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrid.pyx
1486 lines (1362 loc) · 68.4 KB
/
grid.pyx
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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#cython: boundscheck=False
#cython: nonecheck=False
#cython: wraparound=False
#cython: cdivision=True
import numpy
import matplotlib.pyplot as mpl
cimport numpy
cimport cython
import scipy.sparse as spsp
from constants cimport *
# Import some C methods.
cdef extern from "math.h":
double sin(double x) nogil
double cos(double x) nogil
double asin(double x) nogil
double acos(double x) nogil
double atan(double x) nogil
double atan2(double x, double y) nogil
double sqrt(double x) nogil
double log2(double x) nogil
double fabs(double x) nogil
'''
Class to calculate and store grid quantities.
It looks like a lot happens here (especially the cut-cell stuff),
but it is fairly simple. Just long.
Requires scipy.sparse (spsp) currently.
'''
cdef class Grid:
'''
Constructor.
Input:
- nx, ny: Number of grid points in x and y direction.
- lx, ly: Grid size in x and y direction.
- boundType: Type of boundary. One of 0=rectangular, 1=elliptical or 2=arbitrary.
- boundFunc: Function which returns 1 if point is inside and 0 if outside domain (optional).
- cutCellAcc: Accuracy to which cut cell edge length are calculated (optional).
- scanAroundPoint: Relative area around each grid point which must be inside the grid
boundary for the point to be set as inside point. (optional).
'''
def __init__(Grid self, unsigned int nx, unsigned int ny, double lx, double ly,
unsigned short boundType, object boundFunc = None, double cutCellAcc = 1e-12, double scanAroundPoint = 1.e-3):
# Calculate and store basic parameters of the uniform grid. For cut-cell some accuracy parameters.
self.nx = nx
self.ny = ny
self.lx = lx
self.ly = ly
self.dx = self.lx/(self.nx-1.)
self.dy = self.ly/(self.ny-1.)
self.cutCellAcc = cutCellAcc
self.scanAroundPoint = scanAroundPoint
# Extend grid internally by 1 guard cell on each side. These guard cells are needed for
# the particleBoundary and are outside of domain.
self.nxExt = self.nx + 2
self.nyExt = self.ny + 2
self.npExt = self.nxExt*self.nyExt
self.lxExt = self.lx + 2.*self.dx
self.lyExt = self.ly + 2.*self.dy
# Some position arrays for later.
self.xMesh = numpy.linspace(-self.lxExt/2., self.lxExt/2, self.nxExt)
self.yMesh = numpy.linspace(-self.lyExt/2., self.lyExt/2, self.nyExt)
# Allocate memory. Numpy is fast enough here and more convenient (garbage collection!).
self.insidePoints = numpy.empty(self.npExt, dtype=numpy.ushort)
self.insideEdges = numpy.empty(2*self.npExt, dtype=numpy.ushort)
self.insideFaces = numpy.empty(self.npExt, dtype=numpy.ushort)
self.ds = numpy.empty(2*self.npExt, dtype=numpy.double)
self.dsi = numpy.empty(2*self.npExt, dtype=numpy.double)
self.da = numpy.empty(self.npExt, dtype=numpy.double)
self.dai = numpy.empty(self.npExt, dtype=numpy.double)
self.dst = numpy.empty(2*self.npExt, dtype=numpy.double)
self.dsti = numpy.empty(2*self.npExt, dtype=numpy.double)
self.dat = numpy.empty(self.npExt, dtype=numpy.double)
self.dati = numpy.empty(self.npExt, dtype=numpy.double)
# Calculate other grid parameters depending on boundary.
if boundType == 0:
self.boundFunc = lambda x,y: _boundFuncRectangular(x,y,self.lx*0.5,self.ly*0.5)
self.computeStaircaseGridGeom()
elif boundType == 1:
self.boundFunc = lambda x,y: _boundFuncElliptical(x,y,4./self.lx**2,4./self.ly**2)
self.computeCutCellGridGeom()
elif boundType == 2:
self.boundFunc = boundFunc
self.computeStaircaseGridGeom()
elif boundType == 3:
self.boundFunc = boundFunc
self.computeCutCellGridGeom()
elif boundType == 4:
self.prepareBoundFuncPoints(boundFunc)
self.boundFunc = self.pointInPolygon
self.computeCutCellGridGeom()
else:
raise NotImplementedError("Not yet implemented.")
cpdef unsigned int pointInPolygon(Grid self, double xx, double yy):
cdef:
int res
double* xy = [0.,0.]
xy[0] = fabs(xx)
xy[1] = fabs(yy)
res = _wn_PnPoly(xy, &self.boundFuncPoints[0,0], self.nBoundFuncPoints)
if res == 0:
return 0
else:
return 1
cdef void prepareBoundFuncPoints(Grid self, double[:,:] boundFuncPoints):
cdef:
unsigned int ii, jj
double minArea
unsigned short[:] necessaryPoints
nBoundFuncPoints = boundFuncPoints.shape[0]
# Clean up unnecessary points
necessaryPoints = numpy.ones(nBoundFuncPoints, dtype=numpy.ushort)
ii = 0
while ii+2 < nBoundFuncPoints:
inc = 0
while ii+2+inc < nBoundFuncPoints:
minArea = 1.e-16 * ( sqrt( (boundFuncPoints[ii,0]-boundFuncPoints[ii+1,0])**2 +
(boundFuncPoints[ii,1]-boundFuncPoints[ii+1,1])**2) *
sqrt( (boundFuncPoints[ii+1+inc,0]-boundFuncPoints[ii+2+inc,0])**2 +
(boundFuncPoints[ii+1+inc,1]-boundFuncPoints[ii+2+inc,1])**2) )
area = ( boundFuncPoints[ii,0]*(boundFuncPoints[ii+1+inc,1]-boundFuncPoints[ii+2+inc,1]) +
boundFuncPoints[ii+1+inc,0]*(boundFuncPoints[ii+2+inc,1]-boundFuncPoints[ii,1]) +
boundFuncPoints[ii+2+inc,0]*(boundFuncPoints[ii,1]-boundFuncPoints[ii+1+inc,1]) )
if fabs(minArea) > fabs(area):
inc += 1
else:
break
for jj in range(ii+1, ii+inc+1):
necessaryPoints[jj] = 0
ii += inc + 1
self.nBoundFuncPoints = numpy.sum(necessaryPoints)
self.boundFuncPoints = numpy.zeros((self.nBoundFuncPoints,2), dtype=numpy.double)
jj = 0
for ii in range(nBoundFuncPoints):
if necessaryPoints[ii] == 1:
self.boundFuncPoints[jj,0] = boundFuncPoints[ii,0]
self.boundFuncPoints[jj,1] = boundFuncPoints[ii,1]
jj += 1
'''
Helper function for the setup of staircase geometries. Calculates grid quantities.
'''
cdef void computeStaircaseGridGeom(Grid self):
cdef:
unsigned int ii, jj, kk
unsigned int nx = self.nxExt, ny = self.nyExt, np = self.npExt
double dx = self.dx, dy = self.dy, dxi = 1./dx, dyi = 1./dy
double dxHalf = 0.5*dx, dyHalf = 0.5*dy, dxHalfi = 1./dxHalf, dyHalfi = 1./dyHalf
double dxdy = dx*dy, dxdyi = 1./dxdy
double dxMin = self.scanAroundPoint*dx, dyMin = self.scanAroundPoint*dy
unsigned short* insidePoints = &self.insidePoints[0]
unsigned short* insideEdges = &self.insideEdges[0]
unsigned short* insideFaces = &self.insideFaces[0]
double* ds = &self.ds[0]
double* dsi = &self.dsi[0]
double* da = &self.da[0]
double* dai = &self.dai[0]
double* dst = &self.dst[0]
double* dsti = &self.dsti[0]
double* dat = &self.dat[0]
double* dati = &self.dati[0]
double* xMesh = &self.xMesh[0]
double* yMesh = &self.yMesh[0]
unsigned int[:] rows = numpy.empty(6*np, dtype=numpy.uintc) # In worst case interpolation matrix has
unsigned int[:] columns = numpy.empty(6*np, dtype=numpy.uintc) # quite a lot of non-zero elements.
double[:] values = numpy.empty(6*np, dtype=numpy.double)
object boundFunc = self.boundFunc
# Check if grid point lies inside domain with some given tolerance.
for ii in range(nx):
insidePoints[ii] = 0
insidePoints[ii+nx] = 0
insidePoints[ii+(ny-2)*nx] = 0
insidePoints[ii+(ny-1)*nx] = 0
for jj in range(2,ny-2):
insidePoints[jj*nx] = 0
insidePoints[1+jj*nx] = 0
insidePoints[(nx-2)+jj*nx] = 0
insidePoints[(nx-1)+jj*nx] = 0
for ii in range(2,nx-2):
for jj in range(2,ny-2):
if (boundFunc(xMesh[ii]+dxMin,yMesh[jj]+dyMin)==0 or
boundFunc(xMesh[ii]+dxMin,yMesh[jj]-dyMin)==0 or
boundFunc(xMesh[ii]-dxMin,yMesh[jj]+dyMin)==0 or
boundFunc(xMesh[ii]-dxMin,yMesh[jj]-dyMin)==0):
insidePoints[ii+jj*nx] = 0
else:
insidePoints[ii+jj*nx] = 1
# Classify edges. 0 = outside, 1 = boundary edges and 2 = normal inside edges.
for ii in range(nx):
insideEdges[ii] = 0
insideEdges[ii+nx] = 0
insideEdges[ii+(ny-2)*nx] = 0
insideEdges[ii+(ny-1)*nx] = 0
insideEdges[ii+np] = 0
insideEdges[ii+(ny-1)*nx+np] = 0
for jj in range(ny):
insideEdges[jj*nx+np] = 0
insideEdges[1+jj*nx+np] = 0
insideEdges[(nx-2)+jj*nx+np] = 0
insideEdges[(nx-1)+jj*nx+np] = 0
insideEdges[jj*nx] = 0
insideEdges[(nx-1)+jj*nx] = 0
for jj in range(2,ny-2):
for ii in range(1,nx-1):
insideEdges[ii+jj*nx] = insidePoints[ii+jj*nx] + insidePoints[ii+1+jj*nx]
for jj in range(1,ny-1):
for ii in range(2,nx-2):
insideEdges[ii+jj*nx+np] = insidePoints[ii+jj*nx] + insidePoints[ii+(jj+1)*nx]
# Edges length. Set known values manually (e.g. guard cells). Fast!
for ii in range(nx):
ds[ii] = 0.
ds[ii+nx] = 0.
ds[ii+(ny-2)*nx] = 0.
ds[ii+(ny-1)*nx] = 0.
ds[ii+np] = 0.
ds[ii+(ny-2)*nx+np] = 0.
ds[ii+(ny-1)*nx+np] = 0.
dsi[ii] = 0.
dsi[ii+nx] = 0.
dsi[ii+(ny-2)*nx] = 0.
dsi[ii+(ny-1)*nx] = 0.
dsi[ii+np] = 0.
dsi[ii+(ny-2)*nx+np] = 0.
dsi[ii+(ny-1)*nx+np] = 0.
for jj in range(1,ny-2):
ds[jj*nx+np] = 0.
ds[1+jj*nx+np] = 0.
ds[(nx-2)+jj*nx+np] = 0.
ds[(nx-1)+jj*nx+np] = 0.
dsi[jj*nx+np] = 0.
dsi[1+jj*nx+np] = 0.
dsi[(nx-2)+jj*nx+np] = 0.
dsi[(nx-1)+jj*nx+np] = 0.
for jj in range(2,ny-2):
ds[jj*nx] = 0.
ds[(nx-2)+jj*nx] = 0.
ds[(nx-1)+jj*nx] = 0.
dsi[jj*nx] = 0.
dsi[(nx-2)+jj*nx] = 0.
dsi[(nx-1)+jj*nx] = 0.
# X-edges.
for jj in range(2,ny-2):
for ii in range(1,nx-2):
if insideEdges[ii+jj*nx]==0:
ds[ii+jj*nx] = 0.
dsi[ii+jj*nx] = 0.
else:
ds[ii+jj*nx] = dx # Only equidistant (in each direction) grid here.
dsi[ii+jj*nx] = dxi
# Y-edges.
for jj in range(1,ny-2):
for ii in range(2,nx-2):
if insideEdges[ii+jj*nx+np]==0:
ds[ii+jj*nx+np] = 0.
dsi[ii+jj*nx+np] = 0.
else:
ds[ii+jj*nx+np] = dy # Only equidistant (in each direction) grid here.
dsi[ii+jj*nx+np] = dyi
# Calculation of faces. Primary grid.
# Note that these are not used for any essential calculation if a Poisson solver is used.
for ii in range(nx):
insideFaces[ii] = 0
insideFaces[ii+(ny-2)*nx] = 0
insideFaces[ii+(ny-1)*nx] = 0
for jj in range(1,ny-2):
insideFaces[jj*nx] = 0
insideFaces[(nx-2)+jj*nx] = 0
insideFaces[(nx-1)+jj*nx] = 0
for jj in range(1,ny-2):
for ii in range(1,nx-2):
insideFaces[ii+jj*nx] = insideEdges[ii+jj*nx] + insideEdges[ii+(jj+1)*nx] + \
insideEdges[ii+jj*nx+np] + insideEdges[ii+1+jj*nx+np]
for ii in range(nx):
da[ii] = 0.
da[ii+(ny-2)*nx] = 0.
da[ii+(ny-1)*nx] = 0.
dai[ii] = 0.
dai[ii+(ny-2)*nx] = 0.
dai[ii+(ny-1)*nx] = 0.
for jj in range(1,ny-2):
da[jj*nx] = 0.
da[(nx-2)+jj*nx] = 0.
da[(nx-1)+jj*nx] = 0.
dai[jj*nx] = 0.
dai[(nx-2)+jj*nx] = 0.
dai[(nx-1)+jj*nx] = 0.
for jj in range(1,ny-2):
for ii in range(1,nx-2):
if insideFaces[ii+jj*nx]==0:
da[ii+jj*nx] = 0.
dai[ii+jj*nx] = 0.
else:
da[ii+jj*nx] = dxdy
dai[ii+jj*nx] = dxdyi
# Dual grid quantities are not affected. Always same as for rectangular.
# They are not needed for any essential
# calculation in the Poisson problem, just for visualization of the charge density.
for ii in range(nx):
dst[ii] = 0.
dst[ii+(ny-1)*nx] = 0.
dst[ii+np] = 0.
dst[ii+(ny-1)*nx+np] = 0.
dsti[ii] = 0.
dsti[ii+(ny-1)*nx] = 0.
dsti[ii+np] = 0.
dsti[ii+(ny-1)*nx+np] = 0.
for jj in range(1,ny-1):
dst[jj*nx] = 0.
dst[(nx-1)+jj*nx] = 0.
dst[jj*nx+np] = 0.
dst[(nx-1)+jj*nx+np] = 0.
dsti[jj*nx] = 0.
dsti[(nx-1)+jj*nx] = 0.
dsti[jj*nx+np] = 0.
dsti[(nx-1)+jj*nx+np] = 0.
for ii in range(2,nx-2):
for jj in range(1,ny-1):
dst[ii+jj*nx] = dx # Here assuming equidistant meshes (in each direction).
dsti[ii+jj*nx] = dxi
for ii in range(1,nx-1):
for jj in range(2,ny-2):
dst[ii+jj*nx+np] = dy
dsti[ii+jj*nx+np] = dyi
for jj in range(1,ny-1):
dst[1+jj*nx] = dxHalf
dst[(nx-2)+jj*nx] = dxHalf
dsti[1+jj*nx] = dxHalfi
dsti[(nx-2)+jj*nx] = dxHalfi
for ii in range(1,nx-1):
dst[ii+nx+np] = dyHalf
dst[ii+(ny-2)*nx+np] = dyHalf
dsti[ii+nx+np] = dyHalfi
dsti[ii+(ny-2)*nx+np] = dyHalfi
for ii in range(nx):
for jj in range(ny):
dat[ii+jj*nx] = dst[ii+jj*nx]*dst[ii+jj*nx+np]
dati[ii+jj*nx] = dsti[ii+jj*nx]*dsti[ii+jj*nx+np]
# Standard interpolation stuff. Interpolation from edges to _inside_ grid points. Same as rectangular.
kk = 0
for ii in range(1,nx-2):
for jj in range(2,ny-2):
currentInd = ii + jj*nx
if insidePoints[currentInd]==1:
rows[kk] = currentInd
columns[kk] = currentInd
values[kk] = ds[currentInd]/(ds[currentInd-1]+ds[currentInd]) # Should works for non-equidistant grid.
kk += 1
for ii in range(2,nx-1):
for jj in range(2,ny-2):
currentInd = ii + jj*nx
if insidePoints[currentInd]==1:
rows[kk] = currentInd
columns[kk] = currentInd-1
values[kk] = ds[currentInd-1]/(ds[currentInd-1]+ds[currentInd])
kk += 1
for ii in range(2,nx-2):
for jj in range(1,ny-2):
currentInd = ii + jj*nx
if insidePoints[currentInd]==1:
currentInd += np
rows[kk] = currentInd
columns[kk] = currentInd
values[kk] = ds[currentInd]/(ds[currentInd-nx]+ds[currentInd])
kk += 1
for ii in range(2,nx-2):
for jj in range(2,ny-1):
currentInd = ii + jj*nx
if insidePoints[currentInd]==1:
currentInd += np
rows[kk] = currentInd
columns[kk] = currentInd-nx
values[kk] = ds[currentInd-nx]/(ds[currentInd-nx]+ds[currentInd])
kk += 1
self.edgeToNode = spsp.csc_matrix(spsp.coo_matrix((values[:kk],(rows[:kk],columns[:kk])),shape=(2*np,2*np)))
# Boundary interpolation for correct fields at boundary.
kk = 0
for jj in range(1,ny-1):
for ii in range(1,nx-1):
currentInd = ii+jj*nx
# Inside points are unchanged.
if insidePoints[currentInd]==1:
rows[kk] = currentInd
columns[kk] = currentInd
values[kk] = 1.
kk += 1
rows[kk] = currentInd+np
columns[kk] = currentInd+np
values[kk] = 1.
kk += 1
# Boundary edges.
else:
if insideEdges[currentInd]==1:
# X in negative x-direction.
if insidePoints[currentInd+nx]==1 and insidePoints[currentInd-nx]==1:
rows[kk] = currentInd
columns[kk] = currentInd+1
values[kk] = 1.
kk += 1
elif insideEdges[currentInd-1]==1:
# X in positive x-direction
if insidePoints[currentInd+nx]==1 and insidePoints[currentInd-nx]==1:
rows[kk] = currentInd
columns[kk] = currentInd-1
values[kk] = 1.
kk += 1
if insideEdges[currentInd+np]==1:
# Y in negative y-direction.
if insidePoints[currentInd+1]==1 and insidePoints[currentInd-1]==1:
rows[kk] = currentInd
columns[kk] = currentInd+nx
values[kk] = 1.
kk += 1
elif insideEdges[currentInd+np-nx]==1:
# Y in positive y-direction.
if insidePoints[currentInd+1]==1 and insidePoints[currentInd-1]==1:
rows[kk] = currentInd
columns[kk] = currentInd-nx
values[kk] = 1.
kk += 1
self.edgeToNode = spsp.csc_matrix(spsp.coo_matrix((values[:kk],(rows[:kk],columns[:kk])),shape=(2*np,2*np))
).dot(self.edgeToNode)
'''
Helper function for the setup of cut-cell geometries. Calculates grid quantities.
'''
cdef void computeCutCellGridGeom(Grid self):
cdef:
unsigned int ii, jj, kk
unsigned int nx = self.nxExt, ny = self.nyExt, np = self.npExt
double dx = self.dx, dy = self.dy, dxi = 1./dx, dyi = 1./dy
double dxHalf = 0.5*dx, dyHalf = 0.5*dy, dxHalfi = 1./dxHalf, dyHalfi = 1./dyHalf
double dxdy = dx*dy, dxdyi = 1./dxdy
double dxMin = self.scanAroundPoint*dx, dyMin = self.scanAroundPoint*dy
double cutCellAcc = self.cutCellAcc
unsigned int boundAccIter = <unsigned int> (log2(1./cutCellAcc)+2.) # Number of iterations for given accuracy.
unsigned short* insidePoints = &self.insidePoints[0]
unsigned short* insideEdges = &self.insideEdges[0]
unsigned short* insideFaces = &self.insideFaces[0]
double* ds = &self.ds[0]
double* dsi = &self.dsi[0]
double* da = &self.da[0]
double* dai = &self.dai[0]
double* dst = &self.dst[0]
double* dsti = &self.dsti[0]
double* dat = &self.dat[0]
double* dati = &self.dati[0]
double* xMesh = &self.xMesh[0]
double* yMesh = &self.yMesh[0]
double* boundaryPoints
int* cutCellPointsInd
unsigned int* boundaryPointsInd
unsigned int nBoundaryPoints
double* cutCellCenter
double* cutCellNormalVectors
unsigned int[:] rows = numpy.empty(18*np, dtype=numpy.uintc) # In worst case interpolation matrix has
unsigned int[:] columns = numpy.empty(18*np, dtype=numpy.uintc) # quite a lot of non-zero elements.
double[:] values = numpy.empty(18*np, dtype=numpy.double)
double[:] connectedEdgesInv = numpy.empty(np, dtype=numpy.double)
double[:] cosAlpha = numpy.empty(np, dtype=numpy.double)
double[:] sinAlpha = numpy.empty(np, dtype=numpy.double)
double[:] cosBeta = numpy.empty(np, dtype=numpy.double)
double[:] sinBeta = numpy.empty(np, dtype=numpy.double)
double x1, x2, x3, y
double y1, y2, y3, x
unsigned int tempuint
double tempdouble
unsigned short test01, test02, test03
object boundFunc = self.boundFunc
double bestDis
unsigned int bestInd
unsigned int searchInd1, searchInd2
# Check if grid point lies inside domain with some given tolerance.
for ii in range(nx):
insidePoints[ii] = 0
insidePoints[ii+nx] = 0
insidePoints[ii+(ny-2)*nx] = 0
insidePoints[ii+(ny-1)*nx] = 0
for jj in range(2,ny-2):
insidePoints[jj*nx] = 0
insidePoints[1+jj*nx] = 0
insidePoints[(nx-2)+jj*nx] = 0
insidePoints[(nx-1)+jj*nx] = 0
for ii in range(2,nx-2):
for jj in range(2,ny-2):
if (boundFunc(xMesh[ii]+dxMin,yMesh[jj]+dyMin)==0 or
boundFunc(xMesh[ii]+dxMin,yMesh[jj]-dyMin)==0 or
boundFunc(xMesh[ii]-dxMin,yMesh[jj]+dyMin)==0 or
boundFunc(xMesh[ii]-dxMin,yMesh[jj]-dyMin)==0):
insidePoints[ii+jj*nx] = 0
else:
insidePoints[ii+jj*nx] = 1
# Classify edges. 0 = outside, 1 = possible cut cell edges and 2 = normal inside edges.
for ii in range(nx):
insideEdges[ii] = 0
insideEdges[ii+nx] = 0
insideEdges[ii+(ny-2)*nx] = 0
insideEdges[ii+(ny-1)*nx] = 0
insideEdges[ii+np] = 0
insideEdges[ii+(ny-1)*nx+np] = 0
for jj in range(ny):
insideEdges[jj*nx+np] = 0
insideEdges[1+jj*nx+np] = 0
insideEdges[(nx-2)+jj*nx+np] = 0
insideEdges[(nx-1)+jj*nx+np] = 0
insideEdges[jj*nx] = 0
insideEdges[(nx-1)+jj*nx] = 0
for jj in range(2,ny-2):
for ii in range(1,nx-1):
insideEdges[ii+jj*nx] = insidePoints[ii+jj*nx] + insidePoints[ii+1+jj*nx]
for jj in range(1,ny-1):
for ii in range(2,nx-2):
insideEdges[ii+jj*nx+np] = insidePoints[ii+jj*nx] + insidePoints[ii+(jj+1)*nx]
# Edges length. Set known values manually (e.g. guard cells). Fast!
for ii in range(nx):
ds[ii] = 0.
ds[ii+nx] = 0.
ds[ii+(ny-2)*nx] = 0.
ds[ii+(ny-1)*nx] = 0.
ds[ii+np] = 0.
ds[ii+(ny-2)*nx+np] = 0.
ds[ii+(ny-1)*nx+np] = 0.
dsi[ii] = 0.
dsi[ii+nx] = 0.
dsi[ii+(ny-2)*nx] = 0.
dsi[ii+(ny-1)*nx] = 0.
dsi[ii+np] = 0.
dsi[ii+(ny-2)*nx+np] = 0.
dsi[ii+(ny-1)*nx+np] = 0.
for jj in range(1,ny-2):
ds[jj*nx+np] = 0.
ds[1+jj*nx+np] = 0.
ds[(nx-2)+jj*nx+np] = 0.
ds[(nx-1)+jj*nx+np] = 0.
dsi[jj*nx+np] = 0.
dsi[1+jj*nx+np] = 0.
dsi[(nx-2)+jj*nx+np] = 0.
dsi[(nx-1)+jj*nx+np] = 0.
for jj in range(2,ny-2):
ds[jj*nx] = 0.
ds[(nx-2)+jj*nx] = 0.
ds[(nx-1)+jj*nx] = 0.
dsi[jj*nx] = 0.
dsi[(nx-2)+jj*nx] = 0.
dsi[(nx-1)+jj*nx] = 0.
for jj in range(2,ny-2):
for ii in range(1,nx-2):
currentInd = ii+jj*nx
if insideEdges[currentInd]==2:
ds[currentInd] = dx # Only equidistant (in each direction) grid here.
dsi[currentInd] = dxi
elif insideEdges[currentInd]==0:
ds[currentInd] = 0.
dsi[currentInd] = 0.
else:
# Cut-cell edge.
# Bisect repeatedly the possible cut cell edges to get length of each.
x1 = xMesh[ii]
x2 = xMesh[ii+1]
y = yMesh[jj]
test01 = boundFunc(x1,y)
test02 = boundFunc(x2,y)
if test01 == 1 and test02 == 1:
ds[currentInd] = dx#(1.-self.scanAroundPoint)*dx # WORKAROUND. Maybe better possible.
else:
for kk in range(boundAccIter):
x3 = 0.5*(x1+x2)
test03 = boundFunc(x3,y)
if test01==test03:
x1 = x3
test01 = test03
else:
x2 = x3
test02 = test03
if insidePoints[currentInd]==1:
ds[currentInd] = 0.5*(x1+x2) - xMesh[ii]
else:
ds[currentInd] = xMesh[ii+1] - 0.5*(x1+x2)
dsi[currentInd] = 1./ds[currentInd]
# Same stuff as above, only for y-edges.
for jj in range(1,ny-2):
for ii in range(2,nx-2):
currentInd = ii+jj*nx
if insideEdges[currentInd+np]==2:
ds[currentInd+np] = dy # Only equidistant (in each direction) grid here.
dsi[currentInd+np] = dyi
elif insideEdges[currentInd+np]==0:
ds[currentInd+np] = 0.
dsi[currentInd+np] = 0.
else:
# Cut-cell edge.
# Bisect repeatedly the possible cut cell edges to get length of each.
y1 = yMesh[jj]
y2 = yMesh[jj+1]
x = xMesh[ii]
test01 = boundFunc(x,y1)
test02 = boundFunc(x,y2)
if test01 == 1 and test02 == 1:
ds[currentInd+np] = dy#(1.-self.scanAroundPoint)*dy # WORKAROUND. Maybe better possible.
else:
for kk in range(boundAccIter):
y3 = 0.5*(y1+y2)
test03 = boundFunc(x,y3)
if test01==test03:
y1 = y3
test01 = test03
else:
y2 = y3
test02 = test03
if insidePoints[currentInd]==1:
ds[currentInd+np] = 0.5*(y1+y2) - yMesh[jj]
else:
ds[currentInd+np] = yMesh[jj+1] - 0.5*(y1+y2)
dsi[currentInd+np] = 1./ds[currentInd+np]
# Determination of boundary points.
# And save indices of moved grid points due to cut-cell for particle boundary later.
nBoundaryPoints = 0
for jj in range(1,ny-1):
for ii in range(1,nx-1):
currentInd = ii+jj*nx
if insideEdges[currentInd]==1:
nBoundaryPoints += 1
if insideEdges[currentInd+np]==1:
nBoundaryPoints += 1
self.boundaryPoints = numpy.empty((nBoundaryPoints,2), dtype=numpy.double)
boundaryPoints = &self.boundaryPoints[0,0]
self.boundaryPointsInd = numpy.empty(nBoundaryPoints, dtype=numpy.uintc)
boundaryPointsInd = &self.boundaryPointsInd[0]
self.cutCellPointsInd = -numpy.ones((np,2), dtype=numpy.intc)
cutCellPointsInd = &self.cutCellPointsInd[0,0]
kk = 0
for jj in range(1,ny-1):
for ii in range(1,nx-1):
currentInd = ii+jj*nx
if insideEdges[currentInd]==1:
if insidePoints[currentInd]==1:
boundaryPoints[2*kk] = xMesh[ii] + ds[currentInd]
boundaryPoints[2*kk+1] = yMesh[jj]
boundaryPointsInd[kk] = currentInd+1
else:
boundaryPoints[2*kk] = xMesh[ii+1] - ds[currentInd]
boundaryPoints[2*kk+1] = yMesh[jj]
boundaryPointsInd[kk] = currentInd
if cutCellPointsInd[2*currentInd]==-1:
cutCellPointsInd[2*currentInd] = kk
else:
cutCellPointsInd[2*currentInd+1] = kk
if cutCellPointsInd[2*(currentInd-nx)]==-1:
cutCellPointsInd[2*(currentInd-nx)] = kk
else:
cutCellPointsInd[2*(currentInd-nx)+1] = kk
kk += 1
if insideEdges[currentInd+np]==1:
if insidePoints[currentInd]==1:
boundaryPoints[2*kk] = xMesh[ii]
boundaryPoints[2*kk+1] = yMesh[jj] + ds[currentInd+np]
boundaryPointsInd[kk] = currentInd+nx+np
else:
boundaryPoints[2*kk] = xMesh[ii]
boundaryPoints[2*kk+1] = yMesh[jj+1] - ds[currentInd+np]
boundaryPointsInd[kk] = currentInd+np
if cutCellPointsInd[2*currentInd]==-1:
cutCellPointsInd[2*currentInd] = kk
else:
cutCellPointsInd[2*currentInd+1] = kk
if cutCellPointsInd[2*(currentInd-1)]==-1:
cutCellPointsInd[2*(currentInd-1)] = kk
else:
cutCellPointsInd[2*(currentInd-1)+1] = kk
kk += 1
# Calculation of faces. Primary grid. Linear approximation.
# Note that these are not used for any essential calculation if a Poisson solver is used.
for ii in range(nx):
insideFaces[ii] = 0
insideFaces[ii+(ny-2)*nx] = 0
insideFaces[ii+(ny-1)*nx] = 0
for jj in range(1,ny-2):
insideFaces[jj*nx] = 0
insideFaces[(nx-2)+jj*nx] = 0
insideFaces[(nx-1)+jj*nx] = 0
for jj in range(1,ny-2):
for ii in range(1,nx-2):
insideFaces[ii+jj*nx] = insideEdges[ii+jj*nx] + insideEdges[ii+(jj+1)*nx] + \
insideEdges[ii+jj*nx+np] + insideEdges[ii+1+jj*nx+np]
# Sort boundary points of each cell such that counter-clockwise.
self.cutCellCenter = numpy.zeros((np,2), dtype=numpy.double)
cutCellCenter = &self.cutCellCenter[0,0]
for jj in range(1,ny-1):
for ii in range(1,nx-1):
currentInd = ii + jj*nx
if insideFaces[currentInd] != 0 and insideFaces[currentInd] != 8:
# First calculate center of faces.
if insideFaces[currentInd]==2:
if insidePoints[currentInd]==1:
cutCellCenter[2*currentInd] = xMesh[ii] + ds[currentInd]/3.
cutCellCenter[2*currentInd+1] = yMesh[jj] + ds[currentInd+np]/3.
elif insidePoints[currentInd+1]==1:
cutCellCenter[2*currentInd] = xMesh[ii+1] - ds[currentInd]/3.
cutCellCenter[2*currentInd+1] = yMesh[jj] + ds[currentInd+np+1]/3.
elif insidePoints[currentInd+nx]==1:
cutCellCenter[2*currentInd] = xMesh[ii] + ds[currentInd+nx]/3.
cutCellCenter[2*currentInd+1] = yMesh[jj+1] - ds[currentInd+np]/3.
elif insidePoints[currentInd+nx+1]==1:
cutCellCenter[2*currentInd] = xMesh[ii+1] - ds[currentInd+nx]/3.
cutCellCenter[2*currentInd+1] = yMesh[jj+1] - ds[currentInd+np+1]/3.
elif insideFaces[currentInd]==4:
if insidePoints[currentInd]==1 and insidePoints[currentInd+1]==1:
cutCellCenter[2*currentInd] = (xMesh[ii] + xMesh[ii+1])*0.5
cutCellCenter[2*currentInd+1] = yMesh[jj] + (ds[currentInd+np] + ds[currentInd+np+1])*0.25
elif insidePoints[currentInd+nx]==1 and insidePoints[currentInd+nx+1]==1:
cutCellCenter[2*currentInd] = (xMesh[ii] + xMesh[ii+1])*0.5
cutCellCenter[2*currentInd+1] = yMesh[jj+1] - (ds[currentInd+np] + ds[currentInd+np+1])*0.25
elif insidePoints[currentInd]==1 and insidePoints[currentInd+nx]==1:
cutCellCenter[2*currentInd] = xMesh[ii] + (ds[currentInd] + ds[currentInd+nx])*0.25
cutCellCenter[2*currentInd+1] = (yMesh[jj] + yMesh[jj+1])*0.5
elif insidePoints[currentInd+1]==1 and insidePoints[currentInd+nx+1]==1:
cutCellCenter[2*currentInd] = xMesh[ii+1] - (ds[currentInd] + ds[currentInd+nx])*0.25
cutCellCenter[2*currentInd+1] = (yMesh[jj] + yMesh[jj+1])*0.5
elif insideFaces[currentInd]==6:
if insidePoints[currentInd]==0:
cutCellCenter[2*currentInd] = (2.*xMesh[ii] + 3.*xMesh[ii+1] - ds[currentInd])*0.2
cutCellCenter[2*currentInd+1] = (2.*yMesh[jj] + 3.*yMesh[jj+1] - ds[currentInd+np])*0.2
elif insidePoints[currentInd+1]==0:
cutCellCenter[2*currentInd] = (3.*xMesh[ii] + 2.*xMesh[ii+1] + ds[currentInd])*0.2
cutCellCenter[2*currentInd+1] = (2.*yMesh[jj] + 3.*yMesh[jj+1] - ds[currentInd+np+1])*0.2
elif insidePoints[currentInd+nx]==0:
cutCellCenter[2*currentInd] = (2.*xMesh[ii] + 3.*xMesh[ii+1] - ds[currentInd+nx])*0.2
cutCellCenter[2*currentInd+1] = (3.*yMesh[jj] + 2.*yMesh[jj+1] + ds[currentInd+np])*0.2
elif insidePoints[currentInd+nx+1]==0:
cutCellCenter[2*currentInd] = (3.*xMesh[ii] + 2.*xMesh[ii+1] + ds[currentInd+nx])*0.2
cutCellCenter[2*currentInd+1] = (3.*yMesh[jj] + 2.*yMesh[jj+1] + ds[currentInd+np+1])*0.2
# Now sort boundary points in cutCellPointsInd counter-clockwise.
if ( (boundaryPoints[2*cutCellPointsInd[2*currentInd]] - cutCellCenter[2*currentInd]) *
(boundaryPoints[2*cutCellPointsInd[2*currentInd+1]+1] - cutCellCenter[2*currentInd+1]) <
(boundaryPoints[2*cutCellPointsInd[2*currentInd]+1] - cutCellCenter[2*currentInd+1]) *
(boundaryPoints[2*cutCellPointsInd[2*currentInd+1]] - cutCellCenter[2*currentInd]) ):
tempuint = cutCellPointsInd[2*currentInd]
cutCellPointsInd[2*currentInd] = cutCellPointsInd[2*currentInd+1]
cutCellPointsInd[2*currentInd+1] = tempuint
# Cut-cell normal vectors.
self.cutCellNormalVectors = numpy.zeros((np,2), dtype=numpy.double)
cutCellNormalVectors = &self.cutCellNormalVectors[0,0]
for jj in range(1,ny-1):
for ii in range(1,nx-1):
currentInd = ii + jj*nx
if insideFaces[currentInd] != 0 and insideFaces[currentInd] != 8:
cutCellNormalVectors[2*currentInd] = boundaryPoints[2*cutCellPointsInd[2*currentInd]+1] - \
boundaryPoints[2*cutCellPointsInd[2*currentInd+1]+1] # TODO CHECK
cutCellNormalVectors[2*currentInd+1] = boundaryPoints[2*cutCellPointsInd[2*currentInd+1]] - \
boundaryPoints[2*cutCellPointsInd[2*currentInd]]
tempdouble = 1./sqrt(cutCellNormalVectors[2*currentInd]**2 + cutCellNormalVectors[2*currentInd+1]**2)
cutCellNormalVectors[2*currentInd] *= tempdouble
cutCellNormalVectors[2*currentInd+1] *= tempdouble
# Continue with calculation of faces.
for ii in range(nx):
da[ii] = 0.
da[ii+(ny-2)*nx] = 0.
da[ii+(ny-1)*nx] = 0.
dai[ii] = 0.
dai[ii+(ny-2)*nx] = 0.
dai[ii+(ny-1)*nx] = 0.
for jj in range(1,ny-2):
da[jj*nx] = 0.
da[(nx-2)+jj*nx] = 0.
da[(nx-1)+jj*nx] = 0.
dai[jj*nx] = 0.
dai[(nx-2)+jj*nx] = 0.
dai[(nx-1)+jj*nx] = 0.
for jj in range(1,ny-2):
for ii in range(1,nx-2):
if insideFaces[ii+jj*nx]==8:
da[ii+jj*nx] = dxdy
dai[ii+jj*nx] = dxdyi
elif insideFaces[ii+jj*nx]==0:
da[ii+jj*nx] = 0.
dai[ii+jj*nx] = 0.
elif insideFaces[ii+jj*nx]==2:
if insidePoints[ii+jj*nx]==1:
da[ii+jj*nx] = 0.5*ds[ii+jj*nx]*ds[ii+jj*nx+np]
elif insidePoints[ii+1+jj*nx]==1:
da[ii+jj*nx] = 0.5*ds[ii+jj*nx]*ds[ii+1+jj*nx+np]
elif insidePoints[ii+(jj+1)*nx]==1:
da[ii+jj*nx] = 0.5*ds[ii+(jj+1)*nx]*ds[ii+jj*nx+np]
else:
da[ii+jj*nx] = 0.5*ds[ii+(jj+1)*nx]*ds[ii+1+jj*nx+np]
dai[ii+jj*nx] = 1./da[ii+jj*nx]
elif insideFaces[ii+jj*nx]==4:
da[ii+jj*nx] = 0.5*(ds[ii+jj*nx] + ds[ii+(jj+1)*nx])*(ds[ii+jj*nx+np] + ds[ii+1+jj*nx+np])
dai[ii+jj*nx] = 1./da[ii+jj*nx]
elif insideFaces[ii+jj*nx]==6:
if insidePoints[ii+jj*nx]==0:
da[ii+jj*nx] = dxdy-0.5*(dx-ds[ii+jj*nx])*(dy-ds[ii+jj*nx+np]) # Only for equidistant.
elif insidePoints[ii+1+jj*nx]==0:
da[ii+jj*nx] = dxdy-0.5*(dx-ds[ii+jj*nx])*(dy-ds[ii+1+jj*nx+np])
elif insidePoints[ii+(jj+1)*nx]==0:
da[ii+jj*nx] = dxdy-0.5*(dx-ds[ii+(jj+1)*nx])*(dy-ds[ii+jj*nx+np])
else:
da[ii+jj*nx] = dxdy-0.5*(dx-ds[ii+(jj+1)*nx])*(dy-ds[ii+1+jj*nx+np])
dai[ii+jj*nx] = 1./da[ii+jj*nx]
# Dual grid quantities are not affected by cut-cell stuff. Same as for rectangular.
# They are not needed for any essential
# calculation in the poisson problem, just for visualization of the charge density.
for ii in range(nx):
dst[ii] = 0.
dst[ii+(ny-1)*nx] = 0.
dst[ii+np] = 0.
dst[ii+(ny-1)*nx+np] = 0.
dsti[ii] = 0.
dsti[ii+(ny-1)*nx] = 0.
dsti[ii+np] = 0.
dsti[ii+(ny-1)*nx+np] = 0.
for jj in range(1,ny-1):
dst[jj*nx] = 0.
dst[(nx-1)+jj*nx] = 0.
dst[jj*nx+np] = 0.
dst[(nx-1)+jj*nx+np] = 0.
dsti[jj*nx] = 0.
dsti[(nx-1)+jj*nx] = 0.
dsti[jj*nx+np] = 0.
dsti[(nx-1)+jj*nx+np] = 0.
for ii in range(2,nx-2):
for jj in range(1,ny-1):
dst[ii+jj*nx] = dx # Here assuming equidistant meshes (in each direction).
dsti[ii+jj*nx] = dxi
for ii in range(1,nx-1):
for jj in range(2,ny-2):
dst[ii+jj*nx+np] = dy
dsti[ii+jj*nx+np] = dyi
for jj in range(1,ny-1):
dst[1+jj*nx] = dxHalf
dst[(nx-2)+jj*nx] = dxHalf
dsti[1+jj*nx] = dxHalfi
dsti[(nx-2)+jj*nx] = dxHalfi
for ii in range(1,nx-1):
dst[ii+nx+np] = dyHalf
dst[ii+(ny-2)*nx+np] = dyHalf
dsti[ii+nx+np] = dyHalfi
dsti[ii+(ny-2)*nx+np] = dyHalfi
for ii in range(nx):
for jj in range(ny):
# TODO Area calculation could be improved to make plots of cut cells nicer
dat[ii+jj*nx] = dst[ii+jj*nx]*dst[ii+jj*nx+np]
dati[ii+jj*nx] = dsti[ii+jj*nx]*dsti[ii+jj*nx+np]
# Standard interpolation stuff. Interpolation from edges to _inside_ grid points. Same as rectangular.
kk = 0
for ii in range(2,nx-2):
for jj in range(2,ny-2):
currentInd = ii + jj*nx
if insidePoints[currentInd]==1:
rows[kk] = currentInd
columns[kk] = currentInd
values[kk] = ds[currentInd]/(ds[currentInd-1]+ds[currentInd]) # Should work for non-equidistant grid.
kk += 1
rows[kk] = currentInd
columns[kk] = currentInd-1
values[kk] = ds[currentInd-1]/(ds[currentInd-1]+ds[currentInd])
kk += 1
currentInd += np
rows[kk] = currentInd
columns[kk] = currentInd
values[kk] = ds[currentInd]/(ds[currentInd-nx]+ds[currentInd])
kk += 1
rows[kk] = currentInd
columns[kk] = currentInd-nx
values[kk] = ds[currentInd-nx]/(ds[currentInd-nx]+ds[currentInd])
kk += 1
self.edgeToNode = spsp.csc_matrix(spsp.coo_matrix((values[:kk],(rows[:kk],columns[:kk])),shape=(2*np,2*np)))
# # Boundary interpolation for correct cut-cell fields at boundary.
# # This is the only really messy and hard part in this whole class. One has to interpolate
# # at the boundary by using the continuity conditions, which leads to lots of angles and so on...
# # I am very sure that this is in principle is much better than many other people do it (they just
# # ignore the cut cells and work on the equidistant mesh). For openEcloud we need good field
# # interpolation at the wall, so no way around it.
# # There is some stuff included to extrapolate accurately to the equidistant grid, but the last
# # step will be done in the next block.
# for jj in range(1, ny-1):
# for ii in range(1, nx-1):
# currentInd = ii+jj*nx
# if insideEdges[currentInd]==0 or insideEdges[currentInd]==2:
# cosAlpha[currentInd] = 0.
# sinAlpha[currentInd] = 0.
# else:
# # TODO check angle calculation again
# # Take average of the two cells adjescent to the edge as the edge-boundary impact angle
# temp = 0.5 * ( atan2(cutCellNormalVectors[2*currentInd+1],cutCellNormalVectors[2*currentInd]) +
# atan2(cutCellNormalVectors[2*(currentInd-nx)+1],cutCellNormalVectors[2*(currentInd-nx)]) )
# cosAlpha[currentInd] = cos(temp)
# sinAlpha[currentInd] = sin(temp)
## print ii, jj, temp/pi*180, cosAlpha[currentInd], sinAlpha[currentInd]
# if insideEdges[currentInd+np]==0 or insideEdges[currentInd+np]==2:
# cosBeta[currentInd] = 0.
# sinBeta[currentInd] = 0.
# else:
# # TODO check angle calculation again
# # Take average of the two cells adjescent to the edge as the edge-boundary impact angle
# temp = 0.5 * ( atan2(-cutCellNormalVectors[2*currentInd],cutCellNormalVectors[2*currentInd+1]) +
# atan2(-cutCellNormalVectors[2*(currentInd-1)],cutCellNormalVectors[2*(currentInd-1)+1]) )
# cosBeta[currentInd] = cos(temp)
# sinBeta[currentInd] = sin(temp)
## print ii, jj, temp/pi*180, cosBeta[currentInd], sinBeta[currentInd]
# for ii in range(nx):
# for jj in range(ny):
# currentInd = ii+jj*nx
# connectedEdgesInv[currentInd] = 0.
# for jj in range(1,ny-1):
# for ii in range(1,nx-1):
# currentInd = ii+jj*nx
# if insidePoints[currentInd] == 0:
# connectedEdgesInv[currentInd] = insideEdges[currentInd] + insideEdges[currentInd-1] + \
# insideEdges[currentInd+np] + insideEdges[currentInd-nx+np]
# if connectedEdgesInv[currentInd] > 1.:
# connectedEdgesInv[currentInd] = 1./connectedEdgesInv[ii+jj*nx]
# kk = 0
# for jj in range(1,ny-1):
# for ii in range(1,nx-1):
# currentInd = ii+jj*nx
# # Fields at inside points are unchanged.
# if insidePoints[currentInd] == 1:
# rows[kk] = currentInd
# columns[kk] = currentInd
# values[kk] = 1.
# kk += 1
# rows[kk] = currentInd+np
# columns[kk] = currentInd+np
# values[kk] = 1.
# kk += 1
# # Boundary edges.
# else:
# if insideEdges[currentInd] == 1:
# # X to x in negative x-direction.
# rows[kk] = currentInd
# columns[kk] = currentInd+1
# values[kk] = connectedEdgesInv[currentInd]*(1. - sinAlpha[currentInd]**2*dx/ds[currentInd])
# kk += 1
# # Y to x in negative x-direction.
# rows[kk] = currentInd
# columns[kk] = currentInd+np+1
# values[kk] = -connectedEdgesInv[currentInd]*(cosAlpha[currentInd]*sinAlpha[currentInd]*dx/ds[currentInd])
# kk += 1
# # Y to y in negative x-direction.