Module 10 Macro Basics 10 Macro Basics Training
Module 10 Macro Basics
10. Macro Basics Training Manual • 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, which in turn calls a third one, etc. — up to 20 levels deep. INTRODUCTION TO ANSYS 6. 0 - Part 2 • One of the most powerful features of APDL (ANSYS Parametric Design Language) is the ability to create macros. October 30, 2001 Inventory #001571 10 -2
. . . Macro Basics Training Manual A. Creating a Macro B. Macro with Arguments C. Branching D. Looping E. General Guidelines F. Workshop • For more details, please refer to your APDL Programmer’s Guide or the Programming in ANSYS seminar notes. INTRODUCTION TO ANSYS 6. 0 - Part 2 • In this chapter, we will present the basics of macro writing: October 30, 2001 Inventory #001571 10 -3
Macro Basics A. Creating a Macro Training Manual – name can be up to 32 characters, starting with a letter. • Spaces are not allowed in the name. • Also avoid special characters. • Make sure that name is not a valid ANSYS command by typing in name at Begin level and in all processors (PREP 7, POST 1, etc. ). If you get the message … not a recognized command or macro… then the name is “safe. ” – Extension. mac allows you to execute the macro as if it were a command by simply typing in name. INTRODUCTION TO ANSYS 6. 0 - Part 2 • To create a macro, simply start a text editor, insert the desired sequence of commands, and save them to a file called name. mac. October 30, 2001 Inventory #001571 10 -4
Macro Basics . . . Creating a Macro Training Manual – 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 ! Select all elements ! Store volume in element table ! Sum element table items ! totvol = sum of ‘volume’ ! List totvol value – Issue totvolume in POST 1 (after a solve) to calculate the total volume. INTRODUCTION TO ANSYS 6. 0 - Part 2 • Example: October 30, 2001 Inventory #001571 10 -5
Macro Basics . . . Creating a Macro Training Manual – ANSYS will execute the first name. mac file it finds in the following search sequence: 1. /ansys 60/docu 2. directory(ies) in ANSYS_MACROLIB environment variable 3. login directory (home directory on Windows systems) 4. current (working) directory – If the search finds both upper-case and lower-case files of the same name, the upper-case file is used. INTRODUCTION TO ANSYS 6. 0 - Part 2 • Search Path: October 30, 2001 Inventory #001571 10 -6
Macro Basics B. Macro with Arguments Training Manual – NAME, arg 1, arg 2, arg 3, … , ar 10, ar 11, ar 12, … , ar 20 • The arguments behave just like the fields on a standard ANSYS command can accept: – numbers – alphanumeric characters (enclosed in single quotes) – parameters (scalar or array) – parametric expressions • The meaning of the arguments depends on how you want to design the macro. INTRODUCTION TO ANSYS 6. 0 - Part 2 • By using special parameter names, you can create a macro with up to 20 arguments: October 30, 2001 Inventory #001571 10 -7
Macro Basics . . . Macro with Arguments Training Manual – TOTVOLUME, TYPE • The macro would then look like this: esel, s, type, , arg 1 ! Select elements of specified type etable, volume, volu ! Store volume in element table ssum ! Sum element table items *get, totvol, ssum, , item, volume ! totvol = sum of ‘volume’ *vwrite, arg 1, totvol ! Write out arg 1 and totvol (‘Total volume for type ’, F 4. 0, ‘ elements = ’, F 8. 2) • Issuing totvolume, 1 in POST 1 after a solution will then result in: INTRODUCTION TO ANSYS 6. 0 - Part 2 • For example, we could design totvolume. mac to calculate the total volume for all elements of a specified type: October 30, 2001 Inventory #001571 10 -8
Macro Basics . . . Macro with Arguments Training Manual – The special parameter names ARG 1 -ARG 9 and AR 10 -AR 99 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 using these names elsewhere in the model. – Whenever you use arguments, be sure to describe their meaning by including comments in the macro. For 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 POST 1 after a solve ! TYPE = valid element type number esel, s, type, , arg 1 ! Select elements of specified type … … INTRODUCTION TO ANSYS 6. 0 - Part 2 • Notes: October 30, 2001 Inventory #001571 10 -9
Macro Basics C. Branching 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 the *IF and *ELSEIF commands with AND, OR, or XOR options. – • *IF, A, EQ, B, AND, C, GT, D, THEN Branching begins with *IF and ends with *ENDIF. *ELSEIF and *ELSE are also allowed in between: *if, x, eq, y, then *elseif, x, eq, z, then *else *endif • *IF constructs can be nested up to twenty levels INTRODUCTION TO ANSYS 6. 0 - Part 2 • October 30, 2001 Inventory #001571 10 -10
Macro Basics . . . Branching Training Manual • • The condition can be: x, EQ, y !x=y x, NE, y !x y x, LT, y !x<y x, GT, y • The action can be: THEN to execute the subsequent block of commands !x>y *EXIT to exit a do-loop x, LE, y !x y x, GE, y !x y *CYCLE to skip to the end of a do-loop x, ABLT, y ! |x| < |y| x, ABGT, y ! |x| > |y| 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. INTRODUCTION TO ANSYS 6. 0 - Part 2 *if, x, eq, y, then October 30, 2001 Inventory #001571 10 -11
Macro Basics . . . Branching Training Manual *if, arg 1, lt, 1, then ! If arg 1 < 1 *msg, warn ! Issue a warning. . . Element type number must be 1 or greater /eof ! … and exit the macro *endif esel, s, type, , arg 1 ! Select elements of specified type etable, volume, volu ! Store volume in element table ssum ! Sum element table items. . . • Issuing totvolume, -1 will now result in: INTRODUCTION TO ANSYS 6. 0 - Part 2 • For example, you can add an if-test to totvolume. mac to test for valid values of the input argument: October 30, 2001 Inventory #001571 10 -12
Macro Basics D. Looping Training Manual • 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, 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 do-loop, 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. macro to loop through all element types in the model and store the volume for each type in an array parameter. INTRODUCTION TO ANSYS 6. 0 - Part 2 • Do-loops allow you to loop through a block of commands several times. October 30, 2001 Inventory #001571 10 -13
Macro Basics . . . Looping INTRODUCTION TO ANSYS 6. 0 - Part 2 ! -- Macro TOTVOLUME. MAC to calculate total element volume. ! -- Usage: Issue TOTVOLUME in POST 1 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 ! 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) = totvol ! Store totvol in evolume(i) *enddo ! End of do-loop *vscfun, totvol, sum, evolume(i) ! totvol = grand total volume esel, all ! Activate full set of elements Training Manual October 30, 2001 Inventory #001571 10 -14
Macro Basics E. General Guidelines Training Manual • As you create the macro, remember that you can cut and paste the commands into the ANSYS Input window to test and make sure that the command sequence is correct. • Use comments to describe the intent or expected outcome of commands. • Place your “personal” macros in your login directory. • Place company-wide macros in a directory that everyone can access, and include that directory in ANSYS_MACROLIB environment variable. INTRODUCTION TO ANSYS 6. 0 - Part 2 • Start with small, simple macros. October 30, 2001 Inventory #001571 10 -15
Macro Basics F. Workshop Training Manual W 9. Verifying Pressures Please refer to your Workshop Supplement for instructions. INTRODUCTION TO ANSYS 6. 0 - Part 2 • This workshop consists of the following problem: October 30, 2001 Inventory #001571 10 -16
- Slides: 16