Skip to content

Instantly share code, notes, and snippets.

@windstriver
Created April 20, 2015 06:20
Show Gist options
  • Save windstriver/46eb491bea9bf222f436 to your computer and use it in GitHub Desktop.
Save windstriver/46eb491bea9bf222f436 to your computer and use it in GitHub Desktop.
ANSYS Workbench Sending Commands to Mechanical APDL
! set parameters
xlen=3
ylen=4
zlen=7
! define model
/prep7
block,,xlen,,ylen,,zlen
et,1,185
mp,ex,1,1e6
mp,prxy,1,0.3
vmesh,all
nsel,s,loc,z,0
d,all,all
nsel,s,loc,y,ylen
sf,all,pres,125
alls
fini
! obtain the solution
/solution
solve
fini
! retrieve results
/post1
! get the max stress at the fixed end
nsel,s,loc,x,xlen
nsel,r,loc,y,ylen
nsel,r,loc,z,0
*GET,out_my_node_stress,NODE,1,NXTH
*GET,out_seqv,NODE,out_my_node_stress,S,EQV
! get the max displacement at the free end
nsel,s,loc,x,xlen
nsel,r,loc,y,ylen
nsel,r,loc,z,zlen
*GET,out_my_node_def,NODE,1,NXTH
*GET,out_uy,NODE,out_my_node_def,U,Y
alls
fini
# Import the 'os' module, which provides a portable way of using
# operating system dependent functionality
import os
SetUserPathRoot(DirectoryPath = "E:\workbench-script-demo")
# Specify the Mechanical APDL Input file to be processed
inputFile = AbsUserPathName("bar.apdl")
# Provide a list of bar length (Z) values to solve and write CDB files for.
# Note: We expect '0' to fail.
zValues = [3,5,0,12,15]
# Open a log file to record script progress
logFile = open(AbsUserPathName("bar_script.log"),"w")
# Start a new project and create the Mechanical APDL system
Reset()
template1 = GetTemplate(TemplateName="Mechanical APDL")
system1 = template1.CreateSystem()
# Read the input file into the Mechanical APDL Setup
setup1 = system1.GetContainer(ComponentName="Setup")
mapdlInputFile1 = setup1.AddInputFile(FilePath=inputFile)
# Create Workbench parameters from two of the Mechanical APDL parameters
# in the input file
mapdlInputFile1.PublishMapdlParameter(Name="ZLEN")
parZlen = Parameters.GetParameter(Name="P1")
mapdlInputFile1.PublishMapdlParameter(
Name="OUT_UY",
IsDirectOutput=True)
parUY = Parameters.GetParameter(Name="P2")
# Save the initial project definition.
Save(
FilePath=AbsUserPathName("myBar.wbpj"),
Overwrite=True)
# Loop through all provided bar lengths
for zVal in zValues:
# Set the Z (length) parameter expression
parZlen.Expression = str(zVal)
logFile.write("Updating for z = %s\n" % zVal)
# Update the project for the new parameter value, and report
# success or failure to the log file.
try:
Update()
except:
logFile.write(" Update failed.\n")
else:
logFile.write(" Update succeeded. UY = %s\n" % parUY.Value)
# Generate the name of the CDB file to save
cdbName = os.path.join(GetUserFilesDirectory(), "bar_" + str(zVal) + ".cdb")
cdbNameForCmd = cdbName.replace("\\","/")
# Delete the cdb file if it already exists, to prevent
# Mechanical APDL from promting us about overwrite.
if os.path.exists(cdbName):
os.remove(cdbName)
# Generate the APDL command to save the CDB file and send it.
apdlCmd = "cdwr,db,%s" % cdbNameForCmd
setup1.SendCommand(Command=apdlCmd)
logFile.write(" CDB written to %s\n\n" % cdbName)
# Save the final project state.
Save()
logFile.close()
@MakarevichDmitry
Copy link

This sample from documentation to Ansys Workbench.
Sample will work if execute them from Workbench coomand prompt only.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment