From cd5f2a0726bc8e756f22f1b86eb725209f1d2d94 Mon Sep 17 00:00:00 2001 From: LilDosto Date: Wed, 28 Oct 2026 18:47:11 +0300 Subject: [PATCH] =?UTF-8?q?feat(v2.4.0):=20Crank-Nicolson=20PDE,=202B=20La?= =?UTF-8?q?place,=20=C5=9Fekil=20d=C3=B6nd=C3=BCrme=20a=C3=A7=C4=B1s=C4=B1?= =?UTF-8?q?=20ve=20interaktif=20katman=20y=C3=B6netimi?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 6 +- .../Controls/GraphPlotterControl.Designer.cs | 38 +++++- .../Controls/GraphPlotterControl.cs | 102 ++++++++++----- .../Controls/PdeSolverControl.Designer.cs | 8 +- src/ttCalculator/Controls/PdeSolverControl.cs | 94 +++++++++++--- .../Core/DifferentialEquations.cs | 118 +++++++++++++++++- src/ttCalculator/MainForm.Designer.cs | 2 +- src/ttCalculator/ttCalculator.csproj | 2 +- tests/ttCalculator.Tests/Program.cs | 39 +++++- 9 files changed, 345 insertions(+), 64 deletions(-) diff --git a/README.md b/README.md index 11501b8..a14af5f 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# 📐 ttCalculator v2.3.0 +# 📐 ttCalculator v2.4.0 [![GitHub release](https://img.shields.io/github/v/release/LilDosto/ttCalculator?color=brightgreen&label=S%C3%BCr%C3%BCm)](https://github.com/LilDosto/ttCalculator/releases) [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE) @@ -15,8 +15,8 @@ Tüm matematiksel çekirdek, otomatik test paketinden **%100 başarıyla (0 hata Uygulamanın hazır derlenmiş Windows sürümünü (`ttCalculator.exe`) hemen çalıştırmak için: -1. **[Releases](https://github.com/LilDosto/ttCalculator/releases/tag/v2.3.0)** sayfasına gidin. -2. **`ttCalculator.exe`** (veya `ttCalculator-v2.3.0-win-x64.zip`) dosyasını indirin. +1. **[Releases](https://github.com/LilDosto/ttCalculator/releases/tag/v2.4.0)** sayfasına gidin. +2. **`ttCalculator.exe`** (veya `ttCalculator-v2.4.0-win-x64.zip`) dosyasını indirin. 3. Çift tıklayarak doğrudan çalıştırın — ek bağımlılık gerektirmez! --- diff --git a/src/ttCalculator/Controls/GraphPlotterControl.Designer.cs b/src/ttCalculator/Controls/GraphPlotterControl.Designer.cs index 196f6b5..24c5313 100644 --- a/src/ttCalculator/Controls/GraphPlotterControl.Designer.cs +++ b/src/ttCalculator/Controls/GraphPlotterControl.Designer.cs @@ -63,6 +63,7 @@ namespace MathApp.Controls this.cmbCurveThickness = new ComboBox(); this.btnCurveToggle = new Button(); this.btnCurveDelete = new Button(); + this.btnCurveEdit = new Button(); this.btnCurveClearAll = new Button(); // Points Tab Controls @@ -103,6 +104,8 @@ namespace MathApp.Controls this.txtShapeDim1 = new TextBox(); this.lblShapeDim2 = new Label(); this.txtShapeDim2 = new TextBox(); + this.lblShapeAngle = new Label(); + this.txtShapeAngle = new TextBox(); this.btnAddShape = ThemeHelper.CreateActionButton("📐 Şekli Grafiğe Ekle", 260, 32, true); this.pnlStatusBar = new Panel(); @@ -180,10 +183,10 @@ namespace MathApp.Controls this.txtMaxX.ForeColor = Color.White; this.txtMaxX.BorderStyle = BorderStyle.FixedSingle; - this.chkClampRange.Text = "Aralıkta Kısıtla"; + this.chkClampRange.Text = "[Min, Max] Kısıtla"; this.chkClampRange.ForeColor = ThemeHelper.LightText; this.chkClampRange.Location = new Point(452, yRow1 + 3); - this.chkClampRange.Size = new Size(110, 22); + this.chkClampRange.Size = new Size(125, 22); this.btnPlot.Location = new Point(570, yRow1 - 1); @@ -273,9 +276,16 @@ namespace MathApp.Controls this.btnCurveDelete.ForeColor = Color.White; this.btnCurveDelete.FlatStyle = FlatStyle.Flat; - this.btnCurveClearAll.Text = "🧹 Tüm Eğrileri Temizle"; - this.btnCurveClearAll.Size = new Size(284, 28); - this.btnCurveClearAll.Location = new Point(8, 300); + this.btnCurveEdit.Text = "✏️ Düzenle"; + this.btnCurveEdit.Size = new Size(138, 28); + this.btnCurveEdit.Location = new Point(8, 298); + this.btnCurveEdit.BackColor = Color.FromArgb(30, 41, 59); + this.btnCurveEdit.ForeColor = Color.White; + this.btnCurveEdit.FlatStyle = FlatStyle.Flat; + + this.btnCurveClearAll.Text = "🧹 Tümünü Sil"; + this.btnCurveClearAll.Size = new Size(138, 28); + this.btnCurveClearAll.Location = new Point(154, 298); this.btnCurveClearAll.BackColor = Color.FromArgb(45, 55, 72); this.btnCurveClearAll.ForeColor = Color.White; this.btnCurveClearAll.FlatStyle = FlatStyle.Flat; @@ -286,6 +296,7 @@ namespace MathApp.Controls this.tabFunctions.Controls.Add(this.cmbCurveThickness); this.tabFunctions.Controls.Add(this.btnCurveToggle); this.tabFunctions.Controls.Add(this.btnCurveDelete); + this.tabFunctions.Controls.Add(this.btnCurveEdit); this.tabFunctions.Controls.Add(this.btnCurveClearAll); // tabPoints @@ -530,6 +541,18 @@ namespace MathApp.Controls this.txtShapeDim2.BackColor = ThemeHelper.CardBackground; this.txtShapeDim2.ForeColor = Color.White; + this.lblShapeAngle.Text = "Dönüş (°):"; + this.lblShapeAngle.Location = new Point(190, sy2); + this.lblShapeAngle.AutoSize = true; + this.lblShapeAngle.ForeColor = ThemeHelper.LightText; + + this.txtShapeAngle.Location = new Point(190, sy2 + 20); + this.txtShapeAngle.Size = new Size(60, 24); + this.txtShapeAngle.Text = "0"; + this.txtShapeAngle.Font = new Font("Consolas", 9.5F); + this.txtShapeAngle.BackColor = ThemeHelper.CardBackground; + this.txtShapeAngle.ForeColor = Color.White; + this.btnAddShape.Location = new Point(8, 180); this.tabShapes.Controls.Add(this.lblShapeType); @@ -541,6 +564,8 @@ namespace MathApp.Controls this.tabShapes.Controls.Add(this.txtShapeDim1); this.tabShapes.Controls.Add(this.lblShapeDim2); this.tabShapes.Controls.Add(this.txtShapeDim2); + this.tabShapes.Controls.Add(this.lblShapeAngle); + this.tabShapes.Controls.Add(this.txtShapeAngle); this.tabShapes.Controls.Add(this.btnAddShape); this.pnlRightSide.Controls.Add(this.tabManager); @@ -630,6 +655,7 @@ namespace MathApp.Controls private ComboBox cmbCurveThickness; private Button btnCurveToggle; private Button btnCurveDelete; + private Button btnCurveEdit; private Button btnCurveClearAll; private ListBox lstPoints; @@ -667,6 +693,8 @@ namespace MathApp.Controls private TextBox txtShapeDim1; private Label lblShapeDim2; private TextBox txtShapeDim2; + private Label lblShapeAngle; + private TextBox txtShapeAngle; private Button btnAddShape; private Panel pnlStatusBar; diff --git a/src/ttCalculator/Controls/GraphPlotterControl.cs b/src/ttCalculator/Controls/GraphPlotterControl.cs index 2429f25..3f89f04 100644 --- a/src/ttCalculator/Controls/GraphPlotterControl.cs +++ b/src/ttCalculator/Controls/GraphPlotterControl.cs @@ -147,14 +147,18 @@ namespace MathApp.Controls // Curve management lstCurves.SelectedIndexChanged += LstCurves_SelectedIndexChanged; + lstCurves.DoubleClick += (s, e) => EditSelectedCurve(); + lstCurves.KeyDown += (s, e) => { if (e.KeyCode == Keys.Delete) BtnCurveDelete_Click(this, EventArgs.Empty); }; btnCurveColor.Click += BtnCurveColor_Click; cmbCurveStyle.SelectedIndexChanged += CmbCurveStyle_SelectedIndexChanged; cmbCurveThickness.SelectedIndexChanged += CmbCurveThickness_SelectedIndexChanged; btnCurveToggle.Click += BtnCurveToggle_Click; btnCurveDelete.Click += BtnCurveDelete_Click; + btnCurveEdit.Click += (s, e) => EditSelectedCurve(); btnCurveClearAll.Click += BtnCurveClearAll_Click; // Point management + lstPoints.KeyDown += (s, e) => { if (e.KeyCode == Keys.Delete) BtnDeletePoint_Click(this, EventArgs.Empty); }; btnAddPoint.Click += BtnAddPoint_Click; btnDeletePoint.Click += BtnDeletePoint_Click; btnClearPoints.Click += (s, e) => { _points.Clear(); RefreshPointsList(); pnlCanvas.Invalidate(); }; @@ -416,6 +420,23 @@ namespace MathApp.Controls RefreshCurvesList(); RefreshToolCombos(); pnlCanvas.Invalidate(); + lblStatus.Text = "Eğri listeden silindi."; + } + } + + private void EditSelectedCurve() + { + if (lstCurves.SelectedItem is PlotCurve c && !c.IsVerticalLine) + { + txtEquation.Text = c.Equation; + chkClampRange.Checked = c.ClampDomain; + if (c.ClampDomain) + { + txtMinX.Text = c.DomainMinX.ToString(CultureInfo.InvariantCulture); + txtMaxX.Text = c.DomainMaxX.ToString(CultureInfo.InvariantCulture); + } + txtEquation.Focus(); + lblStatus.Text = $"Eğri düzenleme için yüklendi: y = {c.Equation}"; } } @@ -1278,6 +1299,19 @@ namespace MathApp.Controls float cy = ParseFloat(txtShapeCenterY.Text, 0f); float d1 = Math.Max(0.1f, ParseFloat(txtShapeDim1.Text, 4f)); float d2 = Math.Max(0.1f, ParseFloat(txtShapeDim2.Text, 2f)); + float angleDeg = ParseFloat(txtShapeAngle.Text, 0f); + float angleRad = (float)(angleDeg * Math.PI / 180.0); + PointF center = new PointF(cx, cy); + + PointF RotatePt(PointF pt) + { + if (Math.Abs(angleDeg) < 1e-3f) return pt; + float cos = (float)Math.Cos(angleRad); + float sin = (float)Math.Sin(angleRad); + float dx = pt.X - center.X; + float dy = pt.Y - center.Y; + return new PointF(center.X + dx * cos - dy * sin, center.Y + dx * sin + dy * cos); + } int shapeIdx = cmbShapeType.SelectedIndex; Color shapeColor = _palette[_colorIndex % _palette.Length]; @@ -1286,7 +1320,6 @@ namespace MathApp.Controls switch (shapeIdx) { case 0: // Daire (Çember) - // y = cy ± sqrt(R^2 - (x - cx)^2) string upCircle = $"{cy:F3} + sqrt({d1 * d1:F4} - (x - {cx:F3})^2)"; string lowCircle = $"{cy:F3} - sqrt({d1 * d1:F4} - (x - {cx:F3})^2)"; AddFunctionCurve(upCircle, true, cx - d1, cx + d1, shapeColor, DashStyle.Solid, 2.5f); @@ -1295,44 +1328,52 @@ namespace MathApp.Controls break; case 1: // Kare (Kenar d1) - float halfSide = d1 / 2f; - // Top, Bottom, Left, Right - AddFunctionCurve($"{cy + halfSide:F3}", true, cx - halfSide, cx + halfSide, shapeColor, DashStyle.Solid, 2.5f); - AddFunctionCurve($"{cy - halfSide:F3}", true, cx - halfSide, cx + halfSide, shapeColor, DashStyle.Solid, 2.5f); - AddVerticalLine(cx - halfSide, cy - halfSide, cy + halfSide, shapeColor, DashStyle.Solid, 2.5f); - AddVerticalLine(cx + halfSide, cy - halfSide, cy + halfSide, shapeColor, DashStyle.Solid, 2.5f); - lblStatus.Text = $"Kare eklendi [Merkez: ({cx}, {cy}), Kenar: {d1}]"; + float hs = d1 / 2f; + PointF[] sq = new PointF[] + { + RotatePt(new PointF(cx - hs, cy - hs)), + RotatePt(new PointF(cx + hs, cy - hs)), + RotatePt(new PointF(cx + hs, cy + hs)), + RotatePt(new PointF(cx - hs, cy + hs)) + }; + for (int i = 0; i < 4; i++) AddLineSegmentCurve(sq[i], sq[(i + 1) % 4], shapeColor); + lblStatus.Text = $"Kare eklendi [Merkez: ({cx}, {cy}), Kenar: {d1}, Dönüş: {angleDeg}°]"; break; case 2: // Dikdörtgen (Genişlik d1, Yükseklik d2) float hw = d1 / 2f; float hh = d2 / 2f; - AddFunctionCurve($"{cy + hh:F3}", true, cx - hw, cx + hw, shapeColor, DashStyle.Solid, 2.5f); - AddFunctionCurve($"{cy - hh:F3}", true, cx - hw, cx + hw, shapeColor, DashStyle.Solid, 2.5f); - AddVerticalLine(cx - hw, cy - hh, cy + hh, shapeColor, DashStyle.Solid, 2.5f); - AddVerticalLine(cx + hw, cy - hh, cy + hh, shapeColor, DashStyle.Solid, 2.5f); - lblStatus.Text = $"Dikdörtgen eklendi [Merkez: ({cx}, {cy}), {d1} × {d2}]"; + PointF[] rect = new PointF[] + { + RotatePt(new PointF(cx - hw, cy - hh)), + RotatePt(new PointF(cx + hw, cy - hh)), + RotatePt(new PointF(cx + hw, cy + hh)), + RotatePt(new PointF(cx - hw, cy + hh)) + }; + for (int i = 0; i < 4; i++) AddLineSegmentCurve(rect[i], rect[(i + 1) % 4], shapeColor); + lblStatus.Text = $"Dikdörtgen eklendi [Merkez: ({cx}, {cy}), {d1} × {d2}, Dönüş: {angleDeg}°]"; break; case 3: // Eşkenar Üçgen (Yarıçap d1) - // 3 vertices on circle of radius d1 - PointF v1 = new PointF(cx, cy + d1); - PointF v2 = new PointF(cx - d1 * 0.866f, cy - d1 * 0.5f); - PointF v3 = new PointF(cx + d1 * 0.866f, cy - d1 * 0.5f); - + PointF v1 = RotatePt(new PointF(cx, cy + d1)); + PointF v2 = RotatePt(new PointF(cx - d1 * 0.866025f, cy - d1 * 0.5f)); + PointF v3 = RotatePt(new PointF(cx + d1 * 0.866025f, cy - d1 * 0.5f)); AddLineSegmentCurve(v1, v2, shapeColor); AddLineSegmentCurve(v2, v3, shapeColor); AddLineSegmentCurve(v3, v1, shapeColor); - lblStatus.Text = $"Eşkenar Üçgen eklendi [Merkez: ({cx}, {cy})]"; + lblStatus.Text = $"Eşkenar Üçgen eklendi [Merkez: ({cx}, {cy}), Dönüş: {angleDeg}°]"; break; - case 4: // Elips (a=d1, b=d2) - // y = cy ± (b/a)*sqrt(a^2 - (x - cx)^2) - string upEllipse = $"{cy:F3} + ({d2:F3}/{d1:F3}) * sqrt({d1 * d1:F4} - (x - {cx:F3})^2)"; - string lowEllipse = $"{cy:F3} - ({d2:F3}/{d1:F3}) * sqrt({d1 * d1:F4} - (x - {cx:F3})^2)"; - AddFunctionCurve(upEllipse, true, cx - d1, cx + d1, shapeColor, DashStyle.Solid, 2.5f); - AddFunctionCurve(lowEllipse, true, cx - d1, cx + d1, shapeColor, DashStyle.Solid, 2.5f); - lblStatus.Text = $"Elips eklendi [Merkez: ({cx}, {cy}), a={d1}, b={d2}]"; + case 4: // Elips (a=d1, b=d2, Dönüş Açılı) + int nEll = 36; + PointF[] ell = new PointF[nEll]; + for (int i = 0; i < nEll; i++) + { + double t = i * 2.0 * Math.PI / nEll; + ell[i] = RotatePt(new PointF((float)(cx + d1 * Math.Cos(t)), (float)(cy + d2 * Math.Sin(t)))); + } + for (int i = 0; i < nEll; i++) AddLineSegmentCurve(ell[i], ell[(i + 1) % nEll], shapeColor); + lblStatus.Text = $"Elips eklendi [Merkez: ({cx}, {cy}), a={d1}, b={d2}, Dönüş: {angleDeg}°]"; break; case 5: // Düzgün Altıgen (Yarıçap d1) @@ -1340,13 +1381,10 @@ namespace MathApp.Controls for (int i = 0; i < 6; i++) { double ang = i * Math.PI / 3.0; - hex[i] = new PointF((float)(cx + d1 * Math.Cos(ang)), (float)(cy + d1 * Math.Sin(ang))); + hex[i] = RotatePt(new PointF((float)(cx + d1 * Math.Cos(ang)), (float)(cy + d1 * Math.Sin(ang)))); } - for (int i = 0; i < 6; i++) - { - AddLineSegmentCurve(hex[i], hex[(i + 1) % 6], shapeColor); - } - lblStatus.Text = $"Düzgün Altıgen eklendi [Merkez: ({cx}, {cy}), R={d1}]"; + for (int i = 0; i < 6; i++) AddLineSegmentCurve(hex[i], hex[(i + 1) % 6], shapeColor); + lblStatus.Text = $"Düzgün Altıgen eklendi [Merkez: ({cx}, {cy}), R={d1}, Dönüş: {angleDeg}°]"; break; } } diff --git a/src/ttCalculator/Controls/PdeSolverControl.Designer.cs b/src/ttCalculator/Controls/PdeSolverControl.Designer.cs index 5a7e657..e87d265 100644 --- a/src/ttCalculator/Controls/PdeSolverControl.Designer.cs +++ b/src/ttCalculator/Controls/PdeSolverControl.Designer.cs @@ -104,9 +104,11 @@ namespace MathApp.Controls this.cmbPdeType.ForeColor = Color.White; this.cmbPdeType.Font = new Font("Segoe UI", 9.5F); this.cmbPdeType.Items.AddRange(new object[] { - "Isı Denklemi [u_t = α·u_xx]", - "Dalga Denklemi [u_tt = c²·u_xx]", - "Taşınım Denklemi [u_t + v·u_x = 0]" + "Isı Denklemi [FTCS Açık - u_t = α·u_xx]", + "Isı Denklemi [Crank-Nicolson Koşulsuz Kararlı]", + "Dalga Denklemi [Leapfrog - u_tt = c²·u_xx]", + "Taşınım Denklemi [Upwind - u_t + v·u_x = 0]", + "2B Laplace Denklemi [SOR - ∇²u = 0]" }); this.cmbPdeType.SelectedIndex = 0; diff --git a/src/ttCalculator/Controls/PdeSolverControl.cs b/src/ttCalculator/Controls/PdeSolverControl.cs index 36c7e2b..3d1a96d 100644 --- a/src/ttCalculator/Controls/PdeSolverControl.cs +++ b/src/ttCalculator/Controls/PdeSolverControl.cs @@ -35,12 +35,24 @@ namespace MathApp.Controls cmbPdeType.SelectedIndexChanged += (s, e) => { - bool isWave = cmbPdeType.SelectedIndex == 1; + int sel = cmbPdeType.SelectedIndex; + bool isWave = sel == 2; + bool isLaplace = sel == 4; + lblInitVel.Visible = isWave; txtInitVel.Visible = isWave; - if (cmbPdeType.SelectedIndex == 0) lblParam.Text = "Difüzyon Katsayısı (α):"; - else if (cmbPdeType.SelectedIndex == 1) lblParam.Text = "Dalga Hızı (c):"; - else lblParam.Text = "Taşınım Hızı (v):"; + + lblInitU.Visible = !isLaplace; + txtInitU.Visible = !isLaplace; + lblT.Visible = !isLaplace; + txtT.Visible = !isLaplace; + lblNt.Visible = !isLaplace; + numNt.Visible = !isLaplace; + + if (sel == 0 || sel == 1) lblParam.Text = "Difüzyon Katsayısı (α):"; + else if (sel == 2) lblParam.Text = "Dalga Hızı (c):"; + else if (sel == 3) lblParam.Text = "Taşınım Hızı (v):"; + else lblParam.Text = "Boşta (Laplace):"; }; } @@ -60,7 +72,7 @@ namespace MathApp.Controls private void SetPresetWave() { - cmbPdeType.SelectedIndex = 1; + cmbPdeType.SelectedIndex = 2; txtInitU.Text = "sin(pi * x)"; txtInitVel.Text = "0"; txtParam.Text = "2"; @@ -74,7 +86,7 @@ namespace MathApp.Controls private void SetPresetAdvection() { - cmbPdeType.SelectedIndex = 2; + cmbPdeType.SelectedIndex = 3; txtInitU.Text = "exp(-40 * (x - 0.3)^2)"; txtInitVel.Text = "0"; txtParam.Text = "1"; @@ -88,12 +100,12 @@ namespace MathApp.Controls private void SetPresetGauss() { - cmbPdeType.SelectedIndex = 0; + cmbPdeType.SelectedIndex = 1; // Crank-Nicolson txtInitU.Text = "exp(-100 * (x - 0.5)^2)"; txtInitVel.Text = "0"; - txtParam.Text = "0.05"; + txtParam.Text = "0.2"; txtL.Text = "1"; - txtT.Text = "0.2"; + txtT.Text = "0.3"; numNx.Value = 31; numNt.Value = 200; txtBcLeft.Text = "0"; @@ -107,13 +119,7 @@ namespace MathApp.Controls btnSolve.Enabled = false; btnSolve.Text = "⏳ Hesaplanıyor..."; - DifferentialEquations.PdeType pdeType; - if (cmbPdeType.SelectedIndex == 0) pdeType = DifferentialEquations.PdeType.HeatEquation; - else if (cmbPdeType.SelectedIndex == 1) pdeType = DifferentialEquations.PdeType.WaveEquation; - else pdeType = DifferentialEquations.PdeType.AdvectionEquation; - - string initU = txtInitU.Text.Trim(); - string initVel = txtInitVel.Text.Trim(); + int sel = cmbPdeType.SelectedIndex; if (!decimal.TryParse(txtParam.Text.Replace(',', '.'), NumberStyles.Any, CultureInfo.InvariantCulture, out decimal paramVal)) paramVal = 0.1m; @@ -130,6 +136,61 @@ namespace MathApp.Controls if (!decimal.TryParse(txtBcRight.Text.Replace(',', '.'), NumberStyles.Any, CultureInfo.InvariantCulture, out decimal bcRight)) bcRight = 0m; + if (sel == 4) + { + // 2D Laplace + decimal topBc = 100m; + decimal bottomBc = 0m; + var laplace = await Task.Run(() => + { + return DifferentialEquations.Solve2DLaplace(nx, nx, bcLeft, bcRight, topBc, bottomBc); + }); + + lblStability.Text = $"2B Laplace Denklemi (SOR): {laplace.Iterations} iterasyonda yakınsadı (Kalıntı: {laplace.FinalResidual:G4})"; + lblDetails.Text = $"Izgara: {nx} × {nx} düğüm | Sınırlar: Sol={bcLeft}, Sağ={bcRight}, Üst={topBc}, Alt={bottomBc}"; + + StringBuilder sbL = new StringBuilder(); + sbL.AppendLine("=========================================================================================="); + sbL.AppendLine("2 BOYUTLU ELLİPTİK LAPLACE DENKLEMİ (∇²u = u_xx + u_yy = 0) POTANSİYEL DAĞILIMI"); + sbL.AppendLine($"Izgara: {nx} x {nx} | İterasyon: {laplace.Iterations} | Yakınsama Residual: {laplace.FinalResidual:G4}"); + sbL.AppendLine("=========================================================================================="); + + int stepX = Math.Max(1, nx / 10); + int stepY = Math.Max(1, nx / 10); + + sbL.Append(string.Format("{0,-8}", "y \\ x")); + for (int i = 0; i < nx; i += stepX) + { + decimal xPos = (decimal)i / (nx - 1); + sbL.Append(string.Format(" | {0,-8:F2}", xPos)); + } + sbL.AppendLine(); + sbL.AppendLine(new string('-', 100)); + + for (int j = nx - 1; j >= 0; j -= stepY) + { + decimal yPos = (decimal)j / (nx - 1); + sbL.Append(string.Format("{0,-8:F2}", yPos)); + for (int i = 0; i < nx; i += stepX) + { + sbL.Append(string.Format(" | {0,-8:F2}", laplace.Grid[i, j])); + } + sbL.AppendLine(); + } + sbL.AppendLine("=========================================================================================="); + txtOutput.Text = sbL.ToString(); + return; + } + + DifferentialEquations.PdeType pdeType; + if (sel == 0) pdeType = DifferentialEquations.PdeType.HeatEquation; + else if (sel == 1) pdeType = DifferentialEquations.PdeType.HeatEquationCrankNicolson; + else if (sel == 2) pdeType = DifferentialEquations.PdeType.WaveEquation; + else pdeType = DifferentialEquations.PdeType.AdvectionEquation; + + string initU = txtInitU.Text.Trim(); + string initVel = txtInitVel.Text.Trim(); + var grid = await Task.Run(() => { return DifferentialEquations.Solve1DPde( @@ -159,7 +220,6 @@ namespace MathApp.Controls sb.AppendLine($"Izgara: {nx} mekan noktası, {nt} zaman adımı | dx = {dx:F5}, dt = {dt:F5}"); sb.AppendLine("=========================================================================================="); - // Select 5 key time slices: t=0, 25%, 50%, 75%, 100% int[] timeIndices = new int[] { 0, diff --git a/src/ttCalculator/Core/DifferentialEquations.cs b/src/ttCalculator/Core/DifferentialEquations.cs index 6127edf..a6926a8 100644 --- a/src/ttCalculator/Core/DifferentialEquations.cs +++ b/src/ttCalculator/Core/DifferentialEquations.cs @@ -287,7 +287,7 @@ namespace MathApp #region 3. Partial Differential Equations (1D Heat, Wave, Advection) - public enum PdeType { HeatEquation, WaveEquation, AdvectionEquation } + public enum PdeType { HeatEquation, HeatEquationCrankNicolson, WaveEquation, AdvectionEquation } public class PdeSolutionGrid { @@ -370,6 +370,48 @@ namespace MathApp } } } + else if (pdeType == PdeType.HeatEquationCrankNicolson) + { + // Crank-Nicolson Implicit Scheme: O(dt^2 + dx^2) unconditionally stable for all r > 0 + decimal r = (paramAlphaOrC * dt) / (dx * dx); + decimal rHalf = r * 0.5m; + stabilityNote = $"Isı Denklemi (Crank-Nicolson): r = α·Δt / Δx² = {r:G4} ✓ Koşulsuz Kararlı (Unconditionally Stable)"; + + int m = spatialPointsNx - 2; + decimal diagA = -rHalf; + decimal diagB = 1m + r; + decimal diagC = -rHalf; + + decimal[] d = new decimal[m]; + decimal[] cPrime = new decimal[m]; + decimal[] dPrime = new decimal[m]; + + for (int n = 0; n < timeStepsNt - 1; n++) + { + for (int i = 1; i <= m; i++) + { + d[i - 1] = rHalf * u[i - 1, n] + (1m - r) * u[i, n] + rHalf * u[i + 1, n]; + } + d[0] += rHalf * leftBoundaryVal; + d[m - 1] += rHalf * rightBoundaryVal; + + // Thomas algorithm + cPrime[0] = diagC / diagB; + dPrime[0] = d[0] / diagB; + for (int k = 1; k < m; k++) + { + decimal denom = diagB - diagA * cPrime[k - 1]; + if (k < m - 1) cPrime[k] = diagC / denom; + dPrime[k] = (d[k] - diagA * dPrime[k - 1]) / denom; + } + + u[m, n + 1] = dPrime[m - 1]; + for (int k = m - 2; k >= 0; k--) + { + u[k + 1, n + 1] = dPrime[k] - cPrime[k] * u[k + 2, n + 1]; + } + } + } else if (pdeType == PdeType.WaveEquation) { // Leapfrog central difference for Wave equation: u_tt = c^2 * u_xx @@ -530,5 +572,79 @@ namespace MathApp } #endregion + + #region 5. 2D Elliptic PDEs (Laplace / Poisson Equation: u_xx + u_yy = 0) + + public class Laplace2DSolution + { + public int Nx { get; set; } + public int Ny { get; set; } + public decimal[,] Grid { get; set; } + public int Iterations { get; set; } + public decimal FinalResidual { get; set; } + } + + public static Laplace2DSolution Solve2DLaplace( + int nx, + int ny, + decimal leftBc, + decimal rightBc, + decimal topBc, + decimal bottomBc, + int maxIter = 1000, + decimal tol = 1e-5m) + { + if (nx < 4) nx = 20; + if (ny < 4) ny = 20; + decimal[,] u = new decimal[nx, ny]; + + for (int j = 0; j < ny; j++) + { + u[0, j] = leftBc; + u[nx - 1, j] = rightBc; + } + for (int i = 0; i < nx; i++) + { + u[i, 0] = bottomBc; + u[i, ny - 1] = topBc; + } + + decimal avg = (leftBc + rightBc + topBc + bottomBc) / 4m; + for (int i = 1; i < nx - 1; i++) + for (int j = 1; j < ny - 1; j++) + u[i, j] = avg; + + int iter = 0; + decimal maxDiff = 1m; + decimal omega = 1.35m; // SOR factor + + while (iter < maxIter && maxDiff > tol) + { + maxDiff = 0m; + for (int i = 1; i < nx - 1; i++) + { + for (int j = 1; j < ny - 1; j++) + { + decimal uNew = 0.25m * (u[i + 1, j] + u[i - 1, j] + u[i, j + 1] + u[i, j - 1]); + decimal uRelaxed = (1m - omega) * u[i, j] + omega * uNew; + decimal diff = DecimalMath.Abs(uRelaxed - u[i, j]); + if (diff > maxDiff) maxDiff = diff; + u[i, j] = uRelaxed; + } + } + iter++; + } + + return new Laplace2DSolution + { + Nx = nx, + Ny = ny, + Grid = u, + Iterations = iter, + FinalResidual = maxDiff + }; + } + + #endregion } } diff --git a/src/ttCalculator/MainForm.Designer.cs b/src/ttCalculator/MainForm.Designer.cs index fb0bfd7..8e0cbd6 100644 --- a/src/ttCalculator/MainForm.Designer.cs +++ b/src/ttCalculator/MainForm.Designer.cs @@ -139,7 +139,7 @@ namespace MathApp // Footer version label this.lblVersion.Dock = DockStyle.Bottom; this.lblVersion.Height = 26; - this.lblVersion.Text = "v2.3 • 128-bit Decimal Motoru"; + this.lblVersion.Text = "v2.4 • 128-bit Decimal Motoru"; this.lblVersion.Font = new Font("Segoe UI", 8F); this.lblVersion.ForeColor = Color.FromArgb(100, 116, 139); this.lblVersion.TextAlign = ContentAlignment.MiddleCenter; diff --git a/src/ttCalculator/ttCalculator.csproj b/src/ttCalculator/ttCalculator.csproj index d2d58f0..4381af7 100644 --- a/src/ttCalculator/ttCalculator.csproj +++ b/src/ttCalculator/ttCalculator.csproj @@ -9,7 +9,7 @@ logo.ico ttCalculator ttCalculator - Gelişmiş Matematik & Nümerik Analiz Stüdyosu - 2.3.0 + 2.4.0 ttCalculator diff --git a/tests/ttCalculator.Tests/Program.cs b/tests/ttCalculator.Tests/Program.cs index 65f172b..65e6f7d 100644 --- a/tests/ttCalculator.Tests/Program.cs +++ b/tests/ttCalculator.Tests/Program.cs @@ -10,7 +10,7 @@ namespace MathApp.Tests static void Main(string[] args) { Console.WriteLine("========================================"); - Console.WriteLine("ttCalculator v2.3 Algorithm Verification"); + Console.WriteLine("ttCalculator v2.4 Algorithm Verification"); Console.WriteLine("========================================"); int passed = 0; @@ -164,6 +164,43 @@ namespace MathApp.Tests AssertTrue(false, "SolveCoupledPdeSystem: " + ex.Message); } + // Test 8: 1D Crank-Nicolson Heat Equation (Unconditionally Stable) + try + { + var cnHeat = DifferentialEquations.Solve1DPde( + DifferentialEquations.PdeType.HeatEquationCrankNicolson, + "sin(pi * x)", + 1.0m, + 0.2m, + 21, + 50, + 0.1m, + 0m, + 0m + ); + decimal midU = cnHeat.U[10, 49]; + double expected = Math.Exp(-0.1 * Math.PI * Math.PI * 0.2) * Math.Sin(Math.PI * 0.5); + AssertTrue(Math.Abs((double)midU - expected) < 0.02, + $"1D Crank-Nicolson Heat U={midU:F4} (expected ≈ {expected:F4})"); + } + catch (Exception ex) + { + AssertTrue(false, "Crank-Nicolson: " + ex.Message); + } + + // Test 9: 2D Laplace Elliptic Solver + try + { + var laplace = DifferentialEquations.Solve2DLaplace(15, 15, 0m, 0m, 100m, 0m); + decimal centerVal = laplace.Grid[7, 7]; + AssertTrue(centerVal > 0m && centerVal < 100m && laplace.Iterations > 0, + $"2D Laplace SOR center potential = {centerVal:F2} (expected 0 < V < 100)"); + } + catch (Exception ex) + { + AssertTrue(false, "2D Laplace: " + ex.Message); + } + Console.WriteLine("========================================"); Console.WriteLine($"RESULTS: {passed}/{total} tests passed ({(passed * 100.0 / total):F1}%)"); Console.WriteLine("========================================");