Ejemplo n.º 1
0
 def test_properly_open_a_network_file(self):
     import filecmp
     file = os.path.join(os.path.dirname(simple.__file__), 'Net3.inp')
     es = EPANetSimulation(file)
     self.assertNotEqual(file, self.es.inputfile)
     self.assertTrue(os.path.isfile(self.es.inputfile))
     self.assertFalse(os.path.isdir(self.es.inputfile))
     # file names are unique
     self.assertEqual(
         len(set([EPANetSimulation(file).inputfile for i in range(100)])),
         100)
     # file content is identical to the original file
     self.assertTrue(filecmp.cmp(self.es.inputfile, file))
     # but names are not the same
     self.assertFalse(self.es.inputfile == file)
def GetConnectionData(inputFile):
    d = EPANetSimulation(inputFile)
    ret, nnodes = d.ENgetcount(d.EN_NODECOUNT)
    ret, llinks = d.ENgetcount(d.EN_LINKCOUNT)
    Conn = []
    NoConn = []
    h_degree = 0
    if (len(Conn) == 0):
        for n in range(0, nnodes + 1):
            c = []
            Conn.append(c)

    # generate mapping used later for resilience calculation
    for i in range(0, (llinks + 1)):
        if (len(Conn) == (nnodes + 1)):
            nodes = d.ENgetlinknodes(i + 1)
            if (nodes[0] == 0):
                Conn[nodes[1]].append(i + 1)
                Conn[nodes[2]].append(i + 1)
                h_degree = max(len(Conn[nodes[0]]), len(Conn[nodes[1]]),
                               h_degree)

    for idx in range(0, nnodes + 1):
        NoConn.append(len(Conn[idx]))
        while (len(Conn[idx]) < h_degree):
            Conn[idx].append(0)
    del Conn[0]
    del NoConn[0]

    return Conn, NoConn
Ejemplo n.º 3
0
    def open_network(self, epanet_network):
        logger.info("Opening network %s" % epanet_network)
        self.es = EPANetSimulation(epanet_network, pdd=True)
        if (self.adfcalc):
            logger.info("Doing ADF calculations")
            self.es.adfcalc(diafact=self.diafact)
        else:
            logger.info("Skipping ADF")
        self._set_static_values()
        # set nodes, links for easy access!

        self.nodes = Nodes()
        self.links = Links()
        logger.info("Mapping nodes and links to new objects")
        for key, value in self.es.network.nodes.items():
            n = _Node()
            n.id = value.id
            self.nodes[key] = n

        for key, value in self.es.network.links.items():
            l = _Link()
            l.id = value.id
            l.length = value.length
            l.diameter = value.diameter
            try:
                l.ADF = value.ADF
            except:
                pass
            l.start = self.nodes[value.start.id]
            l.end = self.nodes[value.end.id]
            self.links[key] = l
Ejemplo n.º 4
0
 def fitness(self, x):
     from epanettools import epanet2 as et
     from epanettools.epanettools import EPANetSimulation, Node, Link, Network, Nodes, \
         Links, Patterns, Pattern, Controls, Control  # import all elements needed
     d = EPANetSimulation('/home/varsha/Documents/Project.inp')
     f1 = Cost.Cost(x)
     f2 = PRI.PRI(x, d)
     return [f1, f2]
Ejemplo n.º 5
0
    def get_pipe_closed_demand(self, pipeindex, dia_factor):
        import os
        prefix_ = "epanet_" + self._c_and_r(self.es.ENgetlinkid(pipeindex))
        fd, f = tempfile.mkstemp(suffix=".inp",
                                 prefix=prefix_,
                                 dir=tempfile.gettempdir(),
                                 text=True)
        os.close(fd)
        d = Link.value_type['EN_DIAMETER']

        ret, diam = self.es.ENgetlinkvalue(pipeindex, d)
        dsmall = diam / float(dia_factor)
        self._c_and_r(self.es.ENsetlinkvalue(pipeindex, d, dsmall))
        self.es.ENsaveinpfile(f)
        self._c_and_r(self.es.ENsetlinkvalue(pipeindex, d,
                                             diam))  # reset diameter
        self.es = EPANetSimulation(f, pdd=True)
        demand = self.get_total_demand()
        self.es = EPANetSimulation(
            self.orig_networkfile)  # reset original network
        return demand
