public static void DumpErrorMessages(TextWriter w)
        {
            const int MSGLEN = UnsafeNativeMethods.MAXMSG * 2;

            w.WriteLine();
            w.WriteLine("ERROR MESSAGES:");

            StringBuilder sb = new StringBuilder(MSGLEN);

            foreach (ErrorCode err in Enum.GetValues(typeof(ErrorCode)))
            {
                if (err == ErrorCode.Ok)
                {
                    continue;
                }
                try {
                    EpanetException.Check(UnsafeNativeMethods.ENgeterror(err, sb, MSGLEN));
                    w.WriteLine("    {0}: {1}", err, sb);
                }
                catch (EpanetException ex) {
                    if (ex.ErrorCode != ErrorCode.Err251)
                    {
                        throw;
                    }
                    Console.Out.WriteLine("    {0}: <undefined>", err);
                }
            }
        }
        public static void DumpPatterns(TextWriter w)
        {
            int count;

            EpanetException.Check(UnsafeNativeMethods.ENgetcount(CountType.Pattern, out count));

            for (int i = 1; i <= count; i++)
            {
                int           len;
                StringBuilder id = new StringBuilder(UnsafeNativeMethods.MAXID);

                EpanetException.Check(UnsafeNativeMethods.ENgetpatternid(i, id));
                EpanetException.Check(UnsafeNativeMethods.ENgetpatternlen(i, out len));

                w.WriteLine();
                w.WriteLine("PATTERN {0}: Length={1}", id, len);

                string[] values = new string[len];

                for (int j = 1; j <= len; j++)
                {
                    float value;
                    EpanetException.Check(UnsafeNativeMethods.ENgetpatternvalue(i, j, out value));
                    values[j - 1] = value.ToString(NumberFormatInfo.InvariantInfo);
                }

                w.WriteLine("    [{0}]", string.Join(", ", values));
            }
        }
        public static void DumpNodes(TextWriter w)
        {
            int count;

            EpanetException.Check(UnsafeNativeMethods.ENgetcount(CountType.Node, out count));

            w.WriteLine();
            w.WriteLine("NODES ({0}):", count);

            for (int i = 1; i <= count; i++)
            {
                StringBuilder id = new StringBuilder(UnsafeNativeMethods.MAXID);
                EpanetException.Check(UnsafeNativeMethods.ENgetnodeid(i, id));

                NodeType type;
                EpanetException.Check(UnsafeNativeMethods.ENgetnodetype(i, out type));

                w.WriteLine(NLFORMAT, "NODE", i, id, type);

                foreach (NodeValue code in Enum.GetValues(typeof(NodeValue)))
                {
                    try {
                        float value;
                        EpanetException.Check(UnsafeNativeMethods.ENgetnodevalue(i, code, out value));
                        w.WriteLine("        {0}={1}", code, value);
                    }
                    catch (EpanetException) {
                        w.WriteLine("        {0}=N/A", code);
                    }
                }
            }
        }
        public static void DumpSummary(TextWriter w)
        {
            w.WriteLine();
            w.WriteLine("OPTIONS:");

            foreach (MiscOption option in Enum.GetValues(typeof(MiscOption)))
            {
                float value;
                EpanetException.Check(UnsafeNativeMethods.ENgetoption(option, out value));
                w.WriteLine("    {0}={1}", option, value);
            }

            w.WriteLine();
            w.WriteLine("TIME PARAMETERS:");

            foreach (TimeParameter param in Enum.GetValues(typeof(TimeParameter)))
            {
                int value;
                EpanetException.Check(UnsafeNativeMethods.ENgettimeparam(param, out value));
                w.WriteLine("    {0}={1}", param, value);
            }

            FlowUnitsType unit;

            EpanetException.Check(UnsafeNativeMethods.ENgetflowunits(out unit));
            w.WriteLine();
            w.WriteLine("FLOW UNIT: {0}", unit);

            QualType qualcode;
            int      tracenode;

            EpanetException.Check(UnsafeNativeMethods.ENgetqualtype(out qualcode, out tracenode));

            w.WriteLine();
            w.WriteLine("QUALITY ANALYSIS: Type={0}, Trace node={1}", qualcode, tracenode);

            int count;

            EpanetException.Check(UnsafeNativeMethods.ENgetcount(CountType.Tank, out count));
            w.WriteLine("TANK COUNT:{0}", count);

            EpanetException.Check(UnsafeNativeMethods.ENgetcount(CountType.Curve, out count));
            w.WriteLine("CURVE COUNT:{0}", count);
        }
        public static void DumpLinks(TextWriter w)
        {
            int count;

            EpanetException.Check(UnsafeNativeMethods.ENgetcount(CountType.Link, out count));

            w.WriteLine();
            w.WriteLine("LINKS ({0}):", count);

            for (int i = 1; i <= count; i++)
            {
                StringBuilder id = new StringBuilder(UnsafeNativeMethods.MAXID);
                EpanetException.Check(UnsafeNativeMethods.ENgetlinkid(i, id));

                LinkType type;
                EpanetException.Check(UnsafeNativeMethods.ENgetlinktype(i, out type));

                w.WriteLine(NLFORMAT, "LINK", i, id, type);

                int fnode, tnode;
                EpanetException.Check(UnsafeNativeMethods.ENgetlinknodes(i, out fnode, out tnode));

                w.WriteLine("        Start node={0}", fnode);
                w.WriteLine("        End   node={0}", tnode);

                foreach (LinkValue code in Enum.GetValues(typeof(LinkValue)))
                {
                    try
                    {
                        float value;
                        EpanetException.Check(UnsafeNativeMethods.ENgetlinkvalue(i, code, out value));
                        w.WriteLine("        {0}={1}", code, value);
                    }
                    catch (EpanetException)
                    {
                        w.WriteLine("        {0}=N/A", code);
                    }
                }
            }
        }
        public static void DumpControls(TextWriter w)
        {
            int count;

            EpanetException.Check(UnsafeNativeMethods.ENgetcount(CountType.Control, out count));

            for (int i = 1; i <= count; i++)
            {
                ControlType type;
                float       settings, level;
                int         lindex, nindex;
                EpanetException.Check(
                    UnsafeNativeMethods.ENgetcontrol(i, out type, out lindex, out settings, out nindex, out level));

                w.WriteLine();
                w.WriteLine("CONTROL: #{0}, Type={1}", i, type);
                w.WriteLine("    Link={0}", lindex);
                w.WriteLine("    Settings={0}", settings);
                w.WriteLine("    Node={0}", nindex);
                w.WriteLine("    Level={0}", level);
            }
        }
