|
| 1 | +--- |
| 2 | +name: dpdata-driver |
| 3 | +description: Use dpdata Python Driver plugins to label systems (energies/forces/virials) via System.predict(), list available drivers, and build Driver objects (ase/deepmd/gaussian/sqm/hybrid). Use when working with dpdata Python API (not CLI) and you need driver-based energy/force prediction, plugin registration keys, or examples of using dpdata with ASE calculators or DeePMD models. |
| 4 | +license: LGPL-3.0-or-later |
| 5 | +compatibility: Requires dpdata or uv for running dpdata |
| 6 | +metadata: |
| 7 | + author: njzjz-bot |
| 8 | + version: '1.0' |
| 9 | +--- |
| 10 | + |
| 11 | +# dpdata-driver |
| 12 | + |
| 13 | +Use dpdata “driver plugins” to **label** a `dpdata.System` (predict energies/forces/virials) and obtain a `dpdata.LabeledSystem`. |
| 14 | + |
| 15 | +## Key idea |
| 16 | + |
| 17 | +- A **Driver** converts an unlabeled `System` into a `LabeledSystem` by computing: |
| 18 | + - `energies` (required) |
| 19 | + - `forces` (optional but common) |
| 20 | + - `virials` (optional) |
| 21 | + |
| 22 | +In dpdata, this is exposed as: |
| 23 | + |
| 24 | +- `System.predict(*args, driver="dp", **kwargs) -> LabeledSystem` |
| 25 | + |
| 26 | +`driver` can be: |
| 27 | + |
| 28 | +- a **string key** (plugin name), e.g. `"ase"`, `"dp"`, `"gaussian"` |
| 29 | +- a **Driver object**, e.g. `Driver.get_driver("ase")(...)` |
| 30 | + |
| 31 | +## List supported driver keys (runtime) |
| 32 | + |
| 33 | +When unsure what drivers exist in *this* dpdata version/env, query them at runtime: |
| 34 | + |
| 35 | +```python |
| 36 | +import dpdata |
| 37 | +from dpdata.driver import Driver |
| 38 | + |
| 39 | +print(sorted(Driver.get_drivers().keys())) |
| 40 | +``` |
| 41 | + |
| 42 | +`import dpdata` ensures built-in plugins are loaded before listing registered drivers. |
| 43 | + |
| 44 | +In the current repo state, keys include: |
| 45 | + |
| 46 | +- `ase` |
| 47 | +- `dp` / `deepmd` / `deepmd-kit` |
| 48 | +- `gaussian` |
| 49 | +- `sqm` |
| 50 | +- `hybrid` |
| 51 | + |
| 52 | +(Exact set depends on dpdata version and installed extras.) |
| 53 | + |
| 54 | +## Minimal workflow |
| 55 | + |
| 56 | +```python |
| 57 | +import dpdata |
| 58 | +from dpdata.system import System |
| 59 | + |
| 60 | +sys = System("input.xyz", fmt="xyz") |
| 61 | +ls = sys.predict(driver="ase", calculator=...) # returns dpdata.LabeledSystem |
| 62 | +``` |
| 63 | + |
| 64 | +### Verify you got a labeled system |
| 65 | + |
| 66 | +```python |
| 67 | +assert "energies" in ls.data |
| 68 | +# optional: |
| 69 | +# assert "forces" in ls.data |
| 70 | +# assert "virials" in ls.data |
| 71 | +``` |
| 72 | + |
| 73 | +## Example: use the ASE driver with an ASE calculator (runnable) |
| 74 | + |
| 75 | +This is the easiest *fully runnable* example because it doesn’t require external QM software. |
| 76 | + |
| 77 | +Dependencies (recommended): declare script dependencies with uv inline metadata, then run with `uv run`. |
| 78 | + |
| 79 | +```python |
| 80 | +# /// script |
| 81 | +# requires-python = ">=3.8" |
| 82 | +# dependencies = [ |
| 83 | +# "dpdata", |
| 84 | +# "numpy", |
| 85 | +# "ase", |
| 86 | +# ] |
| 87 | +# /// |
| 88 | +``` |
| 89 | + |
| 90 | +Script: |
| 91 | + |
| 92 | +```python |
| 93 | +from pathlib import Path |
| 94 | + |
| 95 | +import numpy as np |
| 96 | +from ase.calculators.lj import LennardJones |
| 97 | +from dpdata.system import System |
| 98 | + |
| 99 | +# write a tiny molecule |
| 100 | +Path("tmp.xyz").write_text("""2\n\nH 0 0 0\nH 0 0 0.74\n""") |
| 101 | + |
| 102 | +sys = System("tmp.xyz", fmt="xyz") |
| 103 | +ls = sys.predict(driver="ase", calculator=LennardJones()) |
| 104 | + |
| 105 | +print("energies", np.array(ls.data["energies"])) |
| 106 | +print("forces shape", np.array(ls.data["forces"]).shape) |
| 107 | +if "virials" in ls.data: |
| 108 | + print("virials shape", np.array(ls.data["virials"]).shape) |
| 109 | +else: |
| 110 | + print("virials: <not provided by this driver/calculator>") |
| 111 | +``` |
| 112 | + |
| 113 | +## Example: pass a Driver object instead of a string |
| 114 | + |
| 115 | +```python |
| 116 | +from ase.calculators.lj import LennardJones |
| 117 | +from dpdata.driver import Driver |
| 118 | +from dpdata.system import System |
| 119 | + |
| 120 | +sys = System("tmp.xyz", fmt="xyz") |
| 121 | +ase_driver = Driver.get_driver("ase")(calculator=LennardJones()) |
| 122 | +ls = sys.predict(driver=ase_driver) |
| 123 | +``` |
| 124 | + |
| 125 | +## Hybrid driver |
| 126 | + |
| 127 | +Use `driver="hybrid"` to sum energies/forces/virials from multiple drivers. |
| 128 | + |
| 129 | +The `HybridDriver` accepts `drivers=[ ... ]` where each item is either: |
| 130 | + |
| 131 | +- a `Driver` instance |
| 132 | +- a dict like `{"type": "sqm", ...}` (type is the driver key) |
| 133 | + |
| 134 | +Example (structure only; may require external executables): |
| 135 | + |
| 136 | +```python |
| 137 | +from dpdata.driver import Driver |
| 138 | + |
| 139 | +hyb = Driver.get_driver("hybrid")( |
| 140 | + drivers=[ |
| 141 | + {"type": "sqm", "qm_theory": "DFTB3"}, |
| 142 | + {"type": "dp", "dp": "frozen_model.pb"}, |
| 143 | + ] |
| 144 | +) |
| 145 | +# ls = sys.predict(driver=hyb) |
| 146 | +``` |
| 147 | + |
| 148 | +## Notes / gotchas |
| 149 | + |
| 150 | +- Many drivers require extra dependencies or external programs: |
| 151 | + - `dp` requires `deepmd-kit` + a model file |
| 152 | + - `gaussian` requires Gaussian and a valid executable (default `g16`) |
| 153 | + - `sqm` requires AmberTools `sqm` |
| 154 | +- If you just need file format conversion, use the existing **dpdata CLI** skill instead. |
0 commit comments