qinjie
2023-12-19 15d15d24fbccb9b70a305b46b71453b2ab1a720e
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
using MathNet.Numerics.LinearAlgebra.Double; // 引入 Math.NET Numerics 库进行多项式拟合
using MathNet.Numerics;
using MathNet.Numerics.LinearAlgebra;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms.DataVisualization.Charting;
using CommonBase;
 
namespace Hydro.MapView
{
    public class Dataset
    {
        public static List<string> listString = new List<string>() { "流量扬程曲线", "流量功率曲线", "流量效率曲线" };
        public Dataset(string name, PumpViewModel pump, int Degree = 2)
        {
            Name = name;
            this.pump = pump;
            this.degree = Degree;
        }
 
        public override string ToString()
        {
            StringBuilder txt = new StringBuilder();
            foreach (PointF point in _data)
            {
                txt.AppendLine($"{Name}\t{point.X}\t{point.Y}");
            }
            return txt.ToString();
        }
        //public Dataset()
        //{
 
        //}
        public Color Color
        {
            get
            {
                switch (Name)
                {
                    case "流量扬程曲线":
                        return Color.Blue;
                        //break;
                    case "流量功率曲线":
                        return Color.Orange;
                        //break;
                    case "流量效率曲线":
                        return Color.Red;
                        //break;
                    default:
                        return Color.Blue;
                }
 
            }
 
        }
        public string Name { get; set; }
 
 
        public List<PointF> _data = new List<PointF>();
 
        int FitTimes = 2;
 
        public List<PointF> Data
        {
            get
            {
 
                if (pump == null || pump.额定转速 == 0) return _data;
                float eg = (float)pump.当前转速 / (float)pump.额定转速;
                if (eg >= 1) return _data;
                else
                {
                    switch (Name)
                    {
                        case "流量扬程曲线":
                            return _data.Select(p => { p.X *= eg; p.Y *= eg * eg; return p; }).ToList();
                            break;
                        case "流量功率曲线":
                            return _data.Select(p => { p.X *= eg; p.Y *= eg * eg * eg; return p; }).ToList();
                            break;
                        case "流量效率曲线":
                            var c1 = pump.Datasets["流量扬程曲线"];
                            var c2 = pump.Datasets["流量功率曲线"];
                            if (c1.IsFitted && c2.IsFitted && c1.Data.Count == c2.Data.Count)
                            {
                                List<PointF> points = new List<PointF>();
                                for (int i = 0; i < c1.Data.Count; i++)
                                {
                                    float x = c1.Data[i].X;
                                    float y = x * c1.Data[i].Y / c2.Data[i].Y / 3.6f;
                                    points.Add(new PointF(x, y));
 
                                    //功率=流量* 扬程 / 效率 / 3.6f
                                    //效率=流量* 扬程 / 功率 / 3.6f
                                }
                                return points;
                            }
                            else
                            {
                                return _data;
                            }
 
                            break;
                        default:
                            return _data;
                    }
 
 
 
                }
            }
            set
            {
                //float eg = (float)pump.当前转速 / (float)pump.额定转速;
                //if (eg < 1)
                //{
                //    MessageBox.Show("非额定转速,拒绝修改");
                //    return;
                //}
                IsFitted = false;
                _data = value;
            }
        }
 
        public int degree { get { return FitTimes; } set { if (FitTimes != value) this.IsFitted = false; FitTimes = value; } }
 
        public List<double> ForumParams = null;
 
        public DenseVector coefficients = null; // 存储多项式系数
 
        public static double ErrNum { get { return -1; } }
 
        public static double stepNum { get { return 800; } }
 
        private double step { get { return (range_X.Max - range_X.Min) / stepNum; } }
 
 
        [NonSerialized]
        public PumpViewModel pump = null;
 
 
 
