duheng
2024-11-05 21dd2ae9704c484d5d75b2ed980e5402505da7dc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
using System.Text;
 
namespace Yw.EPAnet
{
    /// <summary>
    /// 管网计算拓展
    /// </summary>
    public static class NetworkCalcuExtensions
    {
        /// <summary>
        /// 计算
        /// </summary>
        public static CalcuResult Calcu(this Network network,bool MinorLossPreCalc=true)
        {
            var result = new CalcuResult();
           
            //Null验证
            if (network == null)
            {
                result.Succeed = false;
                return result;
            }
           
            string inpString = null;
            if (MinorLossPreCalc)
            {
                CalcuResult minorLossResult = network.CalcuMinorLoss();
                if (!minorLossResult.Succeed)
                {
                    result.Succeed = false;
                    result.FailedList.AddRange(minorLossResult.FailedList);
                    return result;
                }
                inpString = network.ToInpString(minorLossResult);
            }
            else
            {
                inpString = network.ToInpString();
            }
            //获取系统临时文件目录,创建inp临时文件
            var inpFilePath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N") + ".inp");
        
            File.WriteAllText(inpFilePath, inpString);
 
            //加载管网
            var epanet = new HydraulicCore(true);
            var errOpen = epanet.open(inpFilePath, "", "");
            if (errOpen != 0)
            {
 
                string txt = epanet.geterrormsg();
 
                result.Succeed = false;
                result.FailedList.Add(new CalcuFailed()
                {
                    Code = errOpen,
                    Message = $"{txt}"
                });
                return result;
            }
 
            //水力计算
            var errCalcu = epanet.solveH();
            if (errCalcu != 0)
            {
 
                string txt = epanet.geterrormsg();
                result.Succeed = false;
                result.FailedList.Add(new CalcuFailed()
                {
                    Code = errCalcu,
                    Message = $"{txt}"
                });
                return result;
            }
 
            int nodeCount = 0;
            int linkCount = 0;
            epanet.getcount((int)eCountType.Node, ref nodeCount);
            epanet.getcount((int)eCountType.Link, ref linkCount);
 
            const int MAXID = 31;
 
            var sb = new StringBuilder(MAXID);
 
            for (int i = 1; i <= nodeCount; i++)
            {
                epanet.getnodeid(i, sb);
                var arr = new string[] { "Head", "Press", "Demand" }; //System.Enum.GetValues(typeof(HydraulicModel.NodeValueType));
                var arrnum = new int[] { 10, 11, 9 };
                var resultNode = new Node()
                {
                    Id = sb.ToString(),
                };
                for (var j = 0; j < arr.Length; j++)
                {
                    float v = 0;
                    //var t = (EPAcore.Core.NodeValueType)j;
                    epanet.getnodevalue(i, arrnum[j], ref v);
                    switch (arr[j])
                    {
                        case "Head":
                            resultNode.Head = v;
                            break;
                        case "Press":
                            resultNode.Press = v;
                            break;
                        case "Demand":
                            resultNode.Demand = v;
                            break;
                    }
                }
                result.NodeList.Add(resultNode);
                result.NodeDict.Add(resultNode.Id, resultNode);
            }
 
            for (int i = 1; i <= linkCount; i++)
            {
                epanet.getlinkid(i, sb);
                //var arr = System.Enum.GetValues(typeof(HydraulicModel.LinkValueType));
                var arr = new string[] { "Flow", "Velocity", "Headloss" }; //System.Enum.GetValues(typeof(HydraulicModel.NodeValueType));
                var arrnum = new int[] { 8, 9, 10 };
                var resultLink = new Link()
                {
                    Id = sb.ToString(),
                };
                for (var j = 0; j < arr.Length; j++)
                {
                    float v = 0;
                    //var t = (EPAcore.Core.NodeValueType)j;
                    epanet.getlinkvalue(i, arrnum[j], ref v);
                    switch (arr[j])
                    {
                        case "Flow":
                            resultLink.Flow = v;
                            break;
                        case "Velocity":
                            resultLink.Velocity = v;
                            break;
                        case "Headloss":
                            resultLink.Headloss = v;
                            break;
                    }
                }
                result.LinkList.Add(resultLink);
                result.LinkDict.Add(resultLink.Id, resultLink);
            }
            
 
 
 
            return result;
        }
 
 
    }
}