duheng
2024-12-24 a5770e3760d8e966efcc6076c37fdaec72a04bc0
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
namespace Yw.EPAnet
{
    /// <summary>
    /// 管网检查拓展
    /// </summary>
    public static class NetworkCheckExtensions
    {
        /// <summary>
        /// 根据Inp文件计算
        /// </summary>
        public static CheckResult Check(this Network network)
        {
            var result = new CheckResult();
 
            //Network 为 null 验证
            if (network == null)
            {
                result.Succeed = false;
                return result;
            }
 
            //缺少水源
            var allSourceList = network.GetAllSources();
            if (allSourceList == null || allSourceList.Count < 1)
            {
                result.Succeed = false;
                result.FailedList.Add(new CheckFailed()
                {
                    ParterId = string.Empty,
                    CheckType = CheckType.SourceLack,
                    FailReason = "缺少水源,至少需包含一座水库或水池(水箱)"
                });
            }
 
            //缺少连接节点
            var allJunctionList = network.GetAllJunctions();
            if (allJunctionList == null || allJunctionList.Count < 1)
            {
                result.Succeed = false;
                result.FailedList.Add(new CheckFailed()
                {
                    ParterId = string.Empty,
                    CheckType = CheckType.JunctionLack,
                    FailReason = "缺少连接节点,至少需包含一个连接节点"
                });
            }
 
            //验证节点
            var allNodeList = network.GetAllNodes();
            foreach (var node in allNodeList)
            {
                if (node.Links == null || node.Links.Count < 1)
                {
                    result.Succeed = false;
                    result.FailedList.Add(new CheckFailed()
                    {
                        ParterId = node.Id,
                        CheckType = CheckType.NodeAlone,
                        FailReason = $"孤立节点[{node.Name}],没有任何管段连接"
                    });
                }
                else if (node is Elbow)
                {
                    if (node.Links.Count != 2)
                    {
                        result.Succeed = false;
                        result.FailedList.Add(new CheckFailed()
                        {
                            ParterId = node.Id,
                            CheckType = CheckType.NodeAbnormalConnected,
                            FailReason = $"弯头[{node.Name}]连接管段数异常,连接管段数必须为2"
                        });
                    }
                    else
                    {
                        var pipe1 = node.Links[0] as Pipe;
                        var pipe2 = node.Links[1] as Pipe;
                        if (pipe1 == null || pipe2 == null || pipe1.Diameter != pipe2.Diameter)
                        {
                            result.Succeed = false;
                            result.FailedList.Add(new CheckFailed()
                            {
                                ParterId = node.Id,
                                CheckType = CheckType.NodeAbnormalConnected,
                                FailReason = $"弯头[{node.Name}]必须连接2跟管段,且两根管段管径需一致"
                            });
                        }
                    }
                }
                else if (node is Threelink)
                {
                    if (node.Links.Count != 3)
                    {
                        result.Succeed = false;
                        result.FailedList.Add(new CheckFailed()
                        {
                            ParterId = node.Id,
                            CheckType = CheckType.NodeAbnormalConnected,
                            FailReason = $"三通[{node.Name}]连接管段数异常,连接管段数必须为3"
                        });
                    }
                    else
                    {
                        var pipe1 = node.Links[0] as Pipe;
                        var pipe2 = node.Links[1] as Pipe;
                        var pipe3 = node.Links[2] as Pipe;
                        if (pipe1 == null || pipe2 == null || pipe3 == null || pipe1.Diameter != pipe2.Diameter && pipe2.Diameter != pipe3.Diameter && pipe3.Diameter != pipe1.Diameter)
                        {
                            result.FailedList.Add(new CheckFailed()
                            {
                                ParterId = node.Id,
                                CheckType = CheckType.NodeAbnormalConnected,
                                FailReason = $"弯头[{node.Name}]必须连接3跟管线,且至少两根管线管径需一致"
                            });
                        }
                    }
                }
                else if (node is Fourlink)
                {
                    if (node.Links.Count != 4)
                    {
                        result.Succeed = false;
                        result.FailedList.Add(new CheckFailed()
                        {
                            ParterId = node.Id,
                            CheckType = CheckType.NodeAbnormalConnected,
                            FailReason = $"四通点[{node.Name}]连接管线数异常,连接管线数必须为4"
                        });
                    }
                    else
                    {
                        var pipe1 = node.Links[0] as Pipe;
                        var pipe2 = node.Links[1] as Pipe;
                        var pipe3 = node.Links[2] as Pipe;
                        var pipe4 = node.Links[3] as Pipe;
                        if (pipe1 == null || pipe2 == null || pipe3 == null || pipe4 == null || pipe1.Diameter != pipe2.Diameter || pipe1.Diameter != pipe3.Diameter || pipe1.Diameter != pipe4.Diameter)
                        {
                            result.Succeed = false;
                            result.FailedList.Add(new CheckFailed()
                            {
                                ParterId = node.Id,
                                CheckType = CheckType.NodeAbnormalConnected,
                                FailReason = $"四通点[{node.Name}]必须连接4跟管线,且所有连接管线管径需一致"
                            });
                        }
                    }
                }
 
            }
 
            //验证管段
            var allLinkList = network.GetAllLinks();
            foreach (var link in allLinkList)
            {
                if (link.StartNode == null && link.EndNode == null)
                {
                    result.Succeed = false;
                    result.FailedList.Add(new CheckFailed()
                    {
                        ParterId = link.Id,
                        CheckType = CheckType.LinkAlone,
                        FailReason = "孤立管段,没有连接任何管段"
                    });
                }
                else
                {
                    if (link.StartNode == null)
                    {
                        result.Succeed = false;
                        result.FailedList.Add(new CheckFailed()
                        {
                            ParterId = link.Id,
                            CheckType = CheckType.LinkLackNode,
                            FailReason = "管段缺少节点,缺少上游节点"
                        });
                    }
                    else if (link.EndNode == null)
                    {
                        result.Succeed = false;
                        result.FailedList.Add(new CheckFailed()
                        {
                            ParterId = link.Id,
                            CheckType = CheckType.LinkLackNode,
                            FailReason = "管段缺少节点,缺少下游节点"
                        });
                    }
                    else
                    {
                        if (link.StartNode.Id == link.EndNode.Id)
                        {
                            result.Succeed = false;
                            result.FailedList.Add(new CheckFailed()
                            {
                                ParterId = link.Id,
                                CheckType = CheckType.LinkAbnormalConnected,
                                FailReason = "管段异常连接,上游与下游节点相同"
                            });
                        }
                    }
                }
 
                //验证管道
                if (link is Pipe pipe)
                {
                    if (pipe.Length <= 0)
                    {
                        result.Succeed = false;
                        result.FailedList.Add(new CheckFailed()
                        {
                            ParterId = pipe.Id,
                            CheckType = CheckType.PropSetError,
                            FailReason = "属性设置错误,管道长度必须大于0"
                        });
                    }
                    if (pipe.Roughness <= 5 || pipe.Roughness > 10000)
                    {
                        result.Succeed = false;
                        result.FailedList.Add(new CheckFailed()
                        {
                            ParterId = pipe.Id,
                            CheckType = CheckType.PropSetError,
                            FailReason = "属性设置错误,粗糙系数区间(5,10000】"
                        });
                    }
                    if (pipe.Diameter <= 10 || pipe.Diameter > 10000)
                    {
                        result.Succeed = false;
                        result.FailedList.Add(new CheckFailed()
                        {
                            ParterId = pipe.Id,
                            CheckType = CheckType.PropSetError,
                            FailReason = "属性设置错误,管径区间(10,10000】"
                        });
                    }
                }
            }
 
            //验证连通性
            var objs = network.GetAllLinks().ToList();
            objs = objs.Distinct().ToList();
            var visitedNodes = new HashSet<Node>();
            var FindObjs = new HashSet<Visual>();
            var resultObjs = new List<List<Visual>>();
            objs.ForEach(o =>
            {
                //如果o的两个端点都已经被访问过,则不再访问
                if (visitedNodes.Contains(o.StartNode) && visitedNodes.Contains(o.EndNode))
                    return;
                var list = TraversePipeNetworkALL(o, visitedNodes, FindObjs);
                if (list.Count > 0)
                    resultObjs.Add(list);
            });
            int NumErrRegion = 0;
            resultObjs.ForEach(o =>
            {
                //验证o中是否包含水池或者水库,如果不包含不通过
                if (o.Exists(x => x is Reservoir || x is Tank)) return;
                NumErrRegion++;
            });
            if (NumErrRegion > 0)
            {
                result.Succeed = false;
                result.FailedList.Add(new CheckFailed()
                {
                    ParterId = string.Empty,
                    CheckType = CheckType.Disconnected,
                    FailReason = $"管网中有{resultObjs.Count}个独立连通性区域,其中{NumErrRegion}个没有包含水池或水库"
                });
            }
 
 
 
            return result;
        }
 
