CFD Review  
Serving the CFD Community with News, Articles, and Discussion
 
CFD Review

User Preferences
Site Sponsorship
Headline Feeds
Mobile Edition
Privacy Policy
Terms of Service
twitter

Submit a CFD Story

Site Sponsors
CD-adapco
Pointwise: Reliable CFD meshing
ANSYS

Tell a Friend
Help this site to grow by sending a friend an invitation to visit this site.

CFD News by Email
Did you know that you can get today's CFD Review headlines mailed to your inbox? Just log in and select Email Headlines Each Night on your User Preferences page.

 
Automating CFD Meshing Via Scripting
Posted Tue February 25, 2014 @12:12PM
Print version Email story Tweet story
Grid Generation John Rhoads,
Pointwise, Inc.

The holy grail of the preprocessing world is to be able to produce in the blink of an eye a mesh that meets every grid quality criteria imaginable around any arbitrary geometry. In reality, discretizing the volume around an object can present a number of challenges, particularly when you place constraints on the shape and orientation of the elements. These challenges ultimately require users to make a choice between automatic meshing solutions or manual mesh generation.


Sponsor CFD Review

Automatic meshers often sacrifice fine control over the final mesh in order to minimize user interaction. For some applications this approach is sufficient, but for many applications, researchers want to have the ability and tools to create the highest quality mesh possible. Unfortunately, this implies that the mesh generation will involve some user interaction.

Pointwise was created specifically for those engineers and scientists who need to realize the highest quality mesh possible. The tools within Pointwise give the user unprecedented control over the final mesh, down to the location of individual points in the grid. However, the cost of this control is the sometimes tedious process of manually creating portions of the mesh. Fortunately, scripting can be used to reduce or eliminate some of these tedious tasks.

The Benefits of Scripting
As the name suggests, scripting simply involves writing a series of commands for the program to execute, thereby automating a portion of the meshing process. The scripting tools can be accessed directly from the Pointwise menu bar, as seen in Figure 1. Scripting in Pointwise can be picked up very quickly, overcoming the objections of non-programmers that scripting is difficult, esoteric or simply not worth learning. Pointwise scripting provides a well-balanced compromise between automatic and automated mesh generation.

script menu
Figure 1. Scripting commands can be referenced from the Script menu in the Pointwise menu bar.

Macros
Scripting is the ideal tool to use when you find yourself repeating a task frequently. If you have encountered a cumbersome process you would like to simplify or expedite, then the time investment to create a script will likely provide an excellent return. There are many freely available scripts ready for download from our Glyph Script Exchange (hosted on GitHub) that serve as examples of how scripts can simplify the workflow in Pointwise and even extend its functionality.

  • The ButterflyMaker script in the repository automates the process of creating an OH topology from a simple H-block.
  • Also very handy is the ShapeWizard, which allows you to quickly create common shapes like prisms, spheres, or cones. This is particularly useful for creating the farfield boundary around your geometry.

To be fair, not all processes within Pointwise lend themselves to scripting. For instance, adjusting the control points on a curve to improve orthogonality is better suited to interactive work with the GUI on a case-by-case basis. It is possible to adjust the location of control points through a script, but this type of operation can benefit immensely from the visualization and interactive tools available in Pointwise.

Templates
Also, if you find yourself repetitively meshing the same geometry with minor changes, then it may be possible to script the entire meshing process with a template.

  • A template allows you to quickly mesh slight variations of a baseline geometry as seen in this webinar recording of the aircraft geometry from the AIAA Drag Prediction Workshop.
  • This The Connector article describes a Glyph script for automating the meshing process around jet nozzles.
  • Another example of templates can be found in the AirfoilMesh script in the Glyph Script Exchange. This script will create NACA 4-series 2D airfoils and also generate a boundary layer mesh around the geometry in a couple of mouse clicks.

Although it is possible to script projects from start to finish, some of the most useful scripts simply perform a portion of the meshing process and automate multiple steps, greatly reducing the time spent generating the mesh. Additionally, scripting gives you direct access to information stored in the grid (such as coordinates of nodes), that can be accessed through the GUI, but not easily used.

Scripting, Glyph, and Tcl
The scripting language within Pointwise is called Glyph, which is built upon a Tcl/Tk framework. Tcl is an open-source programming language that provides cross-platform support. The syntax differs slightly from other scripting languages like bash or Python, but the overall approach is very similar. An excellent tutorial in Tcl can be found at www.tcl.tk.

Once you are familiar with the basics of Tcl, you can write a Glyph script for use in Pointwise in a matter of minutes. Every command from the Pointwise GUI is accessible through Glyph, and there are several ways to find the proper syntax to include in the script. Even so, sometimes it can be hard to know where to start. Developing a script can be thought of as a process involving five steps:

  1. Identify candidate for scripting.
  2. Develop workflow.
  3. Identify commands to use in script.
  4. Test, test, test.
  5. Tweak.
