regtests.py 51.8 KB
Newer Older
1
import os
Lauri Himanen's avatar
Lauri Himanen committed
2
import sys
3
4
5
6
7
8
9
import unittest
import logging
import numpy as np
from cp2kparser import CP2KParser
from nomadcore.unit_conversion.unit_conversion import convert_unit


10
11
def get_result(folder, metaname=None):
    """Get the results from the calculation in the given folder. By default goes through different
12
13
14

    Args:
        folder: The folder relative to the directory of this script where the
15
            parsed calculation resides.
16
17
        metaname(str): Optional quantity to return. If not specified, returns
            the full dictionary of results.
18
19
    """
    dirname = os.path.dirname(__file__)
20
    filename = os.path.join("cp2k_{}".format(VERSION), dirname, folder, "unittest.out")
21
22
    parser = CP2KParser(None, debug=True, log_level=logging.CRITICAL)
    results = parser.parse(filename)
23

24
25
    if metaname is None:
        return results
26
    else:
27
        return results[metaname]
28
29


30
class TestErrors(unittest.TestCase):
31
    """Test error situations which may occur during the parsing.
32
33
    """
    def test_no_file(self):
34
35
        """File is not no present.
        """
36
        with self.assertRaises(ValueError):
37
            get_result("errors/no_file", "XC_functional")
38
39

    def test_invalid_file(self):
40
41
42
43
        """Main file is invalid.
        """
        with self.assertRaises(RuntimeError):
            get_result("errors/invalid_file", "XC_functional")
44
45

    def test_invalid_run_type(self):
46
47
48
49
50
        """Unrecognized run type.
        """
        with self.assertRaises(KeyError):
            get_result("errors/invalid_run_type", "XC_functional")

51
52
53
54
55
56
    def test_short_main_file(self):
        """Test what happens if main file is very small.
        """
        with self.assertRaises(RuntimeError):
            get_result("errors/short_main_file", "XC_functional")

57

58
59
60
class TestUnknownInput(unittest.TestCase):
    """Tests for cases where unknown information is encountered in the parsing.
    """
61
    def test_unknown_version(self):
62
63
        """Test how a new version is handled.
        """
64
65
        get_result("errors/unknown_version", "XC_functional")

66
    def test_unknown_input_keyword(self):
67
68
        """Test how an unknown input keyword is handled.
        """
69
70
71
        get_result("errors/unknown_input_keyword", "XC_functional")

    def test_unknown_input_section(self):
72
73
        """Test unknown input file section.
        """
74
75
76
        get_result("errors/unknown_input_section", "XC_functional")

    def test_unknown_input_section_parameter(self):
77
78
        """
        """
79
80
        get_result("errors/unknown_input_section_parameter", "XC_functional")

81

82
class TestXCFunctional(unittest.TestCase):
83
84
    """Tests that the XC functionals can be properly parsed.
    """
85
86
    def test_pade(self):
        xc = get_result("XC_functional/pade", "XC_functional")
87
        self.assertEqual(xc, "1*LDA_XC_TETER93")
88
89
90

    def test_lda(self):
        xc = get_result("XC_functional/lda", "XC_functional")
91
        self.assertEqual(xc, "1*LDA_XC_TETER93")
92
93
94

    def test_blyp(self):
        xc = get_result("XC_functional/blyp", "XC_functional")
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
        self.assertEqual(xc, "1*GGA_C_LYP+1*GGA_X_B88")

    def test_b3lyp(self):
        xc = get_result("XC_functional/b3lyp", "XC_functional")
        self.assertEqual(xc, "1*HYB_GGA_XC_B3LYP")

    def test_olyp(self):
        xc = get_result("XC_functional/olyp", "XC_functional")
        self.assertEqual(xc, "1*GGA_C_LYP+1*GGA_X_OPTX")

    def test_hcth120(self):
        xc = get_result("XC_functional/hcth120", "XC_functional")
        self.assertEqual(xc, "1*GGA_XC_HCTH_120")

    def test_pbe0(self):
        xc = get_result("XC_functional/pbe0", "XC_functional")
        self.assertEqual(xc, "1*HYB_GGA_XC_PBEH")

    def test_pbe(self):
        xc = get_result("XC_functional/pbe", "XC_functional")
        self.assertEqual(xc, "1*GGA_C_PBE+1*GGA_X_PBE")
116
117


118
119
120
121
122
123
124
125
126
127
128
129
130
class TestSCFConvergence(unittest.TestCase):
    """Tests whether the convergence status and number of SCF step can be
    parsed correctly.
    """
    def test_converged(self):
        result = get_result("convergence/converged", "single_configuration_calculation_converged")
        self.assertTrue(result)

    def test_non_converged(self):
        result = get_result("convergence/non_converged", "single_configuration_calculation_converged")
        self.assertFalse(result)


131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
class TestForceFiles(unittest.TestCase):
    """Tests that different force files that can be output, can actually be
    found and parsed.
    """
    def test_single_point(self):

        result = get_result("force_file/single_point", "atom_forces")
        expected_result = convert_unit(
            np.array([
                [0.00000000, 0.00000000, 0.00000000],
                [0.00000000, 0.00000001, 0.00000001],
                [0.00000001, 0.00000001, 0.00000000],
                [0.00000001, 0.00000000, 0.00000001],
                [-0.00000001, -0.00000001, -0.00000001],
                [-0.00000001, -0.00000001, -0.00000001],
                [-0.00000001, -0.00000001, -0.00000001],
                [-0.00000001, -0.00000001, -0.00000001],
            ]),
            "forceAu"
        )
        self.assertTrue(np.array_equal(result, expected_result))


154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
class TestSelfInteractionCorrectionMethod(unittest.TestCase):
    """Tests that the self-interaction correction can be properly parsed.
    """
    def test_no(self):
        sic = get_result("sic/no", "self_interaction_correction_method")
        self.assertEqual(sic, "")

    def test_ad(self):
        sic = get_result("sic/ad", "self_interaction_correction_method")
        self.assertEqual(sic, "SIC_AD")

    def test_explicit_orbitals(self):
        sic = get_result("sic/explicit_orbitals", "self_interaction_correction_method")
        self.assertEqual(sic, "SIC_EXPLICIT_ORBITALS")

    def test_mauri_spz(self):
        sic = get_result("sic/mauri_spz", "self_interaction_correction_method")
        self.assertEqual(sic, "SIC_MAURI_SPZ")

    def test_mauri_us(self):
        sic = get_result("sic/mauri_us", "self_interaction_correction_method")
        self.assertEqual(sic, "SIC_MAURI_US")


178
179
180
181
182
class TestStressTensorMethods(unittest.TestCase):
    """Tests that the stress tensor can be properly parsed for different
    calculation methods.
    """
    def test_none(self):
