Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more checks #17

Merged
merged 8 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 59 additions & 11 deletions .github/workflows/quemb_unittest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ permissions:
contents: read

jobs:
build:
analysis:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.9", "3.12"]
python-version: ["3.12"]


steps:
- uses: actions/checkout@v4
Expand All @@ -31,7 +32,6 @@ jobs:
run: |
python -m pip install --upgrade pip


- uses: actions/cache@v4
with:
path: ${{ env.pythonLocation }}
Expand All @@ -40,31 +40,79 @@ jobs:

- name: Install dependencies
run: |
pip install --upgrade --upgrade-strategy eager pytest ruff
if [ -f requirements.txt ]; then pip install --upgrade --upgrade-strategy eager -r requirements.txt; fi
pip install git+https://github.com/pyscf/dmrgscf
PYSCFHOME=$(pip show pyscf-dmrgscf | grep 'Location' | tr ' ' '\n' | tail -n 1)
wget https://raw.githubusercontent.com/pyscf/dmrgscf/master/pyscf/dmrgscf/settings.py.example
mv settings.py.example ${PYSCFHOME}/pyscf/dmrgscf/settings.py
pip install --upgrade --upgrade-strategy eager ruff pylint mypy scipy-stubs
pip install .
echo ${{ github.workspace }} > $(python -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())")/quemb.pth


- name: Check formatting
run: |
ruff format --diff


- name: Static analysis
- name: Static analysis with ruff
run: |
ruff check . || true # for the moment we want to report always report success


- name: Static analysis with pylint
run: |
# ruff does nearly everything that we want from pylint
# except for detecting unresolved imports
# TODO: if they add it to ruff as well https://github.com/astral-sh/ruff/issues/9103
# remove pylint.
pylint --disable=all --enable=E0401,R0401,E0611 . || true # for the moment we want to report always report success


- name: Static analysis with mypy
run: |
mypy tests/ example/ src/ || true # for the moment we want to report always report success



build:
runs-on: ubuntu-latest
needs: analysis
strategy:
matrix:
python-version: ["3.9", "3.12"]

steps:
- uses: actions/checkout@v4

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}

- name: Prepare pip
run: |
python -m pip install --upgrade pip


- uses: actions/cache@v4
with:
path: ${{ env.pythonLocation }}
key: ${{ env.pythonLocation }}-${{ hashFiles('setup.py') }}-${{ hashFiles('dev-requirements.txt') }}


- name: Install dependencies
run: |
pip install --upgrade --upgrade-strategy eager pytest
if [ -f requirements.txt ]; then pip install --upgrade --upgrade-strategy eager -r requirements.txt; fi
pip install git+https://github.com/pyscf/dmrgscf
PYSCFHOME=$(pip show pyscf-dmrgscf | grep 'Location' | tr ' ' '\n' | tail -n 1)
wget https://raw.githubusercontent.com/pyscf/dmrgscf/master/pyscf/dmrgscf/settings.py.example
mv settings.py.example ${PYSCFHOME}/pyscf/dmrgscf/settings.py
pip install .
echo ${{ github.workspace }} > $(python -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())")/quemb.pth


- name: Test with pytest
run: |
cd tests
QUEMB_SKIP_EXPENSIVE_TESTS=true pytest --doctest-modules --junitxml=junit/quemb-test-results_${{ matrix.python-version }}.xml


- name: Upload pytest junit results
uses: actions/upload-artifact@v4
with:
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,6 @@ cython_debug/
# pyscf files
eri_file.h5


