Chapter 6
Interfacing with HyperMesh Solver Templates
Solver specific data created within HyperMesh can be defined, reviewed, and edited in the card images. Card images are an interpretation of the loaded solver template. Each piece of data that defines a card image has a text string (data name) and a numeric attribute ID.
An example is the Young’s Modulus for a material. Each solver, and even each different material type for a particular solver, can have a Young’s Modulus attribute and a corresponding data name. These data names and attributes are defined in each solver’s feoutput template. They are unique and are not
necessarily consistent between solvers or even between each material within a solver.
Templates exist for each solver supported by HyperMesh and are located in subfolders under the <altair_home>/templates/feoutput directory.
These templates define every solver specific attribute including data names, attribute IDs, card image formats, and the format of the data upon export. The
*defineattribute command is used to define attribute data names and IDs in a template file.
There are several different types of attributes including array, double, entity, integer and string. Each of these types is handled in a similar fashion to the examples below. The difference is that each entity type uses an appropriate
*attributeupdate command based on its type. There are also a number of hm_attribute commands to query entity attributes.
Chapter 6: Interfacing with HyperMesh Solver Templates
Solver Attributes and Card Images
A sample card image for an OptiStruct MAT1 material collector is shown below:
Figure 6.1: Card image for MAT1 card in OptiStruct.
For each field in the above card image there is, a corresponding attribute and data name in one the optistruct templates (in
<altair_home>/templates/feoutput/common_nas_os/attribs) . An excerpt is shown below for several of the card image fields:
*defineattribute(E,1,real,none)
*defineattribute(G,2,real,none)
*defineattribute(Nu,3,real,none)
*defineattribute(Rho,4,real,none)
The first argument in the *defineattribute command is the solver data name. The second is the attribute ID and the third is the attribute type. There are 11 different attribute types. Attributes of different types are created and updated with different commands, but the overall concept is the same.
Each attribute in the template has a definition similar to these. Additionally, there are card image definitions that define how the card editor is generated inside of HyperMesh for each attribute. For optistruct, these card images are defined in
<altair_home>/templates/feoutput/optistruct. An excerpt of the optistruct MAT1 card image is shown below:
*menustring("MAT1 ")
*menufield(ID,integer,id,8) *menufield(E,real,$E,8)
*menudefaultvalue(" ") *menuinitialvalue(210000.0) *menurestrictedvalue(>,0.0)
*menufield(G,real,$G,8)
*menudefaultvalue(" ") *menuinitialvalue(80769.2) *menurestrictedvalue(>,0.0)
Chapter 6: Interfacing with HyperMesh Solver Templates
Notice that the *menufield command is used above. This command
determines how a field is created in the card editor for a particular attribute. The first argument is the text string to show above the data field and the third
argument is the particular data name that is associated with that field. For
example, the attribute and card image definitions for the Young’s Modulus for the MAT1 material are:
*defineattribute(E,1,real,none)
*menufield(E,real,$E,8)
Knowing all of this information allows us to assign, update and query the data.
Querying Solver Attributes
In addition to the standard HyperMesh data names, data names also refer to solver specific attributes as described above. Solver data names are accessed similarly to HyperMesh data names, using the hm_getentityvalue,
hm_attributearrayvalue and hm_attributearray2dvalue commands.
These commands use a data_name_string to access a particular piece of data. As noted above, each attribute has both an ID and a data name. For example, from the MAT1 material card we have several attributes of interest:
Again, the first argument is the solver data name and the second is the attribute ID. So in order to extract the value of E from a material with ID 11:
hm_getentityvalue mats 11 "\$E" 0
There are two things to notice. The first is that the data name is accessed using a leading $, which must be explicitly escaped with the forward slash \. If this is not done, Tcl will attempt to evaluate $E as a variable. The \ indicates that the
Chapter 6: Interfacing with HyperMesh Solver Templates
variable should not be evaluated but instead passed as $E. The second is that the flag argument is set to 0.
Example 6.1: Automate Extracting Data in a Card Image
The purpose of this exercise is to become familiar with creating HyperMesh Tcl scripts which automate extracting data in HyperMesh card images.
The following HyperMesh Tcl commands are used in this exercise:
• hm_getentityvalue
·The following core Tcl/Tk commands are used in this exercise:
• set
• return
• proc
• tk_messageBox
Step 1: Define the task to be automated.
1. An OptiStruct MAT1 material of id 1 exists in the HyperMesh database.
Extract from its card image its values for the parameters Young’s Modulus (E), Shear Modulus (G), Poisson’s ratio (Nu), and density (Rho).
2. Display the values for the parameters.
Step 2: In the optistruct template file, identify the attribute names for the parameters Young’s Modulus, Shear Modulus, Poison’s ratio, and density.
1. In the HyperWorks installation, open the file
<altair_home>\templates\feoutput\optistruct\optistruct.
This file contains the card image definitions for the optistruct template.
2. Notice that on line 22 another template file is included. Open the file
<altair_home>\templates\feoutput\common_nas_os\attribs.
This file contains the *defineattribute commands for the optistruct template.
3. As shown in the above section on Solver Attributes and Card Images, in the MAT1 card image, the field names for Young’s Modulus, Shear
Modulus, Poisson’s ratio, and density are E, G, Nu, and Rho, respectively.
Identify the attribute lines for these field names in the optistruct attribs file.
*defineattribute(E,1,real,none)
*defineattribute(G,2,real,none)
*defineattribute(Nu,3,real,none)
*defineattribute(Rho,4,real,none)
Chapter 6: Interfacing with HyperMesh Solver Templates
Step 3: Create a text file named extract_mat.tcl for the script to be created.
Step 4: Retrieve the parameter values from the MAT1 card image and store them in variables.
Now that the *defineattribute lines has been found, the field names for Young’s Modulus, Shear Modulus, Poisson’s ratio, and density have been determined. Now using those field names and the hm_getentityvalue command, the parameter values can be stored in variables.
set mat_id 1
set mat_E [hm_getentityvalue materials $mat_id “\$E” 0]
set mat_G [hm_getentityvalue materials $mat_id “\$G” 0]
set mat_NU [hm_getentityvalue materials $mat_id “\$Nu” 0]
set mat_RHO [hm_getentityvalue materials $mat_id “\$Rho” 0]
Again, notice how \ is used to indicate that the variable should not be evaluated but instead passed as $E. Also notice that the flag argument is set to 0 so that a floating point value is returned.
Step 5: Return all parameter values as a string.
After the 4 lines created in step 4, add an additional line which prints the parameter values to the Command Window.
return [ list $mat_E $mat_G $mat_NU $mat_RHO ]
Note: The Tcl return command returns a single value. One way of returning multiple values is to return a string containing all the values to be returned.
This is done using brackets [].
Step 6: Wrap the code into a Tcl procedure and pass the material id as an argument to it.
Create a procedure called extract_mat which passes a variable called mat_id. Place the code written in steps 4 and 5 within the procedure.
proc extract_mat {mat_id} { [code]
}
Chapter 6: Interfacing with HyperMesh Solver Templates
Step 7: Evaluate the procedure.
To call the procedure, at the end of the file place the procedure name followed by the material id to be processed (in this case, the material id to be evaluated is 1).
extract_mat 1
Step 8: Save the extract_mat.tcl file.
Step 9: Test the script by running it from the Command Window.
1. Open HyperMesh from your HyperMesh working folder.
2. Load the OptiStruct User Profile when the User Profile GUI appears.
3. Retrieve the file c_channel-tcl_vector.hm.
4. From the Command Window, run the following command:
source extract_mat.tcl Values for E, G, NU, and RHO are returned.
5. Verify that the returned values match the values on the MAT1 card image.
Step 10: Create a button on the Utility menu’s User page.
1. In your HyperMesh working folder, open the file userpage.mac.
If this file does not already exist, create a text file named userpage.mac.
2. On the next blank line from the top of the file, type the following
*createbutton() line:
*createbutton(5,”Extract mat data”,-1,0,10,GREEN,
”Extract material data”,”EvalTcl”,”extract_mat.tcl”) 3. Save the userpage.mac file to your HyperMesh working folder.
Step 11: Test the script from its button in the macro menu.
Although the parameter values are returned, you cannot see them. This is because there is no place (such as the Command Window) to display them.
Modify the script in the next step to rectify this when running the script from the Utility menu.
Step 12: Display the result of the extract procedure in a tk_messageBox command.
Change the script line:
extract_mat 1
Chapter 6: Interfacing with HyperMesh Solver Templates
to
tk_messageBox -message [ extract_mat 1 ] -title \
“Altair HyperMesh, Extract mat data”
Using tk_messageBox –message command a small GUI will pop up which returns the parameter values.
Step 13: Test the script again from its button in the Utility menu.
The script should return the values for E, G, NU, and RHO in a pop-up window titled “Altair HyperMesh, Extract mat data”.
Step 14: The exercise is complete. Close the HyperMesh session.
Assigning Data to Solver Attributes
It is easiest to understand how to assign data to solver attributes with a specific example. When the MAT1 material collector shown above is initially created, the relevant commands to assign values to its attributes are written to the
command.cmf file. Note that the values below correspond to those shown in Figure 6.1 above:
Each of these commands sets one of the MAT1 attributes either to its default value or to the user defined value. The third argument in the
Chapter 6: Interfacing with HyperMesh Solver Templates
*attributeupdateint and *attributeupdatedouble commands is the attribute ID. Looking at one of the commands above we have:
*attributeupdatedouble(materials,1,1,1,1,0,210000) Where:
materials is the type of entity that owns the attribute 1 is the ID of the entity
1 is the identifier of the attribute 1 is the solver number of the attribute 1 is the status of the attribute
0 is the behavior of the attribute 210000 is the value of the attribute
Looking at the optistruct attribs template, attribute 1 corresponds to the data name E, or Young’s Modulus.
Once these commands have been generated, they can be used in Tcl by
extracting them from the command.cmf file and updating the syntax by replacing all () and , with spaces:
*collectorcreate materials "steel" "" 11;
*createmark materials 2 "steel";
*dictionaryload materials 2 \
"C:/Altair/hw10.0/templates/feoutput/optistruct/optistruct" \
"MAT1";
*attributeupdateint materials 1 3240 1 2 0 1;
*attributeupdatedouble materials 1 1 1 1 0 210000;
*attributeupdatedouble materials 1 2 1 0 0 0;
*attributeupdatedouble materials 1 3 1 1 0 0.3;
*attributeupdatedouble materials 1 4 1 1 0 7.85e-009;
*attributeupdatedouble materials 1 5 1 0 0 0;
*attributeupdatedouble materials 1 6 1 0 0 0;
*attributeupdatedouble materials 1 7 1 0 0 0;
*attributeupdatedouble materials 1 341 1 0 0 0;
*attributeupdatedouble materials 1 343 1 0 0 0;
*attributeupdatedouble materials 1 345 1 0 0 0;
*attributeupdateint materials 1 5237 1 2 0 0;
These commands can now be run in Tcl to duplicate the creation of the MAT1 material. However, simply running these commands as they are is not very flexible. The material ID, name and values are all hard coded. The template file location in the *dictionaryload command is also hard coded. Also, there are a lot of extra commands that set attributes that aren’t necessary. These will all be updated or removed to make the script more flexible.
Chapter 6: Interfacing with HyperMesh Solver Templates
Creating a Reusable and Modular Procedure
In order to make this more flexible, we can create a Tcl procedure that allows us to substitute variables for the name, color and data values. We can make the template file path modular across different platforms. Also we can make the material ID a variable that references the newly created material ID, so that the
*attributeupdatedouble and *attributeupdateint commands work properly.
proc mat_create { name color E Nu Rho } {
if {[hm_entityinfo exist mats $name -byname] == 1} { hm_errormessage "Material $name already exists"
return;
} else {
*collectorcreate materials "$name" "" $color;
*createmark materials 2 "$name";
*attributeupdatedouble materials $mat_id 3 1 1 0 $Nu;
*attributeupdatedouble materials $mat_id 4 1 1 0 $Rho;
return;
} }
The advantage of doing this is that mat_create is now modular. Notice the following:
1. The developer only needs to pass the material name, color and the values for the material properties and the procedure will create the material accordingly.
2. The variables are then used in the later commands to pass the appropriate arguments.
3. This procedure also checks to see if a material with that name already exists, and if it does, a message is returned in the HyperMesh message bar.
4. The hm_info command is used to find the appropriate template to load.
Example 6.2: Automate Defining a Card Image
with Data
Chapter 6: Interfacing with HyperMesh Solver Templates
The purpose of this example is to become familiar with creating HyperMesh Tcl scripts which automate defining solver data in HyperMesh card images. This example will help you become familiar with defining and calling Tcl procedures.
The following HyperMesh commands are added and modified in this exercise:
• *dictionaryload()
• *attributeupdateint()
• *attributeupdatedouble()
The following HyperMesh Tcl commands are used in this exercise:
• hm_mark
• hm_info
·The following core Tcl commands are used in this exercise:
• set
• proc
Step 1: Define the task to be automated.
1. Create an OptiStruct MAT1 material with the following parameter values.
Assume the OptiStruct template is already loaded.
• Young’s Modulus (E)
• Shear Modulus (G)
• Poisson’s ratio (Nu)
• density (Rho)
2. Upon automating the task, wrap the code into a Tcl procedure.
Note: The advantage of creating a procedure is that it is accessible from any part of the script. The developer only needs to pass the material name and its
parameter values a material will be created with them.
Steps 2-4: Do the task in HyperMesh to capture its commands to the command.cmf file.
Step 2: Delete the command.cmf file from your HyperMesh working folder.
Every command executed in HyperMesh is recorded in the command.cmf file.
To view only the task’s commands, it is suggested you delete the command.cmf file before doing the task. After deleting this file, the file automatically reappears when more commands are executed in HyperMesh.
Chapter 6: Interfacing with HyperMesh Solver Templates
Step 3: Open HyperMesh and load the OptiStruct User Profile.
1. Open HyperMesh from your HyperMesh working folder (the folder from where HyperMesh is invoked).
2. When HyperMesh is launched it will ask which User Profile to load.
3. Select OptiStruct.
4. Click OK.
Step 4: Create an OptiStruct MAT1 material.
1. From the pull down menus select Materials -> Create.
2. For name =, type steel.
3. For card image =, select MAT1.
4. Create/edit the material. A card image for MAT1 appears.
5. Click [E] and type in its field 123.
6. Click [G] and type in its field 45.
7. Click [NU] and type in its field 0.078.
8. Click [RHO] and type in its field 90.
9. Return to the main menu.
10. Keep this HyperMesh session open for steps later in this exercise.
Step 5: Copy the task’s commands in the command.cmf file and paste them to the create_mat.tcl file.
1. Open a text editor and create a new text file named create_mat.tcl. Save the file to your HyperMesh working folder.
2. From your HyperMesh working folder, open the command.cmf file.
3. Locate the command line *collectorcreate() at or near the end of the file. You should see lines similar to the following.
*collectorcreate(materials,"steel","",11)
Chapter 6: Interfacing with HyperMesh Solver Templates
4. Copy and paste the lines into the create_mat.tcl file. Looking at the commands above, the first ten *attributeupdatedouble commands and first four *attributeupdateint commands are setting the default values for the parameters in HyperMesh. The last four
*attributeupdatedouble commands are setting the values that were entered in HyperMesh in Step 4.
5. Notice that each *attributeupdatedouble command has an attribute value. You can match the first four *attributeupdatedouble
commands to the four parameter values you entered in the card image.
6. Since it is desired to create a script that only specifies the values for the parameters E, G, NU, and RHO, delete all the
*attributeupdatedouble() lines except for the 4 that define these variables. Also delete the four *attributeupdateint() lines. Your script should now have eight lines.
Steps 6-12: Modify and add to the commands as necessary.
Step 6: Modify the code so it is in Tcl syntax.
Replace the parentheses and commas with a blank space.
Step 7: Substitute the attribute values with variables E, G, NU, and RHO.
*attributeupdatedouble materials 1 1 1 1 0 $E
*attributeupdatedouble materials 1 2 1 1 0 $G
*attributeupdatedouble materials 1 3 1 1 0 $NU
*attributeupdatedouble materials 1 4 1 1 0 $RHO
Step 8: Substitute all instances of the hard coded material name steel with the variable mat_name.
*collectorcreate materials "$mat_name" "" 7
*createmark materials 2 "$mat_name"
Chapter 6: Interfacing with HyperMesh Solver Templates
Step 9: Once the material is created, retrieve its id from the mark and store it in a variable.
Add the following command just below the *createmark line.
set mat_id [hm_getmark materials 2]
Step 10: Substitute all instances of the hard coded material id with the variable $mat_id.
*attributeupdateint materials $mat_id 3240 1 2 0 1
*attributeupdatedouble materials $mat_id 1 1 1 0 123
*attributeupdatedouble materials $mat_id 2 1 1 0 45
*attributeupdatedouble materials $mat_id 3 1 1 0 0.078
*attributeupdatedouble materials $mat_id 4 1 1 0 90
Step 11: Substitute the hard coded path to the OptiStruct feoutput template with a request for the global template.
*dictionaryload materials 2 "[hm_info templatefilename]"
"MAT1"
This line now assumes the OptiStruct feoutput template has been loaded prior to running the script. If it is not loaded, a “0” is returned.
Step 12: Review your script to see if it looks similar to the following.
*collectorcreate materials "$mat_name" "" 7
*createmark materials 2 "$mat_name"
set mat_id [hm_getmark materials 2]
*dictionaryload materials 2 "[hm_info templatefilename]"
"MAT1"
*attributeupdateint materials $mat_id 3240 1 2 0 1
*attributeupdatedouble materials $mat_id 1 1 1 0 $E
*attributeupdatedouble materials $mat_id 2 1 1 0 $G
*attributeupdatedouble materials $mat_id 3 1 1 0 $NU
*attributeupdatedouble materials $mat_id 4 1 1 0 $RHO
Step 13: Wrap the code into a Tcl procedure and pass the variables mat_name, E, G, NU and RHO as arguments to it.
proc mat_create {mat_name E G NU RHO} { [code]
}
Chapter 6: Interfacing with HyperMesh Solver Templates
Step 14: Evaluate the procedure and save the create_mat.tcl file.
mat_create “aluminum” “10” “20” “0.3” “40”
Step 15: Test the script by running it from the Command Window.
1. Go to the File menu and select New > Model to clear the current session.
2. In the Command Window, type the following and then press Enter.
source create_mat.tcl The aluminum material is created.
Step 16: Create a button on the macro menu’s User page.
1. In your HyperMesh working folder, open the file userpage.mac.
If this file does not already exist, create a text file named userpage.mac.
2. On the next blank line from the top of the file, type the following
*createbutton()line:
3. *createbutton(5,”Create OS MAT1”,-1,0,10,GREEN,”Create OS MAT1 material”,”EvalTcl”,”create_mat.tcl”)
4. Save the userpage.mac file to your HyperMesh working folder.
Step 17: Test the script by running it from its button in the Utility menu.
Step 18: The exercise is complete. Close the HyperMesh session.
Updating Solver Attributes
In addition to creating a card image from scratch, it is also possible to update an existing card image. A procedure to do this is almost identical to the procedure to create a new card image, with the exception that the material is not created and thus the procedure requires a material ID to be passed instead of a material name.
proc mat_update { mat_id E Nu Rho } {
if {[hm_entityinfo exist mats $mat_id -byid] == 0} {
if {[hm_entityinfo exist mats $mat_id -byid] == 0} {