-
Notifications
You must be signed in to change notification settings - Fork 1
/
average.go
50 lines (44 loc) · 1.1 KB
/
average.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
package engine
// Averaging of quantities over entire universe or just magnet.
import (
"github.com/mumax/3/cuda"
"github.com/mumax/3/data"
)
// average of quantity over universe
func qAverageUniverse(q Quantity) []float64 {
s, recycle := q.Slice()
if recycle {
defer cuda.Recycle(s)
}
return sAverageUniverse(s)
}
// average of slice over universe
func sAverageUniverse(s *data.Slice) []float64 {
nCell := float64(prod(s.Size()))
avg := make([]float64, s.NComp())
for i := range avg {
avg[i] = float64(cuda.Sum(s.Comp(i))) / nCell
}
return avg
}
// average of slice over the magnet volume
func sAverageMagnet(s *data.Slice) []float64 {
if geometry.Gpu().IsNil() {
return sAverageUniverse(s)
} else {
avg := make([]float64, s.NComp())
for i := range avg {
avg[i] = float64(cuda.Dot(s.Comp(i), geometry.Gpu())) / magnetNCell()
}
return avg
}
}
// number of cells in the magnet.
// not necessarily integer as cells can have fractional volume.
func magnetNCell() float64 {
if geometry.Gpu().IsNil() {
return float64(Mesh().NCell())
} else {
return float64(cuda.Sum(geometry.Gpu()))
}
}