mmi script for preserving output data for 3 iterations

Upload: moisixhoxhi

Post on 03-Jun-2018

218 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/12/2019 MMI Script for Preserving Output Data for 3 Iterations

    1/15

    Option Explicit

    '=============================================================================

    ' ---------------------------- Main Program -------------------------------

    ' This program demonstrates how to preseve "Additional Output Data" files

    ' from multiple simulation runs

    '

    ' A simulation can be configured to produce additional otput data files

    ' ( menu: "Simulation->Additional Output Data...")

    '

    ' If the simulation is run more than once,the additional data files will be

    ' overwritten. This program executes multiple simulations in the iteration loop.

    ' To preserve the additonal output data, after each iteration step

    ' the additonal output data files are renamed

    ' ( the current iteration number is appended to the output data file name).

    '******************************************************************************

    '************************ VERY IMPORTANT ************************

    '******** Edit "Additional Output Data Files" section below *******

    '******************************************************************************

    ' variables 'sCF_OutputFile' and 'sRID_OutputFile' should hold the names

    ' of the output data files as specified in the dialog "Additional Output Data"

    ' (Menu: "Simulation->Additional Output Data...")

  • 8/12/2019 MMI Script for Preserving Output Data for 3 Iterations

    2/15

    ' If no file name is specified in the "Additional Output Data" dialog

    ' these variables need to be set to a string of length zero

    '==============================================================================

    Dim sCF_OutputFile, sRID_OutputFile

    '********** Additional Output Data Files ****** START **********

    sCF_OutputFile = "" ' ComplexField output file

    sRID_OutputFile = "" ' RefractiveIndexDistribution output file

    'sCF_OutputFile = "" 'uncomment this line if the ComplexField file is not requierd

    'sRID_OutputFile = "" 'uncomment this line if the RefractiveIndexDistribution file is not requierd

    '********** Additional Output Data Files ******* END **********

    '==============================================================================

    Dim DEBUGFLAG

    DEBUGFLAG = False

    Dim LogWriter

    Set LogWriter = New cLogWriter

    Dim FileUtil

    Set FileUtil = New cFileUtil

    Dim sProjectFolderAbsPath

    sProjectFolderAbsPath = AppMgr.GetProjectFolderName()

  • 8/12/2019 MMI Script for Preserving Output Data for 3 Iterations

    3/15

    Dim sCF_OutFileAbsPath

    sCF_OutFileAbsPath = sProjectFolderAbsPath & sCF_OutputFile

    Dim sRID_OutFileAbsPath

    sRID_OutFileAbsPath = sProjectFolderAbsPath & sRID_OutputFile

    Dim sTargetFileAbsPath, bCopyResult, sNewFileName

    Dim WavelengthValue

    ' ----------- Iteration Loop ---------

    Dim nIterationIdx

    For nIterationIdx = 1 to 3

    '-----------------------------------------

    ' SETTING SIMULATION PARAMETERS

    If nIterationIdx = 1 Then

    WavelengthValue = 0.647

    End If

    If nIterationIdx = 2 Then

    WavelengthValue = 1.294

    End If

    If nIterationIdx = 3 Then

    WavelengthValue = 2.588

  • 8/12/2019 MMI Script for Preserving Output Data for 3 Iterations

    4/15

    End If

    ParamMgr.SetParam "Wavelength" , WavelengthValue

    LogWriter.WriteMsg( "Current iteration: " & nIterationIdx & " Wavelength: " &

    WavelengthValue )

    '-----------------------------------------

    ParamMgr.Simulate

    WGMgr.Sleep( 50 )

    If AppMgr.RunsInSimulator() = True Then

    ' Abort script if the simulator failed to create output file

    Call CheckForOutputFile( sCF_OutputFile )

    Call CheckForOutputFile( sRID_OutputFile )

    End If

    If AppMgr.RunsInSimulator() = False And DEBUGFLAG = True Then

    ' When run in simulator 'ParamMgr.Simulate' will generate the output file

    ' When run in the Layout Designer the call 'ParamMgr.Simulate'

    ' has no effect and for validation (debugging) purposes the file

    ' is generated by the code below

    Call CreateFileInDesigner( sCF_OutputFile )

    Call CreateFileInDesigner( sRID_OutputFile )

    ' Abort script if failed to create output file

    Call CheckForOutputFile( sCF_OutputFile )

  • 8/12/2019 MMI Script for Preserving Output Data for 3 Iterations

    5/15

    Call CheckForOutputFile( sRID_OutputFile )

    End If

    ' rename output files so they will not be overwritten

    ' by the next simulation

    Call RenameOutputFile( sCF_OutputFile , nIterationIdx)

    Call RenameOutputFile( sRID_OutputFile, nIterationIdx)

    Next

    ' --- End of main program

    '=============================================================================

    '------------------------------------------------------------------------------

    Sub CreateFileInDesigner( aFileName)

    ' Takes one argument, a name of the file to create

    ' If the script is run in the "Profile Designer" the file is created

    ' If the script is run in the "Simulator" this function does nothing

    If AppMgr.RunsInSimulator() = False and aFileName "" Then

    Dim sFileAbsPath

    sFileAbsPath = sProjectFolderAbsPath & aFileName

    FileUtil.CreateFile(sFileAbsPath)

  • 8/12/2019 MMI Script for Preserving Output Data for 3 Iterations

    6/15

    End If

    End Sub

    '------------------------------------------------------------------------------

    Sub CheckForOutputFile( aFileName)

    ' takes one argument, a filename

    ' Terminates program if a file by this name does not exist in the project folder

    If aFileName "" Then

    Dim sFileAbsPath

    sFileAbsPath = sProjectFolderAbsPath & aFileName

    If FileUtil.FileExists( sFileAbsPath ) = False Then

    LogWriter.WriteMsg( "Simulation output data file doesn't exist: '"& aFileName & "'")

    LogWriter.WriteMsg("Make sure the file names in the variables 'CF_OutputFile' and

    'RID_OutputFile' match the names in 'Additional Output Data' dialog")

    AbortScript( "Simulation output data file doesn't exist: '"& aFileName & "'" )

    End If

    End If

    End Sub

    '------------------------------------------------------------------------------

    Sub RenameOutputFile(origOutuputFile, iterationNumber)

    ' Renames the file by appending the number to the file basename

    ' If the origOutuputFile does not exist nothing is done

    Dim nSize

    nSize =4

    If origOutuputFile "" Then

    Dim sNewFileName

    sNewFileName = FileUtil.GenerateFileName( origOutuputFile, iterationNumber, nSize )

  • 8/12/2019 MMI Script for Preserving Output Data for 3 Iterations

    7/15

    Dim sTargetFileAbsPath

    sTargetFileAbsPath = sProjectFolderAbsPath & sNewFileName

    Dim sSourceFileAbsPath

    sSourceFileAbsPath = sProjectFolderAbsPath & origOutuputFile

    If FileUtil.FileExists( sSourceFileAbsPath ) = True Then

    Call FileUtil.RenameFile( sSourceFileAbsPath, sTargetFileAbsPath )

    LogWriter.WriteMsg("Current iteration: '" & iterationNumber & "' " & " '" &

    origOutuputFile & "'" & " saved as '" & sNewFileName & "'")

    End If

    End If

    End Sub

    '------------------------------------------------------------------------------

    Sub AbortScript( sReason )

    ' Force runtime error to break script execution

    Call Err.Raise( 507, "Forced termination:", CStr( sReason ))

    End Sub

    '=============================================================================

    '------------------------------------------------------------------------------

    '----- VBS Class definitions

    '------------------------------------------------------------------------------

    '=============================================================================

    '================== ****** ******* ====================

  • 8/12/2019 MMI Script for Preserving Output Data for 3 Iterations

    8/15

    Class cFileUtil

    ' Facilitates some operations on files.

    ' Has the following public subroutines:

    ' CopyFile,RenameFile, DeleteFile CreateFile, FileExists, GenerateFileName

    '------------------------------------------------------------------------------

    Public Function CopyFile( sSourceFileAbsPath , sTargetFileAbsPath )

    ' copies a file

    ' takes 2 arguments, absolute paths to the source file

    ' and target file location

    ' the target folder must already exist

    Dim bSuccess

    bSuccess = False

    If moFSO.FileExists( sSourceFileAbsPath ) = True Then

    Call moFSO.CopyFile (sSourceFileAbsPath,sTargetFileAbsPath )

    bSuccess = True

    End If

    CopyFile = bSuccess

    End Function

    '------------------------------------------------------------------------------

    Public Sub RenameFile( source,target )

    ' Renames a file, takes 2 arguments

    ' first argument: absolute path to the file to be reanamed

    ' second argument: absolute paht to the file with the new name

    ' if the file wiht the new name exists it is deleted before

  • 8/12/2019 MMI Script for Preserving Output Data for 3 Iterations

    9/15

    ' the old file is renamed

    DeleteFile( target )

    moFSO.MoveFile source, target

    End Sub

    '------------------------------------------------------------------------------

    Public Sub DeleteFile( sFileAbsPath )

    ' Takes absolute path to a file

    ' Deletes the file if it exists

    Dim aFile

    If FileExists( sFileAbsPath ) Then

    Set aFile = moFSO.GetFile( sFileAbsPath )

    aFile.Delete

    End If

    End Sub

    '------------------------------------------------------------------------------

    Public Function CreateFile( sFileAbsPath )

    ' takes absolute file path

    ' creates a file if it does not exist

    ' the target folder must already exist

    Dim MyFile

    If moFSO.FileExists( sFileAbsPath ) = False Then

  • 8/12/2019 MMI Script for Preserving Output Data for 3 Iterations

    10/15

    Set MyFile = moFSO.CreateTextFile(sFileAbsPath, True)

    MyFile.Close

    End If

    End Function

    '------------------------------------------------------------------------------

    Public Function FileExists( sFileAbsPath )

    'takes absolute file path

    'returns True if the file exists, False otherwise

    FileExists = moFSO.FileExists( sFileAbsPath )

    End Function

    '------------------------------------------------------------------------------

    Public Function GenerateFileName( sFileName, nFileNumber, nSize )

    ' generates new file name

    ' takes the name of the original file , a number to append and the size of

    ' the string to append

    ' appends a string of lenght 'nSize" to the original file basename.

    ' The string to append is generated by padding 'nFileNumber' with zeros

    Dim moFSO

    Set moFSO = CreateObject("Scripting.FileSystemObject")

    Dim sBaseName

    sBaseName = moFSO.GetBaseName( sFileName )

  • 8/12/2019 MMI Script for Preserving Output Data for 3 Iterations

    11/15

    Dim sExtName

    sExtName = moFSO.GetExtensionName( sFileName )

    Dim sNewName, sNumberStr, sPaddedNumberStr, nNumberStrLen

    sNumberStr = CStr(nFileNumber)

    nNumberStrLen = Len(CStr(sNumberStr))

    If nNumberStrLen > nSize Then

    nSize = nNumberStrLen

    End If

    sPaddedNumberStr = String(nSize-nNumberStrLen, "0")

    GenerateFileName =

    CStr(sBaseName)&CStr("_")&CStr(sPaddedNumberStr)&CStr(sNumberStr)&CStr(".")&CStr(sExtName)

    End Function

    '------------------------------------------------------------------------------

    ' ----- Constructor -----

    Private Sub Class_Initialize

    Set moFSO = CreateObject("Scripting.FileSystemObject")

    End Sub

    '------------------------------------------------------------------------------

    ' ----- Destructor -----

    Private Sub Class_Terminate

  • 8/12/2019 MMI Script for Preserving Output Data for 3 Iterations

    12/15

    Set moFSO = Nothing

    End Sub

    '------------------------------------------------------------------------------

    Private moFSO

    End Class 'cFileUtil

    '=============================================================================

    '================== ****** ******* ====================

    Class cLogWriter

    '------------------------------------------------------------------------------

    Public Sub WriteMsg( aString )

    AppMgr.OutputMsg( aString)

    If AppMgr.RunsInSimulator() = True Or DEBUGFLAG = True Then

    moFile.WriteLine (aString)

    End If

    End Sub

    '------------------------------------------------------------------------------

    Private Sub Class_Initialize ' Setup Initialize event.

    ' Creates the output file if it does not exists

    ' Truncates the output file if it exists

    ' The name of the output (log) file is generated by appending string "_vbscript.log"

    ' to the base name of the project file

  • 8/12/2019 MMI Script for Preserving Output Data for 3 Iterations

    13/15

    Set moFSO = CreateObject("Scripting.FileSystemObject")

    Dim ForWriting

    ForWriting = 2

    'check if the folder exists

    Dim sProjectFolderAbsPath

    sProjectFolderAbsPath = AppMgr.GetProjectFolderName()

    Dim sProjectFileName

    sProjectFileName = AppMgr.GetProjectFileName()

    Dim sProjectFileBaseName

    sProjectFileBaseName = moFSO.GetBaseName( sProjectFileName )

    Dim bProjectFolderExists

    bProjectFolderExists = moFSO.FolderExists( sProjectFolderAbsPath)

    If Not bProjectFolderExists Then

    AbortScript( "Folder '" & sProjectFolderAbsPath & "' does not exist" )

    End If

    Dim sLogFileAbsPath

    sLogFileAbsPath = sProjectFolderAbsPath & sProjectFileBaseName & "_vbscript.log"

  • 8/12/2019 MMI Script for Preserving Output Data for 3 Iterations

    14/15

    Set moFile = Nothing

    If AppMgr.RunsInSimulator() = True Or DEBUGFLAG = True Then

    Set moFile = moFSO.OpenTextFile( sLogFileAbsPath, ForWriting, True )

    End If

    End Sub

    '------------------------------------------------------------------------------

    Private Sub Class_Terminate ' Setup Terminate event.

    Dim uninitialized

    uninitialized = moFile Is Nothing

    If Not uninitialized then

    moFile.Close

    End If

    Set moFile = Nothing

    End Sub

    '------------------------------------------------------------------------------

    Private moFSO

    Private moFile

    '------------------------------------------------------------------------------

    End Class ' cLogWriter

    '=============================================================================

  • 8/12/2019 MMI Script for Preserving Output Data for 3 Iterations

    15/15