You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 16 Next »




We will in this tutorial focus mostly on the scripting features available in APDL, as they are one of the main reasons to use APDL instead of other more graphical programs.

We will in this tutorial assume that you are familiar with general programming features like variables, loops, statements etc.


The Ansys finite element solvers enable a breadth and depth of capabilities unmatched by anyone in the world of computer-aided simulation.  Thermal, Structural, Acoustic, Piezoelectric, Electrostatic and Circuit Coupled Electromagnetics are just an example of what can be simulated.  Regardless of the type of simulation, each model is represented by a powerful scripting language … the Ansys Parametric Design Language (APDL).  APDL is the foundation for all sophisticated features, many of which are not exposed in the Workbench Mechanical user interface.  It also offers many conveniences such as parameterization, macros, branching and looping, and complex math operations.  All these benefits are accessible within the Ansys Mechanical APDL user interface. 

Page content

How to run scripts in APDL

Scrips in APDL are usually executed by the use of one of two methods. The first method is to write (or copy & paste) your code directly into the command window available in APDL.

The other method is to read in an input file (txt file) containing your script. To do this, go to File → Read Input From, select the correct file form the menu and click OK.

When locating the correct file, you will often notice that APDL by default opens the wrong directory (e.g. C:USERS when you actually want ../Marine Konstruksjoner/Exercise 7). To fix this, we need to open APDL with our chosen working directory. We may do so by first opening the ANSYS Mechanical APDL Product Launcher and then choose the correct working directory before clicking Run.

Introduction to APDL commands

Although many APDL commands are similar to what we are familiar with from other programming languages, very few have the exact same syntax. This means that to fully understand your code, and to further improve it, you will have to read the APDL commands documentation.

The easiest way to locate the correct documentation is to open Mechanical APDL and click Help → Help Topics. Since we will focus on the scripting part of APDL, the Command reference documentation is the most relevant to us.


As a first example, we would like to obtain the stresses in the node located in the point (L/2, X/2) and write them into our file answers.txt, given the plate below.

The following script includes some simple comments for most commands, but to truly understand the code, we highly recommend you to search them up in the Command Reference as well.

Script.txt
! Author: Chana Sinsabvarodom
! Edited by: Jon Arnt Kårstad
!========================
! Initial programming setup
FINISH
/CLEAR		! Clear all
/PREP7		! Enter preprocessor
/UNITS,SI	! Use SI units

!========================
! Defining the variables
!========================
L=0.6				! Length [m]
H=0.2				! Height [m]
t=0.005				! Thickness [m]
Diameter = H*0.3		! Diameter of hole [m]
Length2hole = L*0.5		! Length to hole [m]
P=-50e6*t			! Load, we will use as a pressure load [Pa] (positive - compression, negative - tension)
mesh_size=0.01			! Mesh size

! Making the plate
K,,0,-H/2,0			! Lower left node
K,,L,-H/2,0			! Lower right node
K,,L,H/2,0			! Upper right node
K,,0,H/2,0			! Upper left node
A,1,2,3,4			! Making an area from our nodes (notice the order)

CYL4,Length2hole,0,Diameter/2,	! Make cylinder
ASBA,1,2			! Remove area 2 (cylinder) from area 1 (plate)

!========================
! Defining the material data (here aluminium)
!========================
MPTEMP,1,0
MPDATA,EX,1,,0.69e11		! Young's modulus [Pa]
MPDATA,PRXY,1,,0.3		! Poisson's ratio [-]
MPDATA,DENS,1,,2700		! Density [kg/m^3]

!========================
! Define local element type
!========================
ET,1,SHELL181			! Shell element, both bending and membrane stresses

!========================
! Connecting area to component
!========================
ASEL,S,LOC,Z,0			! Select area at z = 0
CM,PLATE,AREA			! Group geometry into component
ALLSEL,ALL			! Select all

!========================
! Assigning a thickness to the plate
!========================
SECTYPE,1,SHELL,,PLATE		! Associates section type information with a section ID number.
SECDATA,T,1,0,3			! Given section data
SECOFFSET,MID			! Define section offset for cross section

!========================
! Defining the mesh
!========================
ESIZE,MESH_SIZE			! Element size

TYPE,1				! Set element type pointer
SECNUM,1			! Set element section attribute pointer
CMSEL,S,PLATE,AREA		! Select component named plate (of type area)
AMESH,PLATE			! Mesh plate	
/ESHAPE,1