183
        get_result("stress_tensor/none", "section_stress_tensor")
184
185

    def test_analytical(self):
186
        results = get_result("stress_tensor/analytical")
187
188
189
190
191
        method = results["stress_tensor_method"]
        results["stress_tensor"]
        self.assertEqual(method, "Analytical")

    def test_numerical(self):
192
        results = get_result("stress_tensor/numerical")
193
194
195
196
197
        method = results["stress_tensor_method"]
        results["stress_tensor"]
        self.assertEqual(method, "Numerical")

    def test_diagonal_analytical(self):
198
        results = get_result("stress_tensor/diagonal_analytical")
199
200
201
202
203
        method = results["stress_tensor_method"]
        results["stress_tensor"]
        self.assertEqual(method, "Diagonal analytical")

    def test_diagonal_numerical(self):
204
        results = get_result("stress_tensor/diagonal_numerical")
205
206
207
208
209
        method = results["stress_tensor_method"]
        results["stress_tensor"]
        self.assertEqual(method, "Diagonal numerical")


210
211
212
class TestConfigurationPeriodicDimensions(unittest.TestCase):
    """Tests that the self-interaction correction can be properly parsed.
    """
213
    folder = "configuration_periodic_dimensions/"
214
215

    def test_default(self):
216
        result = get_result(self.folder+"default", "configuration_periodic_dimensions")
217
218
219
        self.assertTrue(np.array_equal(result, np.array((True, True, True))))

    def test_none(self):
220
        result = get_result(self.folder+"none", "configuration_periodic_dimensions")
221
222
223
        self.assertTrue(np.array_equal(result, np.array((False, False, False))))

    def test_x(self):
224
        result = get_result(self.folder+"x", "configuration_periodic_dimensions")
225
226
227
        self.assertTrue(np.array_equal(result, np.array((True, False, False))))

    def test_y(self):
228
        result = get_result(self.folder+"y", "configuration_periodic_dimensions")
229
230
231
        self.assertTrue(np.array_equal(result, np.array((False, True, False))))

    def test_z(self):
232
        result = get_result(self.folder+"z", "configuration_periodic_dimensions")
233
234
235
        self.assertTrue(np.array_equal(result, np.array((False, False, True))))

    def test_xy(self):
236
        result = get_result(self.folder+"xy", "configuration_periodic_dimensions")
237
238
239
        self.assertTrue(np.array_equal(result, np.array((True, True, False))))

    def test_xyz(self):
240
        result = get_result(self.folder+"xyz", "configuration_periodic_dimensions")
241
242
243
        self.assertTrue(np.array_equal(result, np.array((True, True, True))))

    def test_xz(self):
244
        result = get_result(self.folder+"xz", "configuration_periodic_dimensions")
245
246
247
        self.assertTrue(np.array_equal(result, np.array((True, False, True))))

    def test_yz(self):
248
        result = get_result(self.folder+"yz", "configuration_periodic_dimensions")
249
250
251
        self.assertTrue(np.array_equal(result, np.array((False, True, True))))


252
253
254
class TestGeometryInfo(unittest.TestCase):
    """Tests for a CP2K calculation with RUN_TYPE ENERGY_FORCE.
    """
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
    def test_malformed_xyz_comment(self):
        """Test reading geometry from file with malformed XYZ comment file that
        ASE cannot read. ASE could not read it properly even when pure XYZ was
        requested and thus data contents in the comment should be ignored.
        """
        results = get_result("geometries/input_xyz_malformed_comment")

        # Test the cell information
        simulation_cell = results["simulation_cell"]
        lattice_vectors = results["lattice_vectors"]

        expected = np.array([
            [10.211, 0.000, 0.000],
            [5.105, 8.843, 0.000],
            [0.000, 0.000, 22.084]
        ])*1e-10

        self.assertTrue(np.allclose(simulation_cell, expected, atol=1e-15, rtol=0))
        self.assertTrue(np.allclose(lattice_vectors, expected, atol=1e-15, rtol=0))

        # Test the label information
        labels = results["atom_labels"]
        expected_lab = np.array(32*["Cu"])
        self.assertTrue(np.array_equal(labels[0, :], expected_lab))

280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
    def test_input_file_vectors(self):
        """Test geometry given in input file only
        """
        results = get_result("geometries/input_vectors")

        # Test the cell information
        simulation_cell = results["simulation_cell"]
        lattice_vectors = results["lattice_vectors"]

        expected = np.array([
            [5.430697500, 0.000000000, 0.000000000],
            [0.000000000, 5.430697500, 0.000000000],
            [0.000000000, 0.000000000, 5.430697500]
        ])*1e-10

        self.assertTrue(np.allclose(simulation_cell, expected, atol=1e-15, rtol=0))
        self.assertTrue(np.allclose(lattice_vectors, expected, atol=1e-15, rtol=0))

        # Test the coordinate and label information
        pos = results["atom_positions"]
        labels = results["atom_labels"]
        expected_pos = np.array([
            [0.000000000, 0.000000000, 0.000000000],
            [0.000000000, 2.715348700, 2.715348700],
            [2.715348700, 2.715348700, 0.000000000],
            [2.715348700, 0.000000000, 2.715348700],
            [4.073023100, 1.357674400, 4.073023100],
            [1.357674400, 1.357674400, 1.357674400],
            [1.357674400, 4.073023100, 4.073023100],
            [4.073023100, 4.073023100, 1.357674400]
        ])*1e-10
        self.assertTrue(np.allclose(pos, expected_pos, atol=1e-15, rtol=0))
        expected_lab = np.array(["Si", "Si", "Si", "Si", "Si", "Si", "Si", "Si"])
        self.assertTrue(np.array_equal(labels, expected_lab))
314
315
316
317
318

    def test_input_file_parameters(self):
        """Test geometry given as angles and lengths and with radians as unit.
        """
        results = get_result("geometries/input_parameters")
319
320

        # Test the cell information
321
322
323
324
325
326
327
328
329
330
331
332
        simulation_cell = results["simulation_cell"]
        lattice_vectors = results["lattice_vectors"]

        expected = np.array([
            [5.430697500, 0.000000000, 0.000000000],
            [0.000000000, 5.430697500, 0.000000000],
            [0.000000000, 0.000000000, 5.430697500]
        ])*1e-10

        self.assertTrue(np.allclose(simulation_cell, expected, atol=1e-15, rtol=0))
        self.assertTrue(np.allclose(lattice_vectors, expected, atol=1e-15, rtol=0))

333
        # Test the coordinate and label information
