/
barnes-hut-io.go
222 lines (186 loc) · 4.92 KB
/
barnes-hut-io.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
package barneshut
import (
"bytes"
"encoding/binary"
"encoding/json"
"fmt"
"log"
"os"
"path/filepath"
"strings"
)
const (
CountryBodiesNamePattern = "conf-%s-%08d-%05d.bods"
)
const (
CountryBodiesGifNamePattern = "conf-%s-%08d-%05d.gif"
)
const (
CountryBodiesSVGNamePattern = "conf-%s-%08d-%05d.svg"
)
// serialize bodies's state vector into a file
// convention is "step-xxxx.bod"
// return true if operation was successful
// works only if state is STOPPED
func (r *Run) CaptureConfig() bool {
if r.state == STOPPED {
filename := fmt.Sprintf(r.OutputDir+"/"+CountryBodiesNamePattern, r.country, len(*r.bodies), r.step)
file, err := os.Create(filename)
if err != nil {
log.Fatal(err)
return false
}
jsonBodies, _ := json.MarshalIndent(r.bodies, "", "\t")
file.Write(jsonBodies)
file.Close()
// r.CaptureConfigBase64()
return true
} else {
return false
}
}
func (r *Run) CaptureGif() bool {
filename := fmt.Sprintf(CountryBodiesGifNamePattern, r.country, len(*r.bodies), r.step)
file, err := os.Create(r.OutputDir + "/" + filename)
if err != nil {
log.Fatal(err)
return false
}
r.RenderGif(file, false)
file.Close()
return true
}
func (r *Run) CaptureSVG() bool {
filename := fmt.Sprintf(CountryBodiesSVGNamePattern, r.country, len(*r.bodies), r.step)
file, err := os.Create(filename)
if err != nil {
log.Fatal(err)
return false
}
r.RenderSVG(file)
file.Close()
return true
}
func (r *Run) CaptureConfigBase64() bool {
if r.state == STOPPED {
filename := fmt.Sprintf("conf-base64-TST-%05d.bods", r.step)
file, err := os.Create(filename)
if err != nil {
log.Fatal(err)
return false
}
buf := new(bytes.Buffer)
// encoder := base64.NewEncoder(base64.StdEncoding, &b)
// encoder.Write( *(r.bodies))
// encoder.Close()
for _, v := range *r.bodies {
err = binary.Write(buf, binary.LittleEndian, v.X)
err = binary.Write(buf, binary.LittleEndian, v.Y)
}
file.Write(buf.Bytes())
file.Close()
return true
} else {
return false
}
}
// load configuration from filename (does not contain path)
// works only if state is STOPPED
func (r *Run) LoadConfig(path string, filename string) bool {
Info.Printf("LoadConfig file %s", filename)
if r.state == STOPPED {
// get the number of steps in the file name
// var countryName string
for index, runeValue := range filename {
Trace.Printf("%#U starts at byte position %d\n", runeValue, index)
}
ctry := filename[5:8]
r.country = ctry
// parse the number of elements
nbElementString := filename[9:17]
var nbBodies int
_, errScanNbItems := fmt.Sscanf(nbElementString, "%08d", &nbBodies)
if errScanNbItems != nil {
log.Fatal(errScanNbItems)
return false
}
Info.Printf("Nb bodies in filename %d", nbBodies)
// parse the step
stepString := filename[18:23]
nbItems, errScan := fmt.Sscanf(stepString, "%05d", &r.step)
if errScan != nil {
log.Fatal(errScan)
return false
}
Trace.Printf("nb item parsed in file name %d (should be one)\n", nbItems)
filename = filepath.Join("../../../countries_input", filename)
renderingMutex.Lock()
file, err := os.Open(filename)
if err != nil {
log.Fatal(err)
return false
}
jsonParser := json.NewDecoder(file)
if err = jsonParser.Decode(r.bodies); err != nil {
log.Fatal(fmt.Sprintf("parsing config file %s", err.Error()))
}
Info.Printf("Country is %s, step is %d", ctry, r.step)
Info.Printf("nb item parsed in file %d\n", len(*r.bodies))
file.Close()
r.Init(r.bodies)
renderingMutex.Unlock()
return true
} else {
return false
}
}
// load configuration from filename into the original config (for computing borders)
// works only if state is STOPPED
func (r *Run) LoadConfigOrig(filename string) bool {
if r.state == STOPPED {
file, err := os.Open(filename)
if err != nil {
log.Fatal(err)
return false
}
ctry := filename[5:8]
if r.country != string(ctry) {
Error.Printf("original country %s should be the same as current country %s", ctry, r.country)
}
stepString := filename[9:14]
_, errScan := fmt.Sscanf(stepString, "%05d", &r.step)
if errScan != nil {
log.Fatal(errScan)
return false
}
jsonParser := json.NewDecoder(file)
if err = jsonParser.Decode(r.bodiesOrig); err != nil {
log.Fatal(fmt.Sprintf("parsing config file %s", err.Error()))
}
file.Close()
return true
} else {
return false
}
}
// return the list of available configuration
func (r *Run) DirConfig() []string {
// open the current working directory
cwd, error := os.Open(".")
if error != nil {
panic("not able to open current working directory")
}
// get files with their names
names, err := cwd.Readdirnames(0)
if err != nil {
panic("cannot read names in current working directory")
}
// parse the list of names and pick the ones that match the
var result []string
for _, dirname := range names {
if strings.Contains(dirname, CurrentCountry) {
result = append(result, dirname)
}
}
return result
}