HyperStudy

Solver Script Files

Solver Script Files

Previous topic Next topic Expand/collapse all hidden text  

Solver Script Files

Previous topic Next topic JavaScript is required for expanding text JavaScript is required for the print function  

In HyperStudy, each model is associated with a solver execution script. The role of the solver script is to provide the name and location of the file that HyperStudy uses to execute the model. There is no unique method to the construction of these files. The scripts can be constructed in any language, and its contents can be as simple as a single line or a detailed set of commands. This generality is intentional so that HyperStudy remains flexible enough to be wrapped around any non-interactive process.

For more information on how to register solver script files in HyperStudy, refer to Register Solver Scripts.

register_solver_script

For each run, HyperStudy creates a separate run folder. In the case of multiple models, a separate model folder is also created. These folders are called Study Run folders.
For each run, HyperStudy writes the solver input file to the Study Run folder.
If any other files need to reside in the Study Run folder, they will need to be copied. For example, RADIOSS needs a starter file and an engine file to reside in the Study Run folder. A file can be copied from the study directory by adding its name to the solver input file. Separate names in the solver input file with a semi-colon.
In order for HyperStudy to execute properly, verify that the solver returns control back to HyperStudy only after the execution is finished. Otherwise, HyperStudy will attempt to extract results before all files are finished writing and the study will fail. In order to avoid, this, if possible, the solver should be run in interactive mode. Otherwise, you will need to include a wait command in your batch file.

For abaqus, this is <PATH>/abaqus.exe job=jobname.inp interactive

In a study that uses more than one model, the models are executed in a sequence determined by HyperStudy. To control the sequence of runs, specify the priority option for the model. The results are extracted after the solvers have finished, or earlier depending on any model dependencies.
A failure during the script execution can be noted by creating a file titled task__exe_err.txt. If this file is present in the run directory, HyperStudy will detect the execution as a failure. A similar error file can be created for other task failures: write (task__wri_err.txt), extraction (task__ext_err.txt) and purge (task__pur_err.txt).

 

Role of a Solver Script


A HyperStudy model is a construct that maps a set of independent variables to a set of dependent output responses. The model consists of three separate steps: writing, executing, and extracting. The writing of the model files is the process of getting the independent variable out of HyperStudy and into another format. Most of the time this corresponds to the writing of a file in some format. The opposite is true in the extraction step: data exists in some output files and must be absorbed into HyperStudy. The solver script is responsible for bridging the gap in moving the process along from the input file to the output files. So the question to ask is: “Given an input file, how do I generate the output files?”.

Knowing how to generate the output files will provide the directions on how to construct the solver script. Imagine being given the input file and asked to get the results file. If the process is as simple as submitting the file as direct input to some program’s executable, then the solver script could be as simple as a single line. The solver script should not finish and return control back to HyperStudy until both the process is completed and the output files are generated. If there are many steps involved, such as uploading the file to a server, submitting the file to a queue, waiting for it to finish, and then downloading the file, then the solver script must perform all these actions, too.

A solver script must be able to do everything you would do manually to create the outputs given the input file, but non-interactively.

 

Common CAE Solver Script Inline Commands


Solver script inline commands commonly used in HyperStudy.

Note:For a complete list of available options, refer to the corresponding solver specific documentation.

 

Abaqus

To be used with solver input file $filebasename.

Window: <PATH>/abaqus.exe job=%1 interactive
Linux: <PATH>/abaqus.exe job=$1 interactive

 

ANSYS

Window: <PATH>/ansysXXX.exe -b -i  %1
Linux: <PATH>/ansysXXX.exe -b -i  $1

 

LS-DYNA

Window: <PATH>/dyna.exe i=%1
Linux: <PATH>/dyna.exe i=$1

 

OptiStruct

Window: <PATH>/optistruct.bat %1
Linux: <PATH>/optistruct.bat $1

 

Examples


The following examples are structured to become progressively more complicated. It is recommended that the examples are read in sequence and not treated independently. The examples show the use of OptiStruct, but the script concepts highlighted are general enough to be applied to many processes.

hmtoggle_plus1Direct Call to an Executable

Many programs already have a simple command line executable to begin a batch solution. This command generally takes some additional arguments.  In general, the command line syntax to perform these operations takes the form of an executable command followed by space separated arguments.

[Command] [ Arguments]

 

For example, the file to call the OptiStruct solver may be located, on windows, at “C:\Program Files\Altair\14.0.120\hwsolvers\scripts\optistruct.bat”. This command can take several optional arguments, for example the name of the input file and a selection of the number of cpus.  So the command line syntax in this case would be:

