简体   繁体   中英

passing variables to a python script file using abaqus python 2.6.2

I am trying to pass a variable to a abaqus script file(.psf) through command line. The command line call is made every time another script is executed and has different value for the variable in each call. Can I have help in this regard on the command syntax to be used. I tried os.system and subprocess.Popen , both are giving some sort of errors.

In my main script(.py file) it calls .psf

Xa=150000
abaqusCall = 'abaqus script=tt_Par.psf'
runCommand = 'cmd.exe /c ' + abaqusCall
process = subprocess.Popen(runCommand, cwd=workDir, args=Xa)

and in .psf

it accepts variables in this format..

import sys,os

for item in sys.argv
    x1 = sys.argv[0]
    x2 = sys.argv[1]
    print x1,x2

Could anyone give directions in this regard?

try this out. I am not sure what as psf file is, but I just use py files.

def abaqus_cmd(mycmd):
    ''' 
    used to execute abaqus commands in the windows OS console
    inputs : mycmd, an abaqus command
    '''
    import subprocess, sys
    try:
        retcode = subprocess.call(mycmd,shell=True)
        if retcode < 0:
            print >>sys.stderr, mycmd+"...failed during execution", -retcode
        else:
            print >>sys.stderr, mycmd+"...success"
    except OSError as e:
        print >>sys.stderr, mycmd+"...failed at execution", e

to run a simple command do this

abaqus_cmd('abaqus fetch job=beamExample')

to pass a variable you can do this

odbfile = 'test.odb'
abaqus_cmd('abaqus python odb_to_txt.py '+odbfile)

however, this is just a python instance in abaqus, you cannot access the abaqus kernal here. To access the abaqus kernal, you need to run the script like this.

abaqus_cmd('abaqus cae noGUI=beamExample.py')

I HAVE NOT figured out how to pass variables into scripts in the abaqus kernel, see my comment

Very late to the party but I also needed to call abaqus scripts with variables passed in and out of system. My main script is in Py3 but Abaqus (2021) is still using Py27. You don't need to worry as long as your model script is in py27 you can still call the command using Py3.

I needed to run my model script in a directory different to my py3 main script so in the main script I have:

aba_dir = PATH_TO_DIRECTORY_IN_WHICH_I_WANT_ABAQUS_FILES (.odb, .cae etc.)
script_dir = DIRECTORY_FOR_MODEL_SCRIPT (build_my_model.py - this is py27 script)

job_name = call_cae(cwd, aba_dir, script_dir, var1, var2, var3)

The following functions are used to build the correct command:

Function to define whether to call CAE or ODB viewer:

def caller_type(cae):
#DIFFERENTIATE BETWEEN CAE AND VIEWER
if cae:
    caller = 'abaqus cae noGui='
else:
    caller = 'abaqus viewer noGui='
return caller

Function to build command string with variables:

def build_command_string(script, cae, *args):
# ## GET THE CAE/VIEWER CALLER
caller = caller_type(cae)
# ##CREATE STRING REPRESENTING ABAQUS COMMAND
caller = caller + script
# ##STRING ALL ARGUMENTS AS INDIVIDUAL ITEMS
str_args = [str(arg) for arg in args]
# ##CREATE COMMAND INITIALISER WITH CALLER
c = ['cmd.exe', '/C', caller, '--']
# ##APPEND STRING ARGS TO COMMAND LIST
for arg in str_args:
    c.append(arg)
# ##RETURN COMMAND LIST
return c

Function to submit the command to system and return the job name:

def call_cae(cwd, aba_dir, script, *args):
# SET CAE TO TRUE
cae = True
# CHANGE TO OUTPUT DIRECTORY
os.chdir(aba_dir)
# ##BUILD COMMAND STRING
command = build_command_string(script, cae, *args)
# ##RUN SUBPROCESS COMMAND
p1 = subprocess.run(command,
                    stdout=subprocess.PIPE,
                    stderr=subprocess.PIPE,
                    text=True)
# ##RETURN TO ORIGINAL WORKING DIRECTORY (MAIN FILE)
os.chdir(cwd)

if p1.stdout == None or p1.stdout == '':
    # ##RETURN JOB NAME
    job_name = p1.stderr[p1.stderr.rfind('\n'):].strip('\n')
else:
    # ##RETURN JOB NAME
    job_name = p1.stderr[p1.stderr.rfind('\n'):].strip('\n')
    # ##PRINT STATEMENT TO CHECK CONVERGANCE
    print('This job exited with error')
return job_name

Inside my code to build and execute the abaqus model I specify:

sim_num = int(sys.argv[-3]) #var1
E = float(sys.argv[-2]) #var2
nu = float(sys.argv[-1]) #var3

and at the end of my script sys. stderr .write(cJob.name) #

This is working for me on PyCharm with Py3 in main file and Abaqus python 27 in 'build and execute model.py' file. Hopefully helpful to others too. Now onto creating commands for the ODB output!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM