def restricted_3body(y): # y = [r, v] expected testbody = set_scene(y[0]) t, h = 0.0, 0.001 while True: vp.rate(2000) y = ode.RK4(r3body, y, t, h) testbody.pos = y[0]
def go(vx=5., vy=5.): # default velocity=(5,5) Y = [0., 0., vx, vy] # initial values, [x,y,vx,vy] t, h, x, y = 0., .01, [], [] # time, step size, temp arrays while Y[1] >= 0.0: # loop as long as y>=0 x.append(Y[0]), y.append(Y[1]) # record pos. Y, t = ode.RK4(Lin_drag, Y, t, h), t + h # update Y, t plt.figure() # open fig, plot, label, show fig plt.plot(x, y), plt.xlabel('x (m)'), plt.ylabel('y (m)') plt.show()
def go(x, y, vx, vy): # motion with full drag and spin effects h, t, Y = 0.01, 0., np.array([[x, y, 0.], [vx, vy, 0.]]) # initialize while (Y[0, 0] < R and Y[0, 1] > 0.2): # before homeplate&above ground vp.rate(40) t, Y = t + h, ode.RK4(baseball, Y, t, h) # integrate ball.pos, spin.pos = Y[0], Y[0] - offset # move ball, arrow spin.rotate(angle=phi), ball.rotate(angle=phi, axis=omega) #spin trail.append(pos=ball.pos) ideal.append(pos=(x + vx * t, y + vy * t - 0.5 * g * t * t, 0.)) # ideal case while (not scene.kb.keys): # check for key press vp.rate(40) spin.rotate(angle=phi), ball.rotate(angle=phi, axis=omega) scene.kb.getkey() # clear key trail.append(pos=(0, 0, 0), retain=0) # reset trails ideal.append(pos=(0, 0, 0), retain=0)
import ode initial_nuclei = 10000.0 decay_constant = 0.2 def decay(t, y): return -decay_constant * y[0] print('Solving using Euler method......') for i in [x for x in ode.Euler([decay], 0.0, [initial_nuclei], 0.1, 50.0)]: print(','.join([str(x) for x in i])) print('') print('Solving using RK4 method......') for i in [x for x in ode.RK4([decay], 0.0, [initial_nuclei], 0.1, 50.0)]: print(','.join([str(x) for x in i])) print('') print('Solving using RK3 method......') for i in [x for x in ode.RK3([decay], 0.0, [initial_nuclei], 0.1, 50.0)]: print(','.join([str(x) for x in i])) print('') print("Solving using Heun's method......") for i in [x for x in ode.Heun([decay], 0.0, [initial_nuclei], 0.1, 50.0)]: print(','.join([str(x) for x in i])) print('') print('Solving using RK4 method with 3/8 rule......') for i in [x for x in ode.RK38([decay], 0.0, [initial_nuclei], 0.1, 50.0)]:
resurrected = resurect * y[2] destroyed = destroy * y[0] * y[1] return newly_infected + resurrected - destroyed def dead(t, y): natural_death = death * y[0] destroyed_zombies = destroy * y[0] * y[1] created_zombies = resurect * y[2] return natural_death + destroyed_zombies - created_zombies def modifying_expression(y, step): y[0] = y[0] + (5 * step) return y lower_bound = {'2': [0.0, 0.0]} upper_bound = {'0': [700, 700]} f = [human, zombie, dead] # system of ODEs y = [500.0, 0, 0] # initial human, zombie, death population respectively print('Solving using 4th order Runge Kutta method ......') for i in [ x for x in ode.RK4(f, 0.0, y, 0.1, 100.0, modifying_expression, lower_bound, upper_bound) ]: print(','.join([str(z) for z in i])) print('')
f[1:, :] -= ftop # below, left: use 3rd law f[:, 1:] -= fright a = (f - damp * v) / mass + gvec v[0, 0], v[0, -1], v[-1, 0], v[-1, -1] = 0, 0, 0, 0 # fixed coners return np.array([v, a]) L, M, N = 2.0, 15, 15 # size, (M,N) particle array h, mass, damp = 0.01, 0.004, 0.01 # keep damp between [.01,.1] x, y = np.linspace(0, L, M), np.linspace(0, L, N) # particle grid r, v = np.zeros((N, M, 3)), np.zeros((N, M, 3)) spring_k, spring_l = 50.0, x[1] - x[0] # spring const., relaxed length r[:, :, 0], r[:, :, 1] = np.meshgrid(x, y) # initialize pos Y, gvec = np.array([r, v]), np.array([0, 0, -9.8]) # [v,a], g vector scene = vp.display(title='Tablecloth', background=(.2, .5, 1), up=(0, 0, 1), center=(L / 2, L / 2, -L / 4), forward=(1, 2, -1)) vp.points(pos=[(0, 0, 0), (0, L, 0), (L, L, 0), (L, 0, 0)], size=50) # corners x, y, z = r[:, :, 0], r[:, :, 1], r[:, :, 2] # mesh points net = vpm.net(x, y, z, vp.color.yellow, 0.005) # mesh net mesh = vpm.mesh(x, y, z, vp.color.red, vp.color.yellow) while (1): vp.rate(100), vpm.wait(scene) # pause if key pressed Y = ode.RK4(cloth, Y, 0, h) x, y, z = Y[0, :, :, 0], Y[0, :, :, 1], Y[0, :, :, 2] net.move(x, y, z), mesh.move(x, y, z)
def CustomModel(CustomName, IntInput, InfRateInput, RecoveryInput, TimeInput): #Custom Model global r_RI, r_SI Name = CustomName print() PopInput = int(input("Enter an amount for the total population: ")) print() SusInput = int( input("Enter an amount for the initial susceptible population: ")) print() InfInput = int( input("Enter an amount for the initial infected population: ")) print() print( "As of right now, this custom model does not support any carrier modifications. Maybe that will change in the future when a more advanced version of this code is created..." ) print() #Initial Population Counts TotalPop = PopInput #Total Population SusPop = SusInput #Initial Susceptible Population InfPop = InfInput #Initial Infected Population TotalInteractions = IntInput #number of daily interactions an infected person has with the total population SusceptibleInteractions = InfRateInput #how many interactions between an infected and susceptible people it takes to result in infection RI = 1 / RecoveryInput #Recovery Rate (1/length of time it takes a person to recover) Time = TimeInput #Time, in days, the disease is being modelled over #Time Setup t = 0 #initial time dt = 0.01 #change in time #Redefining Variables for S-I-R Formula N = TotalPop #total population S = SusPop #initial amount of susceptible people I = InfPop #initial amount of infected people R = 0 #initial amount of recovered people f = TotalInteractions #number of daily interactions an infected person has with the total population Ptrans = 1 / SusceptibleInteractions #fraction of interactions between an infected person and a susceptible person that results in spread r_SI = ( (f * Ptrans) / N ) #transmission rate, how likely it is for an infected person to spread the disease r_RI = RI #recovery rate, time it takes for a person to recover #Creating lists for each variable in SIR formula tmodel = [] Smodel = [] Imodel = [] Rmodel = [] data = np.array([S, I, R]) #Loop to actually run the Euler/RK method differential equations while t < Time: #Selecting which model to use #data=ode.Euler(rates,data,t,dt) #Uses Euler Model #data=ode.RK2(rates,data,t,dt) #Uses Runge-Kutta 2 Model data = ode.RK4(rates, data, t, dt) #Uses Runge-Kutta 4 Model #Advancing time t = t + dt #Appending each variable list tmodel.append(t) Smodel.append(data[0]) Imodel.append(data[1]) Rmodel.append(data[2]) #Plotting Data fig = plt.figure(figsize=(10, 6)) plt.title("SIR Model of Disease Spread for " + str(Name)) plt.xlabel("Time (days)") plt.ylabel("Population") plt.plot(tmodel, Smodel, 'b-', label='Susceptible Population') plt.plot(tmodel, Imodel, 'r-', label='Infected Population') plt.plot(tmodel, Rmodel, 'g-', label='Recovered Population') plt.legend(loc="best") plt.grid() plt.show()
def ViDmModel(CarrierInput, IntInput, InfRateInput, RecoveryInput, TimeInput): #Model for Aldridge Village Dormatory global r_RI, r_SI Name = "Aldridge Village Dormatory" Shorthand = "Village" TotalPop = 250 #Total Population SusPop = 249 #Initial Susceptible Population InfPop = 1 #Initial Infected Population TotalInteractions = IntInput #number of daily interactions an infected person has with the total population SusceptibleInteractions = InfRateInput #how many interactions between an infected and susceptible people it takes to result in infection RI = 1 / RecoveryInput #Recovery Rate (1/length of time it takes a person to recover) Time = TimeInput #Time, in days, the disease is being modelled over #Carrier Stuff #List of Potential Carriers of the Disease and how they Impact the Spread Carrier = float(CarrierInput) BirdMod = 0.2 RodentMod = 0.4 InsectMod = 0.6 BloodMod = 0.5 SalivaMod = 0.2 TouchMod = 0.7 AirMod = 0.3 WaterMod = 0.4 NoMod = 0.0 #If-statements determine which Carrier is in use and assigns the proper impact on the spread if Carrier == 1: CarrierMod = BirdMod if Carrier == 2: CarrierMod = RodentMod if Carrier == 3: CarrierMod = InsectMod if Carrier == 4: CarrierMod = BloodMod if Carrier == 5: CarrierMod = SalivaMod if Carrier == 6: CarrierMod = TouchMod if Carrier == 7: CarrierMod = AirMod if Carrier == 8: CarrierMod = WaterMod if Carrier == 9: CarrierMod = NoMod #Assigns the impact a carrier as on the spread, as well as determines how much, as a percentage, the carrier increased the spread CarrierImpact = CarrierMod #Time Setup t = 0 #initial time dt = 0.01 #change in time #Redefining Variables for S-I-R Formula N = TotalPop #total population S = SusPop #initial amount of susceptible people I = InfPop #initial amount of infected people R = 0 #initial amount of recovered people f = TotalInteractions #number of daily interactions an infected person has with the total population Ptrans = 1 / SusceptibleInteractions #fraction of interactions between an infected person and a susceptible person that results in spread r_SI = ( (f * Ptrans) / N ) * CarrierImpact #transmission rate, how likely it is for an infected person to spread the disease r_RI = RI #recovery rate, time it takes for a person to recover #Creating lists for each variable in SIR formula tmodel = [] Smodel = [] Imodel = [] Rmodel = [] data = np.array([S, I, R]) #Loop to actually run the Euler/RK method differential equations while t < Time: #Selecting which model to use #data=ode.Euler(rates,data,t,dt) #Uses Euler Model #data=ode.RK2(rates,data,t,dt) #Uses Runge-Kutta 2 Model data = ode.RK4(rates, data, t, dt) #Uses Runge-Kutta 4 Model #Advancing time t = t + dt #Appending each variable list tmodel.append(t) Smodel.append(data[0]) Imodel.append(data[1]) Rmodel.append(data[2]) #Plotting Data fig = plt.figure(figsize=(10, 6)) plt.title( "SIR Model of a Disease across Different Buildings on HPU's Campus: " + str(Shorthand)) plt.xlabel("Time (days)") plt.ylabel("Population") plt.plot(tmodel, Smodel, 'b-', label='Susceptible Population') plt.plot(tmodel, Imodel, 'r-', label='Infected Population') plt.plot(tmodel, Rmodel, 'g-', label='Recovered Population') plt.legend(loc="best") plt.grid() plt.show()