Beispiel #1
0
 def _make_model_with_external_functions(self):
     m = pyo.ConcreteModel()
     gsl = find_GSL()
     m.bessel = pyo.ExternalFunction(library=gsl,
                                     function="gsl_sf_bessel_J0")
     m.fermi = pyo.ExternalFunction(library=gsl,
                                    function="gsl_sf_fermi_dirac_m1")
     m.v1 = pyo.Var(initialize=1.0)
     m.v2 = pyo.Var(initialize=2.0)
     m.v3 = pyo.Var(initialize=3.0)
     m.con1 = pyo.Constraint(expr=m.v1 == 0.5)
     m.con2 = pyo.Constraint(expr=2 * m.fermi(m.v1) + m.v2**2 - m.v3 == 1.0)
     m.con3 = pyo.Constraint(expr=m.bessel(m.v1) - m.bessel(m.v2) +
                             m.v3**2 == 2.0)
     return m
Beispiel #2
0
def test_cbrt_values():
    m = pyo.ConcreteModel()
    flib = functions_lib()
    m.cbrt = pyo.ExternalFunction(library=flib, function="cbrt")
    assert (abs(pyo.value(m.cbrt(-27.0)) + 3.0) < 0.00001)
    assert (abs(pyo.value(m.cbrt(0.0))) < 0.00001)
    assert (abs(pyo.value(m.cbrt(27.0)) - 3.0) < 0.00001)
Beispiel #3
0
def test_cbrt_hes():
    m = pyo.ConcreteModel()
    flib = functions_lib()
    m.cbrt = pyo.ExternalFunction(library=flib, function="cbrt")
    h = 1e-6
    tol = 1e-5
    for v in [-27.0, -0.5, 0.5, 27]:
        f1, g1, h1 = m.cbrt.evaluate_fgh(args=(v, ))
        f2, g2, h2 = m.cbrt.evaluate_fgh(args=(v + h, ))
        hfd = (g2[0] - g1[0]) / h
        assert (abs(h1[0] - hfd) < tol)
Beispiel #4
0
    def test_external(self):
        DLL = find_GSL()
        if not DLL:
            self.skipTest('Could not find the amplgsl.dll library')

        m = pyo.ConcreteModel()
        m.hypot = pyo.ExternalFunction(library=DLL, function='gsl_hypot')
        m.x = pyo.Var(initialize=0.5)
        m.y = pyo.Var(initialize=1.5)
        e = 2 * m.hypot(m.x, m.x * m.y)
        derivs = reverse_ad(e)
        self.assertAlmostEqual(derivs[m.x], approx_deriv(e, m.x), tol)
        self.assertAlmostEqual(derivs[m.y], approx_deriv(e, m.y), tol)
Beispiel #5
0
    def test_external_function(self):
        DLL = find_GSL()
        if not DLL:
            self.skipTest('Could not find the amplgsl.dll library')

        m = pe.ConcreteModel()
        m.hypot = pe.ExternalFunction(library=DLL, function='gsl_hypot')
        m.x = pe.Var(initialize=0.5)
        m.y = pe.Var(initialize=1.5)
        e = 2 * m.hypot(m.x, m.x * m.y)
        expected = [(ProductExpression, 2), 2,
                    (ExternalFunctionExpression, 2, m.hypot), m.x,
                    (ProductExpression, 2), m.x, m.y]
        pn = convert_expression_to_prefix_notation(e)
        self.assertEqual(expected, pn)
Beispiel #6
0
    def external_func_helper(self, collector: Callable, *args):
        DLL = find_GSL()
        if not DLL:
            self.skipTest('Could not find amplgsl.dll library')

        m = pe.ConcreteModel()
        m.x = pe.Var()
        m.y = pe.Var()
        m.z = pe.Var()
        m.hypot = pe.ExternalFunction(library=DLL, function='gsl_hypot')
        func = m.hypot(m.x, m.x * m.y)
        m.E = pe.Expression(expr=2 * func)
        m.y.fix(3)
        e = m.z + m.x * m.E
        named_exprs, var_list, fixed_vars, external_funcs = collector(e, *args)
        self.assertEqual([m.E], named_exprs)
        self.assertEqual([m.z, m.x, m.y], var_list)
        self.assertEqual([m.y], fixed_vars)
        self.assertEqual([func], external_funcs)
Beispiel #7
0
    def test_external_function(self):
        DLL = find_GSL()
        if not DLL:
            self.skipTest('Could not find the amplgls.dll library')

        opt = pe.SolverFactory('appsi_ipopt')
        if not opt.available(exception_flag=False):
            raise unittest.SkipTest

        m = pe.ConcreteModel()
        m.hypot = pe.ExternalFunction(library=DLL, function='gsl_hypot')
        m.x = pe.Var(bounds=(-10, 10), initialize=2)
        m.y = pe.Var(initialize=2)
        e = 2 * m.hypot(m.x, m.x * m.y)
        m.c = pe.Constraint(expr=e == 2.82843)
        m.obj = pe.Objective(expr=m.x)
        res = opt.solve(m)
        pe.assert_optimal_termination(res)
        self.assertAlmostEqual(pe.value(m.c.body) - pe.value(m.c.lower), 0)
def test_general_cubic_root_finder():
    plib = os.path.join(idaes.bin_directory, "cubic_roots.so")
    m = pyo.ConcreteModel()
    m.croot_l = pyo.ExternalFunction(library=plib, function="cubic_root_l")
    m.croot_m = pyo.ExternalFunction(library=plib, function="cubic_root_m")
    m.croot_h = pyo.ExternalFunction(library=plib, function="cubic_root_h")
    param_dict = {
        1: {
            "b": -3,
            "c": 0.5,
            "d": -1,
            "three": False
        },
        2: {
            "b": -3,
            "c": 0.5,
            "d": 1,
            "three": True
        },
        3: {
            "b": 1,
            "c": 0.5,
            "d": -1,
            "three": False
        },
        4: {
            "b": 1,
            "c": 0.5,
            "d": 2,
            "three": False
        },
    }

    for k, v in param_dict.items():
        b = v["b"]
        c = v["c"]
        d = v["d"]
        fl, gl, hl = m.croot_l.evaluate_fgh(args=(b, c, d))
        fm, gm, hm = m.croot_m.evaluate_fgh(args=(b, c, d))
        fh, gh, hh = m.croot_h.evaluate_fgh(args=(b, c, d))
        if v["three"]:
            assert fl < fm
            assert fm < fh
        else:
            assert pytest.approx(fl, abs=1e-5) == fm
            assert pytest.approx(fm, abs=1e-5) == fh
        assert pytest.approx(0, abs=1e-6) == cubic_function(fl, b, c, d)
        assert pytest.approx(0, abs=1e-6) == cubic_function(fm, b, c, d)
        assert pytest.approx(0, abs=1e-6) == cubic_function(fh, b, c, d)
        g, h = derivs(fl, b, c, d)
        for i, ge in enumerate(g):
            assert pytest.approx(ge, abs=1e-5) == gl[i]
        for i, he in enumerate(h):
            assert pytest.approx(he, abs=1e-5) == hl[i]
        g, h = derivs(fm, b, c, d)
        for i, ge in enumerate(g):
            assert pytest.approx(ge, abs=1e-5) == gm[i]
        for i, he in enumerate(h):
            assert pytest.approx(he, abs=1e-5) == hm[i]
        g, h = derivs(fh, b, c, d)
        for i, ge in enumerate(g):
            assert pytest.approx(ge, abs=1e-5) == gh[i]
        for i, he in enumerate(h):
            assert pytest.approx(he, abs=1e-5) == hh[i]