2_10-macro.ppt

Embed Size (px)

Citation preview

  • 8/11/2019 2_10-macro.ppt

    1/16

    Macro Basics

    Module 10

  • 8/11/2019 2_10-macro.ppt

    2/16

    I

    N

    R

    O

    U

    O

    T

    O

    A

    N

    Y

    6

    0

    P

    2

    Training Manual

    Octob er 30, 2001

    Invento ry #001571

    10-2

    10. Macro Basics

    One of the most powerful features of APDL (ANSYSParametric Design Language) is the ability to create macros.

    A macrois a sequence of ANSYS commands stored in a file

    and executed just like a regular command.

    Some useful macro capabilities: It can have argumentsas in a standard ANSYS command.

    Branching and looping to control the sequence of commands.

    Interactive features such as graphical picking, prompting, and

    dialog boxes.

    Nestedmacros one macro calling a second one, which in turncalls a third one, etc. up to 20 levels deep.

  • 8/11/2019 2_10-macro.ppt

    3/16

    I

    N

    R

    O

    U

    O

    T

    O

    A

    N

    Y

    6

    0

    P

    2

    Training Manual

    Octob er 30, 2001

    Invento ry #001571

    10-3

    ...Macro Basics

    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

    For more details, please refer to yourAPDL Programmers

    Guideor the Programm ing in ANSYS seminar notes.

  • 8/11/2019 2_10-macro.ppt

    4/16

    I

    N

    R

    O

    U

    O

    T

    O

    A

    N

    Y

    6

    0

    P

    2

    Training Manual

    Octob er 30, 2001

    Invento ry #001571

    10-4

    Macro B asics

    A. Creat ing a Macro

    To create a macro, simply start a text editor, insert thedesired sequence of commands, and save them to a file

    called name.mac.

    namecan be up to 32 characters, starting with a letter.

    Spaces are not allowed in the name.

    Also avoid special characters. Make sure that nameis not a valid ANSYS command by

    typing in nameat Begin level and in all processors (PREP7,

    POST1, etc.). If you get the message not a recognized

    command or macrothen the name is safe.

    Extension .macallows you to execute the macro as if it were acommand by simply typing in name.

  • 8/11/2019 2_10-macro.ppt

    5/16

    I

    N

    R

    O

    U

    O

    T

    O

    A

    N

    Y

    6

    0

    P

    2

    Training Manual

    Octob er 30, 2001

    Invento ry #001571

    10-5

    Macro B asics

    ...Creating a Macro

    Example: A macro totvolume.macto calculate the total volume of all

    elements:

    esel,all ! Select all elements

    etable,volume,volu ! Store volume in element table

    ssum ! Sum element table items*get,totvol,ssum,,item,volume ! totvol = sum of volume

    *stat,totvol ! List totvol value

    Issue totvolumein POST1 (after a solve) to calculate the total

    volume.

  • 8/11/2019 2_10-macro.ppt

    6/16

    I

    N

    R

    O

    U

    O

    T

    O

    A

    N

    Y

    6

    0

    P

    2

    Training Manual

    Octob er 30, 2001

    Invento ry #001571

    10-6

    Macro B asics

    ...Creating a Macro

    Search Path: ANSYS will execute the first name.macfile it finds in the

    following search sequence:

    1. /ansys60/docu

    2. directory(ies) inANSYS_MACROLIBenvironment 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.

  • 8/11/2019 2_10-macro.ppt

    7/16

    I

    N

    R

    O

    U

    O

    T

    O

    A

    N

    Y

    6

    0

    P

    2

    Training Manual

    Octob er 30, 2001

    Invento ry #001571

    10-7

    Macro B asics

    B. Macro wi th Arguments

    By using special parameter names, you can create a macrowith up to 20 arguments:

    NAME, arg1, arg2, arg3, , ar10, ar11, ar12, , ar20

    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 the arguments depends on how you want to

    design the macro.

  • 8/11/2019 2_10-macro.ppt

    8/16

    I

    N

    R

    O

    U

    O

    T

    O

    A

    N

    Y

    6

    0

    P

    2

    Training Manual

    Octob er 30, 2001

    Invento ry #001571

    10-8

    Macro B asics

    ...Macro w ith A rguments

    For example, we could design totvolume.macto calculate thetotal 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 ! totvol = sum of volume

    *vwrite,arg1,totvol ! Write out arg1 and totvol

    (Total volume for type , F4.0, elements = , F8.2)

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

  • 8/11/2019 2_10-macro.ppt

    9/16

    I

    N

    R

    O

    U

    O

    T

    O

    A

    N

    Y

    6

    0

    P

    2

    Training Manual

    Octob er 30, 2001

    Invento ry #001571

    10-9

    Macro B asics

    ...Macro w ith A rguments

    Notes:

    The special parameter names ARG1-ARG9 and AR10-AR99 arelocal parameters valid only within a macro.

    They hold no meaning once the macro has finished executionand control is returned to main ANSYS.

    Avoid using these names elsewhere in the model.

    Whenever you use arguments, be sure to describe their meaningby including comments in the macro.

    For example, the following comments at the beginning oftotvolume.macwould 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

  • 8/11/2019 2_10-macro.ppt

    10/16

    I

    N

    R

    O

    U

    O

    T

    O

    A

    N

    Y

    6

    0

    P

    2

    Training Manual

    Octob er 30, 2001

    Invento ry #001571

    10-10

    Macro B asics

    C. Branching

    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 *IFand ends with *ENDIF. *ELSEIFand

    *ELSEare 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

  • 8/11/2019 2_10-macro.ppt

    11/16

    I

    N

    R

    O

    U

    O

    T

    O

    A

    N

    Y

    6

    0

    P

    2

    Training Manual

    Octob er 30, 2001

    Invento ry #001571

    10-11

    Macro B asics

    . . .Branching

    The condition can be:

    x, EQ, y ! x = y

    x, NE, y ! x y

    x, LT, y ! x < y

    x, GT, y ! x > y

    x, LE, y ! x y

    x, GE, y ! x y

    x, ABLT, y ! |x| < |y|

    x, ABGT, y ! |x| > |y|

    x and y can be numbers,

    parameters, or parametric

    expressions.

    The action can be:

    THEN to execute the

    subsequent block of

    commands*EXIT to exit a do-loop

    *CYCLE to skip to the end of a

    do-loop

    The action takes place only if

    the condition is true.Otherwise, ANSYS will move

    on to *ELSEIF(if present),

    *ELSE(if present), and *ENDIF.

    *if, x, eq, y, then

  • 8/11/2019 2_10-macro.ppt

    12/16

    I

    N

    R

    O

    U

    O

    T

    O

    A

    N

    Y

    6

    0

    P

    2

    Training Manual

    Octob er 30, 2001

    Invento ry #001571

    10-12

    Macro B asics

    . . .Branching

    For example, you can add an if-test to totvolume.macto testfor 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

    *endifesel,s,type,,arg1 ! Select elements of specified type

    etable,volume,volu ! Store volume in element table

    ssum ! Sum element table items...

    Issuing totvolume,-1will now result in:

  • 8/11/2019 2_10-macro.ppt

    13/16

    I

    N

    R

    O

    U

    O

    T

    O

    A

    N

    Y

    6

    0

    P

    2

    Training Manual

    Octob er 30, 2001

    Invento ry #001571

    10-13

    Macro B asics

    D. Looping

    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, solution, and

    postprocessing if the situation warrants it.

    *DO or *DOWHILEbegins a loop, *ENDDOends it.

    You can control the looping using *EXIT, which exits the do-

    loop, and *CYCLE, which skips to the end of the do-loop.

    Exitand cyc lecan also be done as a result of an if-test.

    As an example, we can extend the totvolume.macmacro to

    loop through all element types in the model and store the

    volume for each type in an array parameter.

  • 8/11/2019 2_10-macro.ppt

    14/16

    I

    N

    R

    O

    U

    O

    T

    O

    A

    N

    Y

    6

    0

    P

    2

    Training Manual

    Octob er 30, 2001

    Invento ry #001571

    10-14

    Macro B asics

    . . .Looping

    ! -- 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 ! 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

  • 8/11/2019 2_10-macro.ppt

    15/16

    I

    N

    R

    O

    U

    O

    T

    O

    A

    N

    Y

    6

    0

    P

    2

    Training Manual

    Octob er 30, 2001

    Invento ry #001571

    10-15

    Macro B asics

    E. General Guidel ines

    Start with small, simple macros.

    As you create the macro, remember that you can cut and

    pastethe 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 inANSYS_MACROLIB

    environment variable.

  • 8/11/2019 2_10-macro.ppt

    16/16