File: test_idealgas.py

package info (click to toggle)
python-ase 3.22.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 14,344 kB
  • sloc: python: 126,379; xml: 946; makefile: 111; javascript: 47
file content (34 lines) | stat: -rw-r--r-- 1,142 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import pytest
import numpy as np
from ase.md import VelocityVerlet
from ase.build import bulk
from ase.units import kB
from ase.md.velocitydistribution import MaxwellBoltzmannDistribution
from ase.calculators.idealgas import IdealGas


def test_idealgas():
    rng = np.random.RandomState(17)
    atoms = bulk('Kr').repeat((10, 10, 10))
    assert len(atoms) == 1000

    atoms.center(vacuum=100)
    atoms.calc = IdealGas()
    natoms = len(atoms)

    md_temp = 1000

    MaxwellBoltzmannDistribution(atoms, temperature_K=md_temp, rng=rng)
    print("Temperature: {} K".format(atoms.get_temperature()))

    with VelocityVerlet(atoms, timestep=0.1) as md:
        for i in range(5):
            md.run(5)
            stress = atoms.get_stress(include_ideal_gas=True)
            stresses = atoms.get_stresses(include_ideal_gas=True)
            assert stresses.mean(0) == pytest.approx(stress)
            pressure = -stress[:3].sum() / 3
            pV = pressure * atoms.cell.volume
            NkT = natoms * kB * atoms.get_temperature()
            print(f"pV = {pV}  NkT = {NkT}")
            assert pV == pytest.approx(NkT, abs=1e-6)