/
output.go
203 lines (179 loc) · 5 KB
/
output.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
package simulator
import (
"encoding/json"
"fmt"
"log"
"os"
"sync"
"github.com/gorilla/websocket"
"google.golang.org/protobuf/proto"
)
// OutputFunction is the interface that must be implemented for any function
// which can be used to outputs data from the stochastic process when the provided
// OutputCondition is met.
type OutputFunction interface {
Output(partitionIndex int, state []float64, cumulativeTimesteps float64)
}
// NilOutputFunction outputs nothing from the stochastic process.
type NilOutputFunction struct{}
func (f *NilOutputFunction) Output(
partitionIndex int,
state []float64,
cumulativeTimesteps float64,
) {
}
// StdoutOutputFunction outputs the state to the terminal.
type StdoutOutputFunction struct{}
func (s *StdoutOutputFunction) Output(
partitionIndex int,
state []float64,
cumulativeTimesteps float64,
) {
fmt.Println(cumulativeTimesteps, partitionIndex, state)
}
// VariableStoreOutputFunction stores the data from the stochastic process in a provided
// Store variable on the steps when the OutputCondition is met
type VariableStoreOutputFunction struct {
Store [][][]float64
}
func (f *VariableStoreOutputFunction) Output(
partitionIndex int,
state []float64,
cumulativeTimesteps float64,
) {
f.Store[partitionIndex] = append(f.Store[partitionIndex], state)
}
// JsonLogEntry is the format in which the logs are serialised when using the
// JsonLogOutputFunction.
type JsonLogEntry struct {
PartitionIndex int `json:"partition_index"`
State []float64 `json:"state"`
CumulativeTimesteps float64 `json:"time"`
}
// JsonLogOutputFunction outputs data to a log of json packets from
// the simulation.
type JsonLogOutputFunction struct {
file *os.File
}
func (j *JsonLogOutputFunction) Output(
partitionIndex int,
state []float64,
cumulativeTimesteps float64,
) {
logEntry := JsonLogEntry{
PartitionIndex: partitionIndex,
State: state,
CumulativeTimesteps: cumulativeTimesteps,
}
jsonData, err := json.Marshal(logEntry)
if err != nil {
log.Printf("Error encoding JSON: %s\n", err)
panic(err)
}
jsonData = append(jsonData, []byte("\n")...)
_, err = j.file.Write(jsonData)
if err != nil {
panic(err)
}
}
// NewJsonLogOutputFunction creates a new JsonLogOutputFunction.
func NewJsonLogOutputFunction(
filePath string,
) *JsonLogOutputFunction {
file, err := os.Create(filePath)
if err != nil {
log.Fatal("Error creating log file:", err)
panic(err)
}
return &JsonLogOutputFunction{file: file}
}
// WebsocketOutputFunction serialises the state of each partition of the simulation
// and sends this data via a websocket connection.
type WebsocketOutputFunction struct {
connection *websocket.Conn
mutex *sync.Mutex
}
func (w *WebsocketOutputFunction) Output(
partitionIndex int,
state []float64,
cumulativeTimesteps float64,
) {
data, err := proto.Marshal(
&DashboardPartitionState{
CumulativeTimesteps: cumulativeTimesteps,
PartitionIndex: int64(partitionIndex),
State: state,
},
)
if err != nil {
fmt.Println("Error marshaling protobuf message:", err)
}
// lock the mutex to prevent concurrent writing to the websocket connection
w.mutex.Lock()
if w.connection != nil {
err := w.connection.WriteMessage(websocket.BinaryMessage, data)
if err != nil {
if websocket.IsUnexpectedCloseError(
err,
websocket.CloseGoingAway,
websocket.CloseAbnormalClosure,
) {
fmt.Println("WebSocket closed unexpectedly:", err)
} else {
fmt.Println("Error writing to WebSocket:", err)
}
}
} else {
fmt.Println("WebSocket connection is closed or not ready.")
}
w.mutex.Unlock()
}
// NewWebsocketOutputFunction creates a new WebsocketOutputFunction given a
// connection struct and mutex to protect concurrent writes to the connection.
func NewWebsocketOutputFunction(
connection *websocket.Conn,
mutex *sync.Mutex,
) *WebsocketOutputFunction {
return &WebsocketOutputFunction{connection: connection, mutex: mutex}
}
// OutputCondition is the interface that must be implemented to define when the
// stochastic process calls the OutputFunction.
type OutputCondition interface {
IsOutputStep(partitionIndex int, state []float64, cumulativeTimesteps float64) bool
}
// NilOutputCondition never outputs.
type NilOutputCondition struct{}
func (c *NilOutputCondition) IsOutputStep(
partitionIndex int,
state []float64,
cumulativeTimesteps float64,
) bool {
return false
}
// EveryStepOutputCondition calls the OutputFunction at every step.
type EveryStepOutputCondition struct{}
func (c *EveryStepOutputCondition) IsOutputStep(
partitionIndex int,
state []float64,
cumulativeTimesteps float64,
) bool {
return true
}
// EveryStepOutputCondition calls the OutputFunction once for every N
// steps that occur.
type EveryNStepsOutputCondition struct {
N int
ticker int
}
func (c *EveryNStepsOutputCondition) IsOutputStep(
partitionIndex int,
state []float64,
cumulativeTimesteps float64,
) bool {
c.ticker += 1
if c.ticker == c.N {
c.ticker = 0
return true
}
return false
}