File: python-open.py

package info (click to toggle)
lammps 20210122~gita77bb%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 331,996 kB
  • sloc: cpp: 802,213; python: 24,256; xml: 14,949; f90: 10,448; ansic: 8,476; perl: 4,161; sh: 3,466; fortran: 2,805; makefile: 1,250; objc: 238; lisp: 163; csh: 16; awk: 14; tcl: 6
file content (89 lines) | stat: -rw-r--r-- 2,876 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

import sys,os,unittest
from lammps import lammps

has_mpi=False
has_mpi4py=False
try:
    from mpi4py import __version__ as mpi4py_version
    # tested to work with mpi4py versions 2 and 3
    has_mpi4py = mpi4py_version.split('.')[0] in ['2','3']
except:
    pass

try:
    if 'LAMMPS_MACHINE_NAME' in os.environ:
        machine = os.environ['LAMMPS_MACHINE_NAME']
    else:
        machine = ""
    lmp = lammps(name=machine)
    has_mpi = lmp.has_mpi_support
    has_exceptions = lmp.has_exceptions
    lmp.close()
except:
    pass

class PythonOpen(unittest.TestCase):

    def setUp(self):
        self.machine=None
        if 'LAMMPS_MACHINE_NAME' in os.environ:
            self.machine=os.environ['LAMMPS_MACHINE_NAME']

    def testNoArgs(self):
        """Create LAMMPS instance without any arguments"""

        lmp=lammps(name=self.machine)
        self.assertIsNot(lmp.lmp,None)
        self.assertEqual(lmp.opened,1)
        self.assertEqual(has_mpi4py,lmp.has_mpi4py)
        self.assertEqual(has_mpi,lmp.has_mpi_support)
        lmp.close()
        self.assertIsNone(lmp.lmp,None)
        self.assertEqual(lmp.opened,0)

    def testWithArgs(self):
        """Create LAMMPS instance with a few arguments"""
        lmp=lammps(name=self.machine,
                   cmdargs=['-nocite','-sf','opt','-log','none'])
        self.assertIsNot(lmp.lmp,None)
        self.assertEqual(lmp.opened,1)

    @unittest.skipIf(not (has_mpi and has_mpi4py),"Skipping MPI test since LAMMPS is not parallel or mpi4py is not found")
    def testWithMPI(self):
        from mpi4py import MPI
        mycomm=MPI.Comm.Split(MPI.COMM_WORLD, 0, 1)
        lmp=lammps(name=self.machine,comm=mycomm)
        self.assertIsNot(lmp.lmp,None)
        self.assertEqual(lmp.opened,1)
        lmp.close()

    @unittest.skipIf(not has_exceptions,"Skipping death test since LAMMPS isn't compiled with exception support")
    def testUnknownCommand(self):
        lmp = lammps(name=self.machine)

        with self.assertRaisesRegex(Exception, "ERROR: Unknown command: write_paper"):
            lmp.command("write_paper")

        lmp.close()

    @unittest.skipIf(not has_exceptions,"Skipping death test since LAMMPS isn't compiled with exception support")
    def testUnknownCommandInList(self):
        lmp = lammps(name=self.machine)

        with self.assertRaisesRegex(Exception, "ERROR: Unknown command: write_paper"):
            lmp.commands_list(["write_paper"])

        lmp.close()

    @unittest.skipIf(not has_exceptions,"Skipping death test since LAMMPS isn't compiled with exception support")
    def testUnknownCommandInList(self):
        lmp = lammps(name=self.machine)

        with self.assertRaisesRegex(Exception, "ERROR: Unknown command: write_paper"):
            lmp.commands_string("write_paper")

        lmp.close()

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