def start(dt=dt): analitic = Analitic(r=1, v=-gamma_div_2m, r_function=r_analitic_function, v_function=v_analitic_function, a_function=a_function) verlet = Verlet(r=1, v=-gamma_div_2m, a_function=a_function, dt=dt) beeman = Beeman(r=1, v=-gamma_div_2m, a_function=a_function, dt=dt) gear = Gear5(r=1, v=-gamma_div_2m, a_function=a_function, dt=dt, k=k, m=m) strr = "" for index in xrange(1, int(tf / dt) + 1): t = index * dt #import ipdb; ipdb.set_trace() analitic.loop(t) verlet.loop() beeman.loop() gear.loop() #strr+= (str(verlet[R])) + '\t' + str(analitic[R]) + '\t' + (str(verlet[V])) + '\t' + str(analitic[V]) + '\t' + (str(verlet[A])) + '\t' + str(analitic[A]) + '\n' plot(analitic.r_history, verlet.r_history, beeman.r_history, gear.r_history) with open('output_particle/data.txt', 'w') as outfile: methods = [verlet, beeman, gear] outfile.write( reduce( lambda accum, elem: accum + str(elem.__class__) + str( elem.get_error(analitic.r_history)) + " ,", methods, ""))
def test_others(self): v = Verlet(None) ms = [ Molecule(V(1, 2), V(3,4)), Molecule(V(2, 3), V(4, 5)), Molecule(V(4, 5), V(6, 7)) ] assert v.others(ms[0], ms) == [ms[1], ms[2]] assert v.others(ms[1], ms) == [ms[0], ms[2]] assert v.others(ms[2], ms) == [ms[0], ms[1]]
def __init__(self, map, position): print "New player" Entity.__init__(self) self._lock = None self._phys = Verlet(position) self._phys.setDamping((0.02, 0.02, 0.0002)) self._phys.setGravity((0, 0, -0.00006)) self._on_floor = False self._speed = 0 self._map = map self._heading = 0 self.reset() r = self._radius = 0.1 self.setBounds(((-r, -r, -r), (r, r, r))) # entity stuff Entity.moveTo(self, position) # keep the entity updated self._scaling = T.scale_matrix(self._radius) self._last_pos = N.array((0, 0, 0), dtype="f") self._graphics = R.loadObject("sphere.obj", "diffuse")
def __init__(self): self.current_step = 0 self.molecules = MoleculeGenerator.generate() print self.molecules self.canvas = Canvas() self.canvas.setup_timer(self.do_movement) self.verlet = Verlet()
def __init__(self, track, map): self._track_obj = track position = self._track_obj.getPosition() self._target = Verlet(position) self._pos = Verlet(position + (1,0,0)) self._map = map self._pos.setGravity(0) self._target.setGravity(0) self._pos.setDamping((0.3,0.3,0.99)) self._target.setDamping(0.3) self._up = N.array((0,0,1),dtype="f") self._look_at = position
class Simulator(object): def __init__(self): self.current_step = 0 self.molecules = MoleculeGenerator.generate() print self.molecules self.canvas = Canvas() self.canvas.setup_timer(self.do_movement) self.verlet = Verlet() def do_movement(self): self.current_step += 1 self.move_molecules() self.canvas.refresh(self.molecules) # Set the timer to go again if self.current_step < max_steps: if self.current_step % 500 == 0: print "step %s/%s" % (self.current_step, max_steps) self.canvas.setup_timer(self.do_movement) else: Plotter().plot(self.verlet.pes, self.verlet.kes) def move_molecules(self): self.verlet.process_molecules(self.molecules)
def __init__(self, dim=3, n_a=3, st=0.01, sts=100, algorithm='LeapFrog', force_fields=['SoftWall', 'MinBarrierMin'], parameters=[[1, 10], [5, 10, 3, 0.02]]): self.n_atoms = int(n_a) self.atoms = [Atom(dim, st) for i in range(self.n_atoms)] self.time_step = float(st) self.steps = int(sts) if algorithm == 'Verlet': self.algorithm = Verlet() elif algorithm == 'VelocityVerlet': self.algorithm = VelocityVerlet() elif algorithm == 'LeapFrog': self.algorithm = LeapFrog() else: sys.exit('Wrong algorithm name') self.force_fields = [] for f in range(len(force_fields)): if force_fields[f] == 'SoftWall': self.force_fields.append(SoftWall(parameters[f])) for a in self.atoms: a.Recalculate(st, True) elif force_fields[f] == 'MinBarrierMin': self.force_fields.append(MinBarrierMin(parameters[f])) for a in self.atoms: a.Recalculate(st, True) elif force_fields[f] == 'LenardJones': self.force_fields.append(LenardJones(parameters[f])) for a in self.atoms: for i in range(a.coords_t.dim): if a.coords_t[i] >= 0: a.coords_t[i] += a.number else: a.coords_t[i] -= a.number else: sys.exit('Wrong potential name')
class Camera: def __init__(self, track, map): self._track_obj = track position = self._track_obj.getPosition() self._target = Verlet(position) self._pos = Verlet(position + (1,0,0)) self._map = map self._pos.setGravity(0) self._target.setGravity(0) self._pos.setDamping((0.3,0.3,0.99)) self._target.setDamping(0.3) self._up = N.array((0,0,1),dtype="f") self._look_at = position @profile def update(self,time): self._pos.update() self._target.update() look_at = self._track_obj.getPosition()+(0,0,0.6) cam_pos = look_at -self._track_obj.getFrontVector() * 2.0 + N.array((0,0,0.0)) self._pos.impulseTowards(cam_pos) self._target.impulseTowards(look_at) # see if the camera doesn't intersect geometry. If it does, push it up cam_pos =self._pos.getPosition() look_at = self._target._position dv = cam_pos - look_at dist = T.vector_norm(dv[0:2]) # distance across floor n = math.ceil(dist / 0.2) z0 = look_at[2] dray = dv / n m = self._map hit = False # ray-march #z_list = [] ray_pos = look_at + dray # advance once for i in xrange(1,int(n)+3): # start at i=1 to avoid division by zero z = m(ray_pos[0], ray_pos[1], False)+0.4 # is an offset so the camera can see #z_list.append(int(z*10)) if z > ray_pos[2]: slope = (z-z0) / float(i) #if slope > 0.1: # ray_pos -= dray # go back one step # break dray[2] = slope ray_pos[2] = z hit = True ray_pos += dray ray_pos -= dray*3 #print z_list #print "%s -> %s %s" % (self._pos.getPosition(), ray_pos, "HIT" if hit else "") if hit: self._pos.moveTo(ray_pos) def getMatrix(self): return lookAtMtx(self._pos._position, self._target._position, self._up)
def set_positions(self): model = LennartJonesModel('H',1,2) algo = Verlet(model, self) v = algo.run(10) for atom in self: atom.pos = atom.pos + v
class Player(Entity): def __init__(self, map, position): print "New player" Entity.__init__(self) self._lock = None self._phys = Verlet(position) self._phys.setDamping((0.02, 0.02, 0.0002)) self._phys.setGravity((0, 0, -0.00006)) self._on_floor = False self._speed = 0 self._map = map self._heading = 0 self.reset() r = self._radius = 0.1 self.setBounds(((-r, -r, -r), (r, r, r))) # entity stuff Entity.moveTo(self, position) # keep the entity updated self._scaling = T.scale_matrix(self._radius) self._last_pos = N.array((0, 0, 0), dtype="f") self._graphics = R.loadObject("sphere.obj", "diffuse") def getMissile(self, scene): if self._lock is not None: follow = self._lock[0] else: follow = None m_dir = 50 * (self._speed + 0.02) * self._front_vec * 1.5 + self._normal T.unit_vector(m_dir, out=m_dir) return Missile(scene, self._pos, m_dir, "projectile", follow) def reset(self, position=None): self._rotation = T.quaternion_about_axis(0, (1, 0, 0)) self.setHeading(0) if position is not None: self.moveTo(position) def checkLock(self, enemy): best_d = 100000 if self._lock is None else self._lock[1] if enemy._closing: return v = enemy._pos - self._pos d = N.dot(v, v) if d < 1000: # max enemy distance if self._lock is None or best_d > d: self._lock = (enemy, d, enemy._pos) def getLockPosition(self): try: return self._lock[2] # enemy position when locked except: return None # for physics simulation @profile def update(self, time): self._lock = None # ----- here be dragons ph = self._phys ph._impuse = ph._gravity ph.update() # test for collision with map z, normal = self._map(ph._position[0], ph._position[1]) h = (z + self._radius) - ph._position[2] T.unit_vector(normal, out=normal) self._on_floor = h > 0 if self._on_floor and ph._position[2] < ph._last_position[2]: # collision ph._impulse += h * normal * 0.00001 # * normal[2] ph._position[2] += h ph._last_position[2] += h new_pos = ph._position # ----- self._dir = new_pos - self._pos self._speed = T.vector_norm(self._dir) self._last_pos[:] = new_pos Entity.moveTo(self, new_pos) # updates self._pos too self._normal = normal self._axis = N.cross(self._dir, normal) self.roll(self._axis) def moveTo(self, pos): dp = pos - self._pos Entity.moveTo(pos) self._phys.displace(dp) def roll(self, axis): len = T.vector_norm(axis) * 5.0 rot = T.quaternion_about_axis(-len, axis) self._rotation = T.quaternion_multiply(rot, self._rotation) def getHeading(self): return self._heading def setHeading(self, new_angle): self._heading = new_angle self._front_vec = N.array((math.cos(new_angle), math.sin(new_angle), 0), dtype="f") def alterHeading(self, delta_angle): if delta_angle != 0: self.setHeading(self._heading + delta_angle) def advance(self, amount): if amount == 0: return r = N.cross(self._normal, self._front_vec) T.unit_vector(r, out=r) # vector facing front, parallel to the floor front = N.cross(r, self._normal) self._phys.impulse(front * amount) def getFrontVector(self): return self._front_vec def draw(self, scene): self._graphics.draw(scene, mmult(self._pos_m, T.quaternion_matrix(self._rotation), self._scaling)) def jump(self): if self._on_floor: self._phys._last_position = self._phys._position - self._normal * 0.1