예제 #1
0
class TestQMCalculator(unittest.TestCase):
    """
    Contains unit tests for the QMSettings class.
    """

    mopExecutablePath = Mopac.executablePath
    if not os.path.exists(mopExecutablePath):
        NO_MOPAC = NO_LICENCE = True
    else:
        NO_MOPAC = False
        process = subprocess.Popen(mopExecutablePath,
                                   stdin=subprocess.PIPE,
                                   stdout=subprocess.PIPE,
                                   stderr=subprocess.PIPE)
        stdut, stderr = process.communicate(b'\n')
        NO_LICENCE = b'To install the MOPAC license' in stderr

    gaussExecutablePath = Gaussian.executablePath
    NO_GAUSSIAN = not os.path.exists(gaussExecutablePath)

    def setUp(self):
        """
        A function run before each unit test in this class.
        """
        rmg_path = os.path.normpath(os.path.join(get_path(), '..'))
        self.fileStore = os.path.join(rmg_path, 'testing', 'qm', 'QMfiles')

        self.mop1 = QMCalculator(software='mopac',
                                 method='pm3',
                                 fileStore=self.fileStore)

        self.mop2 = QMCalculator(
            software='mopac',
            method='pm6',
        )

        self.mop3 = QMCalculator(software='mopac',
                                 method='pm7',
                                 fileStore=self.fileStore)

        self.mop4 = QMCalculator(software='mopac',
                                 method='pm8',
                                 fileStore=self.fileStore)

        self.gauss1 = QMCalculator(
            software='gaussian',
            method='pm3',
        )

        self.gauss2 = QMCalculator(software='gaussian',
                                   method='pm6',
                                   fileStore=self.fileStore)

        self.gauss3 = QMCalculator(software='gaussian',
                                   method='pm7',
                                   fileStore=self.fileStore)

        self.molpro1 = QMCalculator(software='molpro',
                                    method='mp2',
                                    fileStore=self.fileStore)

        self.qmmol1 = QMCalculator(fileStore=self.fileStore)

        self.qmmol2 = QMCalculator(fileStore=self.fileStore)

    def test_set_default_output_directory(self):
        """
        Test that set_default_output_directory() works correctly.
        """
        self.assertIsNotNone(self.mop1.settings.fileStore)
        self.assertIsNotNone(self.mop3.settings.fileStore)
        self.assertIsNotNone(self.gauss2.settings.fileStore)

        self.assertIsNone(self.mop2.settings.fileStore)
        self.assertIsNone(self.gauss1.settings.fileStore)

        self.assertIsNone(self.mop1.settings.scratchDirectory)
        self.assertIsNone(self.mop2.settings.scratchDirectory)
        self.assertIsNone(self.mop3.settings.scratchDirectory)
        self.assertIsNone(self.gauss1.settings.scratchDirectory)
        self.assertIsNone(self.gauss2.settings.scratchDirectory)

        # Now set the default directories for those not set
        outputDirectory = os.path.join(self.mop1.settings.fileStore, '..',
                                       '..')
        self.mop1.set_default_output_directory(outputDirectory)
        self.mop2.set_default_output_directory(outputDirectory)
        self.mop3.set_default_output_directory(outputDirectory)
        self.gauss1.set_default_output_directory(outputDirectory)
        self.gauss2.set_default_output_directory(outputDirectory)

        self.assertIsNotNone(self.mop1.settings.fileStore)
        self.assertIsNotNone(self.mop2.settings.fileStore)
        self.assertIsNotNone(self.mop3.settings.fileStore)
        self.assertIsNotNone(self.gauss1.settings.fileStore)
        self.assertIsNotNone(self.gauss2.settings.fileStore)
        self.assertIsNotNone(self.mop1.settings.scratchDirectory)
        self.assertIsNotNone(self.mop2.settings.scratchDirectory)
        self.assertIsNotNone(self.mop3.settings.scratchDirectory)
        self.assertIsNotNone(self.gauss1.settings.scratchDirectory)
        self.assertIsNotNone(self.gauss2.settings.scratchDirectory)

    def test_initialize(self):
        """
        Test that initialize() works correctly.
        """

        # Now set the default directories for those not set
        outputDirectory = os.path.join(self.mop1.settings.fileStore, '..',
                                       '..')
        self.mop1.set_default_output_directory(outputDirectory)
        self.mop2.set_default_output_directory(outputDirectory)
        self.mop3.set_default_output_directory(outputDirectory)
        self.gauss1.set_default_output_directory(outputDirectory)
        self.gauss2.set_default_output_directory(outputDirectory)

        try:
            self.mop1.initialize()
            self.mop2.initialize()
            self.mop3.initialize()
            self.gauss1.initialize()
            self.gauss2.initialize()
        except AssertionError:
            self.fail("initialize() raised unexpected AssertionError.")
        except Exception:
            self.fail(
                "initialize() raised Exception. Output file paths not correctly set."
            )

    def test_get_thermo_data(self):
        """
        Test that get_thermo_data() fails when expected.
        """
        output_directory = os.path.join(self.mop4.settings.fileStore, '..',
                                        '..')
        self.mop4.set_default_output_directory(output_directory)
        self.gauss3.set_default_output_directory(output_directory)
        self.molpro1.set_default_output_directory(output_directory)

        mol = Molecule().from_smiles('C1=CC=C2C=CC=CC2=C1')

        with self.assertRaises(Exception):
            self.mop4.get_thermo_data(mol)
            self.gauss3.get_thermo_data(mol)
            self.molpro1.get_thermo_data(mol)

    @unittest.skipIf(
        NO_MOPAC,
        "MOPAC not found. Try resetting your environment variables if you want to use it."
    )
    @unittest.skipIf(NO_LICENCE,
                     "MOPAC license not installed. Run mopac for instructions")
    def test_get_thermo_data_mopac(self):
        """
        Test that Mocpac get_thermo_data() works correctly.
        """
        output_directory = os.path.join(self.mop1.settings.fileStore, '..',
                                        '..')
        self.mop1.set_default_output_directory(output_directory)
        self.mop2.set_default_output_directory(output_directory)
        self.mop3.set_default_output_directory(output_directory)

        mol = Molecule().from_smiles('C1=CC=C2C=CC=CC2=C1')

        for directory in (self.mop1.settings.fileStore,
                          self.mop1.settings.scratchDirectory):
            shutil.rmtree(directory, ignore_errors=True)

        for directory in (self.mop2.settings.fileStore,
                          self.mop2.settings.scratchDirectory):
            shutil.rmtree(directory, ignore_errors=True)

        for directory in (self.mop3.settings.fileStore,
                          self.mop3.settings.scratchDirectory):
            shutil.rmtree(directory, ignore_errors=True)

        thermo1 = self.mop1.get_thermo_data(mol)
        thermo2 = self.mop2.get_thermo_data(mol)
        thermo3 = self.mop3.get_thermo_data(mol)

        self.assertTrue(thermo1.comment.startswith('QM MopacMolPM3'))
        self.assertTrue(thermo2.comment.startswith('QM MopacMolPM6'))
        self.assertTrue(thermo3.comment.startswith('QM MopacMolPM7'))

        self.assertAlmostEqual(thermo1.H298.value_si, 169708.0608,
                               1)  # to 1 decimal place
        self.assertAlmostEqual(thermo1.S298.value_si, 334.5007584,
                               1)  # to 1 decimal place
        self.assertAlmostEqual(thermo2.H298.value_si, 167704.4270,
                               1)  # to 1 decimal place
        self.assertAlmostEqual(thermo2.S298.value_si, 338.0999241,
                               1)  # to 1 decimal place
        self.assertAlmostEqual(thermo3.H298.value_si, 166168.8571,
                               1)  # to 1 decimal place
        self.assertAlmostEqual(thermo3.S298.value_si, 336.3330406,
                               1)  # to 1 decimal place

    @unittest.skipIf(
        NO_GAUSSIAN,
        "Gaussian not found. Try resetting your environment variables if you want to use it."
    )
    def test_get_thermo_data_gaussian(self):
        """
        Test that Gaussian get_thermo_data() works correctly.
        """
        output_directory = os.path.join(self.mop1.settings.fileStore, '..',
                                        '..')
        self.gauss1.set_default_output_directory(output_directory)
        self.gauss2.set_default_output_directory(output_directory)

        mol = Molecule().from_smiles('C1=CC=C2C=CC=CC2=C1')

        for directory in (self.gauss1.settings.fileStore,
                          self.gauss1.settings.scratchDirectory):
            shutil.rmtree(directory, ignore_errors=True)

        for directory in (self.gauss1.settings.fileStore,
                          self.gauss2.settings.scratchDirectory):
            shutil.rmtree(directory, ignore_errors=True)

        thermo1 = self.gauss1.get_thermo_data(mol)
        thermo2 = self.gauss2.get_thermo_data(mol)

        self.assertTrue(thermo1.comment.startswith('QM GaussianMolPM3'))
        self.assertTrue(thermo2.comment.startswith('QM GaussianMolPM6'))

        self.assertAlmostEqual(thermo1.H298.value_si, 169908.3376,
                               0)  # to 1 decimal place
        self.assertAlmostEqual(thermo1.S298.value_si, 335.5438748,
                               0)  # to 1 decimal place
        self.assertAlmostEqual(thermo2.H298.value_si, 169326.2504,
                               0)  # to 1 decimal place
        self.assertAlmostEqual(thermo2.S298.value_si, 338.2696063,
                               0)  # to 1 decimal place

    @unittest.skipIf(
        NO_MOPAC,
        "MOPAC not found. Try resetting your environment variables if you want to use it."
    )
    @unittest.skipIf(NO_LICENCE,
                     "MOPAC license not installed. Run mopac for instructions")
    def test_run_jobs(self):
        """Test that run_jobs() works properly."""
        qm = QMCalculator(
            software='mopac',
            method='pm3',
            fileStore=self.fileStore,
            onlyCyclics=True,
            maxRadicalNumber=0,
        )
        output_directory = os.path.join(qm.settings.fileStore, '..', '..')
        qm.set_default_output_directory(output_directory)

        spc1 = Species().from_smiles('c1ccccc1')
        spc2 = Species().from_smiles('CC1C=CC=CC=1')
        spc_list = [spc1, spc2]

        qm.run_jobs(spc_list, procnum=1)
