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.
Info |
---|
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.
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.
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
! 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 !======================== ! Body weight !======================== ACEL,0,0,9.81 ! Include gravity !======================== ! 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 |
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 like forever, and it is for that reason APDL has included loops and statements.
BibTeX Display Table |
---|