问题
I need to run multiple times the same abaqus .inp file (slightly changed within runs) and after each run ends I need to submit a abaqus python script that will read the results.
I've done the following:
#run the programme
os.system('abaqus job=file_name cpus=2')
#get results and write them to myresults.txt
os.system('abaqus viewer noGUI=python_name.py')
However, the main program executes the second line before the program started in the first line ends. As a result I get an error. How can I solve this?
回答1:
I think you need system('abaqus job=inputfile.inp interactive')
interactive does not consider the system command finished until abaqus has finished running.
Without interactive abaqus runs in the background while the system command is over and we have moved to the next one which is what we do not want.
回答2:
I guess the problem here is not about subprocess waiting (indeed it waits) but the fact that after running the solver, Abaqus takes some seconds to delete some temp files and close its odb. I suggest one of the following:
run the solver from the command line with 'interactive' as @glenn_gould proposed
strCommandLine = 'abaqus interactive job=jobname' subprocess.call(strCommandLine)
run an abaqus python script
strCommandLine = 'abaqus python ScriptToRun.py -- (jobname)' subprocess.call(strCommandLine)
and within ScriptToRun.py use waitForCompletion() as @ellumini proposed
from abaqus import * import job import sys jobname = mdb.JobFromInputFile(sys.argv[-1], sys.argv[-1]+".inp") jobname.submit() jobname.waitForCompletion()
use a try statement to run while file jobname.023 or jobname.lck exist, something like:
strCommandLine = 'abaqus job=jobname' subprocess.call(strCommandLine) while os.path.isfile('jobname.023') == True: sleep(0.1)
This was my first post in this magnificent community, I'd be glad to know if I did something wrong.
回答3:
Take a look at the subprocess module. The call
methods waits until the process is finished. You can also get a much better control over the child process than using os.system()
.
回答4:
The subprocess module has been recommended in another answer. That's the officially recommended way to do this. However, a quicker and easier method (and also deprecated in Python 3, but still works fine in 2.x, so take that into consideration) is the commands module.
import commands
(return_code, output) = commands.getstatusoutput('abaqus job=file_name cpus=2')
回答5:
About running in the background, are you sure it is?
This site suggests that os.system('abaqus job=file_name cpus=2')
would be running in the foreground.
Using Abaqus in Batch Mode To invoke the Abaqus System in batch mode, you must specify a filename in the Abaqus >command. For example, if you want myProg.inp to excute:
abaqus job=myProg
(Note that no extension should follow the file name)
This command should start Abaqus in batch mode. The command will run the program in the foreground. If you want to run the program in the background add the ampersand to the end of the command:
abaqus job=myProg &
Maybe there is a local configuration setting that is forcing background processing? If so perhaps you can add a switch to make sure processing is in the foreground.
回答6:
Try using subprocess but without the os.call option. You can use this method to run Abaqus in the background:
import subprocess
path = location of file in any directory
abaqusCall = 'abaqus job=file_name cpus=2'
runCommand = 'cmd.exe /c ' + abaqusCall
process = subprocess.Popen(runCommand, cwd=path)
The problem with Abaqus is that it takes a long time to run the analysis, so if you try to run your "python_name.py" file in order to get results, the program might get errors because the *.odb file either has not been created yet, or does not contain the data that need extraction.
You can use the command:
process.wait()
to tell Python to wait for Abaqus to finish the analysis before executing your "python_name.py", but this will hang your python command (or GUI) until Abaqus is done, which will take a long time.
One method I use is to read the *.sta file from Abaqus which has the solution time and progress. So, you can write a sequence to read the file every 5 seconds for example, and monitor when the job ends before executing your result extraction file.
Another trick for data extraction is that as long you do not use (import) classes from the CAE module, you can run your Python script using this command:
#get results and write them to myresults.txt
os.system('abaqus python python_name.py')
回答7:
not sure if you've already found an adequate solution, but the job object:
Jobxy.waitForCompletion()
awaits the completion of a job and then continues executing the pyhton script; for example with commands regarding post processing. See abaqus scripting users manual for further details...
来源:https://stackoverflow.com/questions/9658324/submit-a-job-wait-for-its-completion-and-after-submit-another-job