예제 #2
0
파일: mainTest.py 프로젝트: Alborzi/RMG-Py
class TestQMCalculator(unittest.TestCase):
	"""
	Contains unit tests for the QMSettings class.
	"""
	
	mopacEnv = os.getenv('MOPAC_DIR', default="/opt/mopac")
	if os.path.exists(os.path.join(mopacEnv , 'MOPAC2012.exe')):
		mopExecutablePath = os.path.join(mopacEnv , 'MOPAC2012.exe')
	elif os.path.exists(os.path.join(mopacEnv , 'MOPAC2009.exe')):
		mopExecutablePath = os.path.join(mopacEnv , 'MOPAC2009.exe')
	else:
		mopExecutablePath = os.path.join(mopacEnv , '(MOPAC 2009 or 2012)')
	
	gaussEnv = os.getenv('GAUSS_EXEDIR') or os.getenv('g09root') or os.getenv('g03root') or ""
	# GAUSS_EXEDIR may be a list like "path1:path2:path3"
	for possibleDir in gaussEnv.split(':'):
		if os.path.exists(os.path.join(possibleDir , 'g09')):
			gaussExecutablePath = os.path.join(possibleDir , 'g09')
			break
		elif os.path.exists(os.path.join(possibleDir , 'g03')):
			gaussExecutablePath = os.path.join(possibleDir , 'g03')
			break
	else:
		gaussExecutablePath = os.path.join(gaussEnv , '(g03 or g09)')
	
	def setUp(self):
		"""
		A function run before each unit test in this class.
		"""
		RMGpy_path = os.path.normpath(os.path.join(getPath(),'..'))
		
		fileStore = os.path.join(RMGpy_path, 'testing', 'qm', 'QMfiles')
		
		self.mop1 = QMCalculator(software = 'mopac',
								method = 'pm3',
								fileStore = fileStore
								)
				
		self.mop2 = QMCalculator(software = 'mopac',
								method = 'pm6',
								)
		
		self.mop3 = QMCalculator(software = 'mopac',
								method = 'pm7',
								fileStore = fileStore
								)
		
		self.mop4 = QMCalculator(software = 'mopac',
								method = 'pm8',
								fileStore = fileStore
								)
		
		self.gauss1 = QMCalculator(software = 'gaussian',
								  method = 'pm3',
								  )	
		
		self.gauss2 = QMCalculator(software = 'gaussian',
								  method = 'pm6',
								  fileStore = fileStore
								  )
		
		self.gauss3 = QMCalculator(software = 'gaussian',
								  method = 'pm7',
								  fileStore = fileStore
								  )
		
		self.molpro1 = QMCalculator(software = 'molpro',
								   method = 'mp2',
								   fileStore = fileStore
								   )
		
		self.qmmol1 = QMCalculator(fileStore=fileStore)
		
		self.qmmol2 = QMCalculator(fileStore=fileStore)

	def testSetDefaultOutputDirectory(self):
		"""
		Test that setDefaultOutputDirectory() works correctly.
		"""
		self.assertIsNotNone(self.mop1.settings.fileStore)
		self.assertIsNotNone(self.mop3.settings.fileStore)
		self.assertIsNotNone(self.gauss2.settings.fileStore)
		
		self.assertIsNone(self.mop2.settings.fileStore)
		self.assertIsNone(self.gauss1.settings.fileStore)
		
		self.assertIsNone(self.mop1.settings.scratchDirectory)
		self.assertIsNone(self.mop2.settings.scratchDirectory)
		self.assertIsNone(self.mop3.settings.scratchDirectory)
		self.assertIsNone(self.gauss1.settings.scratchDirectory)
		self.assertIsNone(self.gauss2.settings.scratchDirectory)
		
		# Now set the default directories for those not set
		outputDirectory = os.path.join(self.mop1.settings.fileStore, '..','..')
		self.mop1.setDefaultOutputDirectory(outputDirectory)
		self.mop2.setDefaultOutputDirectory(outputDirectory)
		self.mop3.setDefaultOutputDirectory(outputDirectory)
		self.gauss1.setDefaultOutputDirectory(outputDirectory)
		self.gauss2.setDefaultOutputDirectory(outputDirectory)
		
		self.assertIsNotNone(self.mop1.settings.fileStore)
		self.assertIsNotNone(self.mop2.settings.fileStore)
		self.assertIsNotNone(self.mop3.settings.fileStore)
		self.assertIsNotNone(self.gauss1.settings.fileStore)
		self.assertIsNotNone(self.gauss2.settings.fileStore)
		self.assertIsNotNone(self.mop1.settings.scratchDirectory)
		self.assertIsNotNone(self.mop2.settings.scratchDirectory)
		self.assertIsNotNone(self.mop3.settings.scratchDirectory)
		self.assertIsNotNone(self.gauss1.settings.scratchDirectory)
		self.assertIsNotNone(self.gauss2.settings.scratchDirectory)
	
	def testInitialize(self):
		"""
		Test that initialize() works correctly.
		"""
		
		# Now set the default directories for those not set
		outputDirectory = os.path.join(self.mop1.settings.fileStore, '..', '..')
		self.mop1.setDefaultOutputDirectory(outputDirectory)
		self.mop2.setDefaultOutputDirectory(outputDirectory)
		self.mop3.setDefaultOutputDirectory(outputDirectory)
		self.gauss1.setDefaultOutputDirectory(outputDirectory)
		self.gauss2.setDefaultOutputDirectory(outputDirectory)
		
		try:
			self.mop1.initialize()
			self.mop2.initialize()
			self.mop3.initialize()
			self.gauss1.initialize()
			self.gauss2.initialize()
		except AssertionError:
			self.fail("checkAllSet() raised unexpected AssertionError.")
		except Exception:
			self.fail("initialize() raised Exception. Output file paths not correctly set.")
	
	def testGetThermoData(self):
		"""
		Test that getThermoData() fails when expected.
		"""
		outputDirectory = os.path.join(self.mop4.settings.fileStore, '..', '..')
		self.mop4.setDefaultOutputDirectory(outputDirectory)
		self.gauss3.setDefaultOutputDirectory(outputDirectory)
		self.molpro1.setDefaultOutputDirectory(outputDirectory)
		
		mol = Molecule().fromSMILES('C1=CC=C2C=CC=CC2=C1')
		
		with self.assertRaises(Exception):
			self.mop4.getThermoData(mol)
			self.gauss3.getThermoData(mol)
			self.molpro1.getThermoData(mol)
		
	@unittest.skipIf(os.path.exists(mopExecutablePath)==False, "If MOPAC installed, try checking your environment variables.")
	def testGetThermoDataMopac(self):
		"""
		Test that getThermoData() works correctly.
		"""
		outputDirectory = os.path.join(self.mop1.settings.fileStore, '..', '..')
		self.mop1.setDefaultOutputDirectory(outputDirectory)
		self.mop2.setDefaultOutputDirectory(outputDirectory)
		self.mop3.setDefaultOutputDirectory(outputDirectory)
		
		mol = Molecule().fromSMILES('C1=CC=C2C=CC=CC2=C1')
		
		try:
			fileList = os.listdir(self.mop1.settings.fileStore)
			for fileName in fileList:
				os.remove(os.path.join(self.mop1.settings.fileStore, fileName))
		except OSError:
			pass		
		
		try:
			fileList = os.listdir(self.mop2.settings.fileStore)
			for fileName in fileList:
				os.remove(os.path.join(self.mop2.settings.fileStore, fileName))
		except OSError:
			pass
		
		try:
			fileList = os.listdir(self.mop3.settings.fileStore)
			for fileName in fileList:
				os.remove(os.path.join(self.mop3.settings.fileStore, fileName))
		except OSError:
			pass
			
		thermo1 = self.mop1.getThermoData(mol)
		thermo2 = self.mop2.getThermoData(mol)
		thermo3 = self.mop3.getThermoData(mol)
			
		self.assertTrue(thermo1.comment.startswith('QM MopacMolPM3'))
		self.assertTrue(thermo2.comment.startswith('QM MopacMolPM6'))
		self.assertTrue(thermo3.comment.startswith('QM MopacMolPM7'))
		
		self.assertAlmostEqual(thermo1.H298.value_si, 169708.0608, 1) # to 1 decimal place
		self.assertAlmostEqual(thermo1.S298.value_si, 334.5007584, 1) # to 1 decimal place
		self.assertAlmostEqual(thermo2.H298.value_si, 167704.4270, 1) # to 1 decimal place
		self.assertAlmostEqual(thermo2.S298.value_si, 338.0999241, 1) # to 1 decimal place
		self.assertAlmostEqual(thermo3.H298.value_si, 166168.8571, 1) # to 1 decimal place
		self.assertAlmostEqual(thermo3.S298.value_si, 336.3330406, 1) # to 1 decimal place
		
	@unittest.skipIf(os.path.exists(gaussExecutablePath)==False, "If GAUSSIAN installed, try checking your environment variables.")
	def testGetThermoDataGaussian(self):
		"""
		Test that getThermoData() works correctly.
		"""
		outputDirectory = os.path.join(self.mop1.settings.fileStore, '..', '..')
		self.gauss1.setDefaultOutputDirectory(outputDirectory)
		self.gauss2.setDefaultOutputDirectory(outputDirectory)
		
		mol = Molecule().fromSMILES('C1=CC=C2C=CC=CC2=C1')
		
		try:
			fileList = os.listdir(self.gauss1.settings.fileStore)
			for fileName in fileList:
				os.remove(os.path.join(self.gauss1.settings.fileStore, fileName))
		except OSError:
			pass	
		
		try:
			fileList = os.listdir(self.gauss2.settings.fileStore)
			for fileName in fileList:
				os.remove(os.path.join(self.gauss2.settings.fileStore, fileName))
		except OSError:
			pass	
					
		thermo1 = self.gauss1.getThermoData(mol)
		thermo2 = self.gauss2.getThermoData(mol)
		
		self.assertTrue(thermo1.comment.startswith('QM GaussianMolPM3'))
		self.assertTrue(thermo2.comment.startswith('QM GaussianMolPM6'))
		
		self.assertAlmostEqual(thermo1.H298.value_si, 169908.3376, 0) # to 1 decimal place
		self.assertAlmostEqual(thermo1.S298.value_si, 335.5438748, 0) # to 1 decimal place
		self.assertAlmostEqual(thermo2.H298.value_si, 169326.2504, 0) # to 1 decimal place
		self.assertAlmostEqual(thermo2.S298.value_si, 338.2696063, 0) # to 1 decimal place