!======================== 
! Load 
!========================
LSEL,S,LOC,X,L			! Line select at x = L
SFL,ALL,PRES,P			! Apply pressure load to line selected

!========================
! Boundary conditions (Select All X)
!========================
NSEL,S,LOC,X,0			! Select all nodes at x = 0
D,ALL,,0,,,,UX,UZ,UY,ROTX,ROTY,ROTZ

ALLSEL,ALL

!========================
! Solution			
!========================
! Notice that from now on we write only lower case letters
! APDL does does not differentiate between letter cases (lower or upper, 'A' or 'a')
! It therefore does not matter here, but care, it may cause trouble when working with variables

/solu				! Enter solution processor
solve				! Start a solution
/post1				! Enters database results postprocessor
/gline,,-1			! Don't show element lines (0 - solid lines, 1 - dashed lines)
plnsol,s,eqv			! Display results as continous element contours

!========================
! Write results to text file
!========================
! When writing a file, it is often clever to specify where you want to store the file first
/cwd,'C:\....WRITE YOUR OWN PATH HERE'   	! Changing working directory
*cfopen,answers.txt,,,append			! Open file, append (means add to end of file)

!========================
! Example task 1
!========================	

*vwrite,'Meshsize','XX','YY','XY','Von Mises'	! Write text
(A13,A13,A13,A13,A13,A13)			! A = string
! Above we need to specify how much space we want to give the text. The text is then right-aligned

! x = L/2 and y = H/2 -> mid top node
s1=node(L/2, H/2, 0)				! Select node
*GET,normal_stress_x,node,s1,s,x                ! Results Stress X (sigma_x)
*GET,normal_stress_y,node,s1,s,y                ! Results Stress Y (sigma_y)
*GET,shear_stress,node,s1,s,xy                	! Results Stress XY (tau_xy)
*GET,stress_von_mises,node,s1,s,eqv             ! Results Stress Von Mises

*vwrite,mesh_size,normal_stress_x,normal_stress_y,shear_stress,stress_von_mises
(E13.3,E13.3,E13.3,E13.3,E13.3)			! E = number (sci)

*cfclos						! Close file

The following result file will then be created.

answer.txt
     Meshsize     XX           YY           XY           Von Mise
    0.100E-01    0.481E+08    0.500E+06   -0.686E+05    0.479E+08

Loops and statements in APDL?

When doing FEM-analyses, we are often looking to locate e.g. how large our load is before buckling or how small the mesh size has to be before the results converges. If we were to do these tasks manually, it would take forever, and it is for that reason APDL has included loops and statements.

As an example, let's do the analysis presented in the previous section, but for mesh size = 0.20 to mesh size = 0.01 with a step size of 0.01. 

Script.txt
!========================
! Initial programming setup before entering the loop
!========================
FINISH
/CLEAR

!========================
! Defining the variables
!========================
L=0.6				! Length [m]
H=0.2				! Height [m]
t=0.005				! Thickness [m]
Diameter = H*0.3		! Diameter of hole [m]
Length2hole = L*0.5		! Length to hole [m]
P=-50e6*t			! Load, we will use as a pressure load [Pa] (positive - compression, negative - tension)
mesh_size = 0.10		! Initial mesh size

! We only want to write this section on the very top of our file
/cwd,'C:\....WRITE YOUR OWN PATH HERE'   	! Changing working directory
*cfopen,answers.txt,,,append			! Open file, append (means add to end of file)

*vwrite,'Meshsize','XX','YY','XY','Von Mises'	! Write text
(A13,A13,A13,A13,A13,A13)			! A = string

!========================
! Start of loop
!========================
condition = 1					! We want an infinite loop
*DOWHILE, condition				! Regular while loop with condition

*IF,mesh_size,EQ,0.01,EXIT			! If mesh_size == 0.01, exit loop


!========================
! Initial programming setup
!========================

! To clear area, mesh etc, but not all variables, we first write all variables to a separate file
! The reason for this is mainly because we want to keep the mesh_size and our condition
PARSAV,,param		

FINISH
/CLEAR

! After clear, we read in the variables we had previously.
PARRES,,param

/PREP7
/UNITS, SI

