Beispiel #1
0
func run(extra *bytes.Buffer) {
	fn := "/tmp/gosl_mplotlib_go.py"
	if extra != nil {
		io.WriteFile(fn, &ea, &bb, extra)
	} else {
		io.WriteFile(fn, &ea, &bb)
	}
	cmd := exec.Command("python", fn)
	var out, serr bytes.Buffer
	cmd.Stdout = &out
	cmd.Stderr = &serr
	err := cmd.Run()
	if err != nil {
		chk.Panic(_mplotlib_err1, serr.String())
	}
	io.Pf("%s", out.String())
}
Beispiel #2
0
// SmatTriplet writes a ".smat" file that can be visualised with vismatrix
func SmatTriplet(fnkey string, t *Triplet, tol float64) {
	var bfa, bfb bytes.Buffer
	var nnz int
	for k := 0; k < t.pos; k++ {
		if math.Abs(t.x[k]) > tol {
			fmt.Fprintf(&bfb, "  %d  %d  %23.15e\n", t.i[k], t.j[k], t.x[k])
			nnz++
		}
	}
	fmt.Fprintf(&bfa, "%d  %d  %d\n", t.m, t.n, nnz)
	io.WriteFile(fnkey+".smat", &bfa, &bfb)
}
Beispiel #3
0
// SmatCCMatrix writes a ".smat" file that can be visualised with vismatrix
func SmatCCMatrix(fnkey string, a *CCMatrix) {
	var bfa, bfb bytes.Buffer
	var nnz int
	for j := 0; j < a.n; j++ {
		for p := a.p[j]; p < a.p[j+1]; p++ {
			if math.Abs(a.x[p]) > 1e-16 {
				fmt.Fprintf(&bfb, "  %d  %d  %23.15e\n", a.i[p], j, a.x[p])
				nnz++
			}
		}
	}
	fmt.Fprintf(&bfa, "%d  %d  %d\n", a.m, a.n, nnz)
	io.WriteFile(fnkey+".smat", &bfa, &bfb)
}
Beispiel #4
0
func vtu_write(geo, dat *bytes.Buffer, tidx int, label string) {
	if geo == nil || dat == nil {
		return
	}
	nv := len(verts)
	nc := len(elems)
	if label == "ips" {
		nv = len(out.Ipoints)
		nc = nv
	}
	var hdr, foo bytes.Buffer
	io.Ff(&hdr, "<?xml version=\"1.0\"?>\n<VTKFile type=\"UnstructuredGrid\" version=\"0.1\" byte_order=\"LittleEndian\">\n<UnstructuredGrid>\n")
	io.Ff(&hdr, "<Piece NumberOfPoints=\"%d\" NumberOfCells=\"%d\">\n", nv, nc)
	io.Ff(&foo, "</Piece>\n</UnstructuredGrid>\n</VTKFile>\n")
	io.WriteFile(io.Sf("%s/%s_%06d_%s.vtu", dirout, fnkey, tidx, label), &hdr, geo, dat, &foo)
}
Beispiel #5
0
// WriteSmat writes a smat matrix for vismatrix
func WriteSmat(fnkey string, a [][]float64, tol float64) {
	var bfa, bfb bytes.Buffer
	var nnz int = 0
	m := len(a)
	n := len(a[0])
	for i := 0; i < m; i++ {
		for j := 0; j < n; j++ {
			if math.Abs(a[i][j]) > tol {
				fmt.Fprintf(&bfb, "  %d  %d  %g\n", i, j, a[i][j])
				nnz++
			}
		}
	}
	fmt.Fprintf(&bfa, "%d  %d  %d\n", m, n, nnz)
	io.WriteFile(fnkey+".smat", &bfa, &bfb)
}
Beispiel #6
0
func main() {

	// input data
	simfn := "elast.sim"
	matname := "lrm1"
	pcmax := 30.0
	npts := 101

	// parse flags
	flag.Parse()
	if len(flag.Args()) > 0 {
		simfn = flag.Arg(0)
	}
	if len(flag.Args()) > 1 {
		matname = flag.Arg(1)
	}
	if len(flag.Args()) > 2 {
		pcmax = io.Atof(flag.Arg(2))
	}
	if len(flag.Args()) > 3 {
		npts = io.Atoi(flag.Arg(3))
	}

	// check extension
	if io.FnExt(simfn) == "" {
		simfn += ".sim"
	}

	// print input data
	io.Pf("\nInput data\n")
	io.Pf("==========\n")
	io.Pf("  simfn   = %30s // simulation filename\n", simfn)
	io.Pf("  matname = %30s // material name\n", matname)
	io.Pf("  pcmax   = %30v // max pc\n", pcmax)
	io.Pf("  npts    = %30v // number of points\n", npts)
	io.Pf("\n")

	// load simulation
	sim := inp.ReadSim("", simfn, "lrm_", false)
	if sim == nil {
		io.PfRed("cannot load simulation\n")
		return
	}

	// get material data
	mat := sim.Mdb.Get(matname)
	if mat == nil {
		io.PfRed("cannot get material\n")
		return
	}
	io.Pforan("mat = %v\n", mat)

	// get and initialise model
	mdl := mreten.GetModel(simfn, matname, mat.Model, false)
	if mdl == nil {
		io.PfRed("cannot allocate model\n")
		return
	}
	mdl.Init(mat.Prms)

	// plot drying path
	d_Pc := utl.LinSpace(0, pcmax, npts)
	d_Sl := make([]float64, npts)
	d_Sl[0] = 1
	var err error
	for i := 1; i < npts; i++ {
		d_Sl[i], err = mreten.Update(mdl, d_Pc[i-1], d_Sl[i-1], d_Pc[i]-d_Pc[i-1])
		if err != nil {
			io.PfRed("drying: cannot updated model\n%v\n", err)
			return
		}
	}
	plt.Plot(d_Pc, d_Sl, io.Sf("'b-', label='%s (dry)', clip_on=0", matname))

	// plot wetting path
	w_Pc := utl.LinSpace(pcmax, 0, npts)
	w_Sl := make([]float64, npts)
	w_Sl[0] = d_Sl[npts-1]
	for i := 1; i < npts; i++ {
		w_Sl[i], err = mreten.Update(mdl, w_Pc[i-1], w_Sl[i-1], w_Pc[i]-w_Pc[i-1])
		if err != nil {
			io.PfRed("wetting: cannot updated model\n%v\n", err)
			return
		}
	}
	plt.Plot(w_Pc, w_Sl, io.Sf("'c-', label='%s (wet)', clip_on=0", matname))

	// save results
	type Results struct{ Pc, Sl []float64 }
	res := Results{append(d_Pc, w_Pc...), append(d_Sl, w_Sl...)}
	var buf bytes.Buffer
	enc := json.NewEncoder(&buf)
	err = enc.Encode(&res)
	if err != nil {
		io.PfRed("cannot encode results\n")
		return
	}
	fn := path.Join(sim.Data.DirOut, matname+".dat")
	io.WriteFile(fn, &buf)
	io.Pf("file <%s> written\n", fn)

	// show figure
	plt.AxisYrange(0, 1)
	plt.Cross()
	plt.Gll("$p_c$", "$s_{\\ell}$", "")
	plt.Show()
}
Beispiel #7
0
func main() {

	// catch errors
	defer func() {
		if err := recover(); err != nil {
			io.PfRed("ERROR: %v\n", err)
		}
	}()

	// input data
	simfn, _ := io.ArgToFilename(0, "elast", ".sim", true)
	matname := io.ArgToString(1, "lrm1")
	pcmax := io.ArgToFloat(2, 30.0)
	npts := io.ArgToInt(3, 101)

	// print input table
	io.Pf("\n%s\n", io.ArgsTable(
		"simulation filename", "simfn", simfn,
		"material name", "matname", matname,
		"max pc", "pcmax", pcmax,
		"number of points", "npts", npts,
	))

	// load simulation
	sim := inp.ReadSim(simfn, "lrm", false, 0)
	if sim == nil {
		io.PfRed("cannot load simulation\n")
		return
	}

	// get material data
	mat := sim.MatParams.Get(matname)
	if mat == nil {
		io.PfRed("cannot get material\n")
		return
	}
	io.Pforan("mat = %v\n", mat)

	// get and initialise model
	mdl := mreten.GetModel(simfn, matname, mat.Model, false)
	if mdl == nil {
		io.PfRed("cannot allocate model\n")
		return
	}
	mdl.Init(mat.Prms)

	// plot drying path
	d_Pc := utl.LinSpace(0, pcmax, npts)
	d_Sl := make([]float64, npts)
	d_Sl[0] = 1
	var err error
	for i := 1; i < npts; i++ {
		d_Sl[i], err = mreten.Update(mdl, d_Pc[i-1], d_Sl[i-1], d_Pc[i]-d_Pc[i-1])
		if err != nil {
			io.PfRed("drying: cannot updated model\n%v\n", err)
			return
		}
	}
	plt.Plot(d_Pc, d_Sl, io.Sf("'b-', label='%s (dry)', clip_on=0", matname))

	// plot wetting path
	w_Pc := utl.LinSpace(pcmax, 0, npts)
	w_Sl := make([]float64, npts)
	w_Sl[0] = d_Sl[npts-1]
	for i := 1; i < npts; i++ {
		w_Sl[i], err = mreten.Update(mdl, w_Pc[i-1], w_Sl[i-1], w_Pc[i]-w_Pc[i-1])
		if err != nil {
			io.PfRed("wetting: cannot updated model\n%v\n", err)
			return
		}
	}
	plt.Plot(w_Pc, w_Sl, io.Sf("'c-', label='%s (wet)', clip_on=0", matname))

	// save results
	type Results struct{ Pc, Sl []float64 }
	res := Results{append(d_Pc, w_Pc...), append(d_Sl, w_Sl...)}
	var buf bytes.Buffer
	enc := json.NewEncoder(&buf)
	err = enc.Encode(&res)
	if err != nil {
		io.PfRed("cannot encode results\n")
		return
	}
	fn := path.Join(sim.Data.DirOut, matname+".dat")
	io.WriteFile(fn, &buf)
	io.Pf("file <%s> written\n", fn)

	// show figure
	plt.AxisYrange(0, 1)
	plt.Cross("")
	plt.Gll("$p_c$", "$s_{\\ell}$", "")
	plt.Show()
}
Beispiel #8
0
// plot results corresponding to one run
func Plot(dirout, fnkey, method string, bres *bytes.Buffer, ycps []int, ndim int, ycfcn Cb_ycorr, xa, xb float64,
	withdx, show bool, extra string) {

	// save file with results
	os.MkdirAll(dirout, 0777)
	if bres != nil {
		io.WriteFileD(dirout, fnkey+".res", bres)
	}

	// new python script
	var b bytes.Buffer
	fmt.Fprintf(&b, "from gosl import *\n")
	fmt.Fprintf(&b, "d = Read('%s/%s.res')\n", dirout, fnkey)

	// closed-form solution
	var xc []float64
	if ycfcn != nil {
		np := 101
		dx := (xb - xa) / float64(np-1)
		fmt.Fprintf(&b, "yc = array([\n")
		xc = make([]float64, np)
		yc := make([]float64, ndim)
		for i := 0; i < np; i++ {
			xc[i] = xa + dx*float64(i)
			ycfcn(yc, xc[i])
			fmt.Fprintf(&b, "[")
			for j := 0; j < ndim; j++ {
				if j == ndim-1 {
					fmt.Fprintf(&b, "%g", yc[j])
				} else {
					fmt.Fprintf(&b, "%g,", yc[j])
				}
			}
			if i == np-1 {
				fmt.Fprintf(&b, "]")
			} else {
				fmt.Fprintf(&b, "],\n")
			}
		}
		fmt.Fprintf(&b, "])\n")
		fmt.Fprintf(&b, "xc = array([")
		for i := 0; i < np; i++ {
			if i == np-1 {
				fmt.Fprintf(&b, "%g", xc[i])
			} else {
				fmt.Fprintf(&b, "%g,", xc[i])
			}
		}
		fmt.Fprintf(&b, "])\n")
	}

	// number of subplots
	nplt := len(ycps)
	if withdx {
		nplt += 1
	}

	// plot
	for i, cp := range ycps {
		fmt.Fprintf(&b, "subplot(%d,1,%d)\n", nplt, i+1)
		if ycfcn != nil {
			fmt.Fprintf(&b, "plot(xc, yc[:,%d], 'y-', lw=6, clip_on=0, label='solution')\n", cp)
		}
		fmt.Fprintf(&b, "plot(d['x'], d['y%d'], 'b-', marker='.', lw=1, clip_on=0, label='%s')\n", cp, method)
		fmt.Fprintf(&b, "Gll('x', 'y%d')\n", cp)
	}
	if withdx {
		fmt.Fprintf(&b, "subplot(%d,1,%d)\n", nplt, len(ycps)+1)
		fmt.Fprintf(&b, "plot(d['x'], d['dx'], 'b-', marker='.', lw=1, clip_on=0, label='%s')\n", method)
		fmt.Fprintf(&b, "gca().set_yscale('log')\n")
		fmt.Fprintf(&b, "Gll('x', 'step size')\n")
	}
	fmt.Fprintf(&b, extra)
	fmt.Fprintf(&b, "show()\n")

	// write file
	fn := fmt.Sprintf("%s/%s.py", dirout, fnkey)
	io.WriteFile(fn, &b)

	// run script
	if show {
		_, err := exec.Command("python", fn).Output()
		if err != nil {
			chk.Panic("failed when calling python %s\n%v", fn, err)
		}
	}
}