def test_solve_setpoint_unsteady(self): controller = self.make_controller() time = controller.time t0 = time.first() setpoint = [ (controller.mod.flow_in[t0], 3.0), (controller.mod.conc[t0, 'A'], 5.0), ] weights = [ (controller.mod.flow_in[t0], 1.0), (controller.mod.conc[t0, 'A'], 1.0), ] controller.add_setpoint_objective(setpoint, weights) controller.mod.flow_in[:].set_value(3.0) initialize_t0(controller.mod) dof_prior = degrees_of_freedom(controller) controller.solve_setpoint(solver, require_steady=False) dof_post = degrees_of_freedom(controller) assert dof_prior == dof_post assert controller.differential_vars[0].setpoint == \ pytest.approx(5.0, abs=1e-3) assert controller.differential_vars[1].setpoint == \ pytest.approx(7.11, abs=1e-3) assert controller.algebraic_vars[0].setpoint == \ pytest.approx(3.0, abs=1e-3) assert controller.algebraic_vars[1].setpoint == \ pytest.approx(-5.0, abs=1e-3) assert controller.algebraic_vars[2].setpoint == \ pytest.approx(5.0, abs=1e-3) assert controller.input_vars[0].setpoint == \ pytest.approx(3.0, abs=1e-3)
def test_init_samples_by_element(self): blk = self.make_block() time = blk.time blk.mod.flow_in[:].set_value(3.0) initialize_t0(blk.mod) copy_values_forward(blk.mod) blk.mod.flow_in[:].set_value(2.0) blk.set_sample_time(0.5) blk.initialize_samples_by_element((1,2), solver) t0 = time.first() tl = time.last() vectors = blk.vectors assert vectors.input[0,tl].value == 2.0 assert vectors.differential[0,tl].value == pytest.approx(3.185595567867036) assert vectors.differential[1,tl].value == pytest.approx(1.1532474073395755) assert vectors.derivative[0,tl].value == pytest.approx(0.44321329639889284) assert vectors.derivative[1,tl].value == pytest.approx(0.8791007531878847) blk.vectors.input.set_setpoint(3.0) blk.initialize_samples_by_element(1, solver, input_option=InputOption.SETPOINT) blk.initialize_samples_by_element(2, solver, input_option=InputOption.SETPOINT) for t in time: if t == t0: assert vectors.input[0,t].value == 2.0 else: assert vectors.input[0,t].value == 3.0 assert vectors.differential[0,tl].value == pytest.approx(3.7037037037037037) assert vectors.differential[1,tl].value == pytest.approx(1.0746896480968502) assert vectors.derivative[0,tl].value == pytest.approx(0.1851851851851849) assert vectors.derivative[1,tl].value == pytest.approx(0.47963475941315314)
def test_initialize_only_measurement_input(self): model = make_model(horizon=1, nfe=2) time = model.time t0 = time.first() inputs = [model.flow_in] measurements = [ pyo.Reference(model.conc[:, 'A']), pyo.Reference(model.conc[:, 'B']), ] category_dict = { VC.INPUT: inputs, VC.MEASUREMENT: measurements, } db = DynamicBlock( model=model, time=time, category_dict={None: category_dict}, ) db.construct() db.set_sample_time(0.5) db.mod.flow_in[:].set_value(3.0) initialize_t0(db.mod) copy_values_forward(db.mod) db.mod.flow_in[:].set_value(2.0) # Don't need to know any of the special categories to initialize # by element. This is only because we have an implicit discretization. db.initialize_by_solving_elements(solver) t0 = time.first() tl = time.last() vectors = db.vectors assert vectors.input[0, tl].value == 2.0 assert vectors.measurement[0, tl].value == pytest.approx( 3.185595567867036) assert vectors.measurement[1, tl].value == pytest.approx( 1.1532474073395755) assert model.dcdt[tl, 'A'].value == pytest.approx(0.44321329639889284) assert model.dcdt[tl, 'B'].value == pytest.approx(0.8791007531878847)
def test_add_setpoint_objective(self): controller = self.make_controller() time = controller.time t0 = time.first() setpoint = [ (controller.mod.flow_in[t0], 3.0), ] weights = [ (controller.mod.flow_in[t0], 2.0), ] controller.mod.flow_in[:].set_value(3.0) initialize_t0(controller.mod) controller.add_setpoint_objective(setpoint, weights) assert hasattr(controller, 'setpoint_objective') pred_obj_expr = (2.0 * (controller.mod.flow_in[t0] - 3.0)**2) obj_expr = controller.setpoint_objective.expr assert pyo.value(pred_obj_expr) == pyo.value(obj_expr) assert pred_obj_expr.to_string() == obj_expr.to_string() controller.del_component(controller.setpoint_objective) setpoint = [ (controller.mod.flow_in[t0], 3.0), (controller.mod.conc[t0, 'A'], 1.5), ] weights = [ (controller.mod.flow_in[t0], 1.0), (controller.mod.conc[t0, 'A'], 5.0), ] controller.add_setpoint_objective(setpoint, weights) pred_obj_expr = (1.0 * (controller.mod.flow_in[t0] - 3.0)**2 + 5.0 * (controller.mod.conc[t0, 'A'] - 1.5)**2) obj_expr = controller.setpoint_objective.expr assert pyo.value(pred_obj_expr) == pyo.value(obj_expr) assert pred_obj_expr.to_string() == obj_expr.to_string()
def test_add_tracking_objective(self): controller = self.make_controller() time = controller.time t0 = time.first() setpoint = [ (controller.mod.flow_in[t0], 3.0), ] weights = [ (controller.mod.flow_in[t0], 1.0), ] controller.mod.flow_in[:].set_value(3.0) initialize_t0(controller.mod) copy_values_forward(controller.mod) controller.add_setpoint_objective(setpoint, weights) controller.solve_setpoint(solver) # Re-initialize inputs so they are not at the setpoint # for objective evaluation. controller.mod.flow_in[:].set_value(2.5) weights = [ (controller.mod.conc[t0, 'A'], 1), (controller.mod.conc[t0, 'B'], 1), (controller.mod.rate[t0, 'A'], 1), (controller.mod.rate[t0, 'B'], 1), (controller.mod.flow_out[t0], 1), (controller.mod.flow_in[t0], 1), ] # Construct predicted objective functions: pred_obj = {i: 0. for i in range(1, 4)} sample_points = controller.sample_points[1:] sample_time = controller.sample_time for v in controller.component_objects(DiffVar): for t in sample_points: pred_obj[1] += (v[t] - v.setpoint)**2 pred_obj[2] += (v[t] - v.setpoint)**2 pred_obj[3] += (v[t] - v.setpoint)**2 for v in controller.component_objects(AlgVar): for t in sample_points: pred_obj[3] += (v[t] - v.setpoint)**2 for v in controller.component_objects(InputVar): for t in sample_points: i_prev = time.find_nearest_index(t - sample_time, tolerance=1e-8) t_prev = time[i_prev] pred_obj[1] += (v[t] - v.setpoint)**2 pred_obj[2] += (v[t] - v[t_prev])**2 pred_obj[3] += (v[t] - v[t_prev])**2 controller.add_tracking_objective( weights, control_penalty_type=ControlPenaltyType.ERROR, state_ctypes=DiffVar, ) assert pyo.value(controller.tracking_objective.expr == pred_obj[1]) assert pyo.value(controller.tracking_objective.expr) > 0 controller.del_component(controller.tracking_objective) controller.add_tracking_objective( weights, control_penalty_type=ControlPenaltyType.ACTION, state_ctypes=DiffVar, ) assert pyo.value(controller.tracking_objective.expr == pred_obj[2]) assert pyo.value(controller.tracking_objective.expr) > 0 controller.del_component(controller.tracking_objective) controller.add_tracking_objective( weights, control_penalty_type=ControlPenaltyType.ACTION, state_ctypes=(DiffVar, AlgVar), ) assert pyo.value(controller.tracking_objective.expr == pred_obj[3]) assert pyo.value(controller.tracking_objective.expr) > 0