Пример #1
0
// Writes data in OMF Binary 4 format
func writeOmfBinary4(out io.Writer, array *dump.Frame) {
	data := array.Tensors()
	gridsize := array.Size()[1:]

	var bytes []byte

	// OOMMF requires this number to be first to check the format
	var controlnumber float32 = OMF_CONTROL_NUMBER
	// Conversion form float32 [4]byte in big-endian
	// Inlined for performance, terabytes of data will pass here...
	bytes = (*[4]byte)(unsafe.Pointer(&controlnumber))[:]
	bytes[0], bytes[1], bytes[2], bytes[3] = bytes[3], bytes[2], bytes[1], bytes[0] // swap endianess
	out.Write(bytes)

	// Here we loop over X,Y,Z, not Z,Y,X, because
	// internal in C-order == external in Fortran-order
	ncomp := array.Size()[0]
	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 < ncomp; c++ {
					// dirty conversion from float32 to [4]byte
					bytes = (*[4]byte)(unsafe.Pointer(&data[core.SwapIndex(c, ncomp)][i][j][k]))[:]
					bytes[0], bytes[1], bytes[2], bytes[3] = bytes[3], bytes[2], bytes[1], bytes[0]
					out.Write(bytes)
				}
			}
		}
	}
}
Пример #2
0
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()
}
Пример #3
0
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)
	}
}
Пример #4
0
// 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)
			}
		}
	}
}
Пример #5
0
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>")
}