예제 #1
0
        /// <summary>Write simulation summary to one worksheet.</summary>
        ///  <param name="inpFile">Hydraulic network file name.</param>
        /// <param name="net">Hydraulic network.</param>
        /// <param name="msxFile">MSX file.</param>
        /// <param name="msx">MSX solver.</param>
        public void WriteSummary(string inpFile, EpanetNetwork net, string msxFile, EpanetMSX msx)
        {
            var sh = _sheet.NewSpreadsheet("Summary");

            try {
                FieldsMap fMap = net.FieldsMap;

                if (net.Title != null)
                {
                    for (int i = 0; i < net.Title.Count && i < 3; i++)
                    {
                        if (!string.IsNullOrEmpty(net.Title[i]))
                        {
                            sh.AddData(
                                net.Title[i].Length > 70
                                ? net.Title[i].Substring(0, 70)
                                : net.Title[i]);
                        }
                    }
                }

                sh.AddData("\n");
                sh.AddData(Text.FMT19, inpFile);
                sh.AddData(Text.FMT20, net.Junctions.Count());

                int nReservoirs = net.Reservoirs.Count();
                int nTanks      = net.Tanks.Count();
                int nValves     = net.Valves.Count();
                int nPumps      = net.Pumps.Count();
                int nPipes      = net.Links.Count - nPumps - nValves;

                sh.AddData(Text.FMT21a, nReservoirs);
                sh.AddData(Text.FMT21b, nTanks);
                sh.AddData(Text.FMT22, nPipes);
                sh.AddData(Text.FMT23, nPumps);
                sh.AddData(Text.FMT24, nValves);
                sh.AddData(Text.FMT25, net.FormFlag.ParseStr());

                sh.AddData(Text.FMT26, net.HStep.GetClockTime());
                sh.AddData(Text.FMT27, net.HAcc);
                sh.AddData(Text.FMT27a, net.CheckFreq);
                sh.AddData(Text.FMT27b, net.MaxCheck);
                sh.AddData(Text.FMT27c, net.DampLimit);
                sh.AddData(Text.FMT28, net.MaxIter);

                switch (net.Duration == 0 ? QualType.NONE : net.QualFlag)
                {
                case QualType.NONE:
                    sh.AddData(Text.FMT29, "None");
                    break;

                case QualType.CHEM:
                    sh.AddData(Text.FMT30, net.ChemName);
                    break;

                case QualType.TRACE:
                    sh.AddData(Text.FMT31, "Trace From Node", net.GetNode(net.TraceNode).Name);
                    break;

                case QualType.AGE:
                    sh.AddData(Text.FMT32, "Age");
                    break;
                }

                if (net.QualFlag != QualType.NONE && net.Duration > 0)
                {
                    sh.AddData(Text.FMT33, "Time Step", net.QStep.GetClockTime());
                    sh.AddData(
                        Text.FMT34,
                        "Tolerance",
                        fMap.RevertUnit(FieldType.QUALITY, net.Ctol),
                        fMap.GetField(FieldType.QUALITY).Units);
                }

                sh.AddData(Text.FMT36, net.SpGrav);
                sh.AddData(Text.FMT37a, net.Viscos / Constants.VISCOS);
                sh.AddData(Text.FMT37b, net.Diffus / Constants.DIFFUS);
                sh.AddData(Text.FMT38, net.DMult);
                sh.AddData(
                    Text.FMT39,
                    fMap.RevertUnit(FieldType.TIME, net.Duration),
                    fMap.GetField(FieldType.TIME).Units);

                if (msxFile != null && msx != null)
                {
                    sh.AddData("");
                    sh.AddData("MSX data file", msxFile);
                    sh.AddData("Species");
                    Species[] spe = msx.Network.Species;
                    for (int i = 1; i < msx.Network.Species.Length; i++)
                    {
                        sh.AddData(spe[i].Id, spe[i].Units);
                    }
                }
            }
            catch (IOException) {}
            catch (ENException e) {
                Debug.Print(e.ToString());
            }
        }
예제 #2
0
        public static void main123(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);
            }
        }