tangxu
2024-07-24 741135d9c638e5ada2446a4f8013e88da2549932
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
using IStation.CalcModel;
using IStation.Model;
using System;
using System.Collections.Generic;
using System.Linq;
 
namespace IStation.Calc
{
    internal partial class ErQuCalcBaseHelper
    {
        protected int _maxOpenPumpCount = 4;//最大开泵数量
 
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public static IStation.Calc.ErQuCalcBaseHelper Build(IStation.CalcModel.AnaRequest anaRequest)
        {
            if (anaRequest.MaxReservoirLevel != null)
            {
                DateTime? limit_time = GetMaxReservoirLevelTime(anaRequest);
 
                if (limit_time == null)
                {
                    var calc = new IStation.Calc.ErQuCalcHelper_2_递归_定水位_任意时间();
                    calc.SetMaxReservoirHeight(anaRequest.MaxReservoirLevel.Value);
 
                    return calc;
                }
                else
                {
                    var calc = new IStation.Calc.ErQuCalcHelper_2_递归_定水位_指定时间();
                    calc.SetMaxReservoirHeight(anaRequest.MaxReservoirLevel.Value, limit_time.Value);
                    return calc;
                }
            }
            else
            {
                return new IStation.Calc.ErQuCalcHelper_2_递归_定水量();
            }
        }
 
        private static DateTime? GetMaxReservoirLevelTime(IStation.CalcModel.AnaRequest anaRequest)
        {
            if (string.IsNullOrEmpty(anaRequest.MaxReservoirLevelTime))
            {
                return null;
            }
 
            DateTime time;
            if (DateTime.TryParse(anaRequest.MaxReservoirLevelTime, out time))
            {
                return time;
            }
 
            if (anaRequest.MaxReservoirLevelTime.Contains(":"))
            {
                var sss = anaRequest.MaxReservoirLevelTime.Split(':');
                if (sss.Count() == 2)
                {
                    int hour = 0;
                    int miut = 0;
                    if (int.TryParse(sss[0], out hour) && int.TryParse(sss[1], out miut))
                    {
                        if (hour < anaRequest.StartTime.Hour)
                            return new DateTime(anaRequest.EndTime.Year, anaRequest.EndTime.Month, anaRequest.EndTime.Day, hour, miut, 0);
                        else
                            return new DateTime(anaRequest.StartTime.Year, anaRequest.StartTime.Month, anaRequest.StartTime.Day, hour, miut, 0);
                    }
                }
            }
            else
            {
                int hour = 0;
                if (int.TryParse(anaRequest.MaxReservoirLevelTime, out hour))
                {
                    if (hour < anaRequest.StartTime.Hour)
                        return new DateTime(anaRequest.EndTime.Year, anaRequest.EndTime.Month, anaRequest.EndTime.Day, hour, 0, 0);
                    else
                        return new DateTime(anaRequest.StartTime.Year, anaRequest.StartTime.Month, anaRequest.StartTime.Day, hour, 0, 0);
                }
            }
 
            return null;
        }
 
        /// <summary>
        ///
        /// </summary>
        public class RunBlock
        {
            public int PumpCount;
            public int StartIndx;
            public int EndIndx;
            public double TotalCompare;
            public double TotalFlow;
            public double ReservoirStartHeight;//水库水位
            public double ReservoirEndHeight;
        }
 
        /// <summary>
        ///
        /// </summary>
        protected ErQuCalcBaseHelper()
        {
            var calStartTime = DateTime.Now;
            _calStartTime =  new DateTime(calStartTime.Year, calStartTime.Month, calStartTime.Day, calStartTime.Hour, 0, 0);
            _calEndTime = _calStartTime.AddDays(1);
        }
 
        public void SetCalcTimeRange(DateTime calStartTime, DateTime calEndTime,int calcSpaceMinute)
        {
            if (calcSpaceMinute <= 1)
            {
                calcSpaceMinute = 10;
            }
            this._calcSpaceMinute = calcSpaceMinute;
            this._calStartTime = new DateTime(calStartTime.Year, calStartTime.Month, calStartTime.Day, calStartTime.Hour, ((int)(calStartTime.Minute/ calcSpaceMinute)) * calcSpaceMinute, 0);  
            this._calEndTime = new DateTime(calEndTime.Year, calEndTime.Month, calEndTime.Day, calEndTime.Hour, ((int)(calEndTime.Minute / calcSpaceMinute)) * calcSpaceMinute, 0);
        }
 