334
335
        pos = results["atom_positions"]
        labels = results["atom_labels"]
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
        expected_pos = np.array([
            [0.000000000, 0.000000000, 0.000000000],
            [0.000000000, 2.715348700, 2.715348700],
            [2.715348700, 2.715348700, 0.000000000],
            [2.715348700, 0.000000000, 2.715348700],
            [4.073023100, 1.357674400, 4.073023100],
            [1.357674400, 1.357674400, 1.357674400],
            [1.357674400, 4.073023100, 4.073023100],
            [4.073023100, 4.073023100, 1.357674400]
        ])*1e-10
        self.assertTrue(np.allclose(pos, expected_pos, atol=1e-15, rtol=0))
        expected_lab = np.array(["Si", "Si", "Si", "Si", "Si", "Si", "Si", "Si"])
        self.assertTrue(np.array_equal(labels, expected_lab))

    def test_input_file_xyz_centered(self):
        """Test geometry file given as xyz and centered.
        """
        results = get_result("geometries/input_xyz_centered")

        # Test the cell information
        simulation_cell = results["simulation_cell"]
        lattice_vectors = results["lattice_vectors"]

        expected = np.array([
            [8, 0, 0],
            [0, 8, 0],
            [0, 0, 8]
        ])*1e-10
        self.assertTrue(np.allclose(simulation_cell, expected, atol=1e-15, rtol=0))
        self.assertTrue(np.allclose(lattice_vectors, expected, atol=1e-15, rtol=0))

        # Test the coordinate and label information
        pos = results["atom_positions"]
        labels = results["atom_labels"]
        expected_pos = np.array([
            [4.000000, 4.000000, 4.500000],
            [4.000000, 4.000000, 3.500000],
        ])*1e-10
        self.assertTrue(np.allclose(pos, expected_pos, atol=1e-15, rtol=0))
        expected_lab = np.array(["H", "H"])
        self.assertTrue(np.array_equal(labels, expected_lab))

        # Test that other info is OK
        program_name = results["program_name"]
        esm = results["electronic_structure_method"]
        basis_set = results["program_basis_set_type"]
        self.assertEqual(program_name, "CP2K")
        self.assertEqual(esm, "DFT")
        self.assertEqual(basis_set, "gaussians")
385

386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
    def test_input_file_xyz_variable(self):
        """Test geometry file given as xyz where the file name is given through
        a variable.
        """
        results = get_result("geometries/input_xyz_variable")

        # Test the cell information
        simulation_cell = results["simulation_cell"]
        lattice_vectors = results["lattice_vectors"]

        expected = np.array([
            [30.18, 0, 0],
            [0, 29.04, 0],
            [0, 0, 11.13]
        ])*1e-10
        self.assertTrue(np.allclose(simulation_cell, expected, atol=1e-15, rtol=0))
        self.assertTrue(np.allclose(lattice_vectors, expected, atol=1e-15, rtol=0))

        # Test the coordinate and label information
        pos = results["atom_positions"]
        labels = results["atom_labels"]
        self.assertEqual(labels.shape, (59, 132))
        self.assertEqual(pos.shape, (59, 132, 3))

        # Test that other info is OK
        program_name = results["program_name"]
        esm = results["electronic_structure_method"]
        basis_set = results["program_basis_set_type"]
        self.assertEqual(program_name, "CP2K")
        self.assertEqual(esm, "DFT")
        self.assertEqual(basis_set, "gaussians")

418

419
420
421
class TestEnergyForce(unittest.TestCase):
    """Tests for a CP2K calculation with RUN_TYPE ENERGY_FORCE.
    """
422
423
    @classmethod
    def setUpClass(cls):
424
        cls.results = get_result("energy_force")
425
        # cls.results.print_summary()
426

427
    def test_energy_total_scf_iteration(self):
428
        result = self.results["energy_total_scf_iteration"]
429
        expected_result = convert_unit(np.array(-32.2320848878), "hartree")
430
        self.assertTrue(np.array_equal(result[0], expected_result))
431

432
433
434
435
436
437
438
439
    def test_program_name(self):
        result = self.results["program_name"]
        self.assertEqual(result, "CP2K")

    def test_program_compilation_host(self):
        result = self.results["program_compilation_host"]
        self.assertEqual(result, "lenovo700")

440
441
442
443
    def test_scf_max_iteration(self):
        result = self.results["scf_max_iteration"]
        self.assertEqual(result, 300)

444
445
446
447
    def test_basis_set(self):
        section_basis_set = self.results["section_basis_set"][0]

        # Basis name
448
        name = section_basis_set["basis_set_name"]
449
450
451
        self.assertEqual(name, "DZVP-GTH-PADE_PW_150.0")

        # Basis kind
452
        kind = section_basis_set["basis_set_kind"]
453
454
455
        self.assertEqual(kind, "wavefunction")

        # Cell dependent basis mapping
456
        cell_basis_mapping = section_basis_set["mapping_section_basis_set_cell_dependent"]
457
458
459
        self.assertEqual(cell_basis_mapping, 0)

        # # Atom centered basis mapping
460
        atom_basis_mapping = section_basis_set["mapping_section_basis_set_atom_centered"]
461
462
        self.assertTrue(np.array_equal(atom_basis_mapping, np.array(8*[0])))

463
464
    def test_scf_threshold_energy_change(self):
        result = self.results["scf_threshold_energy_change"]
465
        self.assertEqual(result, convert_unit(1.00E-07, "hartree"))
466
467
468
469
470

    def test_number_of_spin_channels(self):
        result = self.results["number_of_spin_channels"]
        self.assertEqual(result, 1)

471
472
473
474
    def test_electronic_structure_method(self):
        result = self.results["electronic_structure_method"]
        self.assertEqual(result, "DFT")

475
476
477
478
479
480
481
482
483
484
    def test_energy_change_scf_iteration(self):
        energy_change = self.results["energy_change_scf_iteration"]
        expected_result = convert_unit(np.array(-3.22E+01), "hartree")
        self.assertTrue(np.array_equal(energy_change[0], expected_result))

    def test_energy_XC_scf_iteration(self):
        result = self.results["energy_XC_scf_iteration"]
        expected_result = convert_unit(np.array(-9.4555961214), "hartree")
        self.assertTrue(np.array_equal(result[0], expected_result))

485
    def test_energy_total(self):
486
        result = self.results["energy_total"]
487
        expected_result = convert_unit(np.array(-31.297885372811074), "hartree")
488
        self.assertTrue(np.array_equal(result, expected_result))
489

490
491
    def test_electronic_kinetic_energy(self):
        result = self.results["electronic_kinetic_energy"]
492
        expected_result = convert_unit(np.array(13.31525592466419), "hartree")
493
494
        self.assertTrue(np.array_equal(result, expected_result))

495
    def test_atom_forces(self):
496
        result = self.results["atom_forces"]
