<commit_before>#!/usr/bin/env python
"""\
Rick's stab at unit testing

<beavis>heh, heh, he said *unit*</beavis>
"""

import unittest
from PyQuante.CI import CIS
from PyQuante.Ints import getbasis,getints
from PyQuante.Molecule import Molecule
from PyQuante.MP import MP2
from PyQuante.SCF import SCF,USCF

tol = 1e-5

class H2Test(unittest.TestCase):
    def setUp(self):
        self.h2 = Molecule('H2',atomlist=[(1,(0.35,0,0)),(1,(-0.35,0,0))],
                           units='Angs')

    def testHF(self):
        "HF energy of H2 close to -1.130501?"
        results = SCF(self.h2)
        self.assertAlmostEqual(results[0],-1.130501,4)

    def testSVWN(self):
        "SVWN energy of H2 close to -1.132710?"
        results = SCF(self.h2,functional='SVWN')
        self.assertAlmostEqual(results[0],-1.132710,4)

    def testBLYP(self):
        "BLYP energy of H2 close to -1.166221?"
        # This is slooooow.
        results = SCF(self.h2,functional='BLYP')
        self.assertAlmostEqual(results[0],-1.166221,4)

    def testHFFT(self):
        "HF energy of H2 at temp=1e4 is close to -1.130502?"
        results = SCF(self.h2,etemp=1e4)
        self.assertAlmostEqual(results[0],-1.130502,4)

    def testSVWNFT(self):
        "SVW energy of H2 at temp=1e4 is close to -1.132473?"
        results = SCF(self.h2,functional='SVWN',etemp=1e4)
        self.assertAlmostEqual(results[0],-1.132473,4)

    def testMP2(self):
        "MP2 energy of H2 is close to -1.156769"
        bfs = getbasis(self.h2)
        S,h,Ints = getints(bfs,self.h2)
        nbf = len(bfs)
        nclosed,nopen = self.h2.get_closedopen()
        en,orbe,orbs = SCF(self.h2,integrals=(S,h,Ints))
        emp2 = MP2(Ints,orbs,orbe,nclosed,nbf-nclosed)
        self.assertAlmostEqual(en+emp2,-1.156769,4)        

    def testCIS(self):
        "CIS energy of H2 is close to -0.559048"
        bfs = getbasis(self.h2)
        S,h,Ints = getints(bfs,self.h2)
        en,orbe,orbs = SCF(self.h2,integrals=(S,h,Ints))
        occs = [1.]+[0.]*9
        Ecis = CIS(Ints,orbs,orbe,occs,en)
        self.assertAlmostEqual(Ecis[0],-0.559048,4)
    
class LiTest(unittest.TestCase):
    def setUp(self):
        self.li = Molecule('Li',atomlist = [(3,(0,0,0))],multiplicity=2)
        self.li_p = Molecule('Li+',atomlist = [(3,(0,0,0))],charge=1)
        self.li_m = Molecule('Li-',atomlist = [(3,(0,0,0))],charge=-1)

    def testUHF(self):
        "UHF energy of Li is close to -7.431364"
        results = USCF(self.li)
        self.assertAlmostEqual(results[0],-7.431364,4)

    def testHFCation(self):
        "HF energy of Li+ is close to -7.235536"
        results = SCF(self.li_p)
        self.assertAlmostEqual(results[0],-7.235536,4)

    def testHFAnion(self):
        "HF energy of Li- is close to -7.407030"
        results = SCF(self.li_m)
        self.assertAlmostEqual(results[0],-7.407030,4)

    def testSVWN(self):
        "SVWN energy of Li is close to -7.332050"
        results = SCF(self.li,functional='SVWN')
        self.assertAlmostEqual(results[0],-7.332050,4)

    def testSVWNFT(self):
        "SVWN energy of Li at temp=1e4 is close to -7.349422"
        results = SCF(self.li,functional='SVWN',etemp=1e4)
        self.assertAlmostEqual(results[0],-7.349422,4)

