-
Notifications
You must be signed in to change notification settings - Fork 22
Tutorial 2 python Dynamic Creation and Deletion
In this tutorial, we will create and delete cubes dynamically throughout the run, adding new masses and springs to the simulation while it is running.
See the first tutorial for the full project setup. As before, include the Titan simulation header to access the library methods.
import titan
import numpy as np
import math
We will start the simulation with a single lattice bouncing on a flat plane. This is essentially the outcome of the previous tutorial. In the main function, we create a Simulation object, and add the plane and lattice.
titan.Simulation sim # create the basic simulation object.
sim.createLattice(np.array([0,0,20]), np.array([0,0,20]), 5, 5, 5)
sim.createPlane(np.array([0,0,1]), 0);
Then we add start the run, and add new cube every second.
sim.start()
while (sim.time() < 10.0):
sim.pause(sim.time() + 1.0) # pause at the current time plus 1 second
sim.createLattice(np.array([math.cos(sim.time()),math.sin(sim.time()),10]), np.array([5,5,5]), 5,5,5) // create a lattice at some random position
if (sim.time() > 5.0):
sim.deleteContainer(sim.getContainerByIndex(0)) # delete the first created container each iteration, after 5 seconds.
sim.resume()
sim.stop() # stop the simulation after breaking out of the loop
This program is easy to write, but runs a rather complicated simulation. After setting it up, every second, it creates a new lattice object. Also, after 5 seconds have passed, it will delete the earliest lattice object still present in the simulation. The simulation lasts for 10 seconds.
The full program now looks like this:
import titan
import numpy as np
import math
titan.Simulation sim # create the basic simulation object.
sim.createLattice(np.array([0,0,20]), np.array([0,0,20]), 5, 5, 5)
sim.createPlane(np.array([0,0,1]), 0);
sim.start()
while (sim.time() < 10.0):
sim.pause(sim.time() + 1.0) # pause at the current time plus 1 second
sim.createLattice(np.array([math.cos(sim.time()),math.sin(sim.time()),10]), np.array([5,5,5]), 5,5,5) // create a lattice at some random position
if (sim.time() > 5.0):
sim.deleteContainer(sim.getContainerByIndex(0)) # delete the first created container each iteration, after 5 seconds.
sim.resume()
sim.stop() # stop the simulation after breaking out of the loop
- Home
- Quickstart
-
About the Titan Library
- Basic Concepts
- Functions
- Data Structures
- CPU - GPU Architecture
- Tutorials