def setUp(self): self.sup = Supervisor() self.gc1 = GridController("gc1", self.sup) self.gc2 = GridController("gc2", self.sup) self.sup.register_device(self.gc1) self.sup.register_device(self.gc2) self.gc1._connected_devices["gc2"] = self.gc2
class TestPricingModel(unittest.TestCase): def setUp(self): self.sup = Supervisor() batt = Battery("batt_1", price_logic="hourly_preference", capacity=5000.0, max_charge_rate=2000.0, max_discharge_rate=2000.0) self.gc1 = GridController(device_id="gc_1", supervisor=self.sup, battery=batt, price_logic='weighted_average') self.gc2 = GridController(device_id="gc_2", supervisor=self.sup, battery=batt, price_logic='weighted_average') self.sup.register_device(self.gc1) self.sup.register_device(self.gc2) self.gc1._connected_devices["gc_2"] = self.gc2 self.gc2._connected_devices["gc_1"] = self.gc1 @unittest.skip("Exception: Device ID not found (gc_2)") def test_weighted_price(self): self.gc1.send_power_message("gc_2", -200) # sending it out self.gc1.send_price_message("gc_2", 0.2) self.gc2.process_events() self.gc2.update_time(3000) self.gc2.modulate_price() print("current interval prices: {}".format(", ".join( map(str, self.gc2._price_logic.get_interval_prices()))))
def setUp(self): self.sup = Supervisor() self.gc1 = GridController("gc1", self.sup) self.gc2 = GridController("gc2", self.sup, [self.gc1]) self.sup.register_device(self.gc1) self.sup.register_device(self.gc2) self.gc1.register_device(self.gc2, self.gc2.get_id(), 1)
class TestDeviceRegister(unittest.TestCase): def setUp(self): self.sup = Supervisor() self.gc1 = GridController("gc1", self.sup) self.gc2 = GridController("gc2", self.sup, [self.gc1]) self.sup.register_device(self.gc1) self.sup.register_device(self.gc2) self.gc1.register_device(self.gc2, self.gc2.get_id(), 1) def test_connections(self): self.assertEqual(self.gc1._connected_devices["gc2"], self.gc2) self.assertEqual(self.gc2._connected_devices["gc1"], self.gc1)
def setUp(self): self.sup = Supervisor() batt = Battery("batt_1", price_logic="hourly_preference", capacity=5000.0, max_charge_rate=2000.0, max_discharge_rate=2000.0) self.gc1 = GridController(device_id="gc_1", supervisor=self.sup, battery=batt, price_logic='weighted_average') self.gc2 = GridController(device_id="gc_2", supervisor=self.sup, battery=batt, price_logic='weighted_average') self.sup.register_device(self.gc1) self.sup.register_device(self.gc2) self.gc1._connected_devices["gc_2"] = self.gc2 self.gc2._connected_devices["gc_1"] = self.gc1
def test_simple_interation_with_grid_controller(self): supervisor = Supervisor() notebook_personal_computer_device_id = "notebook_personal_computer_1" max_operating_power = 12 * 5 notebook_personal_computer = NotebookPersonalComputer(notebook_personal_computer_device_id, supervisor, max_operating_power = max_operating_power) battery_id = "battery_1" battery_price_logic = 'moving_average' # According to the constructor of Battery class, 'hourly_preference' or 'moving_average' capacity = 1200.0 # in [Wh] though it is defined in [Ah] in related science and data sheet max_charge_rate = 120 # in [W], assuming it's 12[V] battery with 100 [Ah] capacity, rouchly lasting 10 hours, 10[A] maximum. # Note: Charge rate varies depending on state of charge. max_discharge_rate = 120 # in [W], same value as above, simplifying for now. battery = Battery(battery_id, battery_price_logic, capacity, max_charge_rate, max_discharge_rate) grid_controller_device_id = "grid_controller_1" grid_controller_price_logic = 'marginal_price' # According to the constructor of GridController, # 'weighted_average', 'marginal_price', 'marginal_price_b', or 'static_price' grid_controller = GridController(grid_controller_device_id, supervisor, battery = battery, price_logic = grid_controller_price_logic, connected_devices = [notebook_personal_computer]) supervisor.register_device(notebook_personal_computer) supervisor.register_device(grid_controller) grid_controller.send_register_message("notebook_personal_computer_1", 1) # grid_controller.build_device_list() grid_controller.send_power_message("notebook_personal_computer_1", 10)
class TestEventModel(unittest.TestCase): def setUp(self): self.sup = Supervisor() self.gc1 = GridController("gc1", self.sup) self.gc2 = GridController("gc2", self.sup) self.sup.register_device(self.gc1) self.sup.register_device(self.gc2) def test_event_queue(self): pq = PriorityQueue() pq.add(Event(self.gc1.set_power_in, 20), 1) pq.add(Event(self.gc1.set_power_in, 20), 2) # to make sure they are treated as distinct entities. event1, time_stamp = pq.pop() event1.run_event() self.assertEqual(self.gc1._power_in, 20) event2, time_stamp = pq.pop() event2.run_event() self.assertEqual(self.gc1._power_in, 20) def test_single_argument_event(self): self.gc1.add_event(Event(self.gc1.set_power_in, 20), 1) self.gc1.update_time(1) self.gc1.process_events() self.assertEqual(self.gc1._power_in, 20) def test_multiple_argument_event(self): self.gc1.add_event(Event(self.gc1.on_allocated, "gc2", 20), 1) self.gc1.update_time(1) self.gc1.process_events() self.assertEqual(self.gc1._allocated["gc2"], 20) def test_no_argument_event(self): self.gc1.add_event(Event(self.gc1.add_power_in), 1) self.gc1.update_time(1) self.gc1.process_events() self.assertEqual(self.gc1._power_in, 10) """More Tests Here are Highest Priority"""
def run_simulation(config_file, override_args): sim = SimulationSetup(supervisor=Supervisor()) sim.setup_simulation(config_file, override_args) while sim.supervisor.has_next_event(): device_id, time_stamp = sim.supervisor.peek_next_event() if time_stamp > sim.end_time: # Reached end of simulation. Stop processing events break sim.supervisor.occur_next_event() sim.supervisor.finish_all(sim.end_time)
class TestGCMessaging(unittest.TestCase): def setUp(self): self.sup = Supervisor() self.gc1 = GridController("gc1", self.sup) self.gc2 = GridController("gc2", self.sup) self.sup.register_device(self.gc1) self.sup.register_device(self.gc2) self.gc1._connected_devices["gc2"] = self.gc2 def test_GC_register_message(self): self.gc1.send_register_message("gc2", 1) while self.sup.has_next_event(): self.sup.occur_next_event() self.assertEqual(self.gc2._connected_devices["gc1"], self.gc1) def test_GC_power_message_unfulfilled(self): # no battery. Can't provide, lists every as request. self.gc2._connected_devices["gc1"] = self.gc1 self.gc1.send_power_message("gc2", 10) while self.sup.has_next_event(): self.sup.occur_next_event() self.assertEqual(self.gc2._requested["gc1"], -10) def test_balance_power_below_max_battery_rate(self): self.gc2._connected_devices["gc1"] = self.gc1 batt1 = Battery(None) # no price logic batt2 = Battery(None) # no price logic. self.gc1._battery = batt1 self.gc2._battery = batt2 self.gc1.send_power_message("gc2", 70) while self.sup.has_next_event(): self.sup.occur_next_event() self.assertEqual(self.gc2._loads["gc1"], -70) self.assertEqual(self.gc2._battery.get_load(), -70) def test_balance_power_above_max_battery_rate(self): self.gc1.send_register_message("gc2", 1) batt1 = Battery(None) # no price logic batt2 = Battery(None) # no price logic. self.gc1._battery = batt1 self.gc2._battery = batt2 self.gc1.send_power_message("gc2", 110) while self.sup.has_next_event(): self.sup.occur_next_event() self.assertEqual(self.gc2._loads["gc1"], -100) # 100 is default max charge rate. self.assertEqual(self.gc2._battery.get_load(), -100) self.assertEqual(self.gc2._requested["gc1"], -10) # 10 remaining to be sold to ya. """
def setUp(self): self._logger = logging.getLogger("test") self.device_id = 1 # Note: Assuming it is an integer not UUID. self.supervisor = Supervisor() self.notebook_personal_computer = NotebookPersonalComputer( self.device_id, self.supervisor)