Metadata-Version: 2.4
Name: pypwdft
Version: 0.3.0
Summary: Educational pure-Python plane-wave density functional theory code
Author-email: Ivo Filot <ivo@ivofilot.nl>
Maintainer-email: Ivo Filot <ivo@ivofilot.nl>
License-Expression: GPL-3.0-or-later
Project-URL: Homepage, https://www.github.com/ifilot/pypwdft
Project-URL: Documentation, https://ifilot.github.io/pypwdft/
Project-URL: Bug Tracker, https://www.github.com/ifilot/pypwdft/issues
Project-URL: Changelog, https://github.com/ifilot/pypwdft/blob/master/CHANGELOG.md
Keywords: density functional theory,plane waves,pseudopotentials,quantum chemistry,education
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Education
Classifier: Intended Audience :: Science/Research
Classifier: Programming Language :: Python :: 3
Classifier: Operating System :: OS Independent
Classifier: Topic :: Scientific/Engineering :: Chemistry
Classifier: Topic :: Scientific/Engineering :: Physics
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
License-File: pypwdft/psp/LICENSE.eminus
License-File: pypwdft/psp/NOTICE
Requires-Dist: scipy
Requires-Dist: numpy
Requires-Dist: pyfftw
Requires-Dist: threadpoolctl
Requires-Dist: matplotlib
Provides-Extra: docs
Requires-Dist: sphinx; extra == "docs"
Requires-Dist: sphinx-rtd-theme; extra == "docs"
Dynamic: license-file

# PyPWDFT

