-
Notifications
You must be signed in to change notification settings - Fork 10
/
node.go
executable file
·323 lines (281 loc) · 7.63 KB
/
node.go
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
package vinamax
import (
"log"
"math"
)
type node struct {
//8 pointers to subnodes (if necessary)
//top/bottom left/right front/back
tlb *node
tlf *node
trb *node
trf *node
blb *node
blf *node
brb *node
brf *node
origin vector //the origin of the cube
diameter float64 //the diameter
com vector //centreofmagnetisation
number int64 //numberofparticles
lijst []*particle //lijst met alle particles
volume float64 //the total volume of all particles in the node
m vector //magnetisation of the node
}
func ReturnParticle(num int) *particle {
if num > len(Universe.lijst) {
log.Fatal("there aren't that many particle")
}
return Universe.lijst[num]
}
//adds particle to node
func (n *node) add(p *particle) {
n.lijst = append(n.lijst, p)
}
//center of magnetisation
func (n *node) calculatecom() {
comx := 0.
comy := 0.
comz := 0.
total := 0.
prefactor := 0.
for i := range n.lijst {
radius := n.lijst[i].r
prefactor = n.lijst[i].msat * cube(radius)
comx += n.lijst[i].x * prefactor
comy += n.lijst[i].y * prefactor
comz += n.lijst[i].z * prefactor
total += prefactor
}
n.com = vector{comx / total, comy / total, comz / total}
}
//descends into the tree, needed for Maketree()
func (w *node) descend() {
w.calculatecom()
if w.number > 1 {
//initialises the 8 subnodes
//tlb
pos := vector{w.origin[0] - w.diameter/4, w.origin[1] + w.diameter/4, w.origin[2] + w.diameter/4}
w.tlb = new(node)
w.tlb.origin = pos
w.tlb.diameter = w.diameter / 2
//tlf
pos = vector{w.origin[0] - w.diameter/4, w.origin[1] + w.diameter/4, w.origin[2] - w.diameter/4}
w.tlf = new(node)
w.tlf.origin = pos
w.tlf.diameter = w.diameter / 2
//trb
pos = vector{w.origin[0] + w.diameter/4, w.origin[1] + w.diameter/4, w.origin[2] + w.diameter/4}
w.trb = new(node)
w.trb.origin = pos
w.trb.diameter = w.diameter / 2
//trf
pos = vector{w.origin[0] + w.diameter/4, w.origin[1] + w.diameter/4, w.origin[2] - w.diameter/4}
w.trf = new(node)
w.trf.origin = pos
w.trf.diameter = w.diameter / 2
//blb
pos = vector{w.origin[0] - w.diameter/4, w.origin[1] - w.diameter/4, w.origin[2] + w.diameter/4}
w.blb = new(node)
w.blb.origin = pos
w.blb.diameter = w.diameter / 2
//blf
pos = vector{w.origin[0] - w.diameter/4, w.origin[1] - w.diameter/4, w.origin[2] - w.diameter/4}
w.blf = new(node)
w.blf.origin = pos
w.blf.diameter = w.diameter / 2
//brb
pos = vector{w.origin[0] + w.diameter/4, w.origin[1] - w.diameter/4, w.origin[2] + w.diameter/4}
w.brb = new(node)
w.brb.origin = pos
w.brb.diameter = w.diameter / 2
//brf
pos = vector{w.origin[0] + w.diameter/4, w.origin[1] - w.diameter/4, w.origin[2] - w.diameter/4}
w.brf = new(node)
w.brf.origin = pos
w.brf.diameter = w.diameter / 2
//for alle particles in node
for i := range w.lijst {
plaats := w.where(vector{w.lijst[i].x, w.lijst[i].y, w.lijst[i].z})
switch plaats {
case 0:
w.tlb.number += 1
w.tlb.add(w.lijst[i])
//fmt.Println("particle at ",vector{w.lijst[i].x, w.lijst[i].y, w.lijst[i].z}," was put at tlb")
case 1:
w.tlf.number += 1
w.tlf.add(w.lijst[i])
//fmt.Println("particle at ",vector{w.lijst[i].x, w.lijst[i].y, w.lijst[i].z}," was put at tlf")
case 2:
w.trb.number += 1
w.trb.add(w.lijst[i])
//fmt.Println("particle at ",vector{w.lijst[i].x, w.lijst[i].y, w.lijst[i].z}," was put at trb")
case 3:
w.trf.number += 1
w.trf.add(w.lijst[i])
//fmt.Println("particle at ",vector{w.lijst[i].x, w.lijst[i].y, w.lijst[i].z}," was put at trf")
case 4:
w.blb.number += 1
w.blb.add(w.lijst[i])
//fmt.Println("particle at ",vector{w.lijst[i].x, w.lijst[i].y, w.lijst[i].z}," was put at blb")
case 5:
w.blf.number += 1
w.blf.add(w.lijst[i])
//fmt.Println("particle at ",vector{w.lijst[i].x, w.lijst[i].y, w.lijst[i].z}," was put at blf")
case 6:
w.brb.number += 1
w.brb.add(w.lijst[i])
//fmt.Println("particle at ",vector{w.lijst[i].x, w.lijst[i].y, w.lijst[i].z}," was put at brb")
case 7:
w.brf.number += 1
w.brf.add(w.lijst[i])
//fmt.Println("particle at ",vector{w.lijst[i].x, w.lijst[i].y, w.lijst[i].z}," was put at brf")
}
}
//for every subnode
//how many are there in the subnode?
if w.tlb.number > 1 {
w.tlb.descend()
}
if w.tlf.number > 1 {
w.tlf.descend()
}
if w.trb.number > 1 {
w.trb.descend()
}
if w.trf.number > 1 {
w.trf.descend()
}
if w.blb.number > 1 {
w.blb.descend()
}
if w.blf.number > 1 {
w.blf.descend()
}
if w.brb.number > 1 {
w.brb.descend()
}
if w.brf.number > 1 {
w.brf.descend()
}
}
}
//Build the tree needed for the FMM method, descends in the "Universe" node
func Maketree() {
treecalled = true
Universe.descend()
Universe.calculatevolume()
}
//returns the position of a particle in a node (in terms of subnode position), or -1 if the particle is not in the node
func (n node) where(position vector) int {
//if not in node
if position[0] > n.origin[0]+n.diameter/2 || position[0] < n.origin[0]-n.diameter/2 || position[1] > n.origin[1]+n.diameter/2 || position[1] < n.origin[1]-n.diameter/2 || position[2] > n.origin[2]+n.diameter/2 || position[2] < n.origin[2]-n.diameter/2 {
return -1
}
if position[0] >= n.origin[0] && position[1] >= n.origin[1] && position[2] >= n.origin[2] {
//trb
return 2
}
if position[0] >= n.origin[0] && position[1] >= n.origin[1] && position[2] < n.origin[2] {
//trf
return 3
}
if position[0] >= n.origin[0] && position[1] < n.origin[1] && position[2] >= n.origin[2] {
//brb
return 6
}
if position[0] >= n.origin[0] && position[1] < n.origin[1] && position[2] < n.origin[2] {
//brf
return 7
}
if position[0] < n.origin[0] && position[1] >= n.origin[1] && position[2] >= n.origin[2] {
//tlb
return 0
}
if position[0] < n.origin[0] && position[1] >= n.origin[1] && position[2] < n.origin[2] {
//tlf
return 1
}
if position[0] < n.origin[0] && position[1] < n.origin[1] && position[2] >= n.origin[2] {
//blb
return 4
}
if position[0] < n.origin[0] && position[1] < n.origin[1] && position[2] < n.origin[2] {
//blf
return 5
}
return -1
}
//calculates the volumes
func (w *node) calculatevolume() {
if w.number > 1 {
//for every subnode
w.tlb.calculatevolume()
w.volume += w.tlb.volume
w.tlf.calculatevolume()
w.volume += w.tlf.volume
w.trb.calculatevolume()
w.volume += w.trb.volume
w.trf.calculatevolume()
w.volume += w.trf.volume
w.blb.calculatevolume()
w.volume += w.blb.volume
w.blf.calculatevolume()
w.volume += w.blf.volume
w.brb.calculatevolume()
w.volume += w.brb.volume
w.brf.calculatevolume()
w.volume += w.brf.volume
}
if w.number == 1 {
radius := w.lijst[0].r
w.volume = 4. / 3. * math.Pi * cube(radius)
}
if w.number == 0 {
w.volume = 0.
}
}
//calculates the magnetisation of a node
func (w *node) calculatem() {
switch w.number {
case 0:
w.m = vector{0., 0., 0.}
case 1:
w.m = w.lijst[0].m.times(w.lijst[0].msat * w.volume)
default:
w.m = vector{0., 0., 0.}
//for every subnode
if w.tlb.number > 0 {
w.tlb.calculatem()
w.m = w.m.add(w.tlb.m)
}
if w.tlf.number > 0 {
w.tlf.calculatem()
w.m = w.m.add(w.tlf.m)
}
if w.trb.number > 0 {
w.trb.calculatem()
w.m = w.m.add(w.trb.m)
}
if w.trf.number > 0 {
w.trf.calculatem()
w.m = w.m.add(w.trf.m)
}
if w.blb.number > 0 {
w.blb.calculatem()
w.m = w.m.add(w.blb.m)
}
if w.blf.number > 0 {
w.blf.calculatem()
w.m = w.m.add(w.blf.m)
}
if w.brb.number > 0 {
w.brb.calculatem()
w.m = w.m.add(w.brb.m)
}
if w.brf.number > 0 {
w.brf.calculatem()
w.m = w.m.add(w.brf.m)
}
}
}