-
Notifications
You must be signed in to change notification settings - Fork 1
/
magnetization.go
145 lines (122 loc) · 4.1 KB
/
magnetization.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
package engine
import (
"github.com/mumax/3/cuda"
"github.com/mumax/3/data"
"github.com/mumax/3/script"
"github.com/mumax/3/util"
"reflect"
)
var M magnetization // reduced magnetization (unit length)
func init() { DeclLValue("m", &M, `Reduced magnetization (unit length)`) }
// Special buffered quantity to store magnetization
// makes sure it's normalized etc.
type magnetization struct {
buffer_ *data.Slice
}
func (m *magnetization) Mesh() *data.Mesh { return Mesh() }
func (m *magnetization) NComp() int { return 3 }
func (m *magnetization) Name() string { return "m" }
func (m *magnetization) Unit() string { return "" }
func (m *magnetization) Buffer() *data.Slice { return m.buffer_ } // todo: rename Gpu()?
func (m *magnetization) Child() []script.Expr { return nil }
func (m *magnetization) Comp(c int) *comp { return Comp(m, c) }
func (m *magnetization) SetValue(v interface{}) { m.SetInShape(nil, v.(Config)) }
func (m *magnetization) InputType() reflect.Type { return reflect.TypeOf(Config(nil)) }
func (m *magnetization) Type() reflect.Type { return reflect.TypeOf(new(magnetization)) }
func (m *magnetization) Eval() interface{} { return m }
func (m *magnetization) average() []float64 { return sAverageMagnet(M.Buffer()) }
func (m *magnetization) Average() data.Vector { return unslice(m.average()) }
func (m *magnetization) normalize() { cuda.Normalize(M.Buffer(), geometry.Gpu()) }
// allocate storage (not done by init, as mesh size may not yet be known then)
func (m *magnetization) alloc() {
m.buffer_ = cuda.NewSlice(3, m.Mesh().Size())
m.Set(RandomMag()) // sane starting config
}
func (b *magnetization) SetArray(src *data.Slice) {
if src.Size() != b.Mesh().Size() {
src = data.Resample(src, b.Mesh().Size())
}
data.Copy(b.Buffer(), src)
M.normalize()
}
func (m *magnetization) Set(c Config) {
checkMesh()
m.SetInShape(nil, c)
}
func (m *magnetization) LoadFile(fname string) {
m.SetArray(LoadFile(fname))
}
func (m *magnetization) Slice() (s *data.Slice, recycle bool) {
return m.Buffer(), false
}
func (m *magnetization) Region(r int) *vOneReg { return vOneRegion(m, r) }
func (m *magnetization) String() string { return util.Sprint(m.Buffer().HostCopy()) }
// Set the value of one cell.
func (m *magnetization) SetCell(ix, iy, iz int, v ...float64) {
nComp := m.NComp()
util.Argument(len(v) == nComp)
for c := 0; c < nComp; c++ {
cuda.SetCell(m.Buffer(), c, ix, iy, iz, float32(v[c]))
}
}
// Get the value of one cell.
func (m *magnetization) GetCell(comp, ix, iy, iz int) float64 {
return float64(cuda.GetCell(m.Buffer(), comp, ix, iy, iz))
}
func (m *magnetization) TableData() []float64 { return slice(m.Average()) }
// Sets the magnetization inside the shape
func (m *magnetization) SetInShape(region Shape, conf Config) {
checkMesh()
if region == nil {
region = universe
}
host := m.Buffer().HostCopy()
h := host.Vectors()
n := m.Mesh().Size()
for iz := 0; iz < n[Z]; iz++ {
for iy := 0; iy < n[Y]; iy++ {
for ix := 0; ix < n[X]; ix++ {
r := Index2Coord(ix, iy, iz)
x, y, z := r[X], r[Y], r[Z]
if region(x, y, z) { // inside
m := conf(x, y, z)
h[X][iz][iy][ix] = float32(m[X])
h[Y][iz][iy][ix] = float32(m[Y])
h[Z][iz][iy][ix] = float32(m[Z])
}
}
}
}
m.SetArray(host)
}
// set m to config in region
func (m *magnetization) SetRegion(region int, conf Config) {
host := m.Buffer().HostCopy()
h := host.Vectors()
n := m.Mesh().Size()
r := byte(region)
regionsArr := regions.HostArray()
for iz := 0; iz < n[Z]; iz++ {
for iy := 0; iy < n[Y]; iy++ {
for ix := 0; ix < n[X]; ix++ {
pos := Index2Coord(ix, iy, iz)
x, y, z := pos[X], pos[Y], pos[Z]
if regionsArr[iz][iy][ix] == r {
m := conf(x, y, z)
h[X][iz][iy][ix] = float32(m[X])
h[Y][iz][iy][ix] = float32(m[Y])
h[Z][iz][iy][ix] = float32(m[Z])
}
}
}
}
m.SetArray(host)
}
func (m *magnetization) resize() {
backup := m.Buffer().HostCopy()
s2 := Mesh().Size()
resized := data.Resample(backup, s2)
m.buffer_.Free()
m.buffer_ = cuda.NewSlice(VECTOR, s2)
data.Copy(m.buffer_, resized)
}