File: test_cli.py

package info (click to toggle)
python-dynasor 2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 22,008 kB
  • sloc: python: 5,263; sh: 20; makefile: 3
file content (72 lines) | stat: -rw-r--r-- 2,529 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import os
import pytest
import numpy as np
from tempfile import NamedTemporaryFile
from dynasor.trajectory import Trajectory
from dynasor.qpoints import get_spherical_qpoints
from dynasor.correlation_functions import compute_dynamic_structure_factors
from dynasor.post_processing import get_spherically_averaged_sample_binned
from dynasor.sample import read_sample_from_npz


@pytest.fixture
def filenames():
    this_dir = os.path.dirname(__file__)
    traj_fname = os.path.join(this_dir,
                              'trajectory_reader/trajectory_files/dump_long_with_velocities.xyz')
    index_fname = os.path.join(this_dir, 'trajectory_reader/trajectory_files/index_file_dump_long')
    return traj_fname, index_fname


def test_dynasor_CLI(filenames):
    # input parameters
    time_window = 8
    max_frames = 100000
    dt = 100

    q_max = 40
    q_bins = 20
    max_q_points = 10000

    traj_fname, index_fname = filenames

    tmpfile = NamedTemporaryFile(suffix='.npz', delete=False)

    # setup dynasor command
    flags = []
    flags.append(f'-f {traj_fname} -n {index_fname}')
    flags.append('--trajectory-format=extxyz')

    flags.append(f'--q-bins={q_bins}')
    flags.append(f'--q-max={q_max}')
    flags.append(f'--max-q-points={max_q_points}')

    flags.append(f'--time-window={time_window}')
    flags.append(f'--dt={dt}')
    flags.append(f'--max-frames={max_frames}')

    flags.append(f'--outfile={tmpfile.name}')
    flags.append('--calculate-incoherent')
    flags.append('--calculate-currents')

    flags_str = ' '.join(flags)

    # Run dynasor and read results
    command = 'dynasor ' + flags_str
    os.system(command)

    sample_cli = read_sample_from_npz(tmpfile.name)

    # Compute results with dynasor API
    traj = Trajectory(traj_fname, trajectory_format='extxyz', atomic_indices=index_fname)
    q_points = get_spherical_qpoints(traj.cell, q_max, max_q_points)
    sample = compute_dynamic_structure_factors(traj, q_points, dt=dt, window_size=time_window,
                                               calculate_currents=True, calculate_incoherent=True)
    sample_api = get_spherically_averaged_sample_binned(sample, num_q_bins=q_bins)

    # compare results
    assert np.allclose(sample_api.time, sample_cli.time)
    assert np.allclose(sample_api.omega, sample_cli.omega)
    assert np.allclose(sample_api.q_norms, sample_cli.q_norms)
    for key in sample_api.available_correlation_functions:
        assert np.allclose(getattr(sample_api, key), getattr(sample_cli, key))