[![build](https://github.com/ifilot/pypwdft/actions/workflows/build_pypi.yml/badge.svg)](https://github.com/ifilot/pypwdft/actions/workflows/build_pypi.yml)
[![PyPI](https://img.shields.io/pypi/v/pypwdft?color=green)](https://pypi.org/project/pypwdft/)
[![License: GPL v3](https://img.shields.io/badge/License-GPLv3-blue.svg)](https://www.gnu.org/licenses/gpl-3.0)

PyPWDFT is an educational pure-Python plane-wave density-functional theory
solver. It supports LDA and PBE exchange-correlation functionals, GTH
pseudopotentials, multiple CPU FFT implementations, and an optional CuPy/CUDA
backend.

> [!IMPORTANT]
> PyPWDFT was developed independently and has no affiliation with the program
> of the same name described by Yang et al. The code in this repository
> predates that publication.

## Features

- Spherical plane-wave basis with automatically derived FFT grids
- GTH frozen-core pseudopotentials with local and non-local terms
- LDA/SVWN5 and PBE exchange-correlation functionals
- NumPy, SciPy, and pyFFTW CPU backends
- Optional CuPy/CUDA execution
- Typed energy, orbital, basis, and SCF results
- Molecular-orbital and density contour plotting

## Installation

```bash
pip install pypwdft
```

## Quick start

```python
from pypwdft import PWDFT, Structure

structure = Structure.from_name("H2", cell=10)
calculation = PWDFT(
    structure,
    cutoff=40,
    xc="pbe",
    pseudopotential="gth",
)
result = calculation.run(
    convergence=1e-6,
    verbosity=1,
)

print(result.energy.total)
result.plot_orbitals(
    plane="xz",
    save="h2-orbitals.png",
)
```

The API separates three concepts:

- `Structure`: atoms, coordinates, units, and cubic simulation cell.
- `PWDFT`: physical and numerical calculation settings.
- `DFTResult`: energies, orbitals, density, basis details, and SCF metadata.

PyPWDFT uses atomic units by default: lengths are in bohr and energies are in
Hartree.

## Structures

Load a bundled molecule or an XYZ file:

```python
methane = Structure.from_name("CH4", cell=12)
water = Structure.from_xyz("water.xyz", cell=12)
```

XYZ coordinates are interpreted as angstrom; `cell` is in bohr. Structures
can also be constructed directly:

```python
hydrogen = Structure(
    symbols=["H", "H"],
    positions=[[0, 0, -0.37], [0, 0, 0.37]],
    cell=6,
    units="angstrom",
)
```

## Calculation configuration

```python
calculation = PWDFT(
    methane,
    cutoff=40,
    density_cutoff=160,
    xc="pbe",
    pseudopotential="gth",
    device="cpu",
)
```

Selecting `pseudopotential="gth"` automatically matches GTH-PADE to LDA and
GTH-PBE to PBE. Advanced valence selections use `GTH`:

```python
from pypwdft import GTH

calculation = PWDFT(
    structure,
    cutoff=40,
    xc="pbe",
    pseudopotential=GTH(charges={"Ga": 13}),
)
```

Use `pseudopotential="all-electron"` for an all-electron Coulomb calculation.

## SCF settings

```python
from pypwdft import SCFSettings

settings = SCFSettings(
    convergence=1e-6,
    density_convergence=1e-6,
    max_iterations=150,
    bands=8,
    verbosity=1,
    mixing="pulay",
    mixing_fraction=0.5,
    mixing_history=6,
)
result = calculation.run(settings)
```

Pulay mixing is the default and uses recent density residuals to accelerate
self-consistency. Lower `mixing_fraction` if the SCF cycle oscillates, or use
`mixing="linear"` for a conventional damped update. Settings can also be
supplied directly to `run()`.

The eigenproblem is solved with a block Davidson solver that restarts from the
orbitals of the previous SCF iteration. Use `eigensolver="arpack"` to select
the ARPACK Lanczos solver instead; it is verified against missed degenerate
states and is considerably slower, so it mainly serves as a cross-check.

## Results

All results are exposed via a `DFTResult` dataclass which has the following
data items.

```python
result.energy.total
result.energy.kinetic
result.energy.hartree
result.energy.xc

result.density
result.orbitals.energies
result.orbitals.real_space
result.orbitals.reciprocal_space

result.basis.plane_waves
result.basis.wavefunction_grid
result.basis.density_grid

result.scf.converged
result.scf.iterations
result.scf.energy_residual
result.scf.elapsed_time
```

## Plotting

```python
result.plot_orbitals(
    plane="xz",
    columns=3,
    save="orbitals.pdf",
    show_imaginary_norm=False,
)
result.plot_density(plane="xz", save="density.pdf")
```

Contour plots use symmetric orbital amplitudes, black isolines, physical axes,
and configurable `xy`, `xz`, or `yz` planes. By default, titles report any
imaginary norm that cannot be removed by a global phase rotation; set
`show_imaginary_norm=False` to suppress this diagnostic.

For batches containing orbitals with different nodal planes, use
`plane="auto"` to select the most informative central plane per orbital.
`ngrid` controls the number of ticks and `tick_rotation` rotates their labels.

## GPU calculations

After installing a CuPy wheel compatible with the local CUDA runtime:

```python
calculation = PWDFT(
    structure,
    cutoff=40,
    xc="pbe",
    pseudopotential="gth",
    device="cuda",
)
result = calculation.run()
```

Returned energies and fields are ordinary Python scalars and NumPy arrays.

## Example results

Valence molecular orbitals of CO and first unoccupied orbital:

![Valence molecular orbitals of CO](img/orbs_co.png)

Valence molecular orbitals of benzene and first three unoccupied orbitals:

![Valence molecular orbitals of CO](img/orbs_benzene.png)

## Acknowledgements

The bundled GTH-PADE and GTH-PBE pseudopotential parameters originate from the
[CP2K data repository](https://github.com/cp2k/cp2k-data) and were obtained
through [eminus](https://github.com/wangenau/eminus). The GTH implementation
in `pypwdft/gth.py` is adapted from eminus (Apache-2.0). See
[`pypwdft/psp/NOTICE`](pypwdft/psp/NOTICE) for licensing details and the
publications to cite.

## License

PyPWDFT is distributed under the GNU General Public License version 3 or later.
Third-party components keep their own licenses; see
[`pypwdft/psp/NOTICE`](pypwdft/psp/NOTICE).