497
498
499
500
501
502
503
504
505
506
507
        expected_result = convert_unit(
            np.array([
                [0.00000000, 0.00000000, 0.00000000],
                [0.00000000, 0.00000001, 0.00000001],
                [0.00000001, 0.00000001, 0.00000000],
                [0.00000001, 0.00000000, 0.00000001],
                [-0.00000001, -0.00000001, -0.00000001],
                [-0.00000001, -0.00000001, -0.00000001],
                [-0.00000001, -0.00000001, -0.00000001],
                [-0.00000001, -0.00000001, -0.00000001],
            ]),
508
            "forceAu"
509
        )
510
        self.assertTrue(np.array_equal(result, expected_result))
511

512
    def test_atom_label(self):
513
        atom_labels = self.results["atom_labels"]
514
        expected_labels = np.array(8*["Si"])
Lauri Himanen's avatar
Lauri Himanen committed
515
516
517
        self.assertTrue(np.array_equal(atom_labels, expected_labels))

    def test_simulation_cell(self):
Lauri Himanen's avatar
Lauri Himanen committed
518
        cell = self.results["simulation_cell"]
519
520
        vectors = self.results["lattice_vectors"]

Lauri Himanen's avatar
Lauri Himanen committed
521
522
523
524
525
        n_vectors = cell.shape[0]
        n_dim = cell.shape[1]
        self.assertEqual(n_vectors, 3)
        self.assertEqual(n_dim, 3)
        expected_cell = convert_unit(np.array([[5.431, 0, 0], [0, 5.431, 0], [0, 0, 5.431]]), "angstrom")
526

Lauri Himanen's avatar
Lauri Himanen committed
527
        self.assertTrue(np.array_equal(cell, expected_cell))
528
        self.assertTrue(np.array_equal(vectors, expected_cell))
Lauri Himanen's avatar
Lauri Himanen committed
529
530

    def test_number_of_atoms(self):
Lauri Himanen's avatar
Lauri Himanen committed
531
        n_atoms = self.results["number_of_atoms"]
Lauri Himanen's avatar
Lauri Himanen committed
532
533
534
        self.assertEqual(n_atoms, 8)

    def test_atom_position(self):
535
        atom_position = self.results["atom_positions"]
Lauri Himanen's avatar
Lauri Himanen committed
536
537
538
        expected_position = convert_unit(np.array([4.073023, 4.073023, 1.357674]), "angstrom")
        self.assertTrue(np.array_equal(atom_position[-1, :], expected_position))

539
    def test_x_cp2k_filenames(self):
540
        input_filename = self.results["x_cp2k_input_filename"]
541
542
543
        expected_input = "si_bulk8.inp"
        self.assertTrue(input_filename, expected_input)

544
        bs_filename = self.results["x_cp2k_basis_set_filename"]
545
546
547
        expected_bs = "../BASIS_SET"
        self.assertEqual(bs_filename, expected_bs)

548
        geminal_filename = self.results["x_cp2k_geminal_filename"]
549
550
551
        expected_geminal = "BASIS_GEMINAL"
        self.assertEqual(geminal_filename, expected_geminal)

552
        potential_filename = self.results["x_cp2k_potential_filename"]
553
554
555
        expected_potential = "../GTH_POTENTIALS"
        self.assertEqual(potential_filename, expected_potential)

556
        mm_potential_filename = self.results["x_cp2k_mm_potential_filename"]
557
558
559
        expected_mm_potential = "MM_POTENTIAL"
        self.assertEqual(mm_potential_filename, expected_mm_potential)

560
        coordinate_filename = self.results["x_cp2k_coordinate_filename"]
561
562
        expected_coordinate = "__STD_INPUT__"
        self.assertEqual(coordinate_filename, expected_coordinate)
563

564
    def test_target_multiplicity(self):
565
        multiplicity = self.results["spin_target_multiplicity"]
566
567
568
569
570
571
        self.assertEqual(multiplicity, 1)

    def test_total_charge(self):
        charge = self.results["total_charge"]
        self.assertEqual(charge, 0)

572
573
    def test_section_basis_set_atom_centered(self):
        basis = self.results["section_basis_set_atom_centered"][0]
574
575
        name = basis["basis_set_atom_centered_short_name"]
        number = basis["basis_set_atom_number"]
576
577
578
        self.assertEquals(name, "DZVP-GTH-PADE")
        self.assertEquals(number, 14)

Lauri Himanen's avatar
Lauri Himanen committed
579
580
    def test_section_basis_set_cell_dependent(self):
        basis = self.results["section_basis_set_cell_dependent"][0]
581
        cutoff = basis["basis_set_planewave_cutoff"]
Lauri Himanen's avatar
Lauri Himanen committed
582
583
        self.assertEquals(cutoff, convert_unit(300.0, "hartree"))

584
585
    def test_section_method_atom_kind(self):
        kind = self.results["section_method_atom_kind"][0]
586
587
        self.assertEqual(kind["method_atom_kind_atom_number"], 14)
        self.assertEqual(kind["method_atom_kind_label"], "Si")
588
589
590

    def test_section_method_basis_set(self):
        kind = self.results["section_method_basis_set"][0]
591
592
        self.assertEqual(kind["method_basis_set_kind"], "wavefunction")
        self.assertEqual(kind["number_of_basis_sets_atom_centered"], 1)
593
        self.assertTrue(np.array_equal(kind["mapping_section_method_basis_set_atom_centered"], np.array([[0, 0]])))
594

595
596
597
598
599
    def test_single_configuration_calculation_converged(self):
        result = self.results["single_configuration_calculation_converged"]
        self.assertTrue(result)

    def test_scf_dft_number_of_iterations(self):
600
        result = self.results["number_of_scf_iterations"]
601
602
603
604
605
606
607
        self.assertEqual(result, 10)

    def test_single_configuration_to_calculation_method_ref(self):
        result = self.results["single_configuration_to_calculation_method_ref"]
        self.assertEqual(result, 0)

    def test_single_configuration_calculation_to_system_description_ref(self):
608
        result = self.results["single_configuration_calculation_to_system_ref"]
609
610
        self.assertEqual(result, 0)

611
    def test_stress_tensor(self):
612
        result = self.results["stress_tensor"]
613
614
        expected_result = convert_unit(
            np.array([
615
616
617
                [7.77640934, -0.00000098, -0.00000099],
                [-0.00000098, 7.77640935, -0.00000101],
                [-0.00000099, -0.00000101, 7.77640935],
618
619
620
621
622
            ]),
            "GPa"
        )
        self.assertTrue(np.array_equal(result, expected_result))

623
624
    def test_stress_tensor_eigenvalues(self):
        result = self.results["x_cp2k_stress_tensor_eigenvalues"]
625
        expected_result = convert_unit(np.array([7.77640735, 7.77641033, 7.77641036]), "GPa")
