Ansys Macro Basics

16
 Macro Basics Module 10

Transcript of Ansys Macro Basics

Page 1: Ansys Macro Basics

7/21/2019 Ansys Macro Basics

http://slidepdf.com/reader/full/ansys-macro-basics 1/16

Macro Basics

Module 10

Page 2: Ansys Macro Basics

7/21/2019 Ansys Macro Basics

http://slidepdf.com/reader/full/ansys-macro-basics 2/16

Training Manual 

October 30, 2001

Inventory #001571

10-2 

10. Macro Basics

• One of the most powerful features of APDL (ANSYSParametric Desin Lanuae! is the a"ilit# to create macros$

• A macro is a se%uence of ANSYS commands stored in a file

and e&ecuted 'ust lie a reular command$

• Some useful macro capa"ilities) – *t can ha+e arguments as in a standard ANSYS command$

 – Branchin and loopin to control the se%uence of commands$

 – *nteracti+e features such as raphical picin, promptin, and

dialo "o&es$

 – Neste  macros - one macro callin a second one, which in turn

calls a third one, etc$ - up to .0 le+els deep$

Page 3: Ansys Macro Basics

7/21/2019 Ansys Macro Basics

http://slidepdf.com/reader/full/ansys-macro-basics 3/16

Training Manual 

October 30, 2001

Inventory #001571

10-3

...Macro Basics

• *n this chapter, we will present the "asics of macro writin)A$ /reatin a Macro

B$ Macro with Aruments

/$ Branchin

D$ Loopin

$ eneral uidelines

2$ 3orshop

• 2or more details, please refer to #our !"$ "rogrammer%s

&uie or the "rogramming in !N'(' seminar notes$

Page 4: Ansys Macro Basics

7/21/2019 Ansys Macro Basics

http://slidepdf.com/reader/full/ansys-macro-basics 4/16

Training Manual 

October 30, 2001

Inventory #001571

10-)

Macro Basics

 !. *reating a Macro

• 4o create a macro, simpl# start a te&t editor, insert thedesired se%uence of commands, and sa+e them to a file

called name.mac$

 – name can "e up to 5. characters, startin with a letter$

• Spaces are not allowed in the name$

• Also a+oid special characters$

• Mae sure that name is not a +alid ANSYS command "#

t#pin in name at Bein le+el and in all processors (P6P7,

POS41, etc$!$ *f #ou et the messae … not a recognized

command or macro… then the name is 8safe$9

 – &tension .mac allows #ou to e&ecute the macro as if it were acommand "# simpl# t#pin in name$

Page 5: Ansys Macro Basics

7/21/2019 Ansys Macro Basics

http://slidepdf.com/reader/full/ansys-macro-basics 5/16

Training Manual 

October 30, 2001

Inventory #001571

10-5 

Macro Basics

...*reating a Macro

• &ample) – A macro totvolume.mac to calculate the total +olume 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

 – *ssue totvolume in POS41 (after a sol+e! to calculate the total

+olume$

Page 6: Ansys Macro Basics

7/21/2019 Ansys Macro Basics

http://slidepdf.com/reader/full/ansys-macro-basics 6/16

Training Manual 

October 30, 2001

Inventory #001571

10-+ 

Macro Basics

...*reating a Macro

• Search Path) – ANSYS will e&ecute the first name.mac file it finds in the

followin search se%uence)

1$ /ansys6/ocu

.$ director#(ies! in "#S$S%&"'()L+ en+ironment +aria"le

5$ loin director# (home director# on 3indows s#stems!

