3. Output Functions

## Overview

This chapter covers MechanicalSystems' numerical and graphical output functions. In general, Mech's output functions are used to convert the symbolic coordinates that define the location and orientation of each body into more specialized quantities, such as the distance between two points or the angle between two lines. Graphical output functions use the symbolic coordinates to locate graphics primitives to reflect the location of the bodies in the model. Throughout this chapter, Mech output functions are used as supplements to, not replacements for, built-in Mathematica functions. Most output from Mech is generated through a mixture of both.

# 3.1 2D Output Functions

Numerical output from a Modeler2D model can be generated with output functions that are defined in the auxiliary package Graphics2D. This package is autoloaded after the Modeler2D main package has been loaded if any of the output functions are called. Each output function returns a symbolic expression that represents the quantity being sought, in terms of the model's dependent variables, X2, Y3, and so on. To achieve a numerical result, the dependent variables must be replaced with numbers, usually by applying a solution rule returned by SolveMech. More complicated and specific output functions can easily be defined by the user, making use of Mech's basic output functions and Mathematica's built-in vector algebra functions.

## 3.1.1 Example Mechanism

To demonstrate the use of Modeler2D output functions, a 2D model is developed that can have various geometric quantities measured. The model is a planar quick-return mechanism consisting of three moving bodies: the crank, drive arm, and slider. A real quick-return mechanism would have a fourth body, the connecting rod between the drive arm and the slider, but this body is simply modeled with a relative-distance constraint.
The motion of the quick-return mechanism is driven by the rotation of the crank that, in turn, forces the drive arm to rock about its lower pivot point on the ground, via a pin on the crank that slides in a straight slot on the drive arm. The top of the drive arm is attached to a connecting rod, which is attached to the slider, which slides on horizontal path on the ground.

### Bodies

Four body objects are defined for the quick-return mechanism model.

• The ground (body 1) requires four point definitions.
P1 is the rotational axis of the drive arm.
P2 is the rotational axis of the crank.
P3 and P4 are two points that define the line upon which the slider slides.
• The crank (body 2) requires two local point definitions.
P1 is the crank's rotational axis.
P2 is the location of the pin on the crank that engages with the drive arm.
• The drive arm (body 3) requires two local point definitions.
P1 is the point on the drive arm where the drive arm pivots with respect to the ground.
P2 is the point where the drive arm is attached to the pseudo-connecting rod.
• The slider (body 4) requires two local point definitions.
P1 and P2 are two points along the horizontal axis of the slider define its translation line.
• ### Constraints

Five constraints and one driving constraint are required to model the quick-return mechanism.

• A Revolute2 constraint forces the axis of the crank to be coincident with its pivot point on the ground. This constrains two degrees of freedom, X and Y displacement of the crank axis.
• A RotationLock1 constraint specifies the angular coordinate of the crank. This driving constraint is used to rotate the crank.
• A Revolute2 constraint forces the lower pivot point of the drive arm to be coincident with the global origin. The drive arm can pivot about this point on the ground.
• A PointOnLine1 constraint between the crank and the drive arm where the pin on the crank slides in a slot on the drive arm.
• A RelativeDistance1 constraint between the drive arm and the slider replaces the pseudo-connecting rod, which is 5.0 units long.
• A Translate2 constraint between the slider and the ground allows the slider to translate horizontally along a track on the ground.

## 3.1.2 Output Functions

Output functions.

More complex output functions.

The Location function is used to convert a Mech Point object into the global coordinates of the point.

 In[20]:=
 Out[20]=

The Distance function is used to find the absolute distance between any two points in space. The points can be on one body or on two different bodies.

Note that it was not necessary to apply a solution rule to the last example to get a numerical result because the dimension being measured is not a function of the coordinates of each body.

Note that the distance is zero when the crank has turned 1/4 turn, and the drive arm is pointed straight up, through the axis of the crank.

The Rotation function simply returns the angular coordinate of the specified body.

The ProjectOnLine function is used to find the projection of a point on one body onto a line located on another body (or bodies).

## 3.1.3 Vector Algebra Functions

Modeler2D vector algebra functions perform many common vector algebra operations such as cross and dot products of vectors. These functions can be used to build more complicated functions that are not provided by Modeler2D. The output functions take point, vector, and axis objects as arguments, identical in structure to the geometry objects used when building constraint objects. When local point numbers are used, the local coordinates are taken from the current body objects, as specified with SetBodies.
Each of the vector algebra functions are demonstrated in the context of the quick-return mechanism model defined at the beginning of this section.

