Skip to content

Commit 0e536a6

Browse files
authored
Merge pull request #12 from EXP-code/copilot/sub-pr-11
Add input validation for gravitational constants in compute_scales
2 parents eefd141 + c28a276 commit 0e536a6

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ Binary/*run1*
55

66
DiskHaloA/*run0*
77
DiskHaloA/processor.rates
8+
__pycache__/

EXP-units/expunits.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@ def compute_scales(G_old, G_new, s_L, s_V, s_M):
106106
s_L, s_V, s_M are either floats or None.
107107
Returns (s_L, s_V, s_M). Raises ValueError if insufficient or inconsistent.
108108
"""
109+
# Validate G_new is physically meaningful
110+
if G_new <= 0:
111+
raise ValueError(f"G_new must be positive (got {G_new}). Zero or negative gravitational constants are not physically meaningful.")
112+
if G_old <= 0:
113+
raise ValueError(f"G_old must be positive (got {G_old}). Zero or negative gravitational constants are not physically meaningful.")
114+
109115
known = [(s_L is not None), (s_V is not None), (s_M is not None)]
110116
n_known = sum(known)
111117

@@ -114,7 +120,7 @@ def compute_scales(G_old, G_new, s_L, s_V, s_M):
114120
G_check = (s_L * s_V**2) / (G_old * s_M)
115121
if not math.isfinite(G_check):
116122
raise ValueError("Computed G_check is not finite.")
117-
if abs((G_check - G_new) / (abs(G_new) if G_new != 0 else 1.0)) > 1e-9:
123+
if abs((G_check - G_new) / G_new) > 1e-9:
118124
raise ValueError(f"Inconsistent scales: G_new requested {G_new} but scales give {G_check}.")
119125
return s_L, s_V, s_M
120126

0 commit comments

Comments
 (0)