class TestNLPInit: """ Test evaluation of function in NLPInitialization and solution of initialization problems. """ @classmethod def setUpClass(cls): """Sets up the test class.""" fpath_daeinit = os.path.join(get_files_path(), 'Modelica', 'DAEInitTest.mo') cpath_daeinit = "DAEInitTest" compile_jmu(cpath_daeinit, fpath_daeinit, compiler_options={ 'state_start_values_fixed': True, 'variability_propagation': False }) def setUp(self): """Test setUp. Load the test model.""" # Load the dynamic library and XML data cpath_daeinit = "DAEInitTest" fname_daeinit = cpath_daeinit.replace('.', '_', 1) self.dae_init_test = JMUModel(fname_daeinit + '.jmu') # This is to check that values set in the model prior to # creation of the NLPInitialization object are used as an # initial guess. self.dae_init_test.set('y1', 0.3) self.init_nlp = NLPInitialization(self.dae_init_test) self.init_nlp_ipopt = InitializationOptimizer(self.init_nlp) @testattr(ipopt=True) def test_init_opt_get_dimensions(self): """ Test NLPInitialization.init_opt_get_dimensions""" res_n_x = 8 res_n_h = 8 res_dh_n_nz = 17 n_x, n_h, dh_n_nz = self.init_nlp.init_opt_get_dimensions() assert N.abs(res_n_x-n_x) + N.abs(res_n_h-n_h) + \ N.abs(res_dh_n_nz-dh_n_nz)==0 @testattr(ipopt=True) def test_init_opt_get_set_x_init(self): n_x, n_h, dh_n_nz = self.init_nlp.init_opt_get_dimensions() # Test init_opt_get_x_init res_x_init = N.array([0, 0, 3, 4, 1, 0, 0, 0]) x_init = N.zeros(n_x) self.init_nlp.init_opt_get_initial(x_init) #print x_init assert N.sum(N.abs(res_x_init - x_init)) < 1e-3 # Test init_opt_set_x_init res_x_init = N.ones(n_x) x_init = N.ones(n_x) self.init_nlp.init_opt_set_initial(x_init) self.init_nlp.init_opt_get_initial(x_init) assert N.sum(N.abs(res_x_init - x_init)) < 1e-3 @testattr(ipopt=True) def test_init_opt_get_set_bounds(self): n_x, n_h, dh_n_nz = self.init_nlp.init_opt_get_dimensions() # Test init_opt_get_bounds res_x_lb = -1e20 * N.ones(n_x) res_x_ub = 1e20 * N.ones(n_x) x_lb = N.zeros(n_x) x_ub = N.zeros(n_x) self.init_nlp.init_opt_get_bounds(x_lb, x_ub) assert N.sum(N.abs(res_x_lb - x_lb)) < 1e-3 assert N.sum(N.abs(res_x_lb - x_lb)) < 1e-3 # Test init_opt_set_bounds res_x_lb = -5000 * N.ones(n_x) res_x_ub = 5000 * N.ones(n_x) x_lb = -5000 * N.ones(n_x) x_ub = 5000 * N.ones(n_x) self.init_nlp.init_opt_set_bounds(x_lb, x_ub) self.init_nlp.init_opt_get_bounds(x_lb, x_ub) assert N.sum(N.abs(res_x_lb - x_lb)) < 1e-3 assert N.sum(N.abs(res_x_lb - x_lb)) < 1e-3 @testattr(ipopt=True) def test_init_opt_f(self): n_x, n_h, dh_n_nz = self.init_nlp.init_opt_get_dimensions() # Test init_opt_f res_f = N.array([0.0]) f = N.zeros(1) self.init_nlp.init_opt_f(f) #print f assert N.sum(N.abs(res_f - f)) < 1e-3 @testattr(ipopt=True) def test_init_opt_df(self): n_x, n_h, dh_n_nz = self.init_nlp.init_opt_get_dimensions() # Test init_opt_df res_df = N.zeros(n_x) df = N.ones(n_x) self.init_nlp.init_opt_df(df) #print df assert N.sum(N.abs(res_df - df)) < 1e-3 @testattr(ipopt=True) def test_init_opt_h(self): n_x, n_h, dh_n_nz = self.init_nlp.init_opt_get_dimensions() # Test init_opt_h res_h = N.array([ -1.98158529e+02, -2.43197505e-01, 5.12000000e+02, 5.00000000e+00, 1.41120008e-01, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00 ]) h = N.zeros(n_h) self.init_nlp.init_opt_h(h) #print h assert N.sum(N.abs(res_h - h)) < 1e-3 @testattr(ipopt=True) def test_init_opt_dh(self): n_x, n_h, dh_n_nz = self.init_nlp.init_opt_get_dimensions() # Test init_opt_dh res_dh = N.array([ -1., -1., -135., 192., -0.9899925, -1., -48., 0.65364362, -1., 0.54030231, -2., -1., -1., 0.9899925, 192., -1., -1., ]) dh = N.ones(dh_n_nz) self.init_nlp.init_opt_dh(dh) #print dh assert N.sum(N.abs(res_dh - dh)) < 1e-3 @testattr(ipopt=True) def test_init_opt_dh_nz_indices(self): n_x, n_h, dh_n_nz = self.init_nlp.init_opt_get_dimensions() # Test init_opt_dh_nz_indices res_dh_irow = N.array( [1, 2, 1, 3, 5, 7, 1, 2, 8, 1, 2, 6, 3, 5, 3, 4, 5]) res_dh_icol = N.array( [1, 2, 3, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 7, 7, 8]) dh_irow = N.zeros(dh_n_nz, dtype=N.int32) dh_icol = N.zeros(dh_n_nz, dtype=N.int32) self.init_nlp.init_opt_dh_nz_indices(dh_irow, dh_icol) assert N.sum(N.abs(res_dh_irow - dh_irow)) < 1e-3 assert N.sum(N.abs(res_dh_icol - dh_icol)) < 1e-3 @testattr(ipopt=True) def test_init_opt_solve(self): n_x, n_h, dh_n_nz = self.init_nlp.init_opt_get_dimensions() # self.init_nlp_ipopt.init_opt_ipopt_set_string_option("derivative_test","first-order") self.init_nlp_ipopt.init_opt_ipopt_solve() print self.dae_init_test.z res_Z = N.array([ 5., -198.1585290151921, -0.2431975046920718, 3.0, 4.0, 1.0, 2197.0, 5.0, -0.92009689684513785, 0., 0, 0, 0, 0, 0, 0, 0, 0 ]) assert max(N.abs(res_Z - self.dae_init_test.z)) < 1e-3 @testattr(ipopt=True) def test_statistics(self): """ Test of 'jmi_init_opt_get_statistics'. """ # Solve the optimization problem self.init_nlp_ipopt.init_opt_ipopt_solve() (return_status, iters, cost, time) = self.init_nlp_ipopt.init_opt_ipopt_get_statistics() assert return_status == 0 assert abs(cost - 2.4134174e+06) < 1 @testattr(ipopt=True) def test_init_opt_write_result(self): cpath_daeinit = "DAEInitTest" fname_daeinit = cpath_daeinit.replace('.', '_', 1) # self.init_nlp_ipopt.init_opt_ipopt_set_string_option("derivative_test","first-order") self.init_nlp_ipopt.init_opt_ipopt_solve() self.init_nlp.export_result_dymola() res = ResultDymolaTextual(fname_daeinit + "_result.txt") res_Z = N.array([ 5., -198.1585290151921, -0.2431975046920718, 3.0, 4.0, 1.0, 2197.0, 5.0, -0.92009689684513785, 0. ]) assert N.abs(res_Z[0] - res.get_variable_data("p").x[0]) < 1e-3 assert N.abs(res_Z[1] - res.get_variable_data("der(x1)").x[0]) < 1e-3 assert N.abs(res_Z[2] - res.get_variable_data("der(x2)").x[0]) < 1e-3 assert N.abs(res_Z[3] - res.get_variable_data("x1").x[0]) < 1e-3 assert N.abs(res_Z[4] - res.get_variable_data("x2").x[0]) < 1e-3 assert N.abs(res_Z[5] - res.get_variable_data("u").x[0]) < 1e-3 assert N.abs(res_Z[6] - res.get_variable_data("y1").x[0]) < 1e-3 assert N.abs(res_Z[7] - res.get_variable_data("y2").x[0]) < 1e-3 assert N.abs(res_Z[8] - res.get_variable_data("y3").x[0]) < 1e-3 @testattr(ipopt=True) def test_invalid_string_option(self): """Test that exceptions are thrown when invalid IPOPT options are set.""" nose.tools.assert_raises( Exception, self.init_nlp_ipopt.init_opt_ipopt_set_string_option, 'invalid_option', 'val') @testattr(ipopt=True) def test_invalid_int_option(self): """Test that exceptions are thrown when invalid IPOPT options are set.""" nose.tools.assert_raises( Exception, self.init_nlp_ipopt.init_opt_ipopt_set_int_option, 'invalid_option', 1) @testattr(ipopt=True) def test_invalid_num_option(self): """Test that exceptions are thrown when invalid IPOPT options are set.""" nose.tools.assert_raises( Exception, self.init_nlp_ipopt.init_opt_ipopt_set_num_option, 'invalid_option', 1.0)
class TestNLPInit: """ Test evaluation of function in NLPInitialization and solution of initialization problems. """ @classmethod def setUpClass(cls): """Sets up the test class.""" fpath_daeinit = os.path.join(get_files_path(), 'Modelica', 'DAEInitTest.mo') cpath_daeinit = "DAEInitTest" compile_jmu(cpath_daeinit, fpath_daeinit, compiler_options={'state_start_values_fixed':True, 'variability_propagation':False}) def setUp(self): """Test setUp. Load the test model.""" # Load the dynamic library and XML data cpath_daeinit = "DAEInitTest" fname_daeinit = cpath_daeinit.replace('.','_',1) self.dae_init_test = JMUModel(fname_daeinit+'.jmu') # This is to check that values set in the model prior to # creation of the NLPInitialization object are used as an # initial guess. self.dae_init_test.set('y1',0.3) self.init_nlp = NLPInitialization(self.dae_init_test) self.init_nlp_ipopt = InitializationOptimizer(self.init_nlp) @testattr(ipopt = True) def test_init_opt_get_dimensions(self): """ Test NLPInitialization.init_opt_get_dimensions""" res_n_x = 8 res_n_h = 8 res_dh_n_nz = 17 n_x, n_h, dh_n_nz = self.init_nlp.init_opt_get_dimensions() assert N.abs(res_n_x-n_x) + N.abs(res_n_h-n_h) + \ N.abs(res_dh_n_nz-dh_n_nz)==0 @testattr(ipopt = True) def test_init_opt_get_set_x_init(self): n_x, n_h, dh_n_nz = self.init_nlp.init_opt_get_dimensions() # Test init_opt_get_x_init res_x_init = N.array([0,0,3,4,1,0,0,0]) x_init = N.zeros(n_x) self.init_nlp.init_opt_get_initial(x_init) #print x_init assert N.sum(N.abs(res_x_init-x_init))<1e-3 # Test init_opt_set_x_init res_x_init = N.ones(n_x) x_init = N.ones(n_x) self.init_nlp.init_opt_set_initial(x_init) self.init_nlp.init_opt_get_initial(x_init) assert N.sum(N.abs(res_x_init-x_init))<1e-3 @testattr(ipopt = True) def test_init_opt_get_set_bounds(self): n_x, n_h, dh_n_nz = self.init_nlp.init_opt_get_dimensions() # Test init_opt_get_bounds res_x_lb = -1e20*N.ones(n_x) res_x_ub = 1e20*N.ones(n_x) x_lb = N.zeros(n_x) x_ub = N.zeros(n_x) self.init_nlp.init_opt_get_bounds(x_lb,x_ub) assert N.sum(N.abs(res_x_lb-x_lb))<1e-3 assert N.sum(N.abs(res_x_lb-x_lb))<1e-3 # Test init_opt_set_bounds res_x_lb = -5000*N.ones(n_x) res_x_ub = 5000*N.ones(n_x) x_lb = -5000*N.ones(n_x) x_ub = 5000*N.ones(n_x) self.init_nlp.init_opt_set_bounds(x_lb,x_ub) self.init_nlp.init_opt_get_bounds(x_lb,x_ub) assert N.sum(N.abs(res_x_lb-x_lb))<1e-3 assert N.sum(N.abs(res_x_lb-x_lb))<1e-3 @testattr(ipopt = True) def test_init_opt_f(self): n_x, n_h, dh_n_nz = self.init_nlp.init_opt_get_dimensions() # Test init_opt_f res_f = N.array([0.0]) f = N.zeros(1) self.init_nlp.init_opt_f(f) #print f assert N.sum(N.abs(res_f-f))<1e-3 @testattr(ipopt = True) def test_init_opt_df(self): n_x, n_h, dh_n_nz = self.init_nlp.init_opt_get_dimensions() # Test init_opt_df res_df = N.zeros(n_x) df = N.ones(n_x) self.init_nlp.init_opt_df(df) #print df assert N.sum(N.abs(res_df-df))<1e-3 @testattr(ipopt = True) def test_init_opt_h(self): n_x, n_h, dh_n_nz = self.init_nlp.init_opt_get_dimensions() # Test init_opt_h res_h = N.array([ -1.98158529e+02, -2.43197505e-01, 5.12000000e+02, 5.00000000e+00, 1.41120008e-01, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00]) h = N.zeros(n_h) self.init_nlp.init_opt_h(h) #print h assert N.sum(N.abs(res_h-h))<1e-3 @testattr(ipopt = True) def test_init_opt_dh(self): n_x, n_h, dh_n_nz = self.init_nlp.init_opt_get_dimensions() # Test init_opt_dh res_dh = N.array([ -1., -1., -135., 192., -0.9899925, -1., -48., 0.65364362, -1., 0.54030231, -2., -1., -1., 0.9899925, 192., -1., -1., ]) dh = N.ones(dh_n_nz) self.init_nlp.init_opt_dh(dh) #print dh assert N.sum(N.abs(res_dh-dh))<1e-3 @testattr(ipopt = True) def test_init_opt_dh_nz_indices(self): n_x, n_h, dh_n_nz = self.init_nlp.init_opt_get_dimensions() # Test init_opt_dh_nz_indices res_dh_irow = N.array([1, 2, 1, 3, 5, 7, 1, 2, 8, 1, 2, 6, 3, 5, 3, 4, 5]) res_dh_icol = N.array([1, 2, 3, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 7, 7, 8]) dh_irow = N.zeros(dh_n_nz,dtype=N.int32) dh_icol = N.zeros(dh_n_nz,dtype=N.int32) self.init_nlp.init_opt_dh_nz_indices(dh_irow,dh_icol) assert N.sum(N.abs(res_dh_irow-dh_irow))<1e-3 assert N.sum(N.abs(res_dh_icol-dh_icol))<1e-3 @testattr(ipopt = True) def test_init_opt_solve(self): n_x, n_h, dh_n_nz = self.init_nlp.init_opt_get_dimensions() # self.init_nlp_ipopt.init_opt_ipopt_set_string_option("derivative_test","first-order") self.init_nlp_ipopt.init_opt_ipopt_solve() print self.dae_init_test.z res_Z = N.array([5., -198.1585290151921, -0.2431975046920718, 3.0, 4.0, 1.0, 2197.0, 5.0, -0.92009689684513785, 0.,0,0,0,0,0,0,0,0]) assert max(N.abs(res_Z-self.dae_init_test.z))<1e-3 @testattr(ipopt = True) def test_statistics(self): """ Test of 'jmi_init_opt_get_statistics'. """ # Solve the optimization problem self.init_nlp_ipopt.init_opt_ipopt_solve() (return_status,iters,cost,time) = self.init_nlp_ipopt.init_opt_ipopt_get_statistics() assert return_status==0 assert abs(cost-2.4134174e+06)<1 @testattr(ipopt = True) def test_init_opt_write_result(self): cpath_daeinit = "DAEInitTest" fname_daeinit = cpath_daeinit.replace('.','_',1) # self.init_nlp_ipopt.init_opt_ipopt_set_string_option("derivative_test","first-order") self.init_nlp_ipopt.init_opt_ipopt_solve() self.init_nlp.export_result_dymola() res = ResultDymolaTextual(fname_daeinit + "_result.txt") res_Z = N.array([5., -198.1585290151921, -0.2431975046920718, 3.0, 4.0, 1.0, 2197.0, 5.0, -0.92009689684513785, 0.]) assert N.abs(res_Z[0] - res.get_variable_data("p").x[0])<1e-3 assert N.abs(res_Z[1] - res.get_variable_data("der(x1)").x[0])<1e-3 assert N.abs(res_Z[2] - res.get_variable_data("der(x2)").x[0])<1e-3 assert N.abs(res_Z[3] - res.get_variable_data("x1").x[0])<1e-3 assert N.abs(res_Z[4] - res.get_variable_data("x2").x[0])<1e-3 assert N.abs(res_Z[5] - res.get_variable_data("u").x[0])<1e-3 assert N.abs(res_Z[6] - res.get_variable_data("y1").x[0])<1e-3 assert N.abs(res_Z[7] - res.get_variable_data("y2").x[0])<1e-3 assert N.abs(res_Z[8] - res.get_variable_data("y3").x[0])<1e-3 @testattr(ipopt = True) def test_invalid_string_option(self): """Test that exceptions are thrown when invalid IPOPT options are set.""" nose.tools.assert_raises(Exception, self.init_nlp_ipopt.init_opt_ipopt_set_string_option, 'invalid_option','val') @testattr(ipopt = True) def test_invalid_int_option(self): """Test that exceptions are thrown when invalid IPOPT options are set.""" nose.tools.assert_raises(Exception, self.init_nlp_ipopt.init_opt_ipopt_set_int_option, 'invalid_option',1) @testattr(ipopt = True) def test_invalid_num_option(self): """Test that exceptions are thrown when invalid IPOPT options are set.""" nose.tools.assert_raises(Exception, self.init_nlp_ipopt.init_opt_ipopt_set_num_option, 'invalid_option',1.0)
class TestKInitSolve: """ Test evaluation of function in NLPInitialization and solution of initialization problems. """ @classmethod def setUpClass(cls): """Sets up the test class.""" # Compile the stationary initialization model into a JMU fpath1 = os.path.join(get_files_path(), 'Modelica', 'CSTRLib.mo') fpath2 = os.path.join(get_files_path(), 'Modelica', 'TestMinMax.mo') compile_jmu("CSTRLib.Components.Two_CSTRs_stat_init", fpath1) #compile_jmu("Tests.TestInvalidStart", fpath2) compile_jmu("TestMinMax.TestGuess", fpath2) def setUp(self): """Test setUp. Load the test model.""" # Load models self.model = JMUModel("CSTRLib_Components_Two_CSTRs_stat_init.jmu") self.problem = JMUAlgebraic(self.model) self.solver = KINSOL(self.problem) """ self.model_test_start = JMUModel(self.invalidStart) self.problem_test_start = JMUAlgebraic(self.model_test_start) """ self.model_test_minmax = JMUModel("TestMinMax_TestGuess.jmu") self.problem_test_minmax = JMUAlgebraic(self.model_test_minmax) # Set inputs for Stationary point A u1_0_A = 1 u2_0_A = 1 self.model.set('u1', u1_0_A) self.model.set('u2', u2_0_A) self.dx0 = N.zeros(6) self.x0 = N.array([ 200., 3.57359316e-02, 446.471014, 100., 1.79867213e-03, 453.258466 ]) self.w0 = N.array([100., 100., -48.1909]) @testattr(assimulo=True) def test_inits(self): """ test if solver is correctly initialized """ nose.tools.assert_true(self.solver._use_jac) nose.tools.assert_equals(self.problem._neqF0, 15) nose.tools.assert_equals(self.problem._dx_size, 6) nose.tools.assert_equals(self.problem._x_size, 6) nose.tools.assert_equals(self.problem._w_size, 3) nose.tools.assert_equals(self.problem._mark, 12) @testattr(assimulo=True) def test_jac_settings(self): """ test if user can set usage of jacobian """ self.solver.set_jac_usage(True) nose.tools.assert_true(self.solver._use_jac) self.solver.set_jac_usage(False) nose.tools.assert_false(self.solver._use_jac) self.solver.set_jac_usage(True) # test bad cases nose.tools.assert_raises(KINSOL_Exception, self.solver.set_jac_usage, 2) nose.tools.assert_raises(KINSOL_Exception, self.solver.set_jac_usage, 'a') nose.tools.assert_raises(KINSOL_Exception, self.solver.set_jac_usage, None) nose.tools.assert_raises(KINSOL_Exception, self.solver.set_jac_usage, [True, False]) nose.tools.assert_raises(KINSOL_Exception, self.solver.set_jac_usage, N.array([True, False])) @testattr(assimulo=True) def test_constraint_settings(self): """ test if user can set usage of constraints """ const = N.ones(self.problem._neqF0) # test boolean settings self.problem.set_constraints_usage(True) nose.tools.assert_true(self.problem.use_constraints) self.problem.set_constraints_usage(False) nose.tools.assert_false(self.problem.use_constraints) # test if constraints can be set properly self.problem.set_constraints_usage(True, const) nose.tools.assert_true(self.problem.use_constraints) res1 = const == self.problem.constraints for r1 in res1: nose.tools.assert_true(r1) self.problem.set_constraints_usage(False, const) nose.tools.assert_false(self.problem.use_constraints) res1 = const == self.problem.constraints for r1 in res1: nose.tools.assert_true(r1) # test if constraints resets self.problem.set_constraints_usage(False) nose.tools.assert_true(self.problem.constraints == None) self.problem.set_constraints_usage(True, const) self.problem.set_constraints_usage(True) nose.tools.assert_true(self.problem.constraints != None) #test bad input nose.tools.assert_raises(JMUAlgebraic_Exception, self.problem.set_constraints_usage, 2) nose.tools.assert_raises(JMUAlgebraic_Exception, self.problem.set_constraints_usage, 'a') nose.tools.assert_raises(JMUAlgebraic_Exception, self.problem.set_constraints_usage, None) nose.tools.assert_raises(JMUAlgebraic_Exception, self.problem.set_constraints_usage, [True, False]) nose.tools.assert_raises(JMUAlgebraic_Exception, self.problem.set_constraints_usage, N.array([True, False])) nose.tools.assert_raises(JMUAlgebraic_Exception, self.problem.set_constraints_usage, False, 2) nose.tools.assert_raises(JMUAlgebraic_Exception, self.problem.set_constraints_usage, False, 'a') nose.tools.assert_raises(JMUAlgebraic_Exception, self.problem.set_constraints_usage, False, True) nose.tools.assert_raises(JMUAlgebraic_Exception, self.problem.set_constraints_usage, False, [5., 6.]) @testattr(assimulo=True) def test_guess_constraints(self): """ test if the guessing of constraints works """ self.problem_test_minmax.set_constraints_usage(True) constraints = self.problem_test_minmax.get_constraints() print constraints nose.tools.assert_equals(constraints[9], 0.0) nose.tools.assert_equals(constraints[10], -1.0) nose.tools.assert_equals(constraints[11], 1.0) nose.tools.assert_equals(constraints[12], -1.0) nose.tools.assert_equals(constraints[13], 1.0) nose.tools.assert_equals(constraints[14], -1.0) nose.tools.assert_equals(constraints[15], 1.0) nose.tools.assert_equals(constraints[16], 1.0) nose.tools.assert_equals(constraints[17], -1.0) @testattr(assimulo=True) def test_heuristic(self): """ test if heuristic works """ self.problem_test_minmax.set_constraints_usage(True) x0_1 = self.problem_test_minmax.get_x0() x0_2 = self.problem_test_minmax.get_heuristic_x0() x0_3 = self.problem_test_minmax.get_x0() """ vars = self.model_test_minmax._xmldoc.get_all_real_variables() rvars = [] for var in vars: cat = var.get_variable_category() type = var.get_fundamental_type() min = type.get_min() max = type.get_max() print min,max if cat == 0: print "Algebraic" elif cat == 1: print "State" elif cat ==6: print "Derivative" rvars.append((var.get_value_reference(),var.get_name())) xvars = self.model_test_minmax._xmldoc.get_x_variable_names() names = self.model_test_minmax.get_variable_names() x_min = self.model_test_minmax._xmldoc.get_x_min() x_max = self.model_test_minmax._xmldoc.get_x_max() print rvars print names print x_min print x_max for i in N.arange(18): #self.problem_test_minmax.print_var_info(i) print "1: ",x0_1[i], " 2: ",x0_2[i]," 3: ",x0_3[i] print names[i] """ nose.tools.assert_equals(x0_1[16], 0.0) nose.tools.assert_equals(x0_1[17], 0.0) nose.tools.assert_equals(x0_2[16], 1.0) nose.tools.assert_equals(x0_2[17], -1.0) nose.tools.assert_equals(x0_3[16], 0.0) nose.tools.assert_equals(x0_3[17], 0.0) @testattr(assimulo=True) def test_initialize(self): """ test if the initialize function works """ self.solver.set_jac_usage(True) self.solver.solve() dx = self.model.real_dx x = self.model.real_x w = self.model.real_w # Test equalities for pre, calced in zip(self.dx0, dx): nose.tools.assert_almost_equal(pre, calced, 6) for pre, calced in zip(self.x0, x): nose.tools.assert_almost_equal(pre, calced, 6) for pre, calced in zip(self.w0, w): nose.tools.assert_almost_equal(pre, calced, 6) @testattr(assimulo=True) def test_sparse_jac(self): """ Test if the sparse jacobian works """ # calculate jacobians at given 'point' input input = N.zeros(15) input[0:6] = self.dx0 input[6:12] = self.x0 input[12:15] = self.w0 jac_dense = self.problem.jac(input) jac_sparse = self.problem.sparse_jac(input) # check the format of the sparse jacoban type_name = type(jac_sparse).__name__ nose.tools.assert_equals(type_name, 'csc_matrix') jac_sparse = N.array(jac_sparse.todense()) """ print "dense: \n",jac_dense print "sparse: \n", jac_sparse.todense() """ # check if it is the same result as the dense jacobian i = 0 j = 0 for d_col, sp_col in zip(jac_dense, jac_sparse): for dense, sparse in zip(d_col, sp_col): nose.tools.assert_almost_equal(dense, sparse, 6) j += 1 j = 0 i += 1
class TestKInitSolve: """ Test evaluation of function in NLPInitialization and solution of initialization problems. """ @classmethod def setUpClass(cls): """Sets up the test class.""" # Compile the stationary initialization model into a JMU fpath1 = os.path.join(get_files_path(), 'Modelica', 'CSTRLib.mo') fpath2 = os.path.join(get_files_path(), 'Modelica', 'TestMinMax.mo') compile_jmu("CSTRLib.Components.Two_CSTRs_stat_init", fpath1) #compile_jmu("Tests.TestInvalidStart", fpath2) compile_jmu("TestMinMax.TestGuess", fpath2) def setUp(self): """Test setUp. Load the test model.""" # Load models self.model = JMUModel("CSTRLib_Components_Two_CSTRs_stat_init.jmu") self.problem = JMUAlgebraic(self.model) self.solver = KINSOL(self.problem) """ self.model_test_start = JMUModel(self.invalidStart) self.problem_test_start = JMUAlgebraic(self.model_test_start) """ self.model_test_minmax = JMUModel("TestMinMax_TestGuess.jmu") self.problem_test_minmax = JMUAlgebraic(self.model_test_minmax) # Set inputs for Stationary point A u1_0_A = 1 u2_0_A = 1 self.model.set('u1',u1_0_A) self.model.set('u2',u2_0_A) self.dx0 = N.zeros(6) self.x0 = N.array([200., 3.57359316e-02, 446.471014, 100., 1.79867213e-03,453.258466]) self.w0 = N.array([100., 100., -48.1909]) @testattr(stddist = True) def test_inits(self): """ test if solver is correctly initialized """ nose.tools.assert_true(self.solver._use_jac) nose.tools.assert_equals(self.problem._neqF0,15) nose.tools.assert_equals(self.problem._dx_size,6) nose.tools.assert_equals(self.problem._x_size,6) nose.tools.assert_equals(self.problem._w_size,3) nose.tools.assert_equals(self.problem._mark,12) @testattr(stddist = True) def test_jac_settings(self): """ test if user can set usage of jacobian """ self.solver.set_jac_usage(True) nose.tools.assert_true(self.solver._use_jac) self.solver.set_jac_usage(False) nose.tools.assert_false(self.solver._use_jac) self.solver.set_jac_usage(True) # test bad cases nose.tools.assert_raises(KINSOL_Exception,self.solver.set_jac_usage,2) nose.tools.assert_raises(KINSOL_Exception,self.solver.set_jac_usage,'a') nose.tools.assert_raises(KINSOL_Exception,self.solver.set_jac_usage,None) nose.tools.assert_raises(KINSOL_Exception,self.solver.set_jac_usage,[True,False]) nose.tools.assert_raises(KINSOL_Exception,self.solver.set_jac_usage,N.array([True,False])) @testattr(stddist = True) def test_constraint_settings(self): """ test if user can set usage of constraints """ const = N.ones(self.problem._neqF0) # test boolean settings self.problem.set_constraints_usage(True) nose.tools.assert_true(self.problem.use_constraints) self.problem.set_constraints_usage(False) nose.tools.assert_false(self.problem.use_constraints) # test if constraints can be set properly self.problem.set_constraints_usage(True,const) nose.tools.assert_true(self.problem.use_constraints) res1 = const == self.problem.constraints for r1 in res1: nose.tools.assert_true(r1) self.problem.set_constraints_usage(False,const) nose.tools.assert_false(self.problem.use_constraints) res1 = const == self.problem.constraints for r1 in res1: nose.tools.assert_true(r1) # test if constraints resets self.problem.set_constraints_usage(False) nose.tools.assert_true(self.problem.constraints == None) self.problem.set_constraints_usage(True,const) self.problem.set_constraints_usage(True) nose.tools.assert_true(self.problem.constraints != None) #test bad input nose.tools.assert_raises(JMUAlgebraic_Exception, self.problem.set_constraints_usage,2) nose.tools.assert_raises(JMUAlgebraic_Exception, self.problem.set_constraints_usage,'a') nose.tools.assert_raises(JMUAlgebraic_Exception, self.problem.set_constraints_usage,None) nose.tools.assert_raises(JMUAlgebraic_Exception, self.problem.set_constraints_usage,[True,False]) nose.tools.assert_raises(JMUAlgebraic_Exception, self.problem.set_constraints_usage,N.array([True,False])) nose.tools.assert_raises(JMUAlgebraic_Exception, self.problem.set_constraints_usage,False,2) nose.tools.assert_raises(JMUAlgebraic_Exception, self.problem.set_constraints_usage,False,'a') nose.tools.assert_raises(JMUAlgebraic_Exception, self.problem.set_constraints_usage,False,True) nose.tools.assert_raises(JMUAlgebraic_Exception, self.problem.set_constraints_usage,False,[5.,6.]) @testattr(stddist = True) def test_guess_constraints(self): """ test if the guessing of constraints works """ self.problem_test_minmax.set_constraints_usage(True) constraints = self.problem_test_minmax.get_constraints() print constraints nose.tools.assert_equals(constraints[9],0.0) nose.tools.assert_equals(constraints[10],-1.0) nose.tools.assert_equals(constraints[11],1.0) nose.tools.assert_equals(constraints[12],-1.0) nose.tools.assert_equals(constraints[13],1.0) nose.tools.assert_equals(constraints[14],-1.0) nose.tools.assert_equals(constraints[15],1.0) nose.tools.assert_equals(constraints[16],1.0) nose.tools.assert_equals(constraints[17],-1.0) @testattr(stddist = True) def test_heuristic(self): """ test if heuristic works """ self.problem_test_minmax.set_constraints_usage(True) x0_1 = self.problem_test_minmax.get_x0() x0_2 = self.problem_test_minmax.get_heuristic_x0() x0_3 = self.problem_test_minmax.get_x0() """ vars = self.model_test_minmax._xmldoc.get_all_real_variables() rvars = [] for var in vars: cat = var.get_variable_category() type = var.get_fundamental_type() min = type.get_min() max = type.get_max() print min,max if cat == 0: print "Algebraic" elif cat == 1: print "State" elif cat ==6: print "Derivative" rvars.append((var.get_value_reference(),var.get_name())) xvars = self.model_test_minmax._xmldoc.get_x_variable_names() names = self.model_test_minmax.get_variable_names() x_min = self.model_test_minmax._xmldoc.get_x_min() x_max = self.model_test_minmax._xmldoc.get_x_max() print rvars print names print x_min print x_max for i in N.arange(18): #self.problem_test_minmax.print_var_info(i) print "1: ",x0_1[i], " 2: ",x0_2[i]," 3: ",x0_3[i] print names[i] """ nose.tools.assert_equals(x0_1[16],0.0) nose.tools.assert_equals(x0_1[17],0.0) nose.tools.assert_equals(x0_2[16],1.0) nose.tools.assert_equals(x0_2[17],-1.0) nose.tools.assert_equals(x0_3[16],0.0) nose.tools.assert_equals(x0_3[17],0.0) @testattr(stddist = True) def test_initialize(self): """ test if the initialize function works """ self.solver.set_jac_usage(True) self.solver.solve() dx = self.model.real_dx x = self.model.real_x w = self.model.real_w # Test equalities for pre,calced in zip(self.dx0,dx): nose.tools.assert_almost_equal(pre,calced,6) for pre,calced in zip(self.x0,x): nose.tools.assert_almost_equal(pre,calced,6) for pre,calced in zip(self.w0,w): nose.tools.assert_almost_equal(pre,calced,6) @testattr(stddist = True) def test_sparse_jac(self): """ Test if the sparse jacobian works """ # calculate jacobians at given 'point' input input = N.zeros(15) input[0:6] = self.dx0 input[6:12] = self.x0 input[12:15] = self.w0 jac_dense = self.problem.jac(input) jac_sparse = self.problem.sparse_jac(input) # check the format of the sparse jacoban type_name = type(jac_sparse).__name__ nose.tools.assert_equals(type_name,'csc_matrix') jac_sparse = N.array(jac_sparse.todense()) """ print "dense: \n",jac_dense print "sparse: \n", jac_sparse.todense() """ # check if it is the same result as the dense jacobian i = 0 j = 0 for d_col, sp_col in zip(jac_dense,jac_sparse): for dense,sparse in zip(d_col,sp_col): nose.tools.assert_almost_equal(dense,sparse,6) j += 1 j = 0 i += 1