Vector algebra functions.

The Direction function is used to convert Modeler2D vector objects into 2D vectors in global coordinates. Note that Mech's definition of Direction does not interfere with its use as a built-in Mathematica symbol.

Several Modeler2D output functions are textbook vector algebra functions that are capable of dealing with normal 2D vectors or Modeler2D line objects interchangeably.

The global coordinates of a point specified in a local reference frame are equal to the coordinates of the origin of the reference frame plus the local coordinates of the point, rotated into the global reference frame.

Two constant matrices that are commonly used in 2D vector algebra are also defined by Modeler2D.

Constant utilities.

### Coordinate Conversions

PolarToXY and XYToPolar are two conversion functions that do not accept Modeler2D geometry objects as arguments. These functions only accept 2D vectors of the form {x, y}.

Coordinate conversions.

# 3.2 3D Output Functions

Numerical output from a Modeler3D model can be generated with output functions that are defined in the auxiliary package Graphics3D. This package is autoloaded after the Modeler3D main package has been loaded if any of the output functions are called. Each output function returns a symbolic expression that represents the quantity being sought, in terms of the model's dependent variables X2, Z3, Ei4, and so on. To achieve a numerical result, the dependent variables must be replaced with numbers, typically by applying a solution rule returned by SolveMech. More complicated and specific output functions can easily be defined by the user, making use of Mech's basic output functions and Mathematica's built-in vector algebra functions.

## 3.2.1 Example Mechanism

To demonstrate the use of Modeler3D output functions, a 3D model is developed that can be measured. The model is a spatial slider-crank mechanism consisting of two moving bodies, the crank and the slider. A real slider-crank mechanism would have a fourth body, the connecting rod between the crank and the slider.
The motion of the slider-crank mechanism is driven by the rotation of the crank, which forces the slider to reciprocate in a straight track that is parallel to the axis of the crank. The crank is attached to the slider via a connecting rod that is modeled by a distance constraint.

### Bodies

Three body objects are defined for the slider-crank mechanism model. Note that the zeroth point is used in this model to avoid repeatedly defining a point at coordinates {0, 0, 0}.

• The ground (body 1) has four points defined in its body object.
P1 and P2 are two points to define the rotational axis of the crank.
P3 and P4 are two points that are used in conjunction with the origin of the ground body to define the translation axis and reference direction of the slider. P4 is also used with the local origin to define a reference direction for the crank's rotation.
• The crank (body 2) has two points defined in its body object.
P1 is a point that is used with the crank's local origin to define its rotational axis.
P2 is the attachment point of the connecting rod, and also a reference for the crank's rotation.
• The slider (body 3) has two points defined in its body object.
P0 is the attachment point of the connecting rod on the slider.
P1 is used with the slider's local origin to define its translation axis.
P2 is used with the slider's local origin to define a reference direction for the translational joint.
• ### Constraints

Four constraints are used to model the slider-crank mechanism.

• A Revolute5 constraint forces the axis of the crank to be coincident with its pivot axis on the ground. This leaves only one degree of freedom for the crank, rotation about its axis.
• A ProjectedAngle1 constraint specifies the angular relationship between a line on the ground body and a line on the crank. This driving constraint is used to rotate the crank.
• A RelativeDistance1 constraint between the crank and the slider replaces the pseudo-connecting rod, which is 30 units long.
• A Translate5 constraint between the slider and the ground leaves only one degree of freedom for the slider; horizontal translation along a track on the ground body. This constraint references two Modeler3D Axis objects that are constrained to be parallel and coincident, and the reference directions of the two axes are constrained to be coplanar.

## 3.2.2 Output Functions

Output functions.

More output functions.

The Location function is used to convert a Mech point object into the global coordinates of the point.

 In[16]:=
 Out[16]=

The Distance function is used to find the absolute distance between any two points in space. The two points can be on one body or two different bodies.

Note that it was not necessary to apply a solution rule to the last example to get a numerical result because the dimension that was measured was a constant.

The measure of an angle in 3D space requires a reference frame for measurement. ProjectedAngle allows the plane of projection of the angle to be specified.

