cloudflight
2024-07-06 0340a1bb6012a97de0e029b26b5c882472749050
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
using Hydro.CodeProvider;
using Hydro.CommonBase;
using Hydro.ConfigModel;
using Hydro.HydraulicModel;
using Hydro.ParrelControl;
 
using SolutionDBHelper_NS;
using System;
using System.CodeDom;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Xml.Linq;
//using WaterDistributioinManager;
using Hydro.ConfigModel;
 
using static System.Net.Mime.MediaTypeNames;
 
namespace Hydro.HydraulicHelperNS
{
    public partial class HydraulicHelper
    {
        #region 系统设置
        public string SetOption()
        {
            string result = null;
            int err = 0;
            err=epanet.setoption(HydraulicCore.Const_class.Const_Save_results_Mode.EN_NOSAVE, 0);
            if (err > 0) return ReportQuestion( err);
 
            err = epanet.setstatusreport(HydraulicCore.Const_class.Const_statuslevel.EN_NONE);
            if (err > 0) return ReportQuestion(err);
 
 
            return result;
        }
        #endregion
         
 
 
        #region 传参到模型中
        string SetValueToModel_条件(string txt="初始条件")
        {
            string result = null;
            int err = 0;
            //设置初始条件后计算
            List<variable> variables;
            if (txt=="初始条件")
                variables= param.初始条件;
            else
                variables = param.下阶段条件;
 
            variables.ForEach(v =>
            {
 
                if (result != null) return;
                List<int> list_index = new List<int>();
                if (v.modelIndex > 0 )
                {
                    list_index.Add(v.modelIndex);
                }                   
                else if (v.modelIndex == 0)
                {
                    v.Vars.ForEach(s => list_index.Add(s.modelIndex));
                }
                dynamic temp = v.LogicValue;
                if (temp == null)
                {
                    result = $"{v.logicType}[{v.Name}]报错,未获取到LogicValue\r\n报错位置:Calc_ValsInitValue=>{txt}设置-->v.LogicValue的值为空";
                    return;
                }
                for (int i = 0; i < list_index.Count; i++)
                {
                    if (!v.isArrInput)
                        err = SetItemValue(list_index[i], temp, (HydraulicInputType)v.Type);
                    else
                        err = SetItemPattern("Pattern_" + v.expressString, list_index[i], v.LogicValues, (HydraulicInputType)v.Type);
 
                    if (err > 9)
                    {
                        result = $"{v.logicType}[{v.Name}]{ReportQuestion(err)}\r\n报错位置:Calc_ValsInitValue-->{txt}设置-->SetValueToModel";
                        break;
                    }
                }
 
 
 
                /*
                 *[Cloudflight修改]2024-5-15 
                 *LogicValues不再作为EPA中的模式使用,而是作为实际值使用,每次计算时都会按照LogicValues[i]的值进行设置
                 */
                //if (v.LogicValues != null && v.LogicValues.Length > 0)
                //{
                //    for (int i = 0; i < list_index.Count; i++)
                //    {
                //        err = SetItemPattern("Pattern_" + v.expressString, list_index[i], v.LogicValues, (HydraulicInputType)v.Type);
                //        if (err > 9)
                //            result = $"Calc_ValsInitValue:{v.logicType}[{v.Name}]{ReportQuestion(err)}";
                //    }
                //}
                //else //设置模式跟设置值,只设一个
                //{
                //    dynamic temp = v.LogicValue;
                //    //if (temp == null) temp = v.expressString;
                //    if (temp == null)
                //        result = $"{v.logicType}[{v.Name}]未获取到LogicValue\r\n报错位置:Calc_ValsInitValue=>初始条件设置";
                //    else
                //    {
                //        err = SetItemValue(v.modelIndex, temp, (HydraulicInputType)v.Type);
                //        if (err > 9)
                //            result = $"{v.logicType}[{v.Name}]{ReportQuestion(err)}报错位置:Calc_ValsInitValue=>初始条件设置";
                //    }
                //}
 
                //else if (v.modelIndex==0)
                //{
                //    v.Vars.ForEach(s => s.CalcValue_Init = GetCalcValue(s));
                //}
            });
             return result;
        }
 
