-
Notifications
You must be signed in to change notification settings - Fork 0
/
controlLoop.go
63 lines (49 loc) · 1.12 KB
/
controlLoop.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
package main
import (
"./graph/"
"./logger/"
"os"
"time"
)
func controlLoop(g *graph.Graph, timeStep time.Duration, saveInterval int,
logRegexp string) {
// Main loop
ticker := time.NewTicker(timeStep)
counter := 0
logger.WriteEvent("entering control loop...")
stopValue := 0.0
for stopValue > -1.0 && stopValue < 1.0 {
<-ticker.C
g.CycleParallel([]string{"inputs"})
g.CycleLines()
g.CycleSerial([]string{"logic"})
g.CycleLines()
g.CycleParallel([]string{"outputs"})
g.CycleSerial([]string{"stops"})
stopValue = checkStops(g)
if counter%saveInterval == 0 {
g.LogData(logRegexp)
counter = 0
}
counter += 1
}
// also save the last time
g.LogData(logRegexp)
if stopValue > -1.0 {
os.Exit(2)
}
}
func checkStops(g *graph.Graph) float64 {
// default value for this variable is 0.0
// this means that without stop criteria the program will run forever
v := g.CycleValues([]string{"stops"}, 0.0, func(bname string, x, v float64) float64 {
if v > x {
x = v
}
if v >= 1.0 {
logger.WriteEvent("checkStops(), ", "in "+bname+": divergence detected")
}
return x
})
return v
}