Exemplo n.º 1
0
func writeVTKHeader(out io.Writer, q *dump.Frame) {
	gridsize := q.Size()[1:]

	fmt.Fprintln(out, "<?xml version=\"1.0\"?>")
	fmt.Fprintln(out, "<VTKFile type=\"StructuredGrid\" version=\"0.1\" byte_order=\"LittleEndian\">")
	fmt.Fprintf(out, "\t<StructuredGrid WholeExtent=\"0 %d 0 %d 0 %d\">\n", gridsize[Z]-1, gridsize[Y]-1, gridsize[X]-1)
	fmt.Fprintf(out, "\t\t<Piece Extent=\"0 %d 0 %d 0 %d\">\n", gridsize[Z]-1, gridsize[Y]-1, gridsize[X]-1)
}
Exemplo n.º 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()
}
Exemplo n.º 3
0
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>")
}
Exemplo n.º 4
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)
	}
}
Exemplo n.º 5
0
// normalize vector data to unit length
func normalize(f *dump.Frame) {
	a := f.Vectors()
	for i := range a[0] {

		for j := range a[0][i] {

			for k := range a[0][i][j] {
				x, y, z := a[0][i][j][k], a[1][i][j][k], a[2][i][j][k]
				norm := math.Sqrt(float64(x*x + y*y + z*z))
				invnorm := float32(1)
				if norm != 0 {
					invnorm = float32(1 / norm)
				}
				a[0][i][j][k] *= invnorm
				a[1][i][j][k] *= invnorm
				a[2][i][j][k] *= invnorm

			}
		}
	}
}
Exemplo n.º 6
0
func process(f *dump.Frame, name string) {
	preprocess(f)

	haveOutput := false

	if *flag_jpeg {
		dumpImage(f, noExt(name)+".jpg")
		haveOutput = true
	}

	if *flag_png {
		dumpImage(f, noExt(name)+".png")
		haveOutput = true
	}

	if *flag_gnuplot {
		dumpGnuplot(f, noExt(name)+".gplot")
		haveOutput = true
	}

	if *flag_gnuplotgzip {
		dumpGnuplotGZip(f, noExt(name)+".gplot.gz")
		haveOutput = true
	}

	if *flag_omf != "" {
		dumpOmf(noExt(name)+".omf", f, *flag_omf)
		haveOutput = true
	}

	if *flag_vtk != "" {
		dumpVTK(noExt(name)+".vtk", f, *flag_vtk)
		haveOutput = true
	}

	if !haveOutput || *flag_show {
		f.Fprintf(os.Stdout, *flag_format)
		haveOutput = true
	}
}
Exemplo n.º 7
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)
				}
			}
		}
	}
}
Exemplo n.º 8
0
// Writes the OMF header
func writeOmfHeader(out io.Writer, q *dump.Frame) {
	gridsize := q.Size()[1:]
	cellsize := q.MeshStep

	hdr(out, "OOMMF", "rectangular mesh v1.0")
	hdr(out, "Segment count", "1")
	hdr(out, "Begin", "Segment")

	hdr(out, "Begin", "Header")

	dsc(out, "Time", q.Time)
	hdr(out, "Title", q.DataLabel)
	hdr(out, "meshtype", "rectangular")
	hdr(out, "meshunit", q.MeshUnit)
	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")
}
Exemplo n.º 9
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)
			}
		}
	}
}
Exemplo n.º 10
0
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))
	}
}
Exemplo n.º 11
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>")
}