        //
        public Action<string> OnShowDebugInfo = null;
 
        //排序方式
        protected IStation.CalcModel.eCalcOptType _clacOptType = CalcModel.eCalcOptType.功率;
 
        /// <summary>
        ///
        /// </summary>
        protected double _stationTotalFlow = 0;//供水总量(不一定有值 单位: 吨)
 
        /// <summary>
        ///
        /// </summary>
        /// <param name="anaRequest"></param>
        public void SetAnaRequest(IStation.CalcModel.AnaRequest anaRequest)
        {
            this._start_level水库水位 = anaRequest.StartReservoirLevel;
            this._clacOptType = anaRequest.CalcOptType;
 
            if (anaRequest.IsKnownStartOpenPump)
            {
                if (anaRequest.StartOpenPumpIndexArray == null)
                {//没有赋值就是表示不知道, 如果都不开机, 赋值[]
                    this._startOpenPumpCount = -1;
                }
                else
                {
                    this._startOpenPumpArray = anaRequest.StartOpenPumpIndexArray;
                    this._startOpenPumpCount = anaRequest.StartOpenPumpIndexArray.Count;
                }
            }
            else
            {
                this._startOpenPumpCount = -1;
            }
 
            if (anaRequest.TotalFlowIn != null)
                this._stationTotalFlow = anaRequest.TotalFlowIn.Value;
 
            this.Initial水库参数(anaRequest);
 
            this.SetRiverWaterLevels(anaRequest.WaterLevels长江);
        }
 
        /// <summary>
        /// 计算最优
        /// </summary>
        /// <param name="TargetFlowTotal"></param>
        /// <param name="error_info"></param>
        /// <returns></returns>
        public virtual AnaPrj CalcOptPrj(out string error_info)
        {
            error_info = null;
 
            #region 检查数据
 
            if (this._calStartTime >= this._calEndTime)
            {
                error_info = "时间范围不对";
                return null;
            }
 
 
 
            if (this._allPumpInfo == null || this._allPumpInfo.Count() != 5)
            {
                error_info = "泵台数不正确";
                return null;
            }
            if (this._riverWaterLevels == null || this._riverWaterLevels.Count() < 1)
            {
                error_info = "长江水位未赋值";
                return null;
            }
 
            if (_listRiverWaterLevelDrop == null)
            {
                _listRiverWaterLevelDrop = IStation.AnaGlobalParas.RiverWaterLevelDropList;
                if (_listRiverWaterLevelDrop == null || _listRiverWaterLevelDrop.Count() == 0)
                {
                    error_info = "无法读取长江水位与水池落差的关系数据";
                    return null;
                }
            }
 
            #endregion 检查数据
 
            // List<AnaPrj> listPrj;
            //System.Diagnostics.Stopwatch sw2 = new System.Diagnostics.Stopwatch();
            //sw2.Start();
 
            //迭代浮动, 不要每次都从0开始, 可能差几分钟更优, 后面此变量也要迭代
            int dd_minute = 0;
            //
            CalcTimeData(dd_minute);
 
            // 真正开始分析
            var optPrj = CalcOptPrjsCore();
            //sw2.Stop();
 
            //var seconds = (int)(sw2.ElapsedMilliseconds / 1000);
            //故意加1分钟
            //if (this._calcSpaceMinute >= 5)
            //{
            //    seconds += 50;
            //    Thread.Sleep(50 * 1000);
            //}
            //if (this._calcSpaceMinute == 4)
            //{
            //    seconds += 20;
            //    Thread.Sleep(20 * 1000);
            //}
 
            if (optPrj == null)
            {
                error_info = "未找到合适的调度方案";
                return null;
            }
 
            optPrj.StartTime = this._calStartTime;
            optPrj.EndTime = this._calEndTime;
 
            // OnShowDebugInfo.Invoke(string.Format("计算用时: {0} 秒", seconds));
 
            return optPrj;
        }
 
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        protected virtual AnaPrj CalcOptPrjsCore()
        {
            return null;
        }
 
        /// <summary>
        ///
        /// </summary>
        protected ulong _totalCalcCount = 0;//计算次数
 