예제 #3
0
class TestQMCalculator(unittest.TestCase):
	"""
	Contains unit tests for the QMSettings class.
	"""
	
	mopExecutablePath = Mopac.executablePath
	if not os.path.exists(mopExecutablePath):
		NO_MOPAC = NO_LICENCE = True
	else:
		NO_MOPAC = False
		process = subprocess.Popen(mopExecutablePath,
	                               stdin=subprocess.PIPE,
	                               stdout=subprocess.PIPE,
	                               stderr=subprocess.PIPE)
		stdut, stderr = process.communicate("\n")
		NO_LICENCE = 'To install the MOPAC license' in stderr

	gaussExecutablePath = Gaussian.executablePath
	NO_GAUSSIAN = not os.path.exists(gaussExecutablePath)

	
	def setUp(self):
		"""
		A function run before each unit test in this class.
		"""
		RMGpy_path = os.path.normpath(os.path.join(getPath(),'..'))
		
		fileStore = os.path.join(RMGpy_path, 'testing', 'qm', 'QMfiles')
		
		self.mop1 = QMCalculator(software = 'mopac',
								method = 'pm3',
								fileStore = fileStore
								)
				
		self.mop2 = QMCalculator(software = 'mopac',
								method = 'pm6',
								)
		
		self.mop3 = QMCalculator(software = 'mopac',
								method = 'pm7',
								fileStore = fileStore
								)
		
		self.mop4 = QMCalculator(software = 'mopac',
								method = 'pm8',
								fileStore = fileStore
								)
		
		self.gauss1 = QMCalculator(software = 'gaussian',
								  method = 'pm3',
								  )	
		
		self.gauss2 = QMCalculator(software = 'gaussian',
								  method = 'pm6',
								  fileStore = fileStore
								  )
		
		self.gauss3 = QMCalculator(software = 'gaussian',
								  method = 'pm7',
								  fileStore = fileStore
								  )
		
		self.molpro1 = QMCalculator(software = 'molpro',
								   method = 'mp2',
								   fileStore = fileStore
								   )
		
		self.qmmol1 = QMCalculator(fileStore=fileStore)
		
		self.qmmol2 = QMCalculator(fileStore=fileStore)

	def testSetDefaultOutputDirectory(self):
		"""
		Test that setDefaultOutputDirectory() works correctly.
		"""
		self.assertIsNotNone(self.mop1.settings.fileStore)
		self.assertIsNotNone(self.mop3.settings.fileStore)
		self.assertIsNotNone(self.gauss2.settings.fileStore)
		
		self.assertIsNone(self.mop2.settings.fileStore)
		self.assertIsNone(self.gauss1.settings.fileStore)
		
		self.assertIsNone(self.mop1.settings.scratchDirectory)
		self.assertIsNone(self.mop2.settings.scratchDirectory)
		self.assertIsNone(self.mop3.settings.scratchDirectory)
		self.assertIsNone(self.gauss1.settings.scratchDirectory)
		self.assertIsNone(self.gauss2.settings.scratchDirectory)
		
		# Now set the default directories for those not set
		outputDirectory = os.path.join(self.mop1.settings.fileStore, '..','..')
		self.mop1.setDefaultOutputDirectory(outputDirectory)
		self.mop2.setDefaultOutputDirectory(outputDirectory)
		self.mop3.setDefaultOutputDirectory(outputDirectory)
		self.gauss1.setDefaultOutputDirectory(outputDirectory)
		self.gauss2.setDefaultOutputDirectory(outputDirectory)
		
		self.assertIsNotNone(self.mop1.settings.fileStore)
		self.assertIsNotNone(self.mop2.settings.fileStore)
		self.assertIsNotNone(self.mop3.settings.fileStore)
		self.assertIsNotNone(self.gauss1.settings.fileStore)
		self.assertIsNotNone(self.gauss2.settings.fileStore)
		self.assertIsNotNone(self.mop1.settings.scratchDirectory)
		self.assertIsNotNone(self.mop2.settings.scratchDirectory)
		self.assertIsNotNone(self.mop3.settings.scratchDirectory)
		self.assertIsNotNone(self.gauss1.settings.scratchDirectory)
		self.assertIsNotNone(self.gauss2.settings.scratchDirectory)
	
	def testInitialize(self):
		"""
		Test that initialize() works correctly.
		"""
		
		# Now set the default directories for those not set
		outputDirectory = os.path.join(self.mop1.settings.fileStore, '..', '..')
		self.mop1.setDefaultOutputDirectory(outputDirectory)
		self.mop2.setDefaultOutputDirectory(outputDirectory)
		self.mop3.setDefaultOutputDirectory(outputDirectory)
		self.gauss1.setDefaultOutputDirectory(outputDirectory)
		self.gauss2.setDefaultOutputDirectory(outputDirectory)
		
		try:
			self.mop1.initialize()
			self.mop2.initialize()
			self.mop3.initialize()
			self.gauss1.initialize()
			self.gauss2.initialize()
		except AssertionError:
			self.fail("initialize() raised unexpected AssertionError.")
		except Exception:
			self.fail("initialize() raised Exception. Output file paths not correctly set.")
	
	def testGetThermoData(self):
		"""
		Test that getThermoData() fails when expected.
		"""
		outputDirectory = os.path.join(self.mop4.settings.fileStore, '..', '..')
		self.mop4.setDefaultOutputDirectory(outputDirectory)
		self.gauss3.setDefaultOutputDirectory(outputDirectory)
		self.molpro1.setDefaultOutputDirectory(outputDirectory)
		
		mol = Molecule().fromSMILES('C1=CC=C2C=CC=CC2=C1')
		
		with self.assertRaises(Exception):
			self.mop4.getThermoData(mol)
			self.gauss3.getThermoData(mol)
			self.molpro1.getThermoData(mol)
		
	@unittest.skipIf(NO_MOPAC, "MOPAC not found. Try resetting your environment variables if you want to use it.")
	@unittest.skipIf(NO_LICENCE, "MOPAC license not installed. Run mopac for instructions")
	def testGetThermoDataMopac(self):
		"""
		Test that Mocpac getThermoData() works correctly.
		"""
		outputDirectory = os.path.join(self.mop1.settings.fileStore, '..', '..')
		self.mop1.setDefaultOutputDirectory(outputDirectory)
		self.mop2.setDefaultOutputDirectory(outputDirectory)
		self.mop3.setDefaultOutputDirectory(outputDirectory)
		
		mol = Molecule().fromSMILES('C1=CC=C2C=CC=CC2=C1')
		
		for directory in (self.mop1.settings.fileStore, self.mop1.settings.scratchDirectory):
			shutil.rmtree(directory, ignore_errors=True)
		
		for directory in (self.mop2.settings.fileStore, self.mop2.settings.scratchDirectory):
			shutil.rmtree(directory, ignore_errors=True)
		
		for directory in (self.mop3.settings.fileStore, self.mop3.settings.scratchDirectory):
			shutil.rmtree(directory, ignore_errors=True)
			
		thermo1 = self.mop1.getThermoData(mol)
		thermo2 = self.mop2.getThermoData(mol)
		thermo3 = self.mop3.getThermoData(mol)
			
		self.assertTrue(thermo1.comment.startswith('QM MopacMolPM3'))
		self.assertTrue(thermo2.comment.startswith('QM MopacMolPM6'))
		self.assertTrue(thermo3.comment.startswith('QM MopacMolPM7'))
		
		self.assertAlmostEqual(thermo1.H298.value_si, 169708.0608, 1) # to 1 decimal place
		self.assertAlmostEqual(thermo1.S298.value_si, 334.5007584, 1) # to 1 decimal place
		self.assertAlmostEqual(thermo2.H298.value_si, 167704.4270, 1) # to 1 decimal place
		self.assertAlmostEqual(thermo2.S298.value_si, 338.0999241, 1) # to 1 decimal place
		self.assertAlmostEqual(thermo3.H298.value_si, 166168.8571, 1) # to 1 decimal place
		self.assertAlmostEqual(thermo3.S298.value_si, 336.3330406, 1) # to 1 decimal place
		
	@unittest.skipIf(NO_GAUSSIAN, "Gaussian not found. Try resetting your environment variables if you want to use it.")
	def testGetThermoDataGaussian(self):
		"""
		Test that Gaussian getThermoData() works correctly.
		"""
		outputDirectory = os.path.join(self.mop1.settings.fileStore, '..', '..')
		self.gauss1.setDefaultOutputDirectory(outputDirectory)
		self.gauss2.setDefaultOutputDirectory(outputDirectory)
		
		mol = Molecule().fromSMILES('C1=CC=C2C=CC=CC2=C1')
		
		for directory in (self.gauss1.settings.fileStore, self.gauss1.settings.scratchDirectory):
			shutil.rmtree(directory, ignore_errors=True)
		
		for directory in (self.gauss1.settings.fileStore, self.gauss2.settings.scratchDirectory):
			shutil.rmtree(directory, ignore_errors=True)
					
		thermo1 = self.gauss1.getThermoData(mol)
		thermo2 = self.gauss2.getThermoData(mol)
		
		self.assertTrue(thermo1.comment.startswith('QM GaussianMolPM3'))
		self.assertTrue(thermo2.comment.startswith('QM GaussianMolPM6'))
		
		self.assertAlmostEqual(thermo1.H298.value_si, 169908.3376, 0) # to 1 decimal place
		self.assertAlmostEqual(thermo1.S298.value_si, 335.5438748, 0) # to 1 decimal place
		self.assertAlmostEqual(thermo2.H298.value_si, 169326.2504, 0) # to 1 decimal place
		self.assertAlmostEqual(thermo2.S298.value_si, 338.2696063, 0) # to 1 decimal place
