-
Notifications
You must be signed in to change notification settings - Fork 1
/
events.go
67 lines (58 loc) · 1.84 KB
/
events.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
package godesim
import (
"fmt"
"math"
"github.com/soypat/godesim/state"
)
// Eventer specifies an event to be applied to simulation
//
// Events which return error when applied to simulation
// will create a special event with the error message in the label
// and log the information.
type Eventer interface {
// Behaviour aspect of event. changing simulation
//
// A nil func(*Simulation) error corresponds to no event taking place (just skips it)
Event(state.State) func(*Simulation) error
// For identification
Label() string
}
var (
// ErrorRemove should be returned by a simulation modifier
// if the Eventer is to be removed from Event handlers
ErrorRemove error = fmt.Errorf("remove this Eventer")
)
// EventDone is the uneventful event. Changes absolutely nothing
// and is removed from Event handler list.
func EventDone(sim *Simulation) error { return nil }
// DiffChangeFromMap Event handler. Takes new state variable (X) functions and applies them
func DiffChangeFromMap(newDiff map[state.Symbol]func(state.State) float64) func(*Simulation) error {
return func(sim *Simulation) error {
applied := 0
for i, sym := range sim.State.XSymbols() {
if _, ok := newDiff[sym]; ok {
sim.Diffs[i] = newDiff[sym]
applied++
}
}
if applied != len(newDiff) {
return fmt.Errorf("%d symbol(s) were not found during DiffChange event", len(newDiff)-applied)
}
return nil
}
}
// NewStepLength Event handler. Sets the new minimum step length
func NewStepLength(h float64) func(*Simulation) error {
return func(sim *Simulation) error {
if sim.IsRunning() {
steps := math.Ceil((sim.End() - sim.CurrentTime()) / h)
sim.SetTimespan(sim.CurrentTime(), sim.CurrentTime()+steps*h, int(steps))
}
return nil
}
}
// EndSimulation Event handler. Ends simulation
func EndSimulation(sim *Simulation) error {
sim.currentStep = -1
return nil
}