def test_quadratic1(): ''' Check if area under quadratic function for Trap Is approx equal to analytical solution (A/3)(b^3 - a^3) + (B/2)(b^2-a^2) + C(b-a) ''' A, B, C, a, b = 2, 4, 5, 0, 100 area1 = (A/3)*(b**3 + a**3) + (B/2)*(b**2 + a**2) + C*(b-a) area2 = trap(quadraticfunc, 0, 100, 1000) assert isclose(area1, area2) is True
def test_gensin1(): ''' Check if area under sin function for Trap Is approx equal to analytical solution sin(x) = cos(a) - cos(b) ''' A, w, a, b = 2, 4, 0, 100 area1 = (A/w)*(cos(w*a) - cos(w*b)) area2 = trap(gensinfunc, 0, 100, 1000, A=A, w=w) assert isclose(area1, area2, atol=0.05) is True
def test_genlinear1(): ''' Check if area under linear function for Trap Is approx equal to analytical solution (1/2)A(b^2 - a^2)+B(b-a) ''' A, B, a, b = 3, 4, 0, 100 area1 = .5*A*(b**2 - a**2) + B*(b-a) area2 = trap(genlinearfunc, 0, 100, 1000, A=A, B=B) assert isclose(area1, area2) is True
def test_sin1(): ''' Check if area under sin function for Trap Is approx equal to analytical solution sin(x) = cos(a) - cos(b) ''' a, b = 0, 100 area1 = cos(a) - cos(b) area2 = trap(sinfunc, 0, 100, 1000) assert isclose(area1, area2, atol=0.05) is True
def test_gensin2(): ''' Checks if area under curve computed for Trap func Is approximately equal to Trapz function from Numpy For f(x) = Asin(wx) - generalized ''' xvector = range(100) yvector = [] for i in xvector: yvector.append(gensinfunc(i, A=2, w=3)) area1 = trapz(yvector, x=xvector) x1, x2 = xvector[0], xvector[-1] step = len(xvector) area2 = trap(gensinfunc, x1, x2, step, A=2, w=3) assert isclose(area1, area2, atol=0.05) is True
def test_genquadratic2(): ''' Checks if area under curve computed for Trap func Is approximately equal to Trapz function from Numpy For f(x) = ax + b - generalized ''' xvector = range(100) yvector = [] for i in xvector: yvector.append(genquadraticfunc(i, A=2, B=2, C=2)) area1 = trapz(yvector, x=xvector) x1, x2 = xvector[0], xvector[-1] step = len(xvector) area2 = trap(genquadraticfunc, x1, x2, step, A=2, B=2, C=2) assert isclose(area1, area2) is True
def test_linear2(): ''' Checks if area under curve computed for Trap func Is approximately equal to Trapz function from Numpy For f(x) = ax + b ''' xvector = range(100) yvector = [] for i in xvector: yvector.append(linearfunc(i)) area1 = trapz(yvector, x=xvector) x1, x2 = xvector[0], xvector[-1] step = len(xvector) area2 = trap(linearfunc, x1, x2, step) assert isclose(area1, area2) is True
Np, A, B, vs = 2, 4, 1, 1 theta1, theta2 = 90, 300 print 'No input given. Taking the default values!' #theta1, theta2 = 90, 300 dim = 3 # dimensionality of the problem eta = 1.0/6 # viscosity of the fluid simulated a = 1 # radius of the particle k = vs/A # stiffness of the trap S0, D0 = 0.01, 0.01 # strength of the stresslet and potDipole ljrmin, ljeps = 3, .01 # lennard-jones parameters Tf, Npts = 400000, 5000 # final time and number of points # instantiate the class trap for simulating active particles in a harmonic potential rm = trap.trap(a, Np, vs, eta, dim, S0, D0, k, ljeps, ljrmin) # module to initialise the system. def initialConfig(rp0, trapCentre, theta, a, a0, vs, k, Np): '''initialise the system''' rr = np.pi*vs*a/k; #confinement radius t1 = np.pi/180 if Np==1: rp0[0], rp0[1], rp0[2] = 0, 0, 8 # particle 1 Position rp0[3], rp0[4], rp0[5] = 0, 0, -1 # Orientation elif Np==3: t1 = np.pi/180 trapCentre[0] = 0 trapCentre[1] = a0
""" Integral of (4 - x**2)**(1/2) from a = 0 and b = 2 computed by trapezoidal rule """ import math as mt from trap import trap def f(x): return (4 - x**2)**(1/2) a = 0 b = 2 for n in range(0, 2): er = 10. **(-n) Inew = trap(f, a, b, tol = er) print('tol=', er, 'Integral=', Inew) tol= 1.0 Integral= (2.9957090681024408+0.75j) tol= 0.1 Integral= (0.13601901796103003+0.001235730667189656j)
''' Parameters: Np= number of particles, vs=active velocity, k = spring constant of the trap, S0 = stresslet strength A = vs/k; B = S0/k''' try: Np, A, B, vs = int(sys.argv[1]), float(sys.argv[2]), float( sys.argv[3]), float(sys.argv[4]) except: Np, A, B, vs = 100, 4, 1, 1 print 'No input given. Taking the default values!' dim = 3 # dimensionality of the problem eta = 1.0 / 6 # viscosity of the fluid simulated a = 1 # radius of the particle mu = 1 / (6 * np.pi * eta * a) # particle mobility k = vs / A # stiffness of the trap S0 = B * k # strength of the stresslet D0 = 0.01 ljrmin, ljeps = 4.0, .01 # Lennard Jones Parameters Tf, Npts = 8000, 2000 # Final time and number of points on which integrator returns the data # instantiate the class trap for simulating active particles in a harmonic potential rm = trap.trap(a, Np, vs, eta, dim, S0, D0, k, ljeps, ljrmin) # initialise the system. Current version supports either Np=2 or initialization on a sphere or on a cube rm.initialise('sphere') # simulate the resulting system rm.simulate(Tf, Npts)
import trap a = -1 b = 1 h1 = 0.5 h2 = 0.25 qqq = trap.trap(a, b, h2) print("F =") print(qqq)