// PlotStress plots stresses along y=0 (horizontal line) and x=0 (vertical line) func (o *PlateHole) PlotStress(t, L float64, npts int) { d := utl.LinSpace(o.r, L, npts) Sx := make([]float64, npts) Sy := make([]float64, npts) Sxy := make([]float64, npts) plt.Subplot(2, 1, 1) for i := 0; i < npts; i++ { Sx[i], Sy[i], _, Sxy[i] = o.Stress(t, []float64{d[i], 0}) // y=0 } plt.Plot(d, Sx, "color='r',label='$\\sigma_x$ @ $y=0$'") plt.Plot(d, Sy, "color='g',label='$\\sigma_y$ @ $y=0$'") plt.Plot(d, Sxy, "color='b',label='$\\sigma_{xy}$ @ $y=0$'") plt.Gll("$x$", "stresses", "") plt.Subplot(2, 1, 2) for i := 0; i < npts; i++ { Sx[i], Sy[i], _, Sxy[i] = o.Stress(t, []float64{0, d[i]}) // x=0 } plt.Plot(Sx, d, "color='r',label='$\\sigma_x$ @ $x=0$'") plt.Plot(Sy, d, "color='g',label='$\\sigma_y$ @ $x=0$'") plt.Plot(Sxy, d, "color='b',label='$\\sigma_{xy}$ @ $x=0$'") plt.Gll("stresses", "$y$", "") }
// PlotTwoNurbs plots two NURBS for comparison func PlotTwoNurbs(dirout, fn string, b, c *Nurbs, npts int, ids bool, extra func()) { plt.Reset() if io.FnExt(fn) == ".eps" { plt.SetForEps(1.5, 500) } else { plt.SetForPng(1.5, 500, 150) } plt.Subplot(3, 1, 1) b.DrawCtrl2d(ids, "", "") b.DrawElems2d(npts, ids, "", "") if extra != nil { extra() } plt.Equal() plt.Subplot(3, 1, 2) c.DrawCtrl2d(ids, "", "") c.DrawElems2d(npts, ids, "", "") plt.Equal() plt.Subplot(3, 1, 3) b.DrawElems2d(npts, ids, ", lw=3", "") c.DrawElems2d(npts, ids, ", color='red', marker='+', markevery=10", "color='green', size=7, va='bottom'") plt.Equal() plt.SaveD(dirout, fn) }
func Test_functions03(tst *testing.T) { //verbose() chk.PrintTitle("functions03") eps := 1e-2 f := func(x float64) float64 { return Sabs(x, eps) } ff := func(x float64) float64 { return SabsD1(x, eps) } np := 401 //x := utl.LinSpace(-5e5, 5e5, np) //x := utl.LinSpace(-5e2, 5e2, np) x := utl.LinSpace(-5e1, 5e1, np) Y := make([]float64, np) y := make([]float64, np) g := make([]float64, np) h := make([]float64, np) tolg, tolh := 1e-6, 1e-5 with_err := false for i := 0; i < np; i++ { Y[i] = math.Abs(x[i]) y[i] = Sabs(x[i], eps) g[i] = SabsD1(x[i], eps) h[i] = SabsD2(x[i], eps) gnum := numderiv(f, x[i]) hnum := numderiv(ff, x[i]) errg := math.Abs(g[i] - gnum) errh := math.Abs(h[i] - hnum) clrg, clrh := "[1;32m", "[1;32m" if errg > tolg { clrg, with_err = "[1;31m", true } if errh > tolh { clrh, with_err = "[1;31m", true } io.Pf("errg = %s%23.15e errh = %s%23.15e[0m\n", clrg, errg, clrh, errh) } if with_err { chk.Panic("errors found") } if false { //if true { plt.Subplot(3, 1, 1) plt.Plot(x, y, "'k--', label='abs'") plt.Plot(x, y, "'b-', label='sabs'") plt.Gll("x", "y", "") plt.Subplot(3, 1, 2) plt.Plot(x, g, "'b-', label='sabs'") plt.Gll("x", "dy/dx", "") plt.Subplot(3, 1, 3) plt.Plot(x, h, "'b-', label='sabs'") plt.Gll("x", "d2y/dx2", "") plt.Show() } }
// PlotFltFlt plots flt-flt contour // use iFlt==-1 || jFlt==-1 to plot all combinations func (o *Optimiser) PlotFltFltContour(sols0 []*Solution, iFlt, jFlt, iOva int, pp *PlotParams) { best, _ := GetBestFeasible(o, iOva) plotAll := iFlt < 0 || jFlt < 0 plotCommands := func(i, j int) { o.PlotContour(i, j, iOva, pp) if sols0 != nil { o.PlotAddFltFlt(i, j, sols0, &pp.FmtSols0) } o.PlotAddFltFlt(i, j, o.Solutions, &pp.FmtSols) if best != nil { plt.PlotOne(best.Flt[i], best.Flt[j], pp.FmtBest.GetArgs("")) } if pp.Extra != nil { pp.Extra() } if pp.AxEqual { plt.Equal() } } if plotAll { idx := 1 ncol := o.Nflt - 1 for row := 0; row < o.Nflt; row++ { idx += row for col := row + 1; col < o.Nflt; col++ { plt.Subplot(ncol, ncol, idx) plt.SplotGap(0.0, 0.0) plotCommands(col, row) if col > row+1 { plt.SetXnticks(0) plt.SetYnticks(0) } else { plt.Gll(io.Sf("$x_{%d}$", col), io.Sf("$x_{%d}$", row), "leg=0") } idx++ } } idx = ncol*(ncol-1) + 1 plt.Subplot(ncol, ncol, idx) plt.AxisOff() // TODO: fix formatting of open marker, add star to legend plt.DrawLegend([]plt.Fmt{pp.FmtSols0, pp.FmtSols, pp.FmtBest}, 8, "center", false, "") } else { plotCommands(iFlt, jFlt) if pp.Xlabel == "" { plt.Gll(io.Sf("$x_{%d}$", iFlt), io.Sf("$x_{%d}$", jFlt), pp.LegPrms) } else { plt.Gll(pp.Xlabel, pp.Ylabel, pp.LegPrms) } } plt.SaveD(pp.DirOut, pp.FnKey+pp.FnExt) }
// Plot plot results func Plot(dirout, fn string, res *Results, yfcn Cb_ycorr, xa, xb float64, argsAna, argsNum string, extra func()) { // data if res == nil { return } ndim := len(res.Y) if ndim < 1 { return } // closed-form solution var xc []float64 var Yc [][]float64 if yfcn != nil { np := 101 dx := (xb - xa) / float64(np-1) xc = make([]float64, np) Yc = utl.DblsAlloc(np, ndim) for i := 0; i < np; i++ { xc[i] = xa + dx*float64(i) yfcn(Yc[i], xc[i]) } } // plot if argsAna == "" { argsAna = "'y-', lw=6, label='analytical', clip_on=0" } if argsNum == "" { argsNum = "'b-', marker='.', lw=1, clip_on=0" } for j := 0; j < ndim; j++ { plt.Subplot(ndim+1, 1, j+1) if yfcn != nil { plt.Plot(xc, Yc[j], argsAna) } plt.Plot(res.X, res.Y[j], argsNum+","+io.Sf("label='%s'", res.Method)) plt.Gll("$x$", "$y$", "") } plt.Subplot(ndim+1, 1, ndim+1) plt.Plot(res.X, res.Dx, io.Sf("'b-', marker='.', lw=1, clip_on=0, label='%s'", res.Method)) plt.SetYlog() plt.Gll("$x$", "$\\log(\\delta x)$", "") // write file if extra != nil { extra() } 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() } }
func do_plot_nurbs_refined(b, c *Nurbs) { plt.SetForEps(1.5, 400) plt.Subplot(3, 1, 1) b.DrawCtrl2D(true) b.DrawElems2D(21, true, "", "") plt.Equal() plt.Subplot(3, 1, 2) c.DrawCtrl2D(true) c.DrawElems2D(21, true, "", "") plt.Equal() plt.Subplot(3, 1, 3) b.DrawElems2D(21, true, ", lw=3", "") c.DrawElems2D(21, true, ", color='red', marker='+', markevery=10", "color='magenta', size=8, va='bottom'") plt.Equal() }
// subplot sets subplot func (o *Plotter) Subplot() { if o.Split { plt.Clf() return } plt.Subplot(o.Nrow, o.Ncol, o.Pidx) o.Pidx += 1 }
func plot_normal(μ, σ float64) { var dist DistNormal dist.Init(&VarData{M: μ, S: σ}) n := 101 x := utl.LinSpace(-2, 2, n) y := make([]float64, n) Y := make([]float64, n) for i := 0; i < n; i++ { y[i] = dist.Pdf(x[i]) Y[i] = dist.Cdf(x[i]) } plt.Subplot(2, 1, 1) plt.Plot(x, y, io.Sf("clip_on=0,zorder=10,label=r'$\\mu=%g,\\;\\sigma=%g$'", μ, σ)) plt.Gll("$x$", "$f(x)$", "leg_out=1, leg_ncol=2") plt.Subplot(2, 1, 2) plt.Plot(x, Y, io.Sf("clip_on=0,zorder=10,label=r'$\\mu=%g,\\;\\sigma=%g$'", μ, σ)) plt.Gll("$x$", "$F(x)$", "leg_out=1, leg_ncol=2") }
// 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() } }
func plot_frechet(l, c, a float64, xmin, xmax float64) { var dist DistFrechet dist.Init(&VarData{L: l, C: c, A: a}) n := 101 x := utl.LinSpace(xmin, xmax, n) y := make([]float64, n) Y := make([]float64, n) for i := 0; i < n; i++ { y[i] = dist.Pdf(x[i]) Y[i] = dist.Cdf(x[i]) } plt.Subplot(2, 1, 1) plt.Plot(x, y, io.Sf("clip_on=0,zorder=10,label=r'$(%g,%g,%g)$'", l, c, a)) plt.Gll("$x$", "$f(x)$", "leg_out=1, leg_ncol=4, leg_hlen=1") plt.SetYnticks(11) plt.Subplot(2, 1, 2) plt.Plot(x, Y, io.Sf("clip_on=0,zorder=10,label=r'$(%g,%g,%g)$'", l, c, a)) plt.Gll("$x$", "$F(x)$", "leg_out=1, leg_ncol=4, leg_hlen=1") }
func plot_uniform(A, B float64, xmin, xmax float64) { var dist DistUniform dist.Init(&VarData{Min: A, Max: B}) n := 101 x := utl.LinSpace(xmin, xmax, n) y := make([]float64, n) Y := make([]float64, n) for i := 0; i < n; i++ { y[i] = dist.Pdf(x[i]) Y[i] = dist.Cdf(x[i]) } plt.Subplot(2, 1, 1) plt.Plot(x, y, io.Sf("clip_on=0,zorder=10,label=r'$(%g,%g)$'", A, B)) plt.Gll("$x$", "$f(x)$", "leg_out=1, leg_ncol=4, leg_hlen=1") plt.SetYnticks(11) plt.Subplot(2, 1, 2) plt.Plot(x, Y, io.Sf("clip_on=0,zorder=10,label=r'$(%g,%g)$'", A, B)) plt.Gll("$x$", "$F(x)$", "leg_out=1, leg_ncol=4, leg_hlen=1") }
// PlotNurbsDerivs plots derivatives of basis functions la and lb func PlotNurbsDerivs(dirout, fn string, b *Nurbs, la, lb int) { npts := 41 plt.Reset() if io.FnExt(fn) == ".eps" { plt.SetForEps(1.5, 500) } else { plt.SetForPng(1.5, 600, 150) } plt.Subplot(4, 2, 1) t0 := time.Now() b.PlotDeriv(la, 0, "", npts, 0) // 0 => CalcBasisAndDerivs io.Pfcyan("time elapsed (calcbasis) = %v\n", time.Now().Sub(t0)) plt.Equal() plt.Subplot(4, 2, 2) t0 = time.Now() b.PlotDeriv(la, 0, "", npts, 1) // 1 => NumericalDeriv io.Pfcyan("time elapsed (numerical) = %v\n", time.Now().Sub(t0)) plt.Equal() plt.Subplot(4, 2, 3) b.PlotDeriv(la, 1, "", npts, 0) // 0 => CalcBasisAndDerivs plt.Equal() plt.Subplot(4, 2, 4) b.PlotDeriv(la, 1, "", npts, 1) // 0 => NumericalDeriv plt.Equal() plt.Subplot(4, 2, 5) b.PlotDeriv(lb, 0, "", npts, 0) // 0 => CalcBasisAndDerivs plt.Equal() plt.Subplot(4, 2, 6) b.PlotDeriv(lb, 0, "", npts, 1) // 0 => NumericalDeriv plt.Equal() plt.Subplot(4, 2, 7) b.PlotDeriv(lb, 1, "", npts, 0) // 0 => CalcBasisAndDerivs plt.Equal() plt.Subplot(4, 2, 8) b.PlotDeriv(lb, 1, "", npts, 1) // 0 => NumericalDeriv plt.Equal() plt.SaveD(dirout, fn) }
func Test_bspline01(tst *testing.T) { //verbose() chk.PrintTitle("bspline01") var s1 Bspline T1 := []float64{0, 0, 0, 1, 1, 1} s1.Init(T1, 2) s1.SetControl([][]float64{{0, 0}, {0.5, 1}, {1, 0}}) var s2 Bspline T2 := []float64{0, 0, 0, 0.5, 1, 1, 1} s2.Init(T2, 2) s2.SetControl([][]float64{{0, 0}, {0.25, 0.5}, {0.75, 0.5}, {1, 0}}) if chk.Verbose { npts := 201 plt.SetForPng(1.5, 600, 150) plt.SplotGap(0.2, 0.4) str0 := ",lw=2" str1 := ",ls='none',marker='+',color='cyan',markevery=10" str2 := ",ls='none',marker='x',markevery=10" str3 := ",ls='none',marker='+',markevery=10" str4 := ",ls='none',marker='4',markevery=10" plt.Subplot(3, 2, 1) s1.Draw2d(str0, "", npts, 0) // 0 => CalcBasis s1.Draw2d(str1, "", npts, 1) // 1 => RecursiveBasis plt.Subplot(3, 2, 2) plt.SetAxis(0, 1, 0, 1) s2.Draw2d(str0, "", npts, 0) // 0 => CalcBasis s2.Draw2d(str1, "", npts, 1) // 1 => RecursiveBasis plt.Subplot(3, 2, 3) s1.PlotBasis("", npts, 0) // 0 => CalcBasis s1.PlotBasis(str2, npts, 1) // 1 => CalcBasisAndDerivs s1.PlotBasis(str3, npts, 2) // 2 => RecursiveBasis plt.Subplot(3, 2, 4) s2.PlotBasis("", npts, 0) // 0 => CalcBasis s2.PlotBasis(str2, npts, 1) // 1 => CalcBasisAndDerivs s2.PlotBasis(str3, npts, 2) // 2 => RecursiveBasis plt.Subplot(3, 2, 5) s1.PlotDerivs("", npts, 0) // 0 => CalcBasisAndDerivs s1.PlotDerivs(str4, npts, 1) // 1 => NumericalDeriv plt.Subplot(3, 2, 6) s2.PlotDerivs("", npts, 0) // 0 => CalcBasisAndDerivs s2.PlotDerivs(str4, npts, 1) // 1 => NumericalDeriv plt.SaveD("/tmp/gosl/gm", "bspline01.png") } }
func Test_dist_lognormal_03(tst *testing.T) { //verbose() chk.PrintTitle("dist_lognormal_03. random numbers") μ := 1.0 σ := 0.25 nsamples := 1000 X := make([]float64, nsamples) for i := 0; i < nsamples; i++ { X[i] = Lognormal(μ, σ) } nstations := 41 xmin := 0.0 xmax := 3.0 dx := (xmax - xmin) / float64(nstations-1) var hist Histogram hist.Stations = utl.LinSpace(xmin, xmax, nstations) hist.Count(X, true) prob := make([]float64, nstations) for i := 0; i < nstations-1; i++ { prob[i] = float64(hist.Counts[i]) / (float64(nsamples) * dx) } io.Pf(TextHist(hist.GenLabels("%.3f"), hist.Counts, 60)) io.Pforan("dx = %v\n", dx) area := 0.0 for i := 0; i < nstations-1; i++ { area += dx * prob[i] } io.Pforan("area = %v\n", area) chk.Scalar(tst, "area", 1e-15, area, 1) if chk.Verbose { plt.SetForEps(1.5, 300) plot_lognormal(μ, σ) plt.Subplot(2, 1, 1) hist.PlotDensity(nil, "") plt.SaveD("/tmp/gosl", "rnd_dist_lognormal_03.eps") } }
// PlotNurbsBasis plots basis functions la and lb func PlotNurbsBasis(dirout, fn string, b *Nurbs, la, lb int) { npts := 41 plt.Reset() if io.FnExt(fn) == ".eps" { plt.SetForEps(1.5, 500) } else { plt.SetForPng(1.5, 600, 150) } plt.Subplot(3, 2, 1) b.DrawCtrl2d(false, "", "") b.DrawElems2d(npts, false, "", "") t0 := time.Now() b.PlotBasis(la, "", 11, 0) // 0 => CalcBasis io.Pfcyan("time elapsed (calcbasis) = %v\n", time.Now().Sub(t0)) plt.Equal() plt.Subplot(3, 2, 2) b.DrawCtrl2d(false, "", "") b.DrawElems2d(npts, false, "", "") b.PlotBasis(lb, "", 11, 0) // 0 => CalcBasis plt.Equal() plt.Subplot(3, 2, 3) b.DrawCtrl2d(false, "", "") b.DrawElems2d(npts, false, "", "") b.PlotBasis(la, "", 11, 1) // 1 => CalcBasisAndDerivs plt.Equal() plt.Subplot(3, 2, 4) b.DrawCtrl2d(false, "", "") b.DrawElems2d(npts, false, "", "") b.PlotBasis(lb, "", 11, 1) // 1 => CalcBasisAndDerivs plt.Equal() plt.Subplot(3, 2, 5) b.DrawCtrl2d(false, "", "") b.DrawElems2d(npts, false, "", "") t0 = time.Now() b.PlotBasis(la, "", 11, 2) // 2 => RecursiveBasis io.Pfcyan("time elapsed (recursive) = %v\n", time.Now().Sub(t0)) plt.Equal() plt.Subplot(3, 2, 6) b.DrawCtrl2d(false, "", "") b.DrawElems2d(npts, false, "", "") b.PlotBasis(lb, "", 11, 2) // 2 => RecursiveBasis plt.Equal() plt.SaveD(dirout, fn) }
func do_plot_nurbs_derivs(b *Nurbs, la, lb int) { np := 11 plt.SetForEps(1.5, 500) plt.Subplot(4, 2, 1) t0 := time.Now() b.PlotDeriv(la, 0, "", np, 0) // 0 => CalcBasisAndDerivs io.Pfcyan("time elapsed (calcbasis) = %v\n", time.Now().Sub(t0)) plt.Equal() plt.Subplot(4, 2, 2) t0 = time.Now() b.PlotDeriv(la, 0, "", np, 1) // 1 => NumericalDeriv io.Pfcyan("time elapsed (numerical) = %v\n", time.Now().Sub(t0)) plt.Equal() plt.Subplot(4, 2, 3) b.PlotDeriv(la, 1, "", np, 0) // 0 => CalcBasisAndDerivs plt.Equal() plt.Subplot(4, 2, 4) b.PlotDeriv(la, 1, "", np, 1) // 0 => NumericalDeriv plt.Equal() plt.Subplot(4, 2, 5) b.PlotDeriv(lb, 0, "", np, 0) // 0 => CalcBasisAndDerivs plt.Equal() plt.Subplot(4, 2, 6) b.PlotDeriv(lb, 0, "", np, 1) // 0 => NumericalDeriv plt.Equal() plt.Subplot(4, 2, 7) b.PlotDeriv(lb, 1, "", np, 0) // 0 => CalcBasisAndDerivs plt.Equal() plt.Subplot(4, 2, 8) b.PlotDeriv(lb, 1, "", np, 1) // 0 => NumericalDeriv plt.Equal() }
func Test_fun01(tst *testing.T) { //verbose() chk.PrintTitle("fun01. Decreasing Reference Model") ya := 1.0 yb := -0.5 λ1 := 1.0 o, err := New("ref-dec-gen", []*Prm{ &Prm{N: "bet", V: 5.0}, &Prm{N: "a", V: -λ1}, &Prm{N: "b", V: -1.0}, &Prm{N: "c", V: ya}, &Prm{N: "A", V: 0.0}, &Prm{N: "B", V: λ1}, &Prm{N: "xini", V: 0.0}, &Prm{N: "yini", V: yb}, }) if err != nil { tst.Errorf("test failed: %v\n", err) return } tmax := 3.0 xcte := []float64{0, 0, 0} if chk.Verbose { plt.SetForPng(1.2, 400, 150) PlotT(o, "", "", 0.0, tmax, xcte, 201, "", "", "", "", "label='f'", "label='g'", "label='h'") plt.Subplot(3, 1, 1) plt.Plot([]float64{0, tmax}, []float64{ya, ya - λ1*tmax}, "'k-'") plt.Equal() plt.SaveD("/tmp/gosl/fun", "ref-dec-gen.png") } // sktol := 1e-10 dtol := 1e-10 dtol2 := 1e-10 ver := chk.Verbose CheckDerivT(tst, o, 0.0, tmax, xcte, 11, nil, sktol, dtol, dtol2, ver) }
func do_plot_nurbs_basis(b *Nurbs, la, lb int) { npts := 21 plt.SetForEps(1.2, 500) plt.Subplot(3, 2, 1) b.DrawCtrl2D(false) b.DrawElems2D(npts, false, "", "") t0 := time.Now() b.PlotBasis(la, "", 11, 0) // 0 => CalcBasis io.Pfcyan("time elapsed (calcbasis) = %v\n", time.Now().Sub(t0)) plt.Equal() plt.Subplot(3, 2, 2) b.DrawCtrl2D(false) b.DrawElems2D(npts, false, "", "") b.PlotBasis(lb, "", 11, 0) // 0 => CalcBasis plt.Equal() plt.Subplot(3, 2, 3) b.DrawCtrl2D(false) b.DrawElems2D(npts, false, "", "") b.PlotBasis(la, "", 11, 1) // 1 => CalcBasisAndDerivs plt.Equal() plt.Subplot(3, 2, 4) b.DrawCtrl2D(false) b.DrawElems2D(npts, false, "", "") b.PlotBasis(lb, "", 11, 1) // 1 => CalcBasisAndDerivs plt.Equal() plt.Subplot(3, 2, 5) b.DrawCtrl2D(false) b.DrawElems2D(npts, false, "", "") t0 = time.Now() b.PlotBasis(la, "", 11, 2) // 2 => RecursiveBasis io.Pfcyan("time elapsed (recursive) = %v\n", time.Now().Sub(t0)) plt.Equal() plt.Subplot(3, 2, 6) b.DrawCtrl2D(false) b.DrawElems2D(npts, false, "", "") b.PlotBasis(lb, "", 11, 2) // 2 => RecursiveBasis plt.Equal() }
func Test_bspline03(tst *testing.T) { //verbose() chk.PrintTitle("bspline03") // 0 1 2 3 4 5 6 7 8 9 10 T := []float64{0, 0, 0, 1, 2, 3, 4, 4, 5, 5, 5} var s Bspline s.Init(T, 2) s.SetControl([][]float64{{0, 0}, {0.5, 1}, {1, 0}, {1.5, 0}, {2, 1}, {2.5, 1}, {3, 0.5}, {3.5, 0}}) // analytical derivatives s.CalcBasisAndDerivs(3.99) io.Pfpink("ana: dNdt(t=3.99, i=5) = %v\n", s.GetDeriv(5)) io.Pfpink("ana: dNdt(t=3.99, i=6) = %v\n", s.GetDeriv(6)) io.Pfpink("ana: dNdt(t=3.99, i=7) = %v\n", s.GetDeriv(7)) s.CalcBasisAndDerivs(4.0) io.Pforan("ana: dNdt(t=4.00, i=5) = %v\n", s.GetDeriv(5)) io.Pforan("ana: dNdt(t=4.00, i=6) = %v\n", s.GetDeriv(6)) io.Pforan("ana: dNdt(t=4.00, i=7) = %v\n", s.GetDeriv(7)) // numerical derivatives io.Pfcyan("num: dNdt(t=3.99, i=5) = %v\n", s.NumericalDeriv(3.99, 5)) io.Pfcyan("num: dNdt(t=3.99, i=6) = %v\n", s.NumericalDeriv(3.99, 6)) io.Pfcyan("num: dNdt(t=3.99, i=7) = %v\n", s.NumericalDeriv(3.99, 7)) io.Pfblue2("num: dNdt(t=4.00, i=5) = %v\n", s.NumericalDeriv(4.00, 5)) io.Pfblue2("num: dNdt(t=4.00, i=6) = %v\n", s.NumericalDeriv(4.00, 6)) io.Pfblue2("num: dNdt(t=4.00, i=7) = %v\n", s.NumericalDeriv(4.00, 7)) ver := false tol := 1e-5 tt := utl.LinSpace(0, 5, 11) numd := make([]float64, s.NumBasis()) anad := make([]float64, s.NumBasis()) for _, t := range tt { for i := 0; i < s.NumBasis(); i++ { s.CalcBasisAndDerivs(t) anad[i] = s.GetDeriv(i) numd[i] = s.NumericalDeriv(t, i) // numerical fails @ 4 [4,5,6] if t == 4 { numd[4] = anad[4] numd[5] = anad[5] numd[6] = anad[6] } chk.PrintAnaNum(io.Sf("i=%d t=%v", i, t), tol, anad[i], numd[i], ver) } chk.Vector(tst, io.Sf("derivs @ %v", t), tol, numd, anad) } if chk.Verbose { npts := 201 plt.SetForPng(1.5, 600, 150) plt.SplotGap(0, 0.3) str0 := ",lw=2" str1 := ",ls='none',marker='+',color='cyan',markevery=10" str2 := ",ls='none',marker='x',markevery=10" str3 := ",ls='none',marker='+',markevery=10" str4 := ",ls='none',marker='4',markevery=10" plt.Subplot(3, 1, 1) s.Draw2d(str0, "", npts, 0) // 0 => CalcBasis s.Draw2d(str1, "", npts, 1) // 1 => RecursiveBasis plt.Subplot(3, 1, 2) s.PlotBasis("", npts, 0) // 0 => CalcBasis s.PlotBasis(str2, npts, 1) // 1 => CalcBasisAndDerivs s.PlotBasis(str3, npts, 2) // 2 => RecursiveBasis plt.Subplot(3, 1, 3) s.PlotDerivs("", npts, 0) // 0 => CalcBasisAndDerivs s.PlotDerivs(str4, npts, 1) // 1 => NumericalDeriv plt.SaveD("/tmp/gosl/gm", "bspline03.png") } }
func main() { // GA parameters opt := new(goga.Optimiser) opt.Default() opt.Nsol = 6 opt.Ncpu = 1 opt.Tf = 10 opt.EpsH = 1e-3 opt.Verbose = true opt.GenType = "latin" //opt.GenType = "halton" //opt.GenType = "rnd" opt.NormFlt = false opt.UseMesh = true opt.Nbry = 3 // define problem opt.RptName = "9" opt.RptFref = []float64{0.0539498478} opt.RptXref = []float64{-1.717143, 1.595709, 1.827247, -0.7636413, -0.7636450} opt.FltMin = []float64{-2.3, -2.3, -3.2, -3.2, -3.2} opt.FltMax = []float64{+2.3, +2.3, +3.2, +3.2, +3.2} ng, nh := 0, 3 fcn := func(f, g, h, x []float64, ξ []int, cpu int) { f[0] = math.Exp(x[0] * x[1] * x[2] * x[3] * x[4]) h[0] = x[0]*x[0] + x[1]*x[1] + x[2]*x[2] + x[3]*x[3] + x[4]*x[4] - 10.0 h[1] = x[1]*x[2] - 5.0*x[3]*x[4] h[2] = math.Pow(x[0], 3.0) + math.Pow(x[1], 3.0) + 1.0 } // check if false { f := make([]float64, 1) h := make([]float64, 3) fcn(f, nil, h, opt.RptXref, nil, 0) io.Pforan("f(xref) = %g (%g)\n", f[0], opt.RptFref[0]) io.Pforan("h0(xref) = %g\n", h[0]) io.Pforan("h1(xref) = %g\n", h[1]) io.Pforan("h2(xref) = %g\n", h[2]) } // initialise optimiser nf := 1 opt.Init(goga.GenTrialSolutions, nil, fcn, nf, ng, nh) // output function T := make([]float64, opt.Tf+1) // [nT] X := utl.Deep3alloc(opt.Nflt, opt.Nsol, opt.Tf+1) // [nx][nsol][nT] F := utl.Deep3alloc(opt.Nova, opt.Nsol, opt.Tf+1) // [nf][nsol][nT] U := utl.Deep3alloc(opt.Noor, opt.Nsol, opt.Tf+1) // [nu][nsol][nT] opt.Output = func(time int, sols []*goga.Solution) { T[time] = float64(time) for j, s := range sols { for i := 0; i < opt.Nflt; i++ { X[i][j][time] = s.Flt[i] } for i := 0; i < opt.Nova; i++ { F[i][j][time] = s.Ova[i] } for i := 0; i < opt.Noor; i++ { U[i][j][time] = s.Oor[i] } } } // initial population fnk := "one-obj-prob9-dbg" //S0 := opt.GetSolutionsCopy() goga.WriteAllValues("/tmp/goga", fnk, opt) // solve opt.Solve() // print if false { io.Pf("%13s%13s%13s%13s%10s\n", "f0", "u0", "u1", "u2", "feasible") for _, s := range opt.Solutions { io.Pf("%13.5e%13.5e%13.5e%13.5e%10v\n", s.Ova[0], s.Oor[0], s.Oor[1], s.Oor[2], s.Feasible()) } } // plot: time series //a, b := 100, len(T) a, b := 0, 1 //len(T) if false { plt.SetForEps(2.0, 400) nrow := opt.Nflt + opt.Nova + opt.Noor for j := 0; j < opt.Nsol; j++ { for i := 0; i < opt.Nflt; i++ { plt.Subplot(nrow, 1, 1+i) plt.Plot(T[a:b], X[i][j][a:b], "") plt.Gll("$t$", io.Sf("$x_%d$", i), "") } } for j := 0; j < opt.Nsol; j++ { for i := 0; i < opt.Nova; i++ { plt.Subplot(nrow, 1, 1+opt.Nflt+i) plt.Plot(T[a:b], F[i][j][a:b], "") plt.Gll("$t$", io.Sf("$f_%d$", i), "") } } for j := 0; j < opt.Nsol; j++ { for i := 0; i < opt.Noor; i++ { plt.Subplot(nrow, 1, 1+opt.Nflt+opt.Nova+i) plt.Plot(T[a:b], U[i][j][a:b], "") plt.Gll("$t$", io.Sf("$u_%d$", i), "") } } plt.SaveD("/tmp/goga", fnk+"-time.eps") } // plot: x-relationships if true { plt.SetForEps(1, 700) ncol := opt.Nflt - 1 for i := 0; i < opt.Nflt-1; i++ { for j := i + 1; j < opt.Nflt; j++ { plt.Subplot(ncol, ncol, i*ncol+j) if opt.UseMesh { opt.Meshes[i][j].CalcDerived(0) opt.Meshes[i][j].Draw2d(false, false, nil, 0) } for k := 0; k < opt.Nsol; k++ { plt.Plot(X[i][k][a:b], X[j][k][a:b], "ls='none', marker='.'") } plt.Gll(io.Sf("$x_%d$", i), io.Sf("$x_%d$", j), "") } } plt.SaveD("/tmp/goga", fnk+"-x.eps") } }
// PlotT plots F, G and H for varying t and fixed coordinates x // fname -- filename to safe figure // args{F,G,H} -- if any is "", the corresponding plot is not created func PlotT(o Func, dirout, fname string, t0, tf float64, xcte []float64, np int, labelT, labelF, labelG, labelH, argsF, argsG, argsH string) { // variables t := utl.LinSpace(t0, tf, np) var f, g, h []float64 withF := argsF != "" withG := argsG != "" withH := argsH != "" nrow := 0 // y-values if withF { f = make([]float64, np) for i := 0; i < np; i++ { f[i] = o.F(t[i], xcte) } 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 } if nrow == 0 { chk.Panic("one of args{F,G,H} must be provided") } // labels if labelT == "" { labelT = "$t$" } labelX := "" for _, x := range xcte { labelX += io.Sf(",%g", x) } if labelF == "" { labelF = "$f(t" + labelX + ")$" } if labelG == "" { labelG = "$g(t" + labelX + ")=\\frac{\\mathrm{d}f}{\\mathrm{d}t}$" } if labelH == "" { labelH = "$h(t" + labelX + ")=\\frac{\\mathrm{d}^2f}{\\mathrm{d}t^2}$" } // plot F pidx := 1 if withF { if nrow > 1 { plt.Subplot(nrow, 1, pidx) } plt.Plot(t, f, argsF+",clip_on=0") plt.Gll(labelT, labelF, "") pidx += 1 } // plot G if withG { if nrow > 1 { plt.Subplot(nrow, 1, pidx) } plt.Plot(t, g, argsG+",clip_on=0") plt.Gll(labelT, labelG, "") pidx += 1 } // plot H if withH { if nrow > 1 { plt.Subplot(nrow, 1, pidx) } plt.Plot(t, h, argsH+",clip_on=0") plt.Gll(labelT, labelH, "") } // save figure if fname != "" { plt.SaveD(dirout, fname) } }
// 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() } }
func main() { mpi.Start(false) defer func() { mpi.Stop(false) }() if mpi.Rank() == 0 { chk.PrintTitle("ode04: Hairer-Wanner VII-p376 Transistor Amplifier\n") } if mpi.Size() != 3 { chk.Panic(">> error: this test requires 3 MPI processors\n") return } // data UE, UB, UF, ALPHA, BETA := 0.1, 6.0, 0.026, 0.99, 1.0e-6 R0, R1, R2, R3, R4, R5 := 1000.0, 9000.0, 9000.0, 9000.0, 9000.0, 9000.0 R6, R7, R8, R9 := 9000.0, 9000.0, 9000.0, 9000.0 W := 2.0 * 3.141592654 * 100.0 // initial values xa := 0.0 ya := []float64{0.0, UB, UB / (R6/R5 + 1.0), UB / (R6/R5 + 1.0), UB, UB / (R2/R1 + 1.0), UB / (R2/R1 + 1.0), 0.0} // endpoint of integration xb := 0.05 //xb = 0.0123 // OK //xb = 0.01235 // !OK // right-hand side of the amplifier problem w := make([]float64, 8) // workspace fcn := func(f []float64, dx, x float64, y []float64, args ...interface{}) error { UET := UE * math.Sin(W*x) FAC1 := BETA * (math.Exp((y[3]-y[2])/UF) - 1.0) FAC2 := BETA * (math.Exp((y[6]-y[5])/UF) - 1.0) la.VecFill(f, 0) switch mpi.Rank() { case 0: f[0] = y[0] / R9 case 1: f[1] = (y[1]-UB)/R8 + ALPHA*FAC1 f[2] = y[2]/R7 - FAC1 case 2: f[3] = y[3]/R5 + (y[3]-UB)/R6 + (1.0-ALPHA)*FAC1 f[4] = (y[4]-UB)/R4 + ALPHA*FAC2 f[5] = y[5]/R3 - FAC2 f[6] = y[6]/R1 + (y[6]-UB)/R2 + (1.0-ALPHA)*FAC2 f[7] = (y[7] - UET) / R0 } mpi.AllReduceSum(f, w) return nil } // Jacobian of the amplifier problem jac := func(dfdy *la.Triplet, dx, x float64, y []float64, args ...interface{}) error { FAC14 := BETA * math.Exp((y[3]-y[2])/UF) / UF FAC27 := BETA * math.Exp((y[6]-y[5])/UF) / UF if dfdy.Max() == 0 { dfdy.Init(8, 8, 16) } NU := 2 dfdy.Start() switch mpi.Rank() { case 0: dfdy.Put(2+0-NU, 0, 1.0/R9) dfdy.Put(2+1-NU, 1, 1.0/R8) dfdy.Put(1+2-NU, 2, -ALPHA*FAC14) dfdy.Put(0+3-NU, 3, ALPHA*FAC14) dfdy.Put(2+2-NU, 2, 1.0/R7+FAC14) case 1: dfdy.Put(1+3-NU, 3, -FAC14) dfdy.Put(2+3-NU, 3, 1.0/R5+1.0/R6+(1.0-ALPHA)*FAC14) dfdy.Put(3+2-NU, 2, -(1.0-ALPHA)*FAC14) dfdy.Put(2+4-NU, 4, 1.0/R4) dfdy.Put(1+5-NU, 5, -ALPHA*FAC27) case 2: dfdy.Put(0+6-NU, 6, ALPHA*FAC27) dfdy.Put(2+5-NU, 5, 1.0/R3+FAC27) dfdy.Put(1+6-NU, 6, -FAC27) dfdy.Put(2+6-NU, 6, 1.0/R1+1.0/R2+(1.0-ALPHA)*FAC27) dfdy.Put(3+5-NU, 5, -(1.0-ALPHA)*FAC27) dfdy.Put(2+7-NU, 7, 1.0/R0) } return nil } // matrix "M" c1, c2, c3, c4, c5 := 1.0e-6, 2.0e-6, 3.0e-6, 4.0e-6, 5.0e-6 var M la.Triplet M.Init(8, 8, 14) M.Start() NU := 1 switch mpi.Rank() { case 0: M.Put(1+0-NU, 0, -c5) M.Put(0+1-NU, 1, c5) M.Put(2+0-NU, 0, c5) M.Put(1+1-NU, 1, -c5) M.Put(1+2-NU, 2, -c4) M.Put(1+3-NU, 3, -c3) case 1: M.Put(0+4-NU, 4, c3) M.Put(2+3-NU, 3, c3) M.Put(1+4-NU, 4, -c3) case 2: M.Put(1+5-NU, 5, -c2) M.Put(1+6-NU, 6, -c1) M.Put(0+7-NU, 7, c1) M.Put(2+6-NU, 6, c1) M.Put(1+7-NU, 7, -c1) } // flags silent := false fixstp := false //method := "Dopri5" method := "Radau5" ndim := len(ya) numjac := false // structure to hold numerical results res := ode.Results{Method: method} // ODE solver var osol ode.Solver osol.Pll = true // solve problem if numjac { osol.Init(method, ndim, fcn, nil, &M, ode.SimpleOutput, silent) } else { osol.Init(method, ndim, fcn, jac, &M, ode.SimpleOutput, silent) } osol.IniH = 1.0e-6 // initial step size // set tolerances atol, rtol := 1e-11, 1e-5 osol.SetTol(atol, rtol) // run t0 := time.Now() if fixstp { osol.Solve(ya, xa, xb, 0.01, fixstp, &res) } else { osol.Solve(ya, xa, xb, xb-xa, fixstp, &res) } // plot if mpi.Rank() == 0 { io.Pfmag("elapsed time = %v\n", time.Now().Sub(t0)) plt.SetForEps(2.0, 400) args := "'b-', marker='.', lw=1, clip_on=0" ode.Plot("/tmp/gosl/ode", "hwamplifier_mpi.eps", &res, nil, xa, xb, "", args, func() { _, T, err := io.ReadTable("data/radau5_hwamplifier.dat") if err != nil { chk.Panic("%v", err) } for j := 0; j < ndim; j++ { plt.Subplot(ndim+1, 1, j+1) plt.Plot(T["x"], T[io.Sf("y%d", j)], "'k+',label='reference',ms=10") } }) } }
func Test_hyperelast01(tst *testing.T) { //verbose() chk.PrintTitle("hyperelast01") var m HyperElast1 m.Init(2, false, []*fun.Prm{ &fun.Prm{N: "kap", V: 0.05}, &fun.Prm{N: "kapb", V: 20.0}, &fun.Prm{N: "G0", V: 10000}, &fun.Prm{N: "pr", V: 2.0}, &fun.Prm{N: "pt", V: 10.0}, }) io.Pforan("m = %+v\n", m) pr := m.pr pt := m.pt np := 21 Ev := utl.LinSpace(0, -0.2, np) P := make([]float64, np) Q := make([]float64, np) X := make([]float64, np) for j, ed := range []float64{0, 0.05, 0.1, 0.15, 0.2} { for i, ev := range Ev { P[i], Q[i] = m.Calc_pq(ev, ed) X[i] = math.Log(1.0 + (P[i]+pt)/pr) } slope := (Ev[0] - Ev[np-1]) / (X[np-1] - X[0]) xm := (X[0] + X[np-1]) / 2.0 ym := (Ev[0]+Ev[np-1])/2.0 - float64(j)*0.01 plt.Subplot(3, 2, 1) plt.Plot(P, Ev, io.Sf("label='$\\\\varepsilon_d=%g$'", ed)) plt.PlotOne(P[0], Ev[0], "'ro', clip_on=0") plt.Gll("$p$", "$\\varepsilon_v$", "") plt.Subplot(3, 2, 3) plt.Plot(X, Ev, "") plt.PlotOne(X[0], Ev[0], "'ro', clip_on=0") plt.Text(xm, ym, io.Sf("slope=%g", slope), "") plt.Gll("$x=\\log{[1+(p+p_t)/p_r]}$", "$\\varepsilon_v$", "") plt.Subplot(3, 2, 5) plt.Plot(Q, Ev, "") plt.PlotOne(Q[0], Ev[0], "'ro', clip_on=0") plt.Gll("$q$", "$\\varepsilon_v$", "") } Ed := utl.LinSpace(0, -0.2, np) for j, ev := range []float64{0, -0.05, -0.1, -0.15, -0.2} { for i, ed := range Ed { P[i], Q[i] = m.Calc_pq(ev, ed) X[i] = math.Log(1.0 + (P[i]+pt)/pr) } slope := (Ed[0] - Ed[np-1]) / (Q[np-1] - Q[0]) xm := (Q[0] + Q[np-1]) / 2.0 ym := (Ed[0]+Ed[np-1])/2.0 - float64(j)*0.01 plt.Subplot(3, 2, 2) plt.Plot(P, Ed, io.Sf("label='$\\\\varepsilon_v=%g$'", ev)) plt.PlotOne(P[0], Ed[0], "'ro', clip_on=0") plt.Gll("$p$", "$\\varepsilon_d$", "") plt.Subplot(3, 2, 4) plt.Plot(X, Ed, "") plt.PlotOne(X[0], Ed[0], "'ro', clip_on=0") plt.Gll("$x=\\log{[1+(p+p_t)/p_r]}$", "$\\varepsilon_d$", "") plt.Subplot(3, 2, 6) plt.Plot(Q, Ed, "") plt.PlotOne(Q[0], Ed[0], "'ro', clip_on=0") plt.Text(xm, ym, io.Sf("slope=%g", slope), "") plt.Gll("$q$", "$\\varepsilon_d$", "") } //plt.Show() }
// PlotNurbsBasis plots basis functions la and lb func PlotNurbsBasis(dirout, fn string, b *Nurbs, la, lb int) { npts := 41 ndiv := 11 if b.gnd == 1 { ndiv = 101 } plt.Reset() if io.FnExt(fn) == ".eps" { plt.SetForEps(1.5, 500) } else { plt.SetForPng(1.5, 600, 150) } plt.Subplot(3, 2, 1) if b.gnd == 2 { b.DrawCtrl2d(false, "", "") b.DrawElems2d(npts, false, "", "") } t0 := time.Now() b.PlotBasis(la, "clip_on=0", ndiv, 0) // 0 => CalcBasis io.Pfcyan("time elapsed (calcbasis) = %v\n", time.Now().Sub(t0)) if b.gnd == 2 { plt.Equal() } plt.Subplot(3, 2, 2) if b.gnd == 2 { b.DrawCtrl2d(false, "", "") b.DrawElems2d(npts, false, "", "") } b.PlotBasis(lb, "clip_on=0", ndiv, 0) // 0 => CalcBasis if b.gnd == 2 { plt.Equal() } plt.Subplot(3, 2, 3) if b.gnd == 2 { b.DrawCtrl2d(false, "", "") b.DrawElems2d(npts, false, "", "") } b.PlotBasis(la, "clip_on=0", ndiv, 1) // 1 => CalcBasisAndDerivs if b.gnd == 2 { plt.Equal() } plt.Subplot(3, 2, 4) if b.gnd == 2 { b.DrawCtrl2d(false, "", "") b.DrawElems2d(npts, false, "", "") } b.PlotBasis(lb, "clip_on=0", ndiv, 1) // 1 => CalcBasisAndDerivs if b.gnd == 2 { plt.Equal() } plt.Subplot(3, 2, 5) if b.gnd == 2 { b.DrawCtrl2d(false, "", "") b.DrawElems2d(npts, false, "", "") } t0 = time.Now() b.PlotBasis(la, "clip_on=0", ndiv, 2) // 2 => RecursiveBasis io.Pfcyan("time elapsed (recursive) = %v\n", time.Now().Sub(t0)) if b.gnd == 2 { plt.Equal() } plt.Subplot(3, 2, 6) if b.gnd == 2 { b.DrawCtrl2d(false, "", "") b.DrawElems2d(npts, false, "", "") } b.PlotBasis(lb, "clip_on=0", ndiv, 2) // 2 => RecursiveBasis if b.gnd == 2 { plt.Equal() } plt.SaveD(dirout, fn) }
// Hairer-Wanner VII-p3 Eq.(1.4) Robertson's Equation func Test_ode03(tst *testing.T) { //verbose() chk.PrintTitle("ode03: Hairer-Wanner VII-p3 Eq.(1.4) Robertson's Equation") fcn := func(f []float64, dx, x float64, y []float64, args ...interface{}) error { f[0] = -0.04*y[0] + 1.0e4*y[1]*y[2] f[1] = 0.04*y[0] - 1.0e4*y[1]*y[2] - 3.0e7*y[1]*y[1] f[2] = 3.0e7 * y[1] * y[1] return nil } jac := func(dfdy *la.Triplet, dx, x float64, y []float64, args ...interface{}) error { if dfdy.Max() == 0 { dfdy.Init(3, 3, 9) } dfdy.Start() dfdy.Put(0, 0, -0.04) dfdy.Put(0, 1, 1.0e4*y[2]) dfdy.Put(0, 2, 1.0e4*y[1]) dfdy.Put(1, 0, 0.04) dfdy.Put(1, 1, -1.0e4*y[2]-6.0e7*y[1]) dfdy.Put(1, 2, -1.0e4*y[1]) dfdy.Put(2, 0, 0.0) dfdy.Put(2, 1, 6.0e7*y[1]) dfdy.Put(2, 2, 0.0) return nil } // data silent := false fixstp := false //method := "Dopri5" method := "Radau5" xa, xb := 0.0, 0.3 ya := []float64{1.0, 0.0, 0.0} ndim := len(ya) // structure to hold numerical results res := Results{Method: method} // allocate ODE object var o Solver o.Init(method, ndim, fcn, jac, nil, SimpleOutput, silent) // tolerances and initial step size rtol := 1e-2 atol := rtol * 1e-6 o.SetTol(atol, rtol) o.IniH = 1.0e-6 // solve problem y := make([]float64, ndim) copy(y, ya) if fixstp { o.Solve(y, xa, xb, 0.01, fixstp, &res) } else { o.Solve(y, xa, xb, xb-xa, fixstp, &res) } // plot if chk.Verbose { plt.SetForEps(1.5, 400) args := "'b-', marker='.', lw=1, clip_on=0" Plot("/tmp/gosl/ode", "rober.eps", &res, nil, xa, xb, "", args, func() { _, T, err := io.ReadTable("data/rober_radau5_cpp.dat") if err != nil { chk.Panic("%v", err) } plt.Subplot(4, 1, 1) plt.Plot(T["x"], T["y0"], "'k+',label='reference',ms=10") plt.Subplot(4, 1, 2) plt.Plot(T["x"], T["y1"], "'k+',label='reference',ms=10") plt.Subplot(4, 1, 3) plt.Plot(T["x"], T["y2"], "'k+',label='reference',ms=10") }) } }
func main() { mpi.Start(false) defer func() { mpi.Stop(false) }() if mpi.Rank() == 0 { chk.PrintTitle("ode02: Hairer-Wanner VII-p5 Eq.(1.5) Van der Pol's Equation") } if mpi.Size() != 2 { chk.Panic(">> error: this test requires 2 MPI processors\n") return } eps := 1.0e-6 w := make([]float64, 2) // workspace fcn := func(f []float64, dx, x float64, y []float64, args ...interface{}) error { f[0], f[1] = 0, 0 switch mpi.Rank() { case 0: f[0] = y[1] case 1: f[1] = ((1.0-y[0]*y[0])*y[1] - y[0]) / eps } // join all f mpi.AllReduceSum(f, w) return nil } jac := func(dfdy *la.Triplet, dx, x float64, y []float64, args ...interface{}) error { if dfdy.Max() == 0 { dfdy.Init(2, 2, 4) } dfdy.Start() if false { // per column switch mpi.Rank() { case 0: dfdy.Put(0, 0, 0.0) dfdy.Put(1, 0, (-2.0*y[0]*y[1]-1.0)/eps) case 1: dfdy.Put(0, 1, 1.0) dfdy.Put(1, 1, (1.0-y[0]*y[0])/eps) } } else { // per row switch mpi.Rank() { case 0: dfdy.Put(0, 0, 0.0) dfdy.Put(0, 1, 1.0) case 1: dfdy.Put(1, 0, (-2.0*y[0]*y[1]-1.0)/eps) dfdy.Put(1, 1, (1.0-y[0]*y[0])/eps) } } return nil } // method and flags silent := false fixstp := false //method := "Dopri5" method := "Radau5" numjac := false xa, xb := 0.0, 2.0 ya := []float64{2.0, -0.6} ndim := len(ya) // structure to hold numerical results res := ode.Results{Method: method} // allocate ODE object var o ode.Solver o.Distr = true if numjac { o.Init(method, ndim, fcn, nil, nil, ode.SimpleOutput, silent) } else { o.Init(method, ndim, fcn, jac, nil, ode.SimpleOutput, silent) } // tolerances and initial step size rtol := 1e-4 atol := rtol o.IniH = 1.0e-4 o.SetTol(atol, rtol) //o.NmaxSS = 2 // solve problem y := make([]float64, ndim) copy(y, ya) t0 := time.Now() if fixstp { o.Solve(y, xa, xb, 0.05, fixstp, &res) } else { o.Solve(y, xa, xb, xb-xa, fixstp, &res) } // plot if mpi.Rank() == 0 { io.Pfmag("elapsed time = %v\n", time.Now().Sub(t0)) plt.SetForEps(1.5, 400) args := "'b-', marker='.', lw=1, ms=4, clip_on=0" ode.Plot("/tmp/gosl/ode", "vdpolA_mpi.eps", &res, nil, xa, xb, "", args, func() { _, T, err := io.ReadTable("data/vdpol_radau5_for.dat") if err != nil { chk.Panic("%v", err) } plt.Subplot(3, 1, 1) plt.Plot(T["x"], T["y0"], "'k+',label='reference',ms=7") plt.Subplot(3, 1, 2) plt.Plot(T["x"], T["y1"], "'k+',label='reference',ms=7") }) } }
func Test_lognorm03(tst *testing.T) { //verbose() chk.PrintTitle("lognorm03. Rackwitz-Fiessler conversion") dat := &VarData{D: D_Log, M: 10, S: 2} var dist DistLogNormal dist.Init(dat) dat.distr = &dist doplot := false if doplot { plt.SetForEps(1.5, 300) n := 101 x := utl.LinSpace(5, 15, n) y := make([]float64, n) Y := make([]float64, n) for i := 0; i < n; i++ { y[i] = dist.Pdf(x[i]) Y[i] = dist.Cdf(x[i]) } plt.Subplot(2, 1, 1) plt.Plot(x, y, io.Sf("clip_on=0,zorder=10,label=r'$m=%.3f,\\;s=%.3f$'", dist.M, dist.S)) plt.Gll("$x$", "$f(x)$", "leg_out=0, leg_ncol=2") plt.Subplot(2, 1, 2) plt.Plot(x, Y, io.Sf("clip_on=0,zorder=10,label=r'$m=%.3f,\\;s=%.3f$'", dist.M, dist.S)) plt.Gll("$x$", "$F(x)$", "leg_out=0, leg_ncol=2") plt.SaveD("/tmp/gosl", "test_lognorm03.eps") } for i, x := range []float64{10, 20, 50} { // Rackwitz-Fiessler f := dist.Pdf(x) F := dist.Cdf(x) io.Pforan("\nx=%g f(x)=%v F(x)=%v\n", x, f, F) var σNrf, μNrf float64 if F == 0 || F == 1 { // z = Φ⁻¹(F) → -∞ or +∞ chk.Panic("cannot compute equivalent normal parameters @ %g because F=%g", x, F) } else { z := StdInvPhi(F) σNrf = Stdphi(z) / f μNrf = x - σNrf*z if μNrf < 0 { μNrf = 0 σNrf = x / z } } // analytical solution for lognormal distribution μN, σN, invalid := dat.CalcEquiv(x) if invalid { tst.Errorf("CalcEquiv failed\n") return } // check tol := 1e-10 if i > 0 { tol = 1e-6 } err := chk.PrintAnaNum("μN", tol, μN, μNrf, chk.Verbose) if err != nil { tst.Errorf("μN values are different: %v\n", err) //return } err = chk.PrintAnaNum("σN", tol, σN, σNrf, chk.Verbose) if err != nil { tst.Errorf("σN values are different: %v\n", err) //return } } }
// Hairer-Wanner VII-p5 Eq.(1.5) Van der Pol's Equation func Test_ode02(tst *testing.T) { //verbose() chk.PrintTitle("ode02: Hairer-Wanner VII-p5 Eq.(1.5) Van der Pol's Equation") // problem definition eps := 1.0e-6 fcn := func(f []float64, dx, x float64, y []float64, args ...interface{}) error { f[0] = y[1] f[1] = ((1.0-y[0]*y[0])*y[1] - y[0]) / eps return nil } jac := func(dfdy *la.Triplet, dx, x float64, y []float64, args ...interface{}) error { if dfdy.Max() == 0 { dfdy.Init(2, 2, 4) } dfdy.Start() dfdy.Put(0, 0, 0.0) dfdy.Put(0, 1, 1.0) dfdy.Put(1, 0, (-2.0*y[0]*y[1]-1.0)/eps) dfdy.Put(1, 1, (1.0-y[0]*y[0])/eps) return nil } // method and flags silent := false fixstp := false //method := "Dopri5" method := "Radau5" numjac := false xa, xb := 0.0, 2.0 ya := []float64{2.0, -0.6} ndim := len(ya) // structure to hold numerical results res := Results{Method: method} // allocate ODE object var o Solver if numjac { o.Init(method, ndim, fcn, nil, nil, SimpleOutput, silent) } else { o.Init(method, ndim, fcn, jac, nil, SimpleOutput, silent) } // tolerances and initial step size rtol := 1e-4 atol := rtol o.IniH = 1.0e-4 o.SetTol(atol, rtol) //o.NmaxSS = 2 // solve problem y := make([]float64, ndim) copy(y, ya) t0 := time.Now() if fixstp { o.Solve(y, xa, xb, 0.05, fixstp, &res) } else { o.Solve(y, xa, xb, xb-xa, fixstp, &res) } io.Pfmag("elapsed time = %v\n", time.Now().Sub(t0)) // plot if chk.Verbose { plt.SetForEps(1.5, 400) args := "'b-', marker='.', lw=1, ms=4, clip_on=0" Plot("/tmp/gosl/ode", "vdpolA.eps", &res, nil, xa, xb, "", args, func() { _, T, err := io.ReadTable("data/vdpol_radau5_for.dat") if err != nil { chk.Panic("%v", err) } plt.Subplot(3, 1, 1) plt.Plot(T["x"], T["y0"], "'k+',label='reference',ms=7") plt.Subplot(3, 1, 2) plt.Plot(T["x"], T["y1"], "'k+',label='reference',ms=7") }) } }