        /// <summary>
        /// 不需要读取
        /// </summary>
        public string Legend
        {
            get
            {
                switch (Name)
                {
                    case "流量扬程曲线":
                        return "Legend1";
                    case "流量功率曲线":
                        return "Legend1";
                    case "流量效率曲线":
                        return "Legend1";
 
                }
                return "Legend1";
            }
        }
 
        public AxisType axisType
        {
            get
            {
                switch (Name)
                {
                    case "流量扬程曲线":
                        return AxisType.Primary;
                    default:
                        return AxisType.Secondary;
 
                }
            }
        }
 
        public void CurveFit()
        {
 
            if (!HasData)
            {
                IsFitted = false;
                return;
            }
 
            coefficients = MathNet.Numerics.Fit.Polynomial(Data.Select(p => (double)p.X).ToArray(), Data.Select(p => (double)p.Y).ToArray(), degree);
 
            if (range_X == null)
            {
                range_X = new Range(double.MaxValue, double.MinValue);
                Data.ForEach(p => {
                    if (range_X.Min > p.X) range_X.Min = p.X;
                    if (range_X.Max < p.X) range_X.Max = p.X;
                });
            }
            IsFitted = true;
        }
        public double Evaluate(double x)
 
        {
            if (!IsFitted) return ErrNum;
            // 计算多项式在点 x 处的取值
            double y = 0;
            for (int i = 0; i < coefficients.Count; i++)
            {
                y += coefficients[i] * Math.Pow(x, i);
            }
            return y;
        }
 
 
        // 定义 solve 方法来解多项式方程
        //double Solve(double y)
        //{
        //    FittedCurve
        //}
        double Solve(double y)
        {
            List<PointF> points = FittedCurve.ToList();
            PointF prevPoint = PointF.Empty;
            PointF nextPoint = PointF.Empty;
 
            foreach (var point in points)
            {
                if (point.Y == y)
                {
                    return point.X;
                }
                else if (point.Y > y)
                {
                    nextPoint = point;
                }
                else
                {
                    prevPoint = point;
                    break;
                }
            }
 
            if (prevPoint.IsEmpty)
            {
                return points[points.Count - 1].X; //throw new Exception("No valid interpolation range found.");
            }
            else if (nextPoint.IsEmpty)
            {
                return points[0].X;
            }
 
            double xDiff = nextPoint.X - prevPoint.X;
            double yDiff = nextPoint.Y - prevPoint.Y;
            double slope = yDiff / xDiff;
            double x = prevPoint.X + (y - prevPoint.Y) / slope;
            if (double.IsNaN(x)) return ErrNum;
            else
                return x;
        }
 
