class SimulatedCogenerationUnitMethodUpdateParametersTest(unittest.TestCase): def setUp(self): self.env = BaseEnvironment() self.heat_storage = Mock(spec=SimulatedHeatStorage) self.power_meter = SimulatedPowerMeter(0, self.env) self.cu = SimulatedCogenerationUnit(1, self.env) self.cu.heat_storage = self.heat_storage self.cu.power_meter = self.power_meter self.cu.config['minimal_workload'] = 0.20 self.cu.off_time = self.env.now - 1 self.gas_input = 20.0 self.cu.config['max_gas_input'] = self.gas_input self.electrical_efficiency = 0.25 self.cu.config['electrical_efficiency'] = self.electrical_efficiency self.thermal_efficiency = 0.7 self.cu.config['thermal_efficiency'] = self.thermal_efficiency self.total_hours_of_operation = 1 self.cu.total_hours_of_operation = self.total_hours_of_operation def test_normal_workload(self): '''The given workload shouldn't be altered if the workload is valid. current gas consumption, current electrical production, current thermal production and total_hours_of_operation should be set. ''' precalculated_workload = 0.5 self.cu.set_workload(precalculated_workload) self.cu.consume_and_produce_energy() expected_current_gas_consumption = self.gas_input * \ precalculated_workload complete_current_power = expected_current_gas_consumption * \ self.cu.get_efficiency_loss_factor() expected_current_electrical_production = self.electrical_efficiency * \ complete_current_power expected_current_thermal_production = self.thermal_efficiency * \ complete_current_power # assumption: self.env.step_size are seconds per step self.total_hours_of_operation += self.env.step_size/(60.0*60.0) self.assertEqual(self.cu._workload, precalculated_workload, "new workload is wrong. " + values_comparison(self.cu._workload, precalculated_workload)) self.assertEqual(self.cu.current_gas_consumption, expected_current_gas_consumption, "current_gas_consumption is wrong. " + values_comparison(self.cu.current_gas_consumption, expected_current_gas_consumption)) self.assertAlmostEqual(self.cu.current_electrical_production, expected_current_electrical_production) self.assertAlmostEqual(self.cu.current_thermal_production, expected_current_thermal_production) self.assertEqual(self.cu.total_hours_of_operation, self.total_hours_of_operation, "total_hours_of_operation is wrong. " + values_comparison(self.cu.total_hours_of_operation, self.total_hours_of_operation)) def test_power_on_count(self): ''' the cu should increment the power on count if the cu was turned off and is turned on again''' precalculated_workload = 0.5 self.cu._workload = 0 # means the cu was turned off power_on_count = 0 self.cu.power_on_count = power_on_count self.cu.set_workload(precalculated_workload) self.assertEqual(self.cu.power_on_count, power_on_count + 1) def test_power_on_count_unaffected(self): ''' the cu musn't increment the power on count if the cu was turned on''' precalculated_workload = 0.35 self.cu._workload = 25 # means the cu was turned on power_on_count = 0 self.cu.power_on_count = power_on_count self.cu.set_workload(precalculated_workload) self.assertEqual(self.cu.power_on_count, power_on_count) def test_too_low_workload(self): ''' The workload should be zero if the given workload is less than the minimal workload.''' precalculated_workload = 0.10 old_hours = self.total_hours_of_operation self.cu.set_workload(precalculated_workload) self.assertEqual(self.cu._workload, 0, "workload is wrong. " + values_comparison(self.cu._workload, 0)) self.assertEqual(self.cu.current_gas_consumption, 0, "current_gas_consumption is wrong. " + values_comparison(self.cu.current_gas_consumption, 0)) self.assertEqual(self.cu.current_electrical_production, 0, "current_electrical_production is wrong. " + values_comparison(self.cu.current_electrical_production, 0)) self.assertEqual(self.cu.current_thermal_production, 0, "current_thermal_production is wrong. " + values_comparison(self.cu.current_thermal_production, 0)) self.assertEqual(self.cu.total_hours_of_operation, old_hours, "total_hours_of_operation is wrong. " + values_comparison(self.cu.total_hours_of_operation, old_hours)) def test_too_high_workload(self): '''If the workload is greater than 1 it should be truncated to 1.''' precalculated_workload = 2.0 self.cu.set_workload(precalculated_workload) self.cu.consume_and_produce_energy() expected_current_gas_consumption = self.gas_input complete_current_power = expected_current_gas_consumption * \ self.cu.get_efficiency_loss_factor() expected_current_electrical_production = self.electrical_efficiency * \ complete_current_power expected_current_thermal_production = self.thermal_efficiency * \ complete_current_power # assumption: self.env.step_size are seconds per step self.total_hours_of_operation += self.env.step_size/(60.0*60.0) self.assertEqual(self.cu._workload, 1, "wrong workload. " + values_comparison(self.cu._workload, 1)) self.assertEqual(self.cu.current_gas_consumption, expected_current_gas_consumption, "wrong current_gas_consumption. " + values_comparison(self.cu.current_gas_consumption, expected_current_gas_consumption)) self.assertEqual(self.cu.current_electrical_production, expected_current_electrical_production, "wrong expected_current_electrical_production. " + values_comparison(self.cu.current_electrical_production, expected_current_electrical_production)) self.assertEqual(self.cu.current_thermal_production, expected_current_thermal_production, "wrong current_thermal_production. " + values_comparison(self.cu.current_thermal_production, expected_current_thermal_production)) self.assertEqual(self.cu.total_hours_of_operation, self.total_hours_of_operation, "wrong total_hours_of_operation. " + values_comparison(self.cu.total_hours_of_operation, self.total_hours_of_operation)) def test_workload_off_time_effective(self): '''If the offtime is in the future the bhkw must stay turned off.''' self.cu.off_time = self.env.now + 1 old_hours = self.total_hours_of_operation self.cu.set_workload(self.cu.calculate_new_workload()) self.assertEqual(self.cu._workload, 0, "workload is wrong. " + values_comparison(self.cu._workload, 0)) self.assertEqual(self.cu.current_gas_consumption, 0, "current_gas_consumption is wrong. " + values_comparison(self.cu.current_gas_consumption, 0)) self.assertEqual(self.cu.current_electrical_production, 0, "current_electrical_production is wrong." + values_comparison(self.cu.current_electrical_production, 0)) self.assertEqual(self.cu.current_thermal_production, 0, "current_thermal_production is wrong . " + values_comparison(self.cu.current_thermal_production, 0)) self.assertEqual(self.cu.total_hours_of_operation, old_hours, "total_hours_of_operation is wrong. " + values_comparison(self.cu.total_hours_of_operation, old_hours)) def test_turn_bhkw_off(self): '''If the cu is turned off, there must be an offtime which determines the time the cu stays turned off ''' self.cu._workload = 0.30 new_workload = 0 self.cu.thermal_driven = True required_energy = 0.0 self.heat_storage.get_required_energy.return_value = required_energy off_time = self.env.now self.cu.off_time = off_time self.cu.set_workload(new_workload) self.assertGreater(self.cu.off_time, off_time) def test_consume_and_produce_energy(self): '''increases total_gas_consumtion, total_thermal_production and total_electrical_production all are measured in energy''' self.cu.total_gas_consumption = 0 self.cu.total_thermal_production = 0 self.cu.total_electrical_production = 0 self.cu._workload = 1 self.cu.consume_and_produce_energy() self.assertGreater(self.cu.total_gas_consumption,0, "wrong total_gas_consumption. " + values_comparison(self.cu.total_gas_consumption,0)) self.assertGreater(self.cu.total_thermal_production,0, "wrong total_thermal_production " + values_comparison(self.cu.total_thermal_production,0)) self.assertGreater(self.cu.total_electrical_production,0, "wrong total_electrical_production" + values_comparison(self.cu.total_electrical_production,0))
class SimulatedCogenerationUnitMethodUpdateParametersTest(unittest.TestCase): def setUp(self): self.env = BaseEnvironment() self.heat_storage = Mock(spec=SimulatedHeatStorage) self.power_meter = SimulatedPowerMeter(0, self.env) self.cu = SimulatedCogenerationUnit(1, self.env) self.cu.heat_storage = self.heat_storage self.cu.power_meter = self.power_meter self.cu.config['minimal_workload'] = 0.20 self.cu.off_time = self.env.now - 1 self.gas_input = 20.0 self.cu.config['max_gas_input'] = self.gas_input self.electrical_efficiency = 0.25 self.cu.config['electrical_efficiency'] = self.electrical_efficiency self.thermal_efficiency = 0.7 self.cu.config['thermal_efficiency'] = self.thermal_efficiency self.total_hours_of_operation = 1 self.cu.total_hours_of_operation = self.total_hours_of_operation def test_normal_workload(self): '''The given workload shouldn't be altered if the workload is valid. current gas consumption, current electrical production, current thermal production and total_hours_of_operation should be set. ''' precalculated_workload = 0.5 self.cu.set_workload(precalculated_workload) self.cu.consume_and_produce_energy() expected_current_gas_consumption = self.gas_input * \ precalculated_workload complete_current_power = expected_current_gas_consumption * \ self.cu.get_efficiency_loss_factor() expected_current_electrical_production = self.electrical_efficiency * \ complete_current_power expected_current_thermal_production = self.thermal_efficiency * \ complete_current_power # assumption: self.env.step_size are seconds per step self.total_hours_of_operation += self.env.step_size / (60.0 * 60.0) self.assertEqual( self.cu._workload, precalculated_workload, "new workload is wrong. " + values_comparison(self.cu._workload, precalculated_workload)) self.assertEqual( self.cu.current_gas_consumption, expected_current_gas_consumption, "current_gas_consumption is wrong. " + values_comparison(self.cu.current_gas_consumption, expected_current_gas_consumption)) self.assertAlmostEqual(self.cu.current_electrical_production, expected_current_electrical_production) self.assertAlmostEqual(self.cu.current_thermal_production, expected_current_thermal_production) self.assertEqual( self.cu.total_hours_of_operation, self.total_hours_of_operation, "total_hours_of_operation is wrong. " + values_comparison(self.cu.total_hours_of_operation, self.total_hours_of_operation)) def test_power_on_count(self): ''' the cu should increment the power on count if the cu was turned off and is turned on again''' precalculated_workload = 0.5 self.cu._workload = 0 # means the cu was turned off power_on_count = 0 self.cu.power_on_count = power_on_count self.cu.set_workload(precalculated_workload) self.assertEqual(self.cu.power_on_count, power_on_count + 1) def test_power_on_count_unaffected(self): ''' the cu musn't increment the power on count if the cu was turned on''' precalculated_workload = 0.35 self.cu._workload = 25 # means the cu was turned on power_on_count = 0 self.cu.power_on_count = power_on_count self.cu.set_workload(precalculated_workload) self.assertEqual(self.cu.power_on_count, power_on_count) def test_too_low_workload(self): ''' The workload should be zero if the given workload is less than the minimal workload.''' precalculated_workload = 0.10 old_hours = self.total_hours_of_operation self.cu.set_workload(precalculated_workload) self.assertEqual( self.cu._workload, 0, "workload is wrong. " + values_comparison(self.cu._workload, 0)) self.assertEqual( self.cu.current_gas_consumption, 0, "current_gas_consumption is wrong. " + values_comparison(self.cu.current_gas_consumption, 0)) self.assertEqual( self.cu.current_electrical_production, 0, "current_electrical_production is wrong. " + values_comparison(self.cu.current_electrical_production, 0)) self.assertEqual( self.cu.current_thermal_production, 0, "current_thermal_production is wrong. " + values_comparison(self.cu.current_thermal_production, 0)) self.assertEqual( self.cu.total_hours_of_operation, old_hours, "total_hours_of_operation is wrong. " + values_comparison(self.cu.total_hours_of_operation, old_hours)) def test_too_high_workload(self): '''If the workload is greater than 1 it should be truncated to 1.''' precalculated_workload = 2.0 self.cu.set_workload(precalculated_workload) self.cu.consume_and_produce_energy() expected_current_gas_consumption = self.gas_input complete_current_power = expected_current_gas_consumption * \ self.cu.get_efficiency_loss_factor() expected_current_electrical_production = self.electrical_efficiency * \ complete_current_power expected_current_thermal_production = self.thermal_efficiency * \ complete_current_power # assumption: self.env.step_size are seconds per step self.total_hours_of_operation += self.env.step_size / (60.0 * 60.0) self.assertEqual( self.cu._workload, 1, "wrong workload. " + values_comparison(self.cu._workload, 1)) self.assertEqual( self.cu.current_gas_consumption, expected_current_gas_consumption, "wrong current_gas_consumption. " + values_comparison(self.cu.current_gas_consumption, expected_current_gas_consumption)) self.assertEqual( self.cu.current_electrical_production, expected_current_electrical_production, "wrong expected_current_electrical_production. " + values_comparison(self.cu.current_electrical_production, expected_current_electrical_production)) self.assertEqual( self.cu.current_thermal_production, expected_current_thermal_production, "wrong current_thermal_production. " + values_comparison(self.cu.current_thermal_production, expected_current_thermal_production)) self.assertEqual( self.cu.total_hours_of_operation, self.total_hours_of_operation, "wrong total_hours_of_operation. " + values_comparison(self.cu.total_hours_of_operation, self.total_hours_of_operation)) def test_workload_off_time_effective(self): '''If the offtime is in the future the bhkw must stay turned off.''' self.cu.off_time = self.env.now + 1 old_hours = self.total_hours_of_operation self.cu.set_workload(self.cu.calculate_new_workload()) self.assertEqual( self.cu._workload, 0, "workload is wrong. " + values_comparison(self.cu._workload, 0)) self.assertEqual( self.cu.current_gas_consumption, 0, "current_gas_consumption is wrong. " + values_comparison(self.cu.current_gas_consumption, 0)) self.assertEqual( self.cu.current_electrical_production, 0, "current_electrical_production is wrong." + values_comparison(self.cu.current_electrical_production, 0)) self.assertEqual( self.cu.current_thermal_production, 0, "current_thermal_production is wrong . " + values_comparison(self.cu.current_thermal_production, 0)) self.assertEqual( self.cu.total_hours_of_operation, old_hours, "total_hours_of_operation is wrong. " + values_comparison(self.cu.total_hours_of_operation, old_hours)) def test_turn_bhkw_off(self): '''If the cu is turned off, there must be an offtime which determines the time the cu stays turned off ''' self.cu._workload = 0.30 new_workload = 0 self.cu.thermal_driven = True required_energy = 0.0 self.heat_storage.get_required_energy.return_value = required_energy off_time = self.env.now self.cu.off_time = off_time self.cu.set_workload(new_workload) self.assertGreater(self.cu.off_time, off_time) def test_consume_and_produce_energy(self): '''increases total_gas_consumtion, total_thermal_production and total_electrical_production all are measured in energy''' self.cu.total_gas_consumption = 0 self.cu.total_thermal_production = 0 self.cu.total_electrical_production = 0 self.cu._workload = 1 self.cu.consume_and_produce_energy() self.assertGreater( self.cu.total_gas_consumption, 0, "wrong total_gas_consumption. " + values_comparison(self.cu.total_gas_consumption, 0)) self.assertGreater( self.cu.total_thermal_production, 0, "wrong total_thermal_production " + values_comparison(self.cu.total_thermal_production, 0)) self.assertGreater( self.cu.total_electrical_production, 0, "wrong total_electrical_production" + values_comparison(self.cu.total_electrical_production, 0))