Manipulating Text Strings in ANSYS Mechanical APDL

Categories:

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:

*DIM,myString,STRING,80

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’
      comp(1,2)=’rightside’
      comp(1,3)=’topside’
      *do,I,1,3
      cmsel,a,%comp(1,i)%
      *enddo
  • 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.

String Functions

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.

Function

Description

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:

Function

Description

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.

Function

Description

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.

Conclusions

There are strings in the human heart that had better not be vibrated. – Charles Dickens

This has been a very brief overview of a powerful tool in ANSYS Mechanical APDL. Take a look at the help and other resources (xansys.org, ansys.net) to learn more and just start using it.  Fun stuff.

Categories

Get Your Ansys Products & Support from the Engineers who Contribute to this Blog.

Technical Expertise to Enable your Addictive Manufacturing Success.

PADT Pulse Newsletter Screen Grab from March 2023

PADT’s Pulse Newsletter

Keep up to date on what is going on at PADT by subscribing to our newsletter.


By submitting this form, you are consenting to receive marketing emails from: . You can revoke your consent to receive emails at any time by using the SafeUnsubscribe® link, found at the bottom of every email. Emails are serviced by Constant Contact

Share this post:

Upcoming Events

05/31/2023

Driving Automotive Innovation with Additive - Webinar

05/24/2023

Hill Air Force Base Tech Expo

05/24/2023

Structural Updates in Ansys 2023 R1 (3) – Structural Optimization & Ex

05/23/2023

CROSSTALK 2023: Emerging Opportunities for Advanced Manufacturing Smal

05/10/2023

Signal & Power Integrity Updates in Ansys 2023 R1 - Webinar

04/26/2023

Additive Manufacturing Updates in Ansys 2023 R1 - Webinar

04/20/2023

38th Space Symposium Arizona Space Industry

More Info

04/19/2023

38th Space Symposium
Arizona Space Industry

04/19/2023

Additive Aids for Manufacturing - Webinar

04/18/2023

38th Space Symposium
Arizona Space Industry

04/17/2023

38th Space Symposium

04/13/2023

Venture Madness 2023

04/12/2023

Fluid Meshing & GPU-Solver Updates in Ansys 2023 R1 - Webinar

03/29/2023

8th Thermal and Fluids Engineering Conference

03/29/2023

Structural Updates in Ansys 2023 R1 - Composites, Fracture & MAPDL

03/28/2023

8th Thermal and Fluids Engineering Conference

03/27/2023

8th Thermal and Fluids Engineering Conference

03/26/2023

8TH Thermal and Fluids Engineering Conference

03/24/2023

Arizona BioPreneur Conference | Spring 2023

03/22/2023

2023 Arizona MedTech Conference

03/22/2023

Optimize Jigs & Fixtures with Additive - Webinar

03/15/2023

3D Design Updates in Ansys 2023 R1 - Webinar

03/08/2023

Competitive Advantages of 1D/3D Coupled Simulation - Webinar

03/01/2023

High Frequency Updates in Ansys 2023 R1 - Webinar

02/22/2023

Additive Advantages in Aerospace - Webinar

02/15/2023

Structural Updates in Ansys 2023 R1 (1) - Webinar

02/09/2023

IME 2023: MD&M | WestPack | ATX | D&M | Plastek

02/08/2023

IME 2023 MD&M | WestPack | ATX | D&M | Plastek

02/07/2023

IME 2023 MD&M | WestPack | ATX | D&M | Plastek

01/27/2023

Arizona Photonics Days, 2023

01/26/2023

Arizona Photonics Days, 2023

01/26/2023

TIPE 3D Printing | 2023

01/26/2023

Venture Cafe Phoenix Talent Night - Job Fari

01/26/2023

VFS 2023 Autonomous/Electric VTOL Symposium

01/25/2023

Arizona Photonics Days, 2023

01/25/2023

Building A.M.- Utah: Kickoff!

01/25/2023

TIPE 3D Printing | 2023

01/25/2023

VFS 2023 Autonomous/Electric VTOL Symposium

01/24/2023

VFS 2023 Autonomous/Electric VTOL Symposium

01/24/2023

TIPE 3D Printing | 2023

01/18/2023

2023 AZ Tech Council Golf Tournament

12/21/2022

Simulation Best Practices for 5G Technology - Webinar

12/14/2022

Digital Twins Updates in Ansys 2022 R2 - Webinar