:$ current (worin! director#

 – *f the search finds "oth upper;case and lower;case files of the

same name, the upper;case file is used$

Page 7: Ansys Macro Basics

7/21/2019 Ansys Macro Basics

http://slidepdf.com/reader/full/ansys-macro-basics 7/16

Training Manual 

October 30, 2001

Inventory #001571

10-7 

Macro Basics

B. Macro it !rguments

• B# usin special parameter names, #ou can create a macrowith up to .0 aruments)

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

• 4he aruments "eha+e 'ust lie the fields on a standard

ANSYS command and can accept)

 – num"ers

 – alphanumeric characters (enclosed in sinle %uotes!

 – parameters (scalar or arra#!

 – parametric e&pressions

• 4he meanin of the aruments depends on how #ou want to

desin the macro$

Page 8: Ansys Macro Basics

7/21/2019 Ansys Macro Basics

http://slidepdf.com/reader/full/ansys-macro-basics 8/16

Training Manual 

October 30, 2001

Inventory #00157110- 

Macro Basics

...Macro it !rguments

• 2or e&ample, we could desin totvolume.mac to calculate thetotal +olume for all elements of a specified t#pe)

 – )-)L&, TYPE 

• 4he macro would then loo lie this)

esel,s,ty0e,,arg1 ! Select elements of s0ecifie ty0e

etable,volume,volu ! Store volume in element table

ssum ! Sum element table items

*get,totvol,ssum,,item,volume ! totvol = sum of ‘volume’

*v2rite,arg1,totvol ! 3rite out arg1 an totvol

4‘otal volume for ty0e ’, 5., ‘ elements = ’, 57.89

• *ssuin totvolume,1 in POS41 after a solution will then result in)

Page 9: Ansys Macro Basics

7/21/2019 Ansys Macro Basics

http://slidepdf.com/reader/full/ansys-macro-basics 9/16

Training Manual 

October 30, 2001

Inventory #00157110-/

Macro Basics

...Macro it !rguments

• Notes) – 4he special parameter names A61;A6< and A610;A6<< are

local parameters +alid onl# within a macro$

• 4he# hold no meanin once the macro has finished e&ecutionand control is returned to 8main9 ANSYS$

• A+oid usin these names elsewhere in the model$

 – 3hene+er #ou use aruments, "e sure to descri"e their meanin"# includin comments in the macro$

2or e&ample, the followin comments at the "einnin oftotvolume.mac would "e helpful$

! &acro )-)L&.&"' to calculate total volume of elements

! sage: )-)L&, $; < vali only in ;)S1 after a solve! $; = vali element ty0e number 

esel,s,ty0e,,arg1 ! Select elements of s0ecifie ty0e

Page 10: Ansys Macro Basics

7/21/2019 Ansys Macro Basics

http://slidepdf.com/reader/full/ansys-macro-basics 10/16

Training Manual 

October 30, 2001

Inventory #00157110-10 

Macro Basics

*. Brancing 

• B# usin an *2;4=N;LS construct, #ou can e&ecute a command or

"loc of commands onl# if certain conditions are met$

• Additional comparison operation are a+aila"le for the >*2 and >LS*2commands with AND, O6, or ?O6 options$

 –  >*2,A,@,B,AND,/,4,D,4=N

• Branchin "eins with *5 and ends with *#>5$ *LS5 and *LS 

are also allowed in "etween)*if, ?, e@, y, tAen

*elseif, ?, e@, B, tAen

*else

*enif 

• >*2 constructs can "e nested up to twent# le+els

Page 11: Ansys Macro Basics

7/21/2019 Ansys Macro Basics

http://slidepdf.com/reader/full/ansys-macro-basics 11/16

Training Manual 

October 30, 2001

Inventory #00157110-11

Macro Basics

...Brancing 

• 4he condition can "e)

?, C, y ! ? = y

?, #, y ! ? ≠ y

?, L, y ! ? D y

?, E, y ! ? F y

?, L, y ! ? ≤ y

?, E, y ! ? ≥ y

?, "+L, y ! G?G D GyG

?, "+E, y ! G?G F GyG

• & and # can "e num"ers,

parameters, or parametric

e&pressions$

• 4he action can "e)

4=N to e&ecute the

su"se%uent "loc of

commands>?*4 to e&it a do;loop

>/Y/L to sip to the end of a

do;loop

• 4he action taes place onl# if

the condition is true$Otherwise, ANSYS will mo+e

