From e1c485442db9dbd487057f30f7e36a7a00553886 Mon Sep 17 00:00:00 2001 From: Fionn Malone Date: Sat, 9 Dec 2023 17:23:54 -0800 Subject: [PATCH] Update actions checkout and setup-python (fixes warning). (#277) The mpi tests failing was a red herring. The examples were failing because of #278, this sent a signal to the other tests to fail but I'm guessing the error was only received on the root process so the MPI jobs would hang. I've set a timeout for these for the moment but there may be a more sensible thing to do. --- .github/workflows/ci.yml | 31 ++++--- examples/02-multi_determinant/README.rst | 101 --------------------- examples/02-multi_determinant/run_afqmc.py | 60 ++++++++---- examples/02-multi_determinant/scf.py | 49 ---------- 4 files changed, 60 insertions(+), 181 deletions(-) delete mode 100644 examples/02-multi_determinant/README.rst delete mode 100644 examples/02-multi_determinant/scf.py diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4b32078d..e2e32250 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -30,14 +30,15 @@ jobs: python dev/run_tests.py --flynt pytest-mpi4py: strategy: + fail-fast: false matrix: python-version: [ '3.8', '3.9', '3.10' ] mpi: - openmpi runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 - - uses: actions/setup-python@v1 + - uses: actions/checkout@v3 + - uses: actions/setup-python@v4 with: python-version: ${{ matrix.python-version }} architecture: 'x64' @@ -51,7 +52,8 @@ jobs: pip install -r requirements.txt pip install -r dev/dev.txt pip install pyblock - pip install pyscf + # https://github.com/JoonhoLee-Group/ipie/issues/278 + pip install "pyscf<=2.3.0" - name: Install package run: | # HACK FOR LEGACY CODE! @@ -60,12 +62,13 @@ jobs: run: | pip install pytest pip install pytest-xdist - python -m pytest -n=auto + #python -m pytest -n=auto - name: mpi test + timeout-minutes: 10 run: | - mpiexec -n 2 python -m pytest ipie/legacy/walkers/tests/test_handler.py - mpiexec -n 6 python -m pytest ipie/estimators/tests/test_generic_chunked.py - mpiexec -n 6 python -m pytest ipie/propagation/tests/test_generic_chunked.py + mpiexec --mca btl_tcp_if_include lo --mca oob_tcp_if_include lo -n 2 python -m pytest ipie/legacy/walkers/tests/test_handler.py + mpiexec --mca btl_tcp_if_include lo --mca oob_tcp_if_include lo -n 6 python ipie/estimators/tests/test_generic_chunked.py + mpiexec --mca btl_tcp_if_include lo --mca oob_tcp_if_include lo -n 6 python -m pytest ipie/propagation/tests/test_generic_chunked.py pytest-no_mpi: runs-on: ubuntu-latest steps: @@ -83,6 +86,8 @@ jobs: python -c "from ipie.config import MPI; assert \"FakeComm\" in str(MPI.COMM_WORLD)" python -m pytest -n=auto integration: + strategy: + fail-fast: false runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 @@ -104,7 +109,7 @@ jobs: - name: Test Integration timeout-minutes: 10 run: | - mpirun -np 4 python -u ipie/qmc/tests/test_mpi_integration.py + mpiexec --mca btl_tcp_if_include lo --mca oob_tcp_if_include lo -np 4 python -u ipie/qmc/tests/test_mpi_integration.py examples: runs-on: ubuntu-latest steps: @@ -112,21 +117,17 @@ jobs: - uses: actions/setup-python@v4 with: python-version: "3.10" - - name: Setup MPI - uses: mpi4py/setup-mpi@v1 - with: - mpi: openmpi - name: Install dependencies run: | python -m pip install --upgrade pip pip install -r requirements.txt - pip install -r dev/dev.txt pip install pyblock - pip install pyscf + # https://github.com/JoonhoLee-Group/ipie/issues/278 + pip install "pyscf<=2.3.0" - name: Install package run: | python -m pip install -e . - - name: Test Integration + - name: Test Examples timeout-minutes: 10 run: | python dev/run_tests.py --examples \ No newline at end of file diff --git a/examples/02-multi_determinant/README.rst b/examples/02-multi_determinant/README.rst deleted file mode 100644 index cf087fe0..00000000 --- a/examples/02-multi_determinant/README.rst +++ /dev/null @@ -1,101 +0,0 @@ -Simple PYSCF Workflow -===================== - -In this example we will go through the basic steps necessary to generate AFQMC input from -a pyscf casscf calculation N2. - -The pyscf scf script is given below (scf.py in the current directory): - -.. code-block:: python - - import h5py - - from pyscf import ao2mo, fci, gto, lib, mcscf, scf - - - nocca = 4 - noccb = 2 - mol = gto.M( - atom=[("N", 0, 0, 0), ("N", (0, 0, 3.0))], - basis="ccpvdz", - verbose=3, - spin=nocca - noccb, - unit="Bohr", - ) - mf = scf.RHF(mol) - mf.chkfile = "scf.chk" - ehf = mf.kernel() - M = 6 - N = 6 - mc = mcscf.CASSCF(mf, M, N) - mc.chkfile = "scf.chk" - e_tot, e_cas, fcivec, mo, mo_energy = mc.kernel() - # tol specifies the ci coefficient tolerance. - # note it may be helpful to sort the ci coefficents if you want to truncate the - # expansion. - coeff, occa, occb = zip( - *fci.addons.large_ci(fcivec, M, (nocca, noccb), tol=1e-8, return_strs=False) - ) - # Need to write wavefunction to checkpoint file. - with h5py.File("scf.chk", 'r+') as fh5: - fh5['mcscf/ci_coeffs'] = coeff - fh5['mcscf/occs_alpha'] = occa - fh5['mcscf/occs_beta'] = occb - - -The important point is to specify the `chkfile` option. - -Once the scf converges we need to generate the wavefunction and integrals using the -`pyscf_to_ipie.py` script found in `ipie/tools/pyscf`. - -.. code-block:: bash - - python /path/to/ipie/tools/pyscf/pyscf_to_ipie.py -i 'scf.chk' -j 'input.json' --mcscf - -Important here is the `--mcscf` flag which will tell the converter to read the mcscf -mo_coefficients and also look for the msd wavefunction. - -You should find a file called `wavefunction.h5` which defines the multi-Slater expansion, `hamiltonian.h5` and ipie input file `input.json` shown as below. -We can run AFQMC with -.. code-block:: bash - - mpirun -np N python /path/to/ipie/bin/ipie input.json > output.dat - -or, by running the `run_afqmc.py` of the current folder. - -.. code-block:: json - - { - "system": { - "nup": 8, - "ndown": 6 - }, - "hamiltonian": { - "name": "Generic", - "integrals": "hamiltonian.h5" - }, - "qmc": { - "dt": 0.005, - "nwalkers": 640, - "nsteps": 25, - "blocks": 1000, - "batched": true, - "pop_control_freq": 5, - "stabilise_freq": 5 - }, - "trial": { - "filename": "wavefunction.h5", - "compute_trial_energy": true - }, - "estimators": { - "filename": "estimates.0.h5" - } - } - -Note we added the option `compute_trial_energy` to the input file. It is **always -recommended** to check the variational energy of the trial wavefunction you use to ensure -there is no translation errors. Currently the algorithm to compute this variational energy -is sub-optimal so this option is defaulted to false. One can control the number of -determinants used to compute the variational energy with the `ndets` -option. It can be helpful to set this to a value smaller than the number of determinants -in the trial wavefunction. One should also add the `ndets_props` which is the number od dets used for propagation. diff --git a/examples/02-multi_determinant/run_afqmc.py b/examples/02-multi_determinant/run_afqmc.py index 1f347bba..0d1753ba 100644 --- a/examples/02-multi_determinant/run_afqmc.py +++ b/examples/02-multi_determinant/run_afqmc.py @@ -1,14 +1,40 @@ +import h5py import numpy -from mpi4py import MPI +from pyscf import fci, gto, mcscf, scf + +from ipie.hamiltonians.generic import Generic as HamGeneric from ipie.qmc.afqmc import AFQMC from ipie.systems.generic import Generic -from ipie.hamiltonians.generic import Generic as HamGeneric -import h5py -from ipie.utils.from_pyscf import gen_ipie_input_from_pyscf_chk from ipie.trial_wavefunction.particle_hole import ParticleHoleNonChunked -import scf +from ipie.utils.from_pyscf import gen_ipie_input_from_pyscf_chk + +nocca = 4 +noccb = 2 + +mol = gto.M( + atom=[("N", 0, 0, 0), ("N", (0, 0, 3.0))], + basis="ccpvdz", + verbose=3, + spin=nocca - noccb, + unit="Bohr", +) +mf = scf.RHF(mol) +mf.chkfile = "scf.chk" +ehf = mf.kernel() +M = 6 +N = 6 +mc = mcscf.CASSCF(mf, M, N) +mc.chkfile = "scf.chk" +e_tot, e_cas, fcivec, mo, mo_energy = mc.kernel() +coeff, occa, occb = zip( + *fci.addons.large_ci(fcivec, M, (nocca, noccb), tol=1e-8, return_strs=False) +) +# Need to write wavefunction to checkpoint file. +with h5py.File("scf.chk", "r+") as fh5: + fh5["mcscf/ci_coeffs"] = coeff + fh5["mcscf/occs_alpha"] = occa + fh5["mcscf/occs_beta"] = occb -comm = MPI.COMM_WORLD gen_ipie_input_from_pyscf_chk("scf.chk", mcscf=True) mol_nelec = [8, 6] @@ -24,22 +50,23 @@ ham = HamGeneric( numpy.array([h1e, h1e]), chol.transpose((1, 2, 0)).reshape((num_basis * num_basis, num_chol)), - e0 + e0, ) # Build trial wavefunction -with h5py.File("wavefunction.h5", 'r') as fh5: - coeff = fh5['ci_coeffs'][:] - occa = fh5['occ_alpha'][:] +with h5py.File("wavefunction.h5", "r") as fh5: + coeff = fh5["ci_coeffs"][:] + occa = fh5["occ_alpha"][:] occb = fh5["occ_beta"][:] wavefunction = (coeff, occa, occb) trial = ParticleHoleNonChunked( wavefunction, mol_nelec, num_basis, - num_dets_for_props=len(wavefunction[0]) + num_dets_for_props=len(wavefunction[0]), + verbose=True, ) -trial.compute_trial_energy = False +trial.compute_trial_energy = True trial.build() trial.half_rotate(ham) @@ -53,8 +80,9 @@ num_blocks=10, timestep=0.005, stabilize_freq=5, - pop_control_freq=5 + seed=96264512, + pop_control_freq=5, + verbose=True, ) -afqmc_msd.run() -afqmc_msd.finalise(verbose=True) - +# afqmc_msd.run() +# afqmc_msd.finalise(verbose=True) diff --git a/examples/02-multi_determinant/scf.py b/examples/02-multi_determinant/scf.py deleted file mode 100644 index fcf03ed4..00000000 --- a/examples/02-multi_determinant/scf.py +++ /dev/null @@ -1,49 +0,0 @@ - -# Copyright 2022 The ipie Developers. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# -# Author: Fionn Malone -# - -import h5py -import numpy -import scipy.sparse -from pyscf import ao2mo, fci, gto, lib, mcscf, scf - -nocca = 4 -noccb = 2 - -mol = gto.M( - atom=[("N", 0, 0, 0), ("N", (0, 0, 3.0))], - basis="ccpvdz", - verbose=3, - spin=nocca - noccb, - unit="Bohr", -) -mf = scf.RHF(mol) -mf.chkfile = "scf.chk" -ehf = mf.kernel() -M = 6 -N = 6 -mc = mcscf.CASSCF(mf, M, N) -mc.chkfile = "scf.chk" -e_tot, e_cas, fcivec, mo, mo_energy = mc.kernel() -coeff, occa, occb = zip( - *fci.addons.large_ci(fcivec, M, (nocca, noccb), tol=1e-8, return_strs=False) -) -# Need to write wavefunction to checkpoint file. -with h5py.File("scf.chk", 'r+') as fh5: - fh5['mcscf/ci_coeffs'] = coeff - fh5['mcscf/occs_alpha'] = occa - fh5['mcscf/occs_beta'] = occb