Exemplo n.º 1
0
    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))
Exemplo n.º 2
0
    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))
Exemplo n.º 3
0
    def test_engine_error(self):
        """ Test an engine can raises a non-StopIteration error.
        """
        s = Simulator(max_iterations=5)
        s.network = Network("Iteration test network")

        s.add_engine(TypeErrorEngine(None, error_on_iteration=1))
        s.add_engine(CountEngine(None))
        s.timesteps = [0, ]

        with self.assertRaises(TypeError):
            s.start()
Exemplo n.º 4
0
    def test_engine_iteration(self):
        """ Test setting the global number of iterations in a simulator. """
        s = Simulator(max_iterations=5)
        s.network = Network("Iteration test network")
        engine = CountEngine(None)
        s.add_engine(engine)

        s.timesteps = [0, ]
        s.start()

        assert engine.iteration == 5
        assert engine.run_count == 5
Exemplo n.º 5
0
    def test_default_iteration(self):
        """ Test that the default simulation `max_iterations` is one. """
        s = Simulator()
        s.network = Network("Iteration test network")
        engine = CountEngine(None)
        s.add_engine(engine)

        s.timesteps = [0, ]
        s.start()

        assert engine.iteration == 1
        assert engine.run_count == 1
Exemplo n.º 6
0
    def test_engine_iteration(self):
        """ Test setting the global number of iterations in a simulator. """
        s = Simulator(max_iterations=5)
        s.network = Network("Iteration test network")
        engine = CountEngine(None)
        s.add_engine(engine)

        s.timesteps = [
            0,
        ]
        s.start()

        assert engine.iteration == 5
        assert engine.run_count == 5
Exemplo n.º 7
0
    def test_default_iteration(self):
        """ Test that the default simulation `max_iterations` is one. """
        s = Simulator()
        s.network = Network("Iteration test network")
        engine = CountEngine(None)
        s.add_engine(engine)

        s.timesteps = [
            0,
        ]
        s.start()

        assert engine.iteration == 1
        assert engine.run_count == 1
Exemplo n.º 8
0
    def test_engine_error(self):
        """ Test an engine can raises a non-StopIteration error.
        """
        s = Simulator(max_iterations=5)
        s.network = Network("Iteration test network")

        s.add_engine(TypeErrorEngine(None, error_on_iteration=1))
        s.add_engine(CountEngine(None))
        s.timesteps = [
            0,
        ]

        with self.assertRaises(TypeError):
            s.start()
Exemplo n.º 9
0
    def test_dict_overwrite(self):
        """
            Test to ensure that dictionaries and other objects contained in history
            are not overwritten by maintaining references to the same objects.
        """
        print("Testing...")
        network = Network("History Test Network")
        network.add_node(HistoryTestNode(x=0, y=0, name="Test Node"))

        s = Simulator()
        s.network = network
        s.timesteps = ['a', 'b', 'c']


        s.start()
        
        assert s.network.nodes[0]._history['property_dict'] == [{'test': 'a'}, {'test': 'b'}, {'test': 'c'}]
Exemplo n.º 10
0
    def test_engine_stopping_iteration(self):
        """ Test an engine terminating iteration

        The engine must raise a `StopIteration` exception.
        """
        s = Simulator(max_iterations=5)
        s.network = Network("Iteration test network")

        stopper_engine = StopperEngine(None, stop_on_iteration=2)
        s.add_engine(stopper_engine)
        engine = CountEngine(None)
        s.add_engine(engine)

        s.timesteps = [0, ]
        s.start()

        assert engine.iteration == 1
        assert engine.run_count == 1
        assert stopper_engine.iteration == 2
Exemplo n.º 11
0
    def test_engine_stopping_iteration(self):
        """ Test an engine terminating iteration

        The engine must raise a `StopIteration` exception.
        """
        s = Simulator(max_iterations=5)
        s.network = Network("Iteration test network")

        stopper_engine = StopperEngine(None, stop_on_iteration=2)
        s.add_engine(stopper_engine)
        engine = CountEngine(None)
        s.add_engine(engine)

        s.timesteps = [
            0,
        ]
        s.start()

        assert engine.iteration == 1
        assert engine.run_count == 1
        assert stopper_engine.iteration == 2
Exemplo n.º 12
0
    def test_dict_overwrite(self):
        """
            Test to ensure that dictionaries and other objects contained in history
            are not overwritten by maintaining references to the same objects.
        """
        print("Testing...")
        network = Network("History Test Network")
        network.add_node(HistoryTestNode(x=0, y=0, name="Test Node"))

        s = Simulator()
        s.network = network
        s.timesteps = ['a', 'b', 'c']

        s.start()

        assert s.network.nodes[0]._history['property_dict'] == [{
            'test': 'a'
        }, {
            'test': 'b'
        }, {
            'test': 'c'
        }]
Exemplo n.º 13
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'])
plt.title('SR storage')
Exemplo n.º 14
0
l3._upper_flow = flow_upper_matrix['l3']
l3._cost=flow_cost_matrix['l3']


l4 = RiverSection(name="l4", start_node=jn1, end_node=endpt)
l4._flow_multiplier = flow_mult_matrix["l4"]
l4._lower_flow = flow_lower_matrix['l4']
l4._upper_flow = flow_upper_matrix['l4']
l4._cost=flow_cost_matrix['l4']

#When adding these links to the network, the network's nodes will be updated also
#with the link added to the starting node's 'out links' and the ending node's 'in links'
n.add_links(l1, l2, l3, l4)

#Set the network on the simulator
s.network = n


#Create an instance of the pyomo allocator engine.
allocator = PyomoAllocation(n)

#add the engine to the simulator. Multiple engines can be added here. The
#engines will be run sequentially based on the order they were added to the
#simulator
s.add_engine(allocator)

#start the simulator
s.start()

total_deficit = 0
Exemplo n.º 15
0
                 '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')
Exemplo n.º 16
0
#Citrus farms and vegetable farms are demand nodes. They use the water supplied
#by the reservoir.
irr1 = CitrusFarm(x=1, y=2, name="I1")
irr2 = CitrusFarm(x=10, y=20, name="I2")
irr3 = VegetableFarm(x=100, y=200, name="I3")

n.add_nodes(sr1, irr1, irr2, irr3)

#Create some links
n.add_link(RiverSection(name="rs1", start_node=sr1, end_node=irr1))
n.add_link(RiverSection(name="rs2", start_node=sr1, end_node=irr2))
n.add_link(RiverSection(name="rs3", start_node=sr1, end_node=irr3))

#Set the simulator's network to this network.
s.network = n

#Add some institutions (loosely based on the Jordan project)
mow = WaterDepartment("Jordan Ministry of Water")
mow.add_nodes(sr1)
jva = IrrigationDecisionMaker("Jordan Valley Authority")
jva.add_nodes(irr1, irr2, irr3)

n.add_institutions(mow, jva)

#Create an instance of the deficit allocation engine.
allocator = DeficitAllocation(n)

#add the engine to the simulator
s.add_engine(allocator)
Exemplo n.º 17
0
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)