|
| 1 | +# SPDX-License-Identifier: LGPL-3.0-or-later |
| 2 | +from collections.abc import ( |
| 3 | + Callable, |
| 4 | +) |
| 5 | +from typing import ( |
| 6 | + Any, |
| 7 | +) |
| 8 | + |
| 9 | +import numpy as np |
| 10 | + |
| 11 | +from deepmd.common import ( |
| 12 | + get_hash, |
| 13 | +) |
| 14 | +from deepmd.utils.path import ( |
| 15 | + DPPath, |
| 16 | +) |
| 17 | + |
| 18 | + |
| 19 | +def _descriptor_rcut_smth(descrpt: Any) -> float: |
| 20 | + if hasattr(descrpt, "rcut_smth"): |
| 21 | + return descrpt.rcut_smth |
| 22 | + return descrpt.rcut_r_smth |
| 23 | + |
| 24 | + |
| 25 | +def _descriptor_sel(descrpt: Any, last_dim: int) -> list[int]: |
| 26 | + if hasattr(descrpt, "get_sel"): |
| 27 | + sel = descrpt.get_sel() |
| 28 | + elif last_dim == 1: |
| 29 | + sel = descrpt.sel_r |
| 30 | + else: |
| 31 | + sel = descrpt.sel_a |
| 32 | + if isinstance(sel, np.ndarray): |
| 33 | + sel = sel.tolist() |
| 34 | + elif isinstance(sel, int): |
| 35 | + sel = [sel] |
| 36 | + return [int(ii) for ii in sel] |
| 37 | + |
| 38 | + |
| 39 | +def _descriptor_stat_path( |
| 40 | + descrpt: Any, |
| 41 | + stat_file_path: DPPath | None, |
| 42 | + last_dim: int, |
| 43 | + mixed_types: bool, |
| 44 | +) -> DPPath | None: |
| 45 | + if stat_file_path is None: |
| 46 | + return None |
| 47 | + sel = _descriptor_sel(descrpt, last_dim) |
| 48 | + stat_hash = get_hash( |
| 49 | + { |
| 50 | + "type": "se_a" if last_dim == 4 else "se_r", |
| 51 | + "ntypes": descrpt.get_ntypes(), |
| 52 | + "rcut": round(descrpt.get_rcut(), 2), |
| 53 | + "rcut_smth": round(_descriptor_rcut_smth(descrpt), 2), |
| 54 | + "nsel": sum(sel), |
| 55 | + "sel": sel, |
| 56 | + "mixed_types": mixed_types, |
| 57 | + } |
| 58 | + ) |
| 59 | + return stat_file_path / stat_hash |
| 60 | + |
| 61 | + |
| 62 | +def _stat_keys(ntypes: int, angular: bool) -> list[str]: |
| 63 | + keys = [f"r_{ii}" for ii in range(ntypes)] |
| 64 | + if angular: |
| 65 | + keys.extend(f"a_{ii}" for ii in range(ntypes)) |
| 66 | + return keys |
| 67 | + |
| 68 | + |
| 69 | +def _load_se_input_stats( |
| 70 | + path: DPPath | None, |
| 71 | + ntypes: int, |
| 72 | + angular: bool, |
| 73 | +) -> dict[str, list[list[float]]] | None: |
| 74 | + if path is None or not path.is_dir(): |
| 75 | + return None |
| 76 | + if any(not (path / kk).is_file() for kk in _stat_keys(ntypes, angular)): |
| 77 | + return None |
| 78 | + |
| 79 | + sumr = [] |
| 80 | + sumn = [] |
| 81 | + sumr2 = [] |
| 82 | + suma = [] |
| 83 | + suma2 = [] |
| 84 | + for type_i in range(ntypes): |
| 85 | + r_stat = (path / f"r_{type_i}").load_numpy() |
| 86 | + sumn.append(float(r_stat[0])) |
| 87 | + sumr.append(float(r_stat[1])) |
| 88 | + sumr2.append(float(r_stat[2])) |
| 89 | + if angular: |
| 90 | + a_stat = (path / f"a_{type_i}").load_numpy() |
| 91 | + suma.append(float(a_stat[1]) / 3.0) |
| 92 | + suma2.append(float(a_stat[2]) / 3.0) |
| 93 | + |
| 94 | + ret = { |
| 95 | + "sumr": [sumr], |
| 96 | + "sumn": [sumn], |
| 97 | + "sumr2": [sumr2], |
| 98 | + } |
| 99 | + if angular: |
| 100 | + ret["suma"] = [suma] |
| 101 | + ret["suma2"] = [suma2] |
| 102 | + return ret |
| 103 | + |
| 104 | + |
| 105 | +def _save_se_input_stats( |
| 106 | + path: DPPath | None, |
| 107 | + stat_dict: dict[str, Any], |
| 108 | + ntypes: int, |
| 109 | + angular: bool, |
| 110 | +) -> None: |
| 111 | + if path is None: |
| 112 | + return |
| 113 | + path.mkdir(parents=True, exist_ok=True) |
| 114 | + |
| 115 | + sumr = np.sum(stat_dict["sumr"], axis=0) |
| 116 | + sumn = np.sum(stat_dict["sumn"], axis=0) |
| 117 | + sumr2 = np.sum(stat_dict["sumr2"], axis=0) |
| 118 | + if angular: |
| 119 | + suma = np.sum(stat_dict["suma"], axis=0) |
| 120 | + suma2 = np.sum(stat_dict["suma2"], axis=0) |
| 121 | + |
| 122 | + for type_i in range(ntypes): |
| 123 | + (path / f"r_{type_i}").save_numpy( |
| 124 | + np.array([sumn[type_i], sumr[type_i], sumr2[type_i]]) |
| 125 | + ) |
| 126 | + if angular: |
| 127 | + (path / f"a_{type_i}").save_numpy( |
| 128 | + np.array([3.0 * sumn[type_i], 3.0 * suma[type_i], 3.0 * suma2[type_i]]) |
| 129 | + ) |
| 130 | + |
| 131 | + |
| 132 | +def load_or_compute_se_input_stats( |
| 133 | + descrpt: Any, |
| 134 | + stat_file_path: DPPath | None, |
| 135 | + last_dim: int, |
| 136 | + compute: Callable[[], dict[str, Any]], |
| 137 | + mixed_types: bool = False, |
| 138 | +) -> dict[str, Any]: |
| 139 | + """Load or compute SE descriptor input statistics using EnvMatStatSe format.""" |
| 140 | + angular = last_dim == 4 |
| 141 | + stat_path = _descriptor_stat_path(descrpt, stat_file_path, last_dim, mixed_types) |
| 142 | + stat_dict = _load_se_input_stats(stat_path, descrpt.get_ntypes(), angular) |
| 143 | + if stat_dict is not None: |
| 144 | + return stat_dict |
| 145 | + |
| 146 | + stat_dict = compute() |
| 147 | + _save_se_input_stats(stat_path, stat_dict, descrpt.get_ntypes(), angular) |
| 148 | + return stat_dict |
0 commit comments