Let’s walk through these five steps for a specific script: a macro for preserving a connector’s grid point distribution attributes after projection.

1. Identify Candidate for Scripting
The first step in creating a script is finding a series of tasks that could be consolidated and handled with minimal user interaction. Any time you find yourself repeating a series of commands on a single entity, chances are a script could be very useful.

For example, suppose you would like to project a connector onto an underlying database while retaining the initial distribution attributes. By default, Pointwise projects each point along the connector individually onto the database, resulting in a general distribution of points after projection. This produces the closest approximation to the initial distribution, but loses the mathematical relationship describing the location of each point. As a result, applying a spacing constraint to either end of the connector will no longer have the same effect on the distribution of points.

Within Pointwise, the distribution can be reset manually after the projection by going to Grid, Distribute, selecting the Functions tab, selecting Tanh (hyperbolic tangent, the default distribution function), and clicking OK. However, this is a perfect example where a script can boost productivity by combining the projection and redistribution into one step.

2. Develop Workflow
The next step toward writing a functional script is to step through the actions to establish the workflow for the script. Having a clear outline of the steps involved and the variables to be parameterized is crucial to developing a robust script. Identifying user-adjustable parameters early in the process can extend the applicability of a script, making it both more useful and more easily adapted for future applications. This is also the point at which careful forethought can provide built-in error handling, rather than necessitating extensive catches further along.

The key is to reduce the operation you are wishing to script into a step-by-step series of commands like you would give someone over the phone. In some cases, this can even be done away from the computer by simply thinking through the requisite steps. In general, the workflow for a script will be identical to that in Pointwise.

For the projection example, the workflow would be as follows:

  1. Select the connector to project.
  2. Save the connector’s initial distribution function and spacing constraints to be reapplied after the projection.
  3. Project the connector onto the database.
  4. Reset the distribution of the projected connector back to its original functional distribution as opposed to the general distribution resulting from the projection.
  5. Re-apply the initial spacing constraints.

3. Identify commands to use in script
Pointwise makes it easy to find the Glyph syntax for features in the program. Without a doubt, this step can be the biggest impediment for those new to scripting. Finding the command and syntax for most scripting languages can be a cumbersome combination of web searches and frequent visits to the manual pages. That said, if you have a general idea of the command you need, the traditional manual pages located online can be a quick way to find it.

manual pages
Figure 2. The manual pages for Glyph can be found online at www.pointwise.com/glyph2.

For instance, if we wish to select a connector to project, we can search for “select” under the About, Functions link (under the letter “S”) and find the selectEntities command. You would notice that this command requires a selection mask as an argument. If you scroll down, you see that the next command is createSelectionMask, which explains how to set up a mask for use in the selectEntities command.

Glyph is well documented, and each command can be found in the manual pages. The names of the commands are quite intuitive. For instance, if you wish to get the distribution from a connector, you can look in the manual pages under Grid Entity Types---Connector, getDistribution (or About, Functions, “G”, getDistribution), which returns the distribution for the specified connector. The beginning and end spacings can then be found with getBeginSpacing and getEndSpacing, respectively. Also, if you would like a general overview of Glyph scripting, the Glyph Reference manual is included with every Pointwise installation and can be accessed directly through the Help menu.

If you have difficulty locating the command in the references, Pointwise actually interfaces with its meshing kernel through Glyph, which means that every operation in the GUI that changes the mesh or database creates a snippet of Glyph script. If you know how to perform an action interactively in Pointwise, there are two additional ways to extract the relevant code:

  • Journaling
  • Echoing
Journaling records every Glyph command sent to the kernel in a file and can be used to re-play the exact commands called through the GUI. To record a journal, simply select Script, Begin Journaling. After you have completed the action, select Script, End Journaling. While a journal is technically a script and can theoretically be adapted to handle tedious tasks, it is not recommended to use a journal file in such a manner. There are a number of reasons for this. First, journals are automatically generated by the software and contain temporary variables and intermediate steps not necessary for a script, which makes them somewhat difficult to decipher when a user wants to dissect the script and modify its behavior. Additionally, the commands and/or syntax in Glyph can change periodically, which can make journals fragile. That being said, a journal can be a great reference for identifying Glyph commands.

When writing scripts for use in Pointwise, it is suggested that you begin with a clean sheet as opposed to a journal. To find the commands without writing a complete journal file, you can activate echoing. Echoing is identical to journaling, but displays the Glyph commands to the Message window within Pointwise as they are called instead of writing them to a file. This option can be changed by right-clicking in the Message window, selecting Preferences, and activating “Journal”.

message window
Figure 3. Echoing in the message window can be enabled by right-clicking and checking the Journal option.

Echoing is a great way to quickly identify the Glyph syntax for an operation you wish to perform. For example, by selecting a connector and performing a closest point projection including all points, the message window will read:

