Skip to content

Commit

Permalink
pyscf.semiemprical import not required unless needed at runtime (#170)
Browse files Browse the repository at this point in the history
* Skip test and import if pyscf.semiempirical is not installed.

Co-authored-by: Valentin Senicourt <valentin.senicourt@1qbit.com>
Co-authored-by: ValentinS4t1qbit <41597680+ValentinS4t1qbit@users.noreply.github.com>
  • Loading branch information
3 people authored Jun 28, 2022
1 parent 4dfbae0 commit 48a4b01
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
7 changes: 5 additions & 2 deletions tangelo/algorithms/classical/semi_empirical_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@
- MINDO3
"""

from pyscf.semiempirical import mindo3

from tangelo.helpers.utils import is_package_installed
from tangelo.algorithms.electronic_structure_solver import ElectronicStructureSolver


Expand All @@ -50,6 +49,9 @@ class MINDO3Solver(ElectronicStructureSolver):
"""

def __init__(self, molecule):
if not is_package_installed("pyscf.semiempirical"):
raise ModuleNotFoundError(f"The pyscf.semiempirical module is not available and is required by {self.__class__.__name__}.")

self.molecule = molecule

def simulate(self):
Expand All @@ -58,6 +60,7 @@ def simulate(self):
Returns:
float: RMINDO3 energy.
"""
from pyscf.semiempirical import mindo3

solver = mindo3.RMINDO3(self.molecule.to_pyscf()).run(verbose=0)
total_energy = solver.e_tot
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,22 @@

import unittest

from tangelo.helpers.utils import is_package_installed
from tangelo.molecule_library import mol_pyridine
from tangelo.algorithms.classical.semi_empirical_solver import MINDO3Solver


class MINDO3SolverTest(unittest.TestCase):
class SemiEmpiricalSolverTest(unittest.TestCase):

def test_energy(self):
@unittest.skipIf(not is_package_installed("pyscf.semiempirical"), "Test Skipped: pyscf.semiempirical module not available \n")
def test_mindo3_energy(self):
"""Test MINDO3Solver with pyridine. Validated with:
- MINDO/3-derived geometries and energies of alkylpyridines and the
related N-methylpyridinium cations. Jeffrey I. Seeman,
John C. Schug, and Jimmy W. Viers
The Journal of Organic Chemistry 1983 48 (14), 2399-2407
DOI: 10.1021/jo00162a021.
"""
from tangelo.algorithms.classical import MINDO3Solver

solver = MINDO3Solver(mol_pyridine)
energy = solver.simulate()
Expand Down

0 comments on commit 48a4b01

Please sign in to comment.