simulating multiple objectscsundergrad.science.uoit.ca/courses/csci3010u/in... · 64 / 75...

15

Upload: others

Post on 21-Jun-2020

10 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Simulating multiple objectscsundergrad.science.uoit.ca/courses/csci3010u/in... · 64 / 75 Simulating multiple objects I So far we have simulated single objects I Now we discuss how
Page 2: Simulating multiple objectscsundergrad.science.uoit.ca/courses/csci3010u/in... · 64 / 75 Simulating multiple objects I So far we have simulated single objects I Now we discuss how

64 / 75

Simulating multiple objectsI So far we have simulated single objectsI Now we discuss how to simulate multiple objects?

I The number of objects is a parameter for the simulation.

Page 3: Simulating multiple objectscsundergrad.science.uoit.ca/courses/csci3010u/in... · 64 / 75 Simulating multiple objects I So far we have simulated single objects I Now we discuss how

65 / 75

Simulating a collection of balls in a squareI Balls move in 2DI Random initial positions and velocitiesI Balls move under the influence of gravityI Balls bounce o� the wallsI Balls pass through each other (i.e., no collisions between balls)I No friction

Page 4: Simulating multiple objectscsundergrad.science.uoit.ca/courses/csci3010u/in... · 64 / 75 Simulating multiple objects I So far we have simulated single objects I Now we discuss how

66 / 75

Simulating a collection of balls in a squareQuestion 1

Say we are interested in simulating n balls in a square. What is thestate size of our simulation?

Answer 1

4n, (x, y) locations and (vx, vy) velocities for each ball.

Question 2

How do we set up the initial state for our simulation, i.e., the initiallocations and initial velocities for each ball?

Answer 2

Random positions and velocities.

Page 5: Simulating multiple objectscsundergrad.science.uoit.ca/courses/csci3010u/in... · 64 / 75 Simulating multiple objects I So far we have simulated single objects I Now we discuss how

67 / 75

Simulating a collection of balls in a squareWhat are we missing in this simulation?

I Not handling collisions between ballsI If only a few balls in a very large square, ball-ball collisions may

be rare event.I If a lot of balls crammed in a small space, we can’t really ignore

ball-ball collisions.

Page 6: Simulating multiple objectscsundergrad.science.uoit.ca/courses/csci3010u/in... · 64 / 75 Simulating multiple objects I So far we have simulated single objects I Now we discuss how

68 / 75

Ball-ball collisionsI Ball-ball collisions are di�cult to do e�ciently.

I Unlike ball-wall collisions, where only one object is moving, inball-ball collision, both objects are moving.

Naive approach

I At each time step, inspect each pair for possible collision.I For n balls this leads to n2 inspections.

Other things to consider

What if three balls collide with each other at the same instant?What if n balls collide with each other at the same instant?

Page 7: Simulating multiple objectscsundergrad.science.uoit.ca/courses/csci3010u/in... · 64 / 75 Simulating multiple objects I So far we have simulated single objects I Now we discuss how

69 / 75

Object-object collisionsI E�cient collisions between multiple objects is very challengingI Most simulations only consider these when absolutely necessary

I Gas molecules are small, so when simulating low-density gases inlarge volumes, inter-molecules collisions are sometimes ignored.

Page 8: Simulating multiple objectscsundergrad.science.uoit.ca/courses/csci3010u/in... · 64 / 75 Simulating multiple objects I So far we have simulated single objects I Now we discuss how

70 / 75

What other things have we ignored in our simulationcontaining multiple balls in a square?

Brainstorm

Page 9: Simulating multiple objectscsundergrad.science.uoit.ca/courses/csci3010u/in... · 64 / 75 Simulating multiple objects I So far we have simulated single objects I Now we discuss how

71 / 75

What other things have we ignored in our simulationcontaining multiple balls in a square?

I Ball-wall collisions ignore the e�ects of impact on the wall (andthe balls)I If the balls were ball bearings, and the walls were made of thin

aluminum then each collision would dent the wall.I The walls will get bent out of shape over time.I How would you model walls that bends overtime? This require