        string set下阶段条件()
        {
            return SetValueToModel_条件("下阶段条件");
        }
 
 
        /// <summary>
        /// 初始值计算
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public string Calc_ValsInitValue(out bool isCalc)
        {
 
            #region 初始判断
            var TypeArr = Enum.GetValues(typeof(HydraulicInputType));
            //this.param = param;
 
            string result = null;
            int err = 0;
 
            #endregion
            //处理变量
            bool flag = true;
 
            isCalc = false;
 
            variable vv = null;
 
            if ((vv = param.水量分配变量.Find(v => v.IndicatorType == "总水量")) != null && gParam.CurrentTotalDemand == -1)
            {
                int multiTimes = gParam.GlobalConfig.Ext.MultiTimes;
 
                err = epanet.setoption(HydraulicCore.Const_class.Const_Option_types.EN_DEMANDMULT, 1);
                
                //获取变量初始值
                flag = true;
 
                if (vv != null)
                {
                    if (gParam.CurrentTotalDemand == -1)
                    {
                        double demand = 0;
                        double demand1 = 0;
 
                        err = epanet.getTotalDemand(multiTimes, out demand);
                        //demand1 = vv.Vars[0].GetValue(GetCalcValue, 0, true);
 
                        gParam.CurrentTotalDemand = demand;
                        //(vv.Vars[0] as variable).Value_Set = null;
                        
 
                        Log.Add($"==============系统设置=============\r\n模型时刻[{gParam.GlobalConfig.Ext.MultiTimes}]epa计算时刻[{multiTimes}]\r\nepanet方法统计总水量:{demand}计算方法统计总水量:{demand1}\r\n总水量为{gParam.CurrentTotalDemand}");
                    }
                }
            }
 
            result=SetValueToModel_条件();
            if (result != null) return result;
 
            param.水量分配变量.ForEach(v => 
            {
                if (result != null) return;
                if ( v.IndicatorType == "总水量")
                {
                    dynamic temp = v.LogicValue;
                    //if (temp == null) temp = v.expressString;
                    if (temp == null)
                        result = $"{v.logicType}[{v.Name}]未获取到逻辑值\r\n报错位置:Calc_ValsInitValue";
                    else
                    {
                        err = SetItemValue(v.modelIndex, temp, (HydraulicInputType)v.Type);
                        if (err > 9)
                            result = $"总水量设置错误:{v.logicType}[{v.Name}]{ReportQuestion(err)}\r\n报错位置:Calc_ValsInitValue";
                    }
 
                }
             
                //else if (v.IndicatorType == "当量分配")
                //{
                //    try
                //    {
                //        gParam.GlobalConfig.Ext.Distribution_UnitDemands = v.LogicPattern.Select(s => (float)s).ToArray();
                //        gParam.GlobalConfig.Ext.waterdistributeMode = 0;
                //    }
                //    catch
                //    {
                //        result = $"{v.logicType}(当量分配)[{v.Name}]{v.expressType}分配表达式格式错误";
                //        return;
                //    }
                //}
                //else if (v.IndicatorType == "随机分配")
                //{
                //    try
                //    {
                //        gParam.GlobalConfig.Ext.Distribution_OverAvgFactor = (float)v.LogicValue;
                //        gParam.GlobalConfig.Ext.waterdistributeMode = 1;
                //    }
                //    catch
                //    {
                //        result = $"{v.logicType}(随机分配)[{v.Name}]{v.expressType}水量分配表达式格式错误";
                //        return;
                //    }
                //}
              
            });
            if (result != null) return result;
 
 
            //if ((vv = param.初始条件.Find(v => v.IndicatorType == "总水量")) != null && gParam.CurrentTotalDemand == -1 || param.变量全集.FindAll(v => variable.listUsedInitValue.IndexOf(v.expressType) >= 0).Count > 0)
            if (gParam.GlobalConfig.Ext.MultiTimes>=0 && gParam.GlobalConfig.Ext.isWaterModelCalc && param.变量全集.FindAll(v => variable.listUsedInitValue.IndexOf(v.expressType) >= 0).Count > 0 )
            {
                //BookMark    :2024年5月21日初始计算值的实现,目前只考虑了单时刻计算,若需要多时刻计算,需要重新设计
                int CurrentEPAPeriod = -1;
                isCalc = true;
                //计算
                int multiTimes = gParam.GlobalConfig.Ext.MultiTimes;
                EPACalc(param, null, multiTimes,ref CurrentEPAPeriod);
 
                param.约束变量.FindAll(m => variable.listUsedInitValue.IndexOf(m.expressType) >= 0).ForEach(v =>
                {
                    v.CalcValue_Init = GetModelValue(v); 
                });
 
                param.变量.FindAll(m => variable.listUsedInitValue.IndexOf(m.expressType) >= 0).ForEach( v => 
                {
                     v.CalcValue_Init = GetModelValue(v);
                });
                //集合不再实现初始计算值
                param.集合.FindAll(m => variable.listUsedInitValue.IndexOf(m.expressType) >= 0).ForEach(v =>
                {
                    v.CalcValue_Init = GetModelValue(v);
                    //s.Vars.ForEach(v =>
                    //{
                    //    v.Type = s.Type;
                    //    v.isNode = s.isNode;
                    //    v.CalcValue_Init = GetCalcValue(v);
                    //});
                });
            }
            return null;
 
        }
 
