0% found this document useful (0 votes)
3 views

CFD代码

The document contains Visual Basic code for importing CFD data from desktop and Linux systems, optimizing the process to reduce import time significantly. It details the structure of the code, including how it reads, processes, and sorts data into Excel files, while also implementing a progress bar for user feedback. Additionally, it describes the use of dynamic arrays and error handling to ensure efficient data management during the import process.

Uploaded by

钱哥
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

CFD代码

The document contains Visual Basic code for importing CFD data from desktop and Linux systems, optimizing the process to reduce import time significantly. It details the structure of the code, including how it reads, processes, and sorts data into Excel files, while also implementing a progress bar for user feedback. Additionally, it describes the use of dynamic arrays and error handling to ensure efficient data management during the import process.

Uploaded by

钱哥
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Appendix D

Visual Basic code: Importing CFD


data

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

inputpath = Cells(2, 2) 'the path of the files


filenames = Cells(3, 2)
nr_of_first_file = MainSheet.Cells(7, 2) 'The number of the first timestep dumped
timesteps = Cells(4, 2) 'how many timesteps do you have data for
skip = Cells(5, 5) 'how many timestep do you want to skip to achive statistical data
Outputfile1 = Cells(2, 7) 'The file for line1
Outputfile2 = Cells(3, 7) 'The file for line2
Outputfile3 = Cells(4, 7) 'The file for line3
Outputfile4 = Cells(5, 7) 'The file for line4
Outputfile5 = Cells(6, 7) 'The file for line5
start_print = Cells(8, 5) 'Where to start print in the outfiles
start_print_pos = Cells(7, 5) 'Where to print the position vector in the outfiles
frmProgressBar.LabelProgress.Width = 0
frmProgressBar.Label3 = "Excecution time..."
frmProgressBar.Show False
Excel.Application.ScreenUpdating = False
ReDim aSheet1(1 To timesteps, 1 To 250) 'resize the dynamic array
ReDim aSheet2(1 To timesteps, 1 To 250) 'resize the dynamic array
ReDim aSheet3(1 To timesteps, 1 To 250) 'resize the dynamic array
ReDim aSheet4(1 To timesteps, 1 To 250) 'resize the dynamic array
ReDim aSheet5(1 To timesteps, 1 To 250) 'resize the dynamic array
ReDim posVec1(1 To 1, 1 To 250) 'resize the dynamic array
ReDim posVec2(1 To 1, 1 To 250) 'resize the dynamic array
ReDim posVec3(1 To 1, 1 To 250) 'resize the dynamic array
ReDim posVec4(1 To 1, 1 To 250) 'resize the dynamic array
ReDim posVec5(1 To 1, 1 To 250) 'resize the dynamic array

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

For l = 1 To UBound(aOut) 'runs through aOut


'Check if tab is included in text string
If VBA.InStr(1, aOut(l), VBA.Chr(9)) = 0 Then
ReDim aOut2(0 To 0)
aOut2(0) = aOut(l) 'No tab is found

ElseIf VBA.InStr(1, aOut(l), VBA.Chr(9)) <> 0 Then


aOut2 = VBA.Split(aOut(l), VBA.Chr(9)) 'tab found
Else
ReDim aOut2(0 To 0)
aOut2(0) = aOut(l) 'No tab is found
End If
ant = UBound(aOut2)
Dim strLen As Long
If l < UBound(aOut) Then
strLen = VBA.Len(aOut2(0))
If strLen > 1 Then 'Remove linefeed if found
If VBA.InStr(strLen, aOut2(0), VBA.Chr(13)) <> 0 Then
aOut2(0) = VBA.Left(aOut2(0), strLen - 1)
End If
End If

aOut2(0) = VBA.Trim(aOut2(0))
FilTekst = VBA.Right(aOut2(0), 3)
Søkestreng = VBA.CStr(linje + 1) & Chr(34) & ")"

If VBA.InStr(1, FilTekst, VBA.Chr(10)) = 0 Then


FilTekst = FilTekst ''i.e no linefeed found
Else
FilTekst = VBA.Replace(FilTekst, VBA.Chr(10), "")
End If

If VBA.InStr(1, FilTekst, VBA.Chr(13)) = 0 Then


FilTekst = FilTekst
Else
FilTekst = VBA.Replace(FilTekst, VBA.Chr(13), "")
End If

If FilTekst = Søkestreng Or l = UBound(aOut) Then


NyLine = True
Else
NyLine = False
End If
ElseIf l = UBound(aOut) Then
NyLine = True
End If

If ant >= 0 Or l = UBound(aOut) Then


If NyLine Then
linje = linje + 1
End If
End If

frmProgressBar.Label2 = "Linjenr.: " & VBA.CStr(linje)


Call updateProgressbar2(VBA.CLng(l), VBA.CLng(UBound(aOut)))

If ant > 0 Then

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

Select Case linje 'places each line in the correct matrix


