Skip to content

Commit 53756a3

Browse files
committed
Fix S24 weas visualisation: triplets -> only adsorbed molecule on surface
1 parent 0434750 commit 53756a3

3 files changed

Lines changed: 71 additions & 60 deletions

File tree

mlip_testing/analysis/surfaces/S24/analyse_S24.py

Lines changed: 43 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,37 @@ def system_names() -> list:
4949
"""
5050
system_names = []
5151
for model_name in MODELS:
52-
for system_path in (CALC_PATH / model_name).glob("*.xyz"):
53-
if system_path.name == "s24_mol_surface_atoms.xyz":
54-
continue
55-
system_names.append(system_path.stem)
56-
break
52+
model_dir = CALC_PATH / model_name
53+
if model_dir.exists():
54+
for system_path in sorted(model_dir.glob("*.xyz")):
55+
mol_surface = read(system_path)
56+
if "system_name" in mol_surface.info:
57+
system_names.append(mol_surface.info["system_name"])
58+
break
5759
return system_names
5860

5961

62+
def sys_ids() -> list:
63+
"""
64+
Get list of system IDs.
65+
66+
Returns
67+
-------
68+
list
69+
List of all system IDs.
70+
"""
71+
sys_ids = []
72+
for model_name in MODELS:
73+
model_dir = CALC_PATH / model_name
74+
if model_dir.exists():
75+
for system_path in sorted(model_dir.glob("*.xyz")):
76+
mol_surface = read(system_path)
77+
if "sys_id" in mol_surface.info:
78+
sys_ids.append(mol_surface.info["sys_id"])
79+
break
80+
return sys_ids
81+
82+
6083
@pytest.fixture
6184
@plot_parity(
6285
filename=OUT_PATH / "figure_adsorption_energies.json",
@@ -65,6 +88,7 @@ def system_names() -> list:
6588
y_label="Reference adsorption energy / eV",
6689
hoverdata={
6790
"System": system_names(),
91+
"Sys ID": sys_ids(),
6892
},
6993
)
7094
def adsorption_energies() -> dict[str, list]:
@@ -80,39 +104,26 @@ def adsorption_energies() -> dict[str, list]:
80104
ref_stored = False
81105

82106
for model_name in MODELS:
83-
for system_path in sorted((CALC_PATH / model_name).glob("*.xyz")):
84-
if system_path.name == "s24_mol_surface_atoms.xyz":
85-
continue
86-
87-
structs = read(system_path, index=":")
88-
if len(structs) != 3:
89-
continue
90-
91-
surface, mol_surface, molecule = structs
92-
93-
# Get predicted energies
94-
surface_e = surface.get_potential_energy()
95-
mol_surf_e = mol_surface.get_potential_energy()
96-
molecule_e = molecule.get_potential_energy()
97-
pred_ads_energy = compute_adsorption_energy(
98-
surface_e, mol_surf_e, molecule_e
99-
)
107+
model_dir = CALC_PATH / model_name
108+
if not model_dir.exists():
109+
results[model_name] = []
110+
continue
111+
112+
for system_path in sorted(model_dir.glob("*.xyz")):
113+
mol_surface = read(system_path)
114+
115+
# Get pre-calculated adsorption energies
116+
pred_ads_energy = mol_surface.info["adsorption_energy"]
100117
results[model_name].append(pred_ads_energy)
101-
102-
# Get reference energies (only store once)
118+
103119
if not ref_stored:
104-
ref_surface_e = surface.info["ref_energy"]
105-
ref_mol_surf_e = mol_surface.info["ref_energy"]
106-
ref_molecule_e = molecule.info["ref_energy"]
107-
ref_ads_energy = compute_adsorption_energy(
108-
ref_surface_e, ref_mol_surf_e, ref_molecule_e
109-
)
120+
ref_ads_energy = mol_surface.info["ref_adsorption_energy"]
110121
results["ref"].append(ref_ads_energy)
111122

112-
# Write structures in order
123+
# Write molecule-surface structure to app data
113124
structs_dir = OUT_PATH / model_name
114125
structs_dir.mkdir(parents=True, exist_ok=True)
115-
write(structs_dir / f"{system_path.stem}.xyz", structs)
126+
write(structs_dir / f"{system_path.stem}.xyz", mol_surface)
116127

117128
ref_stored = True
118129
return results

mlip_testing/app/surfaces/S24/app_S24.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ def register_callbacks(self) -> None:
3535
# Assets dir will be parent directory
3636
structs = [
3737
f"assets/surfaces/S24/{list(MODELS.keys())[0]}/{struct_file.stem}.xyz"
38-
for struct_file in structs_dir.glob("*.xyz")
39-
if struct_file.name != "s24_mol_surface_atoms.xyz"
38+
for struct_file in sorted(structs_dir.glob("*.xyz"))
4039
]
4140

4241
plot_from_table_column(
@@ -79,7 +78,7 @@ def get_app() -> S24App:
7978

8079
if __name__ == "__main__":
8180
# Create Dash app
82-
full_app = Dash(__name__, assets_folder=DATA_PATH.parent)
81+
full_app = Dash(__name__, assets_folder=DATA_PATH.parent.parent)
8382

8483
# Construct layout and register callbacks
8584
s24_app = get_app()

mlip_testing/calcs/surfaces/S24/calc_S24.py

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -78,52 +78,53 @@ def run(self):
7878
data = get_benchmark_data("s24.zip") / "s24/s24_data.extxyz"
7979
atoms_list = read(data, ":")
8080

81-
mol_surface_list = []
82-
8381
for i in tqdm(
8482
range(0, len(atoms_list), 3),
8583
desc=f"Processing Triplets for model: {self.model_name}",
8684
):
8785
surface = atoms_list[i]
8886
mol_surface = atoms_list[i + 1]
8987
molecule = atoms_list[i + 2]
90-
surface_formula = surface.get_chemical_formula()
91-
molecule_formula = molecule.get_chemical_formula()
88+
89+
# Create sys_id
90+
sys_id = f"{(i // 3) + 1:03d}"
9291

9392
# Store reference energies
9493
surface.info["ref_energy"] = surface.get_potential_energy()
9594
mol_surface.info["ref_energy"] = mol_surface.get_potential_energy()
9695
molecule.info["ref_energy"] = molecule.get_potential_energy()
9796

9897
# Store system information
98+
surface_formula = surface.get_chemical_formula()
99+
molecule_formula = molecule.get_chemical_formula()
99100
system_name = f"{surface_formula}-{molecule_formula}"
100-
surface.info["system_name"] = system_name
101+
102+
mol_surface.info["sys_id"] = sys_id
101103
mol_surface.info["system_name"] = system_name
102-
molecule.info["system_name"] = system_name
103-
104-
# Store structure types
105-
surface.info["structure_type"] = "surface"
106-
mol_surface.info["structure_type"] = "mol_surface"
107-
molecule.info["structure_type"] = "molecule"
108104

109105
# Evaluate with the model
110106
triplet = [surface, mol_surface, molecule]
111107
self.evaluate_energies(triplet, calc)
112108

113-
mol_surface_list.append(mol_surface)
114-
115-
# Write structures to output
116-
write_dir = OUT_PATH / self.model_name
117-
write_dir.mkdir(parents=True, exist_ok=True)
118-
write(write_dir / "s24_mol_surface_atoms.xyz", mol_surface_list)
119-
120-
# Write all structures organized by system
121-
for i in range(0, len(atoms_list), 3):
122-
surface = atoms_list[i]
123-
mol_surface = atoms_list[i + 1]
124-
molecule = atoms_list[i + 2]
125-
system_name = surface.info["system_name"]
126-
write(write_dir / f"{system_name}.xyz", [surface, mol_surface, molecule])
109+
# Calculate and store adsorption energies
110+
surface_e = surface.get_potential_energy()
111+
mol_surf_e = mol_surface.get_potential_energy()
112+
molecule_e = molecule.get_potential_energy()
113+
pred_ads_energy = self.compute_adsorption_energy(surface_e, mol_surf_e, molecule_e)
114+
115+
ref_surface_e = surface.info["ref_energy"]
116+
ref_mol_surf_e = mol_surface.info["ref_energy"]
117+
ref_molecule_e = molecule.info["ref_energy"]
118+
ref_ads_energy = self.compute_adsorption_energy(ref_surface_e, ref_mol_surf_e, ref_molecule_e)
119+
120+
# Store adsorption energies in mol_surface
121+
mol_surface.info["adsorption_energy"] = pred_ads_energy
122+
mol_surface.info["ref_adsorption_energy"] = ref_ads_energy
123+
124+
# Save individual molecule-surface structure using sys_id
125+
write_dir = OUT_PATH / self.model_name
126+
write_dir.mkdir(parents=True, exist_ok=True)
127+
write(write_dir / f"{sys_id}.xyz", mol_surface)
127128

128129

129130
def build_project(repro: bool = False) -> None:

0 commit comments

Comments
 (0)