$npx -y skills add jinzhezenggroup/computational-chemistry-agent-skills --skill deepmd-python-inferenceRun Python inference with DeePMD-kit models using the DeepPot API. Use when the user wants to load a trained/frozen DeePMD model (.pth or .pb) or a built-in pretrained model (e.g., DPA-3.2-5M) in Python, predict energy/force/virial for atomic configurations, evaluate descriptors,
| 1 | # DeePMD-kit Python Inference |
| 2 | |
| 3 | Load a trained DeePMD-kit model in Python and predict energy, forces, and virial for atomic configurations. Also covers CLI-based testing with `dp test`. |
| 4 | |
| 5 | ## Quick Start |
| 6 | |
| 7 | ```python |
| 8 | from deepmd.infer import DeepPot |
| 9 | import numpy as np |
| 10 | |
| 11 | dp = DeepPot("model.pth") |
| 12 | coord = np.array([[1, 0, 0], [0, 0, 1.5], [1, 0, 3]]).reshape([1, -1]) |
| 13 | cell = np.diag(10 * np.ones(3)).reshape([1, -1]) |
| 14 | atype = [1, 0, 1] |
| 15 | e, f, v = dp.eval(coord, cell, atype) |
| 16 | ``` |
| 17 | |
| 18 | ## Agent Responsibilities |
| 19 | |
| 20 | 1. Determine the model source: |
| 21 | - Frozen model file (`.pth` for PyTorch, `.pb` for TensorFlow) |
| 22 | - Built-in pretrained model name (e.g., `DPA-3.2-5M`) |
| 23 | - Checkpoint file (requires freezing first) |
| 24 | 1. Determine the inference task: |
| 25 | - Single-frame prediction (energy, force, virial) |
| 26 | - Batch prediction over multiple frames |
| 27 | - Descriptor evaluation |
| 28 | - Model deviation calculation |
| 29 | - CLI-based testing against labeled data |
| 30 | 1. Help the user prepare input arrays in the correct format. |
| 31 | 1. Run inference and report results. |
| 32 | |
| 33 | ## Python API: DeepPot |
| 34 | |
| 35 | ### Load a Model |
| 36 | |
| 37 | ```python |
| 38 | from deepmd.infer import DeepPot |
| 39 | |
| 40 | # From a frozen PyTorch model |
| 41 | dp = DeepPot("model.pth") |
| 42 | |
| 43 | # From a frozen TensorFlow model |
| 44 | dp = DeepPot("graph.pb") |
| 45 | |
| 46 | # From a built-in pretrained model (auto-downloads if not cached) |
| 47 | dp = DeepPot("DPA-3.2-5M") |
| 48 | ``` |
| 49 | |
| 50 | Built-in pretrained model names include `DPA-3.3-1M`, `DPA-3.2-5M`, `DPA-3.1-3M`, `DPA3-Omol-Large`, etc. DeePMD-kit will automatically download and cache the model on first use. |
| 51 | |
| 52 | ### Predict Energy, Forces, and Virial |
| 53 | |
| 54 | ```python |
| 55 | import numpy as np |
| 56 | from deepmd.infer import DeepPot |
| 57 | |
| 58 | dp = DeepPot("model.pth") |
| 59 | |
| 60 | # Prepare inputs |
| 61 | # coord: (nframes, natoms * 3) in Angstrom |
| 62 | # cell: (nframes, 9) cell vectors in Angstrom, row-major |
| 63 | # atype: list of atom type indices (length natoms) |
| 64 | |
| 65 | coord = np.array( |
| 66 | [ |
| 67 | [ |
| 68 | 0.0, |
| 69 | 0.0, |
| 70 | 0.0, # atom 0 (O) |
| 71 | 0.0, |
| 72 | 0.0, |
| 73 | 1.0, # atom 1 (H) |
| 74 | 0.0, |
| 75 | 1.0, |
| 76 | 0.0, |
| 77 | ] # atom 2 (H) |
| 78 | ] |
| 79 | ).reshape([1, -1]) |
| 80 | |
| 81 | cell = np.diag([10.0, 10.0, 10.0]).reshape([1, -1]) |
| 82 | |
| 83 | # atype indices correspond to type_map order in the model |
| 84 | # e.g., if type_map = ["O", "H"], then O=0, H=1 |
| 85 | atype = [0, 1, 1] |
| 86 | |
| 87 | e, f, v = dp.eval(coord, cell, atype) |
| 88 | |
| 89 | print(f"Energy (eV): {e}") # shape: (nframes, 1) |
| 90 | print(f"Forces (eV/A): {f}") # shape: (nframes, natoms, 3) |
| 91 | print(f"Virial (eV): {v}") # shape: (nframes, 9) |
| 92 | ``` |
| 93 | |
| 94 | ### Non-periodic Systems |
| 95 | |
| 96 | For non-periodic (isolated) systems, pass `cell=None`: |
| 97 | |
| 98 | ```python |
| 99 | e, f, v = dp.eval(coord, None, atype) |
| 100 | ``` |
| 101 | |
| 102 | ### Batch Prediction |
| 103 | |
| 104 | Process multiple frames at once: |
| 105 | |
| 106 | ```python |
| 107 | nframes = 10 |
| 108 | natoms = 3 |
| 109 | |
| 110 | coords = np.random.rand(nframes, natoms * 3) |
| 111 | cells = np.tile(np.diag([10.0, 10.0, 10.0]).reshape([1, -1]), (nframes, 1)) |
| 112 | atype = [0, 1, 1] |
| 113 | |
| 114 | e, f, v = dp.eval(coords, cells, atype) |
| 115 | # e: (nframes, 1) |
| 116 | # f: (nframes, natoms, 3) |
| 117 | # v: (nframes, 9) |
| 118 | ``` |
| 119 | |
| 120 | ### Evaluate Descriptors |
| 121 | |
| 122 | Extract the descriptor (atomic environment representation) from the model: |
| 123 | |
| 124 | ```python |
| 125 | descriptors = dp.eval_descriptor(coord, cell, atype) |
| 126 | # shape: (nframes, natoms, ndesc) |
| 127 | ``` |
| 128 | |
| 129 | This can also be done via CLI: |
| 130 | |
| 131 | ```bash |
| 132 | dp eval-desc -m model.pth -s /path/to/system -o desc_output |
| 133 | ``` |
| 134 | |
| 135 | ### Calculate Model Deviation |
| 136 | |
| 137 | Compare predictions from multiple models to estimate uncertainty: |
| 138 | |
| 139 | ```python |
| 140 | from deepmd.infer import calc_model_devi, DeepPot |
| 141 | |
| 142 | coord = np.array([[1, 0, 0], [0, 0, 1.5], [1, 0, 3]]).reshape([1, -1]) |
| 143 | cell = np.diag(10 * np.ones(3)).reshape([1, -1]) |
| 144 | atype = [1, 0, 1] |
| 145 | |
| 146 | graphs = [DeepPot("model_0.pth"), DeepPot("model_1.pth")] |
| 147 | model_devi = calc_model_devi(coord, cell, atype, graphs) |
| 148 | ``` |
| 149 | |
| 150 | Important: avoid loading the same model multiple times in a loop, as this can cause memory leaks. |
| 151 | |
| 152 | ## CLI Testing: dp test |
| 153 | |
| 154 | Test a frozen model against labeled data: |
| 155 | |
| 156 | ```bash |
| 157 | # Basic test |
| 158 | dp --pt test -m model.pth -s /path/to/test_system -n 30 |
| 159 | |
| 160 | # Test with detailed output |
| 161 | dp --pt test -m model.pth -s /path/to/test_system -n 30 -d test_detail |
| 162 | ``` |
| 163 | |
| 164 | ### dp test Options |
| 165 | |
| 166 | | Option | Description | |
| 167 | | ---------------- | ---------------------------------- | |
| 168 | | `-m MODEL` | Path to the frozen model file | |
| 169 | | `-s SYSTEM` | Path to the test data system | |
| 170 | | `-n NUMB` | Number o |