duheng
2025-03-28 dfe7e1653f8309e23e4c314cd58ac4ff7ce49dbc
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
namespace PBS.WinFrmUI.Hydro
{
    public partial class SystemCurvePage : DocumentPage
    {
        public SystemCurvePage()
        {
            InitializeComponent();
            this.facilityTreeListCtrl1.SelectFacilityEvent += FacilityTreeListCtrl1_SelectFacilityEvent;
            this.facilityTreeListCtrl1.RefreshDataEvent += () => { RefreshData(); };
            this.systemCurveChartCtrl1.RefreshCurveEvent += (u, l, a) =>
            {
                _curveUpperPressure = u;
                _curveLowerPressure = l;
                _curveAveragePressure = a;
            };
             
            
        }
 
 
        private FacilityVmo _facility = null;
 
        private Yw.Ahart.CubicCurve _curveUpperPressure = null;
        private Yw.Ahart.CubicCurve _curveLowerPressure = null;
        private Yw.Ahart.CubicCurve _curveAveragePressure = null;
 
        /// <summary>
        /// 初始化数据源
        /// </summary>
        public override async void InitialDataSource()
        {
            base.InitialDataSource();
 
            var allFacilityList = await BLLFactory<BLL.Facility>.Instance.GetAll();
            this.facilityTreeListCtrl1.SetBindingData(allFacilityList);
 
        }
 
        //选择设施
        private void FacilityTreeListCtrl1_SelectFacilityEvent(FacilityVmo obj)
        {
            InitialData(obj);
        }
 
        /// <summary>
        /// 初始化数据
        /// </summary> 
        public void InitialData(FacilityVmo obj)
        {
            _facility = obj;
            this.facilityPropertyCtrl1.SetBindingData(obj);
            this.systemCurveChartCtrl1.Clear();
 
        }
 
        #region 计算
         
