pcmj = p[j]-ptot*mj/mtot rrel = norm(pos[j]-pos[i]) pcmi = pcmi-2*np.dot(pcmi,rrel)*rrel # bounce in cm frame pcmj = pcmj-2*np.dot(pcmj,rrel)*rrel p[i] = pcmi+ptot*mi/mtot # transform momenta back to lab frame p[j] = pcmj+ptot*mj/mtot pos[i] = pos[i]+(p[i]/mi)*deltat # move forward deltat in time pos[j] = pos[j]+(p[j]/mj)*deltat # Bounce off the boundary of the torus for j in range(Natoms): poscircle[j] = norm(pos[j])*RingRadius*[1,1,0] outside = np.greater_equal(mag(poscircle-pos),RingThickness-2*Ratom) for k in range(len(outside)): if outside[k]==1 and np.dot(p[k], pos[k]-poscircle[k])>0: p[k] = reflection(p[k],pos[k]-poscircle[k]) # then update positions of display objects for i in range(Natoms): Atoms[i].pos(pos[i]) ### <-- outside = np.greater_equal(mag(pos), RingRadius+RingThickness) vp.render(resetcam=1) ### <-- vp.camera.Azimuth(.5) vp.camera.Elevation(.1) pb.print() vp.show()
# Example usage of addTrail() # from vtkplotter import Plotter, sin vp = Plotter(axes=6) s = vp.sphere(c='green', res=24) vp.cutPlane(s, [-0.9, 0, 0], showcut=True) #cut left part of sphere p = vp.point([1, 1, 1], r=12) # add a trail to point p with maximum length 0.5 and 50 segments p.addTrail(c='k', lw=3, maxlength=0.5, n=50) for i in range(200): p.pos([-2 + i / 100., sin(i / 5.) / 15, 0]) vp.render() vp.camera.Azimuth(-0.2) vp.show(resetcam=0)
#initial conditions v = vector(0, 0, 0.2) x = vector(x0, 0, 0) xr = vector(l_rest, 0, 0) sx0 = vector(-0.8, 0, 0) offx = vector(0, 0.3, 0) vp.box(pos=(0, -0.1, 0), length=2.0, width=0.02, height=0.5) #surface vp.box(pos=(-.82, .15, 0), length=.04, width=0.50, height=0.3) #wall block = vp.cube(pos=x, length=0.2, c='t') spring = vp.helix(sx0, x, r=.06, thickness=.01, texture='metal1') pb = ProgressBar(0, 500, c='r') for i in pb.range(): F = -k * (x - xr) - b * v # Force and friction a = F / m # acceleration v = v + a * dt # velocity x = x + v * dt + 1 / 2 * a * dt**2 # position block.pos(x) # update block position spring.stretch(sx0, x) # stretch helix accordingly trace = vp.point(x + offx, c='r/0.5', r=3) # leave a red trace vp.camera.Azimuth(.1) vp.camera.Elevation(.1) vp.render(trace) # add trace to the list of actors and render pb.print('Fx=' + str(F[0])) vp.show(interactive=1)
pedestal = vp.box([0,-0.63,0], height=.1, length=.1, width=1, texture='wood5') pedbase = vp.box([0,-1.13,0], height=.5, length=.5, width=.05, texture='wood5') pedpin = vp.pyramid([0,-.08,0], axis=[0,1,0], s=.05, height=.12, texture='wood5') formulas = vp.load('data/images/gyro_formulas.png', alpha=.9).scale(.003).pos([-1,-1,-1.1]) # ############################################################ the physics pb = ProgressBar(0, 4, dt, c='b') for i, t in enumerate(pb.range()): st, ct, sp, cp = sin(x[0]), cos(x[0]), sin(x[1]), cos(x[1]) thetadot, phidot, psidot = v # unpack atheta = st*ct*phidot**2 + (M*g*r*st-I3*(psidot+phidot*ct)*phidot*st)/I1 aphi = (I3/I1)*(psidot+phidot*ct)*thetadot/st - 2*ct*thetadot*phidot/st apsi = phidot*thetadot*st - aphi*ct a = vector(atheta, aphi, apsi) v += a*dt # update velocities x += v*dt # update Lagrangian coordinates gaxis = (Lshaft+0.03)*vector(st*sp, ct, st*cp) # set orientation along gaxis and rotate it around its axis by psidot*t degrees gyro.orientation(gaxis, rotation=psidot*t*57.3) if not i%200: # add trace and render all, every 200 iterations trace = vp.point(gaxis, r=3, c='r') vp.render(trace, resetcam=1) pb.print() vp.show(interactive=1)
vp = Plotter(interactive=0, axes=2, verbose=0) vp.xtitle = '' vp.ytitle = 'Psi^2(x,t)' vp.ztitle = '' bck = vp.load('data/images/schrod.png').scale(0.012).pos([0, 0, -.5]) barrier = vp.line(list(zip(x, V * 15)), c='dr', lw=3) lines = [] for j in range(200): for i in range(500): Psi += d_dt(Psi) * dt # integrate for a while A = np.real(Psi * np.conj(Psi)) * 1.5 # psi squared, probability(x) coords = list(zip(x, A)) Aline = vp.line(coords, c='db', tube=True, lw=.08) vp.show([Aline, barrier, bck]) lines.append(Aline) # now show the same lines along z representing time vp.clear() vp.camera.Elevation(20) vp.camera.Azimuth(20) for i, l in enumerate(lines): p = [0, 0, 20 * i / len(lines)] # shift along z vp.render([l.pos(p), barrier.clone().pos(p)], resetcam=True) vp.show(interactive=1)
vp.ztitle = '' bck = vp.load('data/images/schrod.png', alpha=.3).scale(.0255).pos([0, -5, -.1]) barrier = line(list(zip(x, V * 15, [0] * len(x))), c='black', lw=2) lines = [] for i in range(0, Nsteps): for j in range(500): Psi += d_dt(Psi) * dt # integrate for a while before showing things A = np.real(Psi * np.conj(Psi)) * 1.5 # psi squared, probability(x) coords = list(zip(x, A, [0] * len(x))) Aline = line(coords, c='db', lw=3) vp.show([Aline, barrier, bck]) lines.append([Aline, A]) # store objects # now show the same lines along z representing time vp.clear() vp.camera.Elevation(20) vp.camera.Azimuth(20) bck.alpha(1) for i in range(Nsteps): p = [0, 0, size * i / Nsteps] # shift along z l, a = lines[i] #l.pointColors(a, cmap='rainbow') l.pointColors(-a, cmap='gist_earth') # inverted gist_earth vp.render([l.pos(p), barrier.clone().alpha(.3).pos(p)], resetcam=1) vp.show(interactive=1)
R12 = Pos[s2] - Pos[s1] nR12 = np.linalg.norm(R12) d12 = Radius[s1] + Radius[s2] - nR12 tau = R12 / nR12 DR0 = d12 * tau x1 = Mass[s1] / (Mass[s1] + Mass[s2]) x2 = 1 - x1 # x2 = Mass[s2]/(Mass[s1]+Mass[s2]) Pos[s1] -= x2 * DR0 Pos[s2] += x1 * DR0 DV0 = 2 * dot(Vel[s2] - Vel[s1], tau) * tau Vel[s1] += x2 * DV0 Vel[s2] -= x1 * DV0 # Update the location of the spheres for s in range(Nsp): Spheres[s].pos([Pos[s][0], Pos[s][1], 0]) if not int(i) % 10: # every ten steps: rsp = [Pos[0][0], Pos[0][1], 0] rsv = [Vel[0][0], Vel[0][1], 0] p = torus(pos=rsp, axis=rsv, r=Rb / 4, thickness=Rb / 40, c='r', alpha=0.05) # leave an oriented trace vp.render(p) # add actor p and render scene pb.print('#actors=' + str(len(vp.actors))) vp.show(interactive=1)
from vtkplotter import printc, ProgressBar, Plotter N = 10 # nr of particles along axis s = 0.01 # random step size scene = Plotter(verbose=0, axes=0) scene.plane(pos=[.44, .44, -.1], texture='wood7') for i in range(N): # generate a grid of points for j in range(N): for k in range(N): p = [i / N, j / N, k / N] scene.point(p, c=p) # color point by its own position pb = ProgressBar(0, 80, c='red') for t in pb.range(): # loop of 400 steps pb.print() for i in range(1, N * N * N): # for each particle actor = scene.actors[i] r = [u(-s, s), u(-s, s), u(-s, s)] # random step p = actor.pos() # get point position q = p + r # add the noise if q[2] < 0: q[2] *= -1 # if bounce on the floor actor.pos(q) # set its new position scene.camera.Azimuth(.5) scene.camera.Roll(-.5) scene.render() scene.show(resetcam=0)
## Example # make a textured floor, a lamp post, and load a mesh of a car # make copies of the car, rotate and move them in a loop # vp.render() is used inside the loop, itadds the actor to list vp.actors, # rate=10 limits the speed of the loop to maximum 10 fps from __future__ import division, print_function from vtkplotter import Plotter, plane vp = Plotter(verbose=0, axes=0) vp.add(plane(pos=(4, 0, -.45), sx=12, texture='metalfloor1')) # load and set its position (methods can be concatenated) vp.load('data/shapes/lamp.vtk').pos([1.7, -0.4, 2]) a = vp.load('data/shapes/porsche.ply', c='r').rotateX(90) a.normalize() # set actor at origin and scale size to 1 print('Scene is ready, press q to continue') vp.show() for i in range(1, 10): b = a.clone(c='aqua', alpha=.04 * i) b.rotateX(-20 * i).rotateY(-10 * i).pos([i, i / 2, i / 2]) vp.render(b, rate=10) # add actor b, maximum frame rate in hertz print(i, 'time:', vp.clock, 's') vp.show()
# Make a video (needs ffmpeg) # Set offscreen=True to only produce the video without # any graphical window showing # from vtkplotter import Plotter # declare the class instance vp = Plotter(axes=0, offscreen=False) vp.load('data/shapes/spider.ply', texture='leather2', alpha=1) # open a video file and force it to last 3 seconds in total video = vp.openVideo(name='spider.mp4', duration=3) for i in range(100): vp.render() # render the scene first vp.camera.Azimuth(2) # rotate by 5 deg at each iteration video.addFrame() video.close() # merge all the recorded frames vp.show()