The angular orientation of a body in 3D space can be uniquely specified by a single rotation about a fixed axis. Rotation returns the angle and axis associated with the current spatial orientation of a body. See Section 4.2 for more examples on the angular orientation of 3D bodies.

## 3.2.3 Vector Algebra Functions

Modeler3D vector algebra functions perform many common vector algebra operations, such as cross and dot products of vectors. These functions can be used to build more complicated functions that are not provided by Modeler3D. These functions take point, vector, and axis objects as arguments, just like the output functions described in Section 3.2.2. When local point numbers are used, the local coordinates are taken from the current body objects, as specified with SetBodies.
Each of the vector algebra functions is demonstrated in the context of the slider-crank mechanism model defined at the beginning of this section.

Vector algebra functions.

The Direction function is used to convert Modeler3D vector objects into 3D vectors in global coordinates.

Several Modeler3D output functions are textbook vector algebra functions that are capable of dealing with normal 3D vectors or Modeler3D vector/axis objects interchangeably.

Here is the cross product of a Modeler3D Line object and a vector.

 In[36]:=
 Out[36]=

The global coordinates of a point specified in a local reference frame are equal to the coordinates of the origin of the reference frame plus the local coordinates of the point, rotated into the global reference frame.

A constant utility.

### Coordinate Conversions

The following four conversion functions will not accept Modeler3D geometry objects as arguments. These functions only accept 3D vectors of the form {x, y, z}.

Coordinate conversions.

Coordinate conversion options.

# 3.3 Plots and Graphs

## 3.3.1 Example Mechanism

The quick-return mechanism model that was developed in Section 3.1 is used here to demonstrate all of the plotting techniques in this chapter. The model is redefined as follows in abbreviated form.
Note that a parameter crankarm has been added to the model to allow the position of the drive pin on the crank to be easily changed.

## 3.3.2 Data Plots

To generate a data plot, we must first generate some data. SolveMech is used to return 20 solution points as the crank is rotated through a full turn, with the crank arm length set to three units.

More complicated functions can also be plotted in this manner.

To create a series of plots while varying the value of crank arm, the Table function is used in conjunction with SolveMech to generate a nested list of solutions. Note that at each invocation of SolveMech the value of time T is taken from 0 to 1, which takes the angular coordinate of the crank 2 from 0 to 2 pi. This requires the value of 2 to jump from 2 pi back to 0 in one step at each new invocation of SolveMech, which is a rather large single step for the numerical solution method. To avoid possible numerical difficulties, the initial guesses are reset with SetGuess at each invocation of SolveMech.

## 3.3.3 Interpolation

Mathematica InterpolatingFunction objects can be used to improve the appearance of a ListPlot by smoothing out the data. Mech provides a function that facilitates the creation of InterpolatingFunction objects, by assuming a dependence on time.

Mech interpolation utility.

Using the quick-return mechanism model that was defined in Section 3.3.1, the plot of the connecting rod angle is reproduced using TimeInterpolate to create an InterpolatingFunction.

 In[18]:=
 Out[18]=

# 3.4 2D Mechanism Images

This section demonstrates special Modeler2D graphics functions that are used to generate the mechanism images throughout this document. Modeler2D graphics functions return graphics primitives that are given as arguments to the Mathematica Graphics function and displayed with the Show command, just like the built-in Mathematica graphics primitives.
The primary utility of the Modeler2D graphics functions is that they take their arguments in the form of local coordinates and automatically convert them to global. Thus, it is possible to create the image of a mechanism part using the same techniques as would be used with built-in Mathematica graphics primitives, and then have the part be positioned properly with respect to the other parts of the mechanism automatically, as specified by a Modeler2D solution rule returned by SolveMech.

## 3.4.1 Graphics Functions

The Modeler2D graphics functions closely parallel the built-in Mathematica graphics primitives. Each graphics function generates Mathematica graphics primitives that represent points, lines, or polygons.

Basic Mech graphics function.

More 2D graphics functions.

Options for graphics functions.

The Modeler2D Vertex, Edge, and Facet functions are analogous to the built-in Mathematica Point, Line, and Polygon functions. To demonstrate these functions, graphics objects are generated that are located on body 2, which is named body2.

