HM-8060: Calculate the Resultant Sum of Forces

In this tutorial, you will calculate the resultant sum of forces.

You will create a Tcl script that:
  • Determines the components of a given set of force vectors
  • Calculates a vector resultant sum of the forces
  • Reports the resultant sum

The Tcl commands if, foreach and expr will be used to add logic and mathematical functions to the script. The command hm_getentityvalue is used to extract information from HyperMesh entities, based on data names.

Data names are generic references to the information that physically define an entity in the HyperMesh environment. An example of this is the x, y, and z coordinates that define a node location in three-dimensional space.

Data names are accessed using the hm_getentityvalue command. This command uses the data names available for an entity to return the particular value of interest. The command will return a value that is either a string or a numeric value, depending on the command syntax and the value stored in that particular data name field. The basic syntax of the command is:
hm_getentityvalue entity_type id data_name flag

where entity_type is the requested entity type (elements, loads, nodes, etc.), id is the entity ID, the data_name is the data field name of interest, and flag is either 0 or 1 depending on whether the command should return a numeric value (0) or a string (1).

To retrieve the x-component of a force with ID 12, the following command can be used:
set force_x [hm_getentityvalue loads 12 "comp1" 0]
Note: To assign the value from the command to a variable, the command is placed within square brackets.

Define the Process

In this step, you will define the process.

The script should automate the following processes:
  • Prompt you to select a number of forces to calculate the resultant
  • Make sure you have selected one or more loads
  • Extract the components of each force
  • Sum the components
  • Report the result to you

Determine Data Names

The following table lists several relevant data names for force loads:

Table 1.
Item Description
comp1 x component of the vector
comp2 y component of the vector
comp3 z component of the vector
config The number "1" for forces

The number "2" for moments

The number "8" for velocities

The number "9" for accelerations

entitytype The type of entity to which this load is applied (1=node, 3=component, 10=set)
node When a load is applied to a node, this serves as a pointer to the node
inputsystemid Reference system ID

Create a New File

In this step you will create a new text file.

  1. Open a text file in any text editor.
  2. Save the file as HM8060.tcl.

Select and Add Loads to a List

In this step, you will select the desired loads and then add those loads to a list.

The *createmarkpanel command is used to allow you to graphically select the loads from the HyperMesh interface and add them to the mark. The command below adds the loads to mark 1. Once the loads have been added to mark 1, the load ids are assigned to a list called loads_list, using the Tcl command set.

Add the following lines to the file, HM8060.tcl:
*createmarkpanel loads 1 "Select forces to compute resultant";
set loads_list [hm_getmark loads 1];

Initialize Variables

In this step, you will initialize the variables for the x, y, and z components.

Use the Tcl command set to initialize the variables for the x, y, and z components of force to 0. These variables will be used later in the script to compute the resultant force for each component.

Add the following lines to the Tcl file, HM8060.tcl:
set x_comp_sum "0";
set y_comp_sum "0";
set z_comp_sum "0";

Begin an If Loop

In this step you will begin an if loop which checks to see if the variable, loads_list, has values. If it does, then proceed with the macro.

Before calculating the resultant of the forces selected, we should check to make sure that the variable loads_list has values in it. This is done by using an if loop. In the if loop below, we are checking that the variable loads_list is not empty.

Add the following line to the Tcl to initialize the if loop:
if {$loads_list != ""} {

Use a Foreach Loop

In this step you will use a foreach loop to iterate through each load in the list, loads_list, and extract the x, y, and z components using the hm_getentityvalue command and the appropriate data name.

Using a foreach loop, each load in the list loads_list will be iterated through. Within the foreach loop, each load is referenced by load_id and then the component value is added to the previous loads component’s value. For example, let’s look at the x component. Using the set command, the variable x_comp_sum is defined as the previous value of x_comp_sum, plus the x component of the current load. The x component of the current load is retrieved by using the hm_getentityvalue command and the data name comp1 (all the available data names for loads are shown in the table above). This process is done for the y and z components as well.

Add the following lines to the Tcl file:
foreach load_id $loads_list {
        set x_comp_sum [expr $x_comp_sum + [hm_getentityvalue 
loads $load_id "comp1" 0]];
        set y_comp_sum [expr $y_comp_sum + [hm_getentityvalue 
loads $load_id "comp2" 0]];
        set z_comp_sum [expr $z_comp_sum + [hm_getentityvalue 
loads $load_id "comp3" 0]];

Report the Resultant Force

In this step, you will add code that reports the resultant force to you.

Use the command hm_usermessage to report each of the components of the resultant force.

Add the following line of code to the Tcl file:
hm_usermessage "Resultant force: $x_comp_sum, $y_comp_sum 
$z_comp_sum ";

Complete the If Loop

In this step, you will complete the if loop and report an error message if no loads are found.

To complete the if loop, add an else statement. Remember the if statement checked to see if the variable loads_list was not empty. This else statement returns an error message to you to let you know that no loads were selected.

Add the following code to the Tcl file:
} else {
    hm_errormessage "No loads selected";

Test the Script

In this step, you will test the script.

  1. From the menu bar, select View > Command Window to display the command window at the bottom of the screen.
  2. Click and drag to open the command window from the bottom edge of the screen.
  3. Open the file
  4. Use the source command to execute the script. For example: HM8060.tcl.
    It is often necessary to debug Tcl scripts using the command window. This allows you to run the Tcl script and easily review error messages, as well as print out debug information. Additional details can be found in the Creating Tcl Scripts and Running Tcl Scripts sections.
    There are several important assumptions used when creating this script:
    • You will always select force loads, as opposed to moments, pressures, and so on.
    • The forces are applied to nodes, as opposed to comps or sets, and are valid to sum.
    • All of the forces are applied in the same coordinate system so that it is valid to sum the component values directly.

    If any of these assumptions are not true, the values returned by the script may be invalid. Additional conditional logic can be programmed to check for each of these situations and an error message can be returned or they can be handled appropriately.

  5. The result of the macro is shown in the status bar. Either a message with the resultant force is shown or else there is a note saying that no loads were selected.