Академический Документы
Профессиональный Документы
Культура Документы
ANSYS Confidential
Overview
Introduction
Introduction to Perl
Power Syntax
Perl subroutines
Macros
February 23, 2015
ANSYS Confidential
Introduction
Need for scripting and automation
Increase productivity by simplifying repetitive tasks
Standardize practices
Save and re-use data
ANSYS Confidential
ANSYS Confidential
Server
GUI Mode
User
Default mode of operation
Graphical client driven by user input
GUI
CCL
Engine
ANSYS Confidential
e.g. >cfx5post
line
ANSYS Confidential
Batch Mode
Script
Direct to engine
CCL
ANSYS Confidential
Engine
Session Files
Session files contain a list of commands and CCL
objects
Can record the commands executed during a
session to a file and then play back the file at a later
date or in batch mode.
Can write/modify session files in a text editor
Produced in Pre, Post, TurboGrid
Session files can perform actions, for example
Input/Output
8
ANSYS Confidential
State Files
ANSYS Confidential
Introduction to Perl
16.0 Release
ANSYS Confidential
What is Perl?
11
ANSYS Confidential
12
Advantages
Disadvantages
It is an interpreted language
ANSYS Confidential
Perl References
Books:
Introductory
Randal L. Schwartz et al, Learning Perl, O'Reilly and Associates, Inc.
Hoffman, Perl 5 For Dummies, IDG Books, ISBN 0-7645-0460-6
Advanced Use
S. Srivivasan, Advanced Perl Programming, O'Reilly and Associates, Inc.
Web:
www.perl.org, www.perl.com, www.perldoc.perl.org
newsgroups
13
ANSYS Confidential
Perl Example
#!/usr/bin/perl
print What is your name? ;
$name = <STDIN>;
chomp($name);
if ($name eq Steve) {
print Hi Steve! Good to see you again!\n;
#friendly greeting
} else {
print Hello, $name. Nice to meet you.\n;
#ordinary greeting
}
14
ANSYS Confidential
Syntax Basics
15
ANSYS Confidential
Perl Variables
Linear Arrays: @
Elements reference by position
Elements may be any type (scalar, array, hash)
ANSYS Confidential
Scalar Variables $
17
ANSYS Confidential
Strings ,
18
Escape Character
Meaning
\n
\t
Newline
Tab
\\
literal \
\"
\xnn
literal "
hex ascii value nn
ANSYS Confidential
Arrays (lists)
19
ANSYS Confidential
Arrays (lists)
To determine the size of an array, use scalar(@listName)
Useful array functions:
ANSYS Confidential
#
# All are equivalent
#
String operators
concatenation (. or .=)
hello . world
fred . . wilma
la x 3
foo x (4 + 1)
ANSYS Confidential
#gives helloworld
#gives fred wilma
#gives lalala
#gives foofoofoofoofoo
Perl Functions
Perl has many, many built in functions
Functions for:
Text processing, numeric functions, process control, list processing, file
IO, data processing, etc.
They all:
Have a name
Return a value or list of values
Can accept arguments
22
ANSYS Confidential
$res = log(123);
$angle = atan2(.5,-.5);
push(@myList,2,4,7);
$value = pop(@myList);
$textBit = substr($myString,3,2);
Logic in Perl
23
ANSYS Confidential
Numeric
==
!=
<
>
<=
>=
String
eq
ne
lt
gt
le
ge
24
ANSYS Confidential
if...
if:
if (some expression) {
statement 1;
statement 2; ...
} elsif (another_expression) {
statement 3; ...
} else {
statement 4;
statement 5; ...
}
print How old are you?;
$a = <STDIN>;
chomp($a);
if ($a < 18) {
print Sorry, youre too young.\n;
die;
} else {
print Welcome\n;
}
25
ANSYS Confidential
while:
26
foreach...
27
ANSYS Confidential
28
|| is a logical or
ANSYS Confidential
Basic I/O
29
ANSYS Confidential
Basic I/O
ANSYS Confidential
Basic I/O
close(FILEHANDLE) when done with a file
STDIN, STDOUT, STDERR are automatically managed
filehandles.
print How old are you?;
$a = <STDIN>;
chomp($a);
if ($a < 18) {
print Welcome, my child.\n;
} else {
print Wow, $a is pretty old.\n;
}
ANSYS Confidential
External Processes
32
ANSYS Confidential
Regular Expressions
ANSYS Confidential
Subroutines
34
ANSYS Confidential
Subroutines
35
ANSYS Confidential
References
36
ANSYS Confidential
References
sub printHash {
($hashRef) = @_;
foreach $key (keys(%$hashRef)) {
print Key: $key, Value: . $hashRef->{$key} .
\n;
}
}
%myHash = (a => b, c => d);
printHash(\%myHash);
37
ANSYS Confidential
Variable Scope
38
ANSYS Confidential
39
ANSYS Confidential
40
ANSYS Confidential
Or
open(CFDPOST, |cfx5post line);
print CFDPOST CCL COMMANDS
close(CFDPOST);
41
ANSYS Confidential
! $numSteps = 20;
! for (my $i=0; $i<=$numSteps; $i++) {
! $transparency = $i/$numSteps;
BOUNDARY:Default
Transparency = $transparency
END
!}
42
ANSYS Confidential
! $speed = 2.0;
! if ($speed gt 1.5) {
!
$turbulence = on;
! }
...
BOUNDARY: inlet1
Normal speed in = $speed [m/s]
! if ($turbulence == on) {
Eddy length scale = 0.001 [m]
! }
END
43
ANSYS Confidential
44
ANSYS Confidential
Quantitative functions
All CEL extended functions have power syntax equivalents (e.g.)
$val = massFlow(location);
$val = areaAve(variable,location);
45
ANSYS Confidential
46
ANSYS Confidential
Post Macros
47
ANSYS Confidential
Macro Header
48
! sub mySub {
! ( $variable, $plane) = @_;
!
! print "variable = $variable, plane =
$plane\n";
!}
ANSYS Confidential
Macro Header
49
Value
Definition
The file generated by the macro (if any). This enables the
View Report button, which attempts to load the file in a
text/html browser.
ANSYS Confidential
Macro Header
50
Type
Option
Example
string
default
My String
integer
default
range
10
1, 100
float
default
ANSYS Confidential
CFD-Post
This example exports four
mass flow rate values to a text
file
51
ANSYS Confidential
CFD-Post
This example creates 10 cut planes
colored by pressure through a
domain
52
ANSYS Confidential
Workshop Appendix A
This workshop takes you through the use of session files and scripts to run a series of
simulations of flow over a backward facing step to compare the results obtained with
different turbulence models.
3.8 x H
4xH
40 x H
Inlet
Outlet
Flow Separation
q
H
53
ANSYS Confidential