Skip to content

Commit 1780c7c

Browse files
ensure that model coerces pmg vasp output into smaller objects
1 parent 81f144a commit 1780c7c

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

pymatgen/io/validation/common.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import os
99
import numpy as np
1010
from pathlib import Path
11-
from pydantic import BaseModel, Field, model_serializer, PrivateAttr
11+
from pydantic import BaseModel, Field, model_validator, model_serializer, PrivateAttr
1212
from typing import TYPE_CHECKING, Any, Optional
1313

1414
from pymatgen.core import Structure
@@ -199,6 +199,20 @@ class VaspFiles(BaseModel):
199199
outcar: Optional[LightOutcar] = None
200200
vasprun: Optional[LightVasprun] = None
201201

202+
@model_validator(mode="before")
203+
@classmethod
204+
def coerce_to_lightweight(cls, config: Any) -> Any:
205+
"""Ensure that pymatgen objects are converted to minimal representations."""
206+
if isinstance(config.get("outcar"), Outcar):
207+
config["outcar"] = LightOutcar(
208+
drift=config["outcar"].drift,
209+
magnetization=config["outcar"].magnetization,
210+
)
211+
212+
if isinstance(config.get("vasprun"), Vasprun):
213+
config["vasprun"] = LightVasprun.from_vasprun(config["vasprun"])
214+
return config
215+
202216
@property
203217
def md5(self) -> str:
204218
"""Get MD5 of VaspFiles for use in validation checks."""
@@ -256,15 +270,7 @@ def from_paths(
256270
if file_name == "potcar":
257271
potcar_enmax = max(ps.ENMAX for ps in Potcar.from_file(path))
258272

259-
if config.get("outcar"):
260-
config["outcar"] = LightOutcar(
261-
drift=config["outcar"].drift,
262-
magnetization=config["outcar"].magnetization,
263-
)
264-
265-
if config.get("vasprun"):
266-
config["vasprun"] = LightVasprun.from_vasprun(config["vasprun"])
267-
elif not config["user_input"]["incar"].get("ENCUT") and potcar_enmax:
273+
if not config.get("vasprun") and not config["user_input"]["incar"].get("ENCUT") and potcar_enmax:
268274
config["user_input"]["incar"]["ENCUT"] = potcar_enmax
269275

270276
return cls(**config)

0 commit comments

Comments
 (0)