-
Notifications
You must be signed in to change notification settings - Fork 1
/
algorithms_test.go
96 lines (84 loc) · 2.17 KB
/
algorithms_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
package godesim
import (
"math"
"testing"
"github.com/soypat/godesim/state"
)
var stiffDiff = map[state.Symbol]state.Diff{
"x": func(s state.State) float64 { return s.X("Dx") },
"Dx": func(s state.State) float64 { return -50 * (s.X("x") - math.Cos(s.Time())) },
}
var stiffX0 = map[state.Symbol]float64{
"x": 0,
"Dx": -1,
}
func TestConvergenceRKF45(t *testing.T) {
sim := New()
sim.Solver = RKF45Solver
sim.SetTimespan(0, 42., 1)
// set up adaptive timestep
sim.Algorithm.Error.Max = 1e-4
sim.Algorithm.Step.Max, sim.Algorithm.Step.Min = 100, 1e-6
sim.SetDiffFromMap(stiffDiff)
sim.SetX0FromMap(stiffX0)
sim.Begin()
// tm := sim.Results("time")
// t.Errorf("%.2f\n", tm)
// fmt.Printf("%.2f\n", tm)
}
/*
// Benchmarks
*/
func BenchmarkRK4(b *testing.B) {
sim := New()
sim.Algorithm.Steps = b.N
sim.SetTimespan(0, 100., 1)
sim.SetDiffFromMap(stiffDiff)
sim.SetX0FromMap(stiffX0)
sim.Begin()
}
func BenchmarkRK5(b *testing.B) {
sim := New()
sim.Solver = RKF45Solver
sim.Algorithm.Steps = b.N
sim.SetTimespan(0, 100., 1)
// No adaptive timestepping, only 5th order RK
sim.SetDiffFromMap(stiffDiff)
sim.SetX0FromMap(stiffX0)
sim.Begin()
}
func BenchmarkRKF45(b *testing.B) {
sim := New()
sim.Solver = RKF45Solver
sim.Algorithm.Steps = b.N
sim.SetTimespan(0, 100., 1)
// set up adaptive timestep
expectedRKStep := sim.Dt() / float64(b.N)
sim.Algorithm.Error.Max = .1
sim.Algorithm.Step.Min, sim.Algorithm.Step.Max = expectedRKStep, math.Max(expectedRKStep, 4.)
sim.SetDiffFromMap(stiffDiff)
sim.SetX0FromMap(stiffX0)
sim.Begin()
}
func BenchmarkNewton(b *testing.B) {
sim := New()
sim.Solver = NewtonRaphsonSolver
sim.Algorithm.Steps = b.N
sim.SetTimespan(0, 100., 1)
sim.SetDiffFromMap(stiffDiff)
sim.SetX0FromMap(stiffX0)
sim.Begin()
}
func BenchmarkDormandPrince(b *testing.B) {
sim := New()
sim.Solver = DormandPrinceSolver
sim.Algorithm.Steps = b.N
sim.SetTimespan(0, 100., 1)
// set up adaptive timestep
expectedRKStep := sim.Dt() / float64(b.N)
sim.Algorithm.Error.Max = .1
sim.Algorithm.Step.Min, sim.Algorithm.Step.Max = expectedRKStep, math.Max(expectedRKStep, 4.)
sim.SetDiffFromMap(stiffDiff)
sim.SetX0FromMap(stiffX0)
sim.Begin()
}