Example #1
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
}
Example #2
0
func Test_intordmut01(tst *testing.T) {

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

	var ops OpsData
	ops.SetDefault()
	ops.Pm = 1

	rnd.Init(0)

	a := []int{1, 2, 3, 4, 5, 6, 7, 8}
	io.Pforan("before: a = %v\n", a)
	ops.OrdSti = []int{2, 5, 4}
	IntOrdMutation(a, 0, &ops)
	io.Pfcyan("after:  a = %v\n", a)
	chk.Ints(tst, "a", a, []int{1, 2, 6, 7, 3, 4, 5, 8})
	nums := utl.IntRange2(1, 9)
	sort.Ints(a)
	chk.Ints(tst, "asorted = 12345678", a, nums)

	a = []int{1, 2, 3, 4, 5, 6, 7, 8}
	io.Pforan("\nbefore: a = %v\n", a)
	ops.OrdSti = nil
	IntOrdMutation(a, 0, &ops)
	io.Pfcyan("after:  a = %v\n", a)
	sort.Ints(a)
	chk.Ints(tst, "asorted = 12345678", a, nums)
}
Example #3
0
// 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)
}
Example #4
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)
}
Example #5
0
func Test_cxdeb01(tst *testing.T) {

	//verbose()
	chk.PrintTitle("cxdeb01. Deb's crossover")

	var ops OpsData
	ops.SetDefault()
	ops.Pc = 1.0
	ops.Xrange = [][]float64{{-3, 3}, {-4, 4}}
	ops.EnfRange = true

	rnd.Init(0)

	A := []float64{-1, 1}
	B := []float64{1, 2}
	a := make([]float64, len(A))
	b := make([]float64, len(A))
	FltCrossoverDeb(a, b, A, B, 0, &ops)
	io.Pforan("A = %v\n", A)
	io.Pforan("B = %v\n", B)
	io.Pfcyan("a = %.6f\n", a)
	io.Pfcyan("b = %.6f\n", b)

	nsamples := 1000
	a0s, a1s := make([]float64, nsamples), make([]float64, nsamples)
	b0s, b1s := make([]float64, nsamples), make([]float64, nsamples)
	for i := 0; i < nsamples; i++ {
		FltCrossoverDeb(a, b, B, A, 0, &ops)
		a0s[i], a1s[i] = a[0], a[1]
		b0s[i], b1s[i] = b[0], b[1]
	}
	ha0 := rnd.Histogram{Stations: []float64{-4, -3.5, -3, -2.5, -2, -1.5, -1, -0.5, 0, 0.5, 1}}
	hb0 := rnd.Histogram{Stations: []float64{0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 5, 5.5, 6}}
	ha1 := rnd.Histogram{Stations: utl.LinSpace(-4, 4, 11)}
	hb1 := rnd.Histogram{Stations: utl.LinSpace(-4, 4, 11)}
	ha0.Count(a0s, true)
	hb0.Count(b0s, true)
	ha1.Count(a1s, true)
	hb1.Count(b1s, true)

	io.Pforan("\na0s\n")
	io.Pf("%s", rnd.TextHist(ha0.GenLabels("%.1f"), ha0.Counts, 60))
	io.Pforan("b0s\n")
	io.Pf("%s", rnd.TextHist(hb0.GenLabels("%.1f"), hb0.Counts, 60))

	io.Pforan("\na1s\n")
	io.Pf("%s", rnd.TextHist(ha1.GenLabels("%.1f"), ha1.Counts, 60))
	io.Pforan("b1s\n")
	io.Pf("%s", rnd.TextHist(hb1.GenLabels("%.1f"), hb1.Counts, 60))
}
Example #6
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)
}
Example #7
0
// Clean deletes temporary data structures
func (o *LinSolMumps) Clean() {

	// exit if not initialised
	if !o.is_initialised {
		return
	}

	// start time
	if o.ton {
		o.tini = time.Now()
	}

	// message
	if o.verb {
		io.Pfgreen("\n . . . . . . . . . . . . . . LinSolMumps.Clean . . . . . . . . . . . . . . . \n\n")
	}

	// clean up
	if o.cmplx {
		o.mz.job = -2     // finalize code
		C.zmumps_c(&o.mz) // do finalize
	} else {
		o.m.job = -2     // finalize code
		C.dmumps_c(&o.m) // do finalize
	}

	// duration
	if o.ton {
		io.Pfcyan("%s: Time spent in LinSolMumps.Clean   = %v\n", o.name, time.Now().Sub(o.tini))
	}
}
Example #8
0
// SolveC solves the linear Complex system A.x = b
//  NOTES:
//    1) sum_b_to_root is a flag for MUMPS; it tells Solve to sum the values in 'b' arrays to the root processor
func (o *LinSolMumps) SolveC(xR, xC, bR, bC []float64, sum_b_to_root bool) (err error) {

	// check
	if !o.cmplx {
		return chk.Err(_linsol_mumps_err11)
	}

	// start time
	if o.ton {
		o.tini = time.Now()
	}

	// message
	if o.verb {
		io.Pfgreen("\n . . . . . . . . . . . . . . LinSolMumps.SolveC . . . . . . . . . . . . . . . \n\n")
	}

	// MUMPS: set RHS in processor # 0
	if sum_b_to_root {
		mpi.SumToRoot(xR, bR)
		mpi.SumToRoot(xC, bC)
		// join complex values
		if mpi.Rank() == 0 {
			for i := 0; i < len(xR); i++ {
				o.xRC[i*2], o.xRC[i*2+1] = xR[i], xC[i]
			}
		}
	} else {
		// join complex values
		if mpi.Rank() == 0 {
			for i := 0; i < len(xR); i++ {
				o.xRC[i*2], o.xRC[i*2+1] = bR[i], bC[i]
			}
		}
	}

	// MUMPS: solve
	o.mz.job = 3      // solution code
	C.zmumps_c(&o.mz) // solve
	if o.mz.info[1-1] < 0 {
		return chk.Err(_linsol_mumps_err12, mumps_error(o.mz.info[1-1], o.mz.info[2-1]))
	}

	// MUMPS: split complex values
	if mpi.Rank() == 0 {
		for i := 0; i < len(xR); i++ {
			xR[i], xC[i] = o.xRC[i*2], o.xRC[i*2+1]
		}
	}

	// MUMPS: broadcast from root
	mpi.BcastFromRoot(xR)
	mpi.BcastFromRoot(xC)

	// duration
	if o.ton {
		io.Pfcyan("%s: Time spent in LinSolMumps.Solve = %v\n", o.name, time.Now().Sub(o.tini))
	}
	return
}
Example #9
0
func Test_MTint01(tst *testing.T) {

	//verbose()
	chk.PrintTitle("MTint01. integers (Mersenne Twister)")

	Init(1234)

	nints := 10
	vals := make([]int, NSAMPLES)

	// using MTint
	t0 := time.Now()
	for i := 0; i < NSAMPLES; i++ {
		vals[i] = MTint(0, nints-1)
	}
	io.Pforan("time elapsed = %v\n", time.Now().Sub(t0))

	hist := IntHistogram{Stations: utl.IntRange(nints + 1)}
	hist.Count(vals, true)
	io.Pfyel(TextHist(hist.GenLabels("%d"), hist.Counts, 60))

	// using MTints
	t0 = time.Now()
	MTints(vals, 0, nints-1)
	io.Pforan("time elapsed = %v\n", time.Now().Sub(t0))

	hist.Count(vals, true)
	io.Pfcyan(TextHist(hist.GenLabels("%d"), hist.Counts, 60))
}
Example #10
0
// Clean deletes temporary data structures
func (o *LinSolUmfpack) Clean() {

	// start time
	if o.ton {
		o.tini = time.Now()
	}

	// message
	if o.verb {
		io.Pfgreen("\n . . . . . . . . . . . . . . LinSolUmfpack.Clean . . . . . . . . . . . . . . . \n\n")
	}

	// clean up
	if o.cmplx {
		C.umfpack_zl_free_symbolic(&o.usymb)
		C.umfpack_zl_free_numeric(&o.unum)
	} else {
		C.umfpack_dl_free_symbolic(&o.usymb)
		C.umfpack_dl_free_numeric(&o.unum)
	}

	// duration
	if o.ton {
		io.Pfcyan("%s: Time spent in LinSolUmfpack.Clean   = %v\n", o.name, time.Now().Sub(o.tini))
	}
}
Example #11
0
func Test_groups01(tst *testing.T) {

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

	Init(0)

	ng := 3            // number of groups
	nints := 12        // number of integers
	size := nints / ng // groups size
	ints := utl.IntRange(nints)
	groups := utl.IntsAlloc(ng, size)
	hists := make([]*IntHistogram, ng)
	for i := 0; i < ng; i++ {
		hists[i] = &IntHistogram{Stations: utl.IntRange(nints + 1)}
	}
	IntGetGroups(groups, ints)
	io.Pfcyan("groups = %v\n", groups)
	for i := 0; i < NSAMPLES; i++ {
		IntGetGroups(groups, ints)
		for j := 0; j < ng; j++ {
			check_repeated(groups[j])
			hists[j].Count(groups[j], false)
		}
	}
	for i := 0; i < ng; i++ {
		io.Pf("\n")
		io.Pf(TextHist(hists[i].GenLabels("%d"), hists[i].Counts, 60))
	}
}
Example #12
0
// SolveC solves the linear Complex system A.x = b
func (o *LinSolUmfpack) SolveC(xR, xC, bR, bC []float64, dummy bool) (err error) {

	// check
	if !o.cmplx {
		return chk.Err(_linsol_umfpack_err12)
	}

	// start time
	if o.ton {
		o.tini = time.Now()
	}

	// message
	if o.verb {
		io.Pfgreen("\n . . . . . . . . . . . . . . LinSolUmfpack.SolveC . . . . . . . . . . . . . . . \n\n")
	}

	// UMFPACK: pointers
	pxR := (*C.double)(unsafe.Pointer(&xR[0]))
	pxC := (*C.double)(unsafe.Pointer(&xC[0]))
	pbR := (*C.double)(unsafe.Pointer(&bR[0]))
	pbC := (*C.double)(unsafe.Pointer(&bC[0]))

	// UMFPACK: solve
	st := C.umfpack_zl_solve(C.UMFPACK_A, o.ap, o.ai, o.ax, o.az, pxR, pxC, pbR, pbC, o.unum, o.uctrl, nil)
	if st != C.UMFPACK_OK {
		chk.Err(_linsol_umfpack_err13, Uerr2Text[int(st)])
	}

	// duration
	if o.ton {
		io.Pfcyan("%s: Time spent in LinSolUmfpack.Solve = %v\n", o.name, time.Now().Sub(o.tini))
	}
	return
}
Example #13
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)
}
Example #14
0
// 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)
}
Example #15
0
func Test_porous01(tst *testing.T) {

	chk.PrintTitle("porous01")

	mdb := ReadMat("data", "porous.mat")
	if mdb == nil {
		tst.Errorf("test failed\n")
		return
	}
	io.Pf("porous.mat just read:\n%v\n", mdb)

	mat := mdb.Get("porous1")
	if mat == nil {
		tst.Errorf("test failed\n")
		return
	}
	io.Pforan("mat = %+v\n", mat)

	cnd := mdb.GroupGet("porous1", "c")
	if mat == nil {
		tst.Errorf("test failed\n")
		return
	}
	io.Pfcyan("cnd = %+v\n", cnd)

	lrm := mdb.GroupGet("porous1", "l")
	if mat == nil {
		tst.Errorf("test failed\n")
		return
	}
	io.Pforan("lrm = %+v\n", lrm)

	por := mdb.GroupGet("porous1", "p")
	if mat == nil {
		tst.Errorf("test failed\n")
		return
	}
	io.Pfcyan("por = %+v\n", por)

	sld := mdb.GroupGet("porous1", "s")
	if mat == nil {
		tst.Errorf("test failed\n")
		return
	}
	io.Pforan("sld = %+v\n", sld)
}
Example #16
0
func Test_cxint01(tst *testing.T) {

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

	var ops OpsData
	ops.SetDefault()
	ops.Pc = 1
	ops.Ncuts = 1

	A := []int{1, 2}
	B := []int{-1, -2}
	a := make([]int, len(A))
	b := make([]int, len(A))
	IntCrossover(a, b, A, B, 0, &ops)
	io.Pfred("A = %2d\n", A)
	io.PfRed("B = %2d\n", B)
	io.Pfcyan("a = %2d\n", a)
	io.Pfblue2("b = %2d\n", b)
	chk.Ints(tst, "a", a, []int{1, -2})
	chk.Ints(tst, "b", b, []int{-1, 2})
	io.Pf("\n")

	A = []int{1, 2, 3, 4, 5, 6, 7, 8}
	B = []int{-1, -2, -3, -4, -5, -6, -7, -8}
	a = make([]int, len(A))
	b = make([]int, len(A))
	ops.Cuts = []int{1, 3}
	IntCrossover(a, b, A, B, 0, &ops)
	io.Pfred("A = %2v\n", A)
	io.PfRed("B = %2v\n", B)
	io.Pfcyan("a = %2v\n", a)
	io.Pfblue2("b = %2v\n", b)
	chk.Ints(tst, "a", a, []int{1, -2, -3, 4, 5, 6, 7, 8})
	chk.Ints(tst, "b", b, []int{-1, 2, 3, -4, -5, -6, -7, -8})

	ops.Cuts = []int{5, 7}
	IntCrossover(a, b, A, B, 0, &ops)
	io.Pfred("A = %2v\n", A)
	io.PfRed("B = %2v\n", B)
	io.Pfcyan("a = %2v\n", a)
	io.Pfblue2("b = %2v\n", b)
	chk.Ints(tst, "a", a, []int{1, 2, 3, 4, 5, -6, -7, 8})
	chk.Ints(tst, "b", b, []int{-1, -2, -3, -4, -5, 6, 7, -8})
}
Example #17
0
func Test_postp01(tst *testing.T) {

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

	Tout := []float64{0, 0.1, 0.2, 0.200001, 0.201, 0.3001, 0.8, 0.99, 0.999, 1}
	Tsel := []float64{0, 0.2, 0.3, 0.6, 0.8, 0.9, 0.99, -1}

	tol := 0.001
	I, T := GetITout(Tout, Tsel, tol)
	io.Pfcyan("Tout = %v\n", Tout)
	io.Pfcyan("Tsel = %v\n", Tsel)
	io.Pforan("I = %v\n", I)
	io.Pforan("T = %v\n", T)

	chk.Ints(tst, "I", I, []int{0, 2, 5, 6, 7, 9})
	chk.Vector(tst, "T", 1e-16, T, []float64{0, 0.2, 0.3001, 0.8, 0.99, 1})
}
Example #18
0
// ProfMEM activates memory profiling
//  Note: returns a "stop()" function to be called before shutting down
func ProfMEM(dirout, filename string, silent bool) func() {
	os.MkdirAll(dirout, 0777)
	fn := filepath.Join(dirout, filename)
	f, err := os.Create(fn)
	if err != nil {
		chk.Panic(_profiling_err1, "ProfMEM", err.Error())
	}
	if !silent {
		io.Pfcyan("MEM profiling => %s\n", fn)
	}
	return func() {
		pprof.WriteHeapProfile(f)
		f.Close()
		if !silent {
			io.Pfcyan("MEM profiling finished\n")
		}
	}
}
Example #19
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))
}
Example #20
0
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()
}
Example #21
0
func Test_out02(tst *testing.T) {

	// finalise analysis process and catch errors
	defer func() {
		if err := recover(); err != nil {
			tst.Fail()
			io.PfRed("ERROR: %v\n", err)
		}
	}()

	// test title
	//verbose()
	chk.PrintTitle("out02")

	// start simulation
	processing := fem.NewFEM("data/twoqua4.sim", "", true, true, false, false, chk.Verbose, 0)

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

	// start post-processing
	Start("data/twoqua4.sim", 0, 0)

	// get second ip coordinates
	xip := Ipoints[1].X
	io.Pfcyan("xip = %v\n", xip)

	// define points
	Define("A", N{-1})
	Define("ips", Along{{xip[0], 0}, {xip[0], 1}})

	// load results
	LoadResults(nil)

	// solution
	var sol ana.CteStressPstrain
	sol.Init(fun.Prms{
		&fun.Prm{N: "qnH", V: -50},
		&fun.Prm{N: "qnV", V: -100},
	})

	// check displacements
	tolu := 1e-15
	x := GetCoords("A")
	ux := GetRes("ux", "A", 0)
	uy := GetRes("uy", "A", 0)
	io.Pforan("ux=%v uy=%v\n", ux, uy)
	for j, t := range Times {
		io.Pfyel("t=%g\n", t)
		sol.CheckDispl(tst, t, []float64{ux[j], uy[j]}, x, tolu)
	}
}
Example #22
0
// ProfCPU activates CPU profiling
//  Note: returns a "stop()" function to be called before shutting down
func ProfCPU(dirout, filename string, silent bool) func() {
	os.MkdirAll(dirout, 0777)
	fn := filepath.Join(dirout, filename)
	f, err := os.Create(fn)
	if err != nil {
		chk.Panic(_profiling_err1, "ProfCPU", err.Error())
	}
	if !silent {
		io.Pfcyan("CPU profiling => %s\n", fn)
	}
	pprof.StartCPUProfile(f)
	return func() {
		pprof.StopCPUProfile()
		f.Close()
		if !silent {
			io.Pfcyan("CPU profiling finished\n")
		}
	}
}
Example #23
0
func Test_bins02(tst *testing.T) {

	//verbose()
	chk.PrintTitle("bins02. find along line (2D)")

	// bins
	var bins Bins
	bins.Init([]float64{-0.2, -0.2}, []float64{0.8, 1.8}, 5)

	// fill bins structure
	maxit := 5 // number of entries
	ID := make([]int, maxit)
	for k := 0; k < maxit; k++ {
		x := float64(k) / float64(maxit)
		ID[k] = k
		err := bins.Append([]float64{x, 2*x + 0.2}, ID[k])
		if err != nil {
			chk.Panic(err.Error())
		}
	}

	// add more points to bins
	for i := 0; i < 5; i++ {
		err := bins.Append([]float64{float64(i) * 0.1, 1.8}, 100+i)
		if err != nil {
			chk.Panic(err.Error())
		}
	}

	// message
	for _, bin := range bins.All {
		if bin != nil {
			io.Pf("%v\n", bin)
		}
	}

	// find points along diagonal
	ids := bins.FindAlongSegment([]float64{0.0, 0.2}, []float64{0.8, 1.8}, 1e-8)
	io.Pforan("ids = %v\n", ids)
	chk.Ints(tst, "ids", ids, ID)

	// find additional points
	ids = bins.FindAlongSegment([]float64{-0.2, 1.8}, []float64{0.8, 1.8}, 1e-8)
	io.Pfcyan("ids = %v\n", ids)
	chk.Ints(tst, "ids", ids, []int{100, 101, 102, 103, 104, 4})

	// draw
	if chk.Verbose {
		plt.SetForPng(1, 500, 150)
		bins.Draw2d(true, true, true, true, map[int]bool{8: true, 9: true, 10: true})
		plt.SetXnticks(15)
		plt.SetYnticks(15)
		plt.SaveD("/tmp/gosl/gm", "test_bins02.png")
	}
}
Example #24
0
func Test_porous02(tst *testing.T) {

	chk.PrintTitle("porous02")

	mdb := ReadMat("data", "porous.mat")
	if mdb == nil {
		tst.Errorf("test failed\n")
		return
	}
	io.Pf("porous.mat just read:\n%v\n", mdb)

	cnd, lrm, por, err := mdb.GroupGet3("porous1", "c", "l", "p")
	if err != nil {
		tst.Errorf("test failed\n")
		return
	}
	io.Pfcyan("cnd = %+v\n", cnd)
	io.Pforan("lrm = %+v\n", lrm)
	io.Pfcyan("por = %+v\n", por)
}
Example #25
0
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()
}
Example #26
0
func Test_invs03(tst *testing.T) {

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

	// square with vertical stress only under plane-strain
	E, ν := 210000.0, 0.49999
	qY := 240.0
	σx := 0.0
	σy := -qY / math.Sqrt(ν*ν-ν+1.0)
	σz := ν * (σx + σy)
	εx := -(ν*σz + ν*σy - σx) / E
	εy := -(ν*σz - σy + ν*σx) / E
	εz := 0.0

	// check
	c := E / ((1.0 + ν) * (1.0 - 2.0*ν))
	De := [][]float64{
		{c * (1.0 - ν), c * ν, c * ν, 0.0},
		{c * ν, c * (1.0 - ν), c * ν, 0.0},
		{c * ν, c * ν, c * (1.0 - ν), 0.0},
		{0.0, 0.0, 0.0, c * (1.0 - 2.0*ν)},
	}
	ε := [][]float64{
		{εx, 0, 0},
		{0, εy, 0},
		{0, 0, εz},
	}
	εm := make([]float64, 4)
	σm := make([]float64, 4)
	Ten2Man(εm, ε)
	la.MatVecMul(σm, 1, De, εm)
	q := M_q(σm)
	θ := M_θ(σm)
	io.Pfcyan("σm = %v\n", σm)
	io.Pfcyan("q  = %v\n", q)
	io.Pfcyan("θ  = %v\n", θ)
	chk.Scalar(tst, "q", 1e-10, q, qY)
	chk.Scalar(tst, "θ", 1e-3, θ, 0)
}
Example #27
0
func Test_blx01(tst *testing.T) {

	//verbose()
	chk.PrintTitle("blx01. blended crossover")

	var ops OpsData
	ops.SetDefault()
	ops.Pc = 1.0
	ops.Xrange = [][]float64{{-1, 2}, {0, 3}, {1, 4}, {3, 6}, {4, 7}}

	rnd.Init(0)

	A := []float64{0, 1, 2, 4, 5}
	B := []float64{1, 2, 3, 5, 6}
	a := make([]float64, len(A))
	b := make([]float64, len(A))
	FltCrossoverBlx(a, b, A, B, 0, &ops)
	io.Pforan("A = %v\n", A)
	io.Pforan("B = %v\n", B)
	io.Pfcyan("a = %v\n", a)
	io.Pfcyan("b = %v\n", b)
}
Example #28
0
func Test_cubiceq03(tst *testing.T) {

	//verbose()
	chk.PrintTitle("cubiceq03. y(x) = x³ + c")

	doplot := false
	np := 41
	var X, Y []float64
	if doplot {
		X = utl.LinSpace(-2, 2, np)
		Y = make([]float64, np)
		plt.SetForPng(0.8, 400, 200)
	}

	a, b := 0.0, 0.0
	colors := []string{"red", "green", "blue"}
	for k, c := range []float64{-1, 0, 1} {
		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, 1)
		chk.Scalar(tst, "x1", 1e-17, x1, -c)
		if doplot {
			for i, x := range X {
				Y[i] = x*x*x + a*x*x + b*x + c
			}
			plt.Plot(X, Y, io.Sf("color='%s', label='c=%g'", colors[k], c))
			plt.PlotOne(x1, 0, io.Sf("'ko', color='%s'", colors[k]))
			plt.Cross("")
			plt.Gll("x", "y", "")
		}
	}
	if doplot {
		plt.SaveD("/tmp", "fig_cubiceq03.png")
	}
}
Example #29
0
func Test_sort05(tst *testing.T) {

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

	a := map[string]int{"a": 1, "z": 2, "c": 3, "y": 4, "d": 5, "b": 6, "x": 7}
	b := map[string]float64{"a": 1, "z": 2, "c": 3, "y": 4, "d": 5, "b": 6, "x": 7}
	c := map[string]bool{"a": false, "z": true, "c": false, "y": true, "d": true, "b": false, "x": true}
	ka := StrIntMapSort(a)
	kb := StrDblMapSort(b)
	kc := StrBoolMapSort(c)
	io.Pforan("sorted_keys(a) = %v\n", ka)
	io.Pforan("sorted_keys(b) = %v\n", kb)
	io.Pforan("sorted_keys(c) = %v\n", kc)
	chk.Strings(tst, "ka", ka, []string{"a", "b", "c", "d", "x", "y", "z"})
	chk.Strings(tst, "kb", kb, []string{"a", "b", "c", "d", "x", "y", "z"})
	chk.Strings(tst, "kc", kc, []string{"a", "b", "c", "d", "x", "y", "z"})

	ka, va := StrIntMapSortSplit(a)
	io.Pfpink("sorted_keys(a) = %v\n", ka)
	io.Pfpink("sorted_vals(a) = %v\n", va)
	chk.Strings(tst, "ka", ka, []string{"a", "b", "c", "d", "x", "y", "z"})
	chk.Ints(tst, "va", va, []int{1, 6, 3, 5, 7, 4, 2})

	kb, vb := StrDblMapSortSplit(b)
	io.Pfcyan("sorted_keys(b) = %v\n", kb)
	io.Pfcyan("sorted_vals(b) = %v\n", vb)
	chk.Strings(tst, "kb", kb, []string{"a", "b", "c", "d", "x", "y", "z"})
	chk.Vector(tst, "vb", 1e-16, vb, []float64{1, 6, 3, 5, 7, 4, 2})

	kc, vc := StrBoolMapSortSplit(c)
	io.Pfcyan("sorted_keys(c) = %v\n", kc)
	io.Pfcyan("sorted_vals(c) = %v\n", vc)
	chk.Strings(tst, "kc", kc, []string{"a", "b", "c", "d", "x", "y", "z"})
	chk.Bools(tst, "vc", vc, []bool{false, false, false, true, true, true, true})
}
Example #30
0
// InitC initialises a LinSolUmfpack data structure for Complex systems. It also performs some initial analyses.
func (o *LinSolUmfpack) InitC(tC *TripletC, symmetric, verbose, timing bool) (err error) {

	// check
	o.tC = tC
	if tC.pos == 0 {
		return chk.Err(_linsol_umfpack_err02)
	}

	// flags
	o.name = "umfpack"
	o.sym = symmetric
	o.cmplx = true
	o.verb = verbose
	o.ton = timing

	// start time
	if o.ton {
		o.tini = time.Now()
	}

	// check x and z
	if len(o.tC.x) != len(o.tC.i) || len(o.tC.z) != len(o.tC.i) {
		return chk.Err(_linsol_umfpack_err03, len(o.tC.x), len(o.tC.z), len(o.tC.i))
	}

	// pointers
	o.ti = (*C.LONG)(unsafe.Pointer(&o.tC.i[0]))
	o.tj = (*C.LONG)(unsafe.Pointer(&o.tC.j[0]))
	o.tx = (*C.double)(unsafe.Pointer(&o.tC.x[0]))
	o.tz = (*C.double)(unsafe.Pointer(&o.tC.z[0]))
	o.ap = (*C.LONG)(unsafe.Pointer(&make([]int, o.tC.n+1)[0]))
	o.ai = (*C.LONG)(unsafe.Pointer(&make([]int, o.tC.pos)[0]))
	o.ax = (*C.double)(unsafe.Pointer(&make([]float64, o.tC.pos)[0]))
	o.az = (*C.double)(unsafe.Pointer(&make([]float64, o.tC.pos)[0]))

	// control
	o.uctrl = (*C.double)(unsafe.Pointer(&make([]float64, C.UMFPACK_CONTROL)[0]))
	C.umfpack_zl_defaults(o.uctrl)

	// duration
	if o.ton {
		io.Pfcyan("%s: Time spent in LinSolUmfpack.InitC = %v\n", o.name, time.Now().Sub(o.tini))
	}

	// success
	o.is_initialised = true
	return
}