File: testopenbabel.py

package info (click to toggle)
cclib 1.8-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 55,300 kB
  • sloc: python: 23,276; makefile: 84; sh: 26
file content (58 lines) | stat: -rw-r--r-- 2,194 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
# -*- coding: utf-8 -*-
#
# Copyright (c) 2023, the cclib development team
#
# This file is part of cclib (http://cclib.github.io) and is distributed under
# the terms of the BSD 3-Clause License.

import os
import unittest

import numpy

from cclib.bridge import cclib2openbabel


class OpenbabelTest(unittest.TestCase):
    """Tests for the cclib2openbabel bridge in cclib."""

    def setUp(self) -> None:
        self.path = os.path.abspath(os.path.dirname(__file__))

    def test_makeopenbabel(self) -> None:
        try:
            from openbabel import openbabel
        except:
            import openbabel
        atomnos = numpy.array([1, 8, 1], "i")
        atomcoords = numpy.array([[[-1., 1., 0.], [0., 0., 0.], [1., 1., 0.]]])
        obmol = cclib2openbabel.makeopenbabel(atomcoords=atomcoords, atomnos=atomnos)
        obconversion = openbabel.OBConversion()
        formatok = obconversion.SetOutFormat("inchi")
        assert obconversion.WriteString(obmol).strip() == "InChI=1S/H2O/h1H2"

    def test_makeopenbabel_and_makecclib(self) -> None:
        """Ensure that makeopenbabel and makecclib are inverse of each other"""
        atomnos = numpy.array([1, 8, 1], "i")
        atomcoords = numpy.array([[[-1., 1., 0.], [0., 0., 0.], [1., 1., 0.]]])

        # makecclib(makeopenbabel(...))
        obmol = cclib2openbabel.makeopenbabel(atomcoords=atomcoords, atomnos=atomnos)
        data = cclib2openbabel.makecclib(obmol)
        numpy.testing.assert_allclose(data.atomcoords, atomcoords)
        numpy.testing.assert_allclose(data.atomnos, atomnos)

        # makeopenbabel(makecclib(...))
        obmol = cclib2openbabel.makeopenbabel(atomcoords=data.atomcoords, atomnos=data.atomnos)
        data = cclib2openbabel.makecclib(obmol)  # this line is just to make the test easier
        numpy.testing.assert_allclose(data.atomcoords, atomcoords)
        numpy.testing.assert_allclose(data.atomnos, atomnos)

    def test_readfile(self) -> None:
        """Try to load an XYZ file with uracyl through Openbabel"""
        data = cclib2openbabel.readfile(f"{self.path}/uracil.xyz", "XYZ")
        assert data.natom == 12


if __name__ == "__main__":
    unittest.main()