12/08/2022

Tech the Halls - AZ Tech Council Holiday Mixer

12/07/2022

Electric Vehicle and Other Infrastructure Update Panel

11/30/2022

SPEOS Updates in Ansys 2022 R2 - Webinar

11/23/2022

Simulation Best Practices for Electronics Reliability - Webinar

11/16/2022

Discovery Updates in Ansys 2022 R2

11/10/2022

VentureCafe Phoenix Panel: Venture Capital in AZ

11/08/2022

2022 GOVERNOR’S CELEBRATION OF INNOVATION AWARDS + TECH SHOWCASE

11/03/2022

VentureCafe Phoenix Panel: Angel Investment in AZ

11/02/2022

High & Low Frequency Electromagnetics Updates in Ansys 2022 R2

10/26/2022

Simulation Best Practices For Chip-Package-System Design & Development

10/20/2022

Nerdtoberfest 2022

10/19/2022

2022 Southern Arizona Tech + Business Expo

10/19/2022

LS-DYNA Updates in Ansys 2022 R2 - Webinar

10/17/2022

Experience Stratasys Truck Tour - Clearfield Utah

10/14/2022

ASU School of Manufacturing Systems and Networks - Formal Opening Cele

10/14/2022

Experience Stratasys Truck Tour - Midvale Utah

10/12/2022

Experience Stratasys Truck Tour - Littleton Colorado

10/06/2022

Fluids Updates in Ansys 2022 R2 - Webinar

10/05/2022

Experience Stratasys Truck Tour - Colorado Springs

09/29/2022

White Hat Life Science Investor Conference - 2022

09/28/2022

2022 AZBio Awards

09/28/2022

Simulation Best Practices for Rotating Machinery Design & Development

09/21/2022

ExperienceIT NM 2022

09/21/2022

Additive Updates in Ansys 2022 R2 - Webinar

09/14/2022

Rocky Mountain Life Sciences Investor & Partnering Conference

09/08/2022

Ansys Optics Simulation User Group Meeting - Virtual

09/08/2022

Ansys Optics Simulation User Group Meeting

09/07/2022

SI & PI Updates in Ansys 2022 R2 - Webinar

08/31/2022

Simulation Best Practices for Developing Medical Devices - Webinar

08/24/2022

Mechanical Updates in Ansys 2022 R2 - Webinar

08/10/2022

Tucson after5 Tech Mixer: Ruda-Cardinal

08/05/2022

Flagstaff Tech Tour, 2022

08/02/2022

2022 CEO Leadership Retreat

08/01/2022

2022 CEO Leadership Retreat

07/27/2022

Thermal Integrity Updates in Ansys 2022 R1 - Webinar

07/20/2022

Simulation Best Practices for the Pharmaceutical Industry - Webinar

07/14/2022

NCMS Technology Showcase: Corpus Christi Army Depot

07/13/2022

NCMS Technology Showcase: Corpus Christi Army Depot

07/13/2022

Additive & Structural Optimization Updates in Ansys 2022 R1 - Webinar

07/07/2022

Arizona AADM Conference, 2022

06/29/2022

LS-DYNA Updates & Advancements in Ansys 2022 R1 - Webinar

06/23/2022

Simulation Best Practices for Wind Turbine Design - Webinar

06/15/2022

MAPDL Updates & Advancements in Ansys 2022 R1 - Webinar

06/01/2022

Mechanical Updates in Ansys 2022 R1 - pt. 2 Webinar

05/26/2022

Modelling liquid cryogenic rocket engines in Flownex - Webinar

05/25/2022

SMR & Advanced Reactor 2022

05/25/2022

05/24/2022

SMR & Advanced Reactor 2022

05/19/2022

RAPID + tct 2022

05/19/2022

Venture Cafe Roundtable: AI & Healthcare

05/18/2022

Tucson after5 Tech Mixer: World View

05/18/2022

RAPID + tct 2022

More Info

05/18/2022

Signal & Power Integrity Updates in Ansys 2022 R1 - Webinar

05/18/2022

Simulation World 2022

05/17/2022

RAPID + tct 2022

05/11/2022

Experience Stratasys Manufacturing Virtual Event

Search in PADT site

Contact Us

Most of our customers receive their support over the phone or via email. Customers who are close by can also set up a face-to-face appointment with one of our engineers.

For most locations, simply contact us: