Start a new topic

Running multiple simulations in a row from external Python program

Hello,

 

I would like to be able to run multiple simulations in a row without having to press the play/stop buttons in the Editor. I would like to run a Python program, have it start a simulation, continue until a certain condition is reached, stop the simulation, reset everything (the whole scene) to initial conditions with possibly a few modifications and repeat all that n times.

 

I have tried following the suggestion posted in this topic by Marc Liévin and using the PyMechanismViewer.py program as a reference, but I do not know how to proceed further. So far I only have a code that starts the simulation. How can I break out of the loop and reset everything? 


From my understanding this cannot be accomplished using key frames if I want to modify some parameters or content from one simulation to the next.

 

Thanks in advance.

Lyon Belyansky


Hi Lyon,


I see two ways to implement what you are looking for

1.

- have a python script implementing the following

a. instantiate an application

b. load content, ideally a scene

c. apply a set of parameters, e.g. using VHL

d. run simulation and save outputs

- have another script calling the previous one and passing a set of parameters so that several runs are performed

2.

- have a python script implementing the following

a. instantiate an application

b. load content, ideally a scene

c. save keyframe0

d. apply a set of parameters, e.g. using VHL

e. run simulation and save outputs

f. apply keyframe0, go back to d


1. is guarantee to work but is slower

2. is using keyframe and emulates what the Editor is actually doing


Marc.


1 person likes this

If you are interested in using the python moudle 'unittest', we also have an example of tests using it together with some helpers, have a look in \Vortex Studio Content 2020a\Demo Scenes\Verification\.


the helpers provide a decorator that allows parametrization of the test function using @VxATPUtils.parametrized

 

from vxatp import *
from VxSim import *
import matplotlib.pyplot as plt
# In case of an import error, a re-installation of the matplotlib module may solve the issue. Please run the following command with administrator priviledges:
# pip install --upgrade --force-reinstall matplotlib
import os.path
import unittest

class VehicleValidation(unittest.TestCase):
    directory = os.path.dirname(os.path.realpath(__file__))

    def test_lav_speed(self):
        """ Create an application, load the sample scene and retrieve the vehicle's controls needed for the simulation """
        application = VxATPConfig.createApplication(self, setup=VxATPConfig.getAppConfigWithGraphics())
        vehicle_scene = application.getSimulationFileManager().loadObject('%s/../Scenario/Defense Vehicles Scene/'
                                                                          'Defense vehicles.vxscene' % self.directory)
        lav = vehicle_scene.findExtensionByName('LAV').toObject()
        self.assertIsNotNone(lav, "** LAV not found **")
        control_script = lav.findExtensionByName('LAV Driver Interface')
        self.assertIsNotNone(control_script, "** LAV driver interface not found **")

        # Find the controls from the driver interface
        gear_up = control_script.getInput('Gear Up')
        throttle = control_script.getInput('Throttle')
        brake = control_script.getInput('Brake')
        speed = control_script.getOutput('Speed')
        current_gear = control_script.getOutput('Current Gear')
        application.update()

        """ Gather speed values at various gears during simulation and append them to a list """
        lav_speed_list = list()

        # Engage transmission's first gear and set engine to full throttle
        gear_up.value = True
        throttle.value = 1
        application.update()

        # Allow the vehicle to reach maximum speed in 1st gear
        for x in range(90):
            application.update()
            lav_speed_list.append(speed.value)

        # Shift to second, then 3rd gear. Interrupt 'Gear up' signal between each action
        gear_up.value = False
        application.update()
        gear_up.value = True
        application.update()
        gear_up.value = False
        application.update()
        gear_up.value = True
        # Let vehicle go forward for 250 steps.
        for x in range(250):
            application.update()
            lav_speed_list.append(speed.value)
        # Vehicle should be in 3rd gear
        self.assertEqual(current_gear.value, 'D3', "** LAV has not engaged third gear **")
        # Vehicle's speed should reach at least 18 km/hr
        self.assertGreaterEqual(speed.value, 18, "** LAV has not reach expected speed **")

        # Cut throttle, apply full brake
        throttle.value = 0
        brake.value = 1
        # Wait for the vehicle to stop completely
        while speed.value > 0:
            application.update()
            lav_speed_list.append(speed.value)

        # Create a graphical representation of the simulation data
        plt.plot(lav_speed_list)
        plt.grid(True)
        plt.title('LAV- 1st, 2nd and 3rd gear, full throttle')
        plt.xlabel('Simulation step')
        plt.ylabel('Speed (km/h)')
        plt.savefig('%s/LAV speed scenario 1.jpg' % self._config.output_directory)
        print "Test completed successfully. Please check the content of the \Tests results\ folder"


if __name__ == '__main__':
    vxatp_run_thisfile('.\Tests results')

 


1 person likes this
Login to post a comment