Thanks to visit codestin.com
Credit goes to pastebin.com

clopnaz

Untitled

Mar 31st, 2013
1,614
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.84 KB | None | 0 0
  1. import scipy as sp
  2. import scipy.integrate
  3. import numpy as np
  4. import matplotlib.pyplot as plt
  5. import functools
  6. import math
  7.  
  8. def sphere_volume(r):
  9.     return 4*math.pi*r**3/3
  10.  
  11. def full_pressure(v, t, p0, v0, t0):
  12.     return p0*v0*t/(t0*v)
  13.  
  14. def pressure_fn(p0, v0, t0, t):
  15.     return functools.partial(full_pressure, t=t, p0=p0, v0=v0, t0=t0)
  16.  
  17. class Bubble(object):
  18.     def __init__(self, s=0.0728, p0=101325., t0=20, r0=5, dr0=0.0, pi=lambda t:101325., ro=998.2071, nu=1.004, t=np.linspace(0,1,100)):
  19.         """
  20.        Creates a mathy object with method <method_name> to compute
  21.        the Rayleigh-Plesset equation.
  22.  
  23.        The default instance (without arguments) is suitable for an air
  24.        bubble in water at STP.
  25.        """
  26.         self.r0 = r0
  27.         self.dr0 = dr0
  28.         self.pb = pressure_fn(t0, p0, sphere_volume(r0), t0)
  29.         self.pi = pi
  30.         self.s = s
  31.         self.ro = ro
  32.         self.nu = nu
  33.         self.t = t
  34.         self.calculate()
  35.  
  36.     def calculate(self):
  37.         self.R = sp.integrate.odeint(self.dR, [self.r0, self.dr0], self.t)
  38.  
  39.     def dR(self, r, t):
  40.         """derivative function for R"""
  41.         #r[0] = R, r[1] = dR/dt, return [dR/dt=r[1], d2r/dt2=...]
  42.         dR = r[1]
  43.         #the math is messy and looks wrong so i'm splitting it up
  44.         #EDIT: I think it was right. I was just assuming if pinf=pb dr=0
  45.         term = [(self.pb(r[0])-self.pi(t))/(self.ro*r[0]),
  46.         3*r[1]**2/(r[0]*2),
  47.         4*self.nu*r[1]/r[0],
  48.         2*self.s/(self.ro*r[0])]
  49.         ddR = term[0]+term[1]+term[2]+term[3]
  50.         return np.array([dR, ddR])
  51.  
  52.  
  53.     def plot(self):
  54.         plt.subplot(2,1,1)
  55.         plt.plot(self.t, self.R[:,0])
  56.         plt.title('R(t)')
  57.         plt.subplot(2,1,2)
  58.         plt.plot(self.t, self.R[:,1])
  59.         plt.title('''dR(t)/dt''')
  60.         plt.show()
Advertisement
Add Comment
Please, Sign In to add comment