def test_overhead(self): print "Testing overhead..." #import cProfile #import StringIO #import pstats for num_timesteps in self.num_timesteps: for num_nodes in self.num_nodes: #pr = cProfile.Profile(timer=self.timer) #pr.enable() print "%s timesteps * %s nodes" % (num_timesteps, num_nodes) #Create a simulator object which will be run. s = Simulator() #Set the timesteps of the simulator. timesteps = range(num_timesteps) s.set_timesteps(timesteps) #Create the network with initial data n = Network(name="example network %s nodes %s timesteps" % (num_nodes, num_timesteps)) for node_num in range(num_nodes): n.add_node( DummyNode(x=node_num, y=node_num, name="Node number %s" % node_num)) #Set the simulator's network to this network. s.network = n #Create an instance of the deficit allocation engine. empty_engine = EmptyEngine(n) #add the engine to the simulator s.add_engine(empty_engine) t = time.time() #start the simulator s.start() self.result_matrix[num_timesteps][num_nodes] = (time.time() - t) #pr.disable() #s = StringIO.StringIO() #sortby = 'cumulative' #ps = pstats.Stats(pr, stream=s).sort_stats(sortby) #ps.print_stats() #with open('profile_%s_%s.txt' % (num_timesteps, num_nodes), 'w') as profile_result: # profile_result.write(s.getvalue()) overhead_result = open('overhead_result.json', 'w') overhead_result.write(json.dumps(self.result_matrix))
def test_overhead(self): print("Testing overhead...") #import cProfile #import StringIO #import pstats for num_timesteps in self.num_timesteps: for num_nodes in self.num_nodes: #pr = cProfile.Profile(timer=self.timer) #pr.enable() print("%s timesteps * %s nodes"%(num_timesteps, num_nodes)) #Create a simulator object which will be run. s = Simulator() #Set the timesteps of the simulator. timesteps = range(num_timesteps) s.set_timesteps(timesteps) #Create the network with initial data n = Network(name="example network %s nodes %s timesteps"%(num_nodes, num_timesteps)) for node_num in range(num_nodes): n.add_node(DummyNode(x=node_num, y=node_num, name="Node number %s"%node_num)) #Set the simulator's network to this network. s.network = n #Create an instance of the deficit allocation engine. empty_engine = EmptyEngine(n) #add the engine to the simulator s.add_engine(empty_engine) t = time.time() #start the simulator s.start() self.result_matrix[num_timesteps][num_nodes] = (time.time()-t) #pr.disable() #s = StringIO.StringIO() #sortby = 'cumulative' #ps = pstats.Stats(pr, stream=s).sort_stats(sortby) #ps.print_stats() #with open('profile_%s_%s.txt' % (num_timesteps, num_nodes), 'w') as profile_result: # profile_result.write(s.getvalue()) overhead_result = open('overhead_result.json', 'w') overhead_result.write(json.dumps(self.result_matrix))
from components import CitrusFarm, VegetableFarm, SurfaceReservoir, RiverSection from components import WaterDepartment, IrrigationDecisionMaker from engines import DeficitAllocation from pynsim import Simulator, Network #Create a simulator object which will be run. s = Simulator() #Set the timesteps of the simulator. timesteps = [ "2014-01-01", "2014-02-01", "2014-03-01", "2014-04-01", "2014-05-01" ] s.set_timesteps(timesteps) #Create the network with initial data n = Network(name="example network") #set some initial data on this network. n.incoming_water_qty = { "2014-01-01": 50, "2014-02-01": 61, "2014-03-01": 65, "2014-04-01": 45, "2014-05-01": 30 } #Add nodes to the network #All nodes have an x, y and name. The surface reservoir also has 'release' which #can be set on creation or after.
link3._max_flow = {0: 310.0, 1: 300.0, 2: 300.0, 3: 300.0, 4: 310.0, 5: 300.0} link4._max_flow = {0: 220.0, 1: 250.0, 2: 200.0, 3: 230.0, 4: 240.0, 5: 250.0} link5._max_flow = {0: 30.0, 1: 40.0, 2: 20.0, 3: 30.0, 4: 30.0, 5: 10.0} # Define network network = WaterResourcesSystem(name='Priority based') network.add_nodes(SR, IRR, URB, J1, In, Out) network.add_links(link1, link2, link3, link4, link5) # Run simulation simulation = Simulator() simulation.set_timesteps(timesteps) simulation.network = network engine = PriorityBased(network) simulation.add_engine(engine) simulation.start() import seaborn import matplotlib.pyplot as plt plt.figure(1) plt.subplot(2, 5, 1) plt.plot(simulation.network.nodes[0]._history['S'])
#Author: Majed Khadem, Silvia Padula, Khaled Mohamed, Stephen Knox, Julien Harou from components import UrbanNode, AgriculturalNode, SurfaceReservoir, Junction, RiverSection,CostMinimisationNetwork from engines import PyomoAllocation from pynsim import Simulator #create the simulator to run s = Simulator() timesteps = ["1", "2", "3", "4", "5", "6"] #add the timesteps to the simulator. The simulator may use these timesteps #to access timeseries data in the network. s.set_timesteps(timesteps) n = CostMinimisationNetwork(name="Cost Minimisation network") #Create the surface reservoir with a name, x and y coordinate. sr1 = SurfaceReservoir(x=1, y=2, name="SR1") #Add internal data to the reservoir. Internal data is signifiied by having an #underscore before the start of the variable name. #The surface reservoir type has an initial storage, inflow, min storage and max storage #so these are specified here. sr1._initial_storage =450 sr1._inflow = { "1" : 300, "2" : 320, "3" : 50, "4" : 40,
for idx in reservoirs.idx: if idx in W.keys(): n_YRB.add_link(W[idx]) if idx in C.keys(): n_YRB.add_link(C[idx]) if idx in U.keys(): n_YRB.add_link(U[idx]) n_YRB.add_institutions(Jordan_gvt, Israel_gvt, Syria_gvt) # Simulator object that will be run s = Simulator(network=n_YRB) # Timesteps of the simulator t = range(0, T) # months s.set_timesteps(t) # Engines reservoirs_management = SimulationOfSyria(target=Syria_gvt) treaty = TreatyOfPeace(target=Jordan_gvt) pumping = ExchangeWithTiberias(target=Israel_gvt) s.add_engine(reservoirs_management) # if test alpha & beta possibilities s.add_engine(treaty) s.add_engine(pumping) # Simulation s.start() # Create a Pandas Excel writer using XlsxWriter as the engine writer = pd.ExcelWriter('results/' + results_file, engine='xlsxwriter')
'oct': 100.0, 'nov': 100.0, 'dec': 100.0, } # Network network = ReservoirSystem(name="Allocation in a river system") network.add_nodes(J1,J2,J3,J4,J5,J6,J7,U1,F1,F2,F3,F4,E1) network.add_links(J1_J2,J2_J3,J2_J4,J3_U1,J3_F4,J4_J5,J5_J6,J6_J7,J7_E1,J5_F1,J6_F2,J7_F3 ) network.timestep = 86400 * 30 # one month # Simulation simulation = Simulator() simulation.set_timesteps(timesteps) simulation.network = network engine = SimpleRouting(network) simulation.add_engine(engine) simulation.start() for node in simulation.network.nodes: print("%s: %s"%(node.name, node._history['inflow'][0])) simulation.network.plot('inflow')
def run_simulation(diversion_pos, discharge_data, draw=False, queue=None): """Run the simulation based on a given position of diversion nodes. A fixed river network structure is assumed here. N1 | N2 N4 | | N3 N5 \ / N6 | N7 N9 | | N8 N10 | / N11 | N12 """ N1 = RiverNode(x=0, y=7, name='N1') N1.dQdx = copy.deepcopy(discharge_data['N1']) N2 = RiverNode(x=0, y=6, name='N2') N2.dQdx = copy.deepcopy(discharge_data['N2']) N3 = RiverNode(x=0, y=5, name='N3') N3.dQdx = copy.deepcopy(discharge_data['N3']) N4 = RiverNode(x=2, y=6, name='N4') N4.dQdx = copy.deepcopy(discharge_data['N4']) N5 = RiverNode(x=2, y=5, name='N5') N5.dQdx = copy.deepcopy(discharge_data['N5']) N6 = RiverNode(x=1, y=4, name='N6') N6.dQdx = copy.deepcopy(discharge_data['N6']) N7 = RiverNode(x=1, y=3, name='N7') N7.dQdx = copy.deepcopy(discharge_data['N7']) N8 = RiverNode(x=1, y=2, name='N8') N8.dQdx = copy.deepcopy(discharge_data['N8']) N9 = RiverNode(x=3, y=3, name='N9') N9.dQdx = copy.deepcopy(discharge_data['N9']) N10 = RiverNode(x=3, y=2, name='N10') N10.dQdx = copy.deepcopy(discharge_data['N10']) N11 = RiverNode(x=2, y=1, name='N11') N11.dQdx = copy.deepcopy(discharge_data['N11']) N12 = RiverNode(x=2, y=0, name='N12') N12.dQdx = copy.deepcopy(discharge_data['N12']) node_dict = { 0: N1, 1: N2, 2: N3, 3: N4, 4: N5, 5: N6, 6: N7, 7: N8, 8: N9, 9: N10, 10: N11, 11: N12 } # Add a diversion node based on the position parameter div_node = Diversion(x=node_dict[diversion_pos].x, y=node_dict[diversion_pos].y, name='Diversion') div_node.dQdx = node_dict[diversion_pos].dQdx div_node.demand = 0.5 node_dict[diversion_pos] = div_node L1 = GenericLink(start_node=node_dict[0], end_node=node_dict[1], name='L1') L2 = GenericLink(start_node=node_dict[1], end_node=node_dict[2], name='L2') L3 = GenericLink(start_node=node_dict[2], end_node=node_dict[5], name='L3') L4 = GenericLink(start_node=node_dict[3], end_node=node_dict[4], name='L4') L5 = GenericLink(start_node=node_dict[4], end_node=node_dict[5], name='L5') L6 = GenericLink(start_node=node_dict[5], end_node=node_dict[6], name='L6') L7 = GenericLink(start_node=node_dict[6], end_node=node_dict[7], name='L7') L8 = GenericLink(start_node=node_dict[7], end_node=node_dict[10], name='L8') L9 = GenericLink(start_node=node_dict[8], end_node=node_dict[9], name='L9') L10 = GenericLink(start_node=node_dict[9], end_node=node_dict[10], name='L10') L11 = GenericLink(start_node=node_dict[10], end_node=node_dict[11], name='L11') river_network = RiverNetwork(name='River network with diversion') river_network.add_nodes(*node_dict.values()) river_network.add_links(L1, L2, L3, L4, L5, L6, L7, L8, L9, L10, L11) if draw: river_network.draw(block=False) routing = Routing(river_network) simulation = Simulator() simulation.network = river_network simulation.add_engine(routing) simulation.set_timesteps([1]) simulation.start() if queue is None: return simulation.network.discharge else: queue.put(simulation.network.discharge)