626
627
628
629
630
        self.assertTrue(np.array_equal(result, expected_result))

    def test_stress_tensor_eigenvectors(self):
        result = self.results["x_cp2k_stress_tensor_eigenvectors"]
        expected_result = np.array([
631
632
633
            [0.57490332, -0.79965737, -0.17330395],
            [0.57753686, 0.54662171, -0.60634634],
            [0.57960102, 0.24850110, 0.77608624],
634
635
636
637
638
        ])
        self.assertTrue(np.array_equal(result, expected_result))

    def test_stress_tensor_determinant(self):
        result = self.results["x_cp2k_stress_tensor_determinant"]
639
        expected_result = convert_unit(4.70259243E+02, "GPa^3")
640
641
642
643
        self.assertTrue(np.array_equal(result, expected_result))

    def test_stress_tensor_one_third_of_trace(self):
        result = self.results["x_cp2k_stress_tensor_one_third_of_trace"]
644
        expected_result = convert_unit(7.77640934E+00, "GPa")
645
646
        self.assertTrue(np.array_equal(result, expected_result))

647
648
    def test_program_basis_set_type(self):
        result = self.results["program_basis_set_type"]
649
        self.assertEqual(result, "gaussians")
650
651
652


class TestPreprocessor(unittest.TestCase):
653
654
655
    """Tests that the parser can read input files with preprocessor
    declarations, such as variables.
    """
656
    def test_include(self):
657
        result = get_result("input_preprocessing/include", "x_cp2k_input_GLOBAL.PRINT_LEVEL")
658
659
660
        self.assertEqual(result, "LOW")

    def test_variable(self):
661
        result = get_result("input_preprocessing/variable", "x_cp2k_input_GLOBAL.PROJECT_NAME")
662
663
        self.assertEqual(result, "variable_test")

664
    def test_variable_multiple(self):
665
        result = get_result("input_preprocessing/variable_multiple", "x_cp2k_input_FORCE_EVAL.DFT.MGRID.CUTOFF")
666
667
668
        self.assertEqual(result, "5")
        result = get_result("input_preprocessing/variable_multiple", "x_cp2k_input_FORCE_EVAL.DFT.MGRID.NGRIDS")
        self.assertEqual(result, "2")
669

Lauri Himanen's avatar
Lauri Himanen committed
670
    def test_comments(self):
671
        result = get_result("input_preprocessing/comments", "x_cp2k_input_FORCE_EVAL.DFT.MGRID.CUTOFF")
672
        self.assertEqual(result, "120")
Lauri Himanen's avatar
Lauri Himanen committed
673

674
    def test_tabseparator(self):
675
        result = get_result("input_preprocessing/tabseparator", "x_cp2k_input_FORCE_EVAL.DFT.MGRID.CUTOFF")
676
        self.assertEqual(result, "120")
677

678

679
class TestGeoOpt(unittest.TestCase):
680
681
    """Tests that geometry optimizations are correctly parsed.
    """
682
683
    @classmethod
    def setUpClass(cls):
684
        cls.results = get_result("geo_opt/cg")
685
686
687
688
689

    def test_geometry_optimization_converged(self):
        result = self.results["geometry_optimization_converged"]
        self.assertTrue(result)

690
    def test_number_of_frames_in_sequence(self):
691
        result = self.results["number_of_frames_in_sequence"]
692
693
        self.assertEqual(result, 7)

694
695
696
697
698
699
    def test_frame_sequence_to_sampling_ref(self):
        result = self.results["frame_sequence_to_sampling_ref"]
        self.assertEqual(result, 0)

    def test_frame_sequence_local_frames_ref(self):
        result = self.results["frame_sequence_local_frames_ref"]
700
        expected_result = np.array([0, 1, 2, 3, 4, 5, 6])
701
702
        self.assertTrue(np.array_equal(result, expected_result))

703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
    def test_sampling_method(self):
        result = self.results["sampling_method"]
        self.assertEqual(result, "geometry_optimization")

    def test_geometry_optimization_method(self):
        result = self.results["geometry_optimization_method"]
        self.assertEqual(result, "conjugate_gradient")

    def test_geometry_optimization_geometry_change(self):
        result = self.results["geometry_optimization_geometry_change"]
        expected_result = convert_unit(
            0.0010000000,
            "bohr"
        )
        self.assertEqual(result, expected_result)

    def test_geometry_optimization_threshold_force(self):
        result = self.results["geometry_optimization_threshold_force"]
        expected_result = convert_unit(
            0.0010000000,
            "bohr^-1*hartree"
        )
        self.assertEqual(result, expected_result)

    def test_frame_sequence_potential_energy(self):
        result = self.results["frame_sequence_potential_energy"]
        expected_result = convert_unit(
            np.array([
                -17.1534159246,
                -17.1941015290,
                -17.2092321965,
                -17.2097667733,
                -17.2097743028,
                -17.2097743229,
737
                -17.20977820662248,
738
739
740
741
742
            ]),
            "hartree"
        )
        self.assertTrue(np.array_equal(result, expected_result))

743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
    def test_atom_positions(self):
        result = self.results["atom_positions"]
        expected_start = convert_unit(
            np.array([
                [12.2353220000, 1.3766420000, 10.8698800000],
                [12.4175775999, 2.2362362573, 11.2616216864],
                [11.9271436933, 1.5723516602, 10.0115134757],
            ]),
            "angstrom"
        )

        expected_end = convert_unit(
            np.array([
                [12.2353220000, 1.3766420000, 10.8698800000],
                [12.4958164689, 2.2307248873, 11.3354322515],
                [11.9975558616, 1.5748085240, 10.0062792262],
            ]),
            "angstrom"
        )
762
763
        result_start = result[0, :, :]
        result_end = result[-1, :, :]
764
765
        self.assertTrue(np.array_equal(result_start, expected_start))
        self.assertTrue(np.array_equal(result_end, expected_end))
766
767


768
class TestGeoOptTrajFormats(unittest.TestCase):
769
770
    """Different trajectory formats in geometry optimization.
    """
771
772
    def test_default(self):
        result = get_result("geo_opt/geometry_formats/default", "atom_positions")
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
        expected_start = convert_unit(
            np.array([
                [12.2353220000, 1.3766420000, 10.8698800000],
                [12.4175624065, 2.2362390825, 11.2616392180],
                [11.9271777126, 1.5723402996, 10.0115089094],
            ]),
            "angstrom"
        )
        expected_end = convert_unit(
            np.array([
                [12.2353220000, 1.3766420000, 10.8698800000],
                [12.4957995882, 2.2307218433, 11.3354453867],
                [11.9975764125, 1.5747996320, 10.0062529540],
            ]),
            "angstrom"
        )
789
790
        result_start = result[0, :, :]
        result_end = result[-1, :, :]
791
792
793
        self.assertTrue(np.array_equal(result_start, expected_start))
        self.assertTrue(np.array_equal(result_end, expected_end))

