File: test_variables.py

package info (click to toggle)
abinit 9.10.4-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 518,712 kB
  • sloc: xml: 877,568; f90: 577,240; python: 80,760; perl: 7,019; ansic: 4,585; sh: 1,925; javascript: 601; fortran: 557; cpp: 454; objc: 323; makefile: 77; csh: 42; pascal: 31
file content (226 lines) | stat: -rw-r--r-- 9,206 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# coding: utf-8
"""Tests abiref.bib file."""
from __future__ import division, print_function, unicode_literals, absolute_import

from .tools import patch_syspath, AbimkdocsTest
patch_syspath()

import os
import json

from collections import OrderedDict
from pprint import pprint
from abimkdocs.variables import get_codevars, ValueWithUnit, ValueWithConditions, MultipleValue, Range


class VariablesTest(AbimkdocsTest):

    def test_variables(self):
        codevars = get_codevars()
        assert codevars is get_codevars()
        assert "abinit" in codevars
        abinit_vars = codevars["abinit"]
        assert "ecut" in abinit_vars
        ecut = abinit_vars["ecut"]
        assert ecut == ecut
        assert ecut != None
        assert ecut.name == "ecut"
        assert ecut.vartype == "real"
        assert not ecut.isarray
        assert ecut != abinit_vars["pawecutdg"]
        assert ecut in {ecut: "foo"}
        assert ecut.executable == "abinit"
        assert ecut.wikilink == "[[abinit:ecut]]"
        assert not ecut.depends_on_dimension("ntypat")
        assert not ecut.is_internal
        assert abinit_vars["mpw"].is_internal
        acell = abinit_vars["acell"]
        assert acell.isarray
        assert not acell.depends_on_dimension("ntypat")
        xred = abinit_vars["xred"]
        assert xred.isarray and xred.varset == "basic" and "[[EVOLVING]]" in xred.characteristics
        assert xred.depends_on_dimension("natom")
        assert xred.depends_on_dimension("natrd")
        assert len(acell.dimensions) == 1 and acell.dimensions[0] == 3
        abinit_asr = abinit_vars["asr"]
        anaddb_asr = codevars["anaddb"]["asr"]
        assert abinit_asr.executable == "abinit"
        assert abinit_asr.name == "asr"
        assert anaddb_asr.executable == "anaddb"
        assert anaddb_asr.name == "asr"
        assert anaddb_asr.wikilink == "[[anaddb:asr]]"
        assert anaddb_asr != abinit_asr
        symsigma_parents = abinit_vars["symsigma"].get_parent_names()
        assert len(symsigma_parents) > 0 and "optdriver" in symsigma_parents

        # The text of this variable contaings greek symbols in HTML.
        var = abinit_vars["cd_frqim_method"]
        repr(var); str(var)

        # Test "tricky" variables.
        fxcartfactor = codevars["abinit"]["fxcartfactor"]
        str(fxcartfactor)
        assert fxcartfactor.to_abimarkdown()
        d = fxcartfactor.topic2relevances
        assert fxcartfactor.topic2relevances is d and len(d) == 2
        assert "expert" in d["TransPath"] and "expert" in d['GeoOpt']
        assert isinstance(fxcartfactor.defaultval, ValueWithUnit)
        assert fxcartfactor.defaultval.units == "(Bohr^2)/Hartree"
        assert fxcartfactor.defaultval.value == 1

        iomode = codevars["abinit"]["iomode"]
        str(iomode)
        assert iomode.to_abimarkdown()
        assert len(iomode.characteristics) == 1 and iomode.characteristics[0] == "[[DEVELOP]]"
        assert isinstance(iomode.defaultval, ValueWithConditions)
        # TODO: waiting for new version dict-based.
        #assert iomode.defaultval["defaultval"] == 0
        #assert iomode.defaultval['[[MPI_IO]] and [[paral_kgb]]==1'] == 1

        istwfk = codevars["abinit"]["istwfk"]
        assert istwfk.to_abimarkdown()
        assert istwfk.characteristics is None and istwfk.vartype == "integer" and istwfk.varset == "dev"
        assert istwfk.dimensions == ["[[nkpt]]"]
        assert isinstance(istwfk.defaultval, MultipleValue)
        #assert istwfk.defaultval.number is None
        #assert istwfk.defaultval.value == 0
        assert istwfk.requires is None
        assert "nkpt" in istwfk.get_parent_names()

        jdtset = codevars["abinit"]["jdtset"]
        assert isinstance(jdtset.defaultval, Range)
        #assert jdtset.defaultval.start == 1
        #assert jdtset.defaultval.stop == "[[ndtset]]"

        # Abipy test
        # Database methods.
        database = abinit_vars
        assert database.apropos("ecut")
        #assert len(database.json_dumps_varnames())

        print("vargeo section:\n", database.vars_with_varset("vargeo"))
        for section in database.my_varset_list:
            assert len(database.vars_with_varset(section))

        for charact in database.my_characteristics:
            #print("character:", charact)
            assert len(database.vars_with_char(charact))

        name2varset = database.name2varset
        assert name2varset["ecut"] == "basic" and name2varset["ionmov"] == "rlx"

        print("d:", database.group_by_varset("ecut"), "hello")
        assert database.group_by_varset("ecut") ==  {'basic': ['ecut']}

        #abinit_help("ecut", info=True)
        # Should not raise
        #abinit_help("foobar", info=True)
        #for codename, d in codevars.items():
        #    d.validate_vars()

        for var in codevars.iter_allvars():
            try:
                assert repr(var)
                assert str(var)
                assert var.to_string(verbose=2)
                assert var._repr_html_()
                assert var.info
                assert var.to_abimarkdown()
                # topic --> list of tribes
                assert len(var.topic2relevances)
                var.validate()
            except Exception as exc:
                print("Error in %s:\n%s" % (var.abivarname, str(exc)))
                raise

    def test_variables_in_tests(self):
        """
        Find variables that are not tested by comparing the database of variables
        with the input file of the test.
        Build dictionary with list of untested variables for the different codes.
        Finally compare the new dictionary with the reference one and fail if they don't match.
        """
        # Build database with all input variables indexed by code name.
        from abimkdocs.variables import get_codevars
        codevars = get_codevars()

        # Build AbinitTestSuite object.
        from doc import tests as tmod
        tests = tmod.abitests.select_tests(suite_args=[], regenerate=True, flat_list=True)

        # Build conter for the different codes, keys are the varnames from the database.
        from collections import Counter
        count_code = {}
        for code, d in codevars.items():
            count_code[code] = Counter({k: 0 for k in d})

        ierr = 0
        doc_vnames = codevars["anaddb"].get_all_vnames(with_internal=False)
        anaddb_f90vnames = self.get_anaddb_varnames_from_f90()
        #print("anaddb_vames:", anaddb_f90vnames)
        diff = anaddb_f90vnames - doc_vnames
        if diff:
            ierr += 1
            print("\nThe following variables are found in anaddb F90 code but not in variables_anaddb.py")
            pprint(diff)

        diff = doc_vnames - anaddb_f90vnames
        if diff:
            ierr += 1
            print("\nThe following variables are found in variables_anaddb.py but not in anaddb F90 code.")
            pprint(diff)

        doc_vnames = codevars["abinit"].get_all_vnames(with_internal=False)
        abinit_f90vnames = self.get_abinit_varnames_from_f90()
        #print("abinit_f90vnames:", abinit_f90vnames)

        diff = abinit_f90vnames - doc_vnames
        if diff:
            ierr += 1
            print("\nThe following variables are found in abinit F90 code but not in variables_abinit.py")
            pprint(diff)

        diff = doc_vnames - abinit_f90vnames
        if diff:
            ierr += 1
            print("\nThe following variables are found in variables_abinit.py but not in abinit F90 code")
            pprint(diff)

        # TODO: should parse chkvars and
        black_list = set([
            "atompaw", "cut3d", "multibinit", "fftprof", "conducti", "mrgscr", "aTDEP",
            "mrgddb", "mrggkk", "mrgdv", "band2eps", "lruj", "fold2Bloch", "macroave", "testtransposer",
        ])
        for test in tests:
            if test.executable in black_list: continue
            vnset = test.get_varname_set()
            count_code[test.executable].update(vnset)

        untested = OrderedDict()
        for code, count in count_code.items():
            untested[code] = []
            # Add it if var is not tested and not internal.
            for vname, c in count.items():
                if c == 0 and not codevars[code][vname].is_internal:
                    #print(code, vname)
                    untested[code].append(vname)

        for code in sorted(untested.keys()):
            untested[code] = sorted(untested[code])
            if untested[code]:
                print("\nList of untested variables for code:", code)
                pprint(untested[code])

        #ref_json_path = os.path.join(os.path.dirname(__file__), "untested_variables.json")
        #update_ref = False
        #if update_ref:
        #    with open(ref_json_path, "wt") as fh:
        #        json.dump(untested, fh, indent=4)
        #else:
        #    with open(ref_json_path, "rt") as fh:
        #        ref_untested = json.load(fh)

        #    self.assertDictEqual(untested, ref_untested,
        #        msg="Detected mismatch between reference file and new list of untested variables.")

        assert ierr == 0