Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7645633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:59:22+00:00 2026-05-31T09:59:22+00:00

A simulation program I’m using requires a text based input file. I need to

  • 0

A simulation program I’m using requires a text based input file. I need to run the simulation in different configurations by changing the values in the text file. I am looking for a way to do this automatically with any script that does not require third party compilers. It has to run natively on a Windows XP machine. I only have a little bit of coding experience in MATLAB and FORTRAN.

I will describe my idea of what the script should do in some pseudo-code:

% speed.txt - a txt file with 10 different speed values
% coeff.txt - a txt file with 10 different coefficients
% dist.txt - a txt file with 5 different distance values
% input.txt - the txt file containing the input parameters. This file has to be changed.
% output.txt- the output of the simulation
% sp - the i-th speed value
% co - the i-th coeff value
% di - the j-th distance value
% D:\FO - Final Output folder

Read input.txt
for i = 1:10
    Display i on screen   % so I know how much of the batch is done
    Go to line 37, overwrite 1st four characters with i-th value from speed.txt
    Go to line 68, overwrite 1st eight characters with i-th value from coeff.txt
    for j = 1:5
         Display j on screen   % so I know how much of the batch is done
         Go to line 67, overwrite 1st five characters with j-th value from dist.txt
         Run simulation.exe
         When simulation is done, get output.txt, rename it to "output_sp_co_di.txt"
         and move the file to D:\FO
    end
end

I hope that this is possible with a .bat or .vbs script (or anything else that will run natively). All help is greatly appreciated.


EDIT: after some advice I started a vbs script. I have never used that language before but pulled the thing here under together from scraps on the internet:

Option Explicit

Dim objFSO, strTextFile, strData, strLine, arrLines
Dim filesys, filetxt, path
Dim speed(10), ct(10), dist(4), text(73), d(4)
Dim i, j, k
i = 0
j = 0
k = 0

speed(0) = 3.0
speed(1) = 5.0
speed(2) = 7.0
speed(3) = 9.0
speed(4) = 11.0
speed(5) = 13.0
speed(6) = 15.0
speed(7) = 17.0
speed(8) = 19.0
speed(9)= 21.0
speed(10)= 22.0

ct(0) = 0.987433
ct(1) = 0.816257
ct(2) = 0.816361
ct(3) = 0.720357
ct(4) = 0.418192
ct(5) = 0.239146
ct(6) = 0.154534
ct(7) = 0.107608
ct(8) = 0.079057
ct(9)= 0.060437
ct(10)= 0.053465

dist(0) = 173.48
dist(1) = 260.22
dist(2) = 346.96
dist(3) = 433.7
dist(4) = 520.44

d(0) = 2
d(1) = 3
d(2) = 4
d(3) = 5
d(4) = 6


CONST ForReading = 1

'name of the text file
strTextFile = "TurbSim.inp"

'Create a File System Object
Set objFSO = CreateObject("Scripting.FileSystemObject")


'Open the text file - strData now contains the whole file
strData = objFSO.OpenTextFile(strTextFile,ForReading).ReadAll

'Split the text file into lines
arrLines = Split(strData,vbCrLf)

'Step through the lines
For Each strLine in arrLines
    text(i) = strLine
    i = i + 1
Next

'Open text file to write to
path = objFSO.GetAbsolutePathName("D:\Sandbox\TurbSim.inp")

For i = 0 To 10
    If i = 0 Then
        text(37) = Replace(text(37),"UUUU",speed(i))
        text(68) = Replace(text(68),"CCCCCCCC",ct(i))
    Else
        text(37) = Replace(text(37),speed(i-1),speed(i))
        text(68) = Replace(text(68),ct(i-1),ct(i))
    End If
    For j = 0 To 4
        If j = 0 Then
            text(67) = Replace(text(67),"DDDDD",dist(j))
        Else
            text(67) = Replace(text(67),dist(j-1),dist(j))
        End If

        Set filetxt = objFSO.opentextfile("D:\Sandbox\TurbSim.inp", 2, True)
        For k = 0 To 73
            if k = 73 Then
                filetxt.write text(k)
            Else
                filetxt.write text(k) & vbCr & vbLf
            End If
            objFSO.CopyFile "D:\Sandbox\TurbSim.inp", _
            "D:\Sandbox\input\TurbSim_" & speed(i) & "_" & d(j) &"D.inp"
        Next
        filetxt.close
    Next