        /// <summary>
        /// 遍历管网
        /// </summary>
        private static List<Visual> TraversePipeNetworkALL(Link startLink, HashSet<Node> visitedNodes, HashSet<Visual> FindObjs)
        {
            List<Visual> result = new List<Visual>();
            Queue<Link> queue = new Queue<Link>();
 
 
 
            queue.Enqueue(startLink);
            if (visitedNodes == null)
                visitedNodes = new HashSet<Node>();
            //visitedNodes.Add(startLink.StartNode);
            //visitedNodes.Add(startLink.EndNode);
 
            while (queue.Count > 0)
            {
                Link currentLink = queue.Dequeue();
                //Console.WriteLine("Traversing Link: " + currentLink.ID);
 
                foreach (var node in new Node[] { currentLink.StartNode, currentLink.EndNode })
                {
                    if (visitedNodes.Contains(node))
                    {
                        result.Add(node);
                    }
                    if (node != null && !visitedNodes.Contains(node))
                    {
                        visitedNodes.Add(node);
                        result.Add(node);
                        if (!FindObjs.Contains(node)) FindObjs.Add(node);
 
                        foreach (var link in node.Links)
                        {
                            if (!visitedNodes.Contains(link.StartNode) || !visitedNodes.Contains(link.EndNode))
                            {
                                if (!FindObjs.Contains(link)) FindObjs.Add(link);
                                queue.Enqueue(link);
                                result.Add(link);
 
                            }
                        }
                    }
                }
 
            }
            return result;
 
 
        }
 
 
    }
}