maxwell_energyΒΆ

For understanding Maxwell distribution.

Consider the Maxwell distribution with density 50 and bulk velocity 0 and thermal velocity 10 (unitless). The simplest way of generating Maxwell distribution is shown.

'''For understanding Maxwell distribution.

Consider the Maxwell distribution with density 50 and bulk velocity 0 and thermal velocity 10 (unitless).
The simplest way of generating Maxwell distribution is shown.

'''

import numpy as np
from numpy import random

import matplotlib.pyplot as plt

from irfpy.util import maxwell

def fe(m, n, vth):
    coe0 = 4 * np.sqrt(2) * np.pi * n
    coe1 = m ** (1.5)
    coe2 = (2 * np.pi * (vth ** 2)) ** 1.5
    coe3 = -1 / (m * vth**2)

    print(coe0, coe1, coe2, coe3)

    return lambda E: coe0 / coe1 / coe2 * np.sqrt(E) * np.exp(coe3 * E)


def main():
    '''Main routine.
    '''
    # A simple way to get 3-D maxwell distribution.
    ntest = 1e6  # 1,000,000 particles as a sample.
    vth = 5.
    m = 4.

    v = random.standard_normal((ntest, 3))  # Test particles.
    vvec = v * vth   # 10 is the thermal velocity, or standard deviation

    vabs = np.sqrt(vvec[:, 0] ** 2 + vvec[:, 1] ** 2 + vvec[:, 2] ** 2)
    ene = vabs * vabs * m / 2.

    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.hist(ene, range=[0, 500], bins=500)

    theory = fe(m, ntest, vth)

    xthe = np.linspace(0, 500, 500)
    ythe = np.array([theory(x) for x in xthe])
    dx = 1
    ythe = ythe * dx

    ax.plot(xthe, ythe)

    ax.set_xlabel('Energy (unitless)')
    ax.set_ylabel('Number of particle in 1 bin (dx=1)')
    ax.set_title('m=%g, vth=%g, n=%e' % (m, vth, ntest))
    
    fig.savefig('maxwell_energy.png')

    return vabs


if __name__ == '__main__':
    retval = main()