From fe5bb447bbc9d325246cf99b980556af62955254 Mon Sep 17 00:00:00 2001
From: lx <ex_lixiang17@cosl.com.cn>
Date: 星期五, 18 七月 2025 17:32:19 +0800
Subject: [PATCH] plot

---
 ErrorAnalysis.UI/ErrorAnalysis.UI.csproj    |    4 +
 ErrorAnalysis.UI/FrmMain.cs                 |  154 +++++++++++++++++++++++++++++++++-----
 ErrorAnalysis.Service/COMergeCalcService.cs |    2 
 ErrorAnalysis.UI/FrmMain.Designer.cs        |   19 +++-
 ErrorAnalysis.UI/Utility/UnitConvert.cs     |    6 +
 5 files changed, 156 insertions(+), 29 deletions(-)

diff --git a/ErrorAnalysis.Service/COMergeCalcService.cs b/ErrorAnalysis.Service/COMergeCalcService.cs
index c8c4645..b19edce 100644
--- a/ErrorAnalysis.Service/COMergeCalcService.cs
+++ b/ErrorAnalysis.Service/COMergeCalcService.cs
@@ -34,7 +34,7 @@
 
             var length = readSw == 0 ? _cWinEndIndex - _cWinStartIndex + 1 : _oWinEndIndex - _oWinStartIndex + 1;
             var originC = cArr.Skip(readSw == 0 ? _cWinStartIndex : _oWinStartIndex).Take(length).Sum();
