def gibbsDist(): fig, ax = plt.subplots(1, 1) mean, var, skew, kurt = maxwell.stats(moments='mvsk') x = np.linspace(maxwell.ppf(0.01), maxwell.ppf(0.99), 100) ax.plot(x, maxwell.pdf(x), 'r-', lw=5, alpha=0.6, label='maxwell pdf') ax.legend(loc='best', frameon=False) plt.show()
def set_initial_speeds(self) -> np.ndarray: """ Function for sampling initial speeds for the atoms from the maxwell-boltzman distribution. Here this is achieved by means of the 'Inverse CDF sampling' method where sampling N points from a given PDF is similar to evaluating its inverse CDF on N uniformly distributed points (in Uniform(0,1)). Returns: -------- speeds: np.ndarray of shape (nr_particles,) containing floats """ PARENT_SAMPLE_SIZE = 1000000 u = np.random.uniform(low=0, high=1, size=PARENT_SAMPLE_SIZE) scale_factor = np.sqrt(consts.k * self.temperature / (self.mass)) # sqrt(kT/m) speeds = maxwell.ppf(q=u, scale=scale_factor) # ppf is inverse cdf in scipy return speeds[:self.nr_particles]
def gen_velocities(v_0, v_Esc, v_E, num_object): """ Generates a list of velocities """ cdf_v_Esc = maxwell.cdf(v_Esc, scale=v_0 / np.sqrt(2)) velocity_r = maxwell.ppf(np.random.rand(num_object) * cdf_v_Esc, scale=v_0 / np.sqrt(2)) # kpc/yr velocity_theta = np.arccos(1 - 2 * np.random.rand(num_object)) velocity_phi = 2 * np.pi * np.random.rand(num_object) velocity = np.zeros((3, num_object)) velocity[0] = velocity_r * np.sin(velocity_theta) * np.cos(velocity_phi) velocity[1] = velocity_r * np.sin(velocity_theta) * np.sin(velocity_phi) velocity[2] = velocity_r * np.cos(velocity_theta) return velocity.T # (N, 3)
# for a particle # In[8]: temp = 400 kb = 1.380649e-23 # In[9]: mean, var, skew, kurt = maxwell.stats(moments ='mvsk') fig, ax = plt.subplots(1, 1) x = np.linspace(maxwell.ppf(0.01),maxwell.ppf(0.99), 100) ax.plot(x, maxwell.pdf(x), 'r-', lw=4, alpha=0.6, label='maxwell pdf') plt.title('1D Maxwell-Boltzmann Distribution') plt.xlabel('Speed (m/s)') # fix this plt.ylabel('Probability Density (s/m)') # In[10]: m = 2*kb*temp/(np.pi*float(mean)**2) print('At STP, \nmass of particle = %s kg' % (m)) # In[11]:
def hobbs_vdist2(x): return maxwell.ppf(x)*265./10.
from scipy.stats import maxwell import matplotlib.pyplot as plt fig, ax = plt.subplots(1, 1) # Calculate a few first moments: mean, var, skew, kurt = maxwell.stats(moments='mvsk') # Display the probability density function (``pdf``): x = np.linspace(maxwell.ppf(0.01), maxwell.ppf(0.99), 100) ax.plot(x, maxwell.pdf(x), 'r-', lw=5, alpha=0.6, label='maxwell pdf') # Alternatively, the distribution object can be called (as a function) # to fix the shape, location and scale parameters. This returns a "frozen" # RV object holding the given parameters fixed. # Freeze the distribution and display the frozen ``pdf``: rv = maxwell() ax.plot(x, rv.pdf(x), 'k-', lw=2, label='frozen pdf') # Check accuracy of ``cdf`` and ``ppf``: vals = maxwell.ppf([0.001, 0.5, 0.999]) np.allclose([0.001, 0.5, 0.999], maxwell.cdf(vals)) # True # Generate random numbers:
v = [] for i in range(n): # Posición inicial pos = (rd.uniform(-1,1)*L/2,rd.uniform(-1,1)*L/2,rd.uniform(-1,1)*L/2) # Velocidad inicial cos_theta = rd.uniform(-1,1) phi = rd.uniform(0,2*np.pi) theta = np.arccos(cos_theta) f_inv_cdf = rd.uniform(0,1) x = maxwell.ppf(f_inv_cdf) v = x/escala v_x = v*np.sin(theta)*np.cos(phi) v_y = v*np.sin(theta)*np.sin(phi) v_z = v*np.cos(theta) bola = vs.sphere(pos=np.array(pos),radius=r,color=(0,0,1),v=np.array([v_x,v_y,v_z])) bolas.append(bola) # Funciones a utilizar def centro_de_masas(bola1,bola2):
def get_v_maxwell(m, T): """Draw velocity from Maxwell-Boltzmann distribution with mean 0.""" s = np.sqrt(T / m) x_rand = np.random.random(1) return maxwell.ppf(x_rand, loc=0., scale=s)