/
quantity.go
89 lines (79 loc) · 1.66 KB
/
quantity.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
package engine
import (
"github.com/kuchkin/mumax3-gneb/cuda"
"github.com/kuchkin/mumax3-gneb/data"
"reflect"
)
// Arbitrary physical quantity.
type Quantity interface {
NComp() int
EvalTo(dst *data.Slice)
}
func MeshSize() [3]int {
return Mesh().Size()
}
func SizeOf(q Quantity) [3]int {
// quantity defines its own, custom, implementation:
if s, ok := q.(interface {
Mesh() *data.Mesh
}); ok {
return s.Mesh().Size()
}
// otherwise: default mesh
return MeshSize()
}
func AverageOf(q Quantity) []float64 {
// quantity defines its own, custom, implementation:
if s, ok := q.(interface {
average() []float64
}); ok {
return s.average()
}
// otherwise: default mesh
buf := ValueOf(q)
defer cuda.Recycle(buf)
return sAverageMagnet(buf)
}
func NameOf(q Quantity) string {
// quantity defines its own, custom, implementation:
if s, ok := q.(interface {
Name() string
}); ok {
return s.Name()
}
return "unnamed." + reflect.TypeOf(q).String()
}
func UnitOf(q Quantity) string {
// quantity defines its own, custom, implementation:
if s, ok := q.(interface {
Unit() string
}); ok {
return s.Unit()
}
return "?"
}
func MeshOf(q Quantity) *data.Mesh {
// quantity defines its own, custom, implementation:
if s, ok := q.(interface {
Mesh() *data.Mesh
}); ok {
return s.Mesh()
}
return Mesh()
}
func ValueOf(q Quantity) *data.Slice {
// TODO: check for Buffered() implementation
buf := cuda.Buffer(q.NComp(), SizeOf(q))
q.EvalTo(buf)
return buf
}
// Temporary shim to fit Slice into EvalTo
func EvalTo(q interface {
Slice() (*data.Slice, bool)
}, dst *data.Slice) {
v, r := q.Slice()
if r {
defer cuda.Recycle(v)
}
data.Copy(dst, v)
}