# vim swap files
.*.swp
6 changes: 5 additions & 1 deletion .ruff.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[lint]
# see the rules [here](https://docs.astral.sh/ruff/rules/)
select = ["E", "F", "I", "NPY", "ARG"]
select = ["E", "F", "I", "NPY", "ARG", "PL"]
ignore = [
"S101",
# https://docs.astral.sh/ruff/rules/assert/
Expand All @@ -10,5 +10,9 @@ ignore = [
# https://docs.astral.sh/ruff/rules/ambiguous-variable-name/
# Prevents the use of the characters 'l', 'O', or 'I' as variable names.
# Overly restrictive, in particular when implementing mathematical expressions.
"PLR",
# https://docs.astral.sh/ruff/rules/#refactor-r
# while these warnings are nice for a fresh codebase,
# they are too many to fix now and not important enough
]
preview = true
17 changes: 17 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[mypy]
no_implicit_optional = True

# TODO: whenever the following packages have stubs available,
# stop ignoring them.

[mypy-pyscf.*]
ignore_missing_imports = True

[mypy-libdmet.*]
ignore_missing_imports = True

[mypy-setuptools.*]
ignore_missing_imports = True

[mypy-h5py.*]
ignore_missing_imports = True
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@
"scipy>=1.7.0",
"pyscf>=2.0.0",
"matplotlib",
"libdmet @ git+https://github.com/mcocdawc/libdmet_preview.git@add_fixes_for_BE",
"libdmet @ git+https://github.com/gkclab/libdmet_preview.git",
],
)
4 changes: 2 additions & 2 deletions src/kbe/autofrag.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ def autogen(
sys.exit()
# kmesh lkpt ends

## starts here
# starts here
normlist = []
for i in coord:
normlist.append(numpy.linalg.norm(i))
Expand Down Expand Up @@ -572,7 +572,7 @@ def autogen(
# part of l,r,d,u,m,t but are in the
# unit cell that is their k is 1
# for example, klsts[i] = 1
## WARNING !!!
# WARNING !!!
# For systems like Graphene, BN, SiC, hexagonal 2D sheets,
# BE4 can give wrong fragmentations
# the following code adds in redundant atoms
Expand Down
7 changes: 4 additions & 3 deletions src/molbe/be_parallel.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ def run_solver(
efci, civec = mc_.kernel()
rdm1_tmp = mc_.make_rdm1(civec, mc_.norb, mc_.nelec)
elif solver == "HCI":
# pylint: disable-next=E0611
from pyscf import hci # noqa: PLC0415 # hci is an optional module

nao, nmo = mf_.mo_coeff.shape
Expand All @@ -158,6 +159,7 @@ def run_solver(
rdm2s = rdm2aa + rdm2ab + rdm2ab.transpose(2, 3, 0, 1) + rdm2bb

elif solver == "SHCI":
# pylint: disable-next=E0401,E0611
from pyscf.shciscf import shci # noqa: PLC0415 # shci is an optional module

nao, nmo = mf_.mo_coeff.shape
Expand All @@ -176,9 +178,8 @@ def run_solver(
rdm1_tmp, rdm2s = mch.fcisolver.make_rdm12(0, nmo, nelec)

elif solver == "SCI":
from pyscf import ( # noqa: PLC0415 # cornell_shci is an optional module
cornell_shci,
)
# pylint: disable-next=E0611
from pyscf import cornell_shci # noqa: PLC0415 # optional module

nao, nmo = mf_.mo_coeff.shape
nelec = (nocc, nocc)
Expand Down
4 changes: 2 additions & 2 deletions src/molbe/external/optqn.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ def get_atbe_Jblock_frag(fobj, res_func):
for k_ in range(len(edge)):
if j_ > k_:
continue
## response w.r.t matching pot
# response w.r.t matching pot
# edges
tmpje_ = []

Expand Down Expand Up @@ -366,7 +366,7 @@ def get_atbe_Jblock_frag(fobj, res_func):

Jc.append(tmpjc_)

## response w.r.t. chem pot
# response w.r.t. chem pot
# edge
xe.append(dP_mu[edge[j_], edge[k_]])
cout += 1
Expand Down
2 changes: 1 addition & 1 deletion src/molbe/pfrag.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ def set_udim(self, cout):
return cout

def energy(self, rdm2s, eri=None, print_fragE=False):
## This function uses old energy expression and will be removed
# This function uses old energy expression and will be removed
rdm2s = numpy.einsum(
"ijkl,pi,qj,rk,sl->pqrs",
0.5 * rdm2s,
Expand Down
21 changes: 10 additions & 11 deletions src/molbe/solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ def be_func(
rdm1_tmp = mc.make_rdm1(civec, mc.norb, mc.nelec)

elif solver == "HCI":
# pylint: disable-next=E0611
from pyscf import hci # noqa: PLC0415 # optional module

nao, nmo = fobj._mf.mo_coeff.shape
Expand Down Expand Up @@ -172,6 +173,7 @@ def be_func(
rdm2s = rdm2aa + rdm2ab + rdm2ab.transpose(2, 3, 0, 1) + rdm2bb

elif solver == "SHCI":
# pylint: disable-next=E0611,E0401
from pyscf.shciscf import shci # noqa: PLC0415 # shci is optional

if scratch_dir is None and be_var.CREATE_SCRATCH_DIR:
Expand Down Expand Up @@ -203,6 +205,7 @@ def be_func(
rdm1_tmp, rdm2s = mch.fcisolver.make_rdm12(0, nmo, nelec)

elif solver == "SCI":
# pylint: disable-next=E0611
from pyscf import cornell_shci # noqa: PLC0415 # optional module

nao, nmo = fobj._mf.mo_coeff.shape
Expand Down Expand Up @@ -685,16 +688,11 @@ def solve_ccsd(
try:
cc__.verbose = verbose
cc__.kernel(eris=eris)
except:
except Exception as e:
print(flush=True)
print(
"Exception in CCSD -> applying level_shift=0.2, diis_space=25", flush=True
)
print("Exception in CCSD, play with different CC options.", flush=True)
print(flush=True)
cc__.verbose = 4
cc__.diis_space = 25
cc__.level_shift = 0.2
cc__.kernel(eris=eris)
raise e

# Extract the CCSD amplitudes
t1 = cc__.t1
Expand Down Expand Up @@ -782,6 +780,7 @@ def solve_block2(mf: object, nocc: int, frag_scratch: str = None, **solver_kwarg


"""
# pylint: disable-next=E0611
from pyscf import dmrgscf # noqa: PLC0415 # optional module

use_cumulant = solver_kwargs.pop("use_cumulant", True)
Expand Down Expand Up @@ -813,7 +812,7 @@ def solve_block2(mf: object, nocc: int, frag_scratch: str = None, **solver_kwarg

mc = mcscf.CASCI(mf, norb, nelec)
mc.fcisolver = dmrgscf.DMRGCI(mf.mol)
###Sweep scheduling
# Sweep scheduling
mc.fcisolver.scheduleSweeps = schedule_kwargs.pop(
"scheduleSweeps",
[
Expand Down Expand Up @@ -844,7 +843,7 @@ def solve_block2(mf: object, nocc: int, frag_scratch: str = None, **solver_kwarg
"scheduleNoises",
[max_noise, max_noise, max_noise / 10, max_noise / 100, max_noise / 100, 0.0],
)
###Other DMRG parameters
# Other DMRG parameters
mc.fcisolver.threads = int(os.environ.get("OMP_NUM_THREADS", 8))
mc.fcisolver.twodot_to_onedot = int(twodot_to_onedot)
mc.fcisolver.maxIter = int(max_iter)
Expand All @@ -857,7 +856,7 @@ def solve_block2(mf: object, nocc: int, frag_scratch: str = None, **solver_kwarg
mc.kernel(orbs)
rdm1, rdm2 = dmrgscf.DMRGCI.make_rdm12(mc.fcisolver, root, norb, nelec)

###Subtract off non-cumulant contribution to correlated 2RDM.
# Subtract off non-cumulant contribution to correlated 2RDM.
if use_cumulant:
hf_dm = numpy.zeros_like(rdm1)
hf_dm[numpy.diag_indices(nocc)] += 2.0
Expand Down