some very complicated physics, large computational power, andsophisticated numerical techniques.

I We also didn’t model the color of the ballsI This would be of interest if we are intrested in light bounces or

heat transfer.

Page 10: Simulating multiple objectscsundergrad.science.uoit.ca/courses/csci3010u/in... · 64 / 75 Simulating multiple objects I So far we have simulated single objects I Now we discuss how

72 / 75

GuidlinesI We need carefully identify what really needs to be modeled and

simulated.I We can make simulations arbitrarily complex by considering

more things.I This makes it harder to produce simulations.I Simulations will be less e�cient.I Simulations might become less useful.

I We need to know where to draw the line.

Correctly determining the applications of the simulation is animportant first step in getting the model right

Page 11: Simulating multiple objectscsundergrad.science.uoit.ca/courses/csci3010u/in... · 64 / 75 Simulating multiple objects I So far we have simulated single objects I Now we discuss how

73 / 75

SummaryI Input, output and state variablesI Di�erential equations are used to model the behavior of state

variablesI Numerical solvers for solving di�erential equations

I Good numerical solvers really only exist for degree 1 di�erentialequations.

I Transform higher order di�erential equations to multiplefirst-order di�erential equations.I This introduces extra state variables

Page 12: Simulating multiple objectscsundergrad.science.uoit.ca/courses/csci3010u/in... · 64 / 75 Simulating multiple objects I So far we have simulated single objects I Now we discuss how

74 / 75

SummaryI Use of indirect means to determine whether or not our

simulation is correct.I We used our knowledge of the law of conservation of energy to

identify the problem with our simulationI InteractionsI Projectile motion

I Our first encounter with an ODE that has no analytical solutionI Drag

I Terminal velocityI First exposure to infering dynamics from empricial data

Page 13: Simulating multiple objectscsundergrad.science.uoit.ca/courses/csci3010u/in... · 64 / 75 Simulating multiple objects I So far we have simulated single objects I Now we discuss how

75 / 75

SummarySimulating multiple objectsI How to model the system?I How to manage state space?I PerformanceI Problem set up or initialization

Page 14: Simulating multiple objectscsundergrad.science.uoit.ca/courses/csci3010u/in... · 64 / 75 Simulating multiple objects I So far we have simulated single objects I Now we discuss how

"""author: Faisal Qureshiemail: [email protected]: http://www.vclab.calicense: BSD"""

import numpy as np

class Simulation: def __init__(self, title): self.paused = True # starting in paused mode self.title = title self.cur_time = 0 self.dt = 0.033 # 33 millisecond, which corresponds to 30 fps # Fix this - done self.state = np.array([0,0,0,0], dtype='float32') self.mass = 1. self.k = 1. self.l = 1

def init(self, state, mass, k, l): # Fix this - done self.state = state self.mass = mass self.k = k self.l = l

def set_time(self, cur_time=0): self.cur_time = cur_time

def set_dt(self, dt=0.033): self.dt = dt

def step(self): print('state', self.state)

# Fix this - done current_len = np.linalg.norm(self.state[:2]) deformation = current_len - self.l

spring_axis = np.copy(self.state[:2]) spring_axis /= current_len

force = - self.k * deformation * spring_axis

# print('state', self.state) # print("current_len", current_len) # print("deformation", deformation) # print("spring_axis", spring_axis) # print("force", force)

# Update position self.state[-2:] = self.state[-2:] + force * self.dt / self.mass self.state[:2] = self.state[:2] + self.state[-2:] * self.dt # print('mass', self.mass) # print('dt', self.dt) # print('state', self.state)

# x = np.array([1,2,3,4]) # print(x[:2]) # print(x[-2:]) def pause(self):

Page 15: Simulating multiple objectscsundergrad.science.uoit.ca/courses/csci3010u/in... · 64 / 75 Simulating multiple objects I So far we have simulated single objects I Now we discuss how

self.paused = True

def resume(self): self.paused = False

def save(self, filename): # Ignore this pass

def load(self, filename): # Ignore this pass