794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
    # def test_xyz(self):

        # result = get_result("geo_opt/geometry_formats/xyz", "atom_positions")
        # expected_start = convert_unit(
            # np.array([
                # [12.2353220000, 1.3766420000, 10.8698800000],
                # [12.4175624065, 2.2362390825, 11.2616392180],
                # [11.9271777126, 1.5723402996, 10.0115089094],
            # ]),
            # "angstrom"
        # )
        # expected_end = convert_unit(
            # np.array([
                # [12.2353220000, 1.3766420000, 10.8698800000],
                # [12.4957995882, 2.2307218433, 11.3354453867],
                # [11.9975764125, 1.5747996320, 10.0062529540],
            # ]),
            # "angstrom"
        # )
        # result_start = result[0, :, :]
        # result_end = result[-1, :, :]
        # self.assertTrue(np.array_equal(result_start, expected_start))
        # self.assertTrue(np.array_equal(result_end, expected_end))

    # def test_pdb(self):
        # result = get_result("geo_opt/geometry_formats/pdb", "atom_positions")
        # expected_start = convert_unit(
            # np.array([
                # [12.235, 1.377, 10.870],
                # [12.418, 2.236, 11.262],
                # [11.927, 1.572, 10.012],
            # ]),
            # "angstrom"
        # )
        # expected_end = convert_unit(
            # np.array([
                # [12.235, 1.377, 10.870],
                # [12.496, 2.231, 11.335],
                # [11.998, 1.575, 10.006],
            # ]),
            # "angstrom"
        # )
        # result_start = result[0, :, :]
        # result_end = result[-1, :, :]
        # self.assertTrue(np.array_equal(result_start, expected_start))
        # self.assertTrue(np.array_equal(result_end, expected_end))

    # def test_dcd(self):
        # result = get_result("geo_opt/geometry_formats/dcd", "atom_positions")
        # frames = result.shape[0]
        # self.assertEqual(frames, 7)
845
846


847
class TestGeoOptOptimizers(unittest.TestCase):
848
849
    """Different optimization methods in gemoetry optimization.
    """
850
851
852
853
854
855
856
857
    def test_bfgs(self):
        result = get_result("geo_opt/bfgs", "geometry_optimization_method")
        self.assertEqual(result, "bfgs")

    def test_lbfgs(self):
        result = get_result("geo_opt/lbfgs", "geometry_optimization_method")
        self.assertEqual(result, "bfgs")

858
859

class TestGeoOptTrajectory(unittest.TestCase):
860
861
862
    """Tests that the print settings for geometry optimization are handled
    correctly.
    """
863
864
865
866
    def test_each_and_add_last(self):
        """Test that the EACH and ADD_LAST settings affect the parsing
        correctly.
        """
867
        results = get_result("geo_opt/each")
868
869
870
871
872

        single_conf = results["section_single_configuration_calculation"]
        systems = results["section_system"]

        i_conf = 0
873
        for calc in single_conf:
874
            system_index = calc["single_configuration_calculation_to_system_ref"]
875
876
877
            system = systems[system_index]

            if i_conf == 0 or i_conf == 2 or i_conf == 4:
878
                with self.assertRaises(KeyError):
879
                    pos = system["atom_positions"]
880
            else:
881
                pos = system["atom_positions"]
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
                if i_conf == 1:
                    expected_pos = convert_unit(
                        np.array([
                            [12.2353220000, 1.3766420000, 10.8698800000],
                            [12.4618486015, 2.2314871691, 11.3335607388],
                            [11.9990227122, 1.5776813026, 10.0384213366],
                        ]),
                        "angstrom"
                    )
                    self.assertTrue(np.array_equal(pos, expected_pos))
                if i_conf == 3:
                    expected_pos = convert_unit(
                        np.array([
                            [12.2353220000, 1.3766420000, 10.8698800000],
                            [12.4962705528, 2.2308411983, 11.3355758433],
                            [11.9975151486, 1.5746309898, 10.0054430868],
                        ]),
                        "angstrom"
                    )
                    self.assertTrue(np.array_equal(pos, expected_pos))
                if i_conf == 5:
                    expected_pos = convert_unit(
                        np.array([
                            [12.2353220000, 1.3766420000, 10.8698800000],
                            [12.4958168364, 2.2307249171, 11.3354322532],
                            [11.9975556812, 1.5748088251, 10.0062793864],
                        ]),
                        "angstrom"
                    )
                    self.assertTrue(np.array_equal(pos, expected_pos))

                if i_conf == 6:
                    expected_pos = convert_unit(
                        np.array([
                            [12.2353220000, 1.3766420000, 10.8698800000],
                            [12.4958164689, 2.2307248873, 11.3354322515],
                            [11.9975558616, 1.5748085240, 10.0062792262],
                        ]),
                        "angstrom"
                    )
                    self.assertTrue(np.array_equal(pos, expected_pos))

            i_conf += 1

Lauri Himanen's avatar
Lauri Himanen committed
926

927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
class TestRestart(unittest.TestCase):
    """Used to test that restarts from a previous run are parsed correctly.
    """
    @classmethod
    def setUpClass(cls):
        cls.results = get_result("restart")

    def test_program_name(self):
        result = self.results["program_name"]
        self.assertEqual(result, "CP2K")

    def test_frames(self):
        """Test that the number of frames equals the actual number of steps
        that are present in the output file.
        """
        scc = self.results["section_single_configuration_calculation"]
        n_scc = len(scc)
        n_frames = self.results["number_of_frames_in_sequence"]
        self.assertEqual(n_scc, n_frames)


class TestMDPrintSettings(unittest.TestCase):
    """Tests that MD print settings are respected.
    """
    @classmethod
    def setUpClass(cls):
        cls.results = get_result("md/print_settings")

    def test_traj_print(self):
        result = self.results["number_of_frames_in_sequence"]
        self.assertEqual(result, 11)

        sccs = self.results["section_single_configuration_calculation"]
        systems = self.results["section_system"]
        for i_scc, scc in enumerate(sccs):

            # Check that the configuration was read correctly from the XYZ file
            # by taking into account the print settings
            if i_scc % 3 == 0:

                # Check that positions are found
                ref_system = scc["single_configuration_calculation_to_system_ref"]
                system = systems[ref_system]
                system["atom_positions"]

        # Check that frames from ener file are read correctly with interval of 3
        frames = [0, 3, 6, 9]
        self.results["frame_sequence_temperature"]
        temp_frames = self.results["frame_sequence_temperature_frames"]
        pot_frames = self.results["frame_sequence_potential_energy_frames"]
        kin_frames = self.results["frame_sequence_kinetic_energy_frames"]
        con_frames = self.results["frame_sequence_conserved_quantity_frames"]
        self.assertTrue(np.array_equal(temp_frames, frames))
        self.assertTrue(np.array_equal(pot_frames, frames))
        self.assertTrue(np.array_equal(kin_frames, frames))
        self.assertTrue(np.array_equal(con_frames, frames))