on to *LS5 (if present!,

*LS (if present!, and *#>5$

*if, ?, e@, y, tAen

Page 12: Ansys Macro Basics

7/21/2019 Ansys Macro Basics

http://slidepdf.com/reader/full/ansys-macro-basics 12/16

Training Manual 

October 30, 2001

Inventory #00157110-12 

Macro Basics

...Brancing 

•2or e&ample, #ou can add an if;test to totvolume.mac to test for+alid +alues of the input arument)

*if,arg1,lt,1,tAen ! f arg1 D 1

*msg,2arn ! ssue a 2arning...

lement ty0e number must be 1 or greater 

/eof ! an e?it tAe macro

*enif esel,s,ty0e,,arg1 ! Select elements of s0ecifie ty0e

etable,volume,volu ! Store volume in element table

ssum ! Sum element table items...

• *ssuin totvolume,<1 will now result in)

Page 13: Ansys Macro Basics

7/21/2019 Ansys Macro Basics

http://slidepdf.com/reader/full/ansys-macro-basics 13/16

Training Manual 

October 30, 2001

Inventory #00157110-13

Macro Basics

. $ooing 

•Do;loops allow #ou to loop throuh a "loc of commandsse+eral times$

• 4here is +irtuall# no limit to what #ou can include in an

ANSYS do;loop$ You can loop throuh an entire anal#sis

session - includin preprocessin, solution, and

postprocessin - if the situation warrants it$

• *>) or *>)3HL "eins a loop, *#>>) ends it$

 –  You can control the loopin usin *I, which e&its the do;

loop, and *'$'L, which sips to the end of the do;loop$

 – it  and cyce can also "e done as a result of an if;test$

• As an e&ample, we can e&tend the totvolume.mac macro to

loop throuh all element t#pes in the model and store the

+olume for each t#pe in an arra# parameter$

Page 14: Ansys Macro Basics

7/21/2019 Ansys Macro Basics

http://slidepdf.com/reader/full/ansys-macro-basics 14/16

Training Manual 

October 30, 2001

Inventory #00157110-1)

Macro Basics

...$ooing 

! << &acro )-)L&.&"' to calculate total element volume.

! << sage: ssue )-)L& in ;)S1 after a solution.

! << (esult:

! << a9 evolume4i9 = total volume for element ty0e i

! << b9 totvol = gran total volume

!

*get,numty0es,ety0e,,num,count ! Eet number of element ty0es

*im,evolume,array,numty0es ! )0en a numty0es ? 1 array

*o,i,1,numty0es ! 5or i = 1 < numty0es...

esel,s,ty0e,,i ! Select elements of ty0e i

etable,volume,volu ! Store volume in element table

ssum ! Sum element table items

*get,totvol,ssum,,item,volume ! totvol = sum of ‘volume’

evolume4i9 = totvol ! Store totvol in evolume4i9

*eno ! n of o<loo0

*vscfun,totvol,sum,evolume4i9 ! totvol = gran total volume

esel,all ! "ctivate full set of elements

Page 15: Ansys Macro Basics

7/21/2019 Ansys Macro Basics

http://slidepdf.com/reader/full/ansys-macro-basics 15/16

Training Manual 

October 30, 2001

Inventory #00157110-15 

Macro Basics

. &enera &uieines

•Start with small, simple macros$

• As #ou create the macro, remem"er that #ou can cut an

 aste the commands into the ANSYS *nput window to test

and mae sure that the command se%uence is correct$

•se comments to descri"e the intent or e&pected outcome ofcommands$

• Place #our 8personal9 macros in #our loin director#$

• Place compan#;wide macros in a director# that e+er#one can

access, and include that director# in "#S$S%&"'()L+ en+ironment +aria"le$

Page 16: Ansys Macro Basics

7/21/2019 Ansys Macro Basics

http://slidepdf.com/reader/full/ansys-macro-basics 16/16

Training Manual 

October 30, 2001

Inventory #00157110-1+ 

Macro Basics

4. or6so

•4his worshop consists of the followin pro"lem)3<$ erif#in Pressures

Please refer to #our or6so 'uement  for instructions$