“C:\Program Files\Altair\12.0.110\hwsolvers\scripts\optistruct.bat” test.fem –ncpu 4

 

The [Command] is “C:\Program Files\Altair\14.0.120\hwsolvers\scripts\optistruct.bat”, and [Arguments] is “myFile.fem –ncpu 4”.  The command is stored in HyperStudy as the solver execution script, and the solver input arguments entry would take the text “test.fem  –ncpu 4”.

Note:   In the context of the solver input arguments, the quotes are not needed and are used here to isolate the required text.

Another HyperStudy entry is the solver input file, which is the name of the file that will be written by HyperStudy during the model writing phase. The value of this entry is accessible from the internal variable “$file”, which in turn can be accessed in the solver input arguments, letting HyperStudy substitute this entry for you. In this case the solver input arguments would take the form “$file –ncpu 4”.

solver_script_example_direct_call_to_executable

hmtoggle_plus1Basic Solver Script with Arguments (System Native)

Rather than having to type out a set of input arguments, you can create a general purpose script that has preferred options hard coded. A general purpose script can still take a variable input file. The windows batch file (extension *.bat) that will perform this operation contains the line:

“C:\Program Files\Altair\14.0.110\hwsolvers\scripts\optistruct.bat” %1 –ncpu 4 –core in

 

The syntax %1 is a substitution of the first argument to this script, which should be the input file. The argument %2 would be second argument, and so on. This batch file can be registered as the solver script, and can have the solver input arguments as “$file”.

A similar syntax is available on linux/unix shells. The exact syntax depends on the type of shell script being written, but for Bash shells the syntax is $n to access the nth argument.

solver_script_example_basic_solver_script_arguments_system_native

hmtoggle_plus1Basic Solver Script with Arguments (Python)

Writing a script to run in the system native command layer can be instructive, but these languages are limited. Other languages can be used to create more detailed and featured scripts. This example uses Python, a full featured and platform neutral language. It is recommend that you use a language that you are comfortable. Please see any python reference for details on the specifics of this language.

dialog_register_solver_script

The previous simple script can be re-written to work with python. This script has some additional features such a functionality that corrects operating system path differences and logs the output into text files for better transparency when debugging.

# import statements

import os

import subprocess

import sys

 

f = open('logFile.txt', 'w')

 

#set path to file

file_exe = 'C:/Program Files/Altair/14.0/hwsolvers/scripts/optistruct.bat'

 

#set arguments

file_args = sys.argv[1] + ' -ncpu 4' + ' -core in'

 

#correct the path for the operating system, concatenate, and execute

file_exe = os.path.normpath(file_exe)

lstCommands = [file_exe, file_args]

f.write('Running the command:\n' + ' '.join(lstCommands) + '\n')

p1 = subprocess.call(' '.join(lstCommands), stdout=subprocess.PIPE , stderr=subprocess.PIPE)

 

#write output and close

f.write('\nStandard out:' + '\n' + p1.communicate()[0] + '\n')

f.write('\nStandard error:' + '\n' + p1.communicate()[1] + '\n')

f.close()

hmtoggle_plus1Script with Simple Logic

Sometimes a script must perform several operations in sequence. This script copies a file into the current directory. This script is also designed to take a second argument, which is a file that should be removed if it exists. The same solution as in the previous examples can be executed. The solver script contains the lines:

solver_script_example_script_with_simple_logic

# import statements

import os

import subprocess

import sys

import shutil

 

f = open('logFile.txt', 'w')

 

#set path to files

file_exe = 'C:/Program Files/Altair/14.0/hwsolvers/scripts/optistruct.bat'

file_to_copy = 'C:/Users/jmpajot/Documents/HMath_solutions/HST/documentation_changes/solver_script/target.txt'

 

#set arguments

file_args = sys.argv[1] + ' -ncpu 4' + ' -core in'

 

#copy the file

f.write('\nCopying the file: ' + file_to_copy + '\n')

shutil.copy(os.path.normpath(file_to_copy),os.getcwd())

 

#remove the file if it exists

if os.path.isfile(sys.argv[2]):

  f.write('\nRemoving the file: ' + sys.argv[2] + '\n')

  os.remove(sys.argv[2])

 

 

#correct the path for the operating system, concatenate, and execute

file_exe = os.path.normpath(file_exe)

lstCommands = [file_exe, file_args]

f.write('Running the command:\n' + ' '.join(lstCommands) + '\n')

p1 = subprocess.call(' '.join(lstCommands), stdout=subprocess.PIPE , stderr=subprocess.PIPE)

 

#write output and close

f.write('\nStandard out:' + '\n' + p1.communicate()[0] + '\n')

