File: test_placzek.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 (119 lines) | stat: -rw-r--r-- 4,056 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
"""
Test Placzek type resonant Raman implementations
"""
import pytest
from pathlib import Path

from ase.parallel import parprint, world
from ase.vibrations.vibrations import Vibrations
from ase.vibrations.resonant_raman import ResonantRamanCalculator
from ase.vibrations.placzek import Placzek, Profeta
from ase.calculators.h2morse import (H2Morse,
                                     H2MorseExcitedStates,
                                     H2MorseExcitedStatesCalculator)


def test_summary(testdir):
    atoms = H2Morse()
    rmc = ResonantRamanCalculator(atoms, H2MorseExcitedStatesCalculator)
    rmc.run()

    pz = Placzek(atoms, H2MorseExcitedStates)
    pz.summary(1.)


def test_names(testdir):
    """Test different gs vs excited name. Tests also default names."""
    # do a Vibrations calculation first
    atoms = H2Morse()
    vib = Vibrations(atoms)
    vib.run()
    assert '0x-' in vib.cache

    # do a Resonant Raman calculation
    rmc = ResonantRamanCalculator(atoms, H2MorseExcitedStatesCalculator,
                                  verbose=True)
    rmc.run()

    # excitation files should reside in the same directory as cache files
    assert (Path(rmc.name) / ('ex.eq' + rmc.exext)).is_file()

    # XXX does this still make sense?
    # remove the corresponding pickle file,
    # then Placzek can not anymore use it for vibrational properties
    key = '0x-'
    assert key in rmc.cache
    del rmc.cache[key]  # make sure this is not used

    om = 1
    gam = 0.1
    pz = Placzek(atoms, H2MorseExcitedStates,
                 name='vib', exname='raman')
    pzi = pz.get_absolute_intensities(omega=om, gamma=gam)[-1]
    parprint(pzi, 'Placzek')

    # check that work was distributed correctly
    assert len(pz.myindices) <= -(-6 // world.size)


def test_overlap(testdir):
    """Test equality with and without overlap"""
    atoms = H2Morse()
    name = 'rrmorse'
    nstates = 3
    with ResonantRamanCalculator(atoms, H2MorseExcitedStatesCalculator,
                                 exkwargs={'nstates': nstates},
                                 overlap=lambda x, y: x.overlap(y),
                                 name=name, txt='-') as rmc:
        rmc.run()

    om = 1
    gam = 0.1

    with Profeta(atoms, H2MorseExcitedStates,
                 exkwargs={'nstates': nstates}, approximation='Placzek',
                 overlap=True, name=name, txt='-') as po:
        poi = po.get_absolute_intensities(omega=om, gamma=gam)[-1]

    with Profeta(atoms, H2MorseExcitedStates,
                 exkwargs={'nstates': nstates}, approximation='Placzek',
                 name=name, txt=None) as pr:
        pri = pr.get_absolute_intensities(omega=om, gamma=gam)[-1]

    print('overlap', pri, poi, poi / pri)
    assert pri == pytest.approx(poi, 1e-4)


def test_compare_placzek_implementation_intensities(testdir):
    """Intensities of different Placzek implementations
    should be similar"""
    atoms = H2Morse()
    name = 'placzek'
    with ResonantRamanCalculator(atoms, H2MorseExcitedStatesCalculator,
                                  overlap=lambda x, y: x.overlap(y),
                                  name=name, txt='-') as rmc:
        rmc.run()

    om = 1
    gam = 0.1

    with Placzek(atoms, H2MorseExcitedStates,
                 name=name, txt=None) as pz:
        pzi = pz.get_absolute_intensities(omega=om, gamma=gam)[-1]
    print(pzi, 'Placzek')

    # Profeta using frozenset
    with Profeta(atoms, H2MorseExcitedStates,
                 approximation='Placzek',
                 name=name, txt=None) as pr:
        pri = pr.get_absolute_intensities(omega=om, gamma=gam)[-1]
    print(pri, 'Profeta using frozenset')
    assert pzi == pytest.approx(pri, 1e-3)

    # Profeta using overlap
    with Profeta(atoms, H2MorseExcitedStates,
                 approximation='Placzek', overlap=True,
                 name=name, txt=None) as pr:
        pro = pr.get_absolute_intensities(omega=om, gamma=gam)[-1]
    print(pro, 'Profeta using overlap')
    assert pro == pytest.approx(pri, 1e-3)