!========================
! Making plate
!========================
K,,0,-H/2,0			! Lower left node
K,,L,-H/2,0			! Lower right node
K,,L,H/2,0			! Upper right node
K,,0,H/2,0			! Upper left node
A,1,2,3,4			! Making an area from our nodes (notice the order)

CYL4,Length2hole,0,Diameter/2,	! Make cylinder
ASBA,1,2			! Remove area 2 (cylinder) from area 1 (plate)

!========================
! Defining the material data (here aluminium)
!========================
MPTEMP,1,0
MPDATA,EX,1,,0.69e11		! Young's modulus [Pa]
MPDATA,PRXY,1,,0.3		! Poisson's ratio [-]
MPDATA,DENS,1,,2700		! Density [kg/m^3]

!========================
! Define local element type
!========================
ET,1,SHELL181			! Shell element, both bending and membrane stresses

!========================
! Connecting area to component
!========================
ASEL,S,LOC,Z,0			! Select area at z = 0
CM,PLATE,AREA			! Group geometry into component
ALLSEL,ALL			! Select all

!========================
! Assigning a thickness to the plate
!========================
SECTYPE,1,SHELL,,PLATE		! Associates section type information with a section ID number.
SECDATA,T,1,0,3			! Given section data
SECOFFSET,MID			! Define section offset for cross section

!========================
! Defining the mesh
!========================
ESIZE,MESH_SIZE			! Element size

TYPE,1				! Set element type pointer
SECNUM,1			! Set element section attribute pointer
CMSEL,S,PLATE,AREA		! Select component named plate (of type area)
AMESH,PLATE			! Mesh plate	
/ESHAPE,1

!======================== 
! Load 
!========================
LSEL,S,LOC,X,L			! Line select at x = L
SFL,ALL,PRES,P			! Apply pressure load to line selected

!========================
! Boundary conditions (Select All X)
!========================
NSEL,S,LOC,X,0			! Select all nodes at x = 0
D,ALL,,0,,,,UX,UZ,UY,ROTX,ROTY,ROTZ

ALLSEL,ALL

!========================
! Solution			
!========================
/solu				! Enter solution processor
solve				! Start a solution
/post1				! Enters database results postprocessor
/gline,,-1			! Don't show element lines (0 - solid lines, 1 - dashed lines)
plnsol,s,eqv			! Display results as continous element contours

!========================
! Write results to text file
!========================
! When writing a file, it is often clever to specify where you want to store the file first
/cwd,'C:\....WRITE YOUR OWN PATH HERE'   	! Changing working directory
*cfopen,answers.txt,,,append			! Open file, append (means add to end of file)

!========================
! Example task 2
!========================	

! x = L/2 and y = H/2 -> mid top node
s1=node(L/2, H/2, 0)				! Select node
*GET,normal_stress_x,node,s1,s,x                ! Results Stress X (sigma_x)
*GET,normal_stress_y,node,s1,s,y                ! Results Stress Y (sigma_y)
*GET,shear_stress,node,s1,s,xy                	! Results Stress XY (tau_xy)
*GET,stress_von_mises,node,s1,s,eqv             ! Results Stress Von Mises

*vwrite,mesh_size,normal_stress_x,normal_stress_y,shear_stress,stress_von_mises
(E13.3,E13.3,E13.3,E13.3,E13.3)			! E = number (sci)

*cfclos						! Close file

mesh_size = mesh_size - 0.01	! Decrease mesh size
*ENDDO				! End of while loop

The following result file will then be created.

answer.txt
     Meshsize     XX           YY           XY           Von Mise
    0.100E+00    0.592E+08    0.203E+07   -0.428E+05    0.582E+08
    0.900E-01    0.552E+08   -0.294E+06    0.300E+06    0.553E+08
    0.800E-01    0.543E+08   -0.671E+06    0.106E+07    0.546E+08
    0.700E-01    0.577E+08   -0.360E+07    0.222E+07    0.597E+08
    0.600E-01    0.618E+08    0.473E+06    0.276E+07    0.617E+08
    0.500E-01    0.605E+08    0.668E+06   -0.158E+07    0.602E+08
    0.400E-01    0.551E+08    0.169E+07    0.149E+07    0.543E+08
    0.300E-01    0.528E+08    0.988E+06    0.199E+05    0.523E+08
    0.200E-01    0.513E+08    0.974E+06    0.538E+05    0.508E+08




  • No labels