예제 #4
0
class TestQMCalculator(unittest.TestCase):
	"""
	Contains unit tests for the QMSettings class.
	"""
	
	mopacEnv = os.getenv('MOPAC_DIR', default="/opt/mopac")
	if os.path.exists(os.path.join(mopacEnv , 'MOPAC2012.exe')):
		mopExecutablePath = os.path.join(mopacEnv , 'MOPAC2012.exe')
	elif os.path.exists(os.path.join(mopacEnv , 'MOPAC2009.exe')):
		mopExecutablePath = os.path.join(mopacEnv , 'MOPAC2009.exe')
	else:
		mopExecutablePath = os.path.join(mopacEnv , '(MOPAC 2009 or 2012)')
	
	gaussEnv = os.getenv('GAUSS_EXEDIR') or os.getenv('g09root') or os.getenv('g03root') or ""
	# GAUSS_EXEDIR may be a list like "path1:path2:path3"
	for possibleDir in gaussEnv.split(':'):
		if os.path.exists(os.path.join(possibleDir , 'g09')):
			gaussExecutablePath = os.path.join(possibleDir , 'g09')
			break
		elif os.path.exists(os.path.join(possibleDir , 'g03')):
			gaussExecutablePath = os.path.join(possibleDir , 'g03')
			break
	else:
		gaussExecutablePath = os.path.join(gaussEnv , '(g03 or g09)')
	
	def setUp(self):
		"""
		A function run before each unit test in this class.
		"""
		RMGpy_path = os.path.normpath(os.path.join(getPath(),'..'))
		
		fileStore = os.path.join(RMGpy_path, 'testing', 'qm', 'QMfiles')
		
		self.mop1 = QMCalculator(software = 'mopac',
								method = 'pm3',
								fileStore = fileStore
								)
				
		self.mop2 = QMCalculator(software = 'mopac',
								method = 'pm6',
								)
		
		self.mop3 = QMCalculator(software = 'mopac',
								method = 'pm7',
								fileStore = fileStore
								)
		
		self.mop4 = QMCalculator(software = 'mopac',
								method = 'pm8',
								fileStore = fileStore
								)
		
		self.gauss1 = QMCalculator(software = 'gaussian',
								  method = 'pm3',
								  )	
		
		self.gauss2 = QMCalculator(software = 'gaussian',
								  method = 'pm6',
								  fileStore = fileStore
								  )
		
		self.gauss3 = QMCalculator(software = 'gaussian',
								  method = 'pm7',
								  fileStore = fileStore
								  )
		
		self.molpro1 = QMCalculator(software = 'molpro',
								   method = 'mp2',
								   fileStore = fileStore
								   )
		
		self.qmmol1 = QMCalculator(fileStore=fileStore)
		self.qmmol1.RMG_bin_path = os.path.join(RMGpy_path, 'testing', 'qm', 'bin')
		
		self.qmmol2 = QMCalculator(fileStore=fileStore)
		self.qmmol2.RMG_bin_path = os.path.join(RMGpy_path, 'testing', 'hexadiene', 'input.py')

	def testSetDefaultOutputDirectory(self):
		"""
		Test that setDefaultOutputDirectory() works correctly.
		"""
		self.assertIsNotNone(self.mop1.settings.fileStore)
		self.assertIsNotNone(self.mop3.settings.fileStore)
		self.assertIsNotNone(self.gauss2.settings.fileStore)
		
		self.assertIsNone(self.mop2.settings.fileStore)
		self.assertIsNone(self.gauss1.settings.fileStore)
		
		self.assertIsNone(self.mop1.settings.scratchDirectory)
		self.assertIsNone(self.mop2.settings.scratchDirectory)
		self.assertIsNone(self.mop3.settings.scratchDirectory)
		self.assertIsNone(self.gauss1.settings.scratchDirectory)
		self.assertIsNone(self.gauss2.settings.scratchDirectory)
		
		# Now set the default directories for those not set
		outputDirectory = os.path.join(self.mop1.settings.fileStore, '..','..')
		self.mop1.setDefaultOutputDirectory(outputDirectory)
		self.mop2.setDefaultOutputDirectory(outputDirectory)
		self.mop3.setDefaultOutputDirectory(outputDirectory)
		self.gauss1.setDefaultOutputDirectory(outputDirectory)
		self.gauss2.setDefaultOutputDirectory(outputDirectory)
		
		self.assertIsNotNone(self.mop1.settings.fileStore)
		self.assertIsNotNone(self.mop2.settings.fileStore)
		self.assertIsNotNone(self.mop3.settings.fileStore)
		self.assertIsNotNone(self.gauss1.settings.fileStore)
		self.assertIsNotNone(self.gauss2.settings.fileStore)
		self.assertIsNotNone(self.mop1.settings.scratchDirectory)
		self.assertIsNotNone(self.mop2.settings.scratchDirectory)
		self.assertIsNotNone(self.mop3.settings.scratchDirectory)
		self.assertIsNotNone(self.gauss1.settings.scratchDirectory)
		self.assertIsNotNone(self.gauss2.settings.scratchDirectory)

	def testCheckPaths(self):
		"""
		Test that checkPaths() works correctly.
		"""
		
		with self.assertRaises(Exception):
			self.qmmol1.checkPaths()
			self.qmmol2.checkPaths()
	
	def testInitialize(self):
		"""
		Test that initialize() works correctly.
		"""
		
		# Now set the default directories for those not set
		outputDirectory = os.path.join(self.mop1.settings.fileStore, '..', '..')
		self.mop1.setDefaultOutputDirectory(outputDirectory)
		self.mop2.setDefaultOutputDirectory(outputDirectory)
		self.mop3.setDefaultOutputDirectory(outputDirectory)
		self.gauss1.setDefaultOutputDirectory(outputDirectory)
		self.gauss2.setDefaultOutputDirectory(outputDirectory)
		
		try:
			self.mop1.initialize()
			self.mop2.initialize()
			self.mop3.initialize()
			self.gauss1.initialize()
			self.gauss2.initialize()
		except AssertionError:
			self.fail("checkAllSet() raised unexpected AssertionError.")
		except Exception:
			self.fail("initialize() raised Exception. Output file paths not correctly set.")
	
	def testGetThermoData(self):
		"""
		Test that getThermoData() fails when expected.
		"""
		outputDirectory = os.path.join(self.mop4.settings.fileStore, '..', '..')
		self.mop4.setDefaultOutputDirectory(outputDirectory)
		self.gauss3.setDefaultOutputDirectory(outputDirectory)
		self.molpro1.setDefaultOutputDirectory(outputDirectory)
		
		mol = Molecule().fromSMILES('C1=CC=C2C=CC=CC2=C1')
		
		with self.assertRaises(Exception):
			self.mop4.getThermoData(mol)
			self.gauss3.getThermoData(mol)
			self.molpro1.getThermoData(mol)
		
	@unittest.skipIf(os.path.exists(mopExecutablePath)==False, "If MOPAC installed, try checking your environment variables.")
	def testGetThermoDataMopac(self):
		"""
		Test that getThermoData() works correctly.
		"""
		outputDirectory = os.path.join(self.mop1.settings.fileStore, '..', '..')
		self.mop1.setDefaultOutputDirectory(outputDirectory)
		self.mop2.setDefaultOutputDirectory(outputDirectory)
		self.mop3.setDefaultOutputDirectory(outputDirectory)
		
		mol = Molecule().fromSMILES('C1=CC=C2C=CC=CC2=C1')
		
		try:
			fileList = os.listdir(self.mop1.settings.fileStore)
			for fileName in fileList:
				os.remove(os.path.join(self.mop1.settings.fileStore, fileName))
		except OSError:
			pass		
		
		try:
			fileList = os.listdir(self.mop2.settings.fileStore)
			for fileName in fileList:
				os.remove(os.path.join(self.mop2.settings.fileStore, fileName))
		except OSError:
			pass
		
		try:
			fileList = os.listdir(self.mop3.settings.fileStore)
			for fileName in fileList:
				os.remove(os.path.join(self.mop3.settings.fileStore, fileName))
		except OSError:
			pass
			
		thermo1 = self.mop1.getThermoData(mol)
		thermo2 = self.mop2.getThermoData(mol)
		thermo3 = self.mop3.getThermoData(mol)
			
		self.assertTrue(thermo1.comment.startswith('QM MopacMolPM3'))
		self.assertTrue(thermo2.comment.startswith('QM MopacMolPM6'))
		self.assertTrue(thermo3.comment.startswith('QM MopacMolPM7'))
		
		self.assertAlmostEqual(thermo1.H298.value_si, 169708.0608, 1) # to 1 decimal place
		self.assertAlmostEqual(thermo1.S298.value_si, 334.5007584, 1) # to 1 decimal place
		self.assertAlmostEqual(thermo2.H298.value_si, 167704.4270, 1) # to 1 decimal place
		self.assertAlmostEqual(thermo2.S298.value_si, 338.0999241, 1) # to 1 decimal place
		self.assertAlmostEqual(thermo3.H298.value_si, 166168.8571, 1) # to 1 decimal place
		self.assertAlmostEqual(thermo3.S298.value_si, 336.3330406, 1) # to 1 decimal place
		
	@unittest.skipIf(os.path.exists(gaussExecutablePath)==False, "If GAUSSIAN installed, try checking your environment variables.")
	def testGetThermoDataGaussian(self):
		"""
		Test that getThermoData() works correctly.
		"""
		outputDirectory = os.path.join(self.mop1.settings.fileStore, '..', '..')
		self.gauss1.setDefaultOutputDirectory(outputDirectory)
		self.gauss2.setDefaultOutputDirectory(outputDirectory)
		
		mol = Molecule().fromSMILES('C1=CC=C2C=CC=CC2=C1')
		
		try:
			fileList = os.listdir(self.gauss1.settings.fileStore)
			for fileName in fileList:
				os.remove(os.path.join(self.gauss1.settings.fileStore, fileName))
		except OSError:
			pass	
		
		try:
			fileList = os.listdir(self.gauss2.settings.fileStore)
			for fileName in fileList:
				os.remove(os.path.join(self.gauss2.settings.fileStore, fileName))
		except OSError:
			pass	
					
		thermo1 = self.gauss1.getThermoData(mol)
		thermo2 = self.gauss2.getThermoData(mol)
		
		self.assertTrue(thermo1.comment.startswith('QM GaussianMolPM3'))
		self.assertTrue(thermo2.comment.startswith('QM GaussianMolPM6'))
		
		self.assertAlmostEqual(thermo1.H298.value_si, 169908.3376, 0) # to 1 decimal place
		self.assertAlmostEqual(thermo1.S298.value_si, 335.5438748, 0) # to 1 decimal place
		self.assertAlmostEqual(thermo2.H298.value_si, 169326.2504, 0) # to 1 decimal place
		self.assertAlmostEqual(thermo2.S298.value_si, 338.2696063, 0) # to 1 decimal place