Sie sind auf Seite 1von 16

Module 11

Macro Basics

11. Macro Basics


Training Manual

INTRO RODUCTIO ON TO ANSYS A -P Part 2

One of the most powerful features of APDL (ANSYS Parametric Design Language) is the ability to create macros. A macro is a sequence of ANSYS commands stored in a file and executed just like a regular command. Some useful macro capabilities:
It can have arguments as in a standard ANSYS command. Branching and looping to control the sequence of commands. Interactive features such as graphical picking, prompting, and dialog boxes. Nested macros one macro calling a second one one, which in turn calls a third one, etc. up to 20 levels deep.

February 20, 2006 Inventory #002270 11-2

...Macro Basics

Training Manual

INTRO RODUCTIO ON TO ANSYS A -P Part 2

In this chapter, we will present the basics of macro writing:


A. Creating a Macro B. Macro with Arguments C. Branching D. Looping E. General Guidelines F. Workshop p

For more details, please refer to your APDL Programmers Guide or the Programming in ANSYS seminar notes.

February 20, 2006 Inventory #002270 11-3

Macro Basics

A. Creating g a Macro
To create a macro, simply start a text editor, insert the desired sequence of commands, and save them to a file called name.mac.
name can be up to 32 characters, starting with a letter. Spaces p are not allowed in the name. Also avoid special characters.

Training Manual

INTRO RODUCTIO ON TO ANSYS A -P Part 2