        protected ulong _totalCacheCount = 0;//缓存使用次数
 
        //时间范围
        protected DateTime _calStartTime;
 
        protected DateTime _calEndTime;
 
        //泵信息
        internal List<IStation.CalcModel.PumpCurveInfo> _allPumpInfo = null;
 
        public void SetPumpInfo(List<IStation.CalcModel.PumpInfo> allPumpInfo)
        {
            if (allPumpInfo == null)
                return;
 
            this._allPumpInfo = new List<CalcModel.PumpCurveInfo>();
            for (int k = 0; k < allPumpInfo.Count; k++)
            {
                var pump = allPumpInfo[k];
 
                CalcModel.PumpCurveInfo pumpCurveInfo = new CalcModel.PumpCurveInfo();
                pumpCurveInfo.ID = pump.ID;
                pumpCurveInfo.PumpIndex = k;
                pumpCurveInfo.Name = pump.Name;
                pumpCurveInfo.CurveInfo = pump.CurveInfo;
                pumpCurveInfo.RatedParas = pump.RatedParas;
 
                double spaceQ = (pump.CurveInfo.CurveQH.Max - pump.CurveInfo.CurveQH.Min) / 99;
 
                List<IStation.Model.GroupPoint> groupPoints = new List<GroupPoint>();
                for (int i = 0; i < 100; i++)
                {
                    var q = pump.CurveInfo.CurveQH.Min + spaceQ * i;
                    var h = IStation.Common.FitCurveHelper.GetFitPointY(pump.CurveInfo.CurveQH, q);
                    double eta = 0; // IStation.Common.FitCurveHelper.GetFitPointY(pump.CurveInfo.CurveQE, q);
                    var p = IStation.Common.FitCurveHelper.GetFitPointY(pump.CurveInfo.CurveQP, q);
 
                    groupPoints.Add(new GroupPoint() { Q = q, H = h, E = eta, P = p });
                }
                pumpCurveInfo.GroupPoints = groupPoints;
 
                _allPumpInfo.Add(pumpCurveInfo);
            }
        }
 
        //计算时间跨度
        protected int _calcSpaceMinute = 5;
 
        public int CalcSpaceMinute
        { get { return _calcSpaceMinute; } set { _calcSpaceMinute = value; } }
 
        /// <summary>
        /// 中间最多切换次数
        /// </summary>
        protected int _maxPumpSwitchCount = 3;
 
        public int MaxPumpSwitchCount
        { get { return _maxPumpSwitchCount; } set { _maxPumpSwitchCount = value; } }
 
        /// <summary>
        /// 不许切泵时间
        /// </summary>
        protected List<IStation.CalcModel.TimeRange> _limitSwitchPumpTimes = null;
 
        public List<IStation.CalcModel.TimeRange> LimitSwitchPumpTimes
        {
            get { return _limitSwitchPumpTimes; }
            set
            {
                _limitSwitchPumpTimes = value;
            }
        }
 
        /// <summary>
        /// 不许开泵时间
        /// </summary>
        protected List<IStation.CalcModel.TimeRange> _limitOpenPumpTimes = null;
 
        public List<IStation.CalcModel.TimeRange> LimitOpenPumpTimes
        {
            get { return _limitOpenPumpTimes; }
            set
            {
                _limitOpenPumpTimes = value;
            }
        }
 
        #region 泵出口压力
 
        //private void InitialPumpOutletPress()
        //{
        //    _outBoxLevels = new List<Model.TimeWaterLevel>();
        //    _outBoxLevels.Add(new Model.TimeWaterLevel() { Level = 0.068 * 100 });
        //}
 
        ///// <summary>
        ///// 出水水池水位
        ///// </summary>
        //protected List<IStation.Model.TimeWaterLevel> _outBoxLevels = null;
 
        //暂时固定
        protected double CalcPumpOutletPressM(DateTime time)
        {//返回m的单位数值
            return 0.068 * 100;
            //return _outBoxLevels.First().Level;
        }
 
        #endregion 泵出口压力
 
