def test_tensor_divergence_cart(ndim): """test different tensor divergence operators""" bcs = _get_random_grid_bcs(ndim, dx="uniform", periodic="random", rank=2) field = Tensor2Field.random_uniform(bcs.grid) res1 = field.divergence(bcs, backend="scipy").data res2 = field.divergence(bcs, backend="numba").data assert res1.shape == (ndim,) + bcs.grid.shape np.testing.assert_allclose(res1, res2)
def test_tensors_basic(): """test some tensor calculations""" grid = CartesianGrid([[0.1, 0.3], [-2, 3]], [3, 4]) t1 = Tensor2Field(grid, np.full((2, 2) + grid.shape, 1)) t2 = Tensor2Field(grid, np.full((2, 2) + grid.shape, 2)) np.testing.assert_allclose(t2.average, [[2, 2], [2, 2]]) assert t1.magnitude == pytest.approx(2) assert t1["x", "x"] == t1[0, 0] assert t1["x", 1] == t1[0, "y"] == t1[0, 1] t1[0, 0] = t1[0, 0] t3 = t1 + t2 assert t3.grid == grid np.testing.assert_allclose(t3.data, 3) t1 += t2 np.testing.assert_allclose(t1.data, 3) field = Tensor2Field.random_uniform(grid) trace = field.trace() assert isinstance(trace, ScalarField) np.testing.assert_allclose(trace.data, field.data.trace()) t1 = Tensor2Field(grid) t1[0, 0] = 1 t1[0, 1] = 2 t1[1, 0] = 3 t1[1, 1] = 4 for method, value in [ ("min", 1), ("max", 4), ("norm", np.linalg.norm([[1, 2], [3, 4]])), ("squared_sum", 30), ("norm_squared", 30), ("trace", 5), ("invariant1", 5), ("invariant2", -1), ]: p1 = t1.to_scalar(method) assert p1.data.shape == grid.shape np.testing.assert_allclose(p1.data, value) for idx in ((1, ), (1, 2, 3), (1.5, 2), ("a", "b"), 1.0): with pytest.raises(IndexError): t1[idx] t2 = FieldBase.from_state(t1.attributes, data=t1.data) assert t1 == t2 assert t1.grid is t2.grid attrs = Tensor2Field.unserialize_attributes(t1.attributes_serialized) t2 = FieldBase.from_state(attrs, data=t1.data) assert t1 == t2 assert t1.grid is not t2.grid
def test_tensor_invariants(): """test the invariants""" # dim == 1 f = Tensor2Field.random_uniform(UnitGrid([3])) np.testing.assert_allclose( f.to_scalar("invariant1").data, f.to_scalar("invariant3").data) np.testing.assert_allclose(f.to_scalar("invariant2").data, 0) # dim == 2 f = Tensor2Field.random_uniform(UnitGrid([3, 3])) invs = [f.to_scalar(f"invariant{i}").data for i in range(1, 4)] np.testing.assert_allclose(2 * invs[1], invs[2]) a = np.random.uniform(0, 2 * np.pi) # pick random rotation angle rot = Tensor2Field(f.grid) rot.data[0, 0, ...] = np.cos(a) rot.data[0, 1, ...] = np.sin(a) rot.data[1, 0, ...] = -np.sin(a) rot.data[1, 1, ...] = np.cos(a) f_rot = rot @ f @ rot.transpose() # apply the transpose for i, inv in enumerate(invs, 1): np.testing.assert_allclose( inv, f_rot.to_scalar(f"invariant{i}").data, err_msg=f"Mismatch in invariant {i}", ) # dim == 3 from scipy.spatial.transform import Rotation f = Tensor2Field.random_uniform(UnitGrid([1, 1, 1])) rot = Tensor2Field(f.grid) rot_mat = Rotation.from_rotvec(np.random.randn(3)).as_matrix() rot.data = rot_mat.reshape(3, 3, 1, 1, 1) f_rot = rot @ f @ rot.transpose() # apply the transpose for i in range(1, 4): np.testing.assert_allclose( f.to_scalar(f"invariant{i}").data, f_rot.to_scalar(f"invariant{i}").data, err_msg=f"Mismatch in invariant {i}", )
def test_collections(): """test field collections""" grid = UnitGrid([3, 4]) sf = ScalarField.random_uniform(grid, label="sf") vf = VectorField.random_uniform(grid, label="vf") tf = Tensor2Field.random_uniform(grid, label="tf") fields = FieldCollection([sf, vf, tf]) assert fields.data.shape == (7, 3, 4) assert isinstance(str(fields), str) fields.data[:] = 0 np.testing.assert_allclose(sf.data, 0) np.testing.assert_allclose(vf.data, 0) np.testing.assert_allclose(tf.data, 0) assert fields[0] is fields["sf"] assert fields[1] is fields["vf"] assert fields[2] is fields["tf"] with pytest.raises(KeyError): fields["42"] sf.data = 1 vf.data = 1 tf.data = 1 np.testing.assert_allclose(fields.data, 1) assert all(np.allclose(i, 12) for i in fields.integrals) assert all(np.allclose(i, 1) for i in fields.averages) assert np.allclose(fields.magnitudes, np.sqrt([1, 2, 4])) assert sf.data.shape == (3, 4) assert vf.data.shape == (2, 3, 4) assert tf.data.shape == (2, 2, 3, 4) c2 = FieldBase.from_state(fields.attributes, data=fields.data) assert c2 == fields assert c2.grid is grid attrs = FieldCollection.unserialize_attributes( fields.attributes_serialized) c2 = FieldCollection.from_state(attrs, data=fields.data) assert c2 == fields assert c2.grid is not grid fields["sf"] = 2.0 np.testing.assert_allclose(sf.data, 2) with pytest.raises(KeyError): fields["42"] = 0 fields.plot(subplot_args=[{}, {"scale": 1}, {"colorbar": False}])
def test_smoothing_collection(): """ test smoothing of a FieldCollection """ grid = UnitGrid([3, 4], periodic=[True, False]) sf = ScalarField.random_uniform(grid) vf = VectorField.random_uniform(grid) tf = Tensor2Field.random_uniform(grid) fields = FieldCollection([sf, vf, tf]) sgm = 0.5 + np.random.random() out = fields.smooth(sigma=sgm) for i in range(3): np.testing.assert_allclose(out[i].data, fields[i].smooth(sgm).data) out.data = 0 fields.smooth(sigma=sgm, out=out) for i in range(3): np.testing.assert_allclose(out[i].data, fields[i].smooth(sgm).data)