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_steam(self): mesh = UnitSquareMesh(2, 2) V = FunctionSpace(mesh, 'DG', 0) series0 = TempSeries([(interpolate(Constant(1), V), 0), (interpolate(Constant(2), V), 1)]) v = Function(V) stream_series = stream(2*series0, v) # NOTE: it is crucial that this is lazy. With normal zip # v in all the pairse has the last value for vi, v in izip(series0, stream_series): self.assertTrue(error(2*vi, v) < 1E-14) for i, v in enumerate(stream_series): self.assertTrue(error(2*series0.getitem(i), v) < 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)
def test_algebra_harder(self): mesh = UnitSquareMesh(2, 2) V = FunctionSpace(mesh, 'DG', 0) series0 = TempSeries([(interpolate(Constant(2), V), 0), (interpolate(Constant(3), V), 1)]) series1 = TempSeries([(interpolate(Constant(4), V), 0), (interpolate(Constant(5), V), 1)]) series01 = Eval(series1**2 - 2*series0) self.assertTrue(np.linalg.norm(series01.times - np.array([0, 1])) < 1E-14) # Now each should be 1 for f, true in zip(series01, (Constant(12), Constant(19))): self.assertTrue(error(true, f) < 1E-14)
def test_fail_on_times(self): mesh = UnitSquareMesh(2, 2) V = FunctionSpace(mesh, 'DG', 0) ft_pairs = ((Function(V), 0), (Function(V), -2)) with self.assertRaises(AssertionError): TempSeries(ft_pairs)
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_fail_on_spaces(self): # Different element and degree in series mesh = UnitSquareMesh(2, 2) V = FunctionSpace(mesh, 'DG', 0) W = FunctionSpace(mesh, 'CG', 1) ft_pairs = ((Function(V), 0), (Function(W), 1)) with self.assertRaises(ValueError): TempSeries(ft_pairs)
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 = SlidingWindowFilter(Mean, 2, series) assert len(f_series) == 3 assert f_series.times == (1, 2, 3)
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_clip(self): mesh = UnitSquareMesh(2, 2) V = FunctionSpace(mesh, 'DG', 0) series = TempSeries([(interpolate(Constant(1), V), 0), (interpolate(Constant(2), V), 1), (interpolate(Constant(3), V), 2), (interpolate(Constant(4), V), 3)]) clipped_series = clip(series, 0, 3) self.assertTrue(len(clipped_series)) == 2 self.assertEqual(clipped_series.times, (1, 2)) self.assertTrue(error(Constant(2), clipped_series.getitem(0)) < 1E-14) self.assertTrue(error(Constant(3), clipped_series.getitem(1)) < 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 = Mean(TempSeries(ft_pairs)) f.t = 1.0 self.assertTrue(error(f, mean) < 1E-14)
def test_rms(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)) rms = RMS(TempSeries(ft_pairs)) f.t = sqrt(4 / 3.) # Due to quadrature error self.assertTrue(error(f, rms) < 1E-4)
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_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)