Coding excersizes for my statistical mechanics class.
Harmonic Oscillator MD simulation for homework.
Two Level System Project #1
matplotlib
numpy
Here, the Velocity Verlet algorithm is used to numerically solve for a harmonic oscillator's point in phase space at a given time. w and m are set to 1 for simplicity.
For this simulation,
After calculating
The results are compared to the analytical solution for the classical harmonic oscillator, which is
The error of the simulation oscilate within +/-0.1 of the analytical values, and the total energy is conserved, barring tiny fluctuations.
Run with ./MD.py
matplotlib
numpy
rich (for progress bars)
This code calculates entropy by
A plot of the predicted occupancies of each level vs. T is also available. It demonstrates that because
A Monte Carlo simulation is also available. The probabilities for occupying a particular energy level is given by
Decimal types are used instead of floats because floats have very poor precision at the small scales required for this code, and Decimals allow arbitrary precision for arithmetic. This was especially important for the slope calculations for the
For the Monte Carlo simulation, if a particle does not change it's level from the one it was initialized at, the entropy and energy calculations are skipped, because they would yield the same results as the previously calculated value. This increases the performance greatly, especially for large systems at low temperatures (initialized at
The exact value of
Here the entropy of a 2-level system can be explored by exposing customizable system sizes, energy levels and temperatures to the user.
Available calculations are the -p, -s, and -m flags, respectively.
Here is an example of the Monte Carlo simulation with 100,000 particles. Energy levels and temperature are the default.
./levels.py -mn 100000
Determining populations of energy levels: ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 100% 0:00:27
The number of particles in the upper level is 43920 out of 100000 particles.
This ratio is 0.4392, compared to the expected ratio of 0.4403.
The numerically determined temperature is 24.55 K, compared to the expected temperature of 25 K.
We can also specify that the simulation start with all particles in the upper level. We can also use a negative temperature.
./levels.py -m -n 100000 -t -25 -l 1
Determining populations of energy levels: ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 100% 0:00:26
The number of particles in the upper level is 55977 out of 100000 particles.
This ratio is 0.5598, compared to the expected ratio of 0.5597.
The numerically determined temperature is -24.97 K, compared to the expected temperature of -25 K.a
The same rules are being applied in any of the above cases, which shows how negative temperatures can come about in cases of the system being initialized in a configuration inaccessable by thermal excitation.
The program uses the sterling approximation by default to improve performance.
A the default system with 10,000 particles completed instantaneously when only the approximation (./levels.py -st 250).
In addition to the default, the exact value of -x flag. The sterling approximation will be used if an overflow error is encountered. The transition point between the two methods can be seen with the following example due to the small kink formed in the
./levels.py -sxt 250
Calculating S(E) curve: ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 100% 0:00:04
We can also see how reducing the gap between the energy levels allows the populations to approximately equalize at a lower temperature.
./levels.py -p
./levels.py -pe 4 6
The view can be extended further with the -t flag.
See the help, ./levels.py -h for more information.