Exemplo n.º 7
0
        public static void Run(string inFile)
        {
            string logFile = Path.ChangeExtension(inFile, "log");

            // string outFile = Path.ChangeExtension(inFile, "bin");

            // parameter 3 should NOT be null! epanet2.dll does not check it for NULL.
            EpanetException.Check(UnsafeNativeMethods.ENopen(inFile, logFile, string.Empty));

            try {
                using (var workbook = new XlsxWriter()) {
                    int nodeCount, linkCount;
                    EpanetException.Check(UnsafeNativeMethods.ENgetcount(CountType.Node, out nodeCount));
                    EpanetException.Check(UnsafeNativeMethods.ENgetcount(CountType.Link, out linkCount));

                    // set string builder buffer size to be at least MAXID chars
                    var sb = new StringBuilder(UnsafeNativeMethods.MAXID);

                    var nodesHead = new object[nodeCount + 1];
                    nodesHead[0] = "Time/Node";

                    // Fill list of nodes IDs
                    for (int i = 1; i <= nodeCount; i++)
                    {
                        EpanetException.Check(UnsafeNativeMethods.ENgetnodeid(i, sb));
                        nodesHead[i] = sb.ToString();
                    }

                    var linksHead = new object[linkCount + 1];
                    linksHead[0] = "Time/Link";

                    // Fill list of links IDs
                    for (int i = 1; i <= linkCount; i++)
                    {
                        EpanetException.Check(UnsafeNativeMethods.ENgetlinkid(i, sb));
                        linksHead[i] = sb.ToString();
                    }

                    // Sheet definitions
                    var sheetsList = new[] {
                        new { text = "Node head", isNode = true },
                        new { text = "Node actual demand", isNode = true },
                        new { text = "Node pressure", isNode = true },
                        new { text = "Link flows", isNode = false },
                        new { text = "Link velocity", isNode = false },
                        new { text = "Link unit headloss", isNode = false },
                        new { text = "Link setting", isNode = false }
                    };

                    // Fill workbook with sheets
                    for (int i = 0; i < sheetsList.Length; i++)
                    {
                        workbook.AddSheet(sheetsList[i].text);
                        workbook[i].AddRow(sheetsList[i].isNode ? nodesHead : linksHead);
                    }

                    var nodeRow = new object[nodeCount + 1];
                    var linkRow = new object[linkCount + 1];

                    EpanetException.Check(UnsafeNativeMethods.ENopenH());
                    EpanetException.Check(UnsafeNativeMethods.ENinitH(SaveOptions.None));

                    int tstep;
                    do
                    {
                        int t;
                        EpanetException.Check(UnsafeNativeMethods.ENrunH(out t));
                        TimeSpan span = TimeSpan.FromSeconds(t);

                        // Set first cell to current run time
                        nodeRow[0]     =
                            linkRow[0] =
                                string.Format("{0:d2}:{1:d2}:{2:d2}", (int)span.TotalHours, span.Minutes, span.Seconds);

                        WriteStep(workbook, nodeRow, linkRow);

                        EpanetException.Check(UnsafeNativeMethods.ENnextH(out tstep));
                    }while (tstep > 0);

                    workbook.Save(Path.ChangeExtension(inFile, "xlsx"));
                }
            }
            finally {
                EpanetException.Check(UnsafeNativeMethods.ENclose());
            }
        }
