-
Notifications
You must be signed in to change notification settings - Fork 0
/
caching.go
301 lines (277 loc) · 8.07 KB
/
caching.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
// Caching of solution points.
//
// Copyright (C) 2020 Juan Marín Noguera
//
// This file is part of Solvned.
//
// Solvned is free software: you can redistribute it and/or modify it under the
// terms of the GNU Lesser General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option) any
// later version.
//
// Solvned is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
// A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
// details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with Solvned. If not, see <https://www.gnu.org/licenses/>.
package mned
import "sort"
// A CacheSolver is like a dynamic version of a DenseSolution. It stores
// points of the solution of a problem in a conceptually maximal interval that
// is evaluated lazily; that is, it starts with the solutions in a range that
// only includes the initial point and, when a point is asked for a Time value
// outside the range, the range is expanded to cover that point.
type CacheSolver struct {
backward []Point // Invariant: Decreasing times, times before forward's.
forward []Point // Invariant: Non-empty, increasing times.
backStep Stepper // Invariant: Decreasing times from end of backward.
forStep Stepper // Invariant: Increasing times from end of forward.
evs []Event
interp Interpolator
}
// Create a CacheSolver to solve the given IVP with the given solving Method,
// with the given interpolator to calculate points between steps and taking into
// account the events provided.
func CacheSolve(
m Method, ivp *IVP, interp Interpolator, evs ...Event,
) CacheSolver {
return CacheSolver{
forward: []Point{ivp.Start.Clone()},
backward: []Point{},
forStep: m.Forward(ivp),
backStep: m.Backward(ivp),
evs: evs,
interp: interp,
}
}
// Get the time of the stored solution point with the lowest time.
func (c *CacheSolver) Start() float64 {
if len(c.backward) > 0 {
return c.backward[len(c.backward)-1].Time
} else {
return c.forward[0].Time
}
}
// Get the time of the stored solution point with the greatest time.
func (c *CacheSolver) End() float64 {
return c.forward[len(c.forward)-1].Time
}
func (c *CacheSolver) getActions(
current *Point, next *Point, vals []float64,
) requiredActions {
var actions requiredActions = make([]requiredAction, 0)
for i := 0; i < len(vals); i++ {
newVal := c.evs[i].Cross(next)
if differentSign(vals[i], newVal) {
point := c.evs[i].FindPoint(c.interp, current, next)
actions = append(
actions,
requiredAction{index: i, point: point},
)
}
vals[i] = newVal
}
sort.Sort(actions)
return actions
}
func (c *CacheSolver) expandBackward(t float64) bool {
if c.backStep == nil {
return false
}
var current Point
if len(c.backward) == 0 {
current = c.forward[0]
} else {
current = c.backward[len(c.backward)-1]
}
vals := make([]float64, len(c.evs))
for i := 0; i < len(vals); i++ {
vals[i] = c.evs[i].Cross(¤t)
}
for t < current.Time {
next, ok := c.backStep.Next()
if !ok {
// TODO Consider bisection for reasonable last point
c.backStep = nil
return false
}
actions := c.getActions(¤t, next, vals)
for i := len(actions) - 1; i >= 0; i-- {
if !c.evs[actions[i].index].Action(&actions[i].point) {
return false
}
}
current = next.Clone()
c.backward = append(c.backward, current)
}
return true
}
func (c *CacheSolver) expandForward(t float64) bool {
if c.forStep == nil {
return false
}
current := c.forward[len(c.forward)-1]
vals := make([]float64, len(c.evs))
for i := 0; i < len(vals); i++ {
vals[i] = c.evs[i].Cross(¤t)
}
for t < current.Time {
next, ok := c.forStep.Next()
if !ok {
// TODO Consider bisection for reasonable last point
c.forStep = nil
return false
}
actions := c.getActions(¤t, next, vals)
for i := 0; i < len(actions); i++ {
if !c.evs[actions[i].index].Action(&actions[i].point) {
return false
}
}
current = next.Clone()
c.forward = append(c.forward, current)
}
return true
}
// Get the value of the solution for a given time.
//
// If the value is outside of the bounds of what's stored, values are added
// until reaching the given time. If ok is false, the value is out of bounds for
// the problem or some event and points have only been added as far as it was
// possible.
func (c *CacheSolver) Get(t float64) (x []float64, ok bool) {
if t < c.Start() {
if ok := c.expandBackward(t); !ok {
return nil, false
}
}
if t > c.End() {
if ok := c.expandForward(t); !ok {
return nil, false
}
}
if t >= c.forward[0].Time {
i := 1
for t > c.forward[i].Time {
i++
}
return c.interp.FindValue(
&c.forward[i-1], &c.forward[i], t,
), true
}
i := 0
for t > c.backward[i].Time {
i++
}
if i == 0 {
return c.interp.FindValue(
&c.backward[0], &c.forward[0], t,
), true
}
return c.interp.FindValue(&c.backward[i], &c.backward[i-1], t), true
}
// Get the value at `c.Start() - h` as in `c.Get` except that, if the underlying
// backward Stepper is a ConfigurableStepper, its method `NextStep(-h)` is used.
// The result value should *NOT* be mutated.
//
// This is most useful with fixed step methods to specify the step.
func (c *CacheSolver) StepBackward(h float64) ([]float64, bool) {
if cs, ok := c.backStep.(ConfigurableStepper); ok {
if point, ok := cs.NextStep(-h); ok {
if h > 0 {
c.backward = append(c.backward, point.Clone())
}
return point.Value, true
} else {
return nil, false
}
}
return c.Get(c.Start() - h)
}
// Get the value at `c.End() + h` as in `c.Get` except that, if the underlying
// forward Stepper is a ConfigurableStepper, its method `NextStep(h)` is used.
// The result value should *NOT* be mutated.
//
// This is most useful with fixed step methods to specify the step.
func (c *CacheSolver) StepForward(h float64) ([]float64, bool) {
if cs, ok := c.forStep.(ConfigurableStepper); ok {
if point, ok := cs.NextStep(h); ok {
if h > 0 {
c.forward = append(c.forward, point.Clone())
}
return point.Value, true
} else {
return nil, false
}
}
return c.Get(c.End() + h)
}
// Get the points calculated from the initial values to earlier times. The
// points **MUST NOT** be modified.
func (c *CacheSolver) BackwardPoints() []Point {
return c.backward
}
// Get the points calculated from the initial values to later times. The points
// **MUST NOT** be modified.
func (c *CacheSolver) ForwardPoints() []Point {
return c.forward
}
// Rearrange the stored solution points in a result such that the i-th point has
// time `result[0][i]` and value `(result[1][i],...,result[size][i])`, where
// `size` is the number of dimensions of a value. This is useful for plotting.
func (c *CacheSolver) PointCoords() [][]float64 {
size := len(c.forward[0].Value)
result := make([][]float64, size+1)
backs := len(c.backward)
elems := backs + len(c.forward)
for i := 0; i <= size; i++ {
result[i] = make([]float64, elems)
}
for i := 0; i < backs; i++ {
result[0][i] = c.backward[backs-i-1].Time
for j := 0; j < size; j++ {
result[j+1][i] = c.backward[backs-i-1].Value[j]
}
}
for i := backs; i < elems; i++ {
result[0][i] = c.forward[i-backs].Time
for j := 0; j < size; j++ {
result[j+1][i] = c.forward[i-backs].Value[j]
}
}
return result
}
// Force the generation of values to the left until the domain ends or an event
// tells the stepping to stop.
func (c *CacheSolver) StepToBeginning() {
var init Point
if len(c.backward) > 0 {
init = c.backward[len(c.backward)-1]
} else {
init = c.forward[0]
}
StepUntil(
c.backStep,
init,
c.interp,
func(point *Point) {
c.backward = append(c.backward, point.Clone())
},
c.evs...,
)
}
// Force the generation of values to the right until the domain ends or an event
// tells the stepping to stop.
func (c *CacheSolver) StepToEnd() {
StepUntil(
c.forStep,
c.forward[len(c.forward)-1],
c.interp,
func(point *Point) {
c.forward = append(c.forward, point.Clone())
},
c.evs...,
)
}