func dispersion(data [][][][]float32) { say("dispersion") out, err := os.OpenFile("dispersion", os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666) core.Fatal(err) defer out.Close() nx, ny, nz := len(data[0]), len(data[0][0]), len(data[0][0][0]) for f := 1; f < len(data)/2; f++ { k := data[f] freq := float64(f) / totaltime power := make([]float64, nx+ny+nz) for x := range k { for y := range k[x] { for z := range k[x][y] { power[int(math.Sqrt(float64(x*x+y*y+z*z)))] += float64(k[x][y][z] * k[x][y][z]) } } } for i := range power { wvec := float64(i) / meshsize[0] // !!!!!!! Assumes cubic cells. fmt.Fprintln(out, freq, wvec, power[i]) } fmt.Fprintln(out) } }
func writeVTKPoints(out io.Writer, q *dump.Frame, dataformat string) { fmt.Fprintln(out, "\t\t\t<Points>") fmt.Fprintf(out, "\t\t\t\t<DataArray type=\"Float32\" Name=\"points\" NumberOfComponents=\"3\" format=\"%s\">\n", dataformat) gridsize := q.Size()[1:] cellsize := q.MeshStep switch dataformat { case "ascii": for k := 0; k < gridsize[X]; k++ { for j := 0; j < gridsize[Y]; j++ { for i := 0; i < gridsize[Z]; i++ { x := (float32)(i) * (float32)(cellsize[Z]) y := (float32)(j) * (float32)(cellsize[Y]) z := (float32)(k) * (float32)(cellsize[X]) _, err := fmt.Fprint(out, x, " ", y, " ", z, " ") core.Fatal(err) } } } case "binary": // Conversion form float32 [4]byte in big-endian // encoding/binary is too slow // Inlined for performance, terabytes of data will pass here... var bytes []byte for k := 0; k < gridsize[X]; k++ { for j := 0; j < gridsize[Y]; j++ { for i := 0; i < gridsize[Z]; i++ { x := (float32)(i) * (float32)(cellsize[Z]) y := (float32)(j) * (float32)(cellsize[Y]) z := (float32)(k) * (float32)(cellsize[X]) bytes = (*[4]byte)(unsafe.Pointer(&x))[:] out.Write(bytes) bytes = (*[4]byte)(unsafe.Pointer(&y))[:] out.Write(bytes) bytes = (*[4]byte)(unsafe.Pointer(&z))[:] out.Write(bytes) } } } default: core.Fatal(fmt.Errorf("Illegal VTK data format: %v. Options are: ascii, binary", dataformat)) } fmt.Fprintln(out, "</DataArray>") fmt.Fprintln(out, "\t\t\t</Points>") }
func dumpOmf(file string, q *dump.Frame, dataformat string) { switch strings.ToLower(dataformat) { case "binary", "binary 4": dataformat = "Binary 4" case "text": dataformat = "Text" default: core.Fatal(fmt.Errorf("Illegal OMF data format: %v", dataformat)) } out, err := os.OpenFile(file, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666) core.Fatal(err) defer out.Close() writeOmfHeader(out, q) writeOmfData(out, q, dataformat) hdr(out, "End", "Segment") }
func dumpVTK(file string, q *dump.Frame, dataformat string) { out, err := os.OpenFile(file, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666) core.Fatal(err) defer out.Close() writeVTKHeader(out, q) writeVTKPoints(out, q, dataformat) writeVTKCellData(out, q, dataformat) writeVTKFooter(out) }
// Writes data in OMF Text format func writeOmfText(out io.Writer, tens *dump.Frame) { data := tens.Tensors() gridsize := tens.Size()[1:] // Here we loop over X,Y,Z, not Z,Y,X, because // internal in C-order == external in Fortran-order for i := 0; i < gridsize[X]; i++ { for j := 0; j < gridsize[Y]; j++ { for k := 0; k < gridsize[Z]; k++ { for c := 0; c < tens.Size()[0]; c++ { _, err := fmt.Fprint(out, data[core.SwapIndex(c, tens.Size()[0])][i][j][k], " ") // converts to user space. core.Fatal(err) } _, err := fmt.Fprint(out, "\n") core.Fatal(err) } } } }
func main() { flag.Parse() if flag.NArg() == 0 { read(os.Stdin, "") } for _, arg := range flag.Args() { f, err := os.Open(arg) core.Fatal(err) read(f, arg) f.Close() } }
func writeOmfData(out io.Writer, q *dump.Frame, dataformat string) { hdr(out, "Begin", "Data "+dataformat) switch strings.ToLower(dataformat) { case "text": writeOmfText(out, q) case "binary 4": writeOmfBinary4(out, q) default: core.Fatal(fmt.Errorf("Illegal OMF data format: %v. Options are: Text, Binary 4", dataformat)) } hdr(out, "End", "Data "+dataformat) }
func fSpectrum(data [][][][]float32) { say("frequencyspectrum") out, err := os.OpenFile("frequencyspectrum", os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666) core.Fatal(err) defer out.Close() for i := 1; i < len(data); i++ { k := data[i] power := 0. K := reshape.RtoC(reshape.ContiguousR3(k)) for _, s := range K { power += float64(real(s)*real(s) + imag(s)*imag(s)) } f := float64(i) / totaltime fmt.Fprintln(out, f, "\t", power) } }
func read(in io.Reader, name string) { r := dump.NewReader(in, *flag_crc) err := r.Read() i := 0 ext := path.Ext(name) woext := noExt(name) for err != io.EOF { core.Fatal(err) tname := name if !(*flag_onefile) { num := fmt.Sprintf("%06d", i) tname = woext + num + ext } process(&r.Frame, tname) err = r.Read() i = i + 1 } }
func dumpGnuplotGZip(f *dump.Frame, file string) { out, err := os.OpenFile(file, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666) core.Fatal(err) out_gzip, err1 := gzip.NewWriterLevel(out, gzip.BestSpeed) core.Fatal(err1) out_buffered := bufio.NewWriter(out_gzip) defer func() { out_buffered.Flush() out_gzip.Close() out.Close() }() data := f.Tensors() gridsize := f.Size()[1:] cellsize := f.MeshStep ncomp := len(data) // Here we loop over X,Y,Z, not Z,Y,X, because // internal in C-order == external in Fortran-order for i := 0; i < gridsize[X]; i++ { x := float64(i) * cellsize[X] for j := 0; j < gridsize[Y]; j++ { y := float64(j) * cellsize[Y] for k := 0; k < gridsize[Z]; k++ { z := float64(k) * cellsize[Z] _, err := fmt.Fprint(out_buffered, z, " ", y, " ", x, "\t") core.Fatal(err) for c := 0; c < ncomp; c++ { _, err := fmt.Fprint(out_buffered, data[core.SwapIndex(c, ncomp)][i][j][k], " ") // converts to user space. core.Fatal(err) } _, err = fmt.Fprint(out_buffered, "\n") core.Fatal(err) } _, err := fmt.Fprint(out_buffered, "\n") core.Fatal(err) } core.Fatal(err) } out_buffered.Flush() }
func dispersionZ(data [][][][]float32) { say("dispersionz") out, err := os.OpenFile("dispersionz", os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666) core.Fatal(err) defer out.Close() for f := 1; f < len(data)/2; f++ { k := data[f] freq := float64(f) / totaltime for z := range k[0][0] { wvec := float64(z) / meshsize[2] power := 0. for x := range k { for y := range k[0] { power += float64(k[x][y][z] * k[x][y][z]) } } fmt.Fprintln(out, freq, wvec, power) } fmt.Fprintln(out) } }
func dumpImage(f *dump.Frame, file string) { var img *image.NRGBA { dim := f.Size()[0] switch dim { default: core.Fatal(fmt.Errorf("unsupported number of components: %v", dim)) case 3: img = DrawVectors(f.Vectors()) case 1: min, max := extrema(f.Data) if *flag_min != "auto" { m, err := strconv.ParseFloat(*flag_min, 32) core.Fatal(err) min = float32(m) } if *flag_max != "auto" { m, err := strconv.ParseFloat(*flag_max, 32) core.Fatal(err) max = float32(m) } img = DrawFloats(f.Floats(), min, max) } } out, err := os.OpenFile(file, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666) core.Fatal(err) defer out.Close() ext := path.Ext(file) switch ext { default: core.Fatal(fmt.Errorf("unsupported image type: %v", ext)) case ".png": core.Fatal(png.Encode(out, img)) case ".jpg": core.Fatal(jpeg.Encode(out, img, nil)) } }
func dumpGnuplot(f *dump.Frame, file string) { out_, err := os.OpenFile(file, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666) core.Fatal(err) defer out_.Close() out_buffered := bufio.NewWriter(out_) defer out_buffered.Flush() data := f.Tensors() gridsize := f.Size()[1:] cellsize := f.MeshStep // If no cell size is set, use generic cell index. if cellsize == [3]float64{0, 0, 0} { cellsize = [3]float64{1, 1, 1} } ncomp := f.Components core.Assert(ncomp > 0) // Here we loop over X,Y,Z, not Z,Y,X, because // internal in C-order == external in Fortran-order for i := 0; i < gridsize[X]; i++ { x := float64(i) * cellsize[X] for j := 0; j < gridsize[Y]; j++ { y := float64(j) * cellsize[Y] for k := 0; k < gridsize[Z]; k++ { z := float64(k) * cellsize[Z] _, err := fmt.Fprint(out_buffered, z, " ", y, " ", x, "\t") core.Fatal(err) for c := 0; c < ncomp; c++ { _, err := fmt.Fprint(out_buffered, data[core.SwapIndex(c, ncomp)][i][j][k], " ") // converts to user space. core.Fatal(err) } _, err = fmt.Fprint(out_buffered, "\n") core.Fatal(err) } _, err := fmt.Fprint(out_buffered, "\n") core.Fatal(err) } core.Fatal(err) } }
func writeVTKCellData(out io.Writer, q *dump.Frame, dataformat string) { N := q.Size()[0] data := q.Tensors() switch N { case 1: fmt.Fprintf(out, "\t\t\t<PointData Scalars=\"%s\">\n", q.DataLabel) fmt.Fprintf(out, "\t\t\t\t<DataArray type=\"Float32\" Name=\"%s\" NumberOfComponents=\"%d\" format=\"%s\">\n", q.DataLabel, N, dataformat) case 3: fmt.Fprintf(out, "\t\t\t<PointData Vectors=\"%s\">\n", q.DataLabel) fmt.Fprintf(out, "\t\t\t\t<DataArray type=\"Float32\" Name=\"%s\" NumberOfComponents=\"%d\" format=\"%s\">\n", q.DataLabel, N, dataformat) case 6, 9: fmt.Fprintf(out, "\t\t\t<PointData Tensors=\"%s\">\n", q.DataLabel) fmt.Fprintf(out, "\t\t\t\t<DataArray type=\"Float32\" Name=\"%s\" NumberOfComponents=\"%d\" format=\"%s\">\n", q.DataLabel, 9, dataformat) // must be 9! default: core.Fatal(fmt.Errorf("vtk: cannot handle %v components")) } gridsize := q.MeshSize 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[core.SwapIndex(0, 9)][i][j][k], " ") fmt.Fprint(out, data[core.SwapIndex(1, 9)][i][j][k], " ") fmt.Fprint(out, data[core.SwapIndex(2, 9)][i][j][k], " ") fmt.Fprint(out, data[core.SwapIndex(1, 9)][i][j][k], " ") fmt.Fprint(out, data[core.SwapIndex(3, 9)][i][j][k], " ") fmt.Fprint(out, data[core.SwapIndex(4, 9)][i][j][k], " ") fmt.Fprint(out, data[core.SwapIndex(2, 9)][i][j][k], " ") fmt.Fprint(out, data[core.SwapIndex(4, 9)][i][j][k], " ") fmt.Fprint(out, data[core.SwapIndex(5, 9)][i][j][k], " ") } else { for c := 0; c < N; c++ { fmt.Fprint(out, data[core.SwapIndex(c, N)][i][j][k], " ") } } } } } case "binary": // Inlined for performance, terabytes of data will pass here... var bytes []byte 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 { bytes = (*[4]byte)(unsafe.Pointer(&data[core.SwapIndex(0, 9)][i][j][k]))[:] out.Write(bytes) bytes = (*[4]byte)(unsafe.Pointer(&data[core.SwapIndex(1, 9)][i][j][k]))[:] out.Write(bytes) bytes = (*[4]byte)(unsafe.Pointer(&data[core.SwapIndex(2, 9)][i][j][k]))[:] out.Write(bytes) bytes = (*[4]byte)(unsafe.Pointer(&data[core.SwapIndex(1, 9)][i][j][k]))[:] out.Write(bytes) bytes = (*[4]byte)(unsafe.Pointer(&data[core.SwapIndex(3, 9)][i][j][k]))[:] out.Write(bytes) bytes = (*[4]byte)(unsafe.Pointer(&data[core.SwapIndex(4, 9)][i][j][k]))[:] out.Write(bytes) bytes = (*[4]byte)(unsafe.Pointer(&data[core.SwapIndex(2, 9)][i][j][k]))[:] out.Write(bytes) bytes = (*[4]byte)(unsafe.Pointer(&data[core.SwapIndex(4, 9)][i][j][k]))[:] out.Write(bytes) bytes = (*[4]byte)(unsafe.Pointer(&data[core.SwapIndex(5, 9)][i][j][k]))[:] out.Write(bytes) } else { for c := 0; c < N; c++ { bytes = (*[4]byte)(unsafe.Pointer(&data[core.SwapIndex(c, N)][i][j][k]))[:] out.Write(bytes) } } } } } default: core.Fatal(fmt.Errorf("vtk: illegal data format " + dataformat + ". Options are: ascii, binary")) } fmt.Fprintln(out, "</DataArray>") fmt.Fprintln(out, "\t\t\t</PointData>") }