        public double Evaluate_Solve(double y)
        {
            if (!IsFitted) return ErrNum;
            if (degree == 2) // 多项式次数
            {
                if (!is_Yvalue_validate(y)) return ErrNum;
 
 
                double a = coefficients[degree]; // 二次项系数
                double b = coefficients[degree - 1]; // 一次项系数
                double c = coefficients[0] - y; // 常数项系数减去给定的 y 值
 
                // 求解二次方程 ax^2 + bx + c = 0
                double delta = b * b - 4 * a * c;
 
                double x1 = (-b + Math.Sqrt(delta)) / (2 * a);
                double x2 = (-b - Math.Sqrt(delta)) / (2 * a);
 
                // 取两个解中符合实际情况的那个(可以根据具体应用场景来确定)
                double x = (x2 >= x1) ? x2 : x1;
                if (double.IsNaN(x)) return ErrNum;
                else
                    return x;
            }
            else
            {
                return Solve(y);
            }
        }
 
 
        public string getForum
        {
            get
            {
                if (coefficients == null) return null;
                // 生成多项式的字符串表示形式
                string polyStr = "y=";
                for (int i = coefficients.Count - 1; i >= 0; i--)
                {
                    if (coefficients[i] == 0) continue;
                    if (i == coefficients.Count - 1)
                    {
                        polyStr += coefficients[i].ToString() + "x^" + i;
                    }
                    else if (i == 1)
                    {
                        if (coefficients[i] > 0)
                        {
                            if (polyStr != "") polyStr += " + ";
                            polyStr += coefficients[i].ToString() + "x";
                        }
                        else
                        {
                            polyStr += " - " + (-coefficients[i]).ToString() + "x";
                        }
                    }
                    else if (i == 0)
                    {
                        if (coefficients[i] > 0)
                        {
                            if (polyStr != "") polyStr += " + ";
                            polyStr += coefficients[i].ToString();
                        }
                        else
                        {
                            polyStr += " - " + (-coefficients[i]).ToString();
                        }
                    }
                    else
                    {
                        if (coefficients[i] > 0)
                        {
                            if (polyStr != "") polyStr += " + ";
                            polyStr += coefficients[i].ToString() + "x^" + i;
                        }
                        else
                        {
                            polyStr += " - " + (-coefficients[i]).ToString() + "x^" + i;
                        }
                    }
                }
                if (polyStr == "") polyStr = "0";
 
                if (degree == 2)
                {
                    double a = coefficients[degree];
                    double b = coefficients[degree - 1];
                    double c = coefficients[0];
                    double delta = b * b - 4 * a * c;
                    double minExtremePoint = -b / (2 * a);    // 计算导函数的零点
                    polyStr += $" maxX:{minExtremePoint.ToString("0.0")}";
                }
 
                return polyStr;
            }
 
        }
        public double[] getForumNum
        {
            get
            {
                if (coefficients == null) return null;
                List<double> list = new List<double>();
                for (int i = coefficients.Count - 1; i >= 0; i--)
                {
                    list.Add(coefficients[i]);
                }
                return list.ToArray();
            }
 
        }
        public double YMax
        {
            get
            {
                if (!IsFitted) return ErrNum;
                double a = coefficients[degree];
                double b = coefficients[degree - 1];
                double c = coefficients[0];
                double delta = b * b - 4 * a * c;
                double minExtremePoint = -b / (2 * a);    // 计算导函数的零点
                double maxValue = 0;
                if (a < 0)
 
                    maxValue = -delta / (4 * a) + c;
                else
                    maxValue = a * minExtremePoint * minExtremePoint + b * minExtremePoint + c;    // 计算最小值
                return maxValue;
            }
        }
        public double YdataMax
        {
            get
            {
                double max = -99999;
                foreach (var p in Data)
                {
                    if (max < p.Y) max = p.Y;
                }
                return max;
            }
        }
        public bool is_Yvalue_validate(double y)
        {
            if (!IsFitted) return false;
            double a = coefficients[degree];
            if (a < 0)
 
                return y <= YMax;
            else
                return y >= YMax;
        }
 
        /// <summary>
        /// X显示的范围
        /// </summary>
        public Range range_X;
 
 
        /// <summary>
        /// Y显示的范围
        /// </summary>
        public Range range_Y;
 
 
 
 
        public bool HasData { get { return Data.Count >= 3; } }
 
        public bool IsFitted = false;
        public List<PointF> FittedCurve
        {
            get
            {
                if (range_X == null) return null;
                List<PointF> doubles = new List<PointF>();
                for (double x = range_X.Min; x <= range_X.Max; x += step)
                {
                    double y = Evaluate(x);
                    //chart.Series[1].Points.AddXY(x, y);
                    doubles.Add(new PointF((float)x, (float)y));
                }
                return doubles;
 
            }
 
        }
        public List<PointF> FittedCurvebyRange(Range range)
        {
            List<PointF> doubles = new List<PointF>();
 
            if (range == null)
            {
                range = new Range();
                range.Min = range_X.Min;
                range.Max = range_X.Max;
            }
            var step = (range.Max - range.Min) / stepNum;
            for (double x = range.Min; x <= range.Max; x += step)
            {
                double y = Evaluate(x);
                //chart.Series[1].Points.AddXY(x, y);
                doubles.Add(new PointF((float)x, (float)y));
            }
            return doubles;
        }
 
 
    }
}