Пример #1
0
func Test_2dinteg02(tst *testing.T) {

	//verbose()
	chk.PrintTitle("2dinteg02. bidimensional integral")

	// Γ(1/4, 1)
	gamma_1div4_1 := 0.2462555291934987088744974330686081384629028737277219

	x := utl.LinSpace(0, 1, 11)
	y := utl.LinSpace(0, 1, 11)
	m, n := len(x), len(y)
	f := la.MatAlloc(m, n)
	for i := 0; i < m; i++ {
		for j := 0; j < n; j++ {
			f[i][j] = 8.0 * math.Exp(-math.Pow(x[i], 2)-math.Pow(y[j], 4))
		}
	}
	dx, dy := x[1]-x[0], y[1]-y[0]
	Vt := Trapz2D(dx, dy, f)
	Vs := Simps2D(dx, dy, f)
	Vc := math.Sqrt(math.Pi) * math.Erf(1) * (math.Gamma(1.0/4.0) - gamma_1div4_1)
	io.Pforan("Vt = %v\n", Vt)
	io.Pforan("Vs = %v\n", Vs)
	io.Pfgreen("Vc = %v\n", Vc)
	chk.Scalar(tst, "Vt", 0.0114830435645548, Vt, Vc)
	chk.Scalar(tst, "Vs", 1e-4, Vs, Vc)

}
Пример #2
0
func Test_munkres04(tst *testing.T) {

	//verbose()
	chk.PrintTitle("munkres04. row and column matrices")

	C := [][]float64{
		{1.0},
		{2.0},
		{0.5},
		{3.0},
		{4.0},
	}

	var mnk Munkres
	mnk.Init(len(C), len(C[0]))
	mnk.SetCostMatrix(C)
	mnk.Run()
	io.Pforan("links = %v\n", mnk.Links)
	io.Pforan("cost = %v  (13938)\n", mnk.Cost)
	chk.Ints(tst, "links", mnk.Links, []int{-1, -1, 0, -1, -1})
	chk.Scalar(tst, "cost", 1e-17, mnk.Cost, 0.5)

	C = [][]float64{
		{1.0, 2.0, 0.5, 3.0, 4.0},
	}
	mnk.Init(len(C), len(C[0]))
	mnk.SetCostMatrix(C)
	mnk.Run()
	io.Pforan("links = %v\n", mnk.Links)
	io.Pforan("cost = %v  (13938)\n", mnk.Cost)
	chk.Ints(tst, "links", mnk.Links, []int{2})
	chk.Scalar(tst, "cost", 1e-17, mnk.Cost, 0.5)
}
Пример #3
0
func Test_quad01(tst *testing.T) {

	//verbose()
	chk.PrintTitle("quad01")

	y := func(x float64) float64 {
		return math.Sqrt(1.0 + math.Pow(math.Sin(x), 3.0))
	}
	var err error
	Acor := 1.08268158558

	// trapezoidal rule
	var T Quadrature
	T = new(Trap)
	T.Init(y, 0, 1, 1e-11)
	A, err := T.Integrate()
	if err != nil {
		io.Pforan(err.Error())
	}
	io.Pforan("A  = %v\n", A)
	chk.Scalar(tst, "A", 1e-11, A, Acor)

	// Simpson's rule
	var S Quadrature
	S = new(Simp)
	S.Init(y, 0, 1, 1e-11)
	A, err = S.Integrate()
	if err != nil {
		io.Pforan(err.Error())
	}
	io.Pforan("A  = %v\n", A)
	chk.Scalar(tst, "A", 1e-11, A, Acor)
}
Пример #4
0
func Test_mylab08(tst *testing.T) {

	//verbose()
	chk.PrintTitle("mylab08. dot and cross products")

	u := []float64{3, -3, 1}
	v := []float64{4, 9, 2}
	w := make([]float64, 3)
	s := Dot3d(u, v)
	Cross3d(w, u, v) // w := u cross v
	chk.Scalar(tst, "s = u dot v", 1e-17, s, -13)
	chk.Vector(tst, "w = u cross v", 1e-17, w, []float64{-15, -2, 39})

	u = []float64{3, -3, 1}
	v = []float64{-12, 12, -4}
	s = Dot3d(u, v)
	Cross3d(w, u, v) // w := u cross v
	chk.Scalar(tst, "s = u dot v", 1e-17, s, -76)
	chk.Vector(tst, "w = u cross v", 1e-17, w, []float64{0, 0, 0})

	u = []float64{3, 2, -2}
	v = []float64{1, 0, -5}
	s = Dot3d(u, v)
	Cross3d(w, u, v) // w := u cross v
	chk.Scalar(tst, "s = u dot v", 1e-17, s, 13)
	chk.Vector(tst, "w = u cross v", 1e-17, w, []float64{-10, 13, -2})
}
Пример #5
0
func Test_invs07(tst *testing.T) {

	//verbose()
	chk.PrintTitle("invs07")

	smp_a, smp_b, smp_β, smp_ϵ := -1.0, 0.0, 1.0, 1e-3

	σ := []float64{-1, -1, 0, 0}

	pcam, qcam, _ := M_pqw(σ)
	poct, qoct := pcam*SQ3, qcam*SQ2by3

	N := make([]float64, 3)
	n := make([]float64, 3)
	m := SmpDirector(N, σ, smp_a, smp_b, smp_β, smp_ϵ)
	SmpUnitDirector(n, m, N)
	psmp1, qsmp1, err := GenInvs(σ, n, smp_a)
	if err != nil {
		chk.Panic("M_GenInvs failed:\n%v", err)
	}

	psmp2, qsmp2, err := M_pq_smp(σ, smp_a, smp_b, smp_β, smp_ϵ)
	if err != nil {
		chk.Panic("M_pq_smp failed:\n%v", err)
	}
	io.Pforan("pcam,  qcam  = %v, %v\n", pcam, qcam)
	io.Pforan("poct,  qoct  = %v, %v\n", poct, qoct)
	io.Pforan("psmp1, qsmp1 = %v, %v\n", psmp1, qsmp1)
	io.Pforan("psmp2, qsmp2 = %v, %v\n", psmp2, qsmp2)
	chk.Scalar(tst, "p", 1e-15, psmp1, psmp2)
	chk.Scalar(tst, "q", 1e-15, qsmp1, qsmp2)
}
Пример #6
0
func Test_beam01b(tst *testing.T) {

	//verbose()
	chk.PrintTitle("beam01b. simply supported")

	// start simulation
	analysis := NewFEM("data/beam01.sim", "", true, true, false, false, chk.Verbose, 0)

	// run simulation
	err := analysis.Run()
	if err != nil {
		tst.Errorf("Run failed:\n%v", err)
		return
	}

	// check
	dom := analysis.Domains[0]
	ele := dom.Elems[0].(*Beam)
	_, M := ele.CalcVandM(dom.Sol, 0.5, 1)
	qn, L := 15.0, 1.0
	Mcentre := qn * L * L / 8.0
	io.Pforan("M = %v (%v)\n", M, Mcentre)
	chk.Scalar(tst, "M @ centre", 1e-17, M[0], Mcentre)

	// check moment using OutIpsData
	idx_centre := 5 // considering 11 stations
	dat := ele.OutIpsData()
	res := dat[idx_centre].Calc(dom.Sol)
	io.Pfcyan("M @ centre (OutIpsData) = %v\n", res["M"])
	chk.Scalar(tst, "M @ centre (OutIpsData)", 1e-17, res["M"], Mcentre)
}
Пример #7
0
func TestMyLab03b(tst *testing.T) {

	//verbose()
	chk.PrintTitle("mylab03b")

	a := []int{1, 2, 3, -1, -2, 0, 8, -3}
	b := IntFilter(a, func(i int) bool {
		if a[i] < 0 {
			return true
		}
		return false
	})
	c := IntNegOut(a)
	io.Pf("a = %v\n", a)
	io.Pf("b = %v\n", b)
	io.Pf("c = %v\n", c)
	chk.Ints(tst, "b", b, []int{1, 2, 3, 0, 8})
	chk.Ints(tst, "c", c, []int{1, 2, 3, 0, 8})

	A := []float64{1, 2, 3, -1, -2, 0, 8, -3}
	s := DblSum(A)
	mi, ma := DblMinMax(A)
	io.Pf("A      = %v\n", A)
	io.Pf("sum(A) = %v\n", s)
	io.Pf("min(A) = %v\n", mi)
	io.Pf("max(A) = %v\n", ma)
	chk.Scalar(tst, "sum(A)", 1e-17, s, 8)
	chk.Scalar(tst, "min(A)", 1e-17, mi, -3)
	chk.Scalar(tst, "max(A)", 1e-17, ma, 8)
}
Пример #8
0
func Test_prms02(tst *testing.T) {

	//verbose()
	chk.PrintTitle("prms02")

	var prms Prms
	prms = []*Prm{
		&Prm{N: "klx", V: 1.0},
		&Prm{N: "kly", V: 2.0},
		&Prm{N: "klz", V: 3.0},
	}
	io.Pforan("%v\n", prms)

	var klx, kly, klz float64
	err_msg := prms.ConnectSet([]*float64{&klx, &kly, &klz}, []string{"klx", "kly", "klz"}, "Test_prms02")
	if err_msg != "" {
		tst.Error("connect set failed: %v\n", err_msg)
		return
	}

	chk.Scalar(tst, "klx", 1e-15, klx, 1.0)
	chk.Scalar(tst, "kly", 1e-15, kly, 2.0)
	chk.Scalar(tst, "klz", 1e-15, klz, 3.0)

	prms[1].Set(2.2)
	chk.Scalar(tst, "kly", 1e-15, kly, 2.2)
}
Пример #9
0
func Test_ind01(tst *testing.T) {

	//verbose()
	chk.PrintTitle("ind01. representation and copying")

	rnd.Init(0)

	nbases := 3
	A := get_individual(0, nbases)
	B := A.GetCopy()
	chk.Scalar(tst, "ova0", 1e-17, B.Ovas[0], 123)
	chk.Scalar(tst, "ova1", 1e-17, B.Ovas[1], 345)
	chk.Scalar(tst, "oor0", 1e-17, B.Oors[0], 10)
	chk.Scalar(tst, "oor1", 1e-17, B.Oors[1], 20)
	chk.Scalar(tst, "oor2", 1e-17, B.Oors[2], 30)

	fmts := map[string][]string{"int": {" %d"}, "flt": {" %.1f"}, "str": {" %q"}, "key": {" %x"}, "byt": {" %q"}, "fun": {" %q"}}

	oA := A.Output(fmts, false)
	oB := B.Output(fmts, false)
	io.Pfyel("\n%v\n", oA)
	io.Pfyel("%v\n\n", oB)
	chk.String(tst, oA, " 1 20 300 4.4 5.5 666.0 \"abc\" \"b\" \"c\" 53 47 41 \"ABC\" \"DEF\" \"GHI\" \"f0\" \"f1\" \"f2\"")
	chk.String(tst, oB, " 1 20 300 4.4 5.5 666.0 \"abc\" \"b\" \"c\" 53 47 41 \"ABC\" \"DEF\" \"GHI\" \"f0\" \"f1\" \"f2\"")

	A.SetFloat(1, 33)
	A.SetFloat(2, 88)
	oA = A.Output(fmts, false)
	io.Pfyel("\n%v\n", oA)
	chk.String(tst, oA, " 1 20 300 4.4 33.0 88.0 \"abc\" \"b\" \"c\" 53 47 41 \"ABC\" \"DEF\" \"GHI\" \"f0\" \"f1\" \"f2\"")
}
Пример #10
0
func Test_matinvSmall01(tst *testing.T) {

	//verbose()
	chk.PrintTitle("matinvSmall01")

	noise := 0.0
	tol := 1.0e-16

	// 1 x 1 matrix
	res := MatAlloc(1, 1)
	det, err := MatInv(res, [][]float64{{2.0}}, tol)
	if err != nil {
		chk.Panic("%v", err.Error())
	}
	chk.Scalar(tst, "1 x 1 matrix: det ", tol, det, 2.0)
	chk.Matrix(tst, "1 x 1 matrix: ", tol, res, [][]float64{{0.5}})

	// matrix: inverse
	A := [][]float64{{1.0, 2.0}, {3.0, 2.0}}
	Aicorr := [][]float64{{-0.5, 0.5}, {0.75, -0.25 + noise}}
	Ai := MatAlloc(2, 2)
	detA, err := MatInv(Ai, A, tol)
	if err != nil {
		chk.Panic("%v", err.Error())
	}
	chk.Scalar(tst, "matrix: inv (det) ", tol, detA, -4.0+noise)
	chk.Matrix(tst, "matrix: inv (A)   ", tol, Ai, Aicorr)

	// using MatInvG
	Ai_ := MatAlloc(2, 2)
	err = MatInvG(Ai_, A, tol)
	if err != nil {
		chk.Panic("%v", err.Error())
	}
	chk.Matrix(tst, "matrix: inv with MatInvG", tol, Ai_, Aicorr)

	// another test
	B := [][]float64{{9.0, 3.0, 5.0}, {-6.0, -9.0, 7.0}, {-1.0, -8.0, 1.0}}
	Bicorr := [][]float64{
		{7.642276422764227e-02, -6.991869918699187e-02, 1.073170731707317e-01},
		{-1.626016260162601e-03, 2.276422764227642e-02, -1.512195121951219e-01},
		{6.341463414634146e-02, 1.121951219512195e-01, -1.024390243902439e-01 + noise},
	}
	Bi := MatAlloc(3, 3)
	detB, err := MatInv(Bi, B, tol)
	if err != nil {
		chk.Panic("%v", err.Error())
	}
	chk.Scalar(tst, "matrix: det ", tol, detB, 615.0+noise)
	chk.Matrix(tst, "matrix: inv ", tol, Bi, Bicorr)

	// using MatInvG
	Bi_ := MatAlloc(3, 3)
	err = MatInvG(Bi_, B, tol)
	if err != nil {
		chk.Panic("%v", err.Error())
	}
	chk.Matrix(tst, "matrix: inv with MatInvG", tol, Bi_, Bicorr)
}
Пример #11
0
func Test_ind02(tst *testing.T) {

	//verbose()
	chk.PrintTitle("ind02. copy into")

	rnd.Init(0)

	nbases := 1
	A := get_individual(0, nbases)
	B := get_individual(1, nbases)

	fmts := map[string][]string{
		"int": {"%2d", "%4d", "%5d"}, // ints
		"flt": {"%6g", "%6g", "%5g"}, // floats
		"str": {"%4s", "%2s", "%2s"}, // strings
		"key": {"%3x", "%3x", "%3x"}, // keys
		"byt": {"%4s", "%4s", "%4s"}, // bytes
		"fun": {"%3s", "%3s", "%3s"}, // funcs
	}
	io.Pfpink("A = %v\n", A.Output(fmts, false))
	io.Pfcyan("B = %v\n", B.Output(fmts, false))

	var ops OpsData
	ops.SetDefault()
	ops.Pc = 1.0
	ops.Cuts = []int{1, 2}
	ops.Xrange = [][]float64{{0, 1}, {-20, 20}, {-300, 300}}

	a := A.GetCopy()
	b := A.GetCopy()
	IndCrossover(a, b, A, B, 0, &ops)

	io.Pforan("a = %v\n", a.Output(fmts, false))
	io.Pfblue2("b = %v\n", b.Output(fmts, false))

	chk.Ints(tst, "a.Ints   ", a.Ints, []int{1, -20, 300})
	chk.Ints(tst, "b.Ints   ", b.Ints, []int{-1, 20, -300})
	chk.Strings(tst, "a.Strings", a.Strings, []string{"abc", "Y", "c"})
	chk.Strings(tst, "b.Strings", b.Strings, []string{"X", "b", "Z"})
	// TODO: add other tests here
	io.Pf("\n")

	x := get_individual(0, nbases)
	x.Ovas = []float64{0, 0}
	x.Oors = []float64{0, 0, 0}
	io.Pfblue2("x = %v\n", x.Output(fmts, false))
	B.CopyInto(x)

	chk.Scalar(tst, "ova0", 1e-17, x.Ovas[0], 200)
	chk.Scalar(tst, "ova1", 1e-17, x.Ovas[1], 100)
	chk.Scalar(tst, "oor0", 1e-17, x.Oors[0], 15)
	chk.Scalar(tst, "oor1", 1e-17, x.Oors[1], 25)
	chk.Scalar(tst, "oor2", 1e-17, x.Oors[2], 35)

	io.Pforan("x = %v\n", x.Output(fmts, false))
	chk.String(tst, x.Output(fmts, false), B.Output(fmts, false))
}
Пример #12
0
func Test_fun05(tst *testing.T) {

	//verbose()
	chk.PrintTitle("fun05. zero and one")

	io.Pforan("Zero(666,nil) = %v\n", Zero.F(666, nil))
	io.Pforan("One(666,nil)  = %v\n", One.F(666, nil))
	chk.Scalar(tst, "zero", 1e-17, Zero.F(666, nil), 0)
	chk.Scalar(tst, "one ", 1e-17, One.F(666, nil), 1)
}
Пример #13
0
func check_constants(tst *testing.T, E, ν, Kcor, Gcor, lcor float64) {

	K, G := Calc_K_from_Enu(E, ν), Calc_G_from_Enu(E, ν)
	l := Calc_l_from_Enu(E, ν)

	io.Pf("E = %v\n", E)
	io.Pf("ν = %v\n", ν)
	io.Pf("K = %v\n", K)
	io.Pf("G = %v\n", G)
	io.Pf("l = %v\n", l)

	chk.Scalar(tst, "KfromEν", 1e-17, K, Kcor)
	chk.Scalar(tst, "GfromEν", 1e-17, G, Gcor)
	chk.Scalar(tst, "lfromEν", 1e-17, l, lcor)

	EfromKG, νfromKG := Calc_E_from_KG(K, G), Calc_nu_from_KG(K, G)
	chk.Scalar(tst, "EfromKG", 1e-17, EfromKG, E)
	chk.Scalar(tst, "νfromKG", 1e-17, νfromKG, ν)

	EfromlG, νfromlG := Calc_E_from_lG(l, G), Calc_nu_from_lG(l, G)
	chk.Scalar(tst, "EfromlG", 1e-17, EfromlG, E)
	chk.Scalar(tst, "νfromlG", 1e-17, νfromlG, ν)

	EfromKν, GfromKν := Calc_E_from_Knu(K, ν), Calc_G_from_Knu(K, ν)
	chk.Scalar(tst, "EfromKν", 1e-17, EfromKν, E)
	chk.Scalar(tst, "GfromKν", 1e-17, GfromKν, G)
}
Пример #14
0
func Test_gumbel_03(tst *testing.T) {

	//verbose()
	chk.PrintTitle("dist_gumbel_03")

	var dist DistGumbel
	dist.Init(&VarData{M: 61.3, S: 7.52}) // from Haldar & Mahadevan page 90
	io.Pforan("dist = %+#v\n", dist)
	chk.Scalar(tst, "u", 0.00011, dist.U, 57.9157)
	chk.Scalar(tst, "β", 1e-4, dist.B, 1.0/0.17055)
}
Пример #15
0
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)
}
Пример #16
0
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)
}
Пример #17
0
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)
	}
}
Пример #18
0
func Test_phi03(tst *testing.T) {

	// verbose()
	chk.PrintTitle("phi03 - Reinitialisation")

	// run simulation
	analysis := NewFEM("data/phi03.sim", "", true, false, false, false, chk.Verbose, 0)

	// run simulation
	err := analysis.Run()
	if err != nil {
		tst.Errorf("Run failed:\n%v", err)
		return
	}
	// domain
	dom := analysis.Domains[0]

	eq := []int{
		0, 20, 54,
	}
	node := []int{
		0, 10, 27,
	}
	// check results
	for i, v := range eq {
		// chk.PrintTitle(io.Sf("%25.10e%25.10e", dom.Sol.Y[v], dom.Msh.Verts[node[i]].C[0]))
		chk.Scalar(tst, "h @ nod "+string(i), 5e-2, dom.Sol.Y[v], dom.Msh.Verts[node[i]].C[0])
	}
}
Пример #19
0
func Test_msh01(tst *testing.T) {

	chk.PrintTitle("msh01")

	msh := ReadMsh("data", "bh16.msh")
	if msh == nil {
		tst.Errorf("test failed\n")
		return
	}
	io.Pforan("%v\n", msh)
	io.Pfcyan("lims = [%g, %g, %g, %g, %g, %g]\n", msh.Xmin, msh.Xmax, msh.Ymin, msh.Ymax, msh.Zmin, msh.Zmax)
	chk.Scalar(tst, "xmin", 1e-17, msh.Xmin, 10)
	chk.Scalar(tst, "xmax", 1e-17, msh.Xmax, 14)
	chk.Scalar(tst, "ymin", 1e-17, msh.Ymin, -1)
	chk.Scalar(tst, "ymax", 1e-17, msh.Ymax, 1)
}
Пример #20
0
func Test_brent03(tst *testing.T) {

	//verbose()
	chk.PrintTitle("brent03. minimum finding")

	ffcn := func(x float64) (res float64, err error) {
		return x*x*x - 2.0*x - 5.0, nil
	}

	var o Brent
	o.Init(ffcn)
	xa, xb := 0.0, 1.0
	x, err := o.Min(xa, xb, false)
	if err != nil {
		chk.Panic("%v", err)
	}
	y, err := ffcn(x)
	if err != nil {
		chk.Panic("%v", err)
	}
	xcor := math.Sqrt(2.0 / 3.0)
	io.Pforan("x      = %v (correct=%g)\n", x, xcor)
	io.Pforan("f(x)   = %v\n", y)
	io.Pforan("nfeval = %v\n", o.NFeval)
	io.Pforan("nit    = %v\n", o.It)

	//save := true
	save := false
	PlotYxe(ffcn, "results", "brent03.png", x, -1, 3, 101, "Brent", "'b-'", save, false, nil)
	chk.Scalar(tst, "xcorrect", 1e-8, x, xcor)
}
Пример #21
0
// run_rootsol_test runs root solution test
//  Note: xguess is the trial solution for Newton's method (not Brent's)
func run_rootsol_test(tst *testing.T, xa, xb, xguess, tolcmp float64, ffcnA Cb_yxe, ffcnB Cb_f, JfcnB Cb_Jd, fname string, save, show bool) (xbrent float64) {

	// Brent
	io.Pfcyan("\n       - - - - - - - using Brent's method - - -- - - - \n")
	var o Brent
	o.Init(ffcnA)
	var err error
	xbrent, err = o.Solve(xa, xb, false)
	if err != nil {
		chk.Panic("%v", err)
	}
	var ybrent float64
	ybrent, err = ffcnA(xbrent)
	if err != nil {
		chk.Panic("%v", err)
	}
	io.Pforan("x      = %v\n", xbrent)
	io.Pforan("f(x)   = %v\n", ybrent)
	io.Pforan("nfeval = %v\n", o.NFeval)
	io.Pforan("nit    = %v\n", o.It)
	if math.Abs(ybrent) > 1e-10 {
		chk.Panic("Brent failed: f(x) = %g > 1e-10\n", ybrent)
	}

	// Newton
	io.Pfcyan("\n       - - - - - - - using Newton's method - - -- - - - \n")
	var p NlSolver
	p.Init(1, ffcnB, nil, JfcnB, true, false, nil)
	xnewt := []float64{xguess}
	var cnd float64
	cnd, err = p.CheckJ(xnewt, 1e-6, true, !chk.Verbose)
	io.Pforan("cond(J) = %v\n", cnd)
	if err != nil {
		chk.Panic("%v", err.Error())
	}
	err = p.Solve(xnewt, false)
	if err != nil {
		chk.Panic("%v", err.Error())
	}
	var ynewt float64
	ynewt, err = ffcnA(xnewt[0])
	if err != nil {
		chk.Panic("%v", err)
	}
	io.Pforan("x      = %v\n", xnewt[0])
	io.Pforan("f(x)   = %v\n", ynewt)
	io.Pforan("nfeval = %v\n", p.NFeval)
	io.Pforan("nJeval = %v\n", p.NJeval)
	io.Pforan("nit    = %v\n", p.It)
	if math.Abs(ynewt) > 1e-9 {
		chk.Panic("Newton failed: f(x) = %g > 1e-10\n", ynewt)
	}

	// compare Brent's and Newton's solutions
	PlotYxe(ffcnA, "results", fname, xbrent, xa, xb, 101, "Brent", "'b-'", save, show, func() {
		plt.PlotOne(xnewt[0], ynewt, "'g+', ms=15, label='Newton'")
	})
	chk.Scalar(tst, "xbrent - xnewt", tolcmp, xbrent, xnewt[0])
	return
}
Пример #22
0
func Test_brent02(tst *testing.T) {

	//verbose()
	chk.PrintTitle("brent02. root finding")

	ffcnA := func(x float64) (res float64, err error) {
		return x*x*x - 2.0*x - 5.0, nil
	}

	ffcnB := func(fx, x []float64) (err error) {
		fx[0], err = ffcnA(x[0])
		return
	}

	JfcnB := func(dfdx [][]float64, x []float64) (err error) {
		dfdx[0][0] = 3.0*x[0]*x[0] - 2.0
		return
	}

	xa, xb := 2.0, 3.0
	xguess := 2.1
	//save   := true
	save := false
	xbrent := run_rootsol_test(tst, xa, xb, xguess, 1e-7, ffcnA, ffcnB, JfcnB, "brent02.png", save, false)
	chk.Scalar(tst, "xsol", 1e-14, xbrent, 2.09455148154233)
}
Пример #23
0
func Test_pop02(tst *testing.T) {

	//verbose()
	chk.PrintTitle("pop02")

	rnd.Init(0)

	genes := [][]float64{
		{1, 5}, // 0
		{1, 3}, // 1
		{5, 7}, // 2
		{1, 2}, // 3
		{2, 4}, // 4
		{3, 6}, // 5
		{4, 8}, // 6
		{4, 6}, // 7
		{1, 3}, // 8
		{0, 0}, // 9
	}

	// objective values, oor and demerits
	//                0   1   2   3   4   5   6     7     8     9
	ovs := []float64{11, 21, 10, 12, 13, 31, 41, 11.1, 31.5, 11.5}
	dem := []float64{0.2, 0.7, 0.1, 0.5, 0.6, 0.8, 1.0, 0.3, 0.9, 0.4}

	// init population
	ninds := len(genes)
	nova := 1
	noor := 0
	nbases := 2
	var pop Population
	pop = make([]*Individual, ninds)
	for i := 0; i < ninds; i++ {
		pop[i] = NewIndividual(nova, noor, nbases, genes[i])
		pop[i].Ovas[0] = ovs[i]
		pop[i].Demerit = dem[i]
	}

	pop.Sort()

	genes_sorted := [][]float64{
		{5, 7}, // 2
		{1, 5}, // 0
		{4, 6}, // 7
		{0, 0}, // 9
		{1, 2}, // 3
		{2, 4}, // 4
		{1, 3}, // 1
		{3, 6}, // 5
		{1, 3}, // 8
		{4, 8}, // 6
	}

	for i, ind := range pop {
		for j := 0; j < ind.Nfltgenes; j++ {
			chk.Scalar(tst, io.Sf("i%dg%d", i, j), 1e-14, ind.GetFloat(j), genes_sorted[i][j])
		}
	}
}
Пример #24
0
func Test_reliab01(tst *testing.T) {

	//verbose()
	chk.PrintTitle("reliab01. simply supported beam")

	// Simply supported beam
	// Analyse the max deflection at mid-span of simply supported beam
	// with uniform distributed load q and concentrated load at midspan
	//  Data:
	//   L    -- span
	//   EI   -- Young's modulus times cross-sectional moment of inertia
	//   p    -- x[0] concentrated load at mid-span
	//   q    -- x[1] distributed load
	//   δlim -- max deflection (vertical displacement) at mid-span
	// Reference
	//  Haldar A, Reliability-Based Structura Design, 2005

	// constants
	δlim := 0.0381 // [m] max allowed deflection
	L := 9.144     // [m] span
	EI := 182262.0 // [kN m²] flexural rigidity
	L3 := math.Pow(L, 3.0)

	// statistics of p=x[0] and q=x[1]
	μ := []float64{111.2, 35.03} // mean values
	σ := []float64{11.12, 5.25}  // deviation values
	lrv := []bool{true, false}   // is lognormal random variable?

	// limit state function
	gfcn := func(x []float64, args ...interface{}) (g float64, err error) {
		p, q := x[0], x[1]
		g = δlim - (p*L3/EI/48.0 + 5.0*q*L3*L/EI/384.0)
		return
	}

	// derivative of limit state function
	hfcn := func(dgdx, x []float64, args ...interface{}) (err error) {
		dgdx[0] = -L3 / EI / 48.0            // dg/dp
		dgdx[1] = -5.0 * L3 * L / EI / 384.0 // dg/dq
		return
	}

	// first order reliability method
	var form ReliabFORM
	form.Init(μ, σ, lrv, gfcn, hfcn)
	form.NlsSilent = !chk.Verbose
	form.NlsCheckJ = chk.Verbose
	form.TolA = 0.005
	form.TolB = 0.005
	if chk.Verbose {
		form.PlotFnk = "beam"
	}

	// run FORM
	verbose := chk.Verbose // show messages
	βtrial := 3.0
	β, _, _, _ := form.Run(βtrial, verbose)
	chk.Scalar(tst, "β", 0.0015, β, 3.8754)
}
Пример #25
0
func Test_tsr03(tst *testing.T) {

	//verbose()
	chk.PrintTitle("tsr03")

	a := [][]float64{
		{4.0, 1.0 / SQ2, 0},
		{1.0 / SQ2, 5.0, 0},
		{0, 0, 6.0},
	}
	am := make([]float64, 4)
	aa := Alloc2()
	Ten2Man(am, a)
	Man2Ten(aa, am)
	io.Pf("a  = %v\n", a)
	io.Pf("am = %v\n", am)
	io.Pf("aa = %v\n", aa)
	chk.Matrix(tst, "aa", 1.0e-15, aa, a)

	for i := 0; i < 3; i++ {
		for j := 0; j < 3; j++ {
			chk.Scalar(tst, fmt.Sprintf("am[%d][%d]", i, j), 1.0e-15, M2T(am, i, j), a[i][j])
		}
	}

	b := [][]float64{
		{4.0, 1.0 / SQ2, 3.0 / SQ2},
		{1.0 / SQ2, 5.0, 2.0 / SQ2},
		{3.0 / SQ2, 2.0 / SQ2, 6.0},
	}
	bm := make([]float64, 6)
	bb := Alloc2()
	Ten2Man(bm, b)
	Man2Ten(bb, bm)
	io.Pf("b  = %v\n", b)
	io.Pf("bm = %v\n", bm)
	io.Pf("bb = %v\n", bb)
	chk.Matrix(tst, "bb", 1.0e-15, bb, b)

	for i := 0; i < 3; i++ {
		for j := 0; j < 3; j++ {
			chk.Scalar(tst, fmt.Sprintf("bm[%d][%d]", i, j), 1.0e-15, M2T(bm, i, j), b[i][j])
		}
	}
}
Пример #26
0
func Test_nurbs01(tst *testing.T) {

	//verbose()
	chk.PrintTitle("nurbs01")

	nurbs := get_nurbs_A()
	faces := nurbs.ExtractSurfaces()
	spans := nurbs.Elements()
	ibasis0 := nurbs.IndBasis(spans[0])
	ibasis1 := nurbs.IndBasis(spans[1])
	io.Pforan("spans = %v\n", spans)
	chk.Ints(tst, "span0", spans[0], []int{2, 3, 1, 2})
	chk.Ints(tst, "span1", spans[1], []int{3, 4, 1, 2})
	chk.Ints(tst, "ibasis0", ibasis0, []int{0, 1, 2, 4, 5, 6})
	chk.Ints(tst, "ibasis1", ibasis1, []int{1, 2, 3, 5, 6, 7})

	shape0 := GetShapeNurbs(nurbs, faces, spans[0])
	shape1 := GetShapeNurbs(nurbs, faces, spans[1])

	dux := 0.5
	duy := 1.0
	drx := 2.0
	dry := 2.0

	r := []float64{0.75, 0.75, 0}

	shape0.NurbsFunc(shape0.S, shape0.DSdR, r, true, -1)
	io.Pforan("0: u = %v\n", shape0.U)
	chk.Scalar(tst, "0: ux", 1e-17, shape0.U[0], (1.0+r[0])*dux/drx)
	chk.Scalar(tst, "0: uy", 1e-17, shape0.U[1], (1.0+r[1])*duy/dry)
	chk.Ints(tst, "0: ibasis", shape0.Ibasis, []int{0, 1, 2, 4, 5, 6})

	io.Pforan("S(u(r)) = %v\n", shape0.S)

	shape1.NurbsFunc(shape1.S, shape1.DSdR, r, true, -1)
	io.Pfpink("\n1: u = %v\n", shape1.U)
	chk.Scalar(tst, "1: ux", 1e-17, shape1.U[0], 0.5+(1.0+r[0])*dux/drx)
	chk.Scalar(tst, "1: uy", 1e-17, shape1.U[1], (1.0+r[1])*duy/dry)
	chk.Ints(tst, "1: ibasis", shape1.Ibasis, []int{1, 2, 3, 5, 6, 7})

	if chk.Verbose {
		gm.PlotNurbs("/tmp/gofem", "tst_nurbs01", nurbs, 21, false, nil)
	}
}
Пример #27
0
func Test_igd01(tst *testing.T) {

	//verbose()
	chk.PrintTitle("igd. igd metric with star equal to trial => igd=0")

	// load star values
	prob := "UF1"
	fStar, err := io.ReadMatrix(io.Sf("./examples/mulobj-cec09/cec09/pf_data/%s.dat", prob))
	if err != nil {
		tst.Errorf("cannot read fStar matrix:\n%v", err)
		return
	}
	npts := len(fStar)

	// optimiser
	var opt Optimiser
	opt.Default()
	opt.Nsol = npts
	opt.Ncpu = 1
	opt.FltMin = []float64{0, 0} // used to store fStar
	opt.FltMax = []float64{1, 1} // used to store fStar
	nf, ng, nh := 2, 0, 0

	// generator (store fStar into Flt)
	gen := func(sols []*Solution, prms *Parameters) {
		for i, sol := range sols {
			sol.Flt[0], sol.Flt[1] = fStar[i][0], fStar[i][1]
		}
	}

	// objective function (copy fStar from Flt into Ova)
	obj := func(f, g, h, x []float64, ξ []int, cpu int) {
		f[0], f[1] = x[0], x[1]
	}

	// initialise optimiser
	opt.Init(gen, nil, obj, nf, ng, nh)

	// compute igd
	igd := StatIgd(&opt, fStar)
	io.Pforan("igd = %v\n", igd)
	chk.Scalar(tst, "igd", 1e-15, igd, 0)

	// plot
	if chk.Verbose {
		fmt := &plt.Fmt{C: "red", M: ".", Ms: 1, Ls: "None", L: "solutions"}
		fS0 := utl.DblsGetColumn(0, fStar)
		fS1 := utl.DblsGetColumn(1, fStar)
		io.Pforan("len(fS0) = %v\n", len(fS0))
		plt.SetForEps(0.75, 300)
		opt.PlotAddOvaOva(0, 1, opt.Solutions, true, fmt)
		plt.Plot(fS0, fS1, io.Sf("'b.', ms=2, label='star(%s)', clip_on=0", prob))
		plt.Gll("$f_0$", "$f_1$", "")
		plt.SaveD("/tmp/goga", "igd01.eps")
	}
}
Пример #28
0
func Test_trapz01(tst *testing.T) {

	//verbose()
	chk.PrintTitle("trapz01")

	x := []float64{4, 6, 8}
	y := []float64{1, 2, 3}
	A := Trapz(x, y)
	chk.Scalar(tst, "A", 1e-17, A, 8)
}
Пример #29
0
func Test_trapz02(tst *testing.T) {

	//verbose()
	chk.PrintTitle("trapz02")

	y := func(x float64) float64 {
		return math.Sqrt(1.0 + math.Pow(math.Sin(x), 3.0))
	}

	n := 11
	x := utl.LinSpace(0, 1, n)
	A := TrapzF(x, y)
	A_ := TrapzRange(0, 1, n, y)
	io.Pforan("A  = %v\n", A)
	io.Pforan("A_ = %v\n", A_)
	Acor := 1.08306090851465 // right value is Acor := 1.08268158558
	chk.Scalar(tst, "A", 1e-15, A, Acor)
	chk.Scalar(tst, "A_", 1e-15, A_, Acor)
}
Пример #30
0
func Test_basicgeom02(tst *testing.T) {

	//verbose()
	chk.PrintTitle("basicgeom02. Vector")

	u := []float64{1, 2, 3}
	v := []float64{4, 5, 6}
	s := VecDot(u, v)
	r := VecNew(2, u)
	w := VecNewAdd(2, u, -3, v)
	io.Pforan("u = %v  norm = %g\n", u, VecNorm(u))
	io.Pforan("v = %v  norm = %g\n", v, VecNorm(v))
	io.Pforan("w = %v  norm = %g\n", w, VecNorm(w))
	io.Pforan("u.v = %v\n", s)
	chk.Scalar(tst, "u.v", 1e-17, s, 32.0)
	chk.Scalar(tst, "norm(u)", 1e-17, VecNorm(u), math.Sqrt(14.0))
	chk.Scalar(tst, "norm(v)", 1e-17, VecNorm(v), math.Sqrt(77.0))
	chk.Vector(tst, "r", 1e-17, r, []float64{2, 4, 6})
	chk.Vector(tst, "w", 1e-17, w, []float64{-10, -11, -12})
}