Ejemplo n.º 6
0
class my_mo_problem(Problem):
    d = EPANetSimulation('/home/varsha/Documents/Project.inp')
    ret, nlinks = d.ENgetcount(d.EN_LINKCOUNT)

    def __init__(self):
        super(my_mo_problem, self).__init__(self.nlinks, 2, 2)
        self.types[:] = [Integer(0, 16)] * self.nlinks
        self.constraints[:] = "<=0"
        self.directions[:] = Problem.MINIMIZE

    def evaluate(self, solution):
        y = solution.variables
        solution.objectives[:] = [PRI.PRI(y, self.d), Cost.Cost(y, self.d)]
        solution.constraints[:] = Constraint.Constraint(y, self.d)
Ejemplo n.º 7
0
class my_mo_problem(Problem):
    d = EPANetSimulation('/home/varshac/optimization/Optimization-Code/d-town.inp')
    ret, nlinks = d.ENgetcount(d.EN_LINKCOUNT)
    hStar = Settings.SetValues(d)
    Functions.SetVariables(d)
    def __init__(self):
        super(my_mo_problem, self).__init__(self.nlinks, 2, 2)
        self.types[:] = [Integer(0, 16)]*self.nlinks
        self.constraints[:] = "<=0"
        self.directions[:] = Problem.MINIMIZE
    def evaluate(self, solution):
        y = solution.variables
        solution.objectives[:] = [-Functions.Res(y,self.d,self.hStar),Functions.Cost(y,self.d)]
        solution.constraints[:] = Functions.Constraint(self.hStar)
def SetValues(inputFile, id="", ep=True):
    d = Network(inputFile)
    curves = len(d.curves)
    max_elevation = 0

    if (ep):
        d.add_curve('C1', [(1000, 180)])
        d.add_curve('C2', [(2000, 200)])
        d.add_curve('C3', [(3000, 220)])
        d.add_curve('C4', [(4000, 240)])
        d.add_curve('C5', [(5000, 260)])
        d.add_curve('C6', [(1000, 200)])
        d.add_curve('C7', [(2000, 220)])
        d.add_curve('C8', [(3000, 240)])
        d.add_curve('C9', [(4000, 260)])
        d.add_curve('C10', [(5000, 280)])

    MinHead = [40]
    nodes = d.nodes
    nnodes = len(nodes)
    hStar = [0] * nnodes
    nbOfPipes = 0
    nbOfPumps = 0
    nbOfTanks = 0
    links = d.links
    curves = len(d.curves)
    for link in links:
        if link.link_type == 'pipe':
            nbOfPipes += 1
        elif link.link_type == 'pump':
            nbOfPumps += 1
    nbofJunctions = 0
    for node in nodes:
        if node.node_type == 'Junction':
            hStar[nbofJunctions] = random.choice(MinHead)
            max_elevation = max(max_elevation, node.elevation)
            nbofJunctions += 1
        elif node.node_type == 'Tank':
            nbOfTanks += 1
    #d.save_inputfile('temp.inp')

    if ep:
        d.save_inputfile('temp' + id + '.inp')
        et = EPANetSimulation('temp' + id + '.inp')
        Conn, NoConn = GetConnectionDetails(et)
        return et, hStar, curves, curves, nbOfPipes, nbOfPumps, nbOfTanks, Conn, NoConn, max_elevation
    else:
        return nbOfPipes, nbOfPumps, nbOfTanks, max_elevation
Ejemplo n.º 9
0
 def test_runs_a_simulation_with_pipe_closed_and_get_results_pdd_give_reasonable_results(self):
     # now do the same with pipe '247' closed.
     ind = self.es.network.links['247'].index
     dia = Link.value_type["EN_DIAMETER"]  # get the code for EN_STATUS
     # use old interface to set diameter to zero
     self.Error(self.es.ENsetlinkvalue(ind, dia, 0.1))  # now link diameter is small
     file = "1.inp"
     ret=self.es.ENsaveinpfile(file)
     self.assertEqual(ret,0)
     # now create a new object with the new file.
     es = EPANetSimulation(file, pdd=True)
     es.run()
     p = Node.value_type['EN_PRESSURE']
     d = Node.value_type['EN_DEMAND']
     self.assertAlmostEquals(es.network.nodes['215'].results[p][5], -1.3, places=1)
     self.assertAlmostEqual(es.network.nodes['215'].results[d][5], 0.0, places=1)
