Exemplo n.º 1
0
        ///<summary>Retrieves hydraulic solution and hydraulic time step for next hydraulic event.</summary>
        private void GetHyd(BinaryWriter outStream, HydraulicReader hydSeek)
        {
            AwareStep step = hydSeek.GetStep(_htime);

            LoadHydValues(step);

            _htime += step.Step;

            if (_htime >= _rtime)
            {
                SaveOutput(outStream);
                _nperiods++;
                _rtime += _net.RStep;
            }


            if (_qualflag != QualType.NONE && _qtime < _net.Duration)
            {
                if (_reactflag && _qualflag != QualType.AGE)
                {
                    Ratecoeffs();
                }

                if (_qtime == 0)
                {
                    Initsegs();
                }
                else
                {
                    Reorientsegs();
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>Run the water quality simulation.</summary>
        /// <param name="hydFile">The hydraulic output file generated previously.</param>
        /// <param name="out">The output stream were the water quality simulation results will be written.</param>
        ///
        void Simulate(string hydFile, Stream @out)
        {
            BinaryWriter bw = new BinaryWriter(@out);

            bw.Write((int)_net.Nodes.Count);
            bw.Write((int)_net.Links.Count);
            long tstep;

            using (var hydraulicReader = new HydraulicReader(new BinaryReader(File.OpenRead(hydFile))))
                do
                {
                    if (_qtime == _htime)
                    {
                        GetHyd(bw, hydraulicReader);
                    }


                    tstep = Nextqual(bw);
                } while (tstep > 0);
        }
Exemplo n.º 3
0
        public static void main(string[] args)
        {
            int i;

            var net = new EpanetNetwork();

            //Tank
            Tank tank = new Tank("0")
            {
                Elevation = 210
            };

            net.Nodes.Add(tank);

            //Nodes
            Node[] node = new Node[7];

            for (i = 1; i < 7; i++)
            {
                node[i] = new Node(i.ToString());
            }


            node[1].Elevation = 150;
            node[1].Demands.Add(new Demand(100, null));
            node[2].Elevation = 160;
            node[2].Demands.Add(new Demand(100, null));
            node[3].Elevation = 155;
            node[3].Demands.Add(new Demand(120, null));
            node[4].Elevation = 150;
            node[4].Demands.Add(new Demand(270, null));
            node[5].Elevation = 165;
            node[5].Demands.Add(new Demand(330, null));
            node[6].Elevation = 160;
            node[6].Demands.Add(new Demand(200, null));

            //Links
            Link[] pipe = new Link[8];
            for (i = 0; i < 8; i++)
            {
                pipe[i] = new Link(i.ToString())
                {
                    Lenght = 1000
                };
            }
            pipe[0].FirstNode  = tank;
            pipe[0].SecondNode = node[1];
            pipe[1].FirstNode  = node[1];
            pipe[1].SecondNode = node[2];
            pipe[2].FirstNode  = node[1];
            pipe[2].SecondNode = node[3];
            pipe[3].FirstNode  = node[3];
            pipe[3].SecondNode = node[4];
            pipe[4].FirstNode  = node[3];
            pipe[4].SecondNode = node[5];
            pipe[5].FirstNode  = node[5];
            pipe[5].SecondNode = node[6];
            pipe[6].FirstNode  = node[2];
            pipe[6].SecondNode = node[4];
            pipe[7].FirstNode  = node[4];
            pipe[7].SecondNode = node[6];

            for (i = 1; i < 7; i++)
            {
                net.Nodes.Add(node[i]);
            }
            for (i = 0; i < 8; i++)
            {
                net.Links.Add(pipe[i]);
            }

            //Prepare Network
            TraceSource log = new TraceSource(typeof(SampleOOPNetwork2).FullName, SourceLevels.All);
            NullParser  nP  = (NullParser)InputParser.Create(FileType.NULL_FILE);

            Debug.Assert(nP != null);
            nP.Parse(new EpanetNetwork(), null);

            //// Simulate hydraulics
            string       hydFile = Path.GetTempFileName(); // ("hydSim", "bin");
            HydraulicSim hydSim  = new HydraulicSim(net, log);

            hydSim.Simulate(hydFile);

            // Read hydraulic results

            HydraulicReader hydReader = new HydraulicReader(hydFile);

            for (long time = net.RStart; time <= net.Duration; time += net.RStep)
            {
                AwareStep step = hydReader.GetStep((int)time);
                Console.WriteLine("Time : " + step.Time.GetClockTime() + ", nodes heads : ");

                i = 0;
                foreach (Node inode in net.Nodes)
                {
                    Console.Write("{0:F2}\t", step.GetNodeHead(i++, inode, null));
                }

                Console.WriteLine();
            }

            hydReader.Close();
        }
Exemplo n.º 4
0
        public static void main(string[] args)
        {
            TraceSource log = new TraceSource(typeof(EPATool).FullName, SourceLevels.All);

            string hydFile  = null;
            string qualFile = null;
            var    net      = new EpanetNetwork();

            List <NodeVariableType> nodesVariables = new List <NodeVariableType>();
            List <LinkVariableType> linksVariables = new List <LinkVariableType>();

            string        inFile      = "";
            List <long>   targetTimes = new List <long>();
            List <string> targetNodes = new List <string>();
            List <string> targetLinks = new List <string>();

            int parseMode = 0;

            foreach (string arg in args)
            {
                if (arg.EndsWith(".inp", StringComparison.OrdinalIgnoreCase))
                {
                    parseMode = 0;
                    inFile    = arg;
                    if (!File.Exists(inFile))
                    {
                        ConsoleLog("END_RUN_ERR");
                        Console.Error.WriteLine("File not found !");
                        return;
                    }
                    continue;
                }

                switch (arg)
                {
                case "-T":
                case "-t":
                    parseMode = 1;
                    continue;

                case "-N":
                case "-n":
                    parseMode = 2;
                    continue;

                case "-L":
                case "-l":
                    parseMode = 3;
                    continue;
                }

                switch (parseMode)
                {
                case 1:
                    targetTimes.Add((long)(Utilities.GetHour(arg) * 3600));
                    break;

                case 2:
                    targetNodes.Add(arg);
                    break;

                case 3:
                    targetLinks.Add(arg);
                    break;
                }
            }

            try {
                InputParser parserInp = InputParser.Create(FileType.INP_FILE);
                net = parserInp.Parse(new EpanetNetwork(), inFile);


                if (targetTimes.Count > 0)
                {
                    foreach (long time  in  targetTimes)
                    {
                        string epanetTime = time.GetClockTime();
                        if (time < net.RStart)
                        {
                            throw new Exception("Target time \"" + epanetTime + "\" smaller than simulation start time");
                        }

                        if (time > net.Duration)
                        {
                            throw new Exception("Target time \"" + epanetTime + "\" bigger than simulation duration");
                        }

                        if ((time - net.RStart) % net.RStep != 0)
                        {
                            throw new Exception("Target time \"" + epanetTime + "\" not found");
                        }
                    }
                }

                foreach (string nodeName  in  targetNodes)
                {
                    if (net.GetNode(nodeName) == null)
                    {
                        throw new Exception("Node \"" + nodeName + "\" not found");
                    }
                }

                foreach (string linkName  in  targetLinks)
                {
                    if (net.GetLink(linkName) == null)
                    {
                        throw new Exception("Link \"" + linkName + "\" not found");
                    }
                }

                nodesVariables.Add(NodeVariableType.ELEVATION);
                nodesVariables.Add(NodeVariableType.BASEDEMAND);

                if (net.QualFlag != QualType.NONE)
                {
                    nodesVariables.Add(NodeVariableType.INITQUALITY);
                }

                nodesVariables.Add(NodeVariableType.PRESSURE);
                nodesVariables.Add(NodeVariableType.HEAD);
                nodesVariables.Add(NodeVariableType.DEMAND);

                if (net.QualFlag != (QualType.NONE))
                {
                    nodesVariables.Add(NodeVariableType.QUALITY);
                }

                linksVariables.Add(LinkVariableType.LENGHT);
                linksVariables.Add(LinkVariableType.DIAMETER);
                linksVariables.Add(LinkVariableType.ROUGHNESS);
                linksVariables.Add(LinkVariableType.FLOW);
                linksVariables.Add(LinkVariableType.VELOCITY);
                linksVariables.Add(LinkVariableType.UNITHEADLOSS);
                linksVariables.Add(LinkVariableType.FRICTIONFACTOR);

                if (net.QualFlag != QualType.NONE)
                {
                    linksVariables.Add(LinkVariableType.QUALITY);
                }

                hydFile = Path.GetTempFileName(); // "hydSim.bin"

                ConsoleLog("START_RUNNING");

                HydraulicSim hydSim = new HydraulicSim(net, log);
                hydSim.Simulate(hydFile);


                if (net.QualFlag != QualType.NONE)
                {
                    qualFile = Path.GetTempFileName(); // "qualSim.bin"

                    QualitySim q = new QualitySim(net, log);
                    q.Simulate(hydFile, qualFile);
                }


                HydraulicReader hydReader = new HydraulicReader(new BinaryReader(File.OpenRead(hydFile)));

                StreamWriter nodesTextWriter = null;
                StreamWriter linksTextWriter = null;
                string       nodesOutputFile = null;

                if (targetNodes.Count == 0 && targetLinks.Count == 0 || targetNodes.Count > 0)
                {
                    nodesOutputFile = Path.GetFullPath(inFile) + ".nodes.out";
                    nodesTextWriter = new StreamWriter(nodesOutputFile, false, Encoding.UTF8);

                    nodesTextWriter.Write('\t');
                    foreach (NodeVariableType nodeVar  in  nodesVariables)
                    {
                        nodesTextWriter.Write('\t');
                        nodesTextWriter.Write(nodeVar.ToString());
                    }
                    nodesTextWriter.Write("\n\t");

                    foreach (NodeVariableType nodeVar  in  nodesVariables)
                    {
                        nodesTextWriter.Write('\t');
                        nodesTextWriter.Write(net.FieldsMap.GetField(ToFieldType(nodeVar)).Units);
                    }
                    nodesTextWriter.Write('\n');
                }


                if (targetNodes.Count == 0 && targetLinks.Count == 0 || targetLinks.Count > 0)
                {
                    string linksOutputFile = Path.GetFullPath(inFile) + ".links.out";
                    linksTextWriter = new StreamWriter(linksOutputFile, false, Encoding.UTF8);

                    linksTextWriter.Write('\t');
                    foreach (LinkVariableType linkVar  in  linksVariables)
                    {
                        linksTextWriter.Write('\t');
                        linksTextWriter.Write(linkVar.ToString());
                    }
                    linksTextWriter.Write("\n\t");

                    foreach (LinkVariableType linkVar  in  linksVariables)
                    {
                        linksTextWriter.Write('\t');
                        if (linkVar < 0)
                        {
                            continue;
                        }
                        linksTextWriter.Write(net.FieldsMap.GetField((FieldType)linkVar).Units);
                    }
                    linksTextWriter.Write('\n');
                }


                for (long time = net.RStart; time <= net.Duration; time += net.RStep)
                {
                    AwareStep step = hydReader.GetStep((int)time);

                    int i = 0;

                    if (targetTimes.Count > 0 && !targetTimes.Contains(time))
                    {
                        continue;
                    }

                    if (nodesTextWriter != null)
                    {
                        foreach (Node node  in  net.Nodes)
                        {
                            if (targetNodes.Count > 0 && !targetNodes.Contains(node.Name))
                            {
                                continue;
                            }

                            nodesTextWriter.Write(node.Name);

                            nodesTextWriter.Write('\t');
                            nodesTextWriter.Write(time.GetClockTime());

                            foreach (NodeVariableType nodeVar  in  nodesVariables)
                            {
                                nodesTextWriter.Write('\t');
                                double val = GetNodeValue(nodeVar, net.FieldsMap, step, node, i);
                                nodesTextWriter.Write(ConvertToScientifcNotation(val, 1000, 0.01, 2));
                            }

                            nodesTextWriter.Write('\n');

                            i++;
                        }
                    }

                    i = 0;

                    if (linksTextWriter != null)
                    {
                        foreach (Link link  in  net.Links)
                        {
                            if (targetLinks.Count > 0 && !targetLinks.Contains(link.Name))
                            {
                                continue;
                            }

                            linksTextWriter.Write(link.Name);

                            linksTextWriter.Write('\t');
                            linksTextWriter.Write(time.GetClockTime());

                            foreach (LinkVariableType linkVar  in  linksVariables)
                            {
                                linksTextWriter.Write('\t');
                                double val = GetLinkValue(
                                    linkVar,
                                    net.FormFlag,
                                    net.FieldsMap,
                                    step,
                                    link,
                                    i);
                                linksTextWriter.Write(ConvertToScientifcNotation(val, 1000, 0.01, 2));
                            }

                            linksTextWriter.Write('\n');

                            i++;
                        }
                    }
                }

                if (nodesTextWriter != null)
                {
                    nodesTextWriter.Close();
                    ConsoleLog("NODES FILE \"" + nodesOutputFile + "\"");
                }

                if (linksTextWriter != null)
                {
                    linksTextWriter.Close();
                    ConsoleLog("LINKS FILES \"" + nodesOutputFile + "\"");
                }

                ConsoleLog("END_RUN_OK");
            }
            catch (ENException e) {
                ConsoleLog("END_RUN_ERR");
                Debug.Print(e.ToString());
            }
            catch (IOException e) {
                ConsoleLog("END_RUN_ERR");
                Debug.Print(e.ToString());
            }
            catch (Exception e) {
                ConsoleLog("END_RUN_ERR");
                Debug.Print(e.ToString());
            }

            if (!string.IsNullOrEmpty(hydFile))
            {
                File.Delete(hydFile);
            }

            if (!string.IsNullOrEmpty(qualFile))
            {
                File.Delete(qualFile);
            }
        }
Exemplo n.º 5
0
        /// <summary>Generate hydraulic report.</summary>
        ///  <param name="hydBinFile">Name of the hydraulic simulation output file.</param>
        /// <param name="net">Hydraulic network.</param>
        /// <param name="values">Variables report flag.</param>
        public void CreateHydReport(string hydBinFile, EpanetNetwork net, bool[] values)
        {
            Rtime = 0;
            HydraulicReader dseek       = new HydraulicReader(new BinaryReader(File.OpenRead(hydBinFile)));
            int             reportCount = (int)((net.Duration - net.RStart) / net.RStep) + 1;
            var             nodes       = net.Nodes;
            var             links       = net.Links;

            object[] nodesHead = new object[dseek.Nodes + 1];
            nodesHead[0] = _sheet.TransposedMode ? "Node/Time" : "Time/Node";
            for (int i = 0; i < nodes.Count; i++)
            {
                nodesHead[i + 1] = nodes[i].Name;
            }

            var linksHead = new object[dseek.Links + 1];

            linksHead[0] = _sheet.TransposedMode ? "Link/Time" : "Time/Link";
            for (int i = 0; i < links.Count; i++)
            {
                linksHead[i + 1] = links[i].Name;
            }

            XlsxWriter.Spreadsheet[] resultSheets = new XlsxWriter.Spreadsheet[HydVariable.Values.Length];
            // Array.Clear(resultSheets, 0, resultSheets.Length);

            for (int i = 0; i < resultSheets.Length; i++)
            {
                if (values != null && !values[i])
                {
                    continue;
                }
                resultSheets[i] = _sheet.NewSpreadsheet(HydVariable.Values[i].name);
                resultSheets[i].AddHeader(HydVariable.Values[i].isNode ? nodesHead : linksHead);
            }

            var nodeRow = new object[dseek.Nodes + 1];
            var linkRow = new object[dseek.Links + 1];

            for (long time = net.RStart; time <= net.Duration; time += net.RStep)
            {
                var step = dseek.GetStep(time);

                if (step == null)
                {
                    Rtime = time;
                    continue;
                }

                nodeRow[0] = time.GetClockTime();
                linkRow[0] = time.GetClockTime();

                // NODES HEADS
                if (resultSheets[(int)HydVariable.Type.Head] != null)
                {
                    for (int i = 0; i < nodes.Count; i++)
                    {
                        nodeRow[i + 1] = step.GetNodeHead(i, nodes[i], net.FieldsMap);
                    }

                    resultSheets[(int)HydVariable.Type.Head].AddData(nodeRow);
                }

                // NODES DEMANDS
                if (resultSheets[(int)HydVariable.Type.Demands] != null)
                {
                    for (int i = 0; i < nodes.Count; i++)
                    {
                        nodeRow[i + 1] = step.GetNodeDemand(i, nodes[i], net.FieldsMap);
                    }

                    resultSheets[(int)HydVariable.Type.Demands].AddData(nodeRow);
                }

                // NODES PRESSURE
                if (resultSheets[(int)HydVariable.Type.Pressure] != null)
                {
                    for (int i = 0; i < nodes.Count; i++)
                    {
                        nodeRow[i + 1] = step.GetNodePressure(i, nodes[i], net.FieldsMap);
                    }

                    resultSheets[(int)HydVariable.Type.Pressure].AddData(nodeRow);
                }

                // LINK FLOW
                if (resultSheets[(int)HydVariable.Type.Flows] != null)
                {
                    for (int i = 0; i < links.Count; i++)
                    {
                        linkRow[i + 1] = step.GetLinkFlow(i, links[i], net.FieldsMap);
                    }

                    resultSheets[(int)HydVariable.Type.Flows].AddData(linkRow);
                }

                // LINK VELOCITY
                if (resultSheets[(int)HydVariable.Type.Velocity] != null)
                {
                    for (int i = 0; i < links.Count; i++)
                    {
                        linkRow[i + 1] = step.GetLinkVelocity(i, links[i], net.FieldsMap);
                    }

                    resultSheets[(int)HydVariable.Type.Velocity].AddData(linkRow);
                }

                // LINK HEADLOSS
                if (resultSheets[(int)HydVariable.Type.Headloss] != null)
                {
                    for (int i = 0; i < links.Count; i++)
                    {
                        linkRow[i + 1] = step.GetLinkHeadLoss(i, links[i], net.FieldsMap);
                    }

                    resultSheets[(int)HydVariable.Type.Headloss].AddData(linkRow);
                }

                // LINK FRICTION
                if (resultSheets[(int)HydVariable.Type.Friction] != null)
                {
                    for (int i = 0; i < links.Count; i++)
                    {
                        linkRow[i + 1] = step.GetLinkFriction(i, links[i], net.FieldsMap);
                    }

                    resultSheets[(int)HydVariable.Type.Friction].AddData(linkRow);
                }

                Rtime = time;
            }

            dseek.Close();
        }
Exemplo n.º 6
0
 public void Open(string hydFile)
 {
     _dseek = new HydraulicReader(new BinaryReader(File.OpenRead(hydFile)));
 }