Metadata-Version: 2.4
Name: pmeff
Version: 2.0.0
Summary: PMEFF — a self-contained universal molecular force field (Z = 1..118), NumPy-only, with a dependency-free geometry optimizer.
Author: Hiromichi Yokoyama
License:                    GNU LESSER GENERAL PUBLIC LICENSE
                               Version 3, 29 June 2007
        
         Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
         Everyone is permitted to copy and distribute verbatim copies
         of this license document, but changing it is not allowed.
        
        
          This version of the GNU Lesser General Public License incorporates
        the terms and conditions of version 3 of the GNU General Public
        License, supplemented by the additional permissions listed below.
        
          0. Additional Definitions.
        
          As used herein, "this License" refers to version 3 of the GNU Lesser
        General Public License, and the "GNU GPL" refers to version 3 of the GNU
        General Public License.
        
          "The Library" refers to a covered work governed by this License,
        other than an Application or a Combined Work as defined below.
        
          An "Application" is any work that makes use of an interface provided
        by the Library, but which is not otherwise based on the Library.
        Defining a subclass of a class defined by the Library is deemed a mode
        of using an interface provided by the Library.
        
          A "Combined Work" is a work produced by combining or linking an
        Application with the Library.  The particular version of the Library
        with which the Combined Work was made is also called the "Linked
        Version".
        
          The "Minimal Corresponding Source" for a Combined Work means the
        Corresponding Source for the Combined Work, excluding any source code
        for portions of the Combined Work that, considered in isolation, are
        based on the Application, and not on the Linked Version.
        
          The "Corresponding Application Code" for a Combined Work means the
        object code and/or source code for the Application, including any data
        and utility programs needed for reproducing the Combined Work from the
        Application, but excluding the System Libraries of the Combined Work.
        
          1. Exception to Section 3 of the GNU GPL.
        
          You may convey a covered work under sections 3 and 4 of this License
        without being bound by section 3 of the GNU GPL.
        
          2. Conveying Modified Versions.
        
          If you modify a copy of the Library, and, in your modifications, a
        facility refers to a function or data to be supplied by an Application
        that uses the facility (other than as an argument passed when the
        facility is invoked), then you may convey a copy of the modified
        version:
        
           a) under this License, provided that you make a good faith effort to
           ensure that, in the event an Application does not supply the
           function or data, the facility still operates, and performs
           whatever part of its purpose remains meaningful, or
        
           b) under the GNU GPL, with none of the additional permissions of
           this License applicable to that copy.
        
          3. Object Code Incorporating Material from Library Header Files.
        
          The object code form of an Application may incorporate material from
        a header file that is part of the Library.  You may convey such object
        code under terms of your choice, provided that, if the incorporated
        material is not limited to numerical parameters, data structure
        layouts and accessors, or small macros, inline functions and templates
        (ten or fewer lines in length), you do both of the following:
        
           a) Give prominent notice with each copy of the object code that the
           Library is used in it and that the Library and its use are
           covered by this License.
        
           b) Accompany the object code with a copy of the GNU GPL and this license
           document.
        
          4. Combined Works.
        
          You may convey a Combined Work under terms of your choice that,
        taken together, effectively do not restrict modification of the
        portions of the Library contained in the Combined Work and reverse
        engineering for debugging such modifications, if you also do each of
        the following:
        
           a) Give prominent notice with each copy of the Combined Work that
           the Library is used in it and that the Library and its use are
           covered by this License.
        
           b) Accompany the Combined Work with a copy of the GNU GPL and this license
           document.
        
           c) For a Combined Work that displays copyright notices during
           execution, include the copyright notice for the Library among
           these notices, as well as a reference directing the user to the
           copies of the GNU GPL and this license document.
        
           d) Do one of the following:
        
               0) Convey the Minimal Corresponding Source under the terms of this
               License, and the Corresponding Application Code in a form
               suitable for, and under terms that permit, the user to
               recombine or relink the Application with a modified version of
               the Linked Version to produce a modified Combined Work, in the
               manner specified by section 6 of the GNU GPL for conveying
               Corresponding Source.
        
               1) Use a suitable shared library mechanism for linking with the
               Library.  A suitable mechanism is one that (a) uses at run time
               a copy of the Library already present on the user's computer
               system, and (b) will operate properly with a modified version
               of the Library that is interface-compatible with the Linked
               Version.
        
           e) Provide Installation Information, but only if you would otherwise
           be required to provide such information under section 6 of the
           GNU GPL, and only to the extent that such information is
           necessary to install and execute a modified version of the
           Combined Work produced by recombining or relinking the
           Application with a modified version of the Linked Version. (If
           you use option 4d0, the Installation Information must accompany
           the Minimal Corresponding Source and Corresponding Application
           Code. If you use option 4d1, you must provide the Installation
           Information in the manner specified by section 6 of the GNU GPL
           for conveying Corresponding Source.)
        
          5. Combined Libraries.
        
          You may place library facilities that are a work based on the
        Library side by side in a single library together with other library
        facilities that are not Applications and are not covered by this
        License, and convey such a combined library under terms of your
        choice, if you do both of the following:
        
           a) Accompany the combined library with a copy of the same work based
           on the Library, uncombined with any other library facilities,
           conveyed under the terms of this License.
        
           b) Give prominent notice with the combined library that part of it
           is a work based on the Library, and explaining where to find the
           accompanying uncombined form of the same work.
        
          6. Revised Versions of the GNU Lesser General Public License.
        
          The Free Software Foundation may publish revised and/or new versions
        of the GNU Lesser General Public License from time to time. Such new
        versions will be similar in spirit to the present version, but may
        differ in detail to address new problems or concerns.
        
          Each version is given a distinguishing version number. If the
        Library as you received it specifies that a certain numbered version
        of the GNU Lesser General Public License "or any later version"
        applies to it, you have the option of following the terms and
        conditions either of that published version or of any later version
        published by the Free Software Foundation. If the Library as you
        received it does not specify a version number of the GNU Lesser
        General Public License, you may choose any version of the GNU Lesser
        General Public License ever published by the Free Software Foundation.
        
          If the Library as you received it specifies that a proxy can decide
        whether future versions of the GNU Lesser General Public License shall
        apply, that proxy's public statement of acceptance of any version is
        permanent authorization for you to choose that version for the
        Library.
        