#INSTALATION !!!!
#pip install epanettools

#LINKS
#https://pypi.org/project/EPANETTOOLS/
#https://wntr.readthedocs.io/en/latest/apidoc/wntr.epanet.toolkit.html
#https://github.com/Vitens/epynet/blob/master/epynet/epanet2.py

import os, pprint

pp = pprint.PrettyPrinter()
from epanettools.epanettools import EPANetSimulation, Node, Link, Network, Nodes, Links, Patterns, Pattern, Controls, Control
from epanettools.examples import simple

file = os.path.join(os.path.dirname(simple.__file__), 'Net3.inp')
es = EPANetSimulation(file)
'''
getting to understand the network
'''

#Number of elements
Number_nodes = len(es.network.nodes)
Number_links = len(es.network.links)
Number_controls = len(es.network.controls)

#List of indexes
List_constrols = [
    es.network.controls[x].index for x in list(es.network.controls)
]
List_nodes = [es.network.nodes[x].id for x in list(es.network.nodes)]
List_links = [es.network.links[x].id for x in list(es.network.links)]
Ejemplo n.º 11
0
#importar dependencias
import os
from epanettools.epanettools import EPANetSimulation, Node, Link, Network
from epanettools.examples import simple
import numpy as np
import scipy
from sklearn.metrics import mean_squared_error
from math import sqrt
import matplotlib.pyplot as plt
import setPROPERTY

#leer la red
file = os.path.join(os.path.dirname(simple.__file__), 'test.inp')
es = EPANetSimulation(file)

#instanciar clases y metodos
cond = es.network.links
nod = es.network.nodes

#obtener la cantidad de conductos
ret, num_links = es.ENgetcount(es.EN_LINKCOUNT)  #numero de links

#leer propiedades de la red
diametros = Link.value_type['EN_DIAMETER']
scabrezze = Link.value_type['EN_ROUGHNESS']
presiones = Node.value_type['EN_PRESSURE']
flow = Link.value_type['EN_FLOW']
elevations = Node.value_type['EN_ELEVATION']
velocities = Link.value_type['EN_VELOCITY']

#Raccogliere ixs condotte in Cast Iron e listare le loro scabrezze
    def test_runs_a_normal_pressure_simulation_and_get_results_pdd_does_not_change_results(
            self):
        # self.fail("Not yet calling epanet emitter properly")
        def mod1():
            p = Node.value_type['EN_PRESSURE']
            self.assertAlmostEqual(self.es.network.nodes['103'].results[p][5],
                                   59.301,
                                   places=3)
            self.assertAlmostEqual(self.es.network.nodes['125'].results[p][5],
                                   66.051,
                                   places=3)
            self.assertEqual(self.es.network.time[5], 15213)
            self.assertEqual(self.es.network.tsteps[5], 2787)
            self.assertEqual(self.es.network.tsteps[6], 3600)
            self.assertEqual(len(self.es.network.time),
                             len(self.es.network.nodes[1].results[p]))

            d = Node.value_type['EN_DEMAND']
            h = Node.value_type['EN_HEAD']
            self.assertAlmostEqual(self.es.network.nodes['103'].results[d][5],
                                   101.232,
                                   places=3)
            self.assertAlmostEqual(self.es.network.nodes['103'].results[h][5],
                                   179.858,
                                   places=3)

        def mod2():
            p = Link.value_type['EN_DIAMETER']
            self.assertAlmostEquals(
                self.es.network.links[1].results[p][0], 99.0,
                places=1)  # index is not important. Diameter is fixed. !
            self.assertAlmostEquals(self.es.network.links['105'].results[p][0],
                                    12.0,
                                    places=1)
            v = Link.value_type['EN_VELOCITY']
            self.assertAlmostEquals(self.es.network.links[2].results[v][22],
                                    0.025,
                                    places=2)
            self.assertAlmostEquals(self.es.network.links['111'].results[v][1],
                                    3.23,
                                    places=2)

        def mod3():
            p = Node.value_type['EN_PRESSURE']
            self.assertAlmostEqual(self.es.network.nodes['215'].results[p][5],
                                   58.7571,
                                   places=3)
            self.assertAlmostEqual(self.es.network.nodes['225'].results[p][5],
                                   58.320,
                                   places=3)

            d = Node.value_type['EN_DEMAND']
            self.assertAlmostEqual(self.es.network.nodes['215'].results[d][5],
                                   70.064,
                                   places=3)
            self.assertAlmostEqual(self.es.network.nodes['225'].results[d][5],
                                   17.328,
                                   places=3)

        def mod4():
            p = Node.value_type['EN_PRESSURE']
            self.assertAlmostEqual(self.es.network.nodes['215'].results[p][5],
                                   58.7571,
                                   places=3)
            self.assertAlmostEqual(self.es.network.nodes['225'].results[p][5],
                                   58.320,
                                   places=3)

            d = Node.value_type['EN_DEMAND']
            self.assertAlmostEqual(self.es.network.nodes['215'].results[d][5],
                                   70.064,
                                   places=3)
            self.assertAlmostEqual(self.es.network.nodes['225'].results[d][5],
                                   17.328,
                                   places=3)

        self.es.run()
        mod1()
        mod2()
        self.es.runq()
        q = Node.value_type['EN_QUALITY']
        self.assertAlmostEqual(self.es.network.nodes['117'].results[q][4],
                               85.317,
                               places=3)
        self.assertAlmostEqual(self.es.network.nodes['117'].results[q][5],
                               100.0)
        e = Link.value_type['EN_ENERGY']
        self.assertAlmostEquals(self.es.network.links['111'].results[e][23],
                                .00685,
                                places=2)
        mod1()
        mod2()
        mod3()

        file = "1.inp"
        self.Error(self.es.ENsaveinpfile(file))
        # now create a new object with the new file.
        es = EPANetSimulation(file, pdd=True)
        es.run()
        mod1()
        mod2()
        mod3()
 def setUp(self):
     print("SETUP!")
     file = os.path.join(os.path.dirname(simple.__file__), 'Net3.inp')
     self.es = EPANetSimulation(file)
