class TestIpoptException: """ Check that an exception is thrown if Ipopt returns with return status != 0, 1, 6 (succeeded, acceptable, feasible point found) """ @classmethod def setUpClass(cls): """ Compile the test model. """ # compile cstr fpath_cstr = os.path.join(get_files_path(), 'Modelica', 'CSTR.mop') cpath_cstr = "CSTR.CSTR_Init" compile_jmu(cpath_cstr, fpath_cstr, compiler_options={"enable_variable_scaling":True}) def setUp(self): """Test setUp. Load the test model.""" cpath_cstr = "CSTR.CSTR_Init" fname_cstr = cpath_cstr.replace('.','_',1) self.fname_cstr = fname_cstr self.cstr = JMUModel(fname_cstr+'.jmu') @testattr(ipopt = True) def test_exception_thrown(self): """ Test that an IpoptException is thrown if initialization fails. """ opts = self.cstr.initialize_options() opts['IPOPT_options']['max_iter'] = 1 try: self.cstr.initialize(options = opts) except IpoptException as e: assert str(e) == "Ipopt failed with return code: -1 Please see Ipopt documentation for more information." else: assert False, "No IpoptException thrown when initialization fails (running VDP with max_iter = 1)."
class TestIpoptException: """ Check that an exception is thrown if Ipopt returns with return status != 0, 1, 6 (succeeded, acceptable, feasible point found) """ @classmethod def setUpClass(cls): """ Compile the test model. """ # compile cstr fpath_cstr = os.path.join(get_files_path(), 'Modelica', 'CSTR.mop') cpath_cstr = "CSTR.CSTR_Init" compile_jmu(cpath_cstr, fpath_cstr, compiler_options={"enable_variable_scaling": True}) def setUp(self): """Test setUp. Load the test model.""" cpath_cstr = "CSTR.CSTR_Init" fname_cstr = cpath_cstr.replace('.', '_', 1) self.fname_cstr = fname_cstr self.cstr = JMUModel(fname_cstr + '.jmu') @testattr(ipopt=True) def test_exception_thrown(self): """ Test that an IpoptException is thrown if initialization fails. """ opts = self.cstr.initialize_options() opts['IPOPT_options']['max_iter'] = 1 try: self.cstr.initialize(options=opts) except IpoptException as e: assert str( e ) == "Ipopt failed with return code: -1 Please see Ipopt documentation for more information." else: assert False, "No IpoptException thrown when initialization fails (running VDP with max_iter = 1)."
def test_initialize_with_solverargs(self): """ Test the pyjmi.JMUModel.initialize function using all default parameters. """ fpath_pend = os.path.join(get_files_path(), 'Modelica', 'Pendulum_pack.mop') cpath_pend = "Pendulum_pack.Pendulum" jmu_pend = compile_jmu(cpath_pend, fpath_pend) pend = JMUModel(jmu_pend) res = pend.initialize(options={'IPOPT_options':{'max_iter':1000}}) assert N.abs(res.final('theta') - 0.1) < 1e-3 assert N.abs(res.final('dtheta') - 0.) < 1e-3 assert N.abs(res.final('x') - 0) < 1e-3 assert N.abs(res.final('dx') - 0) < 1e-3 assert N.abs(res.final('der(theta)') - 0) < 1e-3 assert N.abs(res.final('der(dtheta)') - 0.09983341) < 1e-3 assert N.abs(res.final('der(x)') - 0) < 1e-3 assert N.abs(res.final('der(dx)') - 0) < 1e-3
def test_initialize_with_solverargs(self): """ Test the pyjmi.JMUModel.initialize function using all default parameters. """ fpath_pend = os.path.join(get_files_path(), 'Modelica', 'Pendulum_pack.mop') cpath_pend = "Pendulum_pack.Pendulum" jmu_pend = compile_jmu(cpath_pend, fpath_pend) pend = JMUModel(jmu_pend) res = pend.initialize(options={'IPOPT_options':{'max_iter':1000}}) assert N.abs(res.final('theta') - 0.1) < 1e-3 assert N.abs(res.final('dtheta') - 0.) < 1e-3 assert N.abs(res.final('x') - 0) < 1e-3 assert N.abs(res.final('dx') - 0) < 1e-3 assert N.abs(res.final('der(theta)') - 0) < 1e-3 assert N.abs(res.final('der(dtheta)') - 0.09983341) < 1e-3 assert N.abs(res.final('der(x)') - 0) < 1e-3 assert N.abs(res.final('der(dx)') - 0) < 1e-3