        /// <summary>
        /// 将配置值和SCADA值设定到模型中
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public string SetVarsToModel(bool isInitVarSetted = false)
        {
 
            #region 初始判断
 
           
 
            var TypeArr = Enum.GetValues(typeof(HydraulicInputType));
            //this.param = param;
 
            string result = null;
            int err = 0;
 
            #endregion
 
            //result=SetOption();
            //if (result != null) return result;
 
 
            param.水量分配变量.ForEach(v => 
            {
                if (result != null) return;
                if ( v.IndicatorType == "总水量")
                {
                    dynamic temp = v.LogicValue;
                    if (temp == null) temp = v.expressString;
                    err = SetItemValue(v.modelIndex, temp, (HydraulicInputType)v.Type);
                    if (err > 9)
                        result = $"总水量设置错误:{v.logicType}[{v.Name}]{ReportQuestion(err)}";
                }
                //else if (v.IndicatorType == "总分配水量")
                //{
                //    dynamic temp = v.LogicValue;
                //    if (temp == null) temp = v.expressString;
                //    err = SetItemValue(v.modelIndex, temp, (HydraulicInputType)v.Type);
                //    if (err > 9)
                //        result = $"总分配水量设置错误:{v.logicType}[{v.Name}]{ReportQuestion(err)}";
                //}
            });
            if (result != null) return result;
 
 
            if (!isInitVarSetted) result = SetValueToModel_条件();
            if (result != null) return result;
            #region 抽取成方法
            //param.初始条件.ForEach(v =>
            //{           
            //    if (result != null) return;
            //    if (v.modelIndex > 0)
            //    {
            //        if (v.LogicValues != null && v.LogicValues.Length > 0)
            //        {
            //            err = SetItemPattern(v.modelObjectID, v.LogicValues, (HydraulicInputType)v.Type);
            //            if (err > 9)
            //                result = $"SetVarsToModel:{v.logicType}[{v.Name}]{ReportQuestion(err)}";
            //        }
            //        else //设置模式跟设置值,只设一个
            //        {
            //            dynamic temp = v.LogicValue;
            //            if (temp == null) temp = v.expressString;
            //            err = SetItemValue(v.modelIndex, temp, (HydraulicInputType)v.Type);
            //            if (err > 9)
            //                result = $"SetVarsToModel:{v.logicType}[{v.Name}]{ReportQuestion(err)}";
            //        }
            //    }
            //});
            //if (result != null) return result;
 
 
            //param.下阶段条件.ForEach(v =>
            //{
            //    if (v.modelIndex > 0)
            //    {
            //        if (v.LogicValues != null && v.LogicValues.Length > 0)
            //        {
            //            err = SetItemPattern(v.modelObjectID, v.LogicValues, (HydraulicInputType)v.Type);
            //            if (err > 9) result = $"SetVarsToModel:{v.logicType}[{v.Name}]{ReportQuestion(err)}";
            //        }
            //        else //设置模式跟设置值,只设一个
            //        {
            //            dynamic temp = v.LogicValue;
            //            if (temp == null) temp = v.expressString;
            //            err = SetItemValue(v.modelIndex, temp, (HydraulicInputType)v.Type);
            //            if (err > 9) result = $"SetVarsToModel:{v.logicType}[{v.Name}]{ReportQuestion(err)}";
            //        }
            //    }
 
            //});
            #endregion
 
            result = set下阶段条件();
            if (result != null) return result;
            return null;
        }
 
        
 
        #endregion
    }
}