Ejemplo n.º 14
0
    '2': 12,
    'aTU1093150205': 6
}

no_leaks = 18696
no_flow_files = 18696
no_pressure_files = 18696
no_pressure_sensors = 21
no_flow_sensors = 7

import os

path_init = "F:\\manual\\Tese\\exploratory\\wisdom\\dataset\\"

path = path_init + "INP Files\\StatusQuoVerao2018.inp"
es = EPANetSimulation(path)

tg_links = [
    es.network.links[x].id for x in list(es.network.links)[:]
    if es.network.links[x].id in epanet_to_scada_flow.keys()
]
print(tg_links)
#7
print("Links:" + str(len(tg_links)))

from os import listdir
from os.path import isfile, join

pathtxt = path_init + "\\simulated\\fugas_txt\\"
pathflowtxt = path_init + "\\simulated\\fugas_txt\\Rotura_Q\\"
pathpressuretxt = path_init + "\\simulated\\fugas_txt\\Rotura_Pnova\\"
Ejemplo n.º 15
0
        return 2

    def get_bounds(self):
        return ([-4] * 2, [4] * 2)

    def get_nic(self):
        return 6

    def get_nix(self):

        return 2


from PyGMO import population

d = EPANetSimulation('/home/varsha/Documents/Project.inp')
prob = problem(my_udp())
print(prob)
algo = pg.algorithm.sms_emoa(
    gen=2000)  # 2000 generations of SMS-EMOA should solve it
pop = population(prob, 500)
pop = algo.evolve(pop)

print(prob.objfun(pop.champion.x))
import matplotlib.pyplot as plt
import numpy as np