        /// <summary>
        /// 获取扬程(暂时简单处理)
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
        internal double GetPipeHead(DateTime time, TimeDataBundle timeDataBundle)
        {
            int openPumpCount = timeDataBundle.OpenPumpInfos.Count;
            var m2 = CalcPumpOutletPressM(time);
            var m1 = GetRiverWaterLevelByTime(time);
            double dropHeight = 0;
            if (openPumpCount >= 4)
            {
                dropHeight = 2.5;
            }
            else if (openPumpCount == 3)
            {
                dropHeight = 1.54;
            }
            else if (openPumpCount == 3)
            {
                dropHeight = 0.81;
            }
            else if (openPumpCount == 2)
            {
                dropHeight = 0.32;
            }
            //double totalFlow = 0;
            //double dropHeight = _listRiverWaterLevelDrop.Last().DropHeight;
            //if (totalFlow < _listRiverWaterLevelDrop.Last().Flow)
            //{
            //    for (int i = 0; i < _listRiverWaterLevelDrop.Count; i++)
            //    {
            //        if (totalFlow < _listRiverWaterLevelDrop[i].Flow)
            //        {
            //            dropHeight = _listRiverWaterLevelDrop[i].DropHeight; break;
            //        }
            //    }
            //}
 
            m1 = m1 - dropHeight;
 
            return (m2 - m1);
        }
 
        //电费
        protected List<ElecPriceMonthSetting> _elecPrice = null;
 
        public List<ElecPriceMonthSetting> ElecPrice { get => _elecPrice; set => _elecPrice = value; }
 
        public double CalcMoney(DateTime time, double power)
        {
            var month = time.Month;
            var hour = time.Hour;
            var monthSettings = _elecPrice.Find(x => month >= x.StartMonth && month <= x.EndMonth);
            var hourList = monthSettings.HourList;
            //if (hour == 23)
            //{
            //    var item = hourList.Find(x => x.EndHour == 23);
            //    return power * item.Price;
            //}
            //else
            {
                var item = hourList.Find(x => hour >= x.StartHour && hour < x.EndHour);
                return power * item.Price;
            }
        }
 
        /// <summary>
        /// 最少开泵时间
        /// </summary>
        public int MinOpenPumpMinute = 30;
 
        protected int _minTimeCountOpenPump = 0;
        public int MinSwitchPumpMinute = 30;
        protected int _minTimeCountSwitch = 0;
 
        /// <summary>
        /// 初始化开机台数
        /// </summary>
        protected int _startOpenPumpCount = -1;
 
        protected List<int> _startOpenPumpArray = null;
 
        /// <summary>
        ///
        /// </summary>
        protected bool isDispDebug = false;
 
        public bool IsDispDebug { get => isDispDebug; set => isDispDebug = value; }
 
        /// <summary>
        ///
        /// </summary>
        /// <param name="prj"></param>
        /// <returns></returns>
        protected bool CheckPrjItemTimeAble(List<AnaPrjBlockTime> time_items)
        {
            if (time_items == null)
                return false;
            //if (time_items.Count == 1)
            //{
            //    for (int i = 0; i < 5; i++)
            //    {
            //        if (time_items[0].OpenPumpIndexs.Contains(i))
            //        {
            //            if ((time_items[0].EndTime - time_items[0].StartTime).TotalMinutes < MinOpenPumpMinute)
            //                return false;
            //        }
            //    }
            //}
 
            //if (time_items.Count == 2)
            //{
            //    for (int i = 0; i < 5; i++)
            //    {
            //        if (time_items[0].OpenPumpIndexs.Contains(i) && time_items[1].OpenPumpIndexs.Contains(i))
            //        {
            //            if ((time_items[1].EndTime - time_items[0].StartTime).TotalMinutes < MinOpenPumpMinute)
            //                return false;
            //        }
            //        if (time_items[0].OpenPumpIndexs.Contains(i) && !time_items[1].OpenPumpIndexs.Contains(i))
            //        {
            //            if ((time_items[0].EndTime - time_items[0].StartTime).TotalMinutes < MinOpenPumpMinute)
            //                return false;
            //        }
            //        if (!time_items[0].OpenPumpIndexs.Contains(i) && time_items[1].OpenPumpIndexs.Contains(i))
            //        {
            //            if ((time_items[1].EndTime - time_items[1].StartTime).TotalMinutes < MinOpenPumpMinute)
            //                return false;
            //        }
            //    }
            //}
 
            return true;
        }
    }
}