Implementing Higher Complexity Functions Through Python User Subroutines in MotionView
Overview:
Using the Expression Builder in MotionView allows the User to write a variety of expressions to define Forces, Motions, and the positions of Points. However, higher level functions can be cumbersome in the Expression Builder, so a User Subroutine can be implemented to enable more complex and higher-order functions. Alternatively, a certain function may already be defined in Python and the User may want to deploy an existing function. This simple example will show how to write and deploy a custom Python-based User Subroutine.
Instructions:
In this example model, there exists a sphere that is attached to Ground via a translational joint. The joint is oriented such that the allowable movement is only in the X axis. A force is applied to the sphere to move it along the X axis, which is to be defined using our simple Python script.
The function we are going to use for this force is Fx = input1 + input2, where input1 and input2 are linear values.
In a text editor, create a new file called “myPySub.py”. Edit the script to contain the following:
Back in MotionView, you can create a Force and enable the User-Defined option. For our MYFORCE subroutine, we are simply going to define par[0] as 50 N and par[1] as 100 N for a total of 150 N force to apply based on our function. Therefore, the USER expression will be:
`USER(50,100)`
The inputs of 50 and 100 enter the subroutine as the array ‘par’, and are referenced using the par[0] and par[1] syntax. Since the force is 3D, the forceOut array we create will be an array of size 3. Therefore, when we return the forceOut array, the output is only in the X axis.
After simulating the model, we can now observe the output force on the sphere and the animation of the simulation:
As shown in the attached GIF, the sphere will move along the X axis due to an imposed 150 N force in the X axis only.
Hopefully this example provides clarity on how to easily implement existing or custom Python functions as User Subroutines within MotionView to best capture the complex nature of your model.
Looking for more material on MotionView/MotionSolve? Check out our available eLearning courses linked below: