Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
VBA help for engineering project
Options
-
01-05-2007 4:43pmThough I would ask the wise here for help.
Trying to explain what i am trying to do would take a while (and i am not 100% sure my self, it is from a handout) But i run this macro, it does not do anything.
Short cut crtl+I creates a matrix of initial internal temperature values for a section of a cylinder
crtl+e creates a matrix of external temperatures
crtl+c calculates run the third sub to calculate the non capacitance heat transfer thingy me bob. All is grand, stepping through it gives me no more errors.
I suppose the question is in the end loop bit, what is the correct way to write a loop as tnow and tmax do not seem to be working for me. What am i doing wrong?
Finde the whole file attached also.
Code:
[PHP]Sub Tstarti()
'Initial Internal Temp'
Dim Tstart As Single
Ti = Range("B4").Value
Dim outputregion As Range
'Tstart = Range("B14").Value
Tstart = Ti
Set outputregion = Range("C18:L28")
Dim arr(10, 10) As Single
For r = 1 To 10
For c = 1 To 10
arr(r, c) = Tstart
outputregion.Cells(r, c).Value = arr(r, c)
Next c
Next r
End Sub
Sub Tstarte()
'Initial External Temp'
Dim Tstart As Single
Te = Range("B5").Value
Dim outputregion As Range
'Tstart = Range("B14").Value
Tstart = Te
Set outputregion = Range("B18:B27")
Dim arr(10, 1) As Single
For r = 1 To 10
For c = 1 To 1
arr(r, c) = Te
outputregion.Cells(r, c).Value = arr(r, c)
Next c
Next r
End Sub
Sub nodes()
Const Nr = 10
Const Ny = 10
'dimensions
ReDim r(Nr), y(Ny), ra(Nr, Ny), yA(Nr), dV(Nr, Ny), Told(Nr, Ny), Tnew(Nr, Ny), arr(Nr, Ny) As Single
ReDim rALocation(Nr), yALocation(Ny) As Single
Dim cB, cT, cR, cL As Single
'Definitions
Radius = Range("B2").Value
HalfHeight = Range("B3").Value
Timemax = Range("B10").Value
Alpha = Range("B9").Value
Ti = Range("B4").Value
Te = Range("B5").Value
dt = Range("B13").Value
Pi = 4 * Atn(1)
k = Range("B8").Value
h = Range("B7").Value
Told = Range("C18:L27").Value
Timenow = Range("B15").Value
dr = 2 * Radius / (2 * Nr - 3)
dy = 2 * HalfHeight / (2 * Ny - 3)
'Nodes in Radius'
r(1) = 0: r(Nr) = Radius
For i = 1 To Nr - 1
r(i) = r(i - 1) + dr
Next i
'Nodes in Halfheight'
y(1) = 0: y(Ny) = HalfHeight
For i = 2 To Nr - 1
y(i) = y(i - 1) + dy
Next i
'Locations for sides of volume elements'
rALocation(1) = r(2) / 2: rALocation(Nr) = Radius
For i = 2 To Nr - 1
' rALocation(i) = (r(i + 1) - r(i)) / (Log(r(i + 1) / r(i)))
rALocation(i) = (r(i + 1) - r(i)) / (Log(r(i + 1) / r(i)))
Next i
'Locations for tops and bottoms of volume elements'
yALocation(1) = y(2) / 2: yALocation(Ny) = HalfHeight
For j = 2 To Ny - 1
yALocation(j) = yALocation(j - 1) + dy
Next j
'Vertical areas perp to radial heat flow'
For j = 1 To Ny
For i = 1 To Nr
ra(i, j) = 2 * Pi * rALocation(i) * (yALocation(j) - yALocation(j - 1))
Next i
Next j
'Horizontal areas perp to vertical heat flow'
yA(1) = Pi * rALocation(1) * rALocation(1)
yAsum = yA(1)
For i = 2 To Nr - 1
yA(i) = Pi * rALocation(i) * rALocation(i) - yAsum
yAsum = yAsum + yA(i)
Next i
'volumes'
For j = 1 To Nr - 1
For i = 1 To Nr - 1
dV(i, j) = yA(i) * (yALocation(j) - yALocation(j - 1))
Next i
Next j
'Main Loop of explicit finite difference heat transfer'
Do
Timenow = Timenow + dt
Te = Range("B5").Value
HeatTransferCoefficient = Range("B7").Value
'del
Dim outputregion As Range
'Set outputregion = Range("C14:L23")
Set outputregion = Range("C18:L27")
ReDim arr(i, j) As Single
'del
For j = 1 To Ny - 1
For i = 1 To Nr - 1
Select Case j
Case 1
Select Case i
Case 1
cR = Alpha * dt / dV(i, j) * ra(i, j) / dr
cT = Alpha * dt / dV(i, j) * yA(i) / dy
Tnew(i, j) = Told(i, j) * (1 - cR - cT) + cR * Told(i + 1, j) + cT * Told(i, j + 1)
Case 2 To Nr - 2
cL = Alpha * dt / dV(i, j) * ra(i - 1, j) / dr
cR = Alpha * dt / dV(i, j) * ra(i, j) / dr
cT = Alpha * dt / dV(i, j) * yA(i) / dy
Tnew(i, j) = Told(i, j) * (1 - cL - cR - cT) + cL * Told(i - 1, j) + cR * Told(i + 1, j) + cT * Told(i, j + 1)
Case Nr - 1
cL = Alpha * dt / dV(i, j) * ra(i - 1, j) / dr
cT = Alpha * dt / dV(i, j) * yA(i) / dy
cR = Alpha * dt / dV(i, j) * 1 / (k / (h * ra(i + 1, j)) + (dr / 2) / ra(i, j))
Tnew(i, j) = Told(i, j) * (1 - cL - cR - cT) + cL * Told(i - 1, j) + cT * Told(i, j + 1) + cR * Te
End Select
Case 2 To Ny - 2
Select Case i
Case 1
cR = Alpha * dt / dV(i, j) * ra(i, j) / dr
cT = Alpha * dt / dV(i, j) * yA(i) / dy
cB = Alpha * dt / dV(i, j) * yA(i) / dy
Tnew(i, j) = Told(i, j) * (1 - cR - cT - cB) + cR * Told(i + 1, j) + cT * Told(i, j + 1) + cB * Told(i, j - 1)
Case 2 To Nr - 2
cL = Alpha * dt / dV(i, j) * ra(i - 1, j) / dr
cR = Alpha * dt / dV(i, j) * ra(i, j) / dr
cT = Alpha * dt / dV(i, j) * yA(i) / dy
cB = Alpha * dt / dV(i, j) * yA(i) / dy
Tnew(i, j) = Told(i, j) * (1 - cL - cR - cT - cB) + cL * Told(i - 1, j) + cR * Told(i, j + 1) + cT * Told(i, j + 1) - cT * Told(i, j - 1)
Case Nr - 1
cL = Alpha * dt / dV(i, j) * ra(i - 1, j) / dr
cT = Alpha * dt / dV(i, j) * yA(i) / dy
cB = Alpha * dt / dV(i, j) * yA(i) / dy
cR = Alpha * dt / dV(i, j) * 1 / (k / (h * ra(i + 1, j)) + (dr / 2) / ra(i, j))
Tnew(i, j) = Told(i, j) * (1 - cL - cT - cB - cR) + cL * Told(i - 1, j) + cT * Told(i, j + 1) + cB * Told(i, j - 1) - cR * Te
End Select
Case Ny - 1
Select Case i
Case 1
cR = Alpha * dt / dV(i, j) * ra(i, j) / dr
cB = Alpha * dt / dV(i, j) * yA(i) / dy
'cT = Alpha * dt / dV(i, j) * 1 / (k / (h * yA(i + 1, j + 1)) + (dr / 2) / yA(i, j))
cT = Alpha * dt / dV(i, j) * 1 / (k / (h * yA(i)) + (dr / 2) / (yA(i)))
Tnew(i, j) = Told(i, j) * (1 - cR - cB - cT) + cR * Told(i + 1, j) + cB * Told(i, j - 1) + cT * Te
Case 2 To Nr - 2
cL = Alpha * dt / dV(i, j) * ra(i - 1, j) / dr
cR = Alpha * dt / dV(i, j) * ra(i, j) / dr
cB = Alpha * dt / dV(i, j) * yA(i) / dy
cT = Alpha * dt / dV(i, j) * 1 / (k / (h * yA(i)) + (dr / 2) / yA(i))
Tnew(i, j) = Told(i, j) * (1 - cL - cR - cB - cT) + cL * Told(i - 1, j) + cR * Told(i + 1, j) + cB * Told(i, j - 1) - cT * Te
Case Nr - 1
cL = Alpha * dt / dV(i, j) * ra(i - 1, j) / dr
cB = Alpha * dt / dV(i, j) * yA(i) / dy
cR = Alpha * dt / dV(i, j) * 1 / (k / (h * ra(i, j)) + ((dr / 2) / ra(i, j)))
cT = Alpha * dt / dV(i, j) * 1 / (k / (h * yA(i)) + ((dr / 2) / yA(i)))
Tnew(i, j) = Told(i, j) * (1 - cL - cR - cB - cT) + cL * Told(i - 1, j) + cR * Told(i + 1, j) + cB * Told(i, j - 1) - cT * Te
End Select
End Select
Next i
Next j
For j = 1 To Ny - 1
For i = 1 To Nr - 1
Told(i, j) = Tnew(i, j)
Next i
Next j
arr(i, j) = Tnew(i, j)
outputregion.Cells(Nr, Ny).Value = arr(Nr, Ny)
Loop Until Timenow >= Timemax
End Sub
[/PHP]0
Comments
-
Never mind, i figured it out.0
-
erm, how?
If you post a question and then subsequently solve it, you should post up your solution for others to see.0 -
well actually my project partner figured it out. It actually was working, I was just setping through it but there was no code to out put the values into a spread sheet. It runs and does the loop then the macro is run.0
Advertisement