Five Steps to Automate Meshing via Scripting
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. 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.
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.
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.
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.
Although it can be 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:
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 (see Figure 6). 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:
3. Identify Commands To Use in Script
Once you have identified the steps needed to perform the task, 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.
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
link (under the letter S) and
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,
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 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 Messages window within Pointwise as they are called instead of writing them to a file. This option can be changed by right-clicking in the Messages window, selecting Preferences, and activating Journal.
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:
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
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:
You would then add the line,
which you expect to return the distribution of the connector. If you
then use the
The code will output
which will return the numerical values you need. Again, this can be
tested by using puts to out the values stored in
Once that works, you can add the project command to the script to see if
the projection is successful. Next, use the
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.
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.
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 Glyph Script Exchange. 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.
The complete Glyph script is shown in Figure 5 and can be downloaded here: projectDistributionSimple.glf.
Figure 6: By writing a short script, the distribution of points before and after projection can be preserved.