File: test_units.py

package info (click to toggle)
neuron 8.2.6-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 34,760 kB
  • sloc: cpp: 149,571; python: 58,465; ansic: 50,329; sh: 3,510; xml: 213; pascal: 51; makefile: 35; sed: 5
file content (92 lines) | stat: -rw-r--r-- 2,785 bytes parent folder | download | duplicates (3)
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
from neuron import h


def switch_units(legacy):
    try:
        h.nrnunit_use_legacy(legacy)
    except:
        pass


def test_mod_legacy():
    s = h.Section()
    ut = h.UnitsTest(s(0.5))
    h.ion_style("na_ion", 1, 2, 1, 1, 0, sec=s)
    switch_units(1)
    h.finitialize()
    names = ["mole", "e", "faraday", "planck", "hbar", "gasconst"]
    legacy_hex_values = [
        "0x1.fe18fef60659ap+78",
        "0x1.7a4e7164efbbcp-63",
        "0x1.78e54cccccccdp+16",
        "0x1.b85f8c5445f02p-111",
        "0x1.18779454e3d48p-113",
        "0x1.0a10624dd2f1bp+3",
    ]
    for i, n in enumerate(names):
        val = eval("ut." + n)
        print("%s = %s (%s)" % (n, str(val.hex()), str(val)))
        legacy_value = float.fromhex(legacy_hex_values[i])
        assert val == legacy_value
    assert ut.avogadro == float.fromhex(legacy_hex_values[0])
    ghk_std = h.ghk(-50, 0.001, 10, 2)
    erev_std = h.nernst(s(0.5).nai, s(0.5).nao, 1)
    assert ut.ghk == ghk_std
    assert ut.erev == erev_std
    switch_units(0)
    h.finitialize()
    ghk_std = h.ghk(-50, 0.001, 10, 2)
    erev_std = h.nernst(s(0.5).nai, s(0.5).nao, 1)
    assert ut.mole != float.fromhex(legacy_hex_values[0])
    assert ut.e * ut.avogadro == ut.faraday
    assert abs(ut.faraday - h.FARADAY) < 1e-10
    assert ut.gasconst == h.R
    assert ut.k * ut.avogadro == ut.gasconst
    assert abs(ut.planck - ut.hbar * 2.0 * h.PI) < 1e-49
    assert ut.avogadro == h.Avogadro_constant
    assert ut.ghk == h.ghk(-50, 0.001, 10, 2)
    assert ut.erev == h.nernst(s(0.5).nai, s(0.5).nao, 1)


def test_hoc_legacy():
    switch_units(1)  # legacy
    print("R = %s" % str(h.R))
    print("FARADAY = %s" % str(h.FARADAY))
    celsius = 6.3
    ghk = h.ghk(30, 0.01, 10, 1)  # nernst requires a Section to get voltage
    print("ghk = %s" % str(ghk))

    assert h.R == 8.31441
    assert h.FARADAY == 96485.309
    assert ghk == -483.7914803097116

    switch_units(0)  # Modern
    print("R = %s" % str(h.R))
    print("FARADAY = %s" % str(h.FARADAY))
    ghk = h.ghk(30, 0.01, 10, 1)
    print("ghk = %s" % str(ghk))
    assert h.R == 8.31446261815324
    assert ghk == -483.8380971405879


def test_env_legacy():
    for i in [0, 1]:
        a = None
        try:  # test NRNUNIT_USE_LEGACY not whether we can successfully run a subprocess
            import sys, subprocess

            a = (
                "NRNUNIT_USE_LEGACY=%d %s -c 'from neuron import h; print (h.nrnunit_use_legacy())'"
                % (i, sys.executable)
            )
            a = subprocess.check_output(a, shell=True)
            a = int(float(a.decode().split()[0]))
        except:
            pass
        assert a == i


if __name__ == "__main__":
    test_mod_legacy()
    test_hoc_legacy()
    test_env_legacy()