-
Notifications
You must be signed in to change notification settings - Fork 1
/
external_test.go
170 lines (157 loc) · 4.43 KB
/
external_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
// Contains examples or tests which require external packages.
package godesim_test
import (
"fmt"
"math"
"strconv"
"strings"
"testing"
"github.com/soypat/godesim"
"github.com/soypat/godesim/state"
)
func TestSimLoggerToCSV(t *testing.T) {
logcfg := godesim.LoggerOptions{}
logcfg.Results.Separator = ","
logcfg.Results.AllStates = true
logcfg.Results.FormatLen = 6
cfg := godesim.Config{
Domain: "time",
Log: logcfg,
}
cfg.Algorithm.Steps = 1
sim := godesim.New()
sim.SetConfig(cfg)
sim.SetDiffFromMap(map[state.Symbol]state.Diff{
"y": func(s state.State) float64 { return 0.1 },
"x": func(s state.State) float64 { return 0.1 },
})
sim.SetX0FromMap(map[state.Symbol]float64{
"y": 0,
"x": 1,
})
sim.SetInputFromMap(map[state.Symbol]state.Input{
"u": func(state.State) float64 { return 1 },
})
const nsteps = 10
sim.SetTimespan(0, 1, nsteps)
var out = &strings.Builder{}
sim.Logger.Output = out
sim.Begin()
lines := strings.Split(out.String(), "\n")
lines = lines[1:] // first line are header
for i := range lines {
vals := strings.Split(lines[i], ",")
if i > nsteps {
break
}
for j := range vals {
_, err := strconv.ParseFloat(strings.TrimSpace(vals[j]), 64)
if err != nil {
t.Error(err)
}
}
}
}
// Solves a simple system of equations of the form
// Dtheta = theta_dot
// Dtheta_dot = 1
func Example_quadratic() {
sim := godesim.New()
sim.SetDiffFromMap(map[state.Symbol]state.Diff{
"theta": func(s state.State) float64 {
return s.X("theta-dot")
},
"theta-dot": func(s state.State) float64 {
return 1
},
})
sim.SetX0FromMap(map[state.Symbol]float64{
"theta": 0,
"theta-dot": 0,
})
sim.SetTimespan(0.0, 1.0, 10)
sim.Begin()
fmt.Printf("%0.3f:\n%0.3f", sim.Results("time"), sim.Results("theta"))
// Output:
//[0.000 0.100 0.200 0.300 0.400 0.500 0.600 0.700 0.800 0.900 1.000]:
//[0.000 0.005 0.020 0.045 0.080 0.125 0.180 0.245 0.320 0.405 0.500]
}
// Solve a stiff equation problem
// using the Newton-Raphson method.
// Equation being solved taken from https://en.wikipedia.org/wiki/Stiff_equation
// Do note that the accuracy for stiff problems is reduced greatly for conventional
// methods.
// y'(t) = -15*y(t)
// solution: y(t) = exp(-15*t)
func Example_implicit() {
sim := godesim.New()
tau := -15.
solution := func(x []float64) []float64 {
sol := make([]float64, len(x))
for i := range x {
sol[i] = math.Exp(tau * x[i])
}
return sol
}
sim.SetDiffFromMap(map[state.Symbol]state.Diff{
"y": func(s state.State) float64 {
return tau * s.X("y")
},
})
sim.SetX0FromMap(map[state.Symbol]float64{
"y": 1,
})
sim.SetTimespan(0.0, 0.5, 15)
sim.Begin()
fmt.Printf("domain :%0.3f:\nresult :%0.3f\nsolution:%0.3f", sim.Results("time"), sim.Results("y"), solution(sim.Results("time")))
// Output:
//domain :[0.000 0.033 0.067 0.100 0.133 0.167 0.200 0.233 0.267 0.300 0.333 0.367 0.400 0.433 0.467 0.500]:
//result :[1.000 0.607 0.368 0.223 0.136 0.082 0.050 0.030 0.018 0.011 0.007 0.004 0.002 0.002 0.001 0.001]
//solution:[1.000 0.607 0.368 0.223 0.135 0.082 0.050 0.030 0.018 0.011 0.007 0.004 0.002 0.002 0.001 0.001]
}
type TypicalEventer struct {
action func(state.State) func(*godesim.Simulation) error
label string
}
func (ev TypicalEventer) Event(s state.State) func(*godesim.Simulation) error {
return ev.action(s)
}
func (ev TypicalEventer) Label() string {
return ev.label
}
/*
Below is an example of Eventer implementation. `TypicalEventer` implements
godesim's Eventer interface.
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 { return ev.label }
*/
func Example_events() {
sim := godesim.New()
sim.SetDiffFromMap(map[state.Symbol]state.Diff{
"theta": func(s state.State) float64 { return 2 * s.X("theta") },
})
sim.SetX0FromMap(map[state.Symbol]float64{
"theta": 0,
})
sim.SetTimespan(0, 10., 10)
initStepLen := sim.Dt()
// We halve the step length somewhere along our simulation.
// this will be the event
newStepLen := initStepLen * 0.5
var refiner godesim.Eventer = TypicalEventer{
label: "refine",
action: func(s state.State) func(*godesim.Simulation) error {
if s.Time() >= 3. {
return godesim.NewStepLength(newStepLen)
}
return nil
},
}
sim.AddEventHandlers(refiner)
sim.Solver = godesim.NewtonRaphsonSolver
sim.Begin()
}