Spaces:
Runtime error
Runtime error
Add SCF convergence check and improve cube file parsing robustness
Browse files- app.py +64 -36
- force_rebuild.txt +1 -1
app.py
CHANGED
|
@@ -4,41 +4,66 @@ import numpy as np
|
|
| 4 |
|
| 5 |
def parse_cube_file(cube_file):
|
| 6 |
"""Parse a cube file and return grid coordinates and values."""
|
| 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 |
import gradio as gr
|
|
@@ -195,7 +220,10 @@ def smiles_to_molecular_orbitals(smiles_input: str, name_input: str) -> str:
|
|
| 195 |
|
| 196 |
# Run Hartree-Fock calculation
|
| 197 |
mf = pyscf.scf.RHF(mol_pyscf)
|
| 198 |
-
mf.kernel()
|
|
|
|
|
|
|
|
|
|
| 199 |
|
| 200 |
# Get HOMO and LUMO indices
|
| 201 |
nocc = mol_pyscf.nelectron // 2
|
|
|
|
| 4 |
|
| 5 |
def parse_cube_file(cube_file):
|
| 6 |
"""Parse a cube file and return grid coordinates and values."""
|
| 7 |
+
try:
|
| 8 |
+
with open(cube_file, 'r') as f:
|
| 9 |
+
lines = f.readlines()
|
| 10 |
+
|
| 11 |
+
if len(lines) < 6:
|
| 12 |
+
raise ValueError("Cube file too short")
|
| 13 |
+
|
| 14 |
+
# Find the line with natoms (should be the third line or so)
|
| 15 |
+
for i, line in enumerate(lines):
|
| 16 |
+
parts = line.split()
|
| 17 |
+
if len(parts) >= 4:
|
| 18 |
+
try:
|
| 19 |
+
natoms = int(float(parts[0])) # Try to parse as int, allowing for float strings
|
| 20 |
+
if natoms > 0:
|
| 21 |
+
break
|
| 22 |
+
except ValueError:
|
| 23 |
+
continue
|
| 24 |
+
else:
|
| 25 |
+
raise ValueError("Could not find natoms in cube file")
|
| 26 |
+
|
| 27 |
+
# Now i is the line with natoms
|
| 28 |
+
origin = np.array([float(x) for x in lines[i].split()[1:4]])
|
| 29 |
+
|
| 30 |
+
# Skip atom lines
|
| 31 |
+
grid_start = i + 1 + natoms
|
| 32 |
+
if grid_start >= len(lines):
|
| 33 |
+
raise ValueError("Cube file malformed")
|
| 34 |
+
|
| 35 |
+
grid_line = lines[grid_start]
|
| 36 |
+
nx = int(grid_line.split()[0])
|
| 37 |
+
dx = float(grid_line.split()[1])
|
| 38 |
+
|
| 39 |
+
grid_line = lines[grid_start + 1]
|
| 40 |
+
ny = int(grid_line.split()[1])
|
| 41 |
+
dy = float(grid_line.split()[2])
|
| 42 |
+
|
| 43 |
+
grid_line = lines[grid_start + 2]
|
| 44 |
+
nz = int(grid_line.split()[2])
|
| 45 |
+
dz = float(grid_line.split()[3])
|
| 46 |
+
|
| 47 |
+
# Read data
|
| 48 |
+
data_start = grid_start + 3
|
| 49 |
+
data = []
|
| 50 |
+
for line in lines[data_start:]:
|
| 51 |
+
data.extend([float(x) for x in line.split()])
|
| 52 |
+
|
| 53 |
+
if len(data) != nx * ny * nz:
|
| 54 |
+
raise ValueError(f"Data size mismatch: expected {nx*ny*nz}, got {len(data)}")
|
| 55 |
+
|
| 56 |
+
# Reshape data
|
| 57 |
+
values = np.array(data).reshape((nx, ny, nz))
|
| 58 |
+
|
| 59 |
+
# Create coordinate grids
|
| 60 |
+
x = origin[0] + np.arange(nx) * dx
|
| 61 |
+
y = origin[1] + np.arange(ny) * dy
|
| 62 |
+
z = origin[2] + np.arange(nz) * dz
|
| 63 |
+
|
| 64 |
+
return x, y, z, values
|
| 65 |
+
except Exception as e:
|
| 66 |
+
raise ValueError(f"Error parsing cube file: {e}")
|
| 67 |
|
| 68 |
|
| 69 |
import gradio as gr
|
|
|
|
| 220 |
|
| 221 |
# Run Hartree-Fock calculation
|
| 222 |
mf = pyscf.scf.RHF(mol_pyscf)
|
| 223 |
+
energy = mf.kernel()
|
| 224 |
+
|
| 225 |
+
if not mf.converged:
|
| 226 |
+
return "<p>Hartree-Fock calculation did not converge. Try a smaller molecule or different geometry.</p>"
|
| 227 |
|
| 228 |
# Get HOMO and LUMO indices
|
| 229 |
nocc = mol_pyscf.nelectron // 2
|
force_rebuild.txt
CHANGED
|
@@ -1,2 +1,2 @@
|
|
| 1 |
# Force rebuild
|
| 2 |
-
2025-11-08
|
|
|
|
| 1 |
# Force rebuild
|
| 2 |
+
2025-11-08 v6
|