Creating a python script to interact with model during simulation

I am running a simulation analysis of an automatic motorcycle. Is it possible to create a python script that could gather the realtime data of the motorcycle during simulation, and also interact with the model to change its attributes for example, throttle position, torque, etc.?
Thanks in advance!
Best Answer
-
Hi @sudo,
Depending on the complexity of this process, you may need to instead implement this AI model using Twin Activate. In this example, Twin Activate would be the primary interface, with a MotionSolve FMU block and your AI Model implemented as a python or OML block as shown:
This is obviously a very simple representation, but it will hopefully paint the picture for how you can alternatively layer this simulation.
If the goal is to compare fuel economy between the standard Driver Model and the AI Model, then you can easily set a speed profile for the MotionSolve model to try and follow, to which the AI Model is governing your shift logic as designed. Then, compare the results from the original and improved models to evaluate the change in shifting performance.
Hope this helps!
Adam Reid
2
Answers
-
Hi @sudo,
Python scripts can be easily implemented to interact with your model using a User Defined Function. First create a Solver Variable, then check the box for User-Defined. Please refer to the following article for more:
https://community.altair.com/discussion/61104/implementing-higher-complexity-functions-through-python-user-subroutines-in-motionview?utm_source=community-search&utm_medium=organic-search&utm_term=user+defined+functionHowever, everything you just described sounds like the Altair Driver Model, which already governs the gear selection, throttle position, and steering control.
Hope this helps!Adam Reid
1 -
Hi @sudo,
Depending on the complexity of this process, you may need to instead implement this AI model using Twin Activate. In this example, Twin Activate would be the primary interface, with a MotionSolve FMU block and your AI Model implemented as a python or OML block as shown:
This is obviously a very simple representation, but it will hopefully paint the picture for how you can alternatively layer this simulation.
If the goal is to compare fuel economy between the standard Driver Model and the AI Model, then you can easily set a speed profile for the MotionSolve model to try and follow, to which the AI Model is governing your shift logic as designed. Then, compare the results from the original and improved models to evaluate the change in shifting performance.
Hope this helps!
Adam Reid
2 -
Hi @GTT Adam Would it be possible to control the Primary and Secondary Pulley Radii of the CVT.
Currently our AI model takes in the inputs: Vehicle Velocity (mm/sec deg/sec), Vehicle Acceleration (g\'s deg/sec**2), Engine Torque, Engine Speed, Vehicle Speed, Power Output, Fuel Consumption.
So these are the inputs that we need to gather from the simulation in real time to be passed to the ai model. Then the AI model will generate the predicted gear ratio based on this input. So the outputs would be Primary and Secondary pulley radii. This is what we need to change in the model during the simulation.
I also wonder if this could be done in jupyter notebook in motionsolve. Currently our python program for the AI model is in google colab.0 -
Hi @sudo,
You would need to analytically model the pulley connection if you are looking to have it change size during simulation. A cylinder graphic can't have it's size be changed during simulation as that is a design-time parameter. Couplers can also not have their ratio changed during simulation.
I would suggest that the cylinders/pulleys are connected via a Motion, where the velocity of the output pulley is equal to the product of the velocity of the input pulley and a solver variable for a dynamic gear ratio.
I have set up this simple CVT model to demonstrate this point:
An input pulley is rotated via an input Motion to achieve a set rotational velocity. A second pulley is rotated in response via a velocity-type Motion. This output Motion is expression based, and is defined as
-1*WX({Body_1.cm.idstring},{Global_Frame.idstring})*{sv_gear_ratio.VARVAL}
, where Body_1 is the input pulley body, WX is its rotational velocity, and sv_gear_ratio is the dynamic gear ratio Solver Variable. The Solver Variable is changed over time in this example from 1:1 to 1:5 using a STEP function:STEP(TIME,1,1,5,5).
In your model, the STEP function would be replaced with the output from the AI model.The relative velocities of the pulleys is plotted below:
Please find the demo model attached for your review and modification:
Hope this helps!
Adam Reid
1 -
Thanks @GTT Adam for this very detailed explanation and for the resources. However, if we implement the integration like this, we cannot simulate using the built in motorcycle events.
What I am planning is to integrate the AI model to the builtin motorcycle model with cvt, and perform a simulation using the motorcycle events.
With this I can compare the performance of the motorcycle with cvt without an AI model, and with the AI model that controls the gear ratio. Specifically I need to compare their fuel consumption as well as power output.Is this achievable?
0 -
Hi @sudo,
An implementation like this should not impact the ability to use the Altair Driver model nor the Standard Events. If you are using the standard assembly model for the motorcycle, for example, then you would likely only have to replace the expressions for the frc_chain_force with your User-Defined function.
Your User-Defined function would just need to accept the Transmission Body rotational speed as an input, along with the values you need for the AI model, and then output the force on the Wheel as per your calculations.
Hope this helps!
Adam Reid
0