-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfmm.go
173 lines (148 loc) · 3.32 KB
/
fmm.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
package vinamax2
import (
"fmt"
"math"
"runtime"
"time"
)
// FMM globals
var (
Root Cell // roots the entire FMM tree
Level [][]*Cell // for each level of the FMM tree: all cells on that level. Root = level 0
Particles []*Particle // all particles, to be manipulated via Root.AddParticle
FMMOrder = 0
Proximity = 1.1
// statistics:
totalPartners int
totalNear int
totalCells int
)
// Add a particle to the global FMM tree and Particles list
func AddParticle(p *Particle) {
Root.addParticle(p)
Particles = append(Particles, p)
}
// Calculates the magnetostatic field of all Particles.
func CalcDemag() {
Root.updateM()
Root.b0 = Vector{0, 0, 0}
Root.dbdx = Vector{0, 0, 0}
Root.dbdy = Vector{0, 0, 0}
Root.dbdz = Vector{0, 0, 0}
switch FMMOrder {
default:
panic(fmt.Sprint("invalid FMMOrder:", FMMOrder))
case 0:
Root.updateBdemag0(&Root) // we abuse root as parent, it only propagates zero fields
case 1:
Root.updateBdemag1(&Root)
case -1:
CalcDemagBrute()
}
}
var (
ch chan *Cell
done chan struct{}
)
// EXPERIMENTAL
func CalcDemagParallel() {
NCPU := 3
if ch == nil {
ch = make(chan *Cell, 8*8)
done = make(chan struct{}, 8*8)
runtime.GOMAXPROCS(NCPU)
for i := 0; i < NCPU; i++ {
go func() {
Log("spinning up worker")
for c := range ch {
c.updateBdemag0(&Root)
done <- struct{}{}
}
}()
}
}
Root.updateM()
Root.b0 = Vector{0, 0, 0}
for _, c := range Root.child {
for _, c := range c.child {
ch <- c
}
}
for _, c := range Root.child {
for _, _ = range c.child {
<-done
}
}
}
func CalcDemagBrute() {
for _, p := range Particles {
p.b = p.BruteDemag()
}
}
// Initializes the global FMM variables Root, Level
// with an FMM octree, nLevels deep (8^(nLevels-1)) base cells.
func InitFMM(worldSize Vector, nLevels int) {
Level = make([][]*Cell, nLevels)
Root = Cell{size: worldSize}
start := time.Now()
Log("dividing", nLevels, "levels", math.Pow(8, float64(nLevels-1)), "base cells...")
Root.Divide(nLevels)
Log(time.Since(start))
}
func InitFMM2() {
PruneTree()
CalculateCenterOfMags()
start := time.Now()
Log("finding partners...")
Root.FindPartners(Level[0])
Log(time.Since(start))
printFMMStats()
}
func printFMMStats() {
nLeaf := int(math.Pow(8, float64(len(Level)-1)) + 0.5)
Log(totalCells, "cells, avg", totalPartners/totalCells, "partners/cell, avg", totalNear/nLeaf, "near/leaf")
}
//Prunes all the empty cells from the fmm tree
func PruneTree() {
prune(&Root)
}
//recursively checks if a child cell contains particles and if not prunes them from the FMMtree
func prune(c *Cell) {
for _, d := range c.child {
if d != nil {
if d.Len() == 0 {
d = nil
} else {
if d.Len() == 1 && c.Len() == 1 {
d = nil
} else {
prune(d)
}
}
}
}
}
func CalculateCenterOfMags() {
updatemoments(&Root)
updatecom(&Root)
}
//recursively calculates com of a cell
func updatecom(c *Cell) Vector {
c.centerofmag = Vector{0., 0., 0.}
if c.IsLeaf() {
for _, p := range c.particles {
c.centerofmag = c.centerofmag.MAdd(p.volume()*p.msat, p.center)
}
} else {
for _, d := range c.child {
if d != nil {
c.centerofmag = c.centerofmag.MAdd(d.Moment(), updatecom(d))
}
}
}
c.centerofmag = c.centerofmag.Div(c.Moment())
if c.Moment() == 0 {
return c.center
}
return c.centerofmag
}