-
Notifications
You must be signed in to change notification settings - Fork 9
/
upmusim.go
216 lines (175 loc) · 6.28 KB
/
upmusim.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
package main
import (
"bytes"
"encoding/binary"
"fmt"
"math"
"math/rand"
"net"
"sync"
"sync/atomic"
"time"
"github.com/BTrDB/smartgridstore/tools/upmuparser"
)
func roundUp4(x uint32) uint32 {
return (x + 3) & 0xFFFFFFFC
}
//simulate a PMU waiting interval seconds between files
func simulatePmu(conn net.Conn, serialint int64, interval int64, lock *sync.Mutex, wg *sync.WaitGroup) {
defer wg.Done()
var sendid uint32 = 0
//Add jitter to simulation
time.Sleep(time.Duration(float64(interval)*rand.Float64()*1000.0) * time.Millisecond)
//Later we can improve this so that multiple runs of the simulator
//with interval < 120 do not overlap if restarted immediately.
//for now I don't care. Round it to 1 second
startTime := (time.Now().UnixNano() / 1000000000) * 1000000000
serial := fmt.Sprintf("P%d", serialint)
fmt.Printf("Starting virtual PMU %s\n", serial)
for {
//Inner loop, for each file
startTime += 120 * 1000 * 1000 * 1000
var blob []byte = generateFile(startTime / 1000000000)
// Filepath for this file...
var filepath string = fmt.Sprintf("/simulation/file%v.dat", sendid)
var lenfp uint32 = uint32(len(filepath))
var lensn uint32 = uint32(len(serial))
var lendt uint32 = uint32(len(blob))
var lenpfp = roundUp4(lenfp)
var lenpsn = roundUp4(lensn)
//Send the blob to the receiver
var header_buf []byte = make([]byte, 16+lenpfp+lenpsn)
var sendid_buf []byte = header_buf[0:4]
var lenfp_buf []byte = header_buf[4:8]
var lensn_buf []byte = header_buf[8:12]
var lendt_buf []byte = header_buf[12:16]
binary.LittleEndian.PutUint32(sendid_buf, sendid)
binary.LittleEndian.PutUint32(lenfp_buf, lenfp)
binary.LittleEndian.PutUint32(lensn_buf, lensn)
binary.LittleEndian.PutUint32(lendt_buf, lendt)
copy(header_buf[16:16+lenpfp], filepath)
copy(header_buf[16+lenpfp:], serial)
lock.Lock()
n, err := conn.Write(header_buf)
if n != len(header_buf) || err != nil {
lock.Unlock()
panic("TCP write failed on header")
}
n, err = conn.Write(blob)
if n != len(blob) || err != nil {
lock.Unlock()
panic("TCP write failed on file")
}
var resp_buf []byte = make([]byte, 4)
totalread := 0
for totalread < 4 {
n, err = conn.Read(resp_buf[totalread:])
if err != nil {
lock.Unlock()
panic("Could not get confirmation of receipt")
}
totalread += n
}
var resp uint32 = binary.LittleEndian.Uint32(resp_buf)
if resp != sendid {
fmt.Printf("Received improper confirmation of receipt: got %v, expected %v\n", resp, sendid)
}
lock.Unlock()
sendid++
//Increment our stats
atomic.AddInt64(&sent, 1)
nxt := startTime + 120*1000*1000*1000
tosleep := time.Unix(0, nxt).Sub(time.Now())
if tosleep < 20*time.Second {
fmt.Printf("Sleeping for less than 20 seconds (sleeping for %v ns); falling behind?\n", tosleep)
tosleep = 20 * time.Second
}
//Wait INTERVAL before doing next file
time.Sleep(tosleep)
}
}
func generateFile(startTime int64) []byte {
//generate a 120 second long file starting from startTime in SECONDS
//and return it as a byte array
var buffer bytes.Buffer
for j := 0; j < 120; j++ {
var data upmuparser.Upmu_one_second_output_standard = generateSecond(startTime + int64(j))
binary.Write(&buffer, binary.LittleEndian, &data)
}
return buffer.Bytes()
}
func clampFloat32(x float64) float32 {
var y float32 = float32(x)
var z float64 = float64(y)
if math.IsInf(z, 1) {
return math.MaxFloat32
} else if math.IsInf(z, -1) {
return -math.MaxFloat32
} else if math.IsNaN(z) {
return 0.0
} else {
return y
}
}
// This function is differentiable, but its derivative is not integrable.
func x2sinxinv(x float64) float64 {
if x == 0.0 {
return 0.0
} else {
return x * x * math.Sin(1/x)
}
}
// This function satisfies the Intermediate Value Property, but is not integrable.
func xinvsinxinv(x float64) float64 {
if x == 0.0 {
return 0.0
} else {
xinv := 1 / x
return xinv * math.Sin(xinv)
}
}
const WEIERSTRASS_A float64 = 0.5
const WEIERSTRASS_B float64 = 13.0
const WEIERSTRASS_ITER int = 13
// Increasing the iteration count to 15 or above results in problems with
// math.Cos. I kept it at 13 to avoid getting too close.
// This approximates a function that is continuous, but not differentiable anywhere.
func weierstrass(x float64) float64 {
var fx float64 = 0
var aton float64 = 1.0
var bton float64 = 1.0
for n := 0; n < WEIERSTRASS_ITER; n++ {
aton *= WEIERSTRASS_A
bton *= WEIERSTRASS_B
fx += aton * math.Cos(bton*math.Pi*x)
}
return fx
}
func generateSecond(startTime int64) upmuparser.Upmu_one_second_output_standard {
var time time.Time = time.Unix(startTime, 0)
var data upmuparser.Upmu_one_second_output_standard
data.Data.Sample_interval_in_milliseconds = 1000.0 / 120.0
data.Data.Timestamp[0] = int32(time.Year())
data.Data.Timestamp[1] = int32(time.Month())
data.Data.Timestamp[2] = int32(time.Day())
data.Data.Timestamp[3] = int32(time.Hour())
data.Data.Timestamp[4] = int32(time.Minute())
data.Data.Timestamp[5] = int32(time.Second())
/* Fill in the actual data */
for i := 0; i < 120; i++ {
data.Data.L1_e_vector_space[i].Fundamental_magnitude_volts = clampFloat32(math.Sin(float64(i) * 4.0 * math.Pi / 120))
data.Data.L2_e_vector_space[i].Fundamental_magnitude_volts = clampFloat32(math.Sin(float64(i) * 2.0 * math.Pi / 120))
data.Data.L3_e_vector_space[i].Fundamental_magnitude_volts = clampFloat32(math.Sin(float64(i) * 1.0 * math.Pi / 120))
data.Data.C1_e_vector_space[i].Fundamental_magnitude_volts = clampFloat32(math.Y0(float64(i+1) * 15.0 / 120))
data.Data.C2_e_vector_space[i].Fundamental_magnitude_volts = clampFloat32(math.Y1(float64(i+1) * 15.0 / 120))
data.Data.C3_e_vector_space[i].Fundamental_magnitude_volts = clampFloat32(math.Yn(2, float64(i+1)*15.0/120))
data.Data.L1_e_vector_space[i].Phase_in_degrees = rand.Float32()
data.Data.L2_e_vector_space[i].Phase_in_degrees = clampFloat32(rand.NormFloat64())
data.Data.L3_e_vector_space[i].Phase_in_degrees = clampFloat32(rand.ExpFloat64())
data.Data.C1_e_vector_space[i].Phase_in_degrees = clampFloat32(x2sinxinv(float64(i-60) / 240))
data.Data.C2_e_vector_space[i].Phase_in_degrees = clampFloat32(0.25 * xinvsinxinv(float64(i-60)/120))
data.Data.C3_e_vector_space[i].Phase_in_degrees = clampFloat32(weierstrass(float64(i-60) / 60))
data.Data.Status[i] = int32(1)
}
return data
}