-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
340 lines (314 loc) · 7.71 KB
/
main.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package main
import (
"bufio"
"bytes"
"fmt"
"io"
"io/ioutil"
"math"
"os"
"runtime"
"time"
"./rig"
"./rig/codec"
)
type ipack struct {
id int
s1 string
s2 string
s3 string
}
type opack struct {
id int
count int
result []float64
rststr string
}
var ipipe = make(chan ipack, 10000)
var opipe = make(chan opack, 10000)
var npipe = make(chan int) // count of events
var spipe = make(chan int) // status
var msg = make(chan int, 5)
const maxev = 20000000
const maxwk = 16 // max workers
var unlimited bool = false
var limn = 50000
func output(name string) {
fout, err := os.Create(name)
if err != nil {
fmt.Fprintln(os.Stderr, "error creating out file: ", err)
os.Exit(0)
}
defer fout.Close()
fmt.Fprintln(fout, "id count xt yt kbx kby x1 y1 kx1 ky1 kxt1 kyt1 x2 kx2 rig tot cx2 cy2 cz2 ckx2 cky2")
total := -1
storage := make([]opack, maxev)
occupacy := make([]bool, maxev)
current := 0
stored := 0
spipe <- 0
dftcnt := 0
loop:
for {
select {
case e := <-opipe:
storage[e.id] = e
occupacy[e.id] = true
stored++
if stored%100 == 0 {
//fmt.Fprintln(os.Stderr, stored)
msg <- 0
}
if stored >= total && total > 0 {
break loop
}
case n := <-npipe:
total = n
if stored >= n {
break loop
}
default:
if dftcnt < 15000 {
dftcnt++
break
}
dftcnt = 0
enough := true
for k := 0; k < 20; k++ {
enough = enough && occupacy[current+k]
if enough == false {
break
}
}
if enough {
for k := 0; k < 20; k++ {
fmt.Fprint(fout, storage[current].rststr)
current++
}
}
}
}
fmt.Println("\nAll collected.")
for i := current; i < total; i++ {
fmt.Fprint(fout, storage[i].rststr)
}
spipe <- 1
}
func work(f func(int, string, string, string) (int, []float64)) {
for {
x := <-ipipe
msg <- 1
//fmt.Println(x.id)
cnt, rst := f(x.id, x.s1, x.s2, x.s3)
str := fmt.Sprintf("%d %d", x.id, cnt)
if rst == nil {
for i := 0; i < 19; i++ {
str += " NA"
}
} else {
for k := 0; k < len(rst); k++ {
str += fmt.Sprintf(" %16.10f", rst[k])
}
}
str += fmt.Sprintln("")
msg <- 2
opipe <- opack{id: x.id, count: cnt, result: rst, rststr: str}
msg <- 3
}
}
func monitor() {
wcount := 0
pcount := 0
ecount := 0
totalt := 0.0
snap := 0
tk := time.NewTicker(2 * time.Second)
fmt.Fprintln(os.Stdout, "| count | #Working | #Waiting | Average Speed | Current Speed |")
for {
select {
case num := <-msg:
if num == 1 {
wcount += 1
} else if num == 2 {
wcount -= 1
pcount += 1
} else if num == 3 {
pcount -= 1
} else if num == 0 {
ecount = ecount + 1
}
case <-tk.C:
totalt += 2
fmt.Fprint(os.Stdout, "\r\033[K")
fmt.Fprintf(os.Stdout, "| %8dk | %8d | %8d | %9.4fke/s | %9.4fke/s |", ecount/10, wcount, pcount, float64(ecount)/totalt/10.0, float64(ecount-snap)/20.0)
snap = ecount
}
}
}
func main() {
//cpuf, _ := os.Create("cpuprofd1.txt")
runtime.GOMAXPROCS(16)
var fbdc *os.File
var ffdc *os.File
var fdck *os.File
var err error
if len(os.Args) < 2 {
fmt.Fprintln(os.Stderr, "add parameter(s)! <1> = bdc file; <2> = fdc0 file; <3> = fdc2k file; <4> = output; [<5>] = dump track only for a specific event")
return
}
fbdc, err = os.Open(os.Args[1])
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
defer fbdc.Close()
ffdc, err = os.Open(os.Args[2])
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
defer ffdc.Close()
fdck, err = os.Open(os.Args[3])
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
defer fdck.Close()
byb, _ := ioutil.ReadAll(fbdc)
brb := bytes.NewReader(byb)
bb := bufio.NewReader(brb)
byf, _ := ioutil.ReadAll(ffdc)
brf := bytes.NewReader(byf)
bf := bufio.NewReader(brf)
byk, _ := ioutil.ReadAll(fdck)
brk := bytes.NewReader(byk)
bk := bufio.NewReader(brk)
bb.ReadLine()
bf.ReadLine()
bk.ReadLine()
bmp0 := codec.Load("bmap.bin")
if bmp0.BX == nil {
fmt.Fprintln(os.Stderr, "Make field binary:")
bmp0 = codec.Encode("180703-1,40T-3000.table", "180703-1,45T-3000.table", 400.0/500.0, "bmap.bin")
}
bmp := bmp0.Backend
//beamline angle: 30 deg.
//direction to FDC2k: 15 dig.
//distance from target to SAMURAI center: 4500
//distance from FDC0 to SAMURAI center: 3000
//distance from SAMURAI center to FDC2: 4000
rig.Init(bmp, 30, 15, 4500, 3000, 4000)
fwk := func(id int, s1, s2, s3 string) (int, []float64) {
//beamline angle: 30 deg.
//direction to FDC2k: 15 dig.
//distance from target to SAMURAI center: 4500
//distance from FDC0 to SAMURAI center: 3000
//distance from SAMURAI center to FDC2: 4000
cnt, rst := rig.RigWork(s1, s2, s3, bmp, 30, 15, 4500, 3000, 4000)
return cnt, rst
}
if len(os.Args) == 5 {
go output(os.Args[4])
go monitor()
<-spipe
for i := 0; i < maxwk; i++ {
go work(fwk)
}
evtn := 0
for unlimited || evtn < limn {
s1, p1, e1 := bb.ReadLine()
s2, p2, e2 := bf.ReadLine()
s3, p3, e3 := bk.ReadLine()
if p1 && p2 && p3 == false {
fmt.Fprintln(os.Stderr, "Error while reading...")
fmt.Fprintln(os.Stderr, " ", p1, e1)
fmt.Fprintln(os.Stderr, " ", p2, e2)
fmt.Fprintln(os.Stderr, " ", p3, e3)
return
}
if e1 != nil && e1 != io.EOF {
fmt.Fprintln(os.Stderr, "error on bdc file: ", e1)
}
if e2 != nil && e2 != io.EOF {
fmt.Fprintln(os.Stderr, "error on bdc file: ", e2)
}
if e3 != nil && e3 != io.EOF {
fmt.Fprintln(os.Stderr, "error on bdc file: ", e3)
}
if e1 == io.EOF || e2 == io.EOF || e3 == io.EOF {
fmt.Fprintln(os.Stderr, e1, e2, e3)
break
}
ipipe <- ipack{id: evtn, s1: string(s1), s2: string(s2), s3: string(s3)}
evtn++
}
npipe <- evtn
<-spipe
} else {
target := 0
fmt.Sscan(os.Args[5], &target)
evtn := -1
for {
s1, p1, e1 := bb.ReadLine()
s2, p2, e2 := bf.ReadLine()
s3, p3, e3 := bk.ReadLine()
evtn++
if evtn == target {
fout, _ := os.Create("dump.txt")
//beamline angle: 30 deg.
//direction to FDC2k: 15 dig.
//distance from target to SAMURAI center: 4500
//distance from FDC0 to SAMURAI center: 3000
//distance from SAMURAI center to FDC2: 4000
cnt, rst := rig.RigWork(string(s1), string(s2), string(s3), bmp, 30, 15, 4500, 3000, 4000)
x1 := rst[4]
y1 := rst[5]
kx := rst[8]
ky := rst[9]
rig0 := rst[12]
cos := math.Cos(math.Pi / 6.0)
sin := math.Sin(math.Pi / 6.0)
kn := math.Sqrt(1.0 + kx*kx + ky*ky)
vx := kx / kn
vy := ky / kn
vz := 1.0 / kn
vxn := vx*cos - vz*sin
vzn := vx*sin + vz*cos
start := [6]float64{3000*sin + x1*cos, y1, x1*sin - 3000*cos, vxn*rig0/ 2.99792458 * 10.0, vy*rig0/ 2.99792458 * 10.0, vzn*rig0/ 2.99792458 * 10.0}
_, rst2 := rig.TraceWithPath(start, 4000, 15, 0.001, bmp)
start[0] = rig0
fmt.Fprint(fout, evtn, cnt)
for i := 0; i < len(rst); i++ {
fmt.Fprintf(fout, "%16.10f ", rst[i])
}
fmt.Fprintln(fout, "")
for i := 0; i < len(rst2); i++ {
fmt.Fprintf(fout, "%16.10f %16.10f ", rst2[i][0], rst2[i][1])
fmt.Fprintf(fout, "%16.10f %16.10f ", rst2[i][2], rst2[i][3])
fmt.Fprintf(fout, "%16.10f %16.10f\n", rst2[i][4], rst2[i][5])
return
}
if p1 && p2 && p3 == false {
fmt.Fprintln(os.Stderr, "Error while reading...")
fmt.Fprintln(os.Stderr, " ", p1, e1)
fmt.Fprintln(os.Stderr, " ", p2, e2)
fmt.Fprintln(os.Stderr, " ", p3, e3)
return
}
if e1 != nil && e1 != io.EOF {
fmt.Fprintln(os.Stderr, "error on bdc file: ", e1)
}
if e2 != nil && e2 != io.EOF {
fmt.Fprintln(os.Stderr, "error on bdc file: ", e2)
}
if e3 != nil && e3 != io.EOF {
fmt.Fprintln(os.Stderr, "error on bdc file: ", e3)
}
if e1 == io.EOF || e2 == io.EOF || e3 == io.EOF {
fmt.Fprintln(os.Stderr, e1, e2, e3)
break
}
}
}
fmt.Println("")
}