Intro2 11 Macro
-
Upload
luisalberto06011985 -
Category
Documents
-
view
231 -
download
0
Transcript of Intro2 11 Macro
-
7/28/2019 Intro2 11 Macro
1/16
Module 11
Macro Basics
-
7/28/2019 Intro2 11 Macro
2/16
Training Manual
11. Macro Basics
INTR
INTR
INTR
INTR
INTR
INTR
One of the most powerful features of APDL (ANSYSParametric Design Language) is the ability to create macros.
DUCTI
DUCTI
DUCTI
DUCTI
DUCTI
DUCTI
A macro is a sequence of ANSYS commands stored in a fileand executed just like a regular command.
NTO
NTO
NTO
NTO
NTO
NTO
Some useful macro capabilities:
It can have arguments as in a standard ANSYS command.
NSYS
NSYS
NSYS---
NSYS
NSYS
NSYS---
Interactive features such as graphical picking, prompting, anddialog boxes.
art2
art2
art2
art2
art2
art2
,calls a third one, etc. up to 20 levels deep.
February 20, 2006Inventory #002270
11-2
-
7/28/2019 Intro2 11 Macro
3/16
Training Manual
...Macro Basics
INTR
INTR
INTR
INTR
INTR
INTR
In this chapter, we will present the basics of macro writing:
A. Creatin a Macro
DUCTI
DUCTI
DUCTI
DUCTI
DUCTI
DUCTI
B. Macro with ArgumentsC. Branching
NT
O
NT
O
NT
O
NT
O
NT
O
NT
O
D. Looping
E. General Guidelines
F. Workshop
NSYS
NSYS
NSYS---
NSYS
NSYS
NSYS---
For more details, please refer to yourAPDL ProgrammersGuide or the Programming in ANSYSseminar notes.
art2
art2
art2
art2
art2
art2
February 20, 2006Inventory #002270
11-3
-
7/28/2019 Intro2 11 Macro
4/16
Training Manual
Macro Basics
A. Creating a Macro INTR
INTR
INTR
INTR
INTR
INTR
To create a macro, simply start a text editor, insert thedesired sequence of commands, and save them to a file
DUCTI
DUCTI
DUCTI
DUCTI
DUCTI
DUCTI
called name.mac.
name can be up to 32 characters, starting with a letter.
S aces are not allowed in the name.NT
O
NT
O
NT
O
NT
O
NT
O
NT
O
Also avoid special characters.
Make sure that name is not a valid ANSYS command byt in in name at Be in level and in all rocessors PREP7 NSYS
NSYS
NSYS---
NSYS
NSYS
NSYS---
POST1, etc.). If you get the message not a recognizedcommand or macro then the name is safe.
Extension .mac allows you to execute the macro as if it were aart2
art2
art2
art2
art2
art2
command by simply typing in name.
February 20, 2006Inventory #002270
11-4
-
7/28/2019 Intro2 11 Macro
5/16
Training Manual
Macro Basics
...Creating a Macro INTR
INTR
INTR
INTR
INTR
INTR
Example:
A macro totvolume.mac to calculate the total volume of all
DUCTI
DUCTI
DUCTI
DUCTI
DUCTI
DUCTI
elements:esel,all ! Select all elements
etable,volume,volu ! Store volume in element tableNT
O
NT
O
NT
O
NT
O
NT
O
NT
O
ssum ! Sum element table items
*get,totvol,ssum,,item,volume ! totvol = sum of volume
*stat,totvol ! List totvol valueNSYS
NSYS
NSYS---
NSYS
NSYS
NSYS---
Issue totvolume in POST1 (after a solve) to calculate the totalvolume.
art2
art2
art2
art2
art2
art2
February 20, 2006Inventory #002270
11-5
-
7/28/2019 Intro2 11 Macro
6/16
Training Manual
Macro Basics
...Creating a Macro INTR
INTR
INTR
INTR
INTR
INTR
Search Path:
ANSYS will execute the first name.mac file it finds in the
DUCTI
DUCTI
DUCTI
DUCTI
DUCTI
DUCTI
following search sequence:
1. The /ansys_inc/v90/ansys/apdl directory.
NT
O
NT
O
NT
O
NT
O
NT
O
NT
O
ANSYS_MACROLIB environment variable (if defined) or thelogin (home) directory.
3. The directory designated by the $HOME environment NSYS
NSYS
NSYS---
NSYS
NSYS
NSYS---
variable.
4. The working directory.
If the search finds both u er-case and lower-case files of theart2
art2
art2
art2
art2
art2
same name, the upper-case file is used.
February 20, 2006Inventory #002270
11-6
-
7/28/2019 Intro2 11 Macro
7/16
Training Manual
Macro Basics
B. Macro with Arguments INTR
INTR
INTR
INTR
INTR
INTR
By using special parameter names, you can create a macrowith up to 19 arguments:
DUCTI
DUCTI
DUCTI
DUCTI
DUCTI
DUCTI
NAME, arg1, arg2, arg3, , ar10, ar11, ar12, , ar19
The arguments behave just like the fields on a standard
NT
O
NT
O
NT
O
NT
O
NT
O
NT
O
numbers
alphanumeric characters (enclosed in single quotes) NSYS
NSYS
NSYS---
NSYS
NSYS
NSYS---
parameters (scalar or array)
parametric expressions
art2
art2
art2
art2
art2
art2
The meaning of the arguments depends on how you want todesign the macro.
February 20, 2006Inventory #002270
11-7
-
7/28/2019 Intro2 11 Macro
8/16
Training Manual
Macro Basics
...Macro with Arguments INTR
INTR
INTR
INTR
INTR
INTR
For example, we could design totvolume.mac to calculate thetotal volume for all elements of a specified type:
DUCTI
DUCTI
DUCTI
DUCTI
DUCTI
DUCTI
TOTVOLUME, TYPE
The macro would then look like this:
NT
O
NT
O
NT
O
NT
O
NT
O
NT
O
esel,s,type,,arg1 ! Select elements of specified type
etable,volume,volu ! Store volume in element table
ssum ! Sum element table items* N
SYS
NSYS
NSYS---
NSYS
NSYS
NSYS---
, , ,, ,
*vwrite,arg1,totvol ! Write out arg1 and totvol
(Total volume for type , F4.0, elements = , F8.2)
art2
art2
art2
art2
art2
art2
Issuing totvolume,1 in POST1 after a solution will then result in:
February 20, 2006Inventory #002270
11-8
-
7/28/2019 Intro2 11 Macro
9/16
Training Manual
Macro Basics
...Macro with Arguments INTR
INTR
INTR
INTR
INTR
INTR
Notes:
The special parameter names ARG1-ARG9 and AR10-AR99 are
DUCTI
DUCTI
DUCTI
DUCTI
DUCTI
DUCTI
local parameters valid only within a macro.
They hold no meaning once the macro has finished executionand control is returned to main ANSYS.
NT
O
NT
O
NT
O
NT
O
NT
O
NT
O
v u w .
Whenever you use arguments, be sure to describe their meaning
by including comments in the macro. NSYS
NSYS
NSYS---
NSYS
NSYS
NSYS---
,totvolume.mac would be helpful.
! Macro TOTVOLUME.MAC to calculate total volume of elements
! Usage: TOTVOLUME, TYPE - valid only in POST1 after a solveart2
art2
art2
art2
art2
art2
! TYPE = valid element type numberesel,s,type,,arg1 ! Select elements of specified type
February 20, 2006
Inventory #002270
11-9
-
7/28/2019 Intro2 11 Macro
10/16
Training Manual
Macro Basics
C. Branching INTR
INTR
INTR
INTR
INTR
INTR
By using an IF-THEN-ELSE construct, you can execute a commandor block of commands only if certain conditions are met.
DUCTI
DUCTI
DUCTI
DUCTI
DUCTI
DUCTI
Additional comparison operation are available or the IF and
*ELSEIF commands with AND, OR, or XOR options. *IF,A,EQ,B,AND,C,GT,D,THEN
NT
O
NT
O
NT
O
NT
O
NT
O
NT
O
ranc ng eg ns w t an en s w t . an*ELSE are also allowed in between:
*if, x, eq, y, thenTTT NSYS
NSYS
NSYS---
NSYS
NSYS
NSYS---
TTT
*elseif, x, eq, z, then
TTT
TTTart2
art2
art2
art2
art2
art2
*elseTTT
TTT
*endif
February 20, 2006
Inventory #002270
11-10 *IF constructs can be nested up to twenty levels
-
7/28/2019 Intro2 11 Macro
11/16
Training Manual
Macro Basics
...Branching INTR
INTR
INTR
INTR
INTR
INTR*if, x, eq, y, then
DUCTI
DUCTI
DUCTI
DUCTI
DUCTI
DUCTI
The condition can be:x, EQ, y ! x = y
The action can be:THEN to execute the
NT
O
NT
O
NT
O
NT
O
NT
O
NT
O
, ,
x, LT, y ! x < y
x, GT, y ! x > y
commands
*EXIT to exit a do-loop
*CYCLE to skip to the end of a NSYS
NSYS
NSYS---
NSYS
NSYS
NSYS---
, ,
x, GE, y ! x y
x, ABLT, y ! |x| < |y|
x ABGT ! x >
do-loop
The action takes place only ifthe condition is true.
art2
art2
art2
art2
art2
art2
x and y can be numbers,parameters, or parametricex ressions.
Otherwise, ANSYS will moveon to *ELSEIF (if present),*ELSE (if present), and *ENDIF.
February 20, 2006
Inventory #002270
11-11
-
7/28/2019 Intro2 11 Macro
12/16
Training Manual
Macro Basics
...Branching INTR
INTR
INTR
INTR
INTR
INTR
For example, you can add an if-test to totvolume.mac to testfor valid values of the input argument:
DUCTI
DUCTI
DUCTI
DUCTI
DUCTI
DUCTI
*if,arg1,lt,1,then ! If arg1 < 1
*msg,warn ! Issue a warning...Element type number must be 1 or greater
NT
O
NT
O
NT
O
NT
O
NT
O
NT
O
*endif
esel,s,type,,arg1 ! Select elements of specified type
etable,volume,volu ! Store volume in element table NSYS
NSYS
NSYS---
NSYS
NSYS
NSYS---
ssum um e emen a e ems...
Issuing totvolume,-1 will now result in:art2
art2
art2
art2
art2
art2
February 20, 2006
Inventory #002270
11-12
-
7/28/2019 Intro2 11 Macro
13/16
Training Manual
Macro Basics
D. Looping INTR
INTR
INTR
INTR
INTR
INTR
Do-loops allow you to loop through a block of commandsseveral times.
DUCTI
DUCTI
DUCTI
DUCTI
DUCTI
DUCTI
There is virtually no limit to what you can include in anANSYS do-loop. You can loop through an entire analysis
NT
O
NT
O
NT
O
NT
O
NT
O
NT
O
, ,postprocessing if the situation warrants it.
*DO or *DOWHILE begins a loop, *ENDDO ends it. NSYS
NSYS
NSYS---
NSYS
NSYS
NSYS---
You can control the looping using *EXIT, which exits the do-loop, and *CYCLE, which skips to the end of the do-loop.
Exitand cycle can also be done as a result of an if-test.art2
art2
art2
art2
art2
art2
As an example, we can extend the totvolume.mac macro toloop through all element types in the model and store the
February 20, 2006
Inventory #002270
11-13
.
-
7/28/2019 Intro2 11 Macro
14/16
Training Manual
Macro Basics
...Looping INTR
INTR
INTR
INTR
INTR
INTR
! -- Macro TOTVOLUME.MAC to calculate total element volume.
! -- Usage: Issue TOTVOLUME in POST1 after a solution.
DUCTI
DUCTI
DUCTI
DUCTI
DUCTI
DUCTI
--
! -- a) evolume(i) = total volume for element type i! -- b) totvol = grand total volume
!NT
O
NT
O
NT
O
NT
O
NT
O
NT
O
*get,numtypes,etype,,num,count ! Get number of element types
*dim,evolume,array,numtypes ! Open a numtypes x 1 array
*do,i,1,numtypes ! For i = 1 - numtypes...NSYS
NSYS
NSYS---
NSYS
NSYS
NSYS---
, , ,,
etable,volume,volu ! Store volume in element table
ssum ! Sum element table items
*get,totvol,ssum,,item,volume ! totvol = sum of volume
art2
art2
art2
art2
art2
art2
evo ume = o vo ore o vo n evo ume*enddo ! End of do-loop
*vscfun,totvol,sum,evolume(i) ! totvol = grand total volume
esel,all ! Activate full set of elements
February 20, 2006
Inventory #002270
11-14
-
7/28/2019 Intro2 11 Macro
15/16
Training Manual
Macro Basics
E. General Guidelines INTR
INTR
INTR
INTR
INTR
INTR
Start with small, simple macros.
DUCTI
DUCTI
DUCTI
DUCTI
DUCTI
DUCTI
,
commands into the ANSYS Input window one line at a time totest that the command sequence is correct.
NT
O
NT
O
NT
O
NT
O
NT
O
NT
O
To test your macros up to a point, place a /EOF at the pointwhere you want the macro to stop, then type in the next
command in the Input window to see if it works. NSYS
NSYS
NSYS---
NSYS
NSYS
NSYS---
Use comments to describe the intent or expected outcome ofcommands.
art2
art2
art2
art2
art2
art2
ace your persona macros n your og n rec ory. Place company-wide macros in a directory that everyone can
access, and include that director inANSYS MACROLIB
February 20, 2006
Inventory #002270
11-15
_
environment variable.
-
7/28/2019 Intro2 11 Macro
16/16
Training Manual
Macro Basics
F. Workshop INTR
INTR
INTR
INTR
INTR
INTR
This workshop consists of the following problem:
W11. Verif in Pressures
DUCTI
DUCTI
DUCTI
DUCTI
DUCTI
DUCTI
Please refer to yourWorkshop Supplementfor instructions.
NT
O
NT
O
NT
O
NT
O
NT
O
NT
ONSYS
NSYS
NSYS---
NSYS
NSYS
NSYS---
art2
art2
art2
art2
art2
art2
February 20, 2006
Inventory #002270
11-16