// Init initialises FORM structure func (o *ReliabFORM) Init(μ, σ []float64, lrv []bool, gfcn ReliabGfcn_t, hfcn ReliabHfcn_t) { // input o.μ = μ o.σ = σ o.lrv = lrv o.gfcn = gfcn o.hfcn = hfcn // default constants o.NmaxItA = 10 o.NmaxItB = 10 o.TolA = 0.001 o.TolB = 0.001 o.NlsSilent = true o.NlsCheckJ = false o.NlsCheckJtol = 1e-9 // allocate slices nx := len(μ) chk.IntAssert(len(σ), nx) chk.IntAssert(len(lrv), nx) o.α = make([]float64, nx) o.xtmp = make([]float64, nx) o.dgdx = make([]float64, nx) }
func Test_nurbs02(tst *testing.T) { //verbose() chk.PrintTitle("nurbs02") // NURBS b := FactoryNurbs2dPlateHole() elems := b.Elements() nbasis := b.GetElemNumBasis() io.Pforan("nbasis = %v\n", nbasis) chk.IntAssert(nbasis, 9) // orders := (2,2) => nbasis = (2+1)*(2+1) = 9 // check basis and elements chk.Ints(tst, "elem[0]", elems[0], []int{2, 3, 2, 3}) chk.Ints(tst, "elem[1]", elems[1], []int{3, 4, 2, 3}) chk.Ints(tst, "ibasis0", b.IndBasis(elems[0]), []int{0, 1, 2, 4, 5, 6, 8, 9, 10}) chk.Ints(tst, "ibasis1", b.IndBasis(elems[1]), []int{1, 2, 3, 5, 6, 7, 9, 10, 11}) chk.IntAssert(b.GetElemNumBasis(), len(b.IndBasis(elems[0]))) // check derivatives b.CheckDerivs(tst, 11, 1e-5, false) // refine NURBS c := b.KrefineN(2, false) elems = c.Elements() chk.IntAssert(c.GetElemNumBasis(), len(c.IndBasis(elems[0]))) // check refined elements io.Pf("\n------------ refined -------------\n") chk.Ints(tst, "elem[0]", elems[0], []int{2, 3, 2, 3}) chk.Ints(tst, "elem[1]", elems[1], []int{3, 4, 2, 3}) chk.Ints(tst, "elem[2]", elems[2], []int{4, 5, 2, 3}) chk.Ints(tst, "elem[3]", elems[3], []int{5, 6, 2, 3}) chk.Ints(tst, "elem[4]", elems[4], []int{2, 3, 3, 4}) chk.Ints(tst, "elem[5]", elems[5], []int{3, 4, 3, 4}) chk.Ints(tst, "elem[6]", elems[6], []int{4, 5, 3, 4}) chk.Ints(tst, "elem[7]", elems[7], []int{5, 6, 3, 4}) // check refined basis chk.Ints(tst, "ibasis0", c.IndBasis(elems[0]), []int{0, 1, 2, 6, 7, 8, 12, 13, 14}) chk.Ints(tst, "ibasis1", c.IndBasis(elems[1]), []int{1, 2, 3, 7, 8, 9, 13, 14, 15}) chk.Ints(tst, "ibasis2", c.IndBasis(elems[2]), []int{2, 3, 4, 8, 9, 10, 14, 15, 16}) chk.Ints(tst, "ibasis3", c.IndBasis(elems[3]), []int{3, 4, 5, 9, 10, 11, 15, 16, 17}) chk.Ints(tst, "ibasis4", c.IndBasis(elems[4]), []int{6, 7, 8, 12, 13, 14, 18, 19, 20}) chk.Ints(tst, "ibasis5", c.IndBasis(elems[5]), []int{7, 8, 9, 13, 14, 15, 19, 20, 21}) chk.Ints(tst, "ibasis6", c.IndBasis(elems[6]), []int{8, 9, 10, 14, 15, 16, 20, 21, 22}) chk.Ints(tst, "ibasis7", c.IndBasis(elems[7]), []int{9, 10, 11, 15, 16, 17, 21, 22, 23}) // plot if chk.Verbose { io.Pf("\n------------ plot -------------\n") la := 0 + 0*b.n[0] lb := 2 + 1*b.n[0] PlotNurbs("/tmp/gosl/gm", "nurbs02a.png", b, 41, true, nil) PlotNurbsBasis("/tmp/gosl/gm", "nurbs02b.png", b, la, lb) PlotNurbsDerivs("/tmp/gosl/gm", "nurbs02c.png", b, la, lb) PlotTwoNurbs("/tmp/gosl/gm", "nurbs02d.png", b, c, 41, true, nil) } }
func Test_frees01a(tst *testing.T) { // finalise analysis process and catch errors defer End() //verbose() chk.PrintTitle("frees01a") // start simulation if !Start("data/frees01.sim", true, chk.Verbose) { chk.Panic("cannot start FE simulation") } // domain distr := false dom := NewDomain(Global.Sim.Regions[0], distr) if dom == nil { chk.Panic("cannot run FE simulation") } // set stage if !dom.SetStage(0, Global.Sim.Stages[0], distr) { chk.Panic("cannot set stage\n") } // nodes and elements chk.IntAssert(len(dom.Nodes), 62) chk.IntAssert(len(dom.Elems), 15) // vertices with "fl" seepverts := map[int]bool{3: true, 45: true, 7: true, 49: true, 11: true, 53: true, 15: true, 57: true, 19: true, 61: true, 23: true} // check dofs var seepeqs []int for _, nod := range dom.Nodes { if seepverts[nod.Vert.Id] { chk.IntAssert(len(nod.Dofs), 2) seepeqs = append(seepeqs, nod.Dofs[1].Eq) } else { chk.IntAssert(len(nod.Dofs), 1) } } sort.Ints(seepeqs) io.Pforan("seepeqs = %v\n", seepeqs) chk.Ints(tst, "seepeqs", seepeqs, []int{14, 16, 19, 30, 32, 43, 45, 56, 58, 69, 71}) // check Fmap e2 := dom.Elems[2].(*ElemP) chk.Ints(tst, "e2.Fmap", e2.Fmap, []int{14, 16, 19}) e5 := dom.Elems[5].(*ElemP) chk.Ints(tst, "e5.Fmap", e5.Fmap, []int{16, 30, 32}) e8 := dom.Elems[8].(*ElemP) chk.Ints(tst, "e8.Fmap", e8.Fmap, []int{30, 43, 45}) e11 := dom.Elems[11].(*ElemP) chk.Ints(tst, "e11.Fmap", e11.Fmap, []int{43, 56, 58}) e14 := dom.Elems[14].(*ElemP) chk.Ints(tst, "e14.Fmap", e14.Fmap, []int{56, 69, 71}) }
func Test_frees01a(tst *testing.T) { //verbose() chk.PrintTitle("frees01a") // start simulation analysis := NewFEM("data/frees01.sim", "", true, false, false, false, chk.Verbose, 0) // set stage err := analysis.SetStage(0) if err != nil { tst.Errorf("SetStage failed:\n%v", err) return } // initialise solution vectros err = analysis.ZeroStage(0, true) if err != nil { tst.Errorf("ZeroStage failed:\n%v", err) return } // domain dom := analysis.Domains[0] // nodes and elements chk.IntAssert(len(dom.Nodes), 62) chk.IntAssert(len(dom.Elems), 15) // vertices with "fl" seepverts := map[int]bool{3: true, 45: true, 7: true, 49: true, 11: true, 53: true, 15: true, 57: true, 19: true, 61: true, 23: true} // check dofs var seepeqs []int for _, nod := range dom.Nodes { if seepverts[nod.Vert.Id] { chk.IntAssert(len(nod.Dofs), 2) seepeqs = append(seepeqs, nod.Dofs[1].Eq) } else { chk.IntAssert(len(nod.Dofs), 1) } } sort.Ints(seepeqs) io.Pforan("seepeqs = %v\n", seepeqs) chk.Ints(tst, "seepeqs", seepeqs, []int{14, 16, 19, 30, 32, 43, 45, 56, 58, 69, 71}) // check Fmap e2 := dom.Elems[2].(*ElemP) chk.Ints(tst, "e2.Fmap", e2.Fmap, []int{14, 16, 19}) e5 := dom.Elems[5].(*ElemP) chk.Ints(tst, "e5.Fmap", e5.Fmap, []int{16, 30, 32}) e8 := dom.Elems[8].(*ElemP) chk.Ints(tst, "e8.Fmap", e8.Fmap, []int{30, 43, 45}) e11 := dom.Elems[11].(*ElemP) chk.Ints(tst, "e11.Fmap", e11.Fmap, []int{43, 56, 58}) e14 := dom.Elems[14].(*ElemP) chk.Ints(tst, "e14.Fmap", e14.Fmap, []int{56, 69, 71}) }
// PlotTwoVarsContour plots contour for two variables problem. len(x) == 2 // Input // dirout -- directory to save files // fnkey -- file name key for eps figure // x -- solution. can be <nil> // np -- number of points for contour // extra -- called just before saving figure // axequal -- axis.equal // vmin -- min 0 values // vmax -- max 1 values // f -- function to plot filled contour. can be <nil> // gs -- functions to plot contour @ level 0. can be <nil> func PlotTwoVarsContour(dirout, fnkey string, x []float64, np int, extra func(), axequal bool, vmin, vmax []float64, f TwoVarsFunc_t, gs ...TwoVarsFunc_t) { if fnkey == "" { return } chk.IntAssert(len(vmin), 2) chk.IntAssert(len(vmax), 2) V0, V1 := utl.MeshGrid2D(vmin[0], vmax[0], vmin[1], vmax[1], np, np) var Zf [][]float64 var Zg [][][]float64 if f != nil { Zf = la.MatAlloc(np, np) } if len(gs) > 0 { Zg = utl.Deep3alloc(len(gs), np, np) } xtmp := make([]float64, 2) for i := 0; i < np; i++ { for j := 0; j < np; j++ { xtmp[0], xtmp[1] = V0[i][j], V1[i][j] if f != nil { Zf[i][j] = f(xtmp) } for k, g := range gs { Zg[k][i][j] = g(xtmp) } } } plt.Reset() plt.SetForEps(0.8, 350) if f != nil { cmapidx := 0 plt.Contour(V0, V1, Zf, io.Sf("fsz=7, cmapidx=%d", cmapidx)) } for k, _ := range gs { plt.ContourSimple(V0, V1, Zg[k], false, 8, "zorder=5, levels=[0], colors=['yellow'], linewidths=[2], clip_on=0") } if x != nil { plt.PlotOne(x[0], x[1], "'r*', label='optimum', zorder=10") } if extra != nil { extra() } if dirout == "" { dirout = "." } plt.Cross("clr='grey'") plt.SetXnticks(11) plt.SetYnticks(11) if axequal { plt.Equal() } plt.AxisRange(vmin[0], vmax[0], vmin[1], vmax[1]) args := "leg_out='1', leg_ncol=4, leg_hlen=1.5" plt.Gll("$x_0$", "$x_1$", args) plt.SaveD(dirout, fnkey+".eps") }
// Set copies states // Note: 1) this and other states must have been pre-allocated with the same sizes // 2) this method does not check for errors func (o *OnedState) Set(other *OnedState) { o.Sig = other.Sig o.Dgam = other.Dgam o.Loading = other.Loading chk.IntAssert(len(o.Alp), len(other.Alp)) chk.IntAssert(len(o.Phi), len(other.Phi)) copy(o.Alp, other.Alp) copy(o.Phi, other.Phi) o.F = other.F }
func Test_mylab09(tst *testing.T) { //verbose() chk.PrintTitle("mylab09. arg min and max") u := []float64{1, 2, 3, -5, 60, -10, 8} imin, imax := DblArgMinMax(u) io.Pforan("imin = %v (5)\n", imin) io.Pforan("imax = %v (4)\n", imax) chk.IntAssert(imin, 5) chk.IntAssert(imax, 4) }
// SetIniIvs sets initial ivs for given values in sol and ivs map func (o *ElemUP) SetIniIvs(sol *Solution, ivs map[string][]float64) (err error) { // set p-element first err = o.P.SetIniIvs(sol, nil) if err != nil { return } // initial stresses given if _, okk := ivs["svT"]; okk { // total vertical stresses and K0 nip := len(o.U.IpsElem) svT := ivs["svT"] K0s := ivs["K0"] chk.IntAssert(len(svT), nip) chk.IntAssert(len(K0s), 1) K0 := K0s[0] // for each integration point sx := make([]float64, nip) sy := make([]float64, nip) sz := make([]float64, nip) for i, ip := range o.U.IpsElem { // compute pl @ ip err = o.P.Cell.Shp.CalcAtIp(o.P.X, ip, false) if err != nil { return } pl := 0.0 for m := 0; m < o.P.Cell.Shp.Nverts; m++ { pl += o.P.Cell.Shp.S[m] * sol.Y[o.P.Pmap[m]] } // compute effective stresses p := pl * o.P.States[i].A_sl svE := svT[i] + p shE := K0 * svE sx[i], sy[i], sz[i] = shE, svE, shE if o.Ndim == 3 { sx[i], sy[i], sz[i] = shE, shE, svE } } ivs = map[string][]float64{"sx": sx, "sy": sy, "sz": sz} } // set u-element return o.U.SetIniIvs(sol, ivs) }
func Test_hist01(tst *testing.T) { //verbose() chk.PrintTitle("hist01") lims := []float64{0, 1, 2, 3, 4, 5} hist := Histogram{Stations: lims} idx := hist.FindBin(-3.3) chk.IntAssert(idx, -1) idx = hist.FindBin(7.0) chk.IntAssert(idx, -1) for i, x := range lims { idx = hist.FindBin(x) io.Pforan("x=%g idx=%d\n", x, idx) if i < len(lims)-1 { chk.IntAssert(idx, i) } else { chk.IntAssert(idx, -1) } } idx = hist.FindBin(0.5) chk.IntAssert(idx, 0) idx = hist.FindBin(1.5) chk.IntAssert(idx, 1) idx = hist.FindBin(2.5) chk.IntAssert(idx, 2) idx = hist.FindBin(3.99999999999999) chk.IntAssert(idx, 3) idx = hist.FindBin(4.999999) chk.IntAssert(idx, 4) hist.Count([]float64{ 0, 0.1, 0.2, 0.3, 0.9, // 5 1, 1, 1, 1.2, 1.3, 1.4, 1.5, 1.99, // 8 2, 2.5, // 2 3, 3.5, // 2 4.1, 4.5, 4.9, // 3 -3, -2, -1, 5, 6, 7, 8, }, true) io.Pforan("counts = %v\n", hist.Counts) chk.Ints(tst, "counts", hist.Counts, []int{5, 8, 2, 2, 3}) labels := hist.GenLabels("%g") io.Pforan("labels = %v\n", labels) }
func Test_nurbs03(tst *testing.T) { //verbose() chk.PrintTitle("nurbs03") // NURBS b := FactoryNurbs1dCurveA() elems := b.Elements() nbasis := b.GetElemNumBasis() io.Pforan("nbasis = %v\n", nbasis) chk.IntAssert(nbasis, 4) // orders := (3,) => nbasis = (3+1) = 4 // check basis and elements chk.Ints(tst, "elem[0]", elems[0], []int{3, 4}) chk.Ints(tst, "elem[1]", elems[1], []int{4, 5}) chk.Ints(tst, "elem[2]", elems[2], []int{5, 6}) chk.Ints(tst, "ibasis0", b.IndBasis(elems[0]), []int{0, 1, 2, 3}) chk.Ints(tst, "ibasis1", b.IndBasis(elems[1]), []int{1, 2, 3, 4}) chk.Ints(tst, "ibasis2", b.IndBasis(elems[2]), []int{2, 3, 4, 5}) // refine NURBS c := b.Krefine([][]float64{ {0.15, 0.5, 0.85}, }) // plot if chk.Verbose { PlotNurbs("/tmp/gosl/gm", "nurbs03a.png", b, 41, true, nil) PlotTwoNurbs("/tmp/gosl/gm", "nurbs03b.png", b, c, 41, true, nil) } }
// RouletteSelect selects n individuals // Input: // cumprob -- cumulated probabilities (from sorted population) // sample -- a list of random numbers; can be nil // Output: // selinds -- selected individuals (indices). len(selinds) == nsel func RouletteSelect(selinds []int, cumprob []float64, sample []float64) { nsel := len(selinds) chk.IntAssertLessThanOrEqualTo(nsel, len(cumprob)) if sample == nil { var s float64 for i := 0; i < nsel; i++ { s = rand.Float64() for j, m := range cumprob { if m > s { selinds[i] = j break } } } return } chk.IntAssert(len(sample), nsel) for i, s := range sample { for j, m := range cumprob { if m > s { selinds[i] = j break } } } }
func Test_mylab09(tst *testing.T) { //verbose() chk.PrintTitle("mylab09. arg min and max and L2norm") u := []float64{1, 2, 3, -5, 60, -10, 8} imin, imax := DblArgMinMax(u) io.Pforan("imin = %v (5)\n", imin) io.Pforan("imax = %v (4)\n", imax) chk.IntAssert(imin, 5) chk.IntAssert(imax, 4) v := []float64{0, 1, 8, 0, -1, 3, 4} d := L2norm(u, v) io.Pforan("d = %v\n", d) chk.Scalar(tst, "L2(u,v)", 1e-17, d, 62.912637840103322) }
func Test_nurbs01(tst *testing.T) { //verbose() chk.PrintTitle("nurbs01") msh, err := ReadMsh("data", "nurbs01.msh", 0) if err != nil { tst.Errorf("test failed:\n%v", err) return } for _, cell := range msh.Cells { p0, p1 := cell.Shp.Nurbs.Ord(0), cell.Shp.Nurbs.Ord(1) io.Pfcyan("cell # %d : NURBS orders = (%d,%d)\n", cell.Id, p0, p1) chk.IntAssert(p0, 2) chk.IntAssert(p1, 2) } }
// CumSum returns the cumulative sum of the elements in p // Input: // p -- values // Output: // cs -- cumulated sum; pre-allocated with len(cs) == len(p) func CumSum(cs, p []float64) { if len(p) < 1 { return } chk.IntAssert(len(cs), len(p)) cs[0] = p[0] for i := 1; i < len(p); i++ { cs[i] = cs[i-1] + p[i] } }
// Point returns the x-y-z coordinates of a point on Bezier curve func (o *BezierQuad) Point(C []float64, t float64) { if len(o.Q) != 3 { chk.Panic("Point: quadratic Bezier must be initialised first (with 3 control points)") } ndim := len(o.Q[0]) chk.IntAssert(len(C), ndim) for i := 0; i < ndim; i++ { C[i] = (1.0-t)*(1.0-t)*o.Q[0][i] + 2.0*t*(1.0-t)*o.Q[1][i] + t*t*o.Q[2][i] } return }
// ConnectSet connects set of parameters func (o *Prms) ConnectSet(V []*float64, names []string, caller string) (err string) { chk.IntAssert(len(V), len(names)) for i, name := range names { prm := o.Find(name) if prm == nil { return io.Sf("cannot find parameter named %q as requested by %q\n", name, caller) } prm.Connect(V[i]) } return }
func Test_hist02(tst *testing.T) { //verbose() chk.PrintTitle("hist02") lims := []int{0, 1, 2, 3, 4, 5} hist := IntHistogram{Stations: lims} idx := hist.FindBin(-3) chk.IntAssert(idx, -1) idx = hist.FindBin(7) chk.IntAssert(idx, -1) for i, x := range lims { idx = hist.FindBin(x) io.Pforan("x=%g idx=%d\n", x, idx) if i < len(lims)-1 { chk.IntAssert(idx, i) } else { chk.IntAssert(idx, -1) } } hist.Count([]int{ 0, 0, 0, 0, 0, // 5 1, 1, 1, 1, 1, 1, 1, 1, // 8 2, 2, // 2 3, 3, // 2 4, 4, 4, // 3 -3, -2, -1, 5, 6, 7, 8, }, true) io.Pforan("counts = %v\n", hist.Counts) chk.Ints(tst, "counts", hist.Counts, []int{5, 8, 2, 2, 3}) labels := hist.GenLabels("%d") io.Pforan("labels = %v\n", labels) }
// DblsParetoMinProb compares two vectors using Pareto's optimal criterion // φ ∃ [0,1] is a scaling factor that helps v win even if it's not smaller. // If φ==0, deterministic analysis is carried out. If φ==1, probabilistic analysis is carried out. // As φ → 1, v "gets more help". // Note: (1) minimum dominates (is better) // (2) v dominates if !u_dominates func DblsParetoMinProb(u, v []float64, φ float64) (u_dominates bool) { chk.IntAssert(len(u), len(v)) var pu, pv float64 for i := 0; i < len(u); i++ { pu += ProbContestSmall(u[i], v[i], φ) pv += ProbContestSmall(v[i], u[i], φ) } pu /= float64(len(u)) if FlipCoin(pu) { u_dominates = true } return }
func Test_cubiceq02(tst *testing.T) { //verbose() chk.PrintTitle("cubiceq02. y(x) = x³ + x²") a, b, c := 1.0, 0.0, 0.0 x1, x2, x3, nx := EqCubicSolveReal(a, b, c) io.Pforan("\na=%v b=%v c=%v\n", a, b, c) io.Pfcyan("nx=%v\n", nx) io.Pfcyan("x1=%v x2=%v x3=%v\n", x1, x2, x3) chk.IntAssert(nx, 2) chk.Scalar(tst, "x1", 1e-17, x1, -1) chk.Scalar(tst, "x2", 1e-17, x2, 0) }
// SimpleOutput implements a simple output function func SimpleOutput(first bool, dx, x float64, y []float64, args ...interface{}) (err error) { chk.IntAssert(len(args), 1) res := args[0].(*Results) res.Dx = append(res.Dx, dx) res.X = append(res.X, x) ndim := len(y) if len(res.Y) == 0 { res.Y = utl.DblsAlloc(ndim, 0) } for j := 0; j < ndim; j++ { res.Y[j] = append(res.Y[j], y[j]) } return }
// FilterPairs generates 2 lists with ninds/2 items each corresponding to selected pairs // for reprodoction. Repeated indices in pairs are avoided. // Input: // selinds -- list of selected individuals len(selinds) == ninds // Output: // A and B -- [ninds/2] lists with pairs func FilterPairs(A, B []int, selinds []int) { ninds := len(selinds) chk.IntAssert(len(A), ninds/2) chk.IntAssert(len(B), ninds/2) var a, b int var aux []int for i := 0; i < ninds/2; i++ { a, b = selinds[2*i], selinds[2*i+1] if a == b { if len(aux) == 0 { aux = rnd.IntGetShuffled(selinds) } else { rnd.IntShuffle(aux) } for _, s := range aux { if s != a { b = s break } } } A[i], B[i] = a, b } }
func Test_cubiceq01(tst *testing.T) { //verbose() chk.PrintTitle("cubiceq01. y(x) = x³ - 3x² - 144x + 432") a, b, c := -3.0, -144.0, 432.0 x1, x2, x3, nx := EqCubicSolveReal(a, b, c) io.Pforan("\na=%v b=%v c=%v\n", a, b, c) io.Pfcyan("nx=%v\n", nx) io.Pfcyan("x1=%v x2=%v x3=%v\n", x1, x2, x3) chk.IntAssert(nx, 3) chk.Scalar(tst, "x1", 1e-17, x1, -12) chk.Scalar(tst, "x2", 1e-17, x2, 12) chk.Scalar(tst, "x3", 1e-14, x3, 3) }
func Test_simplechromo01(tst *testing.T) { //verbose() chk.PrintTitle("simplechromo01") rnd.Init(0) nbases := 2 for i := 0; i < 10; i++ { chromo := SimpleChromo([]float64{1, 10, 100}, nbases) io.Pforan("chromo = %v\n", chromo) chk.IntAssert(len(chromo), 3*nbases) chk.Scalar(tst, "gene0", 1e-14, chromo[0]+chromo[1], 1) chk.Scalar(tst, "gene1", 1e-14, chromo[2]+chromo[3], 10) chk.Scalar(tst, "gene2", 1e-13, chromo[4]+chromo[5], 100) } }
// Scaling computes a scaled version of the input slice with results in [0.0, 1.0] // Input: // x -- values // ds -- δs value to be added to all 's' values // tol -- tolerance for capturing xmax ≅ xmin // reverse -- compute reverse series; // i.e. 's' decreases from 1 to 0 while x goes from xmin to xmax // useinds -- if (xmax-xmin)<tol, use indices to generate the 's' slice; // otherwise, 's' will be filled with δs + zeros // Ouptut: // s -- scaled series; pre--allocated with len(s) == len(x) // xmin, xmax -- min(x) and max(x) func Scaling(s, x []float64, ds, tol float64, reverse, useinds bool) (xmin, xmax float64) { if len(x) < 2 { return } n := len(x) chk.IntAssert(len(s), n) xmin, xmax = x[0], x[0] for i := 1; i < n; i++ { if x[i] < xmin { xmin = x[i] } if x[i] > xmax { xmax = x[i] } } dx := xmax - xmin if dx < tol { if useinds { N := float64(n - 1) if reverse { for i := 0; i < n; i++ { s[i] = ds + float64(n-1-i)/N } return } for i := 0; i < n; i++ { s[i] = ds + float64(i)/N } return } for i := 0; i < n; i++ { s[i] = ds } return } if reverse { for i := 0; i < n; i++ { s[i] = ds + (xmax-x[i])/dx } return } for i := 0; i < n; i++ { s[i] = ds + (x[i]-xmin)/dx } return }
// Init initialises graph // Input: // edges -- [nedges][2] edges (connectivity) // weightsE -- [nedges] weights of edges. can be <nil> // verts -- [nverts][ndim] vertices. can be <nil> // weightsV -- [nverts] weights of vertices. can be <nil> func (o *Graph) Init(edges [][]int, weightsE []float64, verts [][]float64, weightsV []float64) { o.Edges, o.WeightsE = edges, weightsE o.Verts, o.WeightsV = verts, weightsV o.Shares = make(map[int][]int) o.Key2edge = make(map[int]int) for k, edge := range o.Edges { i, j := edge[0], edge[1] utl.IntIntsMapAppend(&o.Shares, i, k) utl.IntIntsMapAppend(&o.Shares, j, k) o.Key2edge[o.HashEdgeKey(i, j)] = k } if o.Verts != nil { chk.IntAssert(len(o.Verts), len(o.Shares)) } nv := len(o.Shares) o.Dist = utl.DblsAlloc(nv, nv) o.Next = utl.IntsAlloc(nv, nv) }
// TextHist prints a text histogram // Input: // labels -- labels // counts -- frequencies func TextHist(labels []string, counts []int, barlen int) string { // check chk.IntAssert(len(labels), len(counts)) if len(counts) < 2 { return "counts slice is too short\n" } // scale fmax := counts[0] lmax := 0 Lmax := 0 for i, f := range counts { fmax = imax(fmax, f) lmax = imax(lmax, len(labels[i])) Lmax = imax(Lmax, len(io.Sf("%d", f))) } if fmax < 1 { return io.Sf("max frequency is too small: fmax=%d\n", fmax) } scale := float64(barlen) / float64(fmax) // print sz := io.Sf("%d", lmax+1) Sz := io.Sf("%d", Lmax+1) l := "" total := 0 for i, f := range counts { l += io.Sf("%"+sz+"s | %"+Sz+"d ", labels[i], f) n := int(float64(f) * scale) if f > 0 { // TODO: improve this n += 1 } for j := 0; j < n; j++ { l += "#" } l += "\n" total += f } sz = io.Sf("%d", lmax+3) l += io.Sf("%"+sz+"s %"+Sz+"d\n", "count =", total) return l }
func Test_nurbs04(tst *testing.T) { //verbose() chk.PrintTitle("nurbs04") // NURBS a := FactoryNurbs2dPlateHole() b := a.KrefineN(2, false) c := a.KrefineN(4, false) // tolerace for normalised space comparisons tol := 1e-7 // tags a_vt := tag_verts(a, tol) a_ct := map[string]int{ "0_0": -1, "0_1": -2, } b_vt := tag_verts(b, tol) c_vt := tag_verts(c, tol) // write .msh files WriteMshD("/tmp/gosl/gm", "m_nurbs04a", []*Nurbs{a}, a_vt, a_ct, tol) WriteMshD("/tmp/gosl/gm", "m_nurbs04b", []*Nurbs{b}, b_vt, nil, tol) WriteMshD("/tmp/gosl/gm", "m_nurbs04c", []*Nurbs{c}, c_vt, nil, tol) // read .msh file back and check a_read := ReadMsh("/tmp/gosl/gm/m_nurbs04a")[0] chk.IntAssert(a_read.gnd, a.gnd) chk.Ints(tst, "p", a.p, a_read.p) chk.Ints(tst, "n", a.n, a_read.n) chk.Deep4(tst, "Q", 1.0e-17, a.Q, a_read.Q) chk.IntMat(tst, "l2i", a.l2i, a_read.l2i) // plot if chk.Verbose { PlotNurbs("/tmp/gosl/gm", "nurbs04a.png", a_read, 41, true, nil) PlotTwoNurbs("/tmp/gosl/gm", "nurbs04b.png", a, b, 41, true, nil) PlotTwoNurbs("/tmp/gosl/gm", "nurbs04c.png", a, c, 41, true, nil) } }
// Init initialises the circle func (o *Circle) Init(xc []float64, r float64) { // check: circle works in 2D only ndim := 2 chk.IntAssert(len(xc), ndim) // save input data o.Xc = make([]float64, ndim) copy(o.Xc, xc) o.R = r // allocate auxiliary variables o.Xa = make([]float64, ndim) o.Xb = make([]float64, ndim) o.e = make([]float64, ndim) o.e0 = make([]float64, ndim) o.v = make([]float64, ndim) o.p = make([]float64, ndim) o.q = make([]float64, ndim) }
func Test_circint02(tst *testing.T) { verbose() chk.PrintTitle("circint02. sloped line (increasing). 2 intersec") var circ Circle xc := []float64{8, 8} r := 9.0 circ.Init(xc, r) pa, pb := []float64{4, 2}, []float64{18, 9} ni := circ.SegmentIntersect(pa, pb) io.Pf("ni = %v\n", ni) io.Pf("pa = %v\n", pa) io.Pf("pb = %v\n", pb) io.Pf("circ.Xa = %v\n", circ.Xa) io.Pf("circ.Xb = %v\n", circ.Xb) chk.IntAssert(ni, 2) chk.Vector(tst, "circ.Xa", 1e-4, circ.Xa, []float64{2.2135, 1.10675}) chk.Vector(tst, "circ.Xb", 1e-4, circ.Xb, []float64{16.9865, 8.49325}) }
func Test_circint03(tst *testing.T) { verbose() chk.PrintTitle("circint03. sloped line (decreasing). 2 intersec") var circ Circle xc := []float64{5, -5} r := 3.0 circ.Init(xc, r) pa, pb := []float64{2, 0}, []float64{9, -11.5} ni := circ.SegmentIntersect(pa, pb) io.Pf("ni = %v\n", ni) io.Pf("pa = %v\n", pa) io.Pf("pb = %v\n", pb) io.Pf("Xa = %v\n", circ.Xa) io.Pf("Xb = %v\n", circ.Xb) chk.IntAssert(ni, 2) chk.Vector(tst, "Xa", 1e-2, circ.Xa, []float64{3.4719, -2.4181}) chk.Vector(tst, "Xb", 1e-2, circ.Xb, []float64{6.5917, -7.5435}) }