def suite():
    import ricks_unit
    return unittest.TestLoader().loadTestsFromModule(ricks_unit)
        
if __name__ == '__main__':
    fullsuite = suite()
    unittest.TextTestRunner(verbosity=2).run(fullsuite)<commit_msg>Modifications to the unit test routines that should fully reproduce the capability of what is in TessSweet.py<commit_after>#!/usr/bin/env python
"""\
UnitSweet.py - Unit testing for Python.

<beavis>heh, heh, he said *unit*</beavis>
"""

import unittest
from PyQuante.CI import CIS
from PyQuante.Molecule import Molecule
from PyQuante.MP import MP2
from PyQuante.HFSolver import HFSolver
from PyQuante.UHFSolver import UHFSolver
from PyQuante.DFTSolver import DFTSolver
from PyQuante.OEP import oep_hf,oep_hf_an
from PyQuante.MINDO3 import scf

class HFUnitTests(unittest.TestCase):
    def setUp(self):
        from PyQuante.Molecule import Molecule
        self.h2 = Molecule('H2',atomlist=[(1,(0.35,0,0)),(1,(-0.35,0,0))],units='Angs')
        self.he = Molecule('He',atomlist = [(2,(0,0,0))])
        self.li = Molecule('Li',atomlist = [(3,(0,0,0))],multiplicity=2)
        self.li_p = Molecule('Li+',atomlist = [(3,(0,0,0))],charge=1)
        self.li_m = Molecule('Li-',atomlist = [(3,(0,0,0))],charge=-1)

    def testH2HF(self):
        h2_hf = HFSolver(self.h2)
        h2_hf.iterate()
        self.assertAlmostEqual(h2_hf.energy,-1.130501,4)

    def testH2HFFT(self):
        h2_hf = HFSolver(self.h2)
        h2_hf.iterate(etemp=1e4)
        self.assertAlmostEqual(h2_hf.energy,-1.130502,4)

    def testHeHF(self):
        he_hf = HFSolver(self.he)
        he_hf.iterate()
        self.assertAlmostEqual(he_hf.energy,-2.855260,4)

    def testLiHF(self):
        li_hf = HFSolver(self.li)
        li_hf.iterate()
        return

    def testLipHF(self):
        li_p_hf = HFSolver(self.li_p)
        li_p_hf.iterate()
        self.assertAlmostEqual(li_p_hf.energy,-7.235536,4)

    def testLimHF(self):
        li_m_hf = HFSolver(self.li_m)
        li_m_hf.iterate()
        self.assertAlmostEqual(li_m_hf.energy,-7.407030,4)

class DFTUnitTests(unittest.TestCase):
    def setUp(self):
        from PyQuante.Molecule import Molecule
        self.h2 = Molecule('H2',atomlist=[(1,(0.35,0,0)),(1,(-0.35,0,0))],units='Angs')
        self.he = Molecule('He',atomlist = [(2,(0,0,0))])
        self.li = Molecule('Li',atomlist = [(3,(0,0,0))],multiplicity=2)

    def testLiLDA(self):
        li_lda = DFTSolver(self.li)
        li_lda.iterate()
        self.assertAlmostEqual(li_lda.energy,-7.332050,4)

    def testLiLDAFT(self):
        li_lda = DFTSolver(self.li)
        li_lda.iterate(etemp=1e4)
        self.assertAlmostEqual(li_lda.energy,-7.349422,4)

    def testH2LDA(self):
        h2_lda = DFTSolver(self.h2)
        h2_lda.iterate()
        self.assertAlmostEqual(h2_lda.energy,-1.132710,4)

    def testH2LDAFT(self):
        h2_lda = DFTSolver(self.h2)
        h2_lda.iterate(etemp=1e4)
        self.assertAlmostEqual(h2_lda.energy,-1.132473,4)

    def testH2BLYP(self):
        h2_blyp = DFTSolver(self.h2,functional='BLYP')
        h2_blyp.iterate()
        self.assertAlmostEqual(h2_blyp.energy,-1.166221,4)

