-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathneumann_matrixinv.py
153 lines (95 loc) · 5.24 KB
/
neumann_matrixinv.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
import numpy
import scipy
import sys
import dielectric_tools
#check for proper usage
if len( sys.argv ) != 5:
print "usage: python %s h Nx Ny Nz" % sys.argv[0]
exit(1)
#setup parameters
lbkt = 1.0
h = int( sys.argv[1] )
Nx = int( sys.argv[2] )
Ny = int( sys.argv[3] )
Nz = int( sys.argv[4] )
#allocate memory
charge_potential = numpy.zeros( [Nz, Ny, Nx], dtype=float )
tmp_grid_fft = numpy.zeros( [Nz, Ny, Nx/2+1], dtype=complex )
boundary_positions = numpy.zeros( [0,3], dtype=int )
#initialize boundary geometry
for z in numpy.arange(0, Nz):
for y in numpy.arange(0, Ny):
for x in numpy.arange(0, Nx):
r_sq = (x-Nx/2.)**2 + (y-Ny/2.)**2 + (z-Nz/2.)**2
if r_sq >= 4.**2 and r_sq < 5.**2:
boundary_positions.resize( [ boundary_positions.shape[0]+1, 3 ] )
boundary_positions[-1] = [z, y, x]
#allocate boundary potential/charge array and element interaction matrix
boundary_potential_charge = numpy.zeros( len(boundary_positions), dtype=float )
boundary_interaction_matrix = numpy.matrix( numpy.zeros( [ len(boundary_positions), len(boundary_positions) ], dtype=float ), dtype=float, copy=False )
#calculate greensfunction
charge_potential[0,0,0] = 1.0
tmp_grid_fft = numpy.fft.rfftn(charge_potential)
for z in numpy.arange(0, Nz):
for y in numpy.arange(0, Ny):
for x in numpy.arange(0, Nx/2+1):
if (x,y,z) == (0,0,0):
tmp_grid_fft[z,y,x] = 0.0
else:
tmp_grid_fft[z,y,x] = tmp_grid_fft[z,y,x] * (-4.0) * numpy.pi * lbkt * h**2 * 0.5 / ( numpy.cos( 2.0 * numpy.pi * x / Nx ) + numpy.cos( 2.0 * numpy.pi * y / Ny ) + numpy.cos( 2.0 * numpy.pi * z / Nz ) - 3.0 )
charge_potential = numpy.fft.irfftn( tmp_grid_fft )
#populate boundary element interaction matrix and invert
E = numpy.zeros(3, dtype=float)
for i in numpy.arange( 0, len(boundary_positions) ):
for k in numpy.arange( 0, len(boundary_positions) ):
d_up = numpy.subtract( boundary_positions[i]+[1,0,0], boundary_positions[k] )
d_up = numpy.mod( d_up, [ Nx, Ny, Nz ] )
d_dn = numpy.subtract( boundary_positions[i]+[-1,0,0], boundary_positions[k] )
d_dn = numpy.mod( d_dn, [ Nx, Ny, Nz ] )
E[0] = -( charge_potential[ d_up[2], d_up[1], d_up[0] ] - charge_potential[ d_dn[2], d_dn[1], d_dn[0] ] ) / ( 2.0 * h )
d_up = numpy.subtract( boundary_positions[i]+[0,1,0], boundary_positions[k] )
d_up = numpy.mod( d_up, [ Nx, Ny, Nz ] )
d_dn = numpy.subtract( boundary_positions[i]+[0,-1,0], boundary_positions[k] )
d_dn = numpy.mod( d_dn, [ Nx, Ny, Nz ] )
E[1] = -( charge_potential[ d_up[2], d_up[1], d_up[0] ] - charge_potential[ d_dn[2], d_dn[1], d_dn[0] ] ) / ( 2.0 * h )
d_up = numpy.subtract( boundary_positions[i]+[0,0,1], boundary_positions[k] )
d_up = numpy.mod( d_up, [ Nx, Ny, Nz ] )
d_dn = numpy.subtract( boundary_positions[i]+[0,0,-1], boundary_positions[k] )
d_dn = numpy.mod( d_dn, [ Nx, Ny, Nz ] )
E[2] = -( charge_potential[ d_up[2], d_up[1], d_up[0] ] - charge_potential[ d_dn[2], d_dn[1], d_dn[0] ] ) / ( 2.0 * h )
n = numpy.array( boundary_positions[i] - [Nx/2.0, Ny/2.0, Nz/2.0] )
n /= numpy.sqrt( numpy.square(n).sum() )
boundary_interaction_matrix[i,k] = E.dot(n)
print "Matrix has size", boundary_interaction_matrix.shape
boundary_interaction_matrix_inv = numpy.linalg.inv( boundary_interaction_matrix )
#calculate potential without boundaries
for z in numpy.arange(0, Nz):
for y in numpy.arange(0, Ny):
for x in numpy.arange(0, Nx):
charge_potential[z,y,x] = x
#collect vector containing boundary normal e-field
for i in numpy.arange( 0, len(boundary_positions) ):
boundary_potential_charge[i] = charge_potential[ boundary_positions[i,2], boundary_positions[i,1], boundary_positions[i,0] ]
#calculate influence charge density
boundary_potential_charge = -boundary_interaction_matrix_inv.dot(boundary_potential_charge)
#expand it into whole field and output
charge_potential = numpy.zeros( [Nz, Ny, Nx], dtype=float )
for i in numpy.arange( 0, len(boundary_positions) ):
charge_potential[ boundary_positions[i,2], boundary_positions[i,1], boundary_positions[i,0] ] = boundary_potential_charge[0,i]
dielectric_tools.write_scalar_vtk( charge_potential, "influence_charge.vtk" )
#calculate potential of influence charge
tmp_grid_fft = numpy.fft.rfftn(charge_potential)
for z in numpy.arange(0, Nz):
for y in numpy.arange(0, Ny):
for x in numpy.arange(0, Nx/2+1):
if (x,y,z) == (0,0,0):
tmp_grid_fft[z,y,x] = 0.0
else:
tmp_grid_fft[z,y,x] = tmp_grid_fft[z,y,x] * (-4.0) * numpy.pi * lbkt * h**2 * 0.5 / ( numpy.cos( 2.0 * numpy.pi * x / Nx ) + numpy.cos( 2.0 * numpy.pi * y / Ny ) + numpy.cos( 2.0 * numpy.pi * z / Nz ) - 3.0 )
charge_potential = numpy.fft.irfftn( tmp_grid_fft )
#add potential
for z in numpy.arange(0, Nz):
for y in numpy.arange(0, Ny):
for x in numpy.arange(0, Nx):
charge_potential[z,y,x] += x
dielectric_tools.write_scalar_vtk( charge_potential, "potential_neumann.vtk" )