The Box function is analogous to the built-in Rectangle function, with the exception that the sides of the Box graphic are aligned with the local coordinate system in which the graphic is located, instead of being aligned with the global coordinate system.
The Bar function also generates a rectangular box, but instead of specifying two points on two opposite corners, two points on two opposing sides of the rectangle are specified. The Faces option is used with the Bar function to show only an outline of the bar. Unlike the Box function, Bar accepts an axis object as its first argument, to allow the two endpoints of the bar to lie on two different bodies.

The Wheel function is used here to generate an octagon with a hole in it. Note that the n-gon returned by Wheel is placed on the body that is referenced in the Point argument, and is located with that body as such.

## 3.4.2 Annotation Functions

Three graphics functions are provided by Modeler2D specifically for annotation.

Graphics functions for annotation.

PointLabel and LocalAxes can be used to conveniently label points and bodies. The following input generates a triangle placed on body 2, with two of the points labeled with their body numbers and local coordinates.

A function for plotting the locus of a moving point.

The nested list of rules required by LocusPlot is of the form returned by SolveMech when given a range of times at which to seek solutions.

## 3.4.3 Example Mechanism Graphics

The quick-return mechanism model that was developed in Section 3.1 is used to demonstrate the Modeler2D graphics functions. The model is redefined as follows in abbreviated form.

To demonstrate the use of the graphics functions, the image of the quick-return mechanism that was displayed in the preceding sections is built. These images are generated with a mix of Modeler2D graphics functions and built-in Mathematica graphics primitives.

### Ground

The ground body of the quick-return mechanism is generated using Modeler2D graphics functions even though it does not move. This is done to take advantage of the easy referencing of local point coordinates that were defined with SetBodies. Note that the Edge function in the following graphic references points either by their {x, y} coordinates or by their local point numbers interchangeably.

### Crank

The crank graphic uses only built-in Mathematica graphics functions, with the exception of the Vertex function to plot two local points on the crank, the coordinates of which are defined in a body object. Note the use of the Location function inside of the Disk primitives. This places the disks at the correct global coordinates when the crank moves.

The text that is displayed with the crank graphic also uses the Location function to specify the global coordinates of the text so that the text moves with the crank.

Because the coordinates of some of the elements in crankgraphic and cranktext are now functions of Modeler2D dependent variables X2, Y2, and 2, a Modeler2D solution rule must be applied before they can be displayed. Note the use of SolveMech in the following input.

### Drive Arm

The drive arm graphic uses mostly built-in Mathematica graphics functions. Note how the Bar and Vertex function are called using local points 1 and 2, while the Edge function is called with explicit local point coordinates {0, 2} and {0, 8}. Most Mech functions attempt to interpret integers as local point numbers and resolve them into coordinates.

The text that was displayed with the drive arm graphic also used the Location function to specify the global coordinates of the text so that the text moves with the drive arm.

Like the crank graphic, the coordinates in the drive arm graphic are functions of Modeler2D dependent variables. Thus, a solution rule must be applied to display the graphic.

### Slider

The slider graphic uses just one built-in Mathematica graphics primitive and one Modeler2D graphics function.

### Connecting Rod

The connecting rod graphic is nothing more than a line connecting two points on two different bodies.

To show the complete quick-return mechanism, all of the graphics objects are combined and displayed subject to a single Modeler2D solution rule.

# 3.5 3D Mechanism Images

This section demonstrates the special Modeler3D graphics functions that are used to generate the mechanism images throughout this document. Modeler3D graphics functions return graphics primitives that are given as arguments to the Mathematica Graphics function and displayed with the Show command, just like the built-in Mathematica graphics primitives.
The primary utility of the Modeler3D graphics functions is that they take their arguments in the form of local coordinates and automatically convert them to global. Thus, it is possible to create the image of a mechanism part using the same techniques as would be used with built-in Mathematica graphics primitives, and then have the part be positioned properly with respect to the other parts of the mechanism automatically, as specified by a Modeler3D solution rule.

## 3.5.1 Graphics Functions

The elementary Modeler3D graphics functions closely parallel the built-in Mathematica graphics primitives. Each graphics function generates Mathematica graphics primitives that represent points, lines, or polygons. The Modeler3D compound graphics functions generate more complex shapes, such as spheres and cylinders.

Basic 3D graphics functions.

More 3D graphics functions.

Options for graphics functions.

