/
main.go
174 lines (146 loc) · 4.05 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
// mumax3 main command
package main
import (
"flag"
"fmt"
"log"
"os"
"os/exec"
"path"
"time"
"github.com/mumax/3/cuda"
"github.com/mumax/3/engine"
"github.com/mumax/3/script"
"github.com/mumax/3/util"
)
var (
flag_failfast = flag.Bool("failfast", false, "If one simulation fails, stop entire batch immediately")
flag_test = flag.Bool("test", false, "Cuda test (internal)")
flag_version = flag.Bool("v", true, "Print version")
flag_vet = flag.Bool("vet", false, "Check input files for errors, but don't run them")
// more flags in engine/gofiles.go
)
func main() {
flag.Parse()
log.SetPrefix("")
log.SetFlags(0)
cuda.Init(*engine.Flag_gpu)
cuda.Synchronous = *engine.Flag_sync
if *flag_version {
printVersion()
}
// used by bootstrap launcher to test cuda
// successful exit means cuda was initialized fine
if *flag_test {
fmt.Println(cuda.GPUInfo)
os.Exit(0)
}
defer engine.Close() // flushes pending output, if any
if *flag_vet {
vet()
return
}
switch flag.NArg() {
case 0:
if *engine.Flag_interactive {
runInteractive()
}
case 1:
runFileAndServe(flag.Arg(0))
default:
RunQueue(flag.Args())
}
}
func runInteractive() {
fmt.Println("//no input files: starting interactive session")
//initEngine()
// setup outut dir
now := time.Now()
outdir := fmt.Sprintf("mumax-%v-%02d-%02d_%02dh%02d.out", now.Year(), int(now.Month()), now.Day(), now.Hour(), now.Minute())
engine.InitIO(outdir, outdir, *engine.Flag_forceclean)
engine.Timeout = 365 * 24 * time.Hour // basically forever
// set up some sensible start configuration
engine.Eval(`SetGridSize(128, 64, 1)
SetCellSize(4e-9, 4e-9, 4e-9)
Msat = 1e6
Aex = 10e-12
alpha = 1
m = RandomMag()`)
addr := goServeGUI()
openbrowser("http://127.0.0.1" + addr)
engine.RunInteractive()
}
func runFileAndServe(fname string) {
if path.Ext(fname) == ".go" {
runGoFile(fname)
} else {
runScript(fname)
}
}
func runScript(fname string) {
outDir := util.NoExt(fname) + ".out"
if *engine.Flag_od != "" {
outDir = *engine.Flag_od
}
engine.InitIO(fname, outDir, *engine.Flag_forceclean)
fname = engine.InputFile
var code *script.BlockStmt
var err2 error
if fname != "" {
// first we compile the entire file into an executable tree
code, err2 = engine.CompileFile(fname)
util.FatalErr(err2)
}
// now the parser is not used anymore so it can handle web requests
goServeGUI()
if *engine.Flag_interactive {
openbrowser("http://127.0.0.1" + *engine.Flag_port)
}
// start executing the tree, possibly injecting commands from web gui
engine.EvalFile(code)
if *engine.Flag_interactive {
engine.RunInteractive()
}
}
func runGoFile(fname string) {
// pass through flags
flags := []string{"run", fname}
flag.Visit(func(f *flag.Flag) {
if f.Name != "o" {
flags = append(flags, fmt.Sprintf("-%v=%v", f.Name, f.Value))
}
})
if *engine.Flag_od != "" {
flags = append(flags, fmt.Sprintf("-o=%v", *engine.Flag_od))
}
cmd := exec.Command("go", flags...)
log.Println("go", flags)
cmd.Stdout = os.Stdout
cmd.Stdin = os.Stdin
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
os.Exit(1)
}
}
// start Gui server and return server address
func goServeGUI() string {
if *engine.Flag_port == "" {
log.Println(`//not starting GUI (-http="")`)
return ""
}
addr := engine.GoServe(*engine.Flag_port)
fmt.Print("//starting GUI at http://127.0.0.1", addr, "\n")
return addr
}
// print version to stdout
func printVersion() {
engine.LogOut(engine.UNAME)
engine.LogOut(fmt.Sprintf("GPU info: %s, using cc=%d PTX", cuda.GPUInfo, cuda.UseCC))
engine.LogOut("(c) Arne Vansteenkiste, Dynamat LAB, Ghent University, Belgium")
engine.LogOut("This is free software without any warranty. See license.txt")
engine.LogOut("********************************************************************//")
engine.LogOut(" If you use mumax in any work or publication, //")
engine.LogOut(" we kindly ask you to cite the references in references.bib //")
engine.LogOut("********************************************************************//")
}