-
Notifications
You must be signed in to change notification settings - Fork 0
/
ovf2.go
148 lines (127 loc) · 3.66 KB
/
ovf2.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
package oommf
import (
"fmt"
"github.com/mumax/3/data"
"io"
"log"
"strings"
"unsafe"
)
func WriteOVF2(out io.Writer, q *data.Slice, meta data.Meta, dataformat string) {
writeOvf2Header(out, q, meta)
writeOVF2Data(out, q, dataformat)
hdr(out, "End", "Segment")
}
func writeOvf2Header(out io.Writer, q *data.Slice, meta data.Meta) {
gridsize := q.Size()
cellsize := meta.CellSize
fmt.Fprintln(out, "# OOMMF OVF 2.0")
hdr(out, "Segment count", "1")
hdr(out, "Begin", "Segment")
hdr(out, "Begin", "Header")
hdr(out, "Title", meta.Name)
hdr(out, "meshtype", "rectangular")
hdr(out, "meshunit", "m")
hdr(out, "xmin", 0)
hdr(out, "ymin", 0)
hdr(out, "zmin", 0)
hdr(out, "xmax", cellsize[X]*float64(gridsize[X]))
hdr(out, "ymax", cellsize[Y]*float64(gridsize[Y]))
hdr(out, "zmax", cellsize[Z]*float64(gridsize[Z]))
name := meta.Name
var labels []interface{}
if q.NComp() == 1 {
labels = []interface{}{name}
} else {
for i := 0; i < q.NComp(); i++ {
labels = append(labels, name+"_"+string('x'+i))
}
}
hdr(out, "valuedim", q.NComp())
hdr(out, "valuelabels", labels...) // TODO
unit := meta.Unit
if unit == "" {
unit = "1"
}
if q.NComp() == 1 {
hdr(out, "valueunits", unit)
} else {
hdr(out, "valueunits", unit, unit, unit)
}
// We don't really have stages
//fmt.Fprintln(out, "# Desc: Stage simulation time: ", meta.TimeStep, " s") // TODO
hdr(out, "Desc", "Total simulation time: ", meta.Time, " s")
hdr(out, "xbase", cellsize[X]/2)
hdr(out, "ybase", cellsize[Y]/2)
hdr(out, "zbase", cellsize[Z]/2)
hdr(out, "xnodes", gridsize[X])
hdr(out, "ynodes", gridsize[Y])
hdr(out, "znodes", gridsize[Z])
hdr(out, "xstepsize", cellsize[X])
hdr(out, "ystepsize", cellsize[Y])
hdr(out, "zstepsize", cellsize[Z])
hdr(out, "End", "Header")
}
func writeOVF2Data(out io.Writer, q *data.Slice, dataformat string) {
canonicalFormat := ""
switch strings.ToLower(dataformat) {
case "text":
canonicalFormat = "Text"
hdr(out, "Begin", "Data "+canonicalFormat)
writeOVFText(out, q)
case "binary", "binary 4":
canonicalFormat = "Binary 4"
hdr(out, "Begin", "Data "+canonicalFormat)
writeOVF2DataBinary4(out, q)
default:
log.Fatalf("Illegal OMF data format: %v. Options are: Text, Binary 4", dataformat)
}
hdr(out, "End", "Data "+canonicalFormat)
}
func writeOVF2DataBinary4(out io.Writer, array *data.Slice) {
//w.count(w.out.Write((*(*[1<<31 - 1]byte)(unsafe.Pointer(&list[0])))[0 : 4*len(list)])) // (shortcut)
data := array.Tensors()
size := array.Size()
var bytes []byte
// OOMMF requires this number to be first to check the format
var controlnumber float32 = OVF_CONTROL_NUMBER_4
bytes = (*[4]byte)(unsafe.Pointer(&controlnumber))[:]
out.Write(bytes)
ncomp := array.NComp()
for iz := 0; iz < size[Z]; iz++ {
for iy := 0; iy < size[Y]; iy++ {
for ix := 0; ix < size[X]; ix++ {
for c := 0; c < ncomp; c++ {
bytes = (*[4]byte)(unsafe.Pointer(&data[c][iz][iy][ix]))[:]
out.Write(bytes)
}
}
}
}
}
func readOVF2DataBinary4(in io.Reader, array *data.Slice) {
size := array.Size()
data := array.Tensors()
var bytes4 [4]byte
bytes := bytes4[:]
in.Read(bytes)
// OOMMF requires this number to be first to check the format
controlnumber := *((*float32)(unsafe.Pointer(&bytes4)))
if controlnumber != OVF_CONTROL_NUMBER_4 {
panic("invalid OVF2 control number: " + fmt.Sprint(controlnumber))
}
ncomp := array.NComp()
for iz := 0; iz < size[Z]; iz++ {
for iy := 0; iy < size[Y]; iy++ {
for ix := 0; ix < size[X]; ix++ {
for c := 0; c < ncomp; c++ {
n, err := in.Read(bytes)
if err != nil || n != 4 {
panic(err)
}
data[c][iz][iy][ix] = *((*float32)(unsafe.Pointer(&bytes4)))
}
}
}
}
}