Project-URL: Homepage, https://github.com/HiroYokoyama/moleditpy_pmeff-plugin
Project-URL: Repository, https://github.com/HiroYokoyama/moleditpy_pmeff-plugin
Keywords: force field,molecular mechanics,geometry optimization,computational chemistry,rdkit,pre-DFT
Classifier: Development Status :: 5 - Production/Stable
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: GNU Lesser General Public License v3 (LGPLv3)
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: Scientific/Engineering :: Chemistry
Classifier: Operating System :: OS Independent
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
License-File: COPYING
License-File: LICENSING.md
Requires-Dist: numpy>=1.20
Provides-Extra: rdkit
Requires-Dist: rdkit; extra == "rdkit"
Dynamic: license-file

# PMEFF — Python Molecular Editor Force Field

[![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.21151897.svg)](https://doi.org/10.5281/zenodo.21151897)
[![Tests](https://github.com/HiroYokoyama/moleditpy_pmeff-plugin/actions/workflows/test.yml/badge.svg)](https://github.com/HiroYokoyama/moleditpy_pmeff-plugin/actions/workflows/test.yml)
![Test Coverage](https://img.shields.io/badge/coverage->90%25-green)
[![PyPI](https://img.shields.io/pypi/v/pmeff.svg)](https://pypi.org/project/pmeff/)
[![GitHub tag](https://img.shields.io/github/v/tag/HiroYokoyama/moleditpy_pmeff-plugin?label=version)](https://github.com/HiroYokoyama/moleditpy_pmeff-plugin/tags)
[![GitHub Downloads](https://img.shields.io/github/downloads/HiroYokoyama/moleditpy_pmeff-plugin/total)](https://github.com/HiroYokoyama/moleditpy_pmeff-plugin/releases)

Repo: [https://github.com/HiroYokoyama/moleditpy_pmeff-plugin/](https://github.com/HiroYokoyama/moleditpy_pmeff-plugin/)


A [MoleditPy](https://github.com/HiroYokoyama/python_molecular_editor) plugin that
adds **PMEFF**, a self-contained *universal* molecular force field covering the
**entire periodic table** (Z = 1 – 118). It needs no external QM binary — just
`numpy` and `rdkit`.

## Why another force field?

The classic force fields shipped with molecular editors are excellent for
organic chemistry but leave gaps: MMFF94 only parameterizes a fixed subset of
main-group elements, and even UFF-style tables are hand-tuned per element.

PMEFF takes a different approach. Every parameter is **derived from a single
per-element property** — the Pyykkö single-bond covalent radius — so *no element
is ever missing a parameter*. Drop in a lanthanide, an actinide, a transition
metal, or a superheavy element and PMEFF still produces a sensible geometry.

## What it does

Once installed, PMEFF registers:

| Feature | Where | Description |
|---|---|---|
| **PMEFF** | Right-click the *Optimize 3D* button | Relaxes the current 3D geometry with a dependency-free FIRE 2.0 + L-BFGS optimizer. |
| **PMEFF Single-Point Energy** | *Analysis* menu | Reports the force-field energy (with per-term decomposition) without modifying the geometry. |
| **PMEFF Minimum Check (Vibrational)** | *Analysis* menu | Diagonalizes the Hessian at the current geometry and reports whether it is a true minimum or a saddle point. |
| **Metal Geometry Override…** | *3D Edit* menu (also a button in *PMEFF Settings*) | Opens a modeless table to force the coordination geometry of individual atoms (see below). |
| **PMEFF Settings…** | *Settings → PMEFF Setting* menu | Opens a dialog to toggle individual physics options (persisted to `settings.json`). |

> **Scope:** PMEFF is a pre-DFT *geometry-cleanup* force field. Its goal is
> **initial structure preparation** — removing clashes, correcting bond lengths,
> angles and torsions, and placing metal centers in the right coordination
> geometry. It is not designed for high-accuracy thermochemistry. For accurate
> energies, pair it with the ORCA or PySCF plugins.
>
> **Accuracy:** PMEFF is only for initial structure preparation before DFT.
> Its optimized geometries, energies and vibrational modes are approximate,
> not quantitatively accurate substitutes for DFT results. Convergence and
> the absence of imaginary PMEFF modes do not establish DFT-level accuracy.
> The minimum checker diagnoses curvature of the approximate PMEFF surface.
> Its unit-mass mode values are not physical wavenumbers in cm⁻¹ and must
> not be used as an accurate IR or Raman spectrum.

### Calculation and result dialogs

Optimization, single-point energy and vibrational checks run in a background
thread when used in MoleditPy. A **Calculating…** dialog keeps the interface
repainting and offers **Cancel**. Optimization shows the phase, iteration,
energy and largest force; the minimum checker shows Hessian progress.
Calculation uses a copy of the molecule; cancellation preserves the original
geometry. The progress dialog temporarily blocks editing to keep the document
consistent with the submitted structure.

The result dialog shows a compact outcome and accuracy note. **Details…** opens
the full data in a resizable window, where it can be copied as a table. An optimization
that exhausts its budget is labelled **Not converged**: choose whether to apply
that geometry or keep the original. The minimum checker lists every mode,
the imaginary and near-zero counts, the classification threshold and the
residual maximum force. Values are unit-mass internal modes, **not cm⁻¹**.
No imaginary modes alone does not confirm a minimum when residual forces remain.

The iteration budget is configurable under **PMEFF Settings → Maximum
optimization steps** (default 1,000). Difficult structures may require more
steps; this changes the optimization budget without changing force-field parameters.

Standalone `pmeff.optimize_mol` and `pmeff.optimize_coords` accept
`progress_callback` and `cancel_callback` too, without depending on Qt.
The progress callback receives an `OptimizationProgress` snapshot. A cancellation
callback returning `True` raises `pmeff.OptimizationCancelled` and preserves
the input coordinates.

## The physics

For the full specification — functional forms, parameter derivations,
gradient formulas, non-bonded bookkeeping and the optimizer — see the
[technical reference](docs/TECHNICAL.md). In brief, PMEFF has these energy terms:

- **Bonds** — by default **Morse potential** `D(1 − e^{−α Δr})²` (bounded above
  at the dissociation energy D; same curvature as harmonic at the minimum). The
  harmonic `½·k·(r − r₀)²` is available as a fallback. Rest lengths and force
  constants follow the same covalent-radius rules in both cases, with a
  **polar-bond contraction** (a capped, quadratic function of the
  electronegativity difference) that pulls in bonds the plain radius sum leaves
  too long — Si–O 1.79→1.63 Å, P=O, B–O, the metal-oxides and C–F — while
  leaving organic C–C/C–O/C–N/C–H unchanged. Toggleable in settings.
- **Angles** — harmonic in the bend angle, `½·k·(θ − θ₀)²`, with the ideal angle
  `θ₀` inferred from the central atom's hybridization (falling back to its
  coordination number for metals and other cases where hybridization is
  ambiguous). sp³ pnictogens and chalcogens are compressed below tetrahedral
  by their lone pairs (`4 − coordination` of them): mildly for period-2 atoms
  (NH₃ ≈ 107°, H₂O ≈ 104.5°), strongly for heavier congeners that bond through
  near-pure p orbitals (H₂S/PH₃ ≈ 93°). The period-2 compression is calibrated
  on hydrides and opens back toward tetrahedral for bulkier substituents
  (dimethyl ether's C-O-C ≈ 110°, not 104.5°). Two more special cases: in-ring angles
  of three-membered rings take their target from the law of cosines over the
  bond rest lengths (60° in cyclopropane), so bonds and angles share one
  minimum; and linear sp centers use `k·(1 + cos θ)`, which matches the
  harmonic curvature at 180° but keeps the gradient finite at the linear
  minimum.
- **Torsions** — a cosine dihedral potential `½·V·(1 + cos(n·φ − γ))`: 2-fold
  for sp²–sp² bonds (keeps double bonds and conjugated systems planar), 3-fold
  for sp³–sp³ bonds (staggered minima), and a weak 6-fold term for mixed
  sp²–sp³ bonds. The per-bond barrier is split evenly over all dihedrals
  sharing the bond, UFF-style, so it doesn't grow with substitution, and the
  2-fold barrier scales with the π character of the central bond — full for a
  double bond, reduced for aromatic, weak for a conjugated single bond, so
  biphenyl can twist while ethylene stays rigid.
- **Out-of-plane** — a harmonic penalty on the pyramidalization of
  3-coordinate sp² centers, expressed through the sum of the three bend angles
  around the center (planar ⇔ 360°).
- **van der Waals** — a Lennard-Jones 12-6 term whose per-atom radius is the
  covalent radius plus a fixed 0.90 Å offset. This reproduces the tabulated vdW
  radii of the common elements to within ~0.05 Å (C → 1.65 Å vs. 1.70 Å,
  O → 1.53 Å vs. 1.52 Å, H → 1.22 Å vs. 1.20 Å). Well depths are derived per
  atom from the covalent radius as a polarizability proxy (carbon anchors the
  scale) and combined with the Lorentz–Berthelot geometric mean. 1-2 and 1-3
  pairs are excluded; 1-4 pairs get the conventional half well depth.

### Optional physics terms

**Settings → PMEFF Setting** opens a dialog. All settings are persisted in
`pmeff_plugin/settings.json`. The optional terms (defaults shown) are:

- **Electronic effects** *(on)* — QEq partial charges from Slater Zeff and
  Allred–Rochow electronegativities feed a shielded Coulomb term; charges are
  re-solved as the geometry relaxes. This is an approximate charge-relaxation
  model; forces use the current fixed charges between refreshes. Before claiming
  convergence the optimizer refreshes charges at the final coordinates and,
  if necessary, uses the remaining iteration budget to reduce residual forces.
  Also enables square-planar angle targets for 4-coordinate Ni/Pd/Pt/Rh/Ir/Au
  and octahedral targets for 6-coordinate d-block transition metals; cis/trans
  assignment is read from the starting geometry so tetrahedral Pd²⁺ converges
  to square-planar.
- **Morse bond stretching** *(on)* — replaces harmonic bonds with the Morse
  potential `D(1−e^{−α Δr})²`; bounded above at D, same curvature at the
  minimum. Improves robustness for severely distorted starting geometries at
  negligible computational cost.
- **Hydrogen bond correction** *(on)* — a geometry-dependent D−H···A term
  (donors/acceptors: N, O, F, S) with a 12-6 radial profile and cos²(∠DHA)
  angular dependence. Improves H-bond distances and linearity. Fast.
- **Dispersion correction** *(off by default)* — Becke-Johnson damped C₆/r⁶
  London dispersion added on top of the LJ term. Improves aromatic stacking
  distances and hydrophobic contacts. Can slow optimization of large molecules.

Lone pairs are not explicit particles, but their steric effect enters through
the hybridization-derived angle targets: sp³ N stays pyramidal, sp³ O stays
bent, and a conjugated (sp²) amide nitrogen stays planar.

### Per-atom geometry override

Connectivity alone cannot always determine a coordination geometry — a
4-coordinate metal may be square-planar *or* tetrahedral, and a bare metal
centre may have no reliable hybridization at all. **3D Edit → PMEFF Metal Geometry
Override** (also a button in *PMEFF Settings*) opens a modeless table where you
can force the geometry of individual atoms:

| Override | Ideal L–M–L angles |
|---|---|
| **Auto** *(default)* | Hybridization / auto-metal detection (unchanged) |
| **Linear** | 180° |
| **Trigonal Planar** | 120°, kept planar |
| **Square Planar** | cis 90° / trans 180° |
| **Tetrahedral** | 109.47° |
| **Trigonal Bipyramidal** | axial–axial 180°, axial–equatorial 90°, equatorial–equatorial 120° |
| **Square Pyramidal** | cis 90° / trans 180° (2 trans pairs) |
| **Octahedral** | cis 90° / trans 180° |

The table lists the current molecule's atoms (an **Atom ID** column identifies
each) with a *Show metals only* filter (on by default — uncheck it to override
carbon or any other element). Each atom's drop-down only enables geometries that
fit its neighbor count (e.g. *Linear* is disabled on a 3-coordinate center).
Rows are colour-coded: **blue** = an unsaved change, **green** = an applied
override, white = none. Click an atom in the 3D view to jump to its row;
selected rows are highlighted in 3D.

Overrides are **entirely opt-in**: an atom left on *Auto* behaves exactly as
before, so the shipped defaults (including the d8 square-planar / octahedral
auto-detection) are unchanged. They are also **independent of the electronic-
effects setting** — a forced geometry applies whether or not electronic effects
are on. **Apply** stores the overrides without moving atoms (then run *Optimize
3D (PMEFF)* when you like); **Apply and Optimize** stores them and relaxes the
molecule immediately. Only *applied* overrides are kept — they are saved with
the project and restored when the window (or project) is reopened; an unsaved
(blue) change is discarded if you close the window without applying it.

Geometry optimization uses **FIRE 2.0** (Fast Inertial Relaxation Engine) for
the far-from-minimum regime and hands over to an **L-BFGS finisher** in the
quadratic basin, with a per-atom displacement clamp for stability and fully
**analytical gradients** for all energy terms — including the dihedral and
Coulomb derivatives, which are verified against numeric differentiation in
the test suite. All terms are evaluated with vectorized numpy over
precompiled index arrays, so evaluation cost is dominated by numpy kernels
rather than Python loops. The short-range van der Waals term is truncated at
a 12 Å cutoff (electrostatics, being long-range, are not) with the pair list
built by an O(N) cell-list search and maintained as a Verlet list during
optimization; a CHARMM-style switching function tapers the LJ term to zero
over the last 2 Å with zero slope at the cutoff, so both energy and force
stay continuous as a pair crosses the boundary. A finite-difference Hessian
over the analytic gradient powers the vibrational minimum check.

> **Note:** PMEFF energies are in internal, consistent units meant for
> **relative comparison and geometry guidance**, not thermochemistry. The goal is
> to give the DFT optimizer a sensible starting geometry — not to replace it.

## Installation

Copy the `pmeff_plugin/` folder into your MoleditPy user plugin
directory:

- **Windows:** `C:\Users\<YourName>\.moleditpy\plugins\`
- **Linux / macOS:** `~/.moleditpy/plugins/`

Then restart MoleditPy, or use **Plugins → Reload All Plugins**.

## Standalone Python package (`pmeff`)

The force-field engine is also published on PyPI as **`pmeff`** — the same
physics, usable from any Python script without MoleditPy. The core has **no
dependency beyond NumPy**; RDKit is an optional extra for the convenience layer.

```bash
pip install pmeff            # core: NumPy only
pip install "pmeff[rdkit]"   # adds the RDKit convenience layer (optimize_mol)
```

### With RDKit — `Mol` in, relaxed `Mol` out

The recommended path: hand it an RDKit molecule that has a 3D conformer, and get
the **same molecule back with its geometry relaxed**. Connectivity, bond orders,
formal charges and properties are all preserved (a raw coordinate list would
throw them away).

```python
from rdkit import Chem
from rdkit.Chem import AllChem
import pmeff

mol = Chem.AddHs(Chem.MolFromSmiles("O[SiH3]"))   # silanol
AllChem.EmbedMolecule(mol, randomSeed=1)          # give it a 3D conformer

mol, result = pmeff.optimize_mol(mol)             # relaxed in place, and returned
print(result.converged, result.energy, result.max_force)
print(Chem.MolToXYZBlock(mol))                    # -> optimized coordinates
```

`optimize_mol` takes the same physics switches as the plugin
(`electronic_effects`, `use_morse`, `use_hbond`, `use_dispersion`,
`use_polar_contraction`) plus `max_iter` and `f_tol`. It also accepts
`geometry_overrides` — a `{atom_index: name}` mapping (`"linear"`,
`"trigonal_planar"`, `"square_planar"`, `"tetrahedral"`, `"octahedral"`) that
forces the coordination geometry of individual atoms:

```python
# Force atom 1 to square-planar regardless of its detected hybridization.
mol, result = pmeff.optimize_mol(mol, geometry_overrides={1: "square_planar"})
```

### Without RDKit — plain arrays in, coordinates out

For pipelines that don't use RDKit, describe the molecule with atomic numbers, a
bond list and coordinates:

```python
import numpy as np
import pmeff

atomic_numbers = [8, 1, 1]                         # water
bonds = [(0, 1), (0, 2)]
coords = np.array([[0.0, 0, 0], [0.96, 0, 0], [-0.3, 0.9, 0.0]])

coords, result = pmeff.optimize_coords(
    atomic_numbers, bonds, coords, hybridizations=["SP3", None, None]
)
print(result.converged)
print(coords)                                      # optimized (N, 3) array
```

Optional charges (`pmeff.qeq_charges(...)`), per-bond orders and hybridizations
sharpen the result; `optimize_coords` also accepts the same `geometry_overrides`
mapping as `optimize_mol`. The lower-level engine — `build_topology`,
`energy_and_gradient`, `energy_components`, `optimize`, `vibrational_analysis` —
is exported too for custom workflows.

For the full programmatic guide — every option, the low-level engine, worked
recipes and an API reference — see **[docs/USAGE.md](docs/USAGE.md)**.

> The engine is a copy of `pmeff_plugin/forcefield.py` (the single source of
> truth); see [PACKAGING.md](PACKAGING.md) for the build/release process.

## Development

```bash
# Run the test suite (uses real numpy + rdkit)
python -m pytest tests/ -v

# With coverage
python -m pytest tests/ --cov=pmeff_plugin --cov-report=term-missing

# Lint
pylint pmeff_plugin/
```

The engine (`pmeff_plugin/forcefield.py`) is deliberately Qt-free and
RDKit-free at its core — it operates on a plain-number `Topology` — which makes
it fully unit-testable without a GUI. Only the thin boundary functions touch
RDKit.

## License

LGPL-3.0-or-later. See [LICENSE](LICENSE).

## Licensing scope

First-party code in this migration is LGPL-3.0-or-later. See [LICENSE](LICENSE), [COPYING](COPYING) and [dependency/provenance terms](LICENSING.md). Third-party components and historical releases retain their own terms.