        /// <summary>
        /// 计算系统曲线
        /// </summary>
        /// <param name="inpFilePath"></param>
        /// <param name="minDemand"></param>
        /// <param name="maxDemand"></param> 
        /// <param name="maxHeight"></param>
        /// <param name="requiredEndPressure"></param>
        /// <param name="calcCount"></param>
        private void CalcSystemCurve(
            string inpFilePath,
            double minDemand,
            double maxDemand,
            double maxHeight,
            double requiredEndPressure,
            int calcCount)
        {
            var nodeConfig = new List<WaterDistributionCalculator.NodeConfig>();
            var waterPointCalcList = new List<WaterPointCalcViewModel>(calcCount);
            using (var helper = new Yw.Epanet.InteropXHelper())
            {
                var code = helper.Open(inpFilePath, "", "");
                CheckCode(code);
                code = helper.GetCount(Yw.Epanet.eCountType.Node, out int totalNodeCount);
                CheckCode(code);
                var rand = new Random();
                for (int nodeIndex = 1; nodeIndex <= totalNodeCount; nodeIndex++)
                {
                    helper.GetNodeType(nodeIndex, out Yw.Epanet.eNodeType nodeType);
                    if (nodeType == Yw.Epanet.eNodeType.Junction)
                    {
                        code = helper.GetNodeId(nodeIndex, out string id);
                        if (!id.Contains("M"))
                        {
                            continue;
                        }
                        helper.GetNodeValue(nodeIndex, Yw.Epanet.eNodeProperty.Elevation, out double elevation);
                        nodeConfig.Add(new WaterDistributionCalculator.NodeConfig()
                        {
                            NodeIndex = nodeIndex,
                            Elevation = elevation,
                            MinFlow = 0,
                            MaxFlow = rand.NextDouble() * 5
                        });
                    }
                }
 
                var userNodeCount = nodeConfig.Count;
                var calculator = new WaterDistributionCalculator(nodeConfig, calcCount, minDemand, maxDemand);
                var allDemands = calculator.CalculateDistributions();
 
                code = helper.OpenH();
                CheckCode(code);
                for (int count = 0; count < allDemands.Count; count++)
                {
                    var calcNodelDict = new Dictionary<int, WaterPointNodeViewModel>(userNodeCount);
                    var calcEndPressure = double.MaxValue;
 
                    code = helper.InitH(false);
                    CheckCode(code);
                    var demands = allDemands[count];
                    foreach (var node in nodeConfig)
                    {
                        var userNodeIndex = node.NodeIndex;
                        double demand = demands.NodeFlows[userNodeIndex];
                        code = helper.SetNodeValue(userNodeIndex, Yw.Epanet.eNodeProperty.BaseDemand, demand);
                        CheckCode(code);
 
                        calcNodelDict[userNodeIndex] = new WaterPointNodeViewModel()
                        {
                            Index = userNodeIndex,
                            Flow = demand
                        };
                    }
 
                    code = helper.RunH(out long t);
                    CheckCode(code);
 
                    foreach (var node in nodeConfig)
                    {
                        var userNodeIndex = node.NodeIndex;
 
                        //自由压力=绝对压力-标高
                        //默认起始点压力为最低水位,所以用 绝对压力 当自由压力 (绝对压力=自由压力+标高)
                        code = helper.GetNodeValue(userNodeIndex, Yw.Epanet.eNodeProperty.Head, out double head);
                        CheckCode(code);
                        var endPressure = head;
                        calcNodelDict[userNodeIndex].Pressure = endPressure;
                        if (calcEndPressure > endPressure)
                        {
                            calcEndPressure = endPressure;
                        }
                    }
 
 
                    code = helper.NextH(out long tstep);
                    CheckCode(code);
                    var waterPointCalc = new WaterPointCalcViewModel();
                    waterPointCalc.TotalFlow = demands.TotalDemand;
                    waterPointCalc.EndPressure = Math.Abs(calcEndPressure) + maxHeight + requiredEndPressure;
                    waterPointCalc.NodeList = calcNodelDict.Values.ToList();
                    waterPointCalcList.Add(waterPointCalc);
 
                }
 
 
                code = helper.Close();
                CheckCode(code);
            }
 
            this.systemCurveChartCtrl1.SetBindingData(waterPointCalcList);
        }
 
 
        private void CheckCode(Yw.Epanet.eErrorCode code)
        {
            if (code != Yw.Epanet.eErrorCode.OK)
            {
                if ((int)code > 100)
                {
                    var msg = code.GetDisplayText();
                    //TipFormHelper.ShowError(msg);
                    throw new Exception(msg);
                }
 
            }
        }
 
        //计算
        private void barBtnCalc_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
 
            if (_facility == null)
            {
                TipFormHelper.ShowWarn("请选择设施!");
                return;
            }
 
 
            var file_path = _facility.ModelPath;
            var minDemand = 0;   // 最小总需水量(m³/h)
            var maxDemand = _facility.MaxWaterDemand ?? 45;  // 最大总需水量(m³/h) 
            var calcCount = 1000;           // 计算次数
            var maxHeight = 22.5; //顶楼标高
            var requiredEndPressure = _facility.TerminalPressure ?? 15; //静压 
            CalcSystemCurve(file_path, minDemand, maxDemand, maxHeight, requiredEndPressure, calcCount);
        }
        #endregion
 
        #region 模拟计算 
 
        //模拟计算
        private void barBtnSimulationCalc_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (_facility==null)
            {
                return;
            }
            if (_curveAveragePressure==null)
            {
                TipFormHelper.ShowWarn("请先计算曲线!");
                return;
            }
            var page = new SimulationSchedulePage();
            page.Dock = DockStyle.Fill;
            page.InitialData(_facility, _curveUpperPressure, _curveLowerPressure, _curveAveragePressure);
            var dlg = new XtraForm();
            dlg.Text = "模拟计算";
            dlg.IconOptions.Icon = Yw.WinFrmUI.GlobalParas.AppIcon;
            dlg.Controls.Add(page);
            dlg.StartPosition = FormStartPosition.CenterScreen;
            dlg.WindowState = FormWindowState.Maximized;
            dlg.ShowDialog();
 
        }
        #endregion
 
        /// <summary>
        /// 刷新数据
        /// </summary>
        public override void RefreshData()
        {
            base.RefreshData();
            InitialDataSource();
        }
 
      
    }
 
}