def __init__(self, physicalEnvironment): self.logger = logging.getLogger(name='Quadsim.Visualizer') self.physEnv = physicalEnvironment self.obj = set() self.simFrames = 0 self.objUpdates = 0 self.canvas = v.display(title='Simulation', width=1024, height=768, background=(0.2,0.2,0.2)) self.canvas.select() self.controls = vc.controls(x=640, y=0, range=20) self.infoLabel = v.label(display=self.controls.display,pos=(-10,-10), text='Click simulation to start', line=False) self.lastFPSCheckTime = None self.lastFPS = 0.0 self.simTime = 0.0 self.fpsValues = [] self.geomLookup = {}
import numpy as np import vpython as vp """ Plot the sine and cosine of the waves in 3-D """ xmax = 201 scene = vp.display(x=0, y=0, width=500, height=500, title="sin(6pi*x/201-t)", background=(1., 1., 1.), forward=(-0.6, -0.5, 1)) sinWave = vp.curve(x=range(0, xmax), color=color.yellow, radius=4.5) cosWave = vp.curve(x=range(0, xmax), color=color.red, radius=4.5) Xaxis = vp.curve(pos=[(-300, 0, 0), (300, 0, 0)], color=color.blue) incr = np.pi / xmax # x increment for t in range(0, 10, .02): # time loop for i in range(0, xmax): # loop through x values x = i * incr f = np.sin(6.0 * x - t) zz = np.cos(6.0 * x - t) yp = 100 * f xp = 200 * x - 300 zp = 100 * zz sinWave.x[i] = xp sinWave.y[i] = yp cosWave.x[i] = xp
import vpython as vp """ Determine the spherical Bessel functions by downward recursion, yo. """ Xmax = 40. Xmin = 0.25 step = 0.1 order = 10 start = 50 graph1 = vp.display(width=500, height=500, title="Spherical Bessel \ L=1 (red) 10", xtitle="x", xmin=Xmin, xmax=Xmax, ymin=0.2, ymax=0.5) funct1 = vp.gcurve(color=color.red) funct2 = vp.gcurve(color=color.green) def down(x, n, m): # Recursion woo! """ Moving downward in the pyramidal method we apply the Bessel equation to find the range of the Bessel function """ j = np.zeros((start + 2), float) # initialize zeros
import numpy as np import vpython as vp """ Solve the wave equation for a string of length L = 1m with its ends fixed and with the gently plucked initial conditions. """ # curve g = vp.display(width=600, height=300, title="Vibrating String") vibst = vp.curve(x=list(range(0, 100)), color=color.yellow) ball2 = vp.sphere(pos=(100, 0), color=color.red, radius=2) ball2 = vp.sphere(pos=(-100, 0), color=color.red, radius=2) ball2.pos ball2.pos vibst.radius = 1 # parameters rho = .01 # string density ten = 40 # string tension c = np.sqrt(ten / rho) # propogation speed c1 = c # Courant–Friedrichs–Lewy condition ratio = c * c / (c1 * c1) # initialize with initialize conditions xi = np.zeros((101, 3), float) for i in range(0, 81): xi[i, 0] = .00125 * i for i in range(81, 101): xi[i, 0] = .1 - .005 * (i - 80) for i in range(0, 100): vibst.x[i] = 2 * i - 100
# j+=1 # if r==4: # South # if j==0: # currentcolor = vpython.color.green # #continue # else: # currentcolor = vpython.color.blue # j-=1 if edgeflag==True: return [vpython.vector(i,j,0),edgecolor] else: return [vpython.vector(i,j,0),currentcolor] L = 11 # length of side of lattice i = j = 5 # starting position of particle -- center of lattice d = vpython.display(background=vpython.color.white) # for the animations, later #d.autoscale = False #d.background=vpython.color.white currentcolor = vpython.color.white position = vpython.vector(i,j,0) # 3-D coordinates of starting position s = vpython.sphere(pos=position,radius=1.5,color=currentcolor) steps = 100 for t in numpy.arange(steps): vpython.rate(10) # rate for visualization l = vpython.sphere(pos=position,radius=1,color=currentcolor) r = random.randint(1,4) #update position vector walk_list = walk(position,L,currentcolor) position = walk_list[0] currentcolor = walk_list[1] final = vpython.sphere(pos=position,radius=1.5,color=vpython.color.black)
================================================================================""" from __future__ import division, print_function #import visual as vs import vpython as vs # for 3D panel import wxPython as wx # for widgets # Draw window & 3D pane ================================================= win = vs.window(width=1024, height=720, menus=False, title='SIMULATE VPYTHON GUI') # make a main window. Also sets w.panel to addr of wx window object. scene = vs.display(window=win, width=830, height=690, forward=-vs.vector(1, 1, 2)) # make a 3D panel clr = vs.color vss = scene # Draw 3D model ====================== def axes(frame, colour, sz, posn): # Make axes visible (of world or frame). # Use None for world. directions = [ vs.vector(sz, 0, 0), vs.vector(0, sz, 0), vs.vector(0, 0, sz) ]
import numpy as np import vpython as vp from random import random """ Produce a 3-D graph of 3-D Random walk. It's a mathematical formalization of a path of random steps in succession. """ random.seed(123) jmax = 1000 xx = yy = zz = 0.0 graph1 = vp.display(x=0, y=0, width=600, height=600, title="3D Random Walk", forward=(-.6, -.5, -1)) # Create the curve pts = np.curve(x=list(range(0, 100)), radius=10.0, color=color.yellow) xax = np.curve(x=list(range(0, 1500)), color=color.red, pos=[(0, 0, 0), (1500, 0, 0)], radius=10.) yax = np.curve(x=list(range(0, 1500)), color=color.red, pos=[(0, 0, 0), (0, 1500, 0)], radius=10.) zax = np.curve(x=list(range(0, 1500)), color=color.red, pos=[(0, 0, 0), (0, 0, 1500)], radius=10.) xname = np.label(text="X", pos=(1000, 150, 0), box=0) yname = np.label(text="Y", pos=(-100, 1000, 0), box=0) zname = np.label(text="Z", pos=(100, 0, 1000), box=0) # Starting point pts.x[0] = pts.y[0] = pts.z[0] for i in range(1, 100): xx += (random() - 0.5)*2 yy += (random() - 0.5)*2 zz += (random() - 0.5)*2
print(r) return i, j def radius(i, j): dx = i - L // 2 dy = j - L // 2 distance = sqrt(dx**2 + dy**2) #print(dx,dy) return distance # Configuring view d = display(center=(L // 2, L // 2)) s = sphere() s.pos = L, L d.autoscale = False s.visible = False grid = zeros((L, L), bool) center = L // 2, L // 2 #50,50 add(*center) t = 0 r = 1 while r * 2 < L // 2: t += 1 i, j = circle(r) ri = 0
import vpython as vp from random import random """ Simulate the growth of ferns (fractial) in 3-dimensions. Simply beautiful. """ imax = 200000 # points x = .5 y = 0 z = -2 xn = 0 yn = 0 graph1 = vp.display(width=500, height=500, forward=(-1,1),\ title="3D Fractal Fern (rotate via right mosue button)", range=10) graph1.show_rendertime = True # Using points: cycle = 27 ms. render = 6 ms. # Using spheres: cycle = 270 ms. render = 30 ms. pts = points(color=color.green, size=0.01) for i in range(1, imax): r = random() if r <= .1: # 10% probability xn = 0 yn = .18 * y zn = 0 elif r > .1 and r <= .7: # 60% probability xn = .85 * xn yn = .85 * y + .1 * z + 1.6
color.cyan, color.magenta, color.green, ] SLEEP_SECONDS = 0.25 # seconds to delay among each simulation dt SAMPLING_RATE = 0.5 # dts to skip among each sampling sigma = 10 # maximum displacement applied when computing the particles movement # Window window = display( title=WINDOW_TITLE, autocenter=1, autoscale=1, width=WINDOW_WIDTH, height=WINDOW_HEIGHT, ) # Axis X axisX = curve(pos=[(-sigma, 0, 0), (sigma, 0, 0)], color=color.white, radius=CUBE_THICKNESS) # Axis Y axisY = curve( pos=[(0, -4 * sigma, 0), (0, 4 * sigma, 0)], color=color.white, radius=CUBE_THICKNESS, )
import numpy as np import vpython as vp """ Runge-Kutta method (rk4 algorithm) for determining solutions to the 1-Dimensional Schrödinger equation for bound-state energies. Now I ain't sayin she a Schrodinger, but she ain't messin with no old thinker, """ psigr = vp.display(x=0, y=0, width=600, height=300, title="R and L Wavefunction") Lwf = vp.curve(x=list(range(502)), color=color.red) Rwf = vp.curve(x=list(range(997)), color=color.yellow) eps = 1e-3 n_steps = 501 E = -17 # Idk I'm just guessing this energy. h = .04 count_max = 100 Emax = 1.1*E Emin = E/1.1 def f(x, y, F, E): """ Bound state energy function. """ F[0] = y[1] F[1] = -(.4829)*(E-V(x))*y[0] def V(x): """
import numpy as np import vpython as vp """ Solve Maxwell's equations with FDTD time-stepping for circularly polarized wave propogations in the z-direction in free space. """ scene = vp.display(x=0, y=0, width=600, height=400, range=200, title="Circular polarization: E field \ in white. H field in yellow") global phy, pyx max = 201 c = .01 # Courant stability condition # set arrays Ex = np.zeros((max + 2, 2), float) Hy = np.zeros((max + 2, 2), float) Ey = np.zeros((max + 2, 2), float) Hx = np.zeros((max + 2, 2), float) arrowcol = color.white Earrows = [] Harrows = [] for i in range(0, max, 10): Earrows.append(arrow(pos=(0, i - 100), axis=(0, 0, 0), color=arrowcol))
import vpython as vp import numpy as np """ Use the Numerov method to solve the 1-D time-independent Schrödinger equation for bound-state energies. People generally use the Runge-Kutta model method for solving ODEs (using a search routine for solving the eigenvalue problem). Numerov, on the other hand, is specialized for ODes not contaiing any first derivatives (so we can apply it to the Schrödinger equation). IT's not as general as rk4, but it's of O(h^6) so it's fast with more precision. """ psigr = vp.display(x=0, y=0, width=600, height=300, title="R and L Wave Functions") psi = vp.curve(x=list(range(0, 1000)), display=psigr, color=color.yellow) psi2gr = vp.display(x=0, y=300, width=600, height=300, title="Wave func^2") psio = vp.curve(x=list(range(0, 1000)), color=color.magenta, display=psi2gr) energr = vp.display(x=0, y=500, width=600, height=200, title="Potential and E") poten = vp.curve(x=list(range(0, 1000)), color=color.cyan, display=energr) autoen = vp.curve(x=list(range0, 1000)), display=energr) dl = 1e-6 # interval to stop bisection ul = np.zeros([1501], float) # u value for left side ur = np.zeros([1501], float) # and the right side k2l = np.zeros([1501], float) # k**2 Schrodinger equation left wavefunction k2r = np.zeros([1501], float) # k**2 S. E. right wavefunction n = 1501 m = 5 # plot every 5 points imax = 100 # number of iterations xl0 = -1000 xr0 = 1000 h = (1*(xr0-xl0)/(n-1)) # h constant
import vpython as vp import numpy as np """ Animated visualizations of the Schrodinger's equation solutions """ # wave function, probability, potential dx = 0.04 dx2 = dx**2 k0 = 5.5 * np.pi dt = dx2 / 20.0 xmax = 6.0 g = vp.display(width=500, height=250, title="Wave packet in harmonic oscillator potential") PlotObj = vp.curve(x=xs, color=color.yellow, radius=0.1) g.center = (0, 2, 0) psr = np.exp(-.5 * (xs / .5)**2) * np.cos(k0 * xs) psi = np.exp(-.5 * (xs / .5)**2) * np.sin(k0 * xs) v = 15.0 * xs**2 while True: vp.rate(500) psr[1:-1] = psr[1:-1] - (dt / dx2) * ( psi[2:] + psi[:-2] - 2 * psi[1:-1]) + dt * v[1:-1] * psi[1:-1] psi[1:-1] = psi[1:-1] - (dt / dx2) * ( psr[2:] + psr[:-2] - 2 * psr[1:-1]) + dt * v[1:-1] * psr[1:-1] PlotObj.y = 4 * (psr**2 + psi**2)
import numpy as np import vpython as vp """ Finite-Difference Time Domain Method of findings solutions to Maxwell's equations for linearly polarized wave propogation in the z-direction in free space. """ xmax = 201 ymax = 100 zmax = 100 scene = vp.display(x=0, y=0, width=800, height=500, title="E: cyan, H: red. Periodic Boundary conditions", forward=(-.6,-.5,-1)) Efield = vp.curve(x=list(range(0, xmax), color=color.cyan, radius=1.5, display=scene)) Hfield = vp.curve(x=list(range(0, xmax), color=color.cyan, radius=1.5, display=scene)) vplane = vp.curve(pos=[(-xmax, ymax), (xmax, ymax), (xmax, -ymax), (-xmax, -ymax), (-xmax, ymax)], color=color.cyan) zaxis = vp.curve(pos=[(-xmax, 0), (xmax, 0)], color=color.magenta) hplace = vp.curve(pos=[(-xmax, 0, zmax), (xmax, 0, zmax), (xmax, 0, -zmax), (-xmax, 0, -zmax), (-xmax,0, zmax)], color=color.magenta) ball1 = vp.sphere(pos =(xmax+30, 0, 0), color=color.black, radius=2) ts = 2 beta = .01 # initialize arrays Ex = np.zeros((xmax, ts), float) Hy = np.zeros((xmax, ts), float) # label Exlabel1 = label(text="Ex", pos=(-xmax-10, 50), box=0)