Lauri Himanen's avatar
Lauri Himanen committed
985
class TestMD(unittest.TestCase):
986
987
    """Molecular dynamics tests.
    """
Lauri Himanen's avatar
Lauri Himanen committed
988
989
    @classmethod
    def setUpClass(cls):
990
        cls.results = get_result("md/nve")
Lauri Himanen's avatar
Lauri Himanen committed
991
992
993
994
995
996
997
998
999
        cls.temp = convert_unit(
            np.array([
                300.000000000,
                275.075405378,
                235.091633019,
                202.752506973,
                192.266488819,
                201.629598676,
                218.299664775,
1000
                230.324748557,
Lauri Himanen's avatar
Lauri Himanen committed
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
                232.691881533,
                226.146979313,
                213.165337396,
            ]),
            "K"
        )
        cls.cons = convert_unit(
            np.array([
                -34.323271136,
                -34.323245645,
                -34.323206964,
                -34.323183380,
                -34.323187747,
                -34.323208962,
                -34.323227533,
                -34.323233583,
                -34.323230715,
                -34.323227013,
                -34.323224123,
            ]),
            "hartree"
        )
        cls.pot = convert_unit(
            np.array([
                -34.330396471,
                -34.329778993,
                -34.328790653,
                -34.327998978,
                -34.327754290,
                -34.327997890,
                -34.328412394,
                -34.328704052,
                -34.328757407,
                -34.328598255,
                -34.328287038,
            ]),
            "hartree"
        )
        cls.kin = convert_unit(
            np.array([
                0.007125335,
                0.006533348,
                0.005583688,
                0.004815598,
                0.004566544,
                0.004788928,
                0.005184860,
                0.005470470,
                0.005526692,
                0.005371243,
                0.005062914,
            ]),
            "hartree"
        )
Lauri Himanen's avatar
Lauri Himanen committed
1055

1056
1057
1058
1059
1060
    def test_number_of_atoms(self):
        result = self.results["number_of_atoms"]
        expected_result = np.array(11*[6])
        self.assertTrue(np.array_equal(result, expected_result))

1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
    def test_simulation_cell(self):
        result = self.results["simulation_cell"]
        self.assertEqual(len(result), 11)
        expected_start = convert_unit(
            np.array([
                [9.853, 0, 0],
                [0, 9.853, 0],
                [0, 0, 9.853],
            ]),
            "angstrom"
        )
        self.assertTrue(np.array_equal(result[0], expected_start))

Lauri Himanen's avatar
Lauri Himanen committed
1074
    def test_ensemble_type(self):
1075
        result = self.results["ensemble_type"]
Lauri Himanen's avatar
Lauri Himanen committed
1076
1077
        self.assertEqual(result, "NVE")

1078
1079
1080
1081
    def test_sampling_method(self):
        result = self.results["sampling_method"]
        self.assertEqual(result, "molecular_dynamics")

Lauri Himanen's avatar
Lauri Himanen committed
1082
1083
1084
1085
    def test_number_of_frames_in_sequence(self):
        result = self.results["number_of_frames_in_sequence"]
        self.assertEqual(result, 11)

1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
    def test_atom_positions(self):
        result = self.results["atom_positions"]
        expected_start = convert_unit(
            np.array([
                [2.2803980000, 9.1465390000, 5.0886960000],
                [1.2517030000, 2.4062610000, 7.7699080000],
                [1.7620190000, 9.8204290000, 5.5284540000],
                [3.0959870000, 9.1070880000, 5.5881860000],
                [0.5541290000, 2.9826340000, 8.0820240000],
                [1.7712570000, 2.9547790000, 7.1821810000],
            ]),
            "angstrom"
        )
        expected_end = convert_unit(
            np.array([
                [2.2916014875, 9.1431763260, 5.0868100688],
                [1.2366834078, 2.4077552776, 7.7630044423],
                [1.6909790671, 9.8235337924, 5.5042564094],
                [3.1130341664, 9.0372111810, 5.6100739746],
                [0.5652070478, 3.0441761067, 8.1734257299],
                [1.8669280879, 2.9877213524, 7.2364955946],
            ]),
            "angstrom"
        )
1110
1111
        self.assertTrue(np.array_equal(result[0, :], expected_start))
        self.assertTrue(np.array_equal(result[-1, :], expected_end))
1112

1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
    def test_atom_velocities(self):
        result = self.results["atom_velocities"]
        expected_start = convert_unit(
            np.array([
                [0.0000299284, 0.0000082360, -0.0000216368],
                [-0.0001665963, 0.0001143863, -0.0000622640],
                [-0.0005732926, -0.0003112611, -0.0007149779],
                [0.0013083605, -0.0009262219, 0.0006258560],
                [0.0012002313, -0.0003701042, 0.0002810523],
                [0.0002340810, -0.0003388418, 0.0011398583],
            ]),
1124
            "bohr*(hbar/hartree)^-1"
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
        )
        expected_end = convert_unit(
            np.array([
                [0.0001600263, -0.0000383308, 0.0000153662],
                [-0.0001269381, -0.0000005151, -0.0000726214],
                [0.0000177093, -0.0003257814, -0.0000257852],
                [-0.0015067045, -0.0001700489, -0.0003651605],
                [0.0000307926, 0.0006886719, 0.0008431321],
                [0.0007424681, 0.0003614127, 0.0005749089],
            ]),
1135
            "bohr*(hbar/hartree)^-1"
1136
1137
        )

1138
1139
        self.assertTrue(np.array_equal(result[0, :], expected_start))
        self.assertTrue(np.array_equal(result[-1, :], expected_end))
1140

1141
1142
    def test_frame_sequence_potential_energy(self):
        result = self.results["frame_sequence_potential_energy"]
Lauri Himanen's avatar
Lauri Himanen committed
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
        self.assertTrue(np.array_equal(result, self.pot))

    def test_frame_sequence_kinetic_energy(self):
        result = self.results["frame_sequence_kinetic_energy"]
        self.assertTrue(np.array_equal(result, self.kin))

    def test_frame_sequence_conserved_quantity(self):
        result = self.results["frame_sequence_conserved_quantity"]
        self.assertTrue(np.array_equal(result, self.cons))

    def test_frame_sequence_temperature(self):
        result = self.results["frame_sequence_temperature"]
        self.assertTrue(np.array_equal(result, self.temp))

    def test_frame_sequence_time(self):
        result = self.results["frame_sequence_time"]