Case 1
n1 = n1 + 1
aSheet1(m, n1) = vel
posVec1(m, n1) = pos
Case 2
n2 = n2 + 1
aSheet2(m, n2) = vel
posVec2(m, n2) = pos
Case 3
n3 = n3 + 1
aSheet3(m, n3) = vel
posVec3(m, n3) = pos
Case 4
n4 = n4 + 1
aSheet4(m, n4) = vel
posVec4(m, n4) = pos
Case 5
n5 = n5 + 1
aSheet5(m, n5) = vel
posVec5(m, n5) = pos
End Select
Else
Select Case linje
Case 1
n1 = n1 + 1
aSheet1(m, n1) = vel
Case 2
n2 = n2 + 1
aSheet2(m, n2) = vel
Case 3
n3 = n3 + 1
aSheet3(m, n3) = vel
Case 4
n4 = n4 + 1
aSheet4(m, n4) = vel
Case 5
n5 = n5 + 1
aSheet5(m, n5) = vel
End Select
End If
End If
End If
Next l
.Close
End With
Set f = Nothing
Set fs = Nothing
Else
a=1
End If
If m = timesteps Then
If n1 = 0 Then n1 = 1
If n2 = 0 Then n2 = 1
If n3 = 0 Then n3 = 1
If n4 = 0 Then n4 = 1
If n5 = 0 Then n5 = 1

ReDim Preserve aSheet1(1 To timesteps, 1 To n1)


Workbooks(Outputfile1) _
.Worksheets(1).Cells(start_print, 3).Resize(timesteps, n1) = aSheet1
ReDim Preserve posVec1(1 To 1, 1 To n1)
Workbooks(Outputfile1) _
.Worksheets(1).Cells(start_print_pos, 3).Resize(1, n1) = posVec1

ReDim Preserve aSheet2(1 To timesteps, 1 To n2)


Workbooks(Outputfile2) _
.Worksheets(1).Cells(start_print, 3).Resize(timesteps, n2) = aSheet2
ReDim Preserve posVec2(1 To 1, 1 To n2)
Workbooks(Outputfile2) _
.Worksheets(1).Cells(start_print_pos, 3).Resize(1, n2) = posVec2

ReDim Preserve aSheet3(1 To timesteps, 1 To n3)


Workbooks(Outputfile3) _
.Worksheets(1).Cells(start_print, 3).Resize(timesteps, n3) = aSheet3
ReDim Preserve posVec3(1 To 1, 1 To n3)
Workbooks(Outputfile3) _
.Worksheets(1).Cells(start_print_pos, 3).Resize(1, n3) = posVec3

ReDim Preserve aSheet4(1 To timesteps, 1 To n4)


Workbooks(Outputfile4) _
.Worksheets(1).Cells(start_print, 3).Resize(timesteps, n4) = aSheet4
ReDim Preserve posVec4(1 To 1, 1 To n4)
Workbooks(Outputfile4) _
.Worksheets(1).Cells(start_print_pos, 3).Resize(1, n4) = posVec4

ReDim Preserve aSheet5(1 To timesteps, 1 To n5)


Workbooks(Outputfile5) _
.Worksheets(1).Cells(start_print, 3).Resize(timesteps, n5) = aSheet5
ReDim Preserve posVec5(1 To 1, 1 To n5)
Workbooks(Outputfile5) _
.Worksheets(1).Cells(start_print_pos, 3).Resize(1, n5) = posVec5

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

Visual Basic Code: Generating


droplet profil

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

'Declares all the used variables inthe routine


Dim rowNo#
Dim rngLastRow As Excel.Range
Dim m_timestep As Double, m_flowrate As Double, d_mean As Double, dens_exxsol As Double
Dim n_spread As Double, pi As Double, height As Double, wid As Double, m_fractot As Double
Dim mtot As Double, Rn As Double, root As Double, d As Double, m_d As Double, m_frac As Double
Dim c As Double
Dim wid_step As Double, w As Double
Dim counter As Long, s As Long, m As Long, h As Long, num As Long, k As Long
Dim Leftcol As String, rightcol As String
Dim PctDone As Single

'Prepares the size of the progressbar


Set rngLastRow = Me.Range("rngStartOutput").End(xlDown)
rngLastRow = rngLastRow.Row

rowNo = rngLastRow - Me.Range("rngStartOutput").Row + 1

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

'Starting the progress bar


frmProgressBar.LabelProgress.Width = 0
frmProgressBar.Label1 = "Making Rosin-ramler distributed diameter"
frmProgressBar.Show False

'This while loop makes the Rosin-ramler distributed diameter


'The loop will continue until mass of the droplets exceed the total mass for one timestep
Do While mtot < m_timestep
Rn = (Int((10 ^ 6 - 0 + 1) * Rnd + 0)) / 10 ^ 6
root = -VBA.Math.Log(Rn)
' d = 175
d = d_mean * (root) ^ (1 / n_spread) 'Making the diameter for one droplet
counter = counter + 1
m_d = pi / 6 * (d * 10 ^ (-6)) ^ 3 * dens_exxsol 'Calculating the mass of the droplet
m_frac = m_d / m_timestep
mtot = mtot + m_d 'Summarize the total mass of the made drop
let
m_fractot = m_fractot + m_frac
Sheet1.Cells(50 + counter, 10) = d * 10 ^ (-6)
Sheet1.Cells(50 + counter, 11) = 300
Sheet1.Cells(50 + counter, 12) = m_frac * m_flowrate
Sheet1 - 2

PctDone = mtot / (m_timestep)


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

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

You might also like