-
Notifications
You must be signed in to change notification settings - Fork 1
/
event_test.go
277 lines (255 loc) · 7.32 KB
/
event_test.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
package godesim
import (
"math"
"testing"
"github.com/soypat/godesim/state"
)
type TypicalEventer struct {
action func(state.State) func(*Simulation) error
label string
}
func (ev TypicalEventer) Event(s state.State) func(*Simulation) error {
return ev.action(s)
}
func (ev TypicalEventer) Label() string {
if ev.label == "" {
panic("empty Eventer label")
}
return ev.label
}
// TestStepLen changes steplength mid-simulation.
// Verifies change of steplength and accuracy of results for simpleInput
func TestStepLen(t *testing.T) {
for _, solver := range gdsimSolvers {
Dtheta := func(s state.State) float64 {
return s.U("u")
}
inputVar := func(s state.State) float64 {
return 1
}
sim := New()
sim.SetDiffFromMap(map[state.Symbol]state.Diff{
"theta": Dtheta,
})
sim.SetX0FromMap(map[state.Symbol]float64{
"theta": 0,
})
sim.SetInputFromMap(map[state.Symbol]state.Input{
"u": inputVar,
})
const NSteps, ti, tf = 10, 0.0, 1.0
sim.SetTimespan(ti, tf, NSteps)
initStepLen := sim.Dt()
newStepLen := initStepLen * 0.25
tswitch := 0.5
var refiner Eventer = TypicalEventer{
label: "refine",
action: func(s state.State) func(*Simulation) error {
if s.Time() >= tswitch {
return NewStepLength(newStepLen)
}
return nil
},
}
sim.AddEventHandlers(refiner)
sim.Solver = solver.f
sim.Begin()
time, xResults := sim.Results("time"), sim.Results("theta")
xQuad := applyFunc(time, func(v float64) float64 { return v /* solution is theta(t) = t*/ })
if len(time) != len(xResults) {
t.Error("length of time and theta vectors should be the same")
}
expectedLen := initStepLen
for i, tm := range time[:len(time)-2] {
if tm >= tswitch {
expectedLen = newStepLen
}
StepLen := time[i+1] - tm
if math.Abs(StepLen-expectedLen) > 1e-12 {
t.Errorf("expected stepLength %.4f. Got %.4f", expectedLen, StepLen)
}
// Also test accuracy of results
if math.Abs(xQuad[i]-xResults[i]) > solver.err(sim.Dt(), float64(i)) {
t.Errorf("incorrect curve profile for test %s", t.Name())
}
}
}
}
// TestBehaviourCubicToQuartic This one's solution is more complex.
// theta-dot's solution for the IVP theta-dot(t=0)=0 is theta-dot=t^2
// thus theta's solution then is theta=1/3*t^3
func TestBehaviourCubicToQuartic(t *testing.T) {
for _, solver := range gdsimSolvers {
Dtheta1 := func(s state.State) float64 {
return 6 * s.Time()
}
Dtheta2 := func(s state.State) float64 {
return 12 * s.Time() * s.Time()
}
sim := New()
sim.SetDiffFromMap(map[state.Symbol]state.Diff{
"theta": func(s state.State) float64 { return s.X("theta-dot") },
"theta-dot": Dtheta1,
})
sim.SetX0FromMap(map[state.Symbol]float64{
"theta": 0,
"theta-dot": 0,
})
const ti, tf, NSteps = 0.0, 2, 10
sim.SetTimespan(ti, tf, NSteps)
tswitch := 1.
var quartic Eventer = TypicalEventer{
label: "change derivative",
action: func(s state.State) func(*Simulation) error {
if s.Time() >= tswitch {
return DiffChangeFromMap(map[state.Symbol]func(state.State) float64{
"theta-dot": Dtheta2,
})
}
return nil
},
}
sim.AddEventHandlers(quartic)
sim.Solver = solver.f
sim.Begin()
time, xResults := sim.Results("time"), sim.Results("theta")
xExpected := applyFunc(time, func(v float64) float64 {
if v > tswitch {
return math.NaN() // I haven't figured out exact solution after switching equation
}
return math.Pow(v, 3)
})
if len(time) != len(xResults) {
t.Error("length of time and theta vectors should be the same")
}
for i := range time {
diff := xResults[i] - xExpected[i]
if math.Abs(diff) > solver.err(sim.Dt(), float64(i)) {
if time[i] > tswitch {
break // I haven't figured the exact solution after tswitch
}
t.Errorf("%s:curve expected %6.4g, got %6.4g", solver.name, xExpected[i], xResults[i])
}
}
}
}
func TestAddEventsError(t *testing.T) {
sim := newWorkingSim()
defer func() {
err := recover()
if err == nil {
t.Error("should panic when adding 0 events")
}
}()
sim.AddEventHandlers()
}
func TestMultiEvent(t *testing.T) {
for _, solver := range gdsimSolvers {
Dtheta1 := func(s state.State) float64 {
return 6 * s.Time()
}
sim := New()
sim.SetDiffFromMap(map[state.Symbol]state.Diff{
"theta": func(s state.State) float64 { return s.X("theta-dot") },
"theta-dot": Dtheta1,
})
sim.SetX0FromMap(map[state.Symbol]float64{
"theta": 0,
"theta-dot": 0,
})
// The test is sensitive to these values since
// it expects a discrete point around tNewEndSim and tStepRefine
// to apply events. Of course, depending on domain subdivision, the
// point at which the event is applied may be just under a step-length away
const ti, tf, NSteps = 0.0, 3, 30
sim.SetTimespan(ti, tf, NSteps)
stepOriginal := sim.Dt()
stepNew := 0.5 * stepOriginal
tStepRefine := 1.
tNewEndSim := 2.
var endsim Eventer = TypicalEventer{
label: "end sim",
action: func(s state.State) func(*Simulation) error {
if s.Time() >= tNewEndSim-1e-4 {
return EndSimulation //NewEvent("time up", EvEndSimulation)
}
return nil
},
}
var refiner Eventer = TypicalEventer{
label: "refine",
action: func(s state.State) func(*Simulation) error {
if s.Time() >= tStepRefine-1e-4 {
return NewStepLength(stepNew)
}
return nil
},
}
sim.AddEventHandlers(endsim, refiner)
sim.Solver = solver.f
sim.Begin()
evs := sim.Events()
if len(evs) != 2 {
t.Error("expected 2 events returned")
}
time, xResults := sim.Results("time"), sim.Results("theta")
xExpected := applyFunc(time, func(v float64) float64 { return math.Pow(v, 3) })
if len(time) != len(xResults) {
t.Error("length of time and theta vectors should be the same")
t.FailNow()
}
if math.Abs(time[len(time)-1]-tNewEndSim) > 1e-12 {
t.Errorf("simulation end event not triggered at domain point. Expected %.3f, got %.3f", tNewEndSim, time[len(time)-1])
}
for i := range time {
diff := xResults[i] - xExpected[i]
if math.Abs(diff) > solver.err(sim.Dt(), float64(i)) {
t.Errorf("%s:curve expected %6.4g, got %6.4g", solver.name, xExpected[i], xResults[i])
}
if i == 0 {
continue
}
dt, tm := time[i]-time[i-1], time[i]
if tm <= tStepRefine && math.Abs(dt-stepOriginal) > 1e-12 {
t.Errorf("steplength event triggered before its time")
}
if tm > tStepRefine && math.Abs(dt-stepNew) > 1e-12 {
t.Errorf("steplength event not applied. expected dt=%.4f, got dt=%.4f", stepNew, dt)
}
}
}
}
func TestEventErrors(t *testing.T) {
sim := New()
sim.SetX0FromMap(map[state.Symbol]float64{
"x": 1,
})
sim.SetDiffFromMap(map[state.Symbol]state.Diff{
"x": func(s state.State) float64 { return 0 },
})
var badEvent Eventer = TypicalEventer{
label: "change derivative",
action: func(s state.State) func(*Simulation) error {
err := DiffChangeFromMap(map[state.Symbol]func(state.State) float64{
"x": func(s state.State) float64 { return 0 },
"extra": func(s state.State) float64 { return 0 },
})
if err != nil {
panic(err)
}
return nil
},
}
sim.SetTimespan(0, 1, 10)
sim.AddEventHandlers(badEvent)
if EventDone(sim) != nil {
t.Error("expected nil return from EventDone")
}
defer func() {
err := recover()
if err == nil {
t.Error("should have gotten an error from a bad event")
}
}()
sim.Begin()
}