-
Notifications
You must be signed in to change notification settings - Fork 0
/
autosave.go
90 lines (77 loc) · 2.41 KB
/
autosave.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
package engine
import (
"fmt"
"github.com/mumax/3/cuda"
"github.com/mumax/3/draw"
"github.com/mumax/3/util"
)
var (
output = make(map[Quantity]*autosave) // when to save quantities
autonum = make(map[interface{}]int) // auto number for out file
)
func init() {
DeclFunc("Save", Save, "Save space-dependent quantity once, with auto filename")
DeclFunc("SaveAs", SaveAs, "Save space-dependent with custom filename")
DeclFunc("AutoSave", AutoSave, "Auto save space-dependent quantity every period (s).")
DeclVar("SnapshotFormat", &SnapshotFormat, "Image format for snapshots: jpg, png or gif.")
DeclVar("FilenameFormat", &FilenameFormat, "printf formatting string for output filenames.")
DeclFunc("Snapshot", Snapshot, "Save image of quantity")
DeclFunc("AutoSnapshot", AutoSnapshot, "Auto save image of quantity every period (s).")
}
// Periodically called by run loop to save everything that's needed at this time.
func DoOutput() {
for q, a := range output {
if a.needSave() {
a.save(q)
a.count++
}
}
if Table.needSave() {
Table.Save()
}
}
// Register quant to be auto-saved every period.
// period == 0 stops autosaving.
func AutoSave(q Quantity, period float64) {
autoSave(q, period, Save)
}
func AutoSnapshot(q Quantity, period float64) {
autoSave(q, period, Snapshot)
}
func autoSave(q Quantity, period float64, save func(Quantity)) {
if period == 0 {
delete(output, q)
} else {
output[q] = &autosave{period, Time, 0, save}
}
}
var FilenameFormat = "%s%06d"
// Save once, with auto file name
func Save(q Quantity) {
fname := fmt.Sprintf(OD+FilenameFormat+".ovf", q.Name(), autonum[q])
SaveAs(q, fname)
autonum[q]++
}
var SnapshotFormat = "jpg"
// Save image once, with auto file name
func Snapshot(q Quantity) {
fname := fmt.Sprintf(OD+FilenameFormat+"."+SnapshotFormat, q.Name(), autonum[q])
s, r := q.Slice()
if r {
defer cuda.Recycle(s)
}
util.FatalErr(draw.RenderFile(fname, assureCPU(s), "", ""))
autonum[q]++
}
// keeps info needed to decide when a quantity needs to be periodically saved
type autosave struct {
period float64 // How often to save
start float64 // Starting point
count int // Number of times it has been autosaved
save func(Quantity) // called to do the actual save
}
// returns true when the time is right to save.
func (a *autosave) needSave() bool {
t := Time - a.start
return a.period != 0 && t-float64(a.count)*a.period >= a.period
}