f.write('\nStandard error:' + '\n' + p1.communicate()[1] + '\n')

f.close()

hmtoggle_plus1Script with Environment Variables

Dynamic scripts can be used to maintain portability by taking advantage of HyperStudy’s environment variables. For example, consider the script in the previous example. The file to be copied is defined with a fully qualified path. Instead you may require that the file be located in the HyperStudy study directory’s _usr directory, and be copied to the current run directory. The _usr directory is always in the study directory, and that path is stored in the environment variable named HST_STUDY_PATH. HyperStudy will write a complete list of available environment variables to a file in the run directory if the verbose level is increased to level 3. Because these are HyperStudy environment variables, a script with these variables can only be run successfully from within HyperStudy. In this case, the above script can be modified as:

# import statements

import os

import subprocess

import sys

import shutil

import time

 

f = open('logFile.txt', 'w')

 

#length of sleep command (seconds)

time_to_sleep = 1

time_out =  10

 

#set path to files

file_exe = 'C:/Program Files/Altair/14.0/hwsolvers/scripts/optistruct.bat'

file_to_copy = 'target.txt'

 

#set arguments

file_args = sys.argv[1] + ' -ncpu 4' + ' -core in'

 

#copy the file

file_to_copy = os.path.join(os.getenv('HST_STUDY_PATH'),'_usr',file_to_copy)

f.write('\nCopying the file: ' + file_to_copy + '\n')

shutil.copy(os.path.normpath(file_to_copy),os.getcwd())

 

#remove the file if it exists

if os.path.exists(sys.argv[2]):

  f.write('\nRemoving the file: ' + sys.argv[2] + '\n')

  os.remove(sys.argv[2])

 

#correct the path for the operating system, concatenate, and execute

file_exe = os.path.normpath(file_exe)

lstCommands = [file_exe, file_args]

f.write('Running the command:\n' + ' '.join(lstCommands) + '\n')

p1 = subprocess.call(' '.join(lstCommands), stdout=subprocess.PIPE , stderr=subprocess.PIPE)

 

 

#write output and close

f.write('\nStandard out:' + '\n' + p1.communicate()[0] + '\n')

f.write('\nStandard error:' + '\n' + p1.communicate()[1] + '\n')

f.close()

hmtoggle_plus1Waiting for an Output File

While most commands wait until they are completed to give control back to the script, some commands return control immediately. This is common, for example, with queuing systems. In this case, additional logic is required in the script to make the script wait before moving onto the next steps. One solution to this problem is to wait for a particular file that only exists when the process in complete.

For example, HyperStudy checks for the task__exe_err.txt file, which indicates a failure in the execution when present. The example of this script may contain:

# import statements

import os

import subprocess

import sys

import shutil

import time

 

f = open('logFile.txt', 'w')

 

#length of sleep command (seconds)

time_to_sleep = 1

time_out =  10

 

#set path to files

file_exe = 'C:/Program Files/Altair/14.0/hwsolvers/scripts/optistruct.bat'

file_to_copy = 'target.txt'

 

#set arguments

file_args = sys.argv[1] + ' -ncpu 4' + ' -core in'

 

#copy the file

file_to_copy = os.path.join(os.getenv('HST_STUDY_PATH'),'_usr',file_to_copy)

f.write('\nCopying the file: ' + file_to_copy + '\n')

shutil.copy(os.path.normpath(file_to_copy),os.getcwd())

 

#remove the file if it exists

if os.path.exists(sys.argv[2]):

  f.write('\nRemoving the file: ' + sys.argv[2] + '\n')

  os.remove(sys.argv[2])

 

#correct the path for the operating system, concatenate, and execute

file_exe = os.path.normpath(file_exe)

lstCommands = [file_exe, file_args]

f.write('Running the command:\n' + ' '.join(lstCommands) + '\n')

p1 = subprocess.Popen(' '.join(lstCommands), stdout=subprocess.PIPE , stderr=subprocess.PIPE)

 

#wait for the file to appear

init_time = time.time()

while not os.path.exists(sys.argv[2]):

  f.write('\n ... Waiting for file to appear: ' + sys.argv[2] + '\n')

  time.sleep(time_to_sleep)

  time_delta = time.time() - init_time

  if time_delta > time_out:

     f2 = open('task__exe_err.txt', 'w')

     f2.write('Time of waiting for ' + sys.argv[2])

     f2.close()

     break

 

 

#write output and close

f.write('\nStandard out:' + '\n' + p1.communicate()[0] + '\n')

f.write('\nStandard error:' + '\n' + p1.communicate()[1] + '\n')

f.close()