-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathgfnff_validate.py
More file actions
290 lines (242 loc) · 10.4 KB
/
Copy pathgfnff_validate.py
File metadata and controls
290 lines (242 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#!/usr/bin/env python3
"""
GFN-FF Validation Framework
Systematically compares Curcuma native GFN-FF against Fortran reference
"""
import subprocess
import re
import json
from pathlib import Path
from dataclasses import dataclass
from typing import Dict, List, Tuple
@dataclass
class EnergyBreakdown:
"""Energy component breakdown"""
total: float = 0.0
bond: float = 0.0
angle: float = 0.0
torsion: float = 0.0
repulsion: float = 0.0
electrostatic: float = 0.0
dispersion: float = 0.0
coulomb: float = 0.0 # Curcuma's name
def dict(self):
return {
"total": self.total,
"bond": self.bond,
"angle": self.angle,
"torsion": self.torsion,
"repulsion": self.repulsion,
"electrostatic": self.electrostatic,
"dispersion": self.dispersion,
}
def __repr__(self):
return json.dumps(self.dict(), indent=2)
class GFNFFValidator:
"""Validates GFN-FF implementation"""
def __init__(self, curcuma_exe: str, reference_exe: str, test_dir: str):
self.curcuma_exe = Path(curcuma_exe)
self.reference_exe = Path(reference_exe)
self.test_dir = Path(test_dir)
def run_reference(self, xyz_file: str) -> EnergyBreakdown:
"""Run Fortran reference implementation"""
try:
result = subprocess.run(
[str(self.reference_exe), str(self.test_dir / xyz_file)],
cwd=str(self.test_dir),
capture_output=True,
text=True,
timeout=30
)
breakdown = EnergyBreakdown()
output = result.stdout + result.stderr
# Extract energy values from output
total_match = re.search(r'Total energy:\s+([-\d.]+)', output)
if total_match:
breakdown.total = float(total_match.group(1))
bond_match = re.search(r'Bond energy:\s+([-\d.]+)\s+Eh', output)
if bond_match:
breakdown.bond = float(bond_match.group(1))
angle_match = re.search(r'Angle energy:\s+([-\d.]+)\s+Eh', output)
if angle_match:
breakdown.angle = float(angle_match.group(1))
torsion_match = re.search(r'Torsion energy:\s+([-\d.]+)\s+Eh', output)
if torsion_match:
breakdown.torsion = float(torsion_match.group(1))
repulsion_match = re.search(r'Repulsion energy:\s+([-\d.]+)\s+Eh', output)
if repulsion_match:
breakdown.repulsion = float(repulsion_match.group(1))
electrostatic_match = re.search(r'Electrostatic[:\s]+([-\d.]+)\s+Eh', output)
if electrostatic_match:
breakdown.electrostatic = float(electrostatic_match.group(1))
dispersion_match = re.search(r'Dispersion[:\s]+([-\d.]+)\s+Eh', output)
if dispersion_match:
breakdown.dispersion = float(dispersion_match.group(1))
return breakdown
except Exception as e:
print(f"Error running reference: {e}")
return None
def run_curcuma(self, xyz_file: str) -> EnergyBreakdown:
"""Run Curcuma native GFN-FF"""
try:
result = subprocess.run(
[str(self.curcuma_exe), "-sp", str(self.test_dir / xyz_file),
"-method", "cgfnff", "-verbosity", "2"],
capture_output=True,
text=True,
timeout=30
)
# Remove ANSI color codes
output = result.stdout + result.stderr
output = re.sub(r'\x1b\[[0-9;]*m', '', output)
breakdown = EnergyBreakdown()
# Extract total energy
total_match = re.search(r'Single Point Energy = ([-\d.]+)\s+Eh', output)
if total_match:
breakdown.total = float(total_match.group(1))
# Extract component energies
# Pattern: [PARAM] bond_energy: -0.165008 Eh
bond_match = re.search(r'bond_energy:\s+([-\d.]+)\s+Eh', output)
if bond_match:
breakdown.bond = float(bond_match.group(1))
angle_match = re.search(r'angle_energy:\s+([-\d.]+)\s+Eh', output)
if angle_match:
breakdown.angle = float(angle_match.group(1))
# Look for repulsion - try multiple patterns
repulsion_match = re.search(r'thread_repulsion_energy:\s+([-\d.]+)\s+Eh', output)
if repulsion_match:
breakdown.repulsion = float(repulsion_match.group(1))
else:
repulsion_match = re.search(r'\w\w_repulsion:\s+([-\d.]+)\s+Eh', output)
if repulsion_match:
breakdown.repulsion = float(repulsion_match.group(1))
# Look for dispersion
dispersion_match = re.search(r'thread_dispersion_energy:\s+([-\d.]+)\s+Eh', output)
if dispersion_match:
breakdown.dispersion = float(dispersion_match.group(1))
else:
dispersion_match = re.search(r'GFNFF_dispersion:\s+([-\d.]+)\s+Eh', output)
if dispersion_match:
breakdown.dispersion = float(dispersion_match.group(1))
# Look for Coulomb - try multiple patterns
coulomb_match = re.search(r'thread_coulomb_energy:\s+([-\d.]+)\s+Eh', output)
if coulomb_match:
breakdown.coulomb = float(coulomb_match.group(1))
else:
# Try alternative pattern for Coulomb output
coulomb_match = re.search(r'GFNFF_coulomb:\s+([-\d.]+)\s+Eh', output)
if coulomb_match:
breakdown.coulomb = float(coulomb_match.group(1))
return breakdown
except Exception as e:
print(f"Error running Curcuma: {e}")
return None
def compare_energies(self, ref: EnergyBreakdown, cur: EnergyBreakdown) -> Dict:
"""Compare energy components"""
def relative_error(ref, cur):
if abs(ref) < 1e-10:
return 0.0 if abs(cur) < 1e-10 else float('inf')
return abs((cur - ref) / ref) * 100.0
comparison = {
"total": {
"reference": ref.total,
"curcuma": cur.total,
"error_abs": cur.total - ref.total,
"error_pct": relative_error(ref.total, cur.total)
},
"bond": {
"reference": ref.bond,
"curcuma": cur.bond,
"error_abs": cur.bond - ref.bond,
"error_pct": relative_error(ref.bond, cur.bond)
},
"angle": {
"reference": ref.angle,
"curcuma": cur.angle,
"error_abs": cur.angle - ref.angle,
"error_pct": relative_error(ref.angle, cur.angle)
},
"repulsion": {
"reference": ref.repulsion,
"curcuma": cur.repulsion,
"error_abs": cur.repulsion - ref.repulsion,
"error_pct": relative_error(ref.repulsion, cur.repulsion)
},
"dispersion": {
"reference": ref.dispersion,
"curcuma": cur.dispersion,
"error_abs": cur.dispersion - ref.dispersion,
"error_pct": relative_error(ref.dispersion, cur.dispersion)
},
"electrostatic": {
"reference": ref.electrostatic,
"curcuma": cur.coulomb,
"error_abs": cur.coulomb - ref.electrostatic,
"error_pct": relative_error(ref.electrostatic, cur.coulomb)
},
}
return comparison
def validate_molecule(self, xyz_file: str) -> Dict:
"""Validate a single molecule"""
print(f"\n{'='*80}")
print(f"Validating: {xyz_file}")
print(f"{'='*80}")
# Run both implementations
ref_energy = self.run_reference(xyz_file)
cur_energy = self.run_curcuma(xyz_file)
if not ref_energy or not cur_energy:
print("ERROR: Failed to run implementations")
return None
# Compare
comparison = self.compare_energies(ref_energy, cur_energy)
# Print results
self._print_results(xyz_file, comparison)
return comparison
def _print_results(self, molecule: str, comparison: Dict):
"""Pretty print comparison results"""
print(f"\nMolecule: {molecule}")
print(f"\n{'Term':<15} {'Reference':>15} {'Curcuma':>15} {'Abs Error':>15} {'% Error':>10}")
print("-" * 75)
for term in ["bond", "angle", "repulsion", "dispersion", "electrostatic", "total"]:
if term in comparison:
c = comparison[term]
pct = c["error_pct"]
if pct == float('inf'):
pct_str = "∞"
else:
pct_str = f"{pct:.2f}%"
print(f"{term:<15} {c['reference']:>15.10f} {c['curcuma']:>15.10f} "
f"{c['error_abs']:>15.10f} {pct_str:>10}")
# Color code: Green if <1%, Yellow if <5%, Red if >5%
if pct < 1.0:
status = "✓ EXCELLENT"
elif pct < 5.0:
status = "⚠ GOOD"
elif pct < 10.0:
status = "! MODERATE"
else:
status = "✗ POOR"
print(f" Status: {status}")
def validate_all(self, molecules: List[str]) -> Dict:
"""Validate all molecules"""
results = {}
for mol in molecules:
results[mol] = self.validate_molecule(mol)
return results
if __name__ == "__main__":
# Paths
curcuma_exe = "/home/conrad/src/claude_curcuma/curcuma/release/curcuma"
reference_exe = "/home/conrad/src/claude_curcuma/curcuma/external/gfnff/build/test/gfnff-gfnff_analyze-test"
test_dir = "/home/conrad/src/claude_curcuma/curcuma/test_cases/molecules/dimers"
# Test molecules
molecules = ["HH.xyz", "HCl.xyz", "OH.xyz"]
validator = GFNFFValidator(curcuma_exe, reference_exe, test_dir)
results = validator.validate_all(molecules)
# Summary
print(f"\n{'='*80}")
print("VALIDATION SUMMARY")
print(f"{'='*80}")
# Write results to file
with open("gfnff_validation_results.json", "w") as f:
json.dump(results, f, indent=2, default=str)
print("\nResults saved to: gfnff_validation_results.json")