pyNastran
pyNastran copied to clipboard
run_nastran function: add option to wait for Nastran subprocess to finish before resuming script execution
Summary
The run_nastran
function (in the nastran_utils
module) is pretty useful in my opinion, however as far as I could see it does not have any option to wait for the Nastran analysis to be completed before resuming the execution of the script. The absence of this option makes the python script fail if for example I want to run an analysis and then read the op2 output:
run_nastran(bdf_filename, nastran_path)
op2 = read_op2(op2_filename)
The op2 file is not created until the Nastran analysis is completed, so the read_op2
function will not find anything if it is executed directly after the run_nastran
without the necessary wait time.
Versions
platform.python_version()
--> 3.8.12
pyNastran.__version__
--> 1.4.0+dev.no.checksum.error (this commit)
Feature development
At the moment I have this kind of function to deal with the issue:
def wait_nastran(analysis_directory):
# Wait for the analysis to start (when bat file appears)
while not any([file.endswith('.bat') for file in os.listdir(analysis_directory)]):
time.sleep(0.1)
# Wait for the analysis to finish (when bat file disappears)
while any([file.endswith('.bat') for file in os.listdir(analysis_directory)]):
time.sleep(0.1)
However I think it would be better to have the feature implemented directly in run_nastran
, with some input flag to activate it. What do you think? I am happy to work on this feature if it is fine.
I think it already supports it. bat=no
I tested it with run_nastran(output_bdf_filename, nastran_path, run_in_bdf_dir=True, keywords='bat=no')
but it did not seem to work. Actually bat=no
seems to be used by default when no keywords
input is specified, together with the other default parameters defined at line 84 of nastran_utils.py
:
keywords_list = ['scr=yes', 'bat=no', 'old=no', 'news=no', 'notify=no']
Is there any other option that I might have missed that allows to pause the python script until Nastran is done?
Apologies for having being silent for so long on this issue, however I've discovered only today that I was passing to the nastran_cmd
argument the path to nastranw.exe
instead of nastran.exe
. It turned out that the former returns 0 as soon as the command prompt is executed and it opens another command window that actually executes Nastran. In this way the subprocess started by Python results finished and the script resumes immediately the execution. Apparently nastranw.exe
is the executable that is run when I look for Nastran on the Start menu (I'm on Windows). If I pass to the nastran_cmd
argument the path to nastran.exe
, the command executes Nastran within itself and subprocess.call()
returns 0 only when Nastran is done.