class UHFUnitTests(unittest.TestCase):
    def setUp(self):
        from PyQuante.Molecule import Molecule
        self.li = Molecule('Li',atomlist = [(3,(0,0,0))],multiplicity=2)

    def testLiUHF(self):
        li_uhf = UHFSolver(self.li)
        li_uhf.iterate()
        self.assertAlmostEqual(li_uhf.energy,-7.431364,4)

    def testLiUHFFT(self):
        li_uhf = UHFSolver(self.li)
        li_uhf.iterate(etemp=1e4)
        return

class OtherUnitTests(unittest.TestCase):
    def setUp(self):
        self.h2 = Molecule('H2',atomlist=[(1,(0.35,0,0)),(1,(-0.35,0,0))],
                           units='Angs')
        self.lih = Molecule('LiH',[(1,(0,0,1.5)),(3,(0,0,-1.5))],units = 'Bohr')
        self.h2o = Molecule('h2o',[(8,(0,0,0)),(1,(1.,0,0)),(1,(0,1.,0))])
        self.ohm = Molecule('oh',[(8,(0,0,0)),(1,(1.,0,0))])

    def testH2OMINDO(self):
        en = scf(self.h2o)
        self.assertAlmostEqual(en,-48.825159,4)

    def testOHMMINDO(self):
        en = scf(self.ohm)
        self.assertAlmostEqual(en,18.127533,4)

    def testMP2(self):
        h2_hf = HFSolver(self.h2)
        h2_hf.iterate()
        nclosed,nopen = self.h2.get_closedopen()
        nbf = len(h2_hf.bfs)
        emp2 = MP2(h2_hf.Ints,h2_hf.orbs,h2_hf.orbe,nclosed,nbf-nclosed)
        self.assertAlmostEqual(h2_hf.energy+emp2,-1.156769,4)        

    def testCIS(self):
        h2_hf = HFSolver(self.h2)
        h2_hf.iterate()
        occs = [1.]+[0.]*9
        Ecis = CIS(h2_hf.Ints,h2_hf.orbs,h2_hf.orbe,occs,h2_hf.energy)
        self.assertAlmostEqual(Ecis[0],-0.559048,4)

    def testLiH_OEP_AN(self):
        do_oep_an = True
        lih_hf = HFSolver(self.lih)
        lih_hf.iterate()
        ints = lih_hf.S,lih_hf.h,lih_hf.Ints
        E_exx,orbe_exx,orbs_exx = oep_hf_an(self.lih,lih_hf.orbs,
                                            bfs=lih_hf.bfs,
                                            integrals=ints)
        self.assertAlmostEqual(E_exx,-7.981044,4)

    # Don't think it necessary to test both OEP and OEP_AN...
    #def testLiH_OEP(self):
    #    do_oep_an = True
    #    lih_hf = HFSolver(self.lih)
    #    lih_hf.iterate()
    #    ints = lih_hf.S,lih_hf.h,lih_hf.Ints
    #    E_exx,orbe_exx,orbs_exx = oep_hf(self.lih,lih_hf.orbs,
    #                                     bfs=lih_hf.bfs,
    #                                     integrals=ints)
    #    self.assertAlmostEqual(E_exx,-7.981044,4)
    
def suite():
    import ricks_unit
    return unittest.TestLoader().loadTestsFromModule(ricks_unit)
        
if __name__ == '__main__':
    fullsuite = suite()
    unittest.TextTestRunner(verbosity=2).run(fullsuite)
{'fraction_non_alphanumeric': 0.14773227653016294, 'fraction_numerical': 0.06285777190664905, 'mean_word_length': 2.6841038118410383, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '189589', 'n_tokens_mistral': 3920, 'n_tokens_neox': 3597, 'n_words': 532}