Esempio n. 1
0
// 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()
	}
}
Esempio n. 2
0
// PlotOct plots a function cross-section and gradients projections on octahedral plane
func PlotOct(filename string, σcCte, rmin, rmax float64, nr, nα int, φ float64, F Cb_F_t, G Cb_G_t,
	notpolarc, simplec, only0, grads, showpts, first, last bool, ferr float64, args ...interface{}) {
	A := make([]float64, 6)
	dfdA := make([]float64, 6)
	dα := 2.0 * math.Pi / float64(nα)
	dr := (rmax - rmin) / float64(nr-1)
	αmin := -math.Pi / 6.0
	if notpolarc {
		rmin = -rmax
		dr = (rmax - rmin) / float64(nr-1)
		nα = nr
		dα = dr
		αmin = rmin
	}
	x, y, f := la.MatAlloc(nα, nr), la.MatAlloc(nα, nr), la.MatAlloc(nα, nr)
	//var gx, gy, L [][]float64
	var gx, gy [][]float64
	if grads {
		gx, gy = la.MatAlloc(nα, nr), la.MatAlloc(nα, nr)
		//L      = O2Lmat()
	}
	var σa, σb []float64
	if showpts {
		σa, σb = make([]float64, nα*nr), make([]float64, nα*nr)
	}
	k := 0
	var err error
	for i := 0; i < nα; i++ {
		for j := 0; j < nr; j++ {
			α := αmin + float64(i)*dα
			r := rmin + float64(j)*dr
			if notpolarc {
				x[i][j] = α // σa
				y[i][j] = r // σb
			} else {
				x[i][j] = r * math.Cos(α) // σa
				y[i][j] = r * math.Sin(α) // σb
			}
			A[0], A[1], A[2] = O2L(x[i][j], y[i][j], σcCte)
			if showpts {
				σa[k] = x[i][j]
				σb[k] = y[i][j]
			}
			f[i][j], err = F(A, args...)
			if err != nil {
				if ferr > 0 {
					f[i][j] = ferr
				} else {
					chk.Panic(_octahedral_err1, "func", err)
				}
			}
			if grads {
				_, err = G(dfdA, A, args...)
				if err != nil {
					chk.Panic(_octahedral_err1, "grads", err)
				}
				/*
				   gx[i][j] = -o.dfdλ[0]*L[0][0] - o.dfdλ[1]*L[1][0] - o.dfdλ[2]*L[2][0]
				   gy[i][j] = -o.dfdλ[0]*L[0][1] - o.dfdλ[1]*L[1][1] - o.dfdλ[2]*L[2][1]
				*/
			}
			k += 1
		}
	}
	if first {
		plt.Reset()
		plt.SetForPng(1, 500, 125)
		PlotRosette(1.1*rmax, true, true, true, 7)
		PlotRefOct(φ, σcCte, false)
		if showpts {
			plt.Plot(σa, σb, "'k.', ms=5")
		}
	}
	if simplec {
		if !only0 {
			plt.ContourSimple(x, y, f, false, 8, "")
		}
		plt.ContourSimple(x, y, f, false, 8, "levels=[0], colors=['blue'], linewidths=[2]")
	} else {
		plt.Contour(x, y, f, "fsz=8")
	}
	if grads {
		plt.Quiver(x, y, gx, gy, "")
	}
	if last {
		plt.AxisOff()
		plt.Equal()
		plt.SaveD("/tmp", filename)
	}
}