The Modeler3D Vertex, Edge, and Facet functions are analogous to the built-in Mathematica Point, Line, and Polygon graphics primitives. To demonstrate these functions, graphics objects are generated on body 2, which is named body2.

To show the pyramid in another orientation, a set of four Euler parameters are fabricated that represent the desired angular orientation. Normally, Modeler3D graphics functions are used in conjunction with a Modeler3D solution rule that contains the values of the Euler parameters needed, but in this case they are generated explicitly.
The pyramid is now rotated 0.5 radians about the global Z axis. The EulerParameters function returns the Euler parameters required for this rotation.

The Box function is analogous to the built-in Cuboid function, with the exception that the sides of the Box graphic are aligned with the local coordinate system on which the graphic is located, instead of being aligned with the global coordinate system.
The Cylinder function generates a faceted cylinder or cone, with endpoints located at the two ends of a Modeler3D line object. The Faces option is used with Cylinder to show only an outline of the cylinder.

The Wheel function is used here to generate a part of an octagon with a hole in it. Note that the n-gon returned by Wheel is placed on the body that is referenced in the Point argument, and is located with that body as such.

The Sphere and Torus functions are used here to generate a sphere and a torus.

Functions to generate pseudo-solid objects.

Extrude is used to generate a sequence of faces that are defined by sweeping a sequence of connected lines through 3D space. The following example takes six points on body2 that define an hourglass form and extrudes them at an oblique angle into a set of six facets forming a canted hourglass.

The same list of points can be revolved about an axis specified on body2.

## 3.5.2 Annotation Functions

Three graphics functions are provided by Modeler3D specifically for annotation.

Graphics functions for annotation.

PointLabel and LocalAxes can be used to conveniently label points and bodies. The following input generates a triangle placed on body 2, with two of the points labeled with their body numbers and local coordinates.

A function for plotting the locus of a moving point.

The nested list of rules required by LocusPlot is of the form returned by SolveMech.

## 3.5.3 Example Mechanism Graphics

The spatial slider-crank mechanism model that was developed in Section 3.2 is used to demonstrate the use of Modeler3D graphics functions to create an image of a mechanism model. Creating a complex mechanism image often requires more lines of Mathematica code than were used in the kinematic model of the mechanism. This is because of the terse nature of the data required to kinematically describe a mechanism. A kinematic model uses only a small number of points in space to define rotational and translational axes that tie the bodies together, while the mechanism image requires much more information about the shape of the bodies in the model.
The spatial slider-crank model is redefined as follows in abbreviated form. See Section 3.2 for a full description of this mechanism model.

To demonstrate the use of the graphics functions the image of the slider-crank mechanism that is displayed in the preceding sections is built. These images are generated with a mix of Modeler3D graphics functions and built-in Mathematica graphics primitives.

### Ground

The ground body of the slider-crank mechanism was generated using mostly built-in Mathematica graphics primitives. The Modeler3D Edge function was used to generate a line on the crank axis referencing local points by their point numbers.

The text that was displayed with the ground body was generated entirely with the built-in Mathematica Text function, except for the axes at the global coordinate origin, generated with the Modeler3D LocalAxes function, and the use of the Location function to reference the global coordinates of local point numbers on the ground body.

### Crank

The crank graphic uses the Modeler3D Cylinder function to generate a very short cylinder. The cylinder is only one unit long because the line that defines its axis is one unit long, while the radius of the cylinder is ten units as specified.

The text that was displayed with the crank graphic also used the Location function to specify the global coordinates of the text so that the text moves with the crank.

Because the coordinates of some of the elements in crankgraphic and cranktext are now functions of Modeler3D dependent variables, X2, Y2, and so on, a Modeler3D solution rule must be applied before they can be displayed. Note the use of SolveMech in the following input. The rules that are returned by SolveMech determine where the crank is located in 3D space.

### Slider

The slider graphic uses the Modeler3D Box function to generate the rectangular box form of the slider.

Like the crank graphic, the coordinates in the slider graphic are functions of Modeler3D dependent variables. Thus, a solution rule must be applied to display the graphic.

### Connecting Rod

The connecting rod graphic uses the Modeler3D Cylinder function again to generate a long, thin cylinder spanning from the crank to the slider.

To show the complete quick-return mechanism, all of the graphics objects are put together and displayed subject to a Modeler3D solution rule.