def test_reduce_colloc_multi_index(self): m = self.m.clone() m.u = Var(m.t, m.s) m2 = m.clone() m3 = m.clone() disc = TransformationFactory('dae.collocation') disc.apply_to(m, nfe=5, ncp=3) disc.reduce_collocation_points(m, contset=m.t, var=m.u, ncp=1) self.assertTrue(hasattr(m, 'u_interpolation_constraints')) self.assertEqual(len(m.u_interpolation_constraints), 30) disc2 = TransformationFactory('dae.collocation') disc2.apply_to(m2, wrt=m2.t, nfe=5, ncp=3) disc2.reduce_collocation_points(m2, contset=m2.t, var=m2.u, ncp=3) self.assertFalse(hasattr(m2, 'u_interpolation_constraints')) disc3 = TransformationFactory('dae.collocation') disc3.apply_to(m3, wrt=m3.t, nfe=5, ncp=3) disc3.reduce_collocation_points(m3, contset=m3.t, var=m3.u, ncp=2) self.assertTrue(hasattr(m3, 'u_interpolation_constraints')) self.assertEqual(len(m3.u_interpolation_constraints), 15)
def test_reduce_colloc_invalid(self): m = self.m.clone() m.u = Var(m.t) m2 = m.clone() disc = TransformationFactory('dae.collocation') disc2 = TransformationFactory('dae.collocation') disc.apply_to(m, nfe=5, ncp=3) # No ContinuousSet specified with self.assertRaises(TypeError): disc.reduce_collocation_points(m, contset=None) # Component passed in is not a ContinuousSet with self.assertRaises(TypeError): disc.reduce_collocation_points(m, contset=m.s) # Call reduce_collocation_points method before applying discretization with self.assertRaises(RuntimeError): disc2.reduce_collocation_points(m2, contset=m2.t) # Call reduce_collocation_points on a ContinuousSet that hasn't been # discretized m2.tt = ContinuousSet(bounds=(0, 1)) disc2.apply_to(m2, wrt=m2.t) with self.assertRaises(ValueError): disc2.reduce_collocation_points(m2, contset=m2.tt) # No Var specified with self.assertRaises(TypeError): disc.reduce_collocation_points(m, contset=m.t, var=None) # Component passed in is not a Var with self.assertRaises(TypeError): disc.reduce_collocation_points(m, contset=m.t, var=m.s) # New ncp not specified with self.assertRaises(TypeError): disc.reduce_collocation_points(m, contset=m.t, var=m.v1, ncp=None) # Negative ncp specified with self.assertRaises(ValueError): disc.reduce_collocation_points(m, contset=m.t, var=m.v1, ncp=-3) # Too large ncp specified with self.assertRaises(ValueError): disc.reduce_collocation_points(m, contset=m.t, var=m.v1, ncp=10) # Passing Vars not indexed by the ContinuousSet m.v2 = Var() m.v3 = Var(m.s) m.v4 = Var(m.s, m.s) with self.assertRaises(IndexError): disc.reduce_collocation_points(m, contset=m.t, var=m.v2, ncp=1) with self.assertRaises(IndexError): disc.reduce_collocation_points(m, contset=m.t, var=m.v3, ncp=1) with self.assertRaises(IndexError): disc.reduce_collocation_points(m, contset=m.t, var=m.v4, ncp=1) # Calling reduce_collocation_points more than once disc.reduce_collocation_points(m, contset=m.t, var=m.u, ncp=1) with self.assertRaises(RuntimeError): disc.reduce_collocation_points(m, contset=m.t, var=m.u, ncp=1)
def test_reduce_colloc_invalid(self): m = self.m.clone() m.u = Var(m.t) m2 = m.clone() disc = TransformationFactory('dae.collocation') disc2 = TransformationFactory('dae.collocation') disc.apply_to(m, nfe=5, ncp=3) # No ContinuousSet specified try: disc.reduce_collocation_points(m, contset=None) self.fail('Expected TypeError') except TypeError: pass # Component passed in is not a ContinuousSet try: disc.reduce_collocation_points(m, contset=m.s) self.fail('Expected TypeError') except TypeError: pass # Call reduce_collocation_points method before applying discretization try: disc2.reduce_collocation_points(m2, contset=m2.t) self.fail('Expected RuntimeError') except RuntimeError: pass # Call reduce_collocation_points on a ContinuousSet that hasn't been # discretized m2.tt = ContinuousSet(bounds=(0, 1)) disc2.apply_to(m2, wrt=m2.t) try: disc2.reduce_collocation_points(m2, contset=m2.tt) self.fail('Expected ValueError') except ValueError: pass # No Var specified try: disc.reduce_collocation_points(m, contset=m.t, var=None) self.fail('Expected TypeError') except TypeError: pass # Component passed in is not a Var try: disc.reduce_collocation_points(m, contset=m.t, var=m.s) self.fail('Expected TypeError') except TypeError: pass # New ncp not specified try: disc.reduce_collocation_points(m, contset=m.t, var=m.v1, ncp=None) self.fail('Expected TypeError') except TypeError: pass # Negative ncp specified try: disc.reduce_collocation_points(m, contset=m.t, var=m.v1, ncp=-3) self.fail('Expected ValueError') except ValueError: pass # Too large ncp specified try: disc.reduce_collocation_points(m, contset=m.t, var=m.v1, ncp=10) self.fail('Expected ValueError') except ValueError: pass # Passing Vars not indexed by the ContinuousSet m.v2 = Var() m.v3 = Var(m.s) m.v4 = Var(m.s, m.s) try: disc.reduce_collocation_points(m, contset=m.t, var=m.v2, ncp=1) self.fail('Expected IndexError') except IndexError: pass try: disc.reduce_collocation_points(m, contset=m.t, var=m.v3, ncp=1) self.fail('Expected IndexError') except IndexError: pass try: disc.reduce_collocation_points(m, contset=m.t, var=m.v4, ncp=1) self.fail('Expected IndexError') except IndexError: pass # Calling reduce_collocation_points more than once disc.reduce_collocation_points(m, contset=m.t, var=m.u, ncp=1) try: disc.reduce_collocation_points(m, contset=m.t, var=m.u, ncp=1) self.fail('Expected RuntimeError') except RuntimeError: pass