// PlotYxe plots the function y(x) implemented by Cb_yxe func PlotYxe(ffcn Cb_yxe, dirout, fname string, xsol, xa, xb float64, np int, xsolLbl, args string, save, show bool, extra func()) (err error) { if !save && !show { return } x := utl.LinSpace(xa, xb, np) y := make([]float64, np) for i := 0; i < np; i++ { y[i], err = ffcn(x[i]) if err != nil { return } } var ysol float64 ysol, err = ffcn(xsol) if err != nil { return } plt.Cross("") plt.Plot(x, y, args) plt.PlotOne(xsol, ysol, io.Sf("'ro', label='%s'", xsolLbl)) if extra != nil { extra() } plt.Gll("x", "y(x)", "") if save { os.MkdirAll(dirout, 0777) plt.Save(dirout + "/" + fname) } if show { plt.Show() } return }
func main() { // input data fn, fnk := io.ArgToFilename(0, "nurbs01", ".msh", true) ctrl := io.ArgToBool(1, true) ids := io.ArgToBool(2, true) useminmax := io.ArgToBool(3, false) axisequal := io.ArgToBool(4, true) xmin := io.ArgToFloat(5, 0) xmax := io.ArgToFloat(6, 0) ymin := io.ArgToFloat(7, 0) ymax := io.ArgToFloat(8, 0) eps := io.ArgToBool(9, false) npts := io.ArgToInt(10, 41) // print input table io.Pf("\n%s\n", io.ArgsTable("INPUT ARGUMENTS", "mesh filename", "fn", fn, "show control points", "ctrl", ctrl, "show ids", "ids", ids, "use xmin,xmax,ymin,ymax", "useminmax", useminmax, "enforce axis.equal", "axisequal", axisequal, "min(x)", "xmin", xmin, "max(x)", "xmax", xmax, "min(y)", "ymin", ymin, "max(y)", "ymax", ymax, "generate eps instead of png", "eps", eps, "number of divisions", "npts", npts, )) // load nurbss B := gm.ReadMsh(fnk) // plot if eps { plt.SetForEps(0.75, 500) } else { plt.SetForPng(0.75, 500, 150) } for _, b := range B { if ctrl { b.DrawCtrl2d(ids, "", "") } b.DrawElems2d(npts, ids, "", "") } if axisequal { plt.Equal() } if useminmax { plt.AxisRange(xmin, xmax, ymin, ymax) } ext := ".png" if eps { ext = ".eps" } plt.Save(fnk + ext) }
func savefig(dirout, fnk, ext string, idx int) { fn := fnk + ext if idx >= 0 { fn = io.Sf("%s_%d%s", fnk, idx, ext) } if dirout == "" { plt.Save(fn) } else { plt.SaveD(dirout, fn) } }
// PlotT plots F, G and H for varying t and fixed coordinates x func PlotT(o Func, dirout, fname string, t0, tf float64, xcte []float64, np int, args string, withG, withH, save, show bool, extra func()) { t := utl.LinSpace(t0, tf, np) y := make([]float64, np) for i := 0; i < np; i++ { y[i] = o.F(t[i], xcte) } var g, h []float64 nrow := 1 if withG { g = make([]float64, np) for i := 0; i < np; i++ { g[i] = o.G(t[i], xcte) } nrow += 1 } if withH { h = make([]float64, np) for i := 0; i < np; i++ { h[i] = o.H(t[i], xcte) } nrow += 1 } os.MkdirAll(dirout, 0777) if withG || withH { plt.Subplot(nrow, 1, 1) } plt.Plot(t, y, args) if extra != nil { extra() } plt.Gll("t", "y", "") pidx := 2 if withG { plt.Subplot(nrow, 1, pidx) plt.Plot(t, g, args) plt.Gll("t", "dy/dt", "") pidx += 1 } if withH { plt.Subplot(nrow, 1, pidx) plt.Plot(t, h, args) plt.Gll("t", "d2y/dt2", "") } if save { plt.Save(dirout + "/" + fname) } if show { plt.Show() } }
// Save saves figure func (o *Plotter) Save(typ, num string) { if o.PlotFcn != nil { o.PlotFcn() } ext := ".png" if o.UseEps { ext = ".eps" } if o.SaveFnk != "" { if o.SaveDir != "" { plt.SaveD(o.SaveDir, o.SaveFnk+typ+num+ext) return } plt.Save(o.SaveFnk + typ + num + ext) } }
func main() { // default input data fn := "nurbs01.msh" ctrl := true ids := true npts := 41 // parse flags flag.Parse() if len(flag.Args()) > 0 { fn = flag.Arg(0) } if len(flag.Args()) > 1 { ctrl = io.Atob(flag.Arg(1)) } if len(flag.Args()) > 2 { ids = io.Atob(flag.Arg(2)) } if len(flag.Args()) > 3 { npts = io.Atoi(flag.Arg(3)) } // print input data io.Pforan("Input data\n") io.Pforan("==========\n") io.Pfblue2(" fn = %v\n", fn) io.Pfblue2(" ctrl = %v\n", ctrl) io.Pfblue2(" ids = %v\n", ids) io.Pfblue2(" npts = %v\n", npts) // load nurbss fnk := io.FnKey(fn) B := gm.ReadMsh(fnk) // plot plt.SetForEps(0.75, 500) for _, b := range B { if ctrl { b.DrawCtrl2d(ids, "", "") } b.DrawElems2d(npts, ids, "", "") } plt.Equal() plt.Save(fnk + ".eps") }
// PlotX plots F and the gradient of F, Gx and Gy, for varying x and fixed t // hlZero -- highlight F(t,x) = 0 // axEqual -- use axis['equal'] func PlotX(o Func, dirout, fname string, tcte float64, xmin, xmax []float64, np int, args string, withGrad, hlZero, axEqual, save, show bool, extra func()) { if len(xmin) == 3 { chk.Panic("PlotX works in 2D only") } X, Y := utl.MeshGrid2D(xmin[0], xmax[0], xmin[1], xmax[1], np, np) F := la.MatAlloc(np, np) var Gx, Gy [][]float64 nrow := 1 if withGrad { Gx = la.MatAlloc(np, np) Gy = la.MatAlloc(np, np) nrow += 1 } x := make([]float64, 2) g := make([]float64, 2) for i := 0; i < np; i++ { for j := 0; j < np; j++ { x[0], x[1] = X[i][j], Y[i][j] F[i][j] = o.F(tcte, x) if withGrad { o.Grad(g, tcte, x) Gx[i][j] = g[0] Gy[i][j] = g[1] } } } prop, wid, dpi := 1.0, 600.0, 200 os.MkdirAll(dirout, 0777) if withGrad { prop = 2 plt.SetForPng(prop, wid, dpi) plt.Subplot(nrow, 1, 1) plt.Title("F(t,x)", "") } else { plt.SetForPng(prop, wid, dpi) } plt.Contour(X, Y, F, args) if hlZero { plt.ContourSimple(X, Y, F, false, 8, "levels=[0], linewidths=[2], colors=['yellow']") } if axEqual { plt.Equal() } if extra != nil { extra() } plt.Gll("x", "y", "") if withGrad { plt.Subplot(2, 1, 2) plt.Title("gradient", "") plt.Quiver(X, Y, Gx, Gy, args) if axEqual { plt.Equal() } plt.Gll("x", "y", "") } if save { plt.Save(dirout + "/" + fname) } if show { plt.Show() } }