commands
Figure 4. Commands useful for scripting can be extracted from either journal files or echoed commands in the Messages window.

Looking beyond the cryptic variable names that begin with an underscore (which will also clutter the journal file), the critical line is highlighted in Figure 4, which shows the command to project the connector.

4. Test, Test, Test
No one expects a script to work perfectly the first time it is executed, particularly if it is reasonably sophisticated. However, you can do yourself a huge favor and test the script as it is being written. One useful tip: Use the puts Tcl command (analogous to echo in bash or print in Python) to print out the values of variables and commands as you go along to make sure that the code is doing what you expect.

For instance, in our projection example, suppose you have found the commands to select a connector and you would like to test the syntax for getting the beginning and end spacing values. Your code would resemble:

## Header, required to load Glyph commands
package require PWI_Glyph 2

## Create selection mask to select only connectors
set mask [pw::Display createSelectionMask -requireConnector {}]

## Prompt user to select the connector to project
set pickedCon [pw::Display selectEntities -description "Select connector" -selectionmask $mask curSelection]

## Set variable $con to the current selection of Connectors
## Note: No error handling, code will break if multiple connectors are selected
set con $curSelection(Connectors)

You would then add the line,

set conDistribution [$con getDistribution 1]

which you expect to return the distribution of the connector. If you then use the puts command, you can see that conDistribution contains the distribution attributes, but additional commands are required to extract the end spacing values. That is, if you add the line

puts $conDistribution

The code will output pw::DistributionTanh_1. Therefore, you know that to get a numerical value for the spacings, you need to add additional code, namely

set beginValue [$spaceBegin getValue]
set endValue [$spaceEnd getValue]

which will return the numerical value you need. Again, this can be tested by using puts to out the values stored in $beginValue and $endValue.

Once that works, you can add the project command to the script to see if the projection is successful. Next, use the replaceDistribution command (found via echoing) to reset the distribution, and finally reset the beginning and end spacing constraints.

By testing the script piece by piece, you reduce the complexity of the code being tested and build upon a known working state, making it easier to diagnose mistakes. Eventually, the code can become quite sophisticated, but by approaching it in simple segments, you can avoid spending your time debugging a hastily written, complex code.

5. Tweak
A script does not have to be a static thing. As you use the script in your work, you may wish to add some error-handling or additional functionality. For instance, perhaps you wish to project multiple connectors together, in which case you can wrap your existing code in a for-loop.

If you would like to eliminate hard-coded parameters used in the script, you could also incorporate a GUI. Creating a GUI for a Glyph script is done through the Tk package, which can prompt the user for inputs to be used within the script.

As an example, suppose you wanted to extend the functionality of the projection script to be able to arbitrarily specify the dimension of the projected connector. A GUI could be created that would have a text box for entering the new dimension each time the script is executed, rather than specifying a fixed value in the script itself. This would allow you to very quickly re-dimension each connector as it was being projected, without having to edit the script between each execution.

distribution of points
Figure 5. By writing a short script, the distribution of points before and after projection can be preserved.

Additional Information
If you would like more instruction on how to create scripts in Pointwise, we offer a training course that focuses exclusively on Glyph. The course begins with an introduction to Tcl, and then goes through many commonly used commands. Additionally, Tutorial 12 found in the Tutorial Workbook (accessible from the Pointwise Help menu) steps through a moderately complex script for creating an entire mesh with a Glyph script. The Glyph Reference Manual, also available through the Pointwise Help menu, provides a good overview of Glyph scripting.

You can also download and use scripts from the Pointwise GitHub repository. These scripts are useful in their own right, but they are also handy to download and study to get ideas for use in developing your own scripts.

Scripting provides you with a powerful tool to assist your mesh generation needs. With a small time investment up front, scripts can be developed that significantly reduce the time required to carry out the tedious, redundant tasks you may encounter or even automate the entire meshing process for an optimization routine. By following the five steps loosely outlined above, you can start realizing the benefits with your own scripts.

If you are interested in trying the scripting capabilities of Pointwise for yourself, just click on the button below to request a free evaluation.

[ Post Comment ]

Cradle CFD Software Online Training Webinar | Casting and Manufacturing Simulation of High Fidelity Parts  >

 

 
CFD Review Login
User name:

Password:

Create an Account

Related Links
  • The Connector article
  • AirfoilMesh script
  • Glyph Script Exchange
  • Pointwise
  • Pointwise, Inc.
  • webinar recording
  • www.tcl.tk
  • More on Grid Generation
  • 'Automating CFD Meshing Via Scripting' | Login/Create an Account | Search Discussion

    The following comments are owned by whoever posted them.
    We are not responsible for them in any way.

    Seattle is so wet that people protect their property with watch-ducks. All content except comments
    ©2014, Viable Computing.

    [ home | submit story | search | polls | faq | preferences | privacy | terms of service | rss  ]