def test_vec_mag(self): mesh = UnitSquareMesh(2, 2) V = VectorFunctionSpace(mesh, 'CG', 1) series = TempSeries([(interpolate(Expression(('x[0]', '0'), degree=1), V), 0), (interpolate(Expression(('0', 'x[1]'), degree=1), V), 1)]) mag_series = Eval(sqrt(inner(series, series))) self.assertTrue(error(Expression('x[0]', degree=1), mag_series.getitem(0)) < 1E-14) self.assertTrue(error(Expression('x[1]', degree=1), mag_series.getitem(1)) < 1E-14)
def test_sliding_window(self): mesh = UnitSquareMesh(4, 4) V = FunctionSpace(mesh, 'CG', 1) series = TempSeries([(interpolate(Constant(1), V), 0), (interpolate(Constant(2), V), 1), (interpolate(Constant(3), V), 2), (interpolate(Constant(4), V), 3)]) f_series = Eval(SlidingWindowFilter(Mean, 2, series**2)) assert len(f_series) == 3 assert f_series.times == (1, 2, 3) self.assertTrue(error(Constant(2.5), f_series.getitem(0)) < 1E-14) self.assertTrue(error(Constant(6.5), f_series.getitem(1)) < 1E-14) self.assertTrue(error(Constant(12.5), f_series.getitem(2)) < 1E-14)
def test_sanity11(self): mesh = UnitSquareMesh(5, 5) true = SpatialCoordinate(mesh) me = Eval(true) e = error(true, me) self.assertTrue(e < 1E-14)
def test_comp_tensor_num_vec(self): mesh = UnitSquareMesh(4, 4) r = SpatialCoordinate(mesh) true = Constant(2) * r me = Eval(true) e = error(true, me) self.assertTrue(e < 1E-14)
def test_algebra_fail_different_times(self): mesh = UnitSquareMesh(2, 2) V = FunctionSpace(mesh, 'DG', 0) series0 = TempSeries(((Function(V), 0), (Function(V), 1))) series1 = TempSeries(((Function(V), 0), (Function(V), 2))) with self.assertRaises(AssertionError): Eval(series0 - series1)
def test_max(self): mesh = UnitSquareMesh(4, 4) x, y = SpatialCoordinate(mesh) true = Max(x + y, 2 * y) me = Eval(true) e = error(true, me) self.assertTrue(e < 1E-14)
def test_std(self): mesh = UnitSquareMesh(4, 4) V = FunctionSpace(mesh, 'CG', 1) f = Expression('(x[0]+x[1])*t', t=0, degree=1) ft_pairs = [] for t in np.linspace(0, 2, 80): f.t = t v = interpolate(f, V) ft_pairs.append((v, t)) series = TempSeries(ft_pairs) std = Eval(STD(series)) # Efficiently in PETSc # From definition std_ = Eval(sqrt(Mean(series**2) - Mean(series)**2)) self.assertTrue(error(std_, std) < 1E-14)
def test_sanity12(self): mesh = UnitSquareMesh(5, 5) x, y = SpatialCoordinate(mesh) true = as_vector((x + y, x - y)) me = Eval(true) e = error(true, me) self.assertTrue(e < 1E-14)
def test_cond_logic(self): errors = [] for n in (4, 8, 16, 32, 64): mesh = UnitSquareMesh(n, n) x, y = SpatialCoordinate(mesh) true = conditional(And(x < y, Constant(0) < x), x + y, x - y) me = Eval(true) errors.append(error(true, me)) self.assertTrue((np.diff(errors) < 0).all())
def test_maybe_fix_future(self): mesh = UnitSquareMesh(10, 10) V = FunctionSpace(mesh, 'RT', 1) x = Function(V) y = Function(V) for f in (inner(x, y), as_vector((x[0], y[1])), 2 * x + y): with self.assertRaises(AssertionError): Eval(f)
def test_eigv(self): A = np.array([[1, -2], [-3, 1]]) mesh = UnitSquareMesh(10, 10) V = TensorFunctionSpace(mesh, 'DG', 0) f = interpolate(Constant(A), V) # FIXME: 2*f leads to ComponentTensor which we don't handle well me = Eval(Eigv(f)) true = Constant((np.linalg.eig(A)[1]).T) self.assertTrue(error(true, me) < 1E-14)
def test_maximum(self): A = np.array([[1, -2], [-3, 1]]) mesh = UnitSquareMesh(10, 10) V = TensorFunctionSpace(mesh, 'DG', 0) f = interpolate(Constant(A), V) me = Eval(Maximum(Eigw(f - 3 * f))) # Eval o Declared true = Constant(np.max(np.linalg.eigvals(A - 3 * A))) self.assertTrue(error(true, me) < 1E-14)
def test_cond_simple_conv(self): # Outside of CG1 with nonlinearity? errors = [] for n in (4, 8, 16, 32, 64): mesh = UnitSquareMesh(n, n) x, y = SpatialCoordinate(mesh) true = conditional(x < y, x + y, x - y) me = Eval(true) errors.append(error(true, me)) self.assertTrue((np.diff(errors) < 0).all())
def test_comp_tensor_col_slice(self): mesh = UnitSquareMesh(4, 4) x, y = SpatialCoordinate(mesh) A = as_matrix(((x, 2 * y), (3 * y, 4 * x))) true = A[:, 0] me = Eval(true) e = error(true, me) self.assertTrue(e < 1E-14)
def test_comp_tensor_num_mat(self): mesh = UnitSquareMesh(4, 4) x, y = SpatialCoordinate(mesh) A = as_matrix(((x, y), (y, -x))) true = Constant(2) * A me = Eval(true) e = error(true, me) self.assertTrue(e < 1E-14)
def test_sanity10(self): mesh = UnitSquareMesh(5, 5) V = FunctionSpace(mesh, 'CG', 1) a0 = interpolate(Expression('x[0]', degree=1), V) a1 = interpolate(Expression('x[1]', degree=1), V) true = as_vector((as_vector((a0, a1)), as_vector((-a0, -a1)))) me = Eval(true) e = error(true, me) self.assertTrue(e < 1E-14)
def test_sanity5(self): mesh = UnitSquareMesh(5, 5) T = TensorFunctionSpace(mesh, 'CG', 1) A = interpolate(Expression((('1', 'x[0]'), ('2', 'x[1]')), degree=1), T) expr = det(A) me = Eval(expr) true = Expression('x[1]-2*x[0]', degree=1) e = error(true, me) self.assertTrue(e < 1E-14)
def test_comp_tensor_mat_mat_mat(self): mesh = UnitSquareMesh(4, 4) T = TensorFunctionSpace(mesh, 'DG', 0) A = interpolate(Constant(((1, 2), (3, 4))), T) B = interpolate(Constant(((1, -2), (-1, 4))), T) true = A * B * A me = Eval(true) e = error(true, me) self.assertTrue(e < 1E-14)
def test_sanity8(self): mesh = UnitSquareMesh(5, 5) V = VectorFunctionSpace(mesh, 'CG', 1) v0 = interpolate(Expression(('x[0]+x[1]', '1'), degree=1), V) v1 = interpolate(Expression(('1', 'x[0]'), degree=1), V) me = Eval(inner(v0, v1)) true = Expression('x[1]+2*x[0]', degree=1) e = error(true, me) self.assertTrue(e < 1E-14)
def test_cond_simple(self): mesh = UnitSquareMesh(4, 4) V = FunctionSpace(mesh, 'DG', 0) x = interpolate(Constant(1), V) y = interpolate(Constant(2), V) true = conditional(x < y, x + y, x - y) me = Eval(true) e = error(true, me) self.assertTrue(e < 1E-14)
def test_get(self): mesh = UnitSquareMesh(2, 2) V = VectorFunctionSpace(mesh, 'CG', 1) series = TempSeries([(interpolate(Expression(('x[0]', '0'), degree=1), V), 0), (interpolate(Expression(('0', 'x[1]'), degree=1), V), 1)]) mag_series = Eval(series[0]) # series of first componentsts self.assertTrue(error(Expression('x[0]', degree=1), mag_series.getitem(0)) < 1E-14) self.assertTrue(error(Expression('0', degree=1), mag_series.getitem(1)) < 1E-14) mag_series = Eval(series[1]) # series of secon componentsts self.assertTrue(error(Expression('0', degree=1), mag_series.getitem(0)) < 1E-14) self.assertTrue(error(Expression('x[1]', degree=1), mag_series.getitem(1)) < 1E-14)
def test_sanity4(self): mesh = UnitSquareMesh(5, 5) T = TensorFunctionSpace(mesh, 'CG', 1) A = interpolate( Expression((('x[0]', 'x[1]'), ('2*x[0]+x[1]', 'x[0]+3*x[1]')), degree=1), T) expr = (sym(A) + skew(A))[:, 0] me = Eval(expr) true = Expression(('x[0]', '2*x[0]+x[1]'), degree=1) e = error(true, me) self.assertTrue(e < 1E-14)
def test_cond_logic_simple(self): # We're outside of the CG1! mesh = UnitSquareMesh(4, 4) V = FunctionSpace(mesh, 'DG', 0) x = interpolate(Constant(1), V) y = interpolate(Constant(2), V) true = conditional(And(x < y, 0 < x), x + y, x - y) me = Eval(true) e = error(true, me) self.assertTrue(e < 1E-14)
def test_eigw(self): # FIxME: more advances A = np.array([[1, -2], [-3, 1]]) mesh = UnitSquareMesh(10, 10) V = TensorFunctionSpace(mesh, 'DG', 0) f = interpolate(Constant(A), V) me = Eval(Eigw(f + f)) # Eval o Declared true = Constant(np.linalg.eigvals(A + A)) self.assertTrue(error(true, me) < 1E-14)
def test_sanity1(self): mesh = UnitSquareMesh(5, 5) T = TensorFunctionSpace(mesh, 'DG', 0) u = interpolate( Expression((('x[0]', 'x[1]'), ('2*x[0]+x[1]', 'x[0]+3*x[1]')), degree=1), T) expr = sym(u) + skew(u) me = Eval(expr) true = u e = error(true, me) self.assertTrue(e < 1E-14)
def test_sanity9(self): mesh = UnitSquareMesh(5, 5) V = FunctionSpace(mesh, 'CG', 1) a0 = interpolate(Expression('x[0]', degree=1), V) a1 = interpolate(Expression('x[1]', degree=1), V) me = Eval(as_vector((a0, a1))) x, y = SpatialCoordinate(mesh) true = as_vector((x, y)) e = error(true, me) self.assertTrue(e < 1E-14)
def test_comp_tensor_mat_vec(self): mesh = UnitSquareMesh(4, 4) x, y = SpatialCoordinate(mesh) A = as_matrix(((x, 2 * y), (3 * y, 4 * x))) V = VectorFunctionSpace(mesh, 'CG', 1) b = interpolate(Constant((1, 2)), V) true = A * b me = Eval(true) e = error(true, me) self.assertTrue(e < 1E-14)
def test_sanity8(self): mesh = UnitSquareMesh(5, 5) T = TensorFunctionSpace(mesh, 'CG', 1) A = interpolate(Expression((('1', 'x[0]'), ('2', 'x[1]')), degree=1), T) V = VectorFunctionSpace(mesh, 'CG', 1) v = interpolate(Expression(('x[0]+x[1]', '1'), degree=1), V) me = Eval(dot(v, transpose(A))) true = Expression(('x[1]+2*x[0]', '2*x[0]+3*x[1]'), degree=1) e = error(true, me) self.assertTrue(e < 1E-14)
def test_mean(self): mesh = UnitSquareMesh(2, 2) V = FunctionSpace(mesh, 'CG', 1) f = Expression('(x[0]+x[1])*t', t=0, degree=1) ft_pairs = [] for t in (0, 0.1, 0.4, 0.6, 2.0): f.t = t v = interpolate(f, V) ft_pairs.append((v, t)) mean = Eval(Mean(TempSeries(ft_pairs))) # Eval o Declared f.t = 1.0 self.assertTrue(error(f, mean) < 1E-14)
def test_algebra(self): mesh = UnitSquareMesh(2, 2) V = FunctionSpace(mesh, 'DG', 0) series0 = TempSeries([(interpolate(Constant(1), V), 0), (interpolate(Constant(2), V), 1)]) series1 = TempSeries([(interpolate(Constant(2), V), 0), (interpolate(Constant(3), V), 1)]) series01 = Eval(series1 - series0) self.assertTrue(np.linalg.norm(series01.times - np.array([0, 1])) < 1E-14) # Now each should be 1 for f in series01: self.assertTrue(error(Constant(1), f) < 1E-14)