Initialize Variables for a Mass-Spring-Damper System
This example shows how you can use block variable initialization, and how it affects the simulation results of a simple mechanical system.
The model is a classical unforced mass-spring-damper system, with the oscillations of the mass caused by the initial deformation of the spring.
Create and Set Up the Model
Create a simple mass-spring-damper system. Use the Mass, Translational Spring, Translational Damper, Mechanical Translational Reference, Ideal Translational Motion Sensor, PS-Simulink Converter, Solver Configuration, and Scope blocks, and connect them as shown in the following illustration.
In the Translational Damper block dialog box, set the Damping coefficient parameter to
10
N/(m/s)
. Use the default parameter values for all of the other blocks.Prepare the model for simulation. In the model window, open the Modeling tab and click Model Settings. The Configuration Parameters dialog box opens, showing the Solver pane. Set Solver to
ode23t (mod.stiff/Trapezoidal)
and Max step size to0.2
. Also adjust the Simulation time to be between 0 and 2 seconds, by setting Stop time to2.0
.Specify the initial deformation of the spring. Double-click the Translational Spring block. In the block dialog box, expand the Initial Targets section, and then select the check box next to the Deformation variable. Keep its Priority as
High
. Change the Value to0.1
, leaving the unit unchanged asm
.Adjust the initial position of the sensor, to compensate for the spring deformation. Double-click the Ideal Translational Motion Sensor block and set its Initial position parameter value to
0.1
m
as well. This way, when you simulate the model, mass oscillations center around 0.Simulate the model.
Open the Variable Viewer. In the model window, on the Debug tab, click Simscape > Variable Viewer.
The Translational Spring variable x, in the bottom row, has high priority and the target value of 0.1 m. This is the Deformation variable that you have just set up in the block dialog box. Its actual start value matches its target value, and therefore its Status column displays a green circle.
The two other high-priority variables in this model are the position, x, of the Ideal Translational Motion Sensor block and the velocity, v, of the Mass block. Both of these variables are set to high priority inside the component file because the high priority is necessary for the correct operation of the block. For both of these variables, the actual start value also matches the target value, and the Status column also displays a green circle.
The rest of the variables in the model do not have initialization priority specified, therefore their Status column also displays green circles. The overall status at the bottom of the Variable Viewer window displays a green circle as well, and says that all the targets are satisfied.
Change Initialization Targets
You can now see how specifying different variable targets affects system initialization and simulation results.
Change the initial velocity of the mass. Double-click the Mass block, expand the Initial Targets section, select the check box next to the Velocity variable, keep its Priority as
High
, and enter a beginning value of10
, keeping the unit asm/s
.When you change variable priorities and targets or adjust the block parameters, the results in the Variable Viewer are not updated automatically. Instead, the Update button displays a warning symbol (exclamation point in a yellow circle), and the timestamp at the bottom of the viewer window indicates that the data in the viewer does not reflect the latest model changes.
Refresh the Variable Viewer by clicking Update.
You can see that the solver has found a different initial solution, which satisfies your variable targets for spring deformation and mass velocity. The Status column displays green circles, and the overall status at the bottom of the Variable Viewer window also displays a green circle and says that all the targets are satisfied.
Notice that when you refreshed the Variable Viewer, the scopes turned blank. This happens because solver runs the simulation for 0 seconds to find the initial solution and display it in the Variable Viewer.
Rerun the simulation and examine the Velocity and Position scope windows, to see the effect of the new initial value for mass velocity on the simulation results.
Deal with Over-Specification
As you specify additional variable targets, sometimes it is possible to over-specify the constraints.
Double-click the Translational Damper block, expand the Initial Targets section, select the check box next to the Force variable, change its Priority to
High
, and enter a value of200
. Keep the unitN
.Refresh the Variable Viewer.
The overall status at the bottom of the Variable Viewer window now displays a red square and says that the solver is unable to satisfy all the high-priority variable targets. There are red squares in the Status column for the two high-priority variables with targets not satisfied.
Notice that the solver has been able to find a solution for model initialization. If you rerun the simulation, it runs without errors and you can see the new simulation results.
However, the Variable Viewer shows that the model initialization solution does not satisfy your target values for block variables. This happens because placing high-priority constraints on all three elements of the mass-spring-damper system results in a conflict. You can resolve the over-specification issue by relaxing the priority of some of the conflicting variable targets.
Double-click the Translational Damper block again and change the priority of the Force variable to
Low
.Refresh the Variable Viewer.
The overall status at the bottom of the Variable Viewer window now displays a yellow triangle and says that all the high-priority targets are satisfied, but some of the low-priority targets are not satisfied. There are now a yellow triangle in the status column for the Translational Damper low-priority force variable f.
Essentially, the solution found in this case is the same as when you previously specified high-priority target for the mass velocity, and the simulation results are the same.
Another way to deal with over-specification is to keep the high priority on the damper force and relax the priority on mass initial velocity. Double-click the Translational Damper block again and change the priority of the Force variable back to
High
. Then double-click the Mass block and change the priority of the Velocity variable toLow
.Refresh the Variable Viewer.
Again, the Variable Viewer status says that all the high-priority targets have been satisfied and that some of the low-priority targets are not satisfied. However, because you changed the variable priorities, the solver now tried to satisfy the initial force on the damper rather than the mass velocity, and the solution is different in this case, as are the simulation results.