-            var coef = originC * 1e8 * 2.54 * 2.54 * 4 * 6 * 2 * 11 * 77 * 0.2 / 0.6 * speed * depth * 0.07;
+            var coef = originC * 1e8 * 2.54 * 2.54 * 4 * 6 * 2 * 11 * 77 * 0.2 / 0.6 / speed * depth * 0.07;
             result = coef;
             if (sw > 0 && sw < 100)
             {
diff --git a/ErrorAnalysis.UI/ErrorAnalysis.UI.csproj b/ErrorAnalysis.UI/ErrorAnalysis.UI.csproj
index ca170d0..dd5f59b 100644
--- a/ErrorAnalysis.UI/ErrorAnalysis.UI.csproj
+++ b/ErrorAnalysis.UI/ErrorAnalysis.UI.csproj
@@ -9,6 +9,10 @@
   </PropertyGroup>
 
   <ItemGroup>
+    <PackageReference Include="OxyPlot.WindowsForms" Version="2.2.0" />
+  </ItemGroup>
+
+  <ItemGroup>
     <ProjectReference Include="..\ErrorAnalysis.Repository\ErrorAnalysis.Repository.csproj" />
     <ProjectReference Include="..\ErrorAnalysis.Service\ErrorAnalysis.Service.csproj" />
   </ItemGroup>
diff --git a/ErrorAnalysis.UI/FrmMain.Designer.cs b/ErrorAnalysis.UI/FrmMain.Designer.cs
index 070ee79..c5ddbc4 100644
--- a/ErrorAnalysis.UI/FrmMain.Designer.cs
+++ b/ErrorAnalysis.UI/FrmMain.Designer.cs
@@ -229,6 +229,7 @@
             // nudSoPrecisionRequired
             // 
             nudSoPrecisionRequired.DecimalPlaces = 1;
+            nudSoPrecisionRequired.Increment = new decimal(new int[] { 2, 0, 0, 0 });
             nudSoPrecisionRequired.Location = new Point(185, 43);
             nudSoPrecisionRequired.Name = "nudSoPrecisionRequired";
             nudSoPrecisionRequired.Size = new Size(54, 23);
@@ -247,7 +248,7 @@
             // 
             pnlLockPass.Controls.Add(nudPass);
             pnlLockPass.Controls.Add(label23);
-            pnlLockPass.Location = new Point(185, 186);
+            pnlLockPass.Location = new Point(183, 186);
             pnlLockPass.Name = "pnlLockPass";
             pnlLockPass.Size = new Size(362, 52);
             pnlLockPass.TabIndex = 11;
@@ -294,6 +295,7 @@
             nudSpeed.DecimalPlaces = 1;
             nudSpeed.Increment = new decimal(new int[] { 1, 0, 0, 65536 });
             nudSpeed.Location = new Point(54, 16);
+            nudSpeed.Maximum = new decimal(new int[] { 9999999, 0, 0, 0 });
             nudSpeed.Name = "nudSpeed";
             nudSpeed.Size = new Size(72, 23);
             nudSpeed.TabIndex = 12;
@@ -529,6 +531,7 @@
             nudBHSalinity.DecimalPlaces = 1;
             nudBHSalinity.Increment = new decimal(new int[] { 5, 0, 0, 65536 });
             nudBHSalinity.Location = new Point(143, 26);
+            nudBHSalinity.Maximum = new decimal(new int[] { 99999999, 0, 0, 0 });
             nudBHSalinity.Name = "nudBHSalinity";
             nudBHSalinity.Size = new Size(55, 23);
             nudBHSalinity.TabIndex = 16;
@@ -601,6 +604,7 @@
             nudOilDensity.DecimalPlaces = 2;
             nudOilDensity.Increment = new decimal(new int[] { 1, 0, 0, 131072 });
             nudOilDensity.Location = new Point(142, 56);
+            nudOilDensity.Maximum = new decimal(new int[] { 99999999, 0, 0, 0 });
             nudOilDensity.Name = "nudOilDensity";
             nudOilDensity.Size = new Size(56, 23);
             nudOilDensity.TabIndex = 16;
@@ -610,6 +614,7 @@
             nudGasDensity.DecimalPlaces = 2;
             nudGasDensity.Increment = new decimal(new int[] { 1, 0, 0, 131072 });
             nudGasDensity.Location = new Point(142, 87);
+            nudGasDensity.Maximum = new decimal(new int[] { 9999999, 0, 0, 0 });
             nudGasDensity.Name = "nudGasDensity";
             nudGasDensity.Size = new Size(56, 23);
             nudGasDensity.TabIndex = 16;
@@ -960,6 +965,7 @@
             nudReservoirSalinity.DecimalPlaces = 1;
             nudReservoirSalinity.Increment = new decimal(new int[] { 5, 0, 0, 65536 });
             nudReservoirSalinity.Location = new Point(146, 118);
+            nudReservoirSalinity.Maximum = new decimal(new int[] { 99999999, 0, 0, 0 });
             nudReservoirSalinity.Name = "nudReservoirSalinity";
             nudReservoirSalinity.Size = new Size(71, 23);
             nudReservoirSalinity.TabIndex = 34;
@@ -1141,23 +1147,26 @@
             // nudAlphaProcessingWin
             // 
             nudAlphaProcessingWin.Location = new Point(555, 98);
+            nudAlphaProcessingWin.Maximum = new decimal(new int[] { 99999999, 0, 0, 0 });
             nudAlphaProcessingWin.Name = "nudAlphaProcessingWin";
             nudAlphaProcessingWin.Size = new Size(47, 23);
             nudAlphaProcessingWin.TabIndex = 13;
             // 
             // nudDepth
             // 
-            nudDepth.Location = new Point(185, 71);
+            nudDepth.Location = new Point(185, 70);
+            nudDepth.Maximum = new decimal(new int[] { 99999999, 0, 0, 0 });
             nudDepth.Name = "nudDepth";
             nudDepth.Size = new Size(54, 23);
             nudDepth.TabIndex = 13;
             // 
             // nudTargetLoggingInterval
             // 
-            nudTargetLoggingInterval.DecimalPlaces = 1;
-            nudTargetLoggingInterval.Location = new Point(183, 99);
+            nudTargetLoggingInterval.DecimalPlaces = 2;
+            nudTargetLoggingInterval.Location = new Point(185, 99);
+            nudTargetLoggingInterval.Maximum = new decimal(new int[] { 99999999, 0, 0, 0 });
             nudTargetLoggingInterval.Name = "nudTargetLoggingInterval";
-            nudTargetLoggingInterval.Size = new Size(56, 23);
+            nudTargetLoggingInterval.Size = new Size(54, 23);
             nudTargetLoggingInterval.TabIndex = 13;
             // 
             // label54
diff --git a/ErrorAnalysis.UI/FrmMain.cs b/ErrorAnalysis.UI/FrmMain.cs
index 7e35121..02cbec4 100644
--- a/ErrorAnalysis.UI/FrmMain.cs
+++ b/ErrorAnalysis.UI/FrmMain.cs
@@ -1,7 +1,15 @@
 using ErrorAnalysis.Repository;
 using ErrorAnalysis.Repository.Entity;
 using ErrorAnalysis.Service;
+using ErrorAnalysis.Service.Model;
+using ErrorAnalysis.UI.Utility;
+using OxyPlot;
+using OxyPlot.Axes;
+using OxyPlot.Legends;
+using OxyPlot.Series;
+using OxyPlot.WindowsForms;
 using System.ComponentModel.DataAnnotations;
+using System.Security.Cryptography;
 using System.Windows.Forms;
 using static System.Windows.Forms.VisualStyles.VisualStyleElement.Menu;
 
@@ -50,12 +58,13 @@
                 txtDataPath.Text = fileDlg.FileName;
                 txtToolType.Text = fileDlg.SafeFileName.Substring(0, fileDlg.SafeFileName.IndexOf("Mcnp"));
                 _connectionString = $"Data Source={fileDlg.FileName};";
-                var modelRepository = new COModelRepository(_connectionString);
+                RepositoryInstance.Instance.ReLoad(_connectionString);
+
                 pbLoadDB.Value = 50;
                 _filters = new Dictionary<string, string>();
                 Task.Run(() =>
                 {
-                    _models = modelRepository.GetCOModels();
+                    _models = RepositoryInstance.Instance.COModelRepository.GetCOModels();
                     BeginInvoke(new System.Action(() =>
                     {
                         pbLoadDB.Value = 80;
@@ -103,9 +112,20 @@
                 return;
             }
 
+            if (!double.TryParse(nudSoPrecisionRequired.Value.ToString(), out double targetErrorRatio) || nudSoPrecisionRequired.Value <= 0)
+            {
+                MessageBox.Show("Please enter a reasonable so precision required");
+                return;
+            }
+
             if (cmbLithology.SelectedIndex == -1)
             {
                 MessageBox.Show("Please select a lithology");
+                return;
+            }
+            if (curModels.Count == 0)
+            {
+                MessageBox.Show("Parameters error!");
                 return;
             }
 
@@ -113,9 +133,8 @@
                 _model = curModels[0];
             _model = curModels.FirstOrDefault(m => m.GravelFillPercent == nudGravelFIllPercent.Value.ToString());
             if (_model == null)
-            {
+                _model = curModels.First();
 
-            }
             if (rdoLockSpeed.Checked)
             {
                 if (!double.TryParse(nudSpeed.Value.ToString(), out double speed) || nudSpeed.Value <= 0)
@@ -123,6 +142,10 @@
                     MessageBox.Show("Please enter a reasonable speed");
                     return;
                 }
+                var calcSpeed = cmbSpeedUnit.Text == "ft/hr" ? UnitConvert.FtHr2MMin(speed) : UnitConvert.MHR2MMin(speed);
+
+                var result = ErrorRatioCalc.GetErrorRatioResult(_model.ModelID, porosity, sw, depth, true, calcSpeed, targetErrorRatio / 100);
+                Plot(result);
 
             }
             else
@@ -171,27 +194,41 @@
             {
                 case "cmbBit":
                     cmbCasingOD.Items.Clear();
+                    _filters.Remove("CasingOD");
                     goto case "cmbCasingOD";
                 case "cmbCasingOD":
                     cmbCasingID.Items.Clear();
+                    _filters.Remove("CasingID");
                     goto case "cmbCasingID";
                 case "cmbCasingID":
                     cmbScreenOD.Items.Clear();
+                    _filters.Remove("ScreenOD");
                     goto case "cmbScreenOD";
                 case "cmbScreenOD":
                     cmbScreenID.Items.Clear();
+                    _filters.Remove("ScreenID");
                     goto case "cmbScreenID";
                 case "cmbScreenID":
                     cmbTubeOD.Items.Clear();
+                    _filters.Remove("TubeOD");
                     goto case "cmbTubeOD";
                 case "cmbTubeOD":
                     cmbTubeID.Items.Clear();
+                    _filters.Remove("TubeID");
                     break;
                 case "cmbTubeID":
                     break;
                 default:
                     break;
             }
+            cmbCasingHoldUp.Items.Clear();
+            _filters.Remove("CasingFluid");
+            cmbScreenHoldUp.Items.Clear();
+            _filters.Remove("ScreenFluid");
+            cmbTubeHoldUp.Items.Clear();
+            _filters.Remove("TubeFluid");
+            cmbLithology.Items.Clear();
+            _filters.Remove("Lithology");
 
             var unSelectedCmbs = allCmb.Where(c => c.SelectedIndex == -1).ToList();
             var curModels = GetFilterResult();
@@ -206,8 +243,10 @@
                     cmb.Enabled = false;
 
                 cmb.Items.AddRange(currentItems);
+
                 if (currentItems.Length == 1)
                     cmb.Text = currentItems[0];
+
             }
 
             LinkageModelProperty();
@@ -226,12 +265,44 @@
             else
                 _filters.Add(property, filterVal);
 
-            var curModels = GetFilterResult();
-
             var allCmb = parentControl.Controls.OfType<Control>().Where(c => c is ComboBox).Select(c => c as ComboBox).ToList();
-
+            switch (control.Name)
+            {
+                case "cmbCasingHoldUp":
+                    cmbScreenHoldUp.Items.Clear();
+                    _filters.Remove("ScreenFluid");
+                    goto case "cmbScreenHoldUp";
+                case "cmbScreenHoldUp":
+                    cmbTubeHoldUp.Items.Clear();
+                    _filters.Remove("TubeFluid");
+                    break;
+                default:
+                    break;
+            }
+            var unSelectedCmbs = allCmb.Where(c => c.SelectedIndex == -1).ToList();
+            var curModels = GetFilterResult();
             var modelType = typeof(COModelTable);
 
+            foreach (var cmb in unSelectedCmbs)
+            {
+                cmb.Items.Clear();
+                cmb.Enabled = true;
+                var cmbProperty = modelType.GetProperty(cmb.Tag.ToString());
+                var currentItems = curModels.Select(c => cmbProperty.GetValue(c).ToString() == "0" ? "water" : cmbProperty.GetValue(c).ToString() == "100" ? "oil" : cmbProperty.GetValue(c).ToString() == "200" ? "gas" : "none").Distinct().ToArray();
+                if (currentItems.Length == 0)
+                    cmb.Enabled = false;
+
+                cmb.Items.AddRange(currentItems);
+                if (_filters.ContainsKey(cmb.Tag.ToString()) && currentItems.Contains(_filters[cmb.Tag.ToString()]))
+                    cmb.Text = _filters[cmb.Tag.ToString()];
+                else
+                {
+                    _filters.Remove(cmb.Tag.ToString());
+                    if (currentItems.Length == 1)
+                        cmb.SelectedIndex = 0;
+                }
+
+            }
             //foreach (var cmb in allCmb)
             //{
             //    if (cmb.Name != control.Name)
@@ -249,17 +320,25 @@
             //}
 
             var lithology = curModels.Select(x => x.Lithology).Distinct().ToArray();
-            var curLithology = cmbLithology.Text;
             cmbLithology.Items.Clear();
             cmbLithology.Items.AddRange(lithology);
-            cmbLithology.Text = curLithology;
 
-            if (lithology.Length == 1)
-                cmbLithology.Text = lithology[0];
+            if (_filters.ContainsKey("Lithology") && lithology.Contains(_filters["Lithology"]))
+                cmbLithology.Text = _filters["Lithology"];
+            else
+                _filters.Remove("Lithology");
         }
 
         private void LinkageModelProperty()
         {
+            cmbCasingHoldUp.Items.Clear();
+            _filters.Remove("CasingFluid");
+            cmbScreenHoldUp.Items.Clear();
+            _filters.Remove("ScreenFluid");
+            cmbTubeHoldUp.Items.Clear();
+            _filters.Remove("TubeFluid");
+            cmbLithology.Items.Clear();
+            _filters.Remove("Lithology");
             var curModels = GetFilterResult();
             if (curModels.Count == 0)
                 return;
@@ -290,9 +369,9 @@
 
 
             var casingFulids = curModels.Select(x => x.CasingFluid == "0" ? "water" : x.CasingFluid == "100" ? "oil" : x.CasingFluid == "200" ? "gas" : "none").Distinct().ToArray();
+
             if (casingFulids.Length == 1 && casingFulids[0] == "999")
             {
-                cmbCasingHoldUp.Items.Clear();
                 cmbCasingHoldUp.Enabled = false;
                 nudCasingHoldUp.Enabled = false;
             }
@@ -301,14 +380,13 @@
                 cmbCasingHoldUp.Enabled = true;
                 nudCasingHoldUp.Enabled = true;
 
-                cmbCasingHoldUp.Items.Clear();
                 cmbCasingHoldUp.Items.AddRange(casingFulids);
             }
 
             var screenFulids = curModels.Select(x => x.ScreenFluid == "0" ? "water" : x.ScreenFluid == "100" ? "oil" : x.ScreenFluid == "200" ? "gas" : "none").Distinct().ToArray();
+
             if (screenFulids.Length == 1 && screenFulids[0] == "none")
             {
-                cmbScreenHoldUp.Items.Clear();
                 cmbScreenHoldUp.Enabled = false;
                 nudScreenHoldUp.Enabled = false;
             }
@@ -316,15 +394,12 @@
             {
                 cmbScreenHoldUp.Enabled = true;
                 nudScreenHoldUp.Enabled = true;
-                cmbScreenHoldUp.Items.Clear();
                 cmbScreenHoldUp.Items.AddRange(screenFulids);
             }
 
             var tubeFluids = curModels.Select(x => x.TubeFluid == "0" ? "water" : x.TubeFluid == "100" ? "oil" : x.TubeFluid == "200" ? "gas" : "none").Distinct().ToArray();
             if (tubeFluids.Length == 1 && tubeFluids[0] == "none")
             {
-
-                cmbTubeHoldUp.Items.Clear();
                 cmbTubeHoldUp.Enabled = false;
                 nudTubeHoldUp.Enabled = false;
             }
@@ -332,19 +407,16 @@
             {
                 cmbTubeHoldUp.Enabled = true;
                 nudTubeHoldUp.Enabled = true;
-
-                cmbTubeHoldUp.Items.Clear();
                 cmbTubeHoldUp.Items.AddRange(tubeFluids);
             }
 
             var lithology = curModels.Select(x => x.Lithology).Distinct().ToArray();
             var curLithology = cmbLithology.Text;
-            cmbLithology.Items.Clear();
             cmbLithology.Items.AddRange(lithology);
             cmbLithology.Text = curLithology;
 
             if (lithology.Length == 1)
-                cmbLithology.Text = lithology[0];
+                cmbLithology.SelectedIndex = 0;
         }
 
         private List<COModelTable> GetFilterResult()
@@ -369,5 +441,45 @@
             else
                 _filters.Add(property, filterVal);
         }
