VERSION 5.00
Begin VB.Form Form1 
   BorderStyle     =   1  'Fixed Single
   Caption         =   "Form1"
   ClientHeight    =   5760
   ClientLeft      =   4020
   ClientTop       =   2280
   ClientWidth     =   9105
   LinkTopic       =   "Form1"
   MaxButton       =   0   'False
   MinButton       =   0   'False
   ScaleHeight     =   5760
   ScaleWidth      =   9105
   Begin VB.ListBox List1 
      Height          =   4740
      Left            =   1200
      TabIndex        =   1
      Top             =   480
      Width           =   6855
   End
   Begin VB.CommandButton Command1 
      Caption         =   "Command1"
      Height          =   495
      Left            =   240
      TabIndex        =   0
      Top             =   960
      Width           =   735
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Dim e, pi
Dim mu, dpdz, Lx, Ly
Dim Uanalitik(30, 20)
Dim uNew(30, 20), uOld(30, 20)
Dim A, dlt, NxA, NyA, Nx, Ny

Private Sub Form_Load()
    pi = 3.141592654
    e = 2.718281828
End Sub

Private Sub Command1_Click()

'   -------------------------------------------
'   Parametreler
'   -------------------------------------------
    Lx = 3          ' Kanal genişliği
    Ly = 2          ' Kanal yüksekliği
    mu = 0.4        ' Viskozite
    dpdz = -20      ' Basınç gradyantı
    Nx = 30         ' x boyunca hücre sayısı
    Ny = 20         ' y boyunca hücre sayısı
    epsilon = 1     ' Hata için tolerans
    MaxIter = 100   ' Maksimum iterasyon sayısı

'   -------------------------------------------
'   Analitik çözüm
'   -------------------------------------------
    Call Analitik

    Form1.List1.AddItem " Analitik çözüm"
    Form1.List1.AddItem ""
    For j = 0 To NyA
        Satır = ""
        For i = 0 To NxA
        Satır = Satır & Format(Uanalitik(i, j), "0#.###0") & "   "
        Next i
        Form1.List1.AddItem Satır
    Next j

'   -------------------------------------------
'   Nümerik çözüm
'   -------------------------------------------
    A = dpdz / mu
    dlt = Lx / Nx

    For j = 0 To Ny
    For i = 0 To Nx
    uNew(i, j) = 0
    Next i
    Next j

    For iter = 1 To MaxIter

        For j = 0 To Ny
        For i = 0 To Nx
        uOld(i, j) = uNew(i, j)
        Next i
        Next j

'    Call PGSstep
    Call LGSstep

'       Yakınsama testi
    
        MaxHata = 0
        For j = 1 To NyA - 1
        For i = 1 To NxA - 1
        hata = Abs(Uanalitik(i, j) - uNew(5 * i, 5 * j))
        If hata > MaxHata Then MaxHata = hata
        Next i
        Next j
    
        If MaxHata <= epsilon Then
        Form1.List1.AddItem "iterasyon yakınsadı iter =" & iter
        GoTo 10
        End If

    Next iter

    Form1.List1.AddItem "iterasyon yakınsamadı iter =" & iter

10  Form1.List1.AddItem ""
    Form1.List1.AddItem " Nümerik çözüm"
    Form1.List1.AddItem ""
    For j = 0 To NyA
        Satır = ""
        For i = 0 To NxA
        Satır = Satır & Format(uNew(5 * i, 5 * j), "0#.###0") & "   "
        Next i
        Form1.List1.AddItem Satır
    Next j

End Sub

'**************************************************************'
'   --------------------------------------------------------   '
'                   ANALİTİK ÇÖZÜM
'   --------------------------------------------------------   '
'**************************************************************'
Public Sub Analitik()

    hx = Lx / 2
    hy = Ly / 2
    dlt = 0.5
    NxA = Lx / dlt
    NyA = Ly / dlt

    Sb = 4 * Lx ^ 2 / (mu * pi ^ 3) * (-dpdz)

    For j = 0 To NyA
        y = j * dlt - hy
        For i = 0 To NxA
            x = i * dlt - hx
            Topl = 0
            For m = 1 To 50 Step 2
                Coshy = Cosh(m * pi * y / Lx)
                Coshx = Cosh(m * pi * hy / Lx)
                Cosx = Cos(m * pi * x / Lx)
                delta = ((-1) ^ ((m - 1) / 2)) * (1 - Coshy / Coshx) * Cosx / m ^ 3
                Topl = Topl + delta
                If Abs(delta) < 0.00001 Then Exit For
            Next m
            Uanalitik(i, j) = Sb * Topl
        Next i
    Next j

End Sub

Function Cosh(y)

    Cosh = (e ^ (y) + e ^ (-y)) / 2

End Function

'**************************************************************'
'   --------------------------------------------------------   '
'                       NÜMERİK ÇÖZÜM
'   --------------------------------------------------------   '
'**************************************************************'
Sub PGSstep()
    
    For j = 1 To Ny - 1
    For i = 1 To Nx - 1
        yeni = uNew(i - 1, j) + uNew(i, j - 1)
        eski = uOld(i + 1, j) + uOld(i, j + 1)
        uNew(i, j) = (yeni + eski - A * dlt ^ 2) / 4
    Next i
    Next j

End Sub

Sub LGSstep()

    ReDim lo(Nx - 1), mi(Nx - 1), up(Nx - 1), Rh(Nx - 1)

    For j = 1 To Ny - 1

        For i = 1 To Nx - 1
            lo(i) = 1
            mi(i) = -4
            up(i) = 1
            Rh(i) = A * dlt ^ 2 - (uNew(i, j - 1) + uOld(i, j + 1))
        Next i
        Rh(1) = Rh(1) - uOld(0, j)
        Rh(Nx - 1) = Rh(Nx - 1) - uOld(Nx, j)
    
        Call thomas(Nx - 1, lo, mi, up, Rh)

        For i = 1 To Nx - 1
        uNew(i, j) = Rh(i)
        Next i

    Next j

End Sub

'************************************************************************
'                                                                    *
'   Thomas method for solving tridiagonal linear equation system
'                                                                    *
'***********************************************************************
Sub thomas(N, lo, mi, up, r)

    Dim c1(300), d1(300)

'   Diyagonal altı elemanların sıfırlanması
    d1(1) = mi(1)
    c1(1) = r(1)
    For i = 2 To N
        d1(i) = mi(i) - lo(i) * up(i - 1) / d1(i - 1)
        c1(i) = r(i) - lo(i) * c1(i - 1) / d1(i - 1)
    Next i

'   Geri süpürme

    r(N) = c1(N) / d1(N)
    For i = N - 1 To 1 Step -1
        r(i) = (c1(i) - up(i) * r(i + 1)) / d1(i)
    Next i

End Sub

