|
11 | 11 | if TYPE_CHECKING:
|
12 | 12 | from dpdata.utils import FileType
|
13 | 13 |
|
| 14 | +import os |
| 15 | + |
14 | 16 | from ..unit import (
|
15 | 17 | EnergyConversion,
|
16 | 18 | ForceConversion,
|
|
20 | 22 |
|
21 | 23 | ry2ev = EnergyConversion("rydberg", "eV").value()
|
22 | 24 | kbar2evperang3 = PressureConversion("kbar", "eV/angstrom^3").value()
|
| 25 | +gpa2evperbohr = PressureConversion("GPa", "eV/bohr^3").value() |
23 | 26 |
|
24 | 27 | length_convert = LengthConversion("bohr", "angstrom").value()
|
25 | 28 | energy_convert = EnergyConversion("hartree", "eV").value()
|
@@ -228,6 +231,32 @@ def to_system_data(input_name, prefix, begin=0, step=1):
|
228 | 231 | )
|
229 | 232 | except FileNotFoundError:
|
230 | 233 | data["cells"] = np.tile(cell, (data["coords"].shape[0], 1, 1))
|
| 234 | + |
| 235 | + # handle virial |
| 236 | + stress_fname = prefix + ".str" |
| 237 | + if os.path.exists(stress_fname): |
| 238 | + # 1. Read stress tensor (in GPa) for each structure |
| 239 | + stress, vsteps = load_data(stress_fname, 3, begin=begin, step=step, convert=1.0) |
| 240 | + if csteps != vsteps: |
| 241 | + csteps.append(None) |
| 242 | + vsteps.append(None) |
| 243 | + for int_id in range(len(csteps)): |
| 244 | + if csteps[int_id] != vsteps[int_id]: |
| 245 | + break |
| 246 | + step_id = begin + int_id * step |
| 247 | + raise RuntimeError( |
| 248 | + f"the step key between files are not consistent. " |
| 249 | + f"The difference locates at step: {step_id}, " |
| 250 | + f".pos is {csteps[int_id]}, .str is {vsteps[int_id]}" |
| 251 | + ) |
| 252 | + # 2. Calculate volume from cell. revert unit to bohr before taking det |
| 253 | + volumes = np.linalg.det(data["cells"] / length_convert).reshape(-1) |
| 254 | + # 3. Calculate virials for each structure |
| 255 | + virials = [] |
| 256 | + for i in range(stress.shape[0]): |
| 257 | + virials.append(stress[i] * gpa2evperbohr * volumes[i]) |
| 258 | + data["virials"] = np.array(virials) # shape: [nframe, 3, 3] |
| 259 | + |
231 | 260 | return data, csteps
|
232 | 261 |
|
233 | 262 |
|
|
0 commit comments