Make sure that name is not a valid ANSYS command by typing typ g in name a e at Begin eg level e e a and d in a all p processors ocesso s ( (PREP7, , POST1, etc.). If you get the message not a recognized command or macro then the name is safe. Extension .mac allows y you to execute the macro as if it were a command by simply typing in name.

February 20, 2006 Inventory #002270 11-4

Macro Basics

...Creating g a Macro
Example:
A macro totvolume.mac to calculate the total volume of all elements: esel,all etable,volume,volu ssum *get,totvol,ssum,,item,volume *stat,totvol

Training Manual

INTRO RODUCTIO ON TO ANSYS A -P Part 2

! Select all elements ! Store volume in element table ! Sum element table items ! totvol = sum of volume ! List totvol value

Issue totvolume in POST1 (after a solve) to calculate the total volume.

February 20, 2006 Inventory #002270 11-5

Macro Basics

...Creating g a Macro
Search Path:
ANSYS will execute the first name.mac file it finds in the following search sequence: 1. The /ansys_inc/v90/ansys/apdl directory.

Training Manual

INTRO RODUCTIO ON TO ANSYS A -P Part 2

2. The directory (or directories) designated by the ANSYS_MACROLIB environment variable (if defined) or the login (home) directory. 3. The directory designated by the $HOME environment variable. 4. The working directory. If the search finds both upper-case pp and lower-case files of the same name, the upper-case file is used.

February 20, 2006 Inventory #002270 11-6

Macro Basics

B. Macro with Arguments g

Training Manual

INTRO RODUCTIO ON TO ANSYS A -P Part 2

By using special parameter names, you can create a macro with up to 19 arguments:
NAME, arg1, arg2, arg3, , ar10, ar11, ar12, , ar19

The arguments behave just like the fields on a standard ANSYS command and can accept:
numbers alphanumeric characters (enclosed in single quotes) parameters (scalar or array) parametric expressions

The meaning of f the arguments depends on how you want to design the macro.

February 20, 2006 Inventory #002270 11-7

Macro Basics

...Macro with Arguments g

Training Manual

INTRO RODUCTIO ON TO ANSYS A -P Part 2

For example, we could design totvolume.mac to calculate the total volume for all elements of a specified type:
TOTVOLUME, TYPE

The macro would then look like this:


esel,s,type,,arg1 ! Select elements of specified type etable,volume,volu ! Store volume in element table ssum ! Sum element table items *get totvol ssum item volume *get,totvol,ssum,,item,volume ! totvol = sum of volume volume *vwrite,arg1,totvol ! Write out arg1 and totvol (Total volume for type , F4.0, elements = , F8.2)

Issuing totvolume,1 in POST1 after a solution will then result in:

February 20, 2006 Inventory #002270 11-8

Macro Basics

...Macro with Arguments g


Notes:

Training Manual

INTRO RODUCTIO ON TO ANSYS A -P Part 2

The special parameter names ARG1-ARG9 and AR10-AR99 are local parameters valid only within a macro. They hold no meaning once the macro has finished execution and control is returned to main ANSYS. Avoid A oid using sing these names else elsewhere here in the model model. Whenever you use arguments, be sure to describe their meaning by including comments in the macro. For example, example the following comments at the beginning of totvolume.mac would be helpful.
! Macro TOTVOLUME.MAC to calculate total volume of elements ! Usage: TOTVOLUME, TYPE - valid only in POST1 after a solve ! TYPE = valid element type number esel,s,type,,arg1 ! Select elements of specified type

February 20, 2006 Inventory #002270 11-9

Macro Basics

C. Branching g

Training Manual

By using an IF-THEN-ELSE construct, you can execute a command or block of commands only if certain conditions are met. Additional comparison operation are available for f the *IF * and *ELSEIF commands with AND, OR, or XOR options.
*IF,A,EQ,B,AND,C,GT,D,THEN

INTRO RODUCTIO ON TO ANSYS A -P Part 2

Branching B hi begins b i with i h *IF and d ends d with i h *ENDIF. *ELSEIF and d *ELSE are also allowed in between:
*if, x, eq, y, then TTT TTT *elseif, x, eq, z, then TTT TTT *else TTT TTT *endif

*IF constructs can be nested up to twenty levels

February 20, 2006 Inventory #002270 11-10

Macro Basics

...Branching g
*if, x, eq, y, then
The condition can be:
x, EQ, y x NE x, NE, y x, LT, y x, GT, y x LE, x, LE y x, GE, y x, ABLT, y x, ABGT, y !x=y !xy !x<y !x>y !xy !xy ! |x| < |y| ! |x| > |y| *EXIT

Training Manual

INTRO RODUCTIO ON TO ANSYS A -P Part 2

The action can be:


THEN to execute the subsequent block of commands to exit a do-loop

*CYCLE to skip p to the end of a do-loop

x and y can be numbers, parameters, or parametric expressions.

The action takes place only if the condition is true. Otherwise, ANSYS will move on to *ELSEIF (if present), *ELSE (if present), and *ENDIF.

February 20, 2006 Inventory #002270 11-11

Macro Basics

...Branching g

Training Manual

INTRO RODUCTIO ON TO ANSYS A -P Part 2

For example, you can add an if-test to totvolume.mac to test for valid values of the input argument:
*if,arg1,lt,1,then ! If arg1 < 1 *msg,warn ! Issue a warning... Element type number must be 1 or greater /eof ! and exit the macro *endif esel,s,type,,arg1 ! Select elements of specified type etable,volume,volu ! Store volume in element table ssum ! Sum S element l t table t bl items it ...

Issuing totvolume,-1 will now result in:

February 20, 2006 Inventory #002270 11-12

Macro Basics

D. Looping g

Training Manual

INTRO RODUCTIO ON TO ANSYS A -P Part 2

Do-loops allow you to loop through a block of commands several times. There is virtually no limit to what you can include in an ANSYS do-loop. You can loop through an entire analysis session including preprocessing, preprocessing solution solution, and postprocessing if the situation warrants it. *DO or *DOWHILE begins a loop, *ENDDO ends it.
You can control the looping using *EXIT, which exits the doloop, and *CYCLE, which skips to the end of the do-loop. Exit and cycle can also be done as a result of an if-test.

As an example, we can extend the totvolume.mac macro to loop through all element types in the model and store the volume for each type in an array parameter.

February 20, 2006 Inventory #002270 11-13

Macro Basics

...Looping g
! -- Macro TOTVOLUME.MAC to calculate total element volume. ! -- Usage: Issue TOTVOLUME in POST1 after a solution. ! -- Result: ! -- a) evolume(i) = total volume for element type i ! -- b) totvol = grand total volume ! *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... esel s type i esel,s,type,,i ! Select elements of type i etable,volume,volu ! Store volume in element table ssum ! Sum element table items *get,totvol,ssum,,item,volume ! totvol = sum of volume evolume(i) l (i) = t totvol t l ! Store St totvol t t l in i evolume(i) l (i) *enddo ! End of do-loop *vscfun,totvol,sum,evolume(i) ! totvol = grand total volume esel,all ! Activate full set of elements

Training Manual

February 20, 2006 Inventory #002270 11-14

INTRO RODUCTIO ON TO ANSYS A -P Part 2

Macro Basics

E. General Guidelines
Start with small, simple macros.

Training Manual

INTRO RODUCTIO ON TO ANSYS A -P Part 2

As you create the macro macro, you can cut and paste the commands into the ANSYS Input window one line at a time to test that the command sequence is correct. To test your macros up to a point, place a /EOF at the point where you want the macro to stop, then type in the next command in the Input window to see if it works. Use comments to describe the intent or expected outcome of commands. Pl Place your personal l macros i in your l login i directory. di t Place company-wide macros in a directory that everyone can access, , and include that directory y in ANSYS_MACROLIB _ environment variable.

February 20, 2006 Inventory #002270 11-15

Macro Basics

F. Workshop
This workshop consists of the following problem:
W11. Verifying Pressures

Training Manual

INTRO RODUCTIO ON TO ANSYS ANSYS - P Part art 2

Please refer to your Workshop Supplement for instructions.

February 20, 2006 Inventory #002270 11-16

Das könnte Ihnen auch gefallen