CFD代码
CFD代码
This code was written in Microsoft Visual Basic, for importing data files from the desktop
computer and the Linux cluster. A data file contains velocity or turbulent kinetic energy
at different lines. Shortly described the code imports the data to a variable, then splits
the variable into a vector at the character ENTER. Then it seek the type of delimiter that
has been used, which is different from Windows and Linux. Then it sorts the different
data for each line into different excel documents. A progress bar was added to the code
since it has been optimized. And the time to import 20.000 data files was reduce by
almost a factor of 1000, by reducing the amount of times VBA had to write to excel. To
save even more time the VBA code was saved as a dll file, meaning that the VBA avoids
to compile the code each time it is being used. The time it now uses is mostly because it
needs to open and close the data files it read from.
125
Option Explicit
Sub LesFraEnTekstFil()
On Error GoTo ExitHere
Dim fs As Scripting.FileSystemObject, f As Scripting.TextStream
Dim m_colTimer As New Collection
Dim m_Timer As clsTimer
Set m_Timer = New clsTimer
m_Timer.ResetTimer
Dim ExecutionTime As Variant
Dim inputpath As String 'inputpath
Dim filenames As String 'name of the files
Dim timesteps As String 'how many sample data are available
Dim Outputfile1 As String 'Name of outfile 1
Dim Outputfile2 As String 'Name of outfile 2
Dim Outputfile3 As String 'Name of outfile 3
Dim Outputfile4 As String 'Name of outfile 4
Dim Outputfile5 As String 'Name of outfile 5
Dim skip As Long 'if the statistical data shall be achieved by skipping some data
Dim m As Long 'counter for number of available time steps
Dim n1 As Long 'nr of points at line1
Dim n2 As Long 'nr of points at line2
Dim n3 As Long 'nr of points at line3
Dim n4 As Long 'nr of points at line4
Dim n5 As Long 'nr of points at line5
Dim i As Long 'counter for non usable data
Dim q As Long 'help counter for where to print
Dim d As Long 'help counter
Dim inputfile As String 'the total path and filename with extension
Dim dump 'position to dump non ustable data
Dim pos As Variant 'y-position for the velocity data
Dim vel As Variant 'velocity data at a spsific y-position
Dim start_print As Long 'specifies where to start printing the vel data
Dim start_print_pos As Long 'specifies where to print the pos data
Dim a As Long, aSheet1, aSheet2, aSheet3, aSheet4, aSheet5
Dim l As Long
Dim nr_of_first_file
Dim strVel As String, aOut As Variant, ant, linje As Long, aOut2
Dim NyLine As Boolean, Søkestreng As String, FilTekst As String
For m = 1 To timesteps Step skip 'Runs through all the data files
q = m + nr_of_first_file - 1 'A counter that starts at your first data file
inputfile = inputpath + filenames + VBA.Format(q, "0000") + ".txt"
d = VBA.Len(VBA.Dir(inputfile)) 'if file exsist bigger than 1
If d > 1 Then 'checks if the file exists
frmProgressBar.Label1 = filenames + VBA.Format(q, "0000") + ".txt"
Call updateProgressbar(VBA.CLng(m), VBA.CLng(timesteps))
linje = 0 'resets case
Set fs = New FileSystemObject
Set f = fs.OpenTextFile(inputfile, ForReading, False)
With f
l=0
strVel = .ReadAll ' put all content in d
aOut = VBA.Split(strVel, VBA.Chr(10)) 'splits the content on ENTER
aOut2(0) = VBA.Trim(aOut2(0))
FilTekst = VBA.Right(aOut2(0), 3)
Søkestreng = VBA.CStr(linje + 1) & Chr(34) & ")"
If VBA.IsNumeric(aOut2(0)) Then
pos = VBA.CDbl(aOut2(0))
vel = VBA.CDbl(aOut2(1))
If m = 1 Then 'writes the position in the excel doc.only _
needed for one timestep
End If
n1 = 0
n2 = 0
n3 = 0
n4 = 0
n5 = 0
Next m
m_Timer.StopTimer
m_colTimer.Add m_Timer.Elapsed
ExecutionTime = "Total time: " & vbTab & VBA.Format(m_colTimer(1) / 1000, "0.000 s")
frmProgressBar.CommandButton1.Visible = True
frmProgressBar.Label3 = ExecutionTime
Set m_Timer = Nothing
Excel.Application.ScreenUpdating = True
'frmProgressBar.Hide
Exit Sub
ExitHere:
m_Timer.StopTimer
m_colTimer.Add m_Timer.Elapsed
ExecutionTime = "Total time: " & VBA.vbTab & VBA.Format(m_colTimer(1) / 1000,
"0.000 s")
frmProgressBar.CommandButton1.Visible = True
frmProgressBar.Label3 = "Excecution error!!" & VBA.vbLf & ExecutionTime
Set m_Timer = Nothing
Excel.Application.ScreenUpdating = True
'frmProgressBar.Hide
End Sub
-----------------------------------------------------------------------------------------------------------------
Private Sub updateProgressbar(it As Long, totIteration As Long)
Dim counter As Long
Dim PctDone As Double
counter = it
PctDone = counter / totIteration '(RowMax * ColMax)
With frmProgressBar
.FrameProgress.Caption = VBA.Format(PctDone, "0%")
.LabelProgress.Width = PctDone * (.FrameProgress.Width - 10)
End With
' The DoEvents statement is responsible for the form updating
DoEvents
End Sub
-----------------------------------------------------------------------------------------------------------------
Private Sub updateProgressbar2(it As Long, totIteration As Long)
Dim counter As Long
Dim PctDone As Double
counter = it
PctDone = counter / totIteration '(RowMax * ColMax)
With frmProgressBar
.FrameProgress2.Caption = VBA.Format(PctDone, "0%")
.LabelProgress2.Width = PctDone * (.FrameProgress.Width - 10)
End With
' The DoEvents statement is responsible for the form updating
DoEvents
End Sub
Appendix E
132
Sheet1 - 1
Option Explicit
Sub outfile()
'This sub rouine creates a droplet profile for Fluent DPM injections
'The the droplet profile has a rosin rammler distribution and the droplets are _
randomly spread in the given plane
Me.Range("rngStartOutput").Resize(rowNo, 9).ClearContents
'This section collects all the parameters for the droplet profile
m_timestep = Sheet1.Cells(20, 2) 'The liquid flow for one timestep
m_flowrate = Sheet1.Cells(19, 2) 'The liquid flowrate
counter = 0 'Initialize the counter
d_mean = Sheet1.Cells(7, 2) 'The mean diameter of the droplets
dens_exxsol = Sheet1.Cells(11, 2) 'Density of the liquid
n_spread = Sheet1.Cells(8, 2) 'The spread of the diameter
pi = WorksheetFunction.pi
height = Sheet1.Cells(27, 2) - Sheet1.Cells(26, 2)
wid = Sheet1.Cells(29, 2) - Sheet1.Cells(28, 2)
'This section sets the random number start point, based on the CPU clock
s = Second(Time)
m = 60 * Minute(Time)
h = Hour(Time)
num = s * m * h
Randomize [num]
m_fractot = 0
Loop
Unload frmProgressBar
Sheet1.Cells(48, 6) = counter
wid_step = wid / 5
c = 1
w = wid_step / 2
'Restarting the progress bar
frmProgressBar.LabelProgress.Width = 0
frmProgressBar.Label1 = "Making random coordinates for the droplets"
frmProgressBar.Show False
Leftcol = Sheet1.Cells(30, 1)
rightcol = Sheet1.Cells(30, 2)
'This while loop makes the random coordinates for the droplets
For k = 1 To counter
Rn = (Int((10 ^ 6 - 0 + 1) * Rnd + 0)) / 10 ^ 6
w = wid * Rn
Rn = (Int((10 ^ 6 - 0 + 1) * Rnd + 0)) / 10 ^ 6
h = height * Rn + Sheet1.Cells(26, 2)
Sheet1.Cells(50 + c + k - 1, 3) = Leftcol
Sheet1.Cells(50 + c + k - 1, 4) = 5 / 1000
Sheet1.Cells(50 + c + k - 1, 5) = h / 1000
Sheet1.Cells(50 + c + k - 1, 6) = w / 1000
Sheet1.Cells(50 + c + k - 1, 7) = 1.013078
Sheet1.Cells(50 + c + k - 1, 8) = 0
Sheet1.Cells(50 + c + k - 1, 9) = 0
Sheet1.Cells(50 + c + k - 1, 13) = rightcol
PctDone = k / (counter)
With frmProgressBar
.FrameProgress.Caption = Format(PctDone, "0%")
.LabelProgress.Width = PctDone * (.FrameProgress.Width - 10)
End With
' The DoEvents statement is responsible for the form updating
DoEvents
Next k
Unload frmProgressBar
End Sub