-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMain.py
239 lines (179 loc) · 7.95 KB
/
Main.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
import vtk
import concurrent.futures
import cupy as cp
import numpy as np
from vtk.util import numpy_support
import ParticlePusher
import MaxwellSolver
import Fields
import Constants
import FieldGather
import ParticleDestributionValues
"""-------------------constants----------------------------------------------------------------------"""
Nx = Constants.Nx # number of cells in x
Ny = Constants.Ny # number of cells in x
Nz = Constants.Nz # number of cells in x
dx = Constants.dx # stepsize x
dy = Constants.dy # stepsize y
dz = Constants.dz # stepsize z
t_i = Constants.t_i # initial t
t_f = Constants.t_f # final t
dt = Constants.dt # stepsize t
n_e = Constants.n_e # number of negative particles per macroparticle
n_p = Constants.n_p # number of positive particles per macroparticle
e_ = Constants.e_ # elementary charge
m_e = Constants.m_e # mass of negative particles
m_p = Constants.m_p # mass of positive particles
"""-------------------constants----------------------------------------------------------------------"""
# create a grid
XX = cp.arange(0, Nx*dx, dx)
YY = cp.arange(0, Ny*dy, dy)
ZZ = cp.arange(0, Nz*dz, dz)
X, Y, Z = cp.meshgrid(XX, YY, ZZ)
R_e = Fields.R_e
V_e = Fields.V_e
R_p = Fields.R_p
V_p = Fields.V_p
E = Fields.EInitial(X, Y, Z)
B = Fields.BInitial(X, Y, Z)
J = Fields.JInitial(X, Y, Z)
E_e = []
B_e = []
E_p = []
B_p = []
N_e = ParticleDestributionValues.N_e
N_p = ParticleDestributionValues.N_p
t = t_i
while t <= t_f:
FG = FieldGather.FieldGather(R_e, V_e, R_p, V_p, J*0., N_e, N_p)
J = FG[0]
R_e = FG[1]
R_p = FG[2]
V_e = FG[3]
V_p = FG[4]
N_e = FG[5]
N_p = FG[6]
Ge = cp.asnumpy(FG[7])
Gp = cp.asnumpy(FG[8])
EM = MaxwellSolver.MaxwellSolver(R_e, V_e, R_p, V_p, E, B, J)
del(E)
del(B)
del(E_e)
del(B_e)
del(E_p)
del(B_p)
E = EM[0]
B = EM[1]
E_e = EM[2]
B_e = EM[3]
E_p = EM[4]
B_p = EM[5]
# electron and ion particle pusher on seperate threads
with concurrent.futures.ThreadPoolExecutor() as executor:
electron = executor.submit(ParticlePusher.Particle, -e_, m_e, n_e, R_e, V_e, E_e, B_e)
proton = executor.submit(ParticlePusher.Particle, e_, m_p, n_p, R_p, V_p, E_p, B_p)
R_e = electron.result()[0]
V_e = electron.result()[1]
R_p = proton.result()[0]
V_p = proton.result()[1]
# ---------------------------------------------------plotting---------------------------------------------------------------------------------
if int(t/dt)%4==0:
print("t=" + str(t))
BDir = 'G:/Quad30Vel0/NUMPY/B/B_t=' + str(int(t/dt))
BNP = cp.asnumpy(cp.array(B))
cp.save(BDir, BNP)
vtype = vtk.util.numpy_support.get_vtk_array_type(BNP[0].dtype)
vcomponents = 3
imageData = vtk.vtkImageData()
imageData.SetSpacing(1.0, 1.0, 1.0)
imageData.SetOrigin(0.0, 0.0, 0.0)
imageData.SetDimensions(Nz, Ny, Nx)
imageData.AllocateScalars(vtype, vcomponents)
vtk_data_array = numpy_support.numpy_to_vtk(num_array=np.transpose(BNP).ravel(), deep=True, array_type=vtype)
vtk_data_array.SetNumberOfComponents(3)
vtk_data_array.SetName("Magnetic Field")
imageData.GetPointData().SetScalars(vtk_data_array)
writer = vtk.vtkXMLImageDataWriter()
writer.SetInputData(imageData)
writer.SetFileName("G:/Quad30Vel0/VTK/B/B" + str(int(t/dt)) + ".vtk")
writer.Write()
EDir = 'G:/Quad30Vel0/NUMPY/E/E_t=' + str(int(t/dt))
ENP = cp.asnumpy(cp.array(E))
cp.save(EDir, ENP)
vtype = vtk.util.numpy_support.get_vtk_array_type(ENP[0].dtype)
vcomponents = 3
imageData = vtk.vtkImageData()
imageData.SetSpacing(1.0, 1.0, 1.0)
imageData.SetOrigin(0.0, 0.0, 0.0)
imageData.SetDimensions(Nz, Ny, Nx)
imageData.AllocateScalars(vtype, vcomponents)
vtk_data_array = numpy_support.numpy_to_vtk(num_array=np.transpose(ENP).ravel(), deep=True, array_type=vtype)
vtk_data_array.SetNumberOfComponents(3)
vtk_data_array.SetName("Electric Field")
imageData.GetPointData().SetScalars(vtk_data_array)
writer = vtk.vtkXMLImageDataWriter()
writer.SetInputData(imageData)
writer.SetFileName("G:/Quad30Vel0/VTK/E/E" + str(int(t/dt)) + ".vtk")
writer.Write()
JDir = 'G:/Quad30Vel0/NUMPY/J/J_t=' + str(int(t/dt))
JNP = cp.asnumpy(cp.array(J))
cp.save(JDir, JNP)
vtype = vtk.util.numpy_support.get_vtk_array_type(JNP[0].dtype)
vcomponents = 3
imageData = vtk.vtkImageData()
imageData.SetSpacing(1.0, 1.0, 1.0)
imageData.SetOrigin(0.0, 0.0, 0.0)
imageData.SetDimensions(Nz, Ny, Nx)
imageData.AllocateScalars(vtype, vcomponents)
vtk_data_array = numpy_support.numpy_to_vtk(num_array=np.transpose(JNP).ravel(), deep=True, array_type=vtype)
vtk_data_array.SetNumberOfComponents(3)
vtk_data_array.SetName("J")
imageData.GetPointData().SetScalars(vtk_data_array)
writer = vtk.vtkXMLImageDataWriter()
writer.SetInputData(imageData)
writer.SetFileName("G:/Quad30Vel0/VTK/J/J" + str(int(t/dt)) + ".vtk")
writer.Write()
vtype = vtk.util.numpy_support.get_vtk_array_type(Ge[0].dtype)
vcomponents = 3
imageData = vtk.vtkImageData()
imageData.SetSpacing(1.0, 1.0, 1.0)
imageData.SetOrigin(0.0, 0.0, 0.0)
imageData.SetDimensions(Nz, Ny, Nx)
imageData.AllocateScalars(vtype, vcomponents)
vtk_data_array = numpy_support.numpy_to_vtk(num_array=np.transpose(Ge).ravel(), deep=True, array_type=vtype)
vtk_data_array.SetNumberOfComponents(3)
vtk_data_array.SetName("Ge")
imageData.GetPointData().SetScalars(vtk_data_array)
writer = vtk.vtkXMLImageDataWriter()
writer.SetInputData(imageData)
writer.SetFileName("G:/Quad30Vel0/VTK/Ge/Ge" + str(int(t/dt)) + ".vtk")
writer.Write()
vtype = vtk.util.numpy_support.get_vtk_array_type(Gp[0].dtype)
vcomponents = 3
imageData = vtk.vtkImageData()
imageData.SetSpacing(1.0, 1.0, 1.0)
imageData.SetOrigin(0.0, 0.0, 0.0)
imageData.SetDimensions(Nz, Ny, Nx)
imageData.AllocateScalars(vtype, vcomponents)
vtk_data_array = numpy_support.numpy_to_vtk(num_array=np.transpose(Gp).ravel(), deep=True, array_type=vtype)
vtk_data_array.SetNumberOfComponents(3)
vtk_data_array.SetName("Gp")
imageData.GetPointData().SetScalars(vtk_data_array)
writer = vtk.vtkXMLImageDataWriter()
writer.SetInputData(imageData)
writer.SetFileName("G:/Quad30Vel0/VTK/Gp/Gp" + str(int(t/dt)) + ".vtk")
writer.Write()
R_eDir = 'G:/Quad30Vel0/NUMPY/R_e/R_e_t=' + str(int(t/dt))
R_eNP = cp.asnumpy(R_e)
cp.save(R_eDir, R_eNP)
V_eDir = 'G:/Quad30Vel0/NUMPY/V_e/V_e_t=' + str(int(t/dt))
V_eNP = cp.asnumpy(V_e)
cp.save(V_eDir, V_eNP)
R_pDir = 'G:/Quad30Vel0/NUMPY/R_p/R_p_t=' + str(int(t/dt))
R_pNP = cp.asnumpy(R_p)
cp.save(R_pDir, R_pNP)
V_pDir = 'G:/Quad30Vel0/NUMPY/V_p/V_p_t=' + str(int(t/dt))
V_pNP = cp.asnumpy(V_p)
cp.save(V_pDir, V_pNP)
# ---------------------------------------------------plotting---------------------------------------------------------------------------------
t += dt