/* 
	Test for crop functionality.
	Slice and dice the magnetization and check
	for correct averages. Also check that we can save/tabulate
*/

Nx := 160
Ny := 50
Nz := 4
c := 1e-9

setGridSize(Nx, Ny, Nz)
setCellSize(c, c, c)

DefRegion(1, xrange(0, inf))
DefRegion(2, yrange(0, inf))
m.SetRegion(0, uniform(1, 0, 0))
m.SetRegion(1, uniform(0, 1, 0))
m.SetRegion(2, uniform(0, 0, -1))

save(m)

tol := 1e-6

m0 := CropX(m, 0, Nx/2)
expect("m0x", m0.Average()[0],  0.5, tol)
expect("m0y", m0.Average()[1],  0.0, tol)
expect("m0z", m0.Average()[2], -0.5, tol)

m1 := CropX(m, Nx/2, Nx)
expect("m1x", m1.Average()[0],  0.0, tol)
expect("m1y", m1.Average()[1],  0.5, tol)
expect("m1z", m1.Average()[2], -0.5, tol)

m2 := CropY(m, 0, Ny/2)
expect("m2x", m2.Average()[0],  0.5, tol)
expect("m2y", m2.Average()[1],  0.5, tol)
expect("m2z", m2.Average()[2],  0.0, tol)

m3 := CropY(m, Ny/2, Ny)
expect("m3x", m3.Average()[0],  0.0, tol)
expect("m3y", m3.Average()[1],  0.0, tol)
expect("m3z", m3.Average()[2], -1.0, tol)

m4 := CropRegion(m, 2)
expect("m4x", m4.Average()[0],  0.0, tol)
expect("m4y", m4.Average()[1],  0.0, tol)
expect("m4z", m4.Average()[2], -1.0, tol)

tableadd(m0)
save(m0)
tableadd(CropLayer(m.Comp(1), 2))
tablesave()
