예제 #1
0
    def test_dynamic_forward(self):
        nfe = 5
        m = make_dynamic_model(nfe=nfe, scheme="FORWARD")
        time = m.time
        t0 = time.first()
        m.flow_in.fix()
        m.height[t0].fix()

        solve_strongly_connected_components(m)

        for con in m.component_data_objects(pyo.Constraint):
            # Sanity check that this is an equality constraint...
            self.assertEqual(pyo.value(con.upper), pyo.value(con.lower))

            # Assert that the constraint is satisfied within tolerance
            self.assertAlmostEqual(pyo.value(con.body),
                                   pyo.value(con.upper),
                                   delta=1e-7)

        for t in time:
            if t == t0:
                self.assertTrue(m.height[t].fixed)
            else:
                self.assertFalse(m.height[t].fixed)
            self.assertFalse(m.flow_out[t].fixed)
            self.assertFalse(m.dhdt[t].fixed)
            self.assertTrue(m.flow_in[t].fixed)
예제 #2
0
    def test_dynamic_backward_no_solver(self):
        nfe = 5
        m = make_dynamic_model(nfe=nfe, scheme="BACKWARD")
        time = m.time
        t0 = time.first()
        m.flow_in.fix()
        m.height[t0].fix()

        with self.assertRaisesRegex(RuntimeError,
                                    "An external solver is required*"):
            solve_strongly_connected_components(m)

        for t in time:
            if t == t0:
                self.assertTrue(m.height[t].fixed)
            else:
                self.assertFalse(m.height[t].fixed)
            self.assertFalse(m.flow_out[t].fixed)
            self.assertFalse(m.dhdt[t].fixed)
            self.assertTrue(m.flow_in[t].fixed)
예제 #3
0
 def test_with_calc_var_kwds(self):
     m = pyo.ConcreteModel()
     m.v0 = pyo.Var()
     m.v1 = pyo.Var()
     m.v2 = pyo.Var(initialize=79703634.05074187)
     m.v2.fix()
     m.p0 = pyo.Param(initialize=3e5)
     m.p1 = pyo.Param(initialize=1.296e12)
     m.con0 = pyo.Constraint(expr=m.v0 == m.p0)
     m.con1 = pyo.Constraint(expr=0.0 == m.p1 * m.v1 / m.v0 + m.v2)
     calc_var_kwds = {"eps": 1e-7}
     # This solve fails to converge without raising the tolerance.
     # The secant method used to converge a linear variable-constraint
     # pair (such as con0, v0 and con1, v1) leaves us with a residual of
     # 1.48e-8 due to roundoff error.
     results = solve_strongly_connected_components(
         m, calc_var_kwds=calc_var_kwds)
     self.assertEqual(len(results), 2)
     self.assertAlmostEqual(m.v0.value, m.p0.value)
     self.assertAlmostEqual(m.v1.value, -18.4499152895)