Exemplo n.º 1
0
Arquivo: ovf2.go Projeto: shenyp09/mx3
func writeOvf2Binary4(out io.Writer, array *data.Slice) {
	data := array.Tensors()
	gridsize := array.Mesh().Size()

	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
	// encoding/binary is too slow
	// Inlined for performance, terabytes of data will pass here...
	bytes = (*[4]byte)(unsafe.Pointer(&controlnumber))[:]
	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.NComp()
	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++ {
					bytes = (*[4]byte)(unsafe.Pointer(&data[swapIndex(c, ncomp)][i][j][k]))[:]
					out.Write(bytes)
				}
			}
		}
	}
}
Exemplo n.º 2
0
func dumpGnuplot(out io.Writer, f *data.Slice) (err error) {
	buf := bufio.NewWriter(out)
	defer buf.Flush()

	data := f.Tensors()
	gridsize := f.Mesh().Size()
	cellsize := f.Mesh().CellSize()
	// 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.NComp()

	// 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[0]; i++ {
		x := float64(i) * cellsize[0]
		for j := 0; j < gridsize[1]; j++ {
			y := float64(j) * cellsize[1]
			for k := 0; k < gridsize[2]; k++ {
				z := float64(k) * cellsize[2]
				_, err = fmt.Fprint(buf, z, " ", y, " ", x, "\t")
				for c := 0; c < ncomp; c++ {
					_, err = fmt.Fprint(buf, data[swapIndex(c, ncomp)][i][j][k], " ") // converts to user space.
				}
				_, err = fmt.Fprint(buf, "\n")
			}
			_, err = fmt.Fprint(buf, "\n")
		}
	}
	return
}
Exemplo n.º 3
0
Arquivo: omf.go Projeto: shenyp09/mx3
// Writes data in OMF Text format
func writeOmfText(out io.Writer, tens *data.Slice) (err error) {

	data := tens.Tensors()
	gridsize := tens.Mesh().Size()

	// 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.NComp(); c++ {
					_, err = fmt.Fprint(out, data[swapIndex(c, tens.NComp())][i][j][k], " ") // converts to user space.
				}
				_, err = fmt.Fprint(out, "\n")
			}
		}
	}
	return
}
Exemplo n.º 4
0
Arquivo: vtk.go Projeto: shenyp09/mx3
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
}