// Writes the OMF header func writeOmfHeader(out io.Writer, q *data.Slice) (err error) { gridsize := q.Mesh().Size() cellsize := q.Mesh().CellSize() err = hdr(out, "OOMMF", "rectangular mesh v1.0") hdr(out, "Segment count", "1") hdr(out, "Begin", "Segment") hdr(out, "Begin", "Header") dsc(out, "Time", 0) //q.Time) // TODO !! hdr(out, "Title", q.Tag()) hdr(out, "meshtype", "rectangular") hdr(out, "meshunit", "m") hdr(out, "xbase", cellsize[Z]/2) hdr(out, "ybase", cellsize[Y]/2) hdr(out, "zbase", cellsize[X]/2) hdr(out, "xstepsize", cellsize[Z]) hdr(out, "ystepsize", cellsize[Y]) hdr(out, "zstepsize", cellsize[X]) hdr(out, "xmin", 0) hdr(out, "ymin", 0) hdr(out, "zmin", 0) hdr(out, "xmax", cellsize[Z]*float64(gridsize[Z])) hdr(out, "ymax", cellsize[Y]*float64(gridsize[Y])) hdr(out, "zmax", cellsize[X]*float64(gridsize[X])) hdr(out, "xnodes", gridsize[Z]) hdr(out, "ynodes", gridsize[Y]) hdr(out, "znodes", gridsize[X]) hdr(out, "ValueRangeMinMag", 1e-08) // not so "optional" as the OOMMF manual suggests... hdr(out, "ValueRangeMaxMag", 1) // TODO hdr(out, "valueunit", "?") hdr(out, "valuemultiplier", 1) hdr(out, "End", "Header") return }
func writeVTKCellData(out io.Writer, q *data.Slice, dataformat string) (err error) { N := q.NComp() data := q.Tensors() switch N { case 1: fmt.Fprintf(out, "\t\t\t<PointData Scalars=\"%s\">\n", q.Tag()) fmt.Fprintf(out, "\t\t\t\t<DataArray type=\"Float32\" Name=\"%s\" NumberOfComponents=\"%d\" format=\"%s\">\n\t\t\t\t\t", q.Tag(), N, dataformat) case 3: fmt.Fprintf(out, "\t\t\t<PointData Vectors=\"%s\">\n", q.Tag()) fmt.Fprintf(out, "\t\t\t\t<DataArray type=\"Float32\" Name=\"%s\" NumberOfComponents=\"%d\" format=\"%s\">\n\t\t\t\t\t", q.Tag(), N, dataformat) case 6, 9: fmt.Fprintf(out, "\t\t\t<PointData Tensors=\"%s\">\n", q.Tag()) fmt.Fprintf(out, "\t\t\t\t<DataArray type=\"Float32\" Name=\"%s\" NumberOfComponents=\"%d\" format=\"%s\">\n\t\t\t\t\t", q.Tag(), 9, dataformat) // must be 9! default: log.Fatalf("vtk: cannot handle %v components", N) } gridsize := q.Mesh().Size() switch dataformat { case "ascii": for i := 0; i < gridsize[X]; i++ { for j := 0; j < gridsize[Y]; j++ { for k := 0; k < gridsize[Z]; k++ { // if symmetric tensor manage it appart to write the full 9 components if N == 6 { fmt.Fprint(out, data[swapIndex(0, 9)][i][j][k], " ") fmt.Fprint(out, data[swapIndex(1, 9)][i][j][k], " ") fmt.Fprint(out, data[swapIndex(2, 9)][i][j][k], " ") fmt.Fprint(out, data[swapIndex(1, 9)][i][j][k], " ") fmt.Fprint(out, data[swapIndex(3, 9)][i][j][k], " ") fmt.Fprint(out, data[swapIndex(4, 9)][i][j][k], " ") fmt.Fprint(out, data[swapIndex(2, 9)][i][j][k], " ") fmt.Fprint(out, data[swapIndex(4, 9)][i][j][k], " ") fmt.Fprint(out, data[swapIndex(5, 9)][i][j][k], " ") } else { for c := 0; c < N; c++ { fmt.Fprint(out, data[swapIndex(c, N)][i][j][k], " ") } } } } } case "binary": // Inlined for performance, terabytes of data will pass here... buffer := new(bytes.Buffer) for i := 0; i < gridsize[X]; i++ { for j := 0; j < gridsize[Y]; j++ { for k := 0; k < gridsize[Z]; k++ { // if symmetric tensor manage it appart to write the full 9 components if N == 6 { binary.Write(buffer, binary.LittleEndian, data[swapIndex(0, 9)][i][j][k]) binary.Write(buffer, binary.LittleEndian, data[swapIndex(1, 9)][i][j][k]) binary.Write(buffer, binary.LittleEndian, data[swapIndex(2, 9)][i][j][k]) binary.Write(buffer, binary.LittleEndian, data[swapIndex(1, 9)][i][j][k]) binary.Write(buffer, binary.LittleEndian, data[swapIndex(3, 9)][i][j][k]) binary.Write(buffer, binary.LittleEndian, data[swapIndex(4, 9)][i][j][k]) binary.Write(buffer, binary.LittleEndian, data[swapIndex(2, 9)][i][j][k]) binary.Write(buffer, binary.LittleEndian, data[swapIndex(4, 9)][i][j][k]) binary.Write(buffer, binary.LittleEndian, data[swapIndex(5, 9)][i][j][k]) } else { for c := 0; c < N; c++ { binary.Write(buffer, binary.LittleEndian, data[swapIndex(c, N)][i][j][k]) } } } } } b64len := uint32(len(buffer.Bytes())) bufLen := new(bytes.Buffer) binary.Write(bufLen, binary.LittleEndian, b64len) base64out := base64.NewEncoder(base64.StdEncoding, out) base64out.Write(bufLen.Bytes()) base64out.Write(buffer.Bytes()) base64out.Close() default: panic(fmt.Errorf("vtk: illegal data format " + dataformat + ". Options are: ascii, binary")) } fmt.Fprintln(out, "\n\t\t\t\t</DataArray>") fmt.Fprintln(out, "\t\t\t</PointData>") return }
func writeOvf2Header(out io.Writer, q *data.Slice, time, tstep float64) { gridsize := q.Mesh().Size() cellsize := q.Mesh().CellSize() fmt.Fprintln(out, "# OOMMF OVF 2.0") fmt.Fprintln(out, "#") hdr(out, "Segment count", "1") fmt.Fprintln(out, "#") hdr(out, "Begin", "Segment") hdr(out, "Begin", "Header") fmt.Fprintln(out, "#") hdr(out, "Title", q.Tag()) // TODO hdr(out, "meshtype", "rectangular") hdr(out, "meshunit", "m") hdr(out, "xmin", 0) hdr(out, "ymin", 0) hdr(out, "zmin", 0) hdr(out, "xmax", cellsize[Z]*float64(gridsize[Z])) hdr(out, "ymax", cellsize[Y]*float64(gridsize[Y])) hdr(out, "zmax", cellsize[X]*float64(gridsize[X])) name := q.Tag() 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 := q.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: ", tstep, " s") fmt.Fprintln(out, "# Desc: Total simulation time: ", time, " s") hdr(out, "xbase", cellsize[Z]/2) hdr(out, "ybase", cellsize[Y]/2) hdr(out, "zbase", cellsize[X]/2) hdr(out, "xnodes", gridsize[Z]) hdr(out, "ynodes", gridsize[Y]) hdr(out, "znodes", gridsize[X]) hdr(out, "xstepsize", cellsize[Z]) hdr(out, "ystepsize", cellsize[Y]) hdr(out, "zstepsize", cellsize[X]) fmt.Fprintln(out, "#") hdr(out, "End", "Header") fmt.Fprintln(out, "#") }