-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParticleToMeshMap.py
More file actions
180 lines (172 loc) · 8.92 KB
/
ParticleToMeshMap.py
File metadata and controls
180 lines (172 loc) · 8.92 KB
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
from math import ceil
from Vec3d import Vec3d
class ParticleToMeshMap():
@classmethod
def weight_particles_charge_to_mesh(cls, spat_mesh, particle_sources):
# Rewrite:
# forall particles {
# find nonzero weights and corresponding nodes
# charge[node] = weight(particle, node) * particle.charge
# }
dx = spat_mesh.x_cell_size
dy = spat_mesh.y_cell_size
dz = spat_mesh.z_cell_size
cell_volume = dx * dy * dz
volume_around_node = cell_volume
# 'tlf' = 'top_left_far'
for part_src in particle_sources.sources:
for p in part_src.particles:
tlf_i, tlf_x_weight = next_node_num_and_weight(p.position.x, dx)
tlf_j, tlf_y_weight = next_node_num_and_weight(p.position.y, dy)
tlf_k, tlf_z_weight = next_node_num_and_weight(p.position.z, dz)
spat_mesh.charge_density[tlf_i][tlf_j][tlf_k] += \
tlf_x_weight * tlf_y_weight * tlf_z_weight \
* p.charge / volume_around_node
spat_mesh.charge_density[tlf_i-1][tlf_j][tlf_k] += \
(1.0 - tlf_x_weight) * tlf_y_weight * tlf_z_weight \
* p.charge / volume_around_node
spat_mesh.charge_density[tlf_i][tlf_j-1][tlf_k] += \
tlf_x_weight * (1.0 - tlf_y_weight) * tlf_z_weight \
* p.charge / volume_around_node
spat_mesh.charge_density[tlf_i-1][tlf_j-1][tlf_k] += \
(1.0 - tlf_x_weight) * (1.0 - tlf_y_weight) * tlf_z_weight \
* p.charge / volume_around_node
spat_mesh.charge_density[tlf_i][tlf_j][tlf_k - 1] += \
tlf_x_weight * tlf_y_weight * (1.0 - tlf_z_weight) \
* p.charge / volume_around_node
spat_mesh.charge_density[tlf_i-1][tlf_j][tlf_k - 1] += \
(1.0 - tlf_x_weight) * tlf_y_weight * (1.0 - tlf_z_weight) \
* p.charge / volume_around_node
spat_mesh.charge_density[tlf_i][tlf_j-1][tlf_k - 1] += \
tlf_x_weight * (1.0 - tlf_y_weight) * (1.0 - tlf_z_weight) \
* p.charge / volume_around_node
spat_mesh.charge_density[tlf_i-1][tlf_j-1][tlf_k - 1] += \
(1.0 - tlf_x_weight) * (1.0 - tlf_y_weight) * \
(1.0 - tlf_z_weight) \
* p.charge / volume_around_node
@classmethod
def field_at_particle_position(cls, spat_mesh, p):
dx = spat_mesh.x_cell_size
dy = spat_mesh.y_cell_size
dz = spat_mesh.z_cell_size
# 'tlf' = 'top_left_far'
tlf_i, tlf_x_weight = next_node_num_and_weight(p.position.x, dx)
tlf_j, tlf_y_weight = next_node_num_and_weight(p.position.y, dy)
tlf_k, tlf_z_weight = next_node_num_and_weight(p.position.z, dz)
# tlf
total_field = Vec3d.zero()
field_from_node = spat_mesh.electric_field[tlf_i][tlf_j][tlf_k].times_scalar(
tlf_x_weight)
field_from_node = field_from_node.times_scalar(tlf_y_weight)
field_from_node = field_from_node.times_scalar(tlf_z_weight)
total_field = total_field.add(field_from_node)
# trf
field_from_node = spat_mesh.electric_field[tlf_i-1][tlf_j][tlf_k].times_scalar(
1.0 - tlf_x_weight)
field_from_node = field_from_node.times_scalar(tlf_y_weight)
field_from_node = field_from_node.times_scalar(tlf_z_weight)
total_field = total_field.add(field_from_node)
# blf
field_from_node = spat_mesh.electric_field[tlf_i][tlf_j - 1][tlf_k].times_scalar(
tlf_x_weight)
field_from_node = field_from_node.times_scalar(1.0 - tlf_y_weight)
field_from_node = field_from_node.times_scalar(tlf_z_weight)
total_field = total_field.add(field_from_node)
# brf
field_from_node = spat_mesh.electric_field[tlf_i-1][tlf_j-1][tlf_k].times_scalar(
1.0 - tlf_x_weight)
field_from_node = field_from_node.times_scalar(1.0 - tlf_y_weight)
field_from_node = field_from_node.times_scalar(tlf_z_weight)
total_field = total_field.add(field_from_node)
# tln
field_from_node = spat_mesh.electric_field[tlf_i][tlf_j][tlf_k-1].times_scalar(
tlf_x_weight)
field_from_node = field_from_node.times_scalar(tlf_y_weight)
field_from_node = field_from_node.times_scalar(1.0 - tlf_z_weight)
total_field = total_field.add(field_from_node)
# trn
field_from_node = spat_mesh.electric_field[tlf_i-1][tlf_j][tlf_k-1].times_scalar(
1.0 - tlf_x_weight)
field_from_node = field_from_node.times_scalar(tlf_y_weight)
field_from_node = field_from_node.times_scalar(1.0 - tlf_z_weight)
total_field = total_field.add(field_from_node)
# bln
field_from_node = spat_mesh.electric_field[tlf_i][tlf_j - 1][tlf_k-1].times_scalar(
tlf_x_weight)
field_from_node = field_from_node.times_scalar(1.0 - tlf_y_weight)
field_from_node = field_from_node.times_scalar(1.0 - tlf_z_weight)
total_field = total_field.add(field_from_node)
# brn
field_from_node = spat_mesh.electric_field[tlf_i-1][tlf_j-1][tlf_k-1].times_scalar(
1.0 - tlf_x_weight)
field_from_node = field_from_node.times_scalar(1.0 - tlf_y_weight)
field_from_node = field_from_node.times_scalar(1.0 - tlf_z_weight)
total_field = total_field.add(field_from_node)
#
return total_field
@classmethod
def force_on_particle(cls, spat_mesh, p):
dx = spat_mesh.x_cell_size
dy = spat_mesh.y_cell_size
dz = spat_mesh.z_cell_size
# 'tlf' = 'top_left_far'
tlf_i, tlf_x_weight = next_node_num_and_weight(p.position.x, dx)
tlf_j, tlf_y_weight = next_node_num_and_weight(p.position.y, dy)
tlf_k, tlf_z_weight = next_node_num_and_weight(p.position.z, dz)
# tlf
total_field = Vec3d.zero()
field_from_node = spat_mesh.electric_field[tlf_i][tlf_j][tlf_k].times_scalar(
tlf_x_weight)
field_from_node = field_from_node.times_scalar(tlf_y_weight)
field_from_node = field_from_node.times_scalar(tlf_z_weight)
total_field = total_field.add(field_from_node)
# trf
field_from_node = spat_mesh.electric_field[tlf_i-1][tlf_j][tlf_k].times_scalar(
1.0 - tlf_x_weight)
field_from_node = field_from_node.times_scalar(tlf_y_weight)
field_from_node = field_from_node.times_scalar(tlf_z_weight)
total_field = total_field.add(field_from_node)
# blf
field_from_node = spat_mesh.electric_field[tlf_i][tlf_j - 1][tlf_k].times_scalar(
tlf_x_weight)
field_from_node = field_from_node.times_scalar(1.0 - tlf_y_weight)
field_from_node = field_from_node.times_scalar(tlf_z_weight)
total_field = total_field.add(field_from_node)
# brf
field_from_node = spat_mesh.electric_field[tlf_i-1][tlf_j-1][tlf_k].times_scalar(
1.0 - tlf_x_weight)
field_from_node = field_from_node.times_scalar(1.0 - tlf_y_weight)
field_from_node = field_from_node.times_scalar(tlf_z_weight)
total_field = total_field.add(field_from_node)
# tln
field_from_node = spat_mesh.electric_field[tlf_i][tlf_j][tlf_k-1].times_scalar(
tlf_x_weight)
field_from_node = field_from_node.times_scalar(tlf_y_weight)
field_from_node = field_from_node.times_scalar(1.0 - tlf_z_weight)
total_field = total_field.add(field_from_node)
# trn
field_from_node = spat_mesh.electric_field[tlf_i-1][tlf_j][tlf_k-1].times_scalar(
1.0 - tlf_x_weight)
field_from_node = field_from_node.times_scalar(tlf_y_weight)
field_from_node = field_from_node.times_scalar(1.0 - tlf_z_weight)
total_field = total_field.add(field_from_node)
# bln
field_from_node = spat_mesh.electric_field[tlf_i][tlf_j - 1][tlf_k-1].times_scalar(
tlf_x_weight)
field_from_node = field_from_node.times_scalar(1.0 - tlf_y_weight)
field_from_node = field_from_node.times_scalar(1.0 - tlf_z_weight)
total_field = total_field.add(field_from_node)
# brn
field_from_node = spat_mesh.electric_field[tlf_i-1][tlf_j-1][tlf_k-1].times_scalar(
1.0 - tlf_x_weight)
field_from_node = field_from_node.times_scalar(1.0 - tlf_y_weight)
field_from_node = field_from_node.times_scalar(1.0 - tlf_z_weight)
total_field = total_field.add(field_from_node)
#
force = total_field.times_scalar(p.charge)
return force
def next_node_num_and_weight(x, grid_step):
x_in_grid_units = x / grid_step
next_node = ceil(x_in_grid_units)
weight = 1.0 - (next_node - x_in_grid_units)
return (next_node, weight)