// PlotDeriv plots derivative dR[i][j][k]du[d] (2D only) // option = 0 : use CalcBasisAndDerivs // 1 : use NumericalDeriv func (o *Nurbs) PlotDeriv(l, d int, args string, npts, option int) { lbls := []string{"N\\&dN", "numD"} switch o.gnd { // curve case 1: // surface case 2: xx := la.MatAlloc(npts, npts) yy := la.MatAlloc(npts, npts) zz := la.MatAlloc(npts, npts) du0 := (o.b[0].tmax - o.b[0].tmin) / float64(npts-1) du1 := (o.b[1].tmax - o.b[1].tmin) / float64(npts-1) drdu := make([]float64, 2) for m := 0; m < npts; m++ { u0 := o.b[0].tmin + float64(m)*du0 for n := 0; n < npts; n++ { u1 := o.b[1].tmin + float64(n)*du1 u := []float64{u0, u1} x := o.Point(u) xx[m][n] = x[0] yy[m][n] = x[1] switch option { case 0: o.CalcBasisAndDerivs(u) o.GetDerivL(drdu, l) case 1: o.NumericalDeriv(drdu, u, l) } zz[m][n] = drdu[d] } } plt.Title(io.Sf("%d,%d:%s", l, d, lbls[option]), "size=10") plt.Contour(xx, yy, zz, "fsz=8") } }
// PlotBasis plots basis function (2D only) // option = 0 : use CalcBasis // 1 : use CalcBasisAndDerivs // 2 : use RecursiveBasis func (o *Nurbs) PlotBasis(l int, args string, npts, option int) { lbls := []string{"Nonly", "N\\&dN", "recN"} switch o.gnd { // curve case 1: xx := make([]float64, npts) yy := make([]float64, npts) du0 := (o.b[0].tmax - o.b[0].tmin) / float64(npts-1) for m := 0; m < npts; m++ { u0 := o.b[0].tmin + float64(m)*du0 u := []float64{u0} x := o.Point(u) xx[m] = x[0] switch option { case 0: o.CalcBasis(u) yy[m] = o.GetBasisL(l) case 1: o.CalcBasisAndDerivs(u) yy[m] = o.GetBasisL(l) case 2: yy[m] = o.RecursiveBasis(u, l) } } plt.Plot(xx, yy, "fsz=8") // surface case 2: xx := la.MatAlloc(npts, npts) yy := la.MatAlloc(npts, npts) zz := la.MatAlloc(npts, npts) du0 := (o.b[0].tmax - o.b[0].tmin) / float64(npts-1) du1 := (o.b[1].tmax - o.b[1].tmin) / float64(npts-1) for m := 0; m < npts; m++ { u0 := o.b[0].tmin + float64(m)*du0 for n := 0; n < npts; n++ { u1 := o.b[1].tmin + float64(n)*du1 u := []float64{u0, u1} x := o.Point(u) xx[m][n] = x[0] yy[m][n] = x[1] switch option { case 0: o.CalcBasis(u) zz[m][n] = o.GetBasisL(l) case 1: o.CalcBasisAndDerivs(u) zz[m][n] = o.GetBasisL(l) case 2: zz[m][n] = o.RecursiveBasis(u, l) } } } plt.Contour(xx, yy, zz, "fsz=8") } plt.Title(io.Sf("%d:%s", l, lbls[option]), "size=10") }
// PlotBasis plots basis function (2D only) // option = 0 : use CalcBasis // 1 : use CalcBasisAndDerivs // 2 : use RecursiveBasis func (o *Nurbs) PlotBasis(l int, args string, npts, option int) { lbls := []string{"CalcBasis function", "CalcBasisAndDerivs function", "RecursiveBasis function"} switch o.gnd { // curve case 1: U := make([]float64, npts) S := make([]float64, npts) du := (o.b[0].tmax - o.b[0].tmin) / float64(npts-1) uvec := []float64{0} for m := 0; m < npts; m++ { U[m] = o.b[0].tmin + float64(m)*du uvec[0] = U[m] switch option { case 0: o.CalcBasis(uvec) S[m] = o.GetBasisL(l) case 1: o.CalcBasisAndDerivs(uvec) S[m] = o.GetBasisL(l) case 2: S[m] = o.RecursiveBasis(uvec, l) } } plt.Plot(U, S, args) plt.Gll("$u$", io.Sf("$S_%d$", l), "") // surface case 2: xx := la.MatAlloc(npts, npts) yy := la.MatAlloc(npts, npts) zz := la.MatAlloc(npts, npts) du0 := (o.b[0].tmax - o.b[0].tmin) / float64(npts-1) du1 := (o.b[1].tmax - o.b[1].tmin) / float64(npts-1) for m := 0; m < npts; m++ { u0 := o.b[0].tmin + float64(m)*du0 for n := 0; n < npts; n++ { u1 := o.b[1].tmin + float64(n)*du1 u := []float64{u0, u1} x := o.Point(u) xx[m][n] = x[0] yy[m][n] = x[1] switch option { case 0: o.CalcBasis(u) zz[m][n] = o.GetBasisL(l) case 1: o.CalcBasisAndDerivs(u) zz[m][n] = o.GetBasisL(l) case 2: zz[m][n] = o.RecursiveBasis(u, l) } } } plt.Contour(xx, yy, zz, "fsz=7") } plt.Title(io.Sf("%s @ %d", lbls[option], l), "size=7") }
// Draw draws or save figure with plot // dirout -- directory to save figure // fname -- file name; e.g. myplot.eps or myplot.png. Use "" to skip saving // show -- shows figure // extra -- is called just after Subplot command and before any plotting // Note: subplots will be split if using 'eps' files func Draw(dirout, fname string, show bool, extra ExtraPlt) { var fnk string // filename key var ext string // extension var eps bool // is eps figure if fname != "" { fnk = io.FnKey(fname) ext = io.FnExt(fname) eps = ext == ".eps" } nplots := len(Splots) nr, nc := utl.BestSquare(nplots) var k int for i := 0; i < nr; i++ { for j := 0; j < nc; j++ { if !eps { plt.Subplot(nr, nc, k+1) } if extra != nil { extra(i+1, j+1, nplots) } if Splots[k].Title != "" { plt.Title(Splots[k].Title, Splots[k].Topts) } data := Splots[k].Data for _, d := range data { if d.Style.L == "" { d.Style.L = d.Alias } x, y := d.X, d.Y if math.Abs(Splots[k].Xscale) > 0 { x = make([]float64, len(d.X)) la.VecCopy(x, Splots[k].Xscale, d.X) } if math.Abs(Splots[k].Yscale) > 0 { y = make([]float64, len(d.Y)) la.VecCopy(y, Splots[k].Yscale, d.Y) } plt.Plot(x, y, d.Style.GetArgs("clip_on=0")) } plt.Gll(Splots[k].Xlbl, Splots[k].Ylbl, Splots[k].GllArgs) if eps { savefig(dirout, fnk, ext, k) plt.Clf() } k += 1 } } if !eps && fname != "" { savefig(dirout, fnk, ext, -1) } if show { plt.Show() } }
// PlotDeriv plots derivative dR[i][j][k]du[d] (2D only) // option = 0 : use CalcBasisAndDerivs // 1 : use NumericalDeriv func (o *Nurbs) PlotDeriv(l, d int, args string, npts, option int) { lbls := []string{"CalcBasisAndDerivs function", "NumericalDeriv function"} switch o.gnd { // curve case 1: U := make([]float64, npts) G := make([]float64, npts) du := (o.b[0].tmax - o.b[0].tmin) / float64(npts-1) uvec := []float64{0} gvec := []float64{0} for m := 0; m < npts; m++ { U[m] = o.b[0].tmin + float64(m)*du uvec[0] = U[m] switch option { case 0: o.CalcBasisAndDerivs(uvec) o.GetDerivL(gvec, l) case 1: o.NumericalDeriv(gvec, uvec, l) } G[m] = gvec[0] } plt.Plot(U, G, args) plt.Gll("$u$", io.Sf("$G_%d$", l), "") // surface case 2: xx := la.MatAlloc(npts, npts) yy := la.MatAlloc(npts, npts) zz := la.MatAlloc(npts, npts) du0 := (o.b[0].tmax - o.b[0].tmin) / float64(npts-1) du1 := (o.b[1].tmax - o.b[1].tmin) / float64(npts-1) drdu := make([]float64, 2) for m := 0; m < npts; m++ { u0 := o.b[0].tmin + float64(m)*du0 for n := 0; n < npts; n++ { u1 := o.b[1].tmin + float64(n)*du1 u := []float64{u0, u1} x := o.Point(u) xx[m][n] = x[0] yy[m][n] = x[1] switch option { case 0: o.CalcBasisAndDerivs(u) o.GetDerivL(drdu, l) case 1: o.NumericalDeriv(drdu, u, l) } zz[m][n] = drdu[d] } } plt.Contour(xx, yy, zz, "fsz=7") } plt.Title(io.Sf("%s @ %d,%d", lbls[option], l, d), "size=7") }
// 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() } }