F = np.array([ind.cur_f for ind in pop]).T
plt.scatter(F[0], F[1])
plt.xlabel("Objective 1")
plt.ylabel("Objective 2")
def run_epanet_model(request):
    start_time = time.time()

    return_obj = {
        'success': False,
        'message': None,
        'results': "",
    }

    if request.is_ajax() and request.method == 'POST':
        model = request.POST['model']
        quality = request.POST['quality']

        temp = 'tmp_' + str(uuid.uuid4()) + '.inp'

        with open(temp, 'w') as f:
            f.write(model)
        try:
            print("Initializing es")
            es = EPANetSimulation(temp)
            print("--- %s seconds ---" % (time.time() - start_time))
            start_time = time.time()

            print("run")
            es.run()
            print("--- %s seconds ---" % (time.time() - start_time))
            start_time = time.time()

            if quality != "NONE":
                print("runq")
                es.runq()
                print("--- %s seconds ---" % (time.time() - start_time))
                start_time = time.time()

            n = es.network.nodes
            nodes = {}
            node_threads = []
            node_range = 500
            print("getNodeRes")
            if len(n) > node_range:
                process = Process(target=getNodeResults,
                                  args=(n, range(1, node_range), nodes))
                process.start()
                node_threads.append(process)

                while node_range < len(n):
                    if len(n) - node_range - 500 < 0:
                        r = range(node_range, len(n) - node_range)
                    else:
                        r = range(node_range, node_range + 500)

                    process = Process(target=getNodeResults,
                                      args=(n, r, nodes))
                    process.start()
                    node_threads.append(process)

                    node_range += 500

            else:
                getNodeResults(n, range(0, len(n)), nodes)

            l = es.network.links
            links = {}
            link_threads = []
            link_range = 500
            if len(l) > link_range:
                process = Process(target=getLinkResults,
                                  args=(l, range(1, link_range), links))
                process.start()
                link_threads.append(process)

                while link_range < len(l):
                    if len(l) - link_range - 500 < 0:
                        r = range(link_range, len(l) - link_range)
                    else:
                        r = range(link_range, link_range + 500)

                    process = Process(target=getLinkResults,
                                      args=(l, r, links))
                    process.start()
                    link_threads.append(process)

                    link_range += 500

            else:
                getLinkResults(l, range(0, len(l)), links)

            if len(node_threads) > 0:
                for thread in node_threads:
                    thread.join()
                for thread in link_threads:
                    thread.join()

            print("--- %s seconds ---" % (time.time() - start_time))
            start_time = time.time()

            print("Setting return obj")

            return_obj['results'] = {'nodes': nodes, 'edges': links}
            print("--- %s seconds ---" % (time.time() - start_time))

            return_obj['success'] = True

        except Exception as e:
            print e
            return_obj[e]

        finally:
            os.remove(temp)

    else:
        return_obj['message'] = message_template_wrong_req_method.format(
            method="POST")

    print("Returning obj")
    return JsonResponse(return_obj)
Ejemplo n.º 17
0
Created on Thu May 30 10:21:45 2019

This file will propose an algorithm to apply the adaptive control methodology for the network of richmond
but solely for pump 4B

Tank D    min 0       max 2.11      initial 1.94  LZHZTariff

Linearization pump D to tank D- 0.3m/hour.pumping

@author: Marcelo
"""
#Define Initial conditions and Determine ID's of pumps and tanks

from Inital_Conditions import tarifario_ST, tarifario_CB, tarifario_S, tarifario_HH, tarifario_LZG, tarifario_LZHZ
from epanettools.epanettools import EPANetSimulation, Node, Link, Network, Nodes, Links, Patterns, Pattern, Controls, Control
es = EPANetSimulation('richmondNet.inp')

p_status_1A = [
    0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
]  #CBTariff
p_status_2A = [
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
]  #CBTariff
p_status_3A = [
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
]  #STTariff
p_status_4B = [
    0, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1
]  #LZHZTariff
p_status_5C = [
    0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1
Ejemplo n.º 18
0
This workflow runs extended-period simulation of CTOWN network and plots pressure reads
   for junction J193
Dependencies: epanettools and matplot modules
"""

from epanettools import epanet2 as et
from epanettools.epanettools import EPANetSimulation
import matplotlib.pyplot as plt
import os

dir = os.path.abspath('testEPANETTOOLS.py')
dir = '\\'.join(dir.split('\\')[0:-2])
file_directory = dir + '\\resources\\CTOWN.inp'

ret = et.ENopen(file_directory, "CTOWN.rpt", "")
es = EPANetSimulation(file_directory)
n = es.network.nodes

junction = 'J193'

pres = []
nodes = []
time = []

et.ENopenH()
et.ENinitH(0)
while True:
    ret, t = et.ENrunH()
    ret, p = et.ENgetnodevalue(n[junction].index, et.EN_PRESSURE)
    print t, p
    ret, t_step = et.ENnextH()