Next    


' wscript.echo text(37)
' wscript.echo text(68)
' wscript.echo text(67)

filetxt.Close
'Cleanup
' Set filesys = Nothing
Set objFSO = Nothing

Problem is now that the distance part (the j-loop) is not working properly. From the output generated (TurbSim_speed_dD.inp) I see that only the last distance (520.44) is used. I don’t really understand why, I’ll look into that later. If anyone has a suggestion for improvement, then you’re ideas are always welcome.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T09:59:23+00:00Added an answer on May 31, 2026 at 9:59 am

    I have managed to pull together a .vbs script from various internet references that does what I want. The script does the following:

    1. Reads in original input file
    2. Stores the data line by line in the text array
    3. Reads in speed, coefficient and distance data from text file
    4. Stores this data in separate speed, coefficient and distance arrays line by line.
    5. Takes first entry of speed and coeff. array and writes it at appropriate places in the
      text array
    6. Loops through distance array and writes the text array line by line back to an .inp file
    7. Runs the simulation with the edited input file
    8. Waits until simulation is terminated.
    9. Copy output files from current directory to output folder, renaming them in the process.
    10. Wait 10 seconds to make sure copying is finished
      10.Repeat steps 6-10 with all other entries of speed and coeff. array

    Requirements for this script to work:

    A. An input folder with an .inp file that has “UUUU”, “DDDDD”, “CCCCCCCC” written at places where respectively the velocity, distance and coefficient should be written.

    B. An output folder

    C. The files speed.txt, ct.txt and distance.txt containing the speed, coefficient and distance values to be used.

    D. You should run this script from an admin account. Otherwise you don’t have the permission to check if the simulation is still running with the “Win32_process”.

        Option Explicit
    
    Dim objFSO, strTextFile, strTData, strLine, arrTLines
    Dim strVelFile, strCtFile, strDistFile
    Dim strVData, strCData, strDData
    Dim arrVLines, arrCLines, arrDLines
    Dim strLineV, strLineC, strLineD
    Dim strComputer, oWMI, colEvents, oEvent
    Dim filesys, filetxt, path, curPath
    Dim speed(), ct(), dist(), text(73)
    Dim oShell
    Dim i, j, k
    i = 0
    j = 0
    k = 0
    
    ' Subroutine to start an executable
    Sub Run(ByVal sFile)
    Dim shell
    
        Set shell = CreateObject("WScript.Shell")
        shell.Run Chr(34) & sFile & Chr(34), 1, false
        Set shell = Nothing
    End Sub
    
    CONST ForReading = 1
    
    ' Create a File System Object
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    
    ' Create Shell object. Needed to change directories
    Set oShell = CreateObject("WScript.Shell")
    
    'Change current directory to \input folder
    oShell.CurrentDirectory = ".\input"
    
    ' The name of the original input file 
    ' with the UUUU, DDDDD, CCCCCCC in the correct places
    strTextFile = "TurbSim.inp"
    
    ' Open the text file and read it all into strTData
    strTData = objFSO.OpenTextFile(strTextFile,ForReading).ReadAll
    
    ' Go back to parent folder as there all the other .txt files reside
    Set oShell = CreateObject("WScript.Shell")
    oShell.CurrentDirectory = ".\.."
    
    ' name of other input text files
    strVelFile = "speed.txt"
    strCtFile = "ct.txt"
    strDistFile = "dist.txt"
    
    ' Open the text file - str*Data now contains the whole file
    strVData = objFSO.OpenTextFile(strVelFile,ForReading).ReadAll
    strCData = objFSO.OpenTextFile(strCtFile,ForReading).ReadAll
    strDData = objFSO.OpenTextFile(strDistFile,ForReading).ReadAll
    
    ' Split the text files into lines
    arrTLines = Split(strTData,vbCrLf)
    arrVLines = Split(strVData,vbCrLf)
    arrCLines = Split(strCData,vbCrLf)
    arrDLines = Split(strDData,vbCrLf)
    
    ' Give the speed, ct and dist arrays their dimension
    ReDim speed(UBound(arrVLines))
    ReDim ct(UBound(arrCLines))
    ReDim dist(UBound(arrDLines))
    
    ' Add data to arrays text, speed, ct and dist line by line
    For Each strLine in arrTLines
        text(i) = strLine
        i = i + 1
    Next
    
    'Reset counter
    i = 0
    
    ' Step through the lines speed
    For Each strLineV in arrVLines
        speed(i) = strLineV
        i = i + 1
    Next
    i = 0
    ' Step through the lines ct
    For Each strLineC in arrCLines
        ct(i) = strLineC
        i = i + 1
    Next
    i = 0
    ' Step through the lines dist
    For Each strLineD in arrDLines
        dist(i) = strLineD
        i = i + 1
    Next
    i = 0
    
    ' Get the current path. Needed to point to the executable.
    curPath = objFSO.GetAbsolutePathName(".")
    
    For i = 0 To UBound(speed)
        If i = 0 Then 
            ' Replace the UUUU and CCCCCCCC values
            ' Only the first run.       
            text(37) = Replace(text(37),"UUUU",speed(i))
            text(68) = Replace(text(68),"CCCCCCCC",ct(i))
        Else ' Replace the previous speed and coeff. values with the current one
            text(37) = Replace(text(37),speed(i-1),speed(i))
            text(68) = Replace(text(68),ct(i-1),ct(i))
        End If
        For j = 0 To UBound(dist)
            If j = 0 And i = 0 Then 
                ' Replace the DDDDD value (only at first run)
                text(67) = Replace(text(67),"DDDDD",dist(j))
            ElseIf j = 0 And i > 0 Then 
                ' Replace the distance value of the previous speed/coeff. case
                ' with the current one
                text(67) = Replace(text(67), dist(UBound(dist)), dist(j))
            Else    ' Replace the previous distance value with the current one
                text(67) = Replace(text(67),dist(j-1),dist(j))
            End If
            Set filetxt = objFSO.opentextfile(curPath & "\TurbSim.inp", 2, True)
            For k = 0 To 73 ' Write to an .inp file line by line
                if k = 73 Then ' Prevent adding a new line at the end
                    filetxt.write text(k)
                Else
                    filetxt.write text(k) & vbCr & vbLf
                End If
            Next
            filetxt.close
            ' Execute the simulation
            Run curPath &"\TurbSimGW.exe"
    
            strComputer = "."
            Set oWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    
            '# Create an event query to be notified within 3 seconds when TurbSimGW is closed
            Set colEvents = oWMI.ExecNotificationQuery _
                ("SELECT * FROM __InstanceDeletionEvent WITHIN 3 " _
                 & "WHERE TargetInstance ISA 'Win32_Process' " _
                 & "AND TargetInstance.Name = 'TurbSimGW.exe'")
    
            '# Wait until TurbSimGW is closed
            Set oEvent = colEvents.NextEvent
    
            ' Copy and rename output files
            objFSO.CopyFile curPath & "\TurbSim.wnd", _
            curPath & "\output\TurbSim_" & speed(i) & "_" & j+2 &"D.wnd"
            wscript.sleep 10000  ' time to allow copying the files
        Next
    Next    
    
    
    '' ' wscript.echo text(37)
    '' ' wscript.echo text(68)
    '' ' wscript.echo text(67)
    
    filetxt.Close
    ' Cleanup
    ' Set filesys = Nothing
    Set objFSO = Nothing
    

    Now it works flawlessly. However A solution to requirement D would be nice. My work-around is instead of checking if the program is terminated I just set a sleep value. For this value of sleep I know for sure the simulation is done and the output files are ready to copy.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is the result of a profiled simulation run of my MATLAB program. I
I'm building a program that uses weather simulation using OpenGL. I want to include
I'm trying to write a driving simulation program and I need some help on
I'm currently writing a program where I need to run many simulations and speed
I would like to implement a simulation program, which requires the following structure: It
Right now I am writing a simulation program which output is formatted according to
I have a bash script that runs a simulation program written in Fortran 90,
I'm writing a program in C++ to perform a simulation of particular system. For
I a writing a Simulation program and wondering if the use of const double
I have a program for some scientific simulation stuff, and as such needs to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.