-
Notifications
You must be signed in to change notification settings - Fork 0
/
oneregion.go
67 lines (54 loc) · 1.55 KB
/
oneregion.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
package engine
import (
"fmt"
"github.com/mumax/3/cuda"
"github.com/mumax/3/data"
"github.com/mumax/3/util"
)
func sOneRegion(q Quantity, r int) *sOneReg {
util.Argument(q.NComp() == 1)
return &sOneReg{oneReg{q, r}}
}
func vOneRegion(q Quantity, r int) *vOneReg {
util.Argument(q.NComp() == 3)
return &vOneReg{oneReg{q, r}}
}
type sOneReg struct{ oneReg }
func (q *sOneReg) Average() float64 { return q.average()[0] }
type vOneReg struct{ oneReg }
func (q *vOneReg) Average() data.Vector { return unslice(q.average()) }
// represents a new quantity equal to q in the given region, 0 outside.
type oneReg struct {
parent Quantity
region int
}
func (q *oneReg) NComp() int { return q.parent.NComp() }
func (q *oneReg) Name() string { return fmt.Sprint(q.parent.Name(), ".region", q.region) }
func (q *oneReg) Unit() string { return q.parent.Unit() }
func (q *oneReg) Mesh() *data.Mesh { return q.parent.Mesh() }
// returns a new slice equal to q in the given region, 0 outside.
func (q *oneReg) Slice() (*data.Slice, bool) {
src, r := q.parent.Slice()
if r {
defer cuda.Recycle(src)
}
out := cuda.Buffer(q.NComp(), q.Mesh().Size())
cuda.RegionSelect(out, src, regions.Gpu(), byte(q.region))
return out, true
}
func (q *oneReg) average() []float64 {
slice, r := q.Slice()
if r {
defer cuda.Recycle(slice)
}
avg := sAverageUniverse(slice)
sDiv(avg, regions.volume(q.region))
return avg
}
func (q *oneReg) Average() []float64 { return q.average() }
// slice division
func sDiv(v []float64, x float64) {
for i := range v {
v[i] /= x
}
}