Exemplo n.º 8
0
        /// <summary>Retrivies single time step data from EPANET and writes it to workbook.</summary>
        private static void WriteStep(XlsxWriter workbook, object[] nodeRow, object[] linkRow)
        {
            float value;

            // NODES HEADS
            for (int i = 1; i < nodeRow.Length; i++)
            {
                EpanetException.Check(UnsafeNativeMethods.ENgetnodevalue(i, NodeValue.Head, out value));
                nodeRow[i] = value;
            }

            workbook[0].AddRow(nodeRow);

            // NODES DEMANDS
            for (int i = 1; i < nodeRow.Length; i++)
            {
                EpanetException.Check(UnsafeNativeMethods.ENgetnodevalue(i, NodeValue.Demand, out value));
                nodeRow[i] = value;
            }

            workbook[1].AddRow(nodeRow);

            // NODES PRESSURE
            for (int i = 1; i < nodeRow.Length; i++)
            {
                EpanetException.Check(UnsafeNativeMethods.ENgetnodevalue(i, NodeValue.Pressure, out value));
                nodeRow[i] = value;
            }

            workbook[2].AddRow(nodeRow);

            // LINK FLOW
            for (int i = 1; i < linkRow.Length; i++)
            {
                EpanetException.Check(UnsafeNativeMethods.ENgetlinkvalue(i, LinkValue.Flow, out value));
                linkRow[i] = value;
            }

            workbook[3].AddRow(linkRow);

            // LINK VELOCITY
            for (int i = 1; i < linkRow.Length; i++)
            {
                EpanetException.Check(UnsafeNativeMethods.ENgetlinkvalue(i, LinkValue.Velocity, out value));
                linkRow[i] = value;
            }

            workbook[4].AddRow(linkRow);

            // LINK HEADLOSS
            for (int i = 1; i < linkRow.Length; i++)
            {
                EpanetException.Check(UnsafeNativeMethods.ENgetlinkvalue(i, LinkValue.HeadLoss, out value));
                linkRow[i] = value;
            }

            workbook[5].AddRow(linkRow);

            // LINK FRICTION
            for (int i = 1; i < linkRow.Length; i++)
            {
                EpanetException.Check(UnsafeNativeMethods.ENgetlinkvalue(i, LinkValue.Setting, out value));
                linkRow[i] = value;
            }

            workbook[6].AddRow(linkRow);
        }