################################ # Exponential Atmosphere exercise ################################ ################################ # Import Digital Material # # ListOfAtoms # Initializers # Transformers # Observers # NeighborLocators # BoundaryConditions # Potentials # Movers # ################################ import DigitalMaterial as DM import imp imp.reload(DM) ################################ # Dimension of space ################################ DM.dim = 3 ################################ # Transfer other libraries from DM ################################ scipy = DM.scipy RandomArray = DM.RandomArray vi = DM.vi # Visual Python pylab = DM.pylab ################################ # Set up pieces of simulation ################################ def ExponentialAtmosphereListOfAtoms(g=10.0, L=20, nAtoms=1000, temperature=30.0, mass=1.0, radius=0.5, color=vi.color.green, heightIndex=1, dimension = DM.dim): """ Put atoms of radius r in box (0,L)^dim, with probability decaying like Exp(-m g z / kB T) """ nAtomsToTry = int(2.0*nAtoms/(1.0-scipy.exp(-mass*g*L/temperature)) + 20) # # Build lots of extra atoms, prune those with z>L # positionsT = [] min = radius # Don't overlap with walls max = L-radius for i in range(dimension): if (i != heightIndex): positionsT.append(RandomArray.uniform(min, max, nAtoms)) else: atomsToTry=int(2*nAtoms/(1-scipy.exp(-mass*g*L/temperature)))+20 if atomsToTry < 1000000: # Truncate exponential distribution # # Exponential distribution, with minimum # height equal to atoms.radius (atoms repelled from floor) # posToTry = RandomArray.exponential( temperature/(mass*g),nAtomsToTry)+radius posOK = [pos for i, pos in enumerate(posToTry) if posL/2.] pylab.hist(upperHalfVelocities, bins=10, normed=1.0) pylab.plot(vs, rho, "k-", linewidth=2) pylab.title('Top half of box: Velocity distribution') pylab.xlabel('Velocity v') pylab.ylabel('Probability density rho(v)') pylab.show() def yesno(): response = input(' Continue? (y/n) ') if len(response)==0: # [CR] returns true return True elif response[0] == 'n' or response[0] == 'N': return False else: # Default return True def demo(): """Demonstrates solution for exercise: example of usage""" print("Exponential Atmosphere Demo: Ideal gas") print(" Ideal gas under gravity") atoms = IdealGasInGravity(nAtoms=500, T=30., L=20., g=10.0, atoms=None, nSteps=500, timeStep=0.01) if not yesno(): return print(" Position and velocity distributions") IdealGasDistributions(atoms, g=10, T=30., L=20.) if __name__=="__main__": demo() # Copyright (C) Cornell University # All rights reserved. # Apache License, Version 2.0