/
parameter.go
355 lines (308 loc) · 8.37 KB
/
parameter.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
package engine
/*
parameters are region- and time dependent input values,
like material parameters.
*/
import (
"fmt"
"github.com/mumax/3/data"
"github.com/mumax/3/script"
"github.com/mumax/3/util"
"math"
"reflect"
"strings"
)
// input parameter, settable by user
type param struct {
lut
upd_reg [NREGION]func() []float64 // time-dependent values
timestamp float64 // used not to double-evaluate f(t)
children []derived // derived parameters
name, unit string
}
func (p *param) init(nComp int, name, unit string, children []derived) {
p.lut.init(nComp, p)
p.name = name
p.unit = unit
p.children = children
p.timestamp = math.Inf(-1)
}
func (p *param) Name() string { return p.name }
func (p *param) Unit() string { return p.unit }
func (p *param) Mesh() *data.Mesh { return Mesh() }
func (p *param) addChild(c ...derived) {
for _, c := range c {
// TODO: no duplicates
if !contains(p.children, c) {
p.children = append(p.children, c)
fmt.Println(p, ".addChild", c)
}
}
}
func contains(s []derived, x derived) bool {
for _, y := range s {
if y == x {
return true
}
}
return false
}
func (p *param) update() {
if p.timestamp != Time {
changed := false
// update functions of time
for r := 0; r < NREGION; r++ {
updFunc := p.upd_reg[r]
if updFunc != nil {
p.bufset_(r, updFunc())
changed = true
}
}
p.timestamp = Time
if changed {
p.invalidate()
}
}
}
// set in one region
func (p *param) setRegion(region int, v []float64) {
if region == -1 {
p.setUniform(v)
} else {
p.setRegions(region, region+1, v)
}
}
// set in all regions
func (p *param) setUniform(v []float64) {
p.setRegions(0, NREGION, v)
}
// set in regions r1..r2(excl)
func (p *param) setRegions(r1, r2 int, v []float64) {
util.Argument(len(v) == len(p.cpu_buf))
util.Argument(r1 < r2) // exclusive upper bound
for r := r1; r < r2; r++ {
p.upd_reg[r] = nil
p.bufset_(r, v)
}
p.invalidate()
}
func (p *param) bufset_(region int, v []float64) {
for c := range p.cpu_buf {
p.cpu_buf[c][region] = float32(v[c])
}
}
func (p *param) setFunc(r1, r2 int, f func() []float64) {
util.Argument(r1 < r2) // exclusive upper bound
for r := r1; r < r2; r++ {
p.upd_reg[r] = f
}
p.invalidate()
}
// mark my GPU copy and my children as invalid (need update)
func (p *param) invalidate() {
p.gpu_ok = false
for _, c := range p.children {
c.invalidate()
}
}
func (p *param) getRegion(region int) []float64 {
cpu := p.cpuLUT()
v := make([]float64, p.NComp())
for i := range v {
v[i] = float64(cpu[i][region])
}
return v
}
func (p *param) IsUniform() bool {
cpu := p.cpuLUT()
v1 := p.getRegion(0)
for r := 1; r < NREGION; r++ {
for c := range v1 {
if cpu[c][r] != float32(v1[c]) {
return false
}
}
}
return true
}
func (p *param) average() []float64 { return qAverageUniverse(p) }
// parameter derived from others (not directly settable). E.g.: Bsat derived from Msat
type DerivedParam struct {
lut // GPU storage
updater func(*DerivedParam) // called to update my value
uptodate bool // cleared if parents' value change
parents []updater // parents updated before I'm updated
}
// any parameter that depends on an inputParam
type derived interface {
invalidate()
}
type parent interface {
update()
addChild(...derived)
}
func NewDerivedParam(nComp int, parents []parent, updater func(*DerivedParam)) *DerivedParam {
p := new(DerivedParam)
p.lut.init(nComp, p) // pass myself to update me if needed
p.updater = updater
for _, P := range parents {
p.parents = append(p.parents, P)
}
return p
}
func (d *DerivedParam) init(nComp int, parents []parent, updater func(*DerivedParam)) {
d.lut.init(nComp, d) // pass myself to update me if needed
d.updater = updater
for _, p := range parents {
d.parents = append(d.parents, p)
p.addChild(d)
}
}
func (p *DerivedParam) invalidate() {
p.uptodate = false
}
func (p *DerivedParam) update() {
for _, par := range p.parents {
par.update() // may invalidate me
}
if !p.uptodate {
p.updater(p)
p.gpu_ok = false
p.uptodate = true
}
}
// Get value in region r.
func (p *DerivedParam) GetRegion(r int) []float64 {
lut := p.cpuLUT() // updates me if needed
v := make([]float64, p.NComp())
for c := range v {
v[c] = float64(lut[c][r])
}
return v
}
// specialized param with 1 component
type ScalarParam struct {
param
}
func (p *ScalarParam) init(name, unit, desc string, children []derived) {
p.param.init(SCALAR, name, unit, children)
DeclLValue(name, p, cat(desc, unit))
}
// TODO: auto derived
func NewScalarParam(name, unit, desc string, children ...derived) *ScalarParam {
p := new(ScalarParam)
p.param.init(SCALAR, name, unit, children)
DeclLValue(name, p, cat(desc, unit))
return p
}
func (p *ScalarParam) SetRegion(region int, f script.ScalarFunction) {
if region == -1 {
p.setRegionsFunc(0, NREGION, f) // uniform
} else {
p.setRegionsFunc(region, region+1, f) // upper bound exclusive
}
}
func (p *ScalarParam) SetValue(v interface{}) {
f := v.(script.ScalarFunction)
p.setRegionsFunc(0, NREGION, f)
}
func (p *ScalarParam) Set(v float64) {
p.setRegions(0, NREGION, []float64{v})
}
func (p *ScalarParam) setRegionsFunc(r1, r2 int, f script.ScalarFunction) {
if Const(f) {
p.setRegions(r1, r2, []float64{f.Float()})
} else {
f := f.Fix() // fix values of all variables except t
p.setFunc(r1, r2, func() []float64 {
return []float64{f.Eval().(script.ScalarFunction).Float()}
})
}
}
func (p *ScalarParam) GetRegion(region int) float64 {
return float64(p.getRegion(region)[0])
}
func (p *ScalarParam) Eval() interface{} { return p }
func (p *ScalarParam) Type() reflect.Type { return reflect.TypeOf(new(ScalarParam)) }
func (p *ScalarParam) InputType() reflect.Type { return script.ScalarFunction_t }
func (p *ScalarParam) Average() float64 { return qAverageUniverse(p)[0] }
func (p *ScalarParam) Region(r int) *sOneReg { return sOneRegion(p, r) }
// checks if a script expression contains t (time)
func Const(e script.Expr) bool {
t := World.Resolve("t")
return !script.Contains(e, t)
}
func cat(desc, unit string) string {
if unit == "" {
return desc
} else {
return desc + " (" + unit + ")"
}
}
// these methods should only be accesible from Go
func (p *ScalarParam) SetRegionValueGo(region int, v float64) {
if region == -1 {
p.setRegions(0, NREGION, []float64{v})
} else {
p.setRegions(region, region+1, []float64{v})
}
}
func (p *ScalarParam) SetRegionFuncGo(region int, f func() float64) {
if region == -1 {
p.setFunc(0, NREGION, func() []float64 {
return []float64{f()}
})
} else {
p.setFunc(region, region+1, func() []float64 {
return []float64{f()}
})
}
}
// vector input parameter, settable by user
type VectorParam struct {
param
}
func NewVectorParam(name, unit, desc string) *VectorParam {
p := new(VectorParam)
p.param.init(VECTOR, name, unit, nil) // no vec param has children (yet)
if !strings.HasPrefix(name, "_") { // don't export names beginning with "_" (e.g. from exciation)
DeclLValue(name, p, cat(desc, unit))
}
return p
}
func (p *VectorParam) SetRegion(region int, f script.VectorFunction) {
if region == -1 {
p.setRegionsFunc(0, NREGION, f) //uniform
} else {
p.setRegionsFunc(region, region+1, f)
}
}
func (p *VectorParam) SetValue(v interface{}) {
f := v.(script.VectorFunction)
p.setRegionsFunc(0, NREGION, f)
}
func (p *VectorParam) setRegionsFunc(r1, r2 int, f script.VectorFunction) {
if Const(f) {
p.setRegions(r1, r2, slice(f.Float3()))
} else {
f := f.Fix() // fix values of all variables except t
p.setFunc(r1, r2, func() []float64 {
return slice(f.Eval().(script.VectorFunction).Float3())
})
}
}
func (p *VectorParam) SetRegionFn(region int, f func() [3]float64) {
p.setFunc(region, region+1, func() []float64 {
return slice(f())
})
}
func (p *VectorParam) GetRegion(region int) [3]float64 {
v := p.getRegion(region)
return unslice(v)
}
func (p *VectorParam) Eval() interface{} { return p }
func (p *VectorParam) Type() reflect.Type { return reflect.TypeOf(new(VectorParam)) }
func (p *VectorParam) InputType() reflect.Type { return script.VectorFunction_t }
func (p *VectorParam) Region(r int) *vOneReg { return vOneRegion(p, r) }
func (p *VectorParam) Average() data.Vector { return unslice(qAverageUniverse(p)) }
func (p *VectorParam) Comp(c int) ScalarField { return Comp(p, c) }