1159
1160
        expected_result = convert_unit(
            np.array([
Lauri Himanen's avatar
Lauri Himanen committed
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
                0.000000,
                0.500000,
                1.000000,
                1.500000,
                2.000000,
                2.500000,
                3.000000,
                3.500000,
                4.000000,
                4.500000,
                5.000000,
1172
            ]),
Lauri Himanen's avatar
Lauri Himanen committed
1173
            "fs"
1174
1175
1176
        )
        self.assertTrue(np.array_equal(result, expected_result))

Lauri Himanen's avatar
Lauri Himanen committed
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
    def test_frame_sequence_potential_energy_stats(self):
        result = self.results["frame_sequence_potential_energy_stats"]
        expected_result = np.array([self.pot.mean(), self.pot.std()])
        self.assertTrue(np.array_equal(result, expected_result))

    def test_frame_sequence_kinetic_energy_stats(self):
        result = self.results["frame_sequence_kinetic_energy_stats"]
        expected_result = np.array([self.kin.mean(), self.kin.std()])
        self.assertTrue(np.array_equal(result, expected_result))

    def test_frame_sequence_conserved_quantity_stats(self):
        result = self.results["frame_sequence_conserved_quantity_stats"]
        expected_result = np.array([self.cons.mean(), self.cons.std()])
        self.assertTrue(np.array_equal(result, expected_result))

    def test_frame_sequence_temperature_stats(self):
        result = self.results["frame_sequence_temperature_stats"]
        expected_result = np.array([self.temp.mean(), self.temp.std()])
        self.assertTrue(np.array_equal(result, expected_result))


class TestMDEnsembles(unittest.TestCase):
1199
1200
    """Different ensembles in MD.
    """
Lauri Himanen's avatar
Lauri Himanen committed
1201
1202
1203
    @classmethod
    def setUpClass(cls):
        cls.pressure = convert_unit(
1204
            np.array([
1205
1206
                -0.192828092559E+04,
                -0.145371071470E+04,
Lauri Himanen's avatar
Lauri Himanen committed
1207
                -0.210098903760E+03,
1208
1209
                0.167260570313E+04,
                0.395562042841E+04,
Lauri Himanen's avatar
Lauri Himanen committed
1210
                0.630374855942E+04,
1211
1212
                0.836906136786E+04,
                0.983216022830E+04,
Lauri Himanen's avatar
Lauri Himanen committed
1213
                0.104711540465E+05,
1214
                0.102444821550E+05,
Lauri Himanen's avatar
Lauri Himanen committed
1215
                0.931695792434E+04,
1216
            ]),
Lauri Himanen's avatar
Lauri Himanen committed
1217
            "bar"
1218
        )
Lauri Himanen's avatar
Lauri Himanen committed
1219
1220

    def test_nvt(self):
1221
        results = get_result("md/nvt")
Lauri Himanen's avatar
Lauri Himanen committed
1222
1223
1224
        ensemble = results["ensemble_type"]
        self.assertEqual(ensemble, "NVT")

1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
    def test_reftraj(self):
        results = get_result("md/reftraj")

        cp2k_ensemble = results["x_cp2k_md_ensemble_type"]
        self.assertEqual(cp2k_ensemble, "REFTRAJ")

        # Currently there is no metainfo type for reftraj
        with self.assertRaises(KeyError):
            results["ensemble_type"]

        results["atom_positions"]
        results["atom_labels"]
        results["frame_sequence_potential_energy"]

Lauri Himanen's avatar
Lauri Himanen committed
1239
    def test_npt(self):
1240
        results = get_result("md/npt")
Lauri Himanen's avatar
Lauri Himanen committed
1241
1242
1243
1244
1245
1246
1247
1248
1249
        ensemble = results["ensemble_type"]
        self.assertEqual(ensemble, "NPT")

        pressure = results["frame_sequence_pressure"]
        self.assertTrue(np.array_equal(pressure, self.pressure))

        pressure_stats = results["frame_sequence_pressure_stats"]
        expected_pressure_stats = np.array([self.pressure.mean(), self.pressure.std()])
        self.assertTrue(np.array_equal(pressure_stats, expected_pressure_stats))
Lauri Himanen's avatar
Lauri Himanen committed
1250

1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
        simulation_cell = results["simulation_cell"]
        expected_cell_start = convert_unit(
            np.array(
                [[
                    6.0000000000,
                    0.0000000000,
                    0.0000000000,
                ], [
                    0.0000000000,
                    6.0000000000,
                    0.0000000000,
                ], [
                    0.0000000000,
                    0.0000000000,
                    6.0000000000,
                ]]),
            "angstrom"
        )
        expected_cell_end = convert_unit(
            np.array(
                [[
                    5.9960617905,
                    -0.0068118798,
                    -0.0102043036,
                ], [
                    -0.0068116027,
                    6.0225574669,
                    -0.0155044063,
                ], [
                    -0.0102048226,
                    -0.0155046726,
                    6.0083072343,
                ]]),
            "angstrom"
        )
        self.assertEqual(simulation_cell.shape[0], 11)
1287
1288
        self.assertTrue(np.array_equal(expected_cell_start, simulation_cell[0, :, :]))
        self.assertTrue(np.array_equal(expected_cell_end, simulation_cell[-1, :, :]))
Lauri Himanen's avatar
Lauri Himanen committed
1289

1290

1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
class TestVDWMethod(unittest.TestCase):
    """Tests that different van der Waals methods are recognized correctly.
    """
    def test_d2(self):
        results = get_result("vdw/d2")
        result = results["van_der_Waals_method"]
        self.assertEqual(result, "G06")

    def test_d3(self):
        results = get_result("vdw/d3")
        result = results["van_der_Waals_method"]
        self.assertEqual(result, "G10")

    def test_d3_bj(self):
        results = get_result("vdw/d3_bj")
        result = results["van_der_Waals_method"]
        self.assertEqual(result, "G10")


1310
class TestElectronicStructureMethod(unittest.TestCase):
1311
1312
    """Tests that different methods are recognized correctly.
    """
1313
    def test_mp2(self):
1314
        results = get_result("electronic_structure_method/mp2")
1315
1316
1317
1318
        result = results["electronic_structure_method"]
        self.assertEqual(result, "MP2")

    def test_dft_plus_u(self):
1319
        results = get_result("electronic_structure_method/dft_plus_u")
1320
1321
1322
1323
        result = results["electronic_structure_method"]
        self.assertEqual(result, "DFT+U")

    def test_rpa(self):
1324
        results = get_result("electronic_structure_method/rpa")
1325
1326
1327
1328
        result = results["electronic_structure_method"]
        self.assertEqual(result, "RPA")


1329
1330
1331
1332
1333
1334
1335
class TestCubeFiles(unittest.TestCase):
    """Tests that different methods are recognized correctly.
    """
    def test_electron_density(self):
        results = get_result("cube/single_point_default_name")