|
5 | 5 | from functools import cached_property |
6 | 6 | import hashlib |
7 | 7 | from importlib import import_module |
| 8 | +from monty.serialization import loadfn |
8 | 9 | import os |
9 | 10 | import numpy as np |
10 | 11 | from pathlib import Path |
11 | 12 | from pydantic import BaseModel, Field, model_validator, model_serializer, PrivateAttr |
12 | 13 | from typing import TYPE_CHECKING, Any, Optional |
13 | 14 |
|
14 | 15 | from pymatgen.core import Structure |
15 | | -from pymatgen.io.vasp import Incar, Kpoints, Poscar, Potcar, Outcar, Vasprun |
| 16 | +from pymatgen.io.vasp.inputs import POTCAR_STATS_PATH, Incar, Kpoints, Poscar, Potcar |
| 17 | +from pymatgen.io.vasp.outputs import Outcar, Vasprun |
16 | 18 | from pymatgen.io.vasp.sets import VaspInputSet |
17 | 19 |
|
18 | 20 | from pymatgen.io.validation.vasp_defaults import VaspParam, VASP_DEFAULTS_DICT |
@@ -170,18 +172,49 @@ def from_vasp_input_set(cls, vis: VaspInputSet) -> Self: |
170 | 172 | ----------- |
171 | 173 | VaspInputSafe |
172 | 174 | """ |
173 | | - new_vis = cls( |
174 | | - **{ |
175 | | - k: getattr(vis, k) |
176 | | - for k in ( |
177 | | - "incar", |
178 | | - "kpoints", |
179 | | - "structure", |
180 | | - ) |
181 | | - }, |
182 | | - potcar=PotcarSummaryStats.from_file(vis.potcar), |
183 | | - potcar_functional=vis.potcar_functional, |
| 175 | + |
| 176 | + cls_config: dict[str, Any] = { |
| 177 | + k: getattr(vis, k) |
| 178 | + for k in ( |
| 179 | + "incar", |
| 180 | + "kpoints", |
| 181 | + "structure", |
| 182 | + ) |
| 183 | + } |
| 184 | + try: |
| 185 | + # Cleaner solution (because these map one POTCAR symbol to one POTCAR) |
| 186 | + # Requires POTCAR library to be available |
| 187 | + potcar: list[PotcarSummaryStats] = PotcarSummaryStats.from_file(vis.potcar) |
| 188 | + potcar_functional = vis.potcar_functional |
| 189 | + |
| 190 | + except FileNotFoundError: |
| 191 | + # Fall back to pregenerated POTCAR meta |
| 192 | + # Note that multiple POTCARs may use the same symbol / TITEL |
| 193 | + # within a given release of VASP. |
| 194 | + |
| 195 | + potcar_stats = loadfn(POTCAR_STATS_PATH) |
| 196 | + potcar_functional = vis._config_dict["POTCAR_FUNCTIONAL"] |
| 197 | + potcar = [] |
| 198 | + for ele in vis.structure.elements: |
| 199 | + if potcar_symb := vis._config_dict["POTCAR"].get(ele.name): |
| 200 | + for titel_no_spc, potcars in potcar_stats[potcar_functional].items(): |
| 201 | + for entry in potcars: |
| 202 | + if entry["symbol"] == potcar_symb: |
| 203 | + titel_comp = titel_no_spc.split(potcar_symb) |
| 204 | + |
| 205 | + potcar += [ |
| 206 | + PotcarSummaryStats( |
| 207 | + titel=" ".join([titel_comp[0], potcar_symb, titel_comp[1]]), |
| 208 | + lexch=entry.get("LEXCH"), |
| 209 | + **entry, |
| 210 | + ) |
| 211 | + ] |
| 212 | + |
| 213 | + cls_config.update( |
| 214 | + potcar=potcar, |
| 215 | + potcar_functional=potcar_functional, |
184 | 216 | ) |
| 217 | + new_vis = cls(**cls_config) |
185 | 218 | new_vis._pmg_vis = vis |
186 | 219 | return new_vis |
187 | 220 |
|
|
0 commit comments