The world is all gates, all opportunities, strings of tension waiting to be struck. – Ralph Waldo Emerson
The ANSYS Parametric Design Language has a lot of features in it. Over the years it has grown to meet the needs of a growing population of users and has gone from a simple command scrip to a fairly sophisticated programming tool. One area of the tool that power users find significant value is are the functions that are used to manipulate text strings. You can use these functions to do all sorts of things that are needed to automate a process or produce more informative results. Some examples are:
- Gather information about your model and create a screen for display on a plot or in a report
- Get information about files and directories and use that to manage, create, delete, etc…, those files.
- Build commands up so APDL can write APDL or input to other programs
- Create or modify parameter names for use in your macros
In this article we will look at strings in APDL and how to work with them. You can find additional, more detailed information in the ANSYS Help, Mechanical APDL>ANSYS Parametric Deign Language Guide. There is also more information on this topic and all things APDL in PADT’s Guide to the ANSYS Parametric Design Language (APDL).
Strings in ANSYS Mechanical APDL
Pull the string, and it will follow wherever you wish. Push it, and it will go nowhere at all. – Dwight D Eisenhower
Before we talk about manipulating strings, we should do some background on strings.
Strings can either be stored as a character parameter or as a string array. Character parameters are nice because they don’t require indices when you use them, and you don’t have to define them with a *dim. The down side is that you are limited to 8 characters.
You create a string parameter by using the parameter = method:
myChr = ‘yeehah’
To be more robust and avoid size issues, you should use a string array, which you create with a *dim command:
This would create a text string called myString that is 80 characters long. And you would do this if you are going to use a function to fill it. Once you create a string you refer to it with its name and an index for the start of the string.
/title, Working on Model %myString(1)%
For a character parameter, you don’t need the index:
/title,Working on Model %myChr%
Stuff you should know about strings in APDL:
- You define the value of a string by putting the text in single quotes ‘string’
- The longest string you can have is 256 character – but don’t worry, you can string strings together.
- String arrays can be two dimensional, so if you have a bunch of text you want to keep together you can do that fairly easily. You do have to use *dim if you do this:
- *dim,comp,string,80, 3 [Fixed on 11/19/14 ]
comp(1,1) = ‘leftSide’
- *dim,comp,string,80, 3 [Fixed on 11/19/14 ]
- You can make an array of type Char as well. This is basically a list of several character parameters.
- To look at what is in your array/parameter, use *stat.
Money is the string with which a sardonic destiny directs the motions of its puppets. – W. Sumerset Maugham
Now on to the fun stuff!
Once you have a string you can manipulate it using the string functions. We will look at each group.
Strings to and from Numbers
Sometimes you have a number as a string and you need it as a number, or you need to convert a number into a string. Those numbers can be decimal or Octal or Hex. This Table shows the functions for going back and forth.
|VALCHR(string)||Converts a string that contains a valid decimal or integer number as text into a double precision parameter|
|VALOCT(string)||Converts a string that contains a valid Octal number as text into a double precision parameter|
|VALHEX(string)||Converts a string that contains a valid Hexadecimal number as text into a double precision parameter|
|CHRVAL(num)||Converts a decimal number into a text string|
|CHROCT(num)||Converts a number into a text string octal number|
|CHRHEX(num)||Converts a number into a text string hexahedral number|
The most commonly used function in this group is CHARVAL() to truncate the number of decimals in a number.
Standard String Functions
If you have every programed in FORTRAN, C or any other “old school” language you are familiar with the standard set of commands that are used to manipulate strings. APDL supports most of them:
|STRSUB(Str1,nLoc,nChar)||Returns a substring of the characters in Str1. Returns characters number nLoc to nLoc+nChar as a string.|
|STRCAT(Str1,Str2)||Concatenates Str2 onto the end of Str1. Returns a string.|
|STRFILL(Str1,Str2,nLoc)||Fill in Str1 with Str2 starting at character number nLoc in Str1. Returns a string.|
|STRCOMP(Str1)||Compress Str1 by removing all the spaces in Str1. Returns a string.|
|STRLEFT(Str1)||Left justify Str1 by removing all leading spaces from Str1. Returns a string.|
|STRPOS(Str1,Str2)||Returns the position in Str1 where Str2 can be found. If it is not found, it returns 0. Returns a number.|
|STRLENG(Str1)||Returns the length of Str1, excluding training spaces. Returns a number.|
|UPCASE(Str1)||Converts all the characters in Str1 to Upper Case. Returns a string.|
|LWCASE(Str1)||Converts all the characters in Str1 to Lower Case. Returns a string.|
These are fairly standard and work as you would expect.
File Name Functions
The last two functions are there for you to work with file names and directories. There are three parts to a path name in ANSYS MAPDL: the directory where the file is (directory), the portion of the filename before the period (filename) and the part after the period (ext). The first function allow you to build a complete string that specifies a file and the second takes a complete path and divides it into its pieces.
|JOIN(directory,filename,extension)||Creates a path string in the current OS (forward vs. backward slashes) from the supplied directory, filename, and extension. Directory is optional.|
|Split(Path,Option)||Takes a full pathname in the current OS and splits it up into pieces. Option can be:
DIR = give just the directory
FILE = Give the filename and extension
Name = just the filename, the part before the period
EXT = give the extension, the part after the period.
Using Strings in APDL
There is geometry in the humming of the strings, there is music in the spacing of the spheres. – Pythagoras
The examples above sort of hint at how to use strings in APDL but it is probably a good idea to explicitly talk about it. You can use strings in commands in clever ways, some commands use strings as arguments, and you can write strings to files.
String Substitution in a Command
This is the most common usage and the most powerfully. Any CHAR or STRING parameter can be put into any APDL command by simply putting it inside percent signs: %myString%
So you can do something like:
/title, Results for Substep %isbstp%. Freq = %frq%
Some people get very sophisticated with this and build their own commands this way. Do note that this substitution also works for numbers. cmsel,s,ncmp%i% where i = 32 will look to the program like cmsel,s,ncmp32.
Strings as Arguments
This is fairly straightforward, if a command is looking for a string, give it a string. There are not too many commands that take strings as arguments but there are a few.
Writing Strings to Files
There are three basic ways to write to a file in APDL: *create, *cwrite, and *vwrite. We are not going to get into there use here, but check out the usage of each in detail in the help.
There are strings in the human heart that had better not be vibrated. – Charles Dickens