User Defined Variable Gravity in Dymola

A while back, I was asked by a customer how hard I thought it would be to create a simulation which utilizes a user defined variable gravity field within Dymola. I’m not quite sure what he wanted to do with it… maybe something acrobatic and cool like this? Anyway, having not played with this functionality all that much in the past (and recognizing that this customer probably had a legitimate need for it), I replied that it was probably straightforward (as most things are in Dymola) but I should look into it before providing a concrete answer. I also told him it sounded like a good topic for a blog post… so here we are! It turns out that creating a custom function to define how gravity behaves in a Dymola model within the Modelica language is relatively straightforward.

The World Block

The World block (Modelica.Mechanics.MultiBody.World) is an ‘inner’ model which is utilized to apply gravity to the various mass objects in a multibody model, and can be parameterized in a few different ways to cover a wide variety of use cases. This includes a user defined variable gravity field like we are going to explore in this post.

The following parameters dictate how the gravitational field operates:

  • gravityType: Type of gravity field
  • g: Constant gravity acceleration
  • n: Direction of gravity resolved in world frame (gravity = g*n/length(n))
  • mu: Gravity field constant (default = field constant of earth)
  • gravityAcceleration: Function to compute the gravity acceleration in world frame

For simulating Motorsport applications (since automobile races are generally held here on Earth) we mostly use the default settings with uniform gravity and the “Standard acceleration of gravity on earth” as it’s referred to in the Modelica Standard Library. We do, however find that pretty much everyone in the world wants to change n to {0,0,-1}… 🙂

Here is what that looks like in the ‘world’ parameter dialog window (please ignore the driveTrainMechanics3D parameter which seems a bit out of place here):

As you can see, the mu and gravityAcceleration parameters are greyed out and inactive… indicating that they are not being utilized for this use-case. In order to activate the gravityAcceleration function, we need to change the gravityType to ‘NoGravity’ as shown here:

Once this selection has been made, notice that the g, n, and mu parameters are greyed out… alluding that they no longer have any effect. It is hopefully clear that the default gravityAcceleration function is now being used to generate the gravitational acceleration. At this point I was confident that this is how our user defined variable gravity field was to be deployed.

Our User Defined Variable Gravity Function

Anytime I need to create a custom component of any type, I generally spend a little time evaluating similar components in any of the existing libraries. In this case, the logical place to start is the default gravityAcceleration function called Modelica.Mechanics.MultiBody.Forces.Internal.standardGravityAcceleration. Using this function as a template, we can then create our own custom gravityAcceleration function. The first place I like to look is the Documentation layer of the model. The I/O and extensions are auto-generated and fairly apparent in this layer and it helps to align my mental model with how things are coded for this component. Here is the Documentation layer for the Modelica.Mechanics.MultiBody.Forces.Internal.standardGravityAcceleration model:

From this we can see that it was extended from Modelica.Mechanics.MultiBody.Interfaces.partialGravityAcceleration function and it only has one output, gravity[3]. Observing the same thing on the extended function Modelica.Mechanics.MultiBody.Interfaces.partialGravityAcceleration yields this:

From this Documentation layer, we can see that the only required input is r[3]. This should be relatively easy to modify to do what we want.

As a demonstration, let’s define a gravitational field which smoothly rotates around the world y-axis through time. For those of you who don’t do this often, Wikipedia can help refresh your memory about 3D rotation matrices… and provide you with this gem:

In order to easily adjust the behavior of this function in the future, I think the following inputs are reasonable:

accelerationMagnitude by default Modelica.Constants.g_n

angularVelocity by default 2*Modelica.Constants.pi/10 (eg: 10 seconds per revolution)

startVector by default {0,0,-1}

simulationTime you must pass time into a function if it’s used in the calculations

Here is the function I’ve created to define our ‘loopy’ gravitational field:

Testing Our User Defined Variable Gravity Function

To test the gravityAcceleration function, let’s use a simple double pendulum. To do this I went ahead and duplicated the Modelica.Mechanics.MultiBody.Examples.Elementary.DoublePendulum model, deleted the rotational damper on the first arm, and set the start angle of the first revolute joint to 90 degrees (downward).

I decided to simulate the model with zero accelerationMagnitude to make sure the pendulum remained static… and it did! 🙂

Next, let’s set the accelerationMagnitude back to Modelica.Constants.g_n and check to see if the pendulum magically rotates in a circle… and it does! Simulating the model for 20 seconds has the pendulum making approximately 2 revolutions. As you can see in the following plot, the angle phi in revolute2 joint wobbles about from +/- 5 degrees.

Now, if we increase the accelerationMagnitude by 10x, 100x, and 1000x note how the ‘wobble’ in the revolute2.phi reduces.

You can also see the wobble in the phi of revolute1:


In closing, creating a custom function to define how gravity behaves in a Dymola model within the Modelica language is relatively straightforward. If you can dream up the gravitational field you want to simulate, you can likely model it. If anyone is interested in the source code for this model it can be downloaded here.

Nate Horn – Vice President

Please get in touch if you have any questions or have got a topic in mind that you would like us to write about. You can submit your questions / topics via: Tech Blog Questions / Topic Suggestion.


Got a question? Just fill in this form and send it to us and we'll get back to you shortly.


© Copyright 2010-2024 Claytex Services Ltd All Rights Reserved

Log in with your credentials

Forgot your details?