+
+        private void Plot(ErrorRatioResult errorResult)
+        {
+            pnlCOPic.Controls.Clear();
+            var plotModel = new PlotModel();
+
+            var xAxis = new LinearAxis { Position = AxisPosition.Bottom, Title = "Porosity(%)" };
+            var yAxis = new LinearAxis { Position = AxisPosition.Left, Title = "C/O" };
+
+            plotModel.Axes.Add(xAxis);
+            plotModel.Axes.Add(yAxis);
+
+            // 创建曲线系列
+            var oSeries = new LineSeries { Title = "Water Line", Color = OxyColors.MediumBlue };
+            var cSeries = new LineSeries { Title = "Oil Line", Color = OxyColors.DarkRed };
+
+            foreach (var o in errorResult.OWOL)
+                oSeries.Points.Add(new DataPoint(o[0], o[1]));
+            foreach (var c in errorResult.CWOL)
+                cSeries.Points.Add(new DataPoint(c[0], c[1]));
+
+            plotModel.Legends.Add(new Legend { LegendPosition = LegendPosition.TopLeft });
+            // 添加曲线到图表模型
+            plotModel.Series.Add(oSeries);
+            plotModel.Series.Add(cSeries);
+
+            // 可视化代码
+            // 创建 Windows 窗体以显示图表
+            var plotView = new PlotView { Model = plotModel };
+            
+            plotView.Width = 600;
+            plotView.Height = 450;
+            var bitmap = new Bitmap(plotView.Width, plotView.Height);
+            plotView.DrawToBitmap(bitmap, new Rectangle(0, 0, bitmap.Width, bitmap.Height));
+            bitmap.Save(AppDomain.CurrentDomain.BaseDirectory + "co.png", System.Drawing.Imaging.ImageFormat.Png);
+
+            plotView.Dock = DockStyle.Fill;
+            pnlCOPic.Controls.Add(plotView);
+            
+        }
     }
 }
diff --git a/ErrorAnalysis.UI/Utility/UnitConvert.cs b/ErrorAnalysis.UI/Utility/UnitConvert.cs
index bedc825..0ebbb64 100644
--- a/ErrorAnalysis.UI/Utility/UnitConvert.cs
+++ b/ErrorAnalysis.UI/Utility/UnitConvert.cs
@@ -12,8 +12,10 @@
 
         public static double M2Ft(double val) => val * 3.281;
 
-        public static double FtHr2MMin(double val) => val / 3.281 * 60;
+        public static double FtHr2MMin(double val) => val / 3.281 / 60;
 
-        public static double MHR2MMin(double val) => val * 60;
+        public static double MHR2MMin(double val) => val /60;
+
+        public static double MMin2FtHr(double val) => val * 3.281 * 60;
     }
 }

--
Gitblit v1.9.3