Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ Native engine improvements add bounded [arrays and reusable modules](docs/STRUCT

The three previously recorded identifier/import failures are now repaired, with exact names preserved through import, simulation, rename and export. See the [identifier repair and compatibility evidence](docs/IDENTIFIER-IMPORT-FIX.md).

Scalar expression repairs preserve arithmetic in MDL imports, nested graphical functions and seeded random identities through XMILE round trips. Vensim-specific operations retain their own semantics. See [expression support and round-trip validation](docs/EXPRESSION-ROUNDTRIP.md).

## Run locally

```bash
Expand Down
27 changes: 22 additions & 5 deletions app/core/adaptive.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from app.core.limits import MAX_WORK_UNITS


DISCRETE_FUNCTIONS = {'FLOOR', 'CEIL', 'ROUND', 'SIGN', 'MOD', 'MODULO', 'INT', 'TRUNC', 'QUANTUM'}
DISCRETE_FUNCTIONS = {'FLOOR', 'CEIL', 'ROUND', 'SIGN', 'MOD', 'MODULO', 'REM', 'INT', 'TRUNC', 'QUANTUM', 'RAMP', 'RAMP_VENSIM'}
SAFE_DIVISION_FUNCTIONS = {'SAFEDIV', 'ZIDZ', 'XIDZ'}


Expand All @@ -26,9 +26,11 @@ class _UncertifiedBoundary(Exception):
def _matching_branch_limits(condition, yes, no, aliases, resolve_key=canonical_name):
"""A bounded algebraic proof, never a numerical sampling heuristic.

Expand algebraic aliases, impose each comparison's equality boundary, and
compare exact polynomials there. Continuous elementary calls can be opaque
atoms; an unknown denominator or domain is deliberately not certified.
Expand algebraic aliases and require a Boolean combination of direct
comparisons, whose equality surfaces cover every possible switch. Impose
each surface and compare exact polynomials there. Continuous elementary
calls can be opaque atoms; an unknown denominator or domain is deliberately
not certified.
This recognizes common zero-crossing feedback and piecewise friction laws
without claiming to locate arbitrary hybrid events.
"""
Expand Down Expand Up @@ -196,7 +198,22 @@ def polynomial(node):

try:
condition, yes, no = [expand(node) for node in (condition, yes, no)]
comparisons = [node for node in ast.walk(condition) if isinstance(node, ast.Compare)]
def comparison_leaves(node):
# A Boolean combination switches only where one of its comparison
# leaves can switch. Arbitrary arithmetic, numeric truth operands,
# or conditional wrappers can introduce additional zeros that the
# descendant comparisons do not describe.
if isinstance(node, ast.Compare):
if len(node.ops) != 1:
raise _UncertifiedBoundary
return [node]
if isinstance(node, ast.BoolOp) and isinstance(node.op, (ast.And, ast.Or)):
return [leaf for value in node.values for leaf in comparison_leaves(value)]
if isinstance(node, ast.UnaryOp) and isinstance(node.op, (ast.Not, ast.Invert)):
return comparison_leaves(node.operand)
raise _UncertifiedBoundary

comparisons = comparison_leaves(condition)
if not comparisons: return False
for comparison in comparisons:
# A chained XMILE comparison can switch when an intermediate
Expand Down
59 changes: 43 additions & 16 deletions app/core/equations.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,20 @@


# Time-based input functions
def step_function(height: float, step_time: float, time: float) -> float:
"""Step function: returns height after step_time, 0 before."""
return height if time >= step_time else 0.0
def step_function(height: float, step_time: float, time: float,
dt: Optional[float] = None, start_time: float = 0.0) -> float:
"""Step at the accepted input time, snapping only local grid roundoff."""
if time >= step_time:
return height
if dt is not None and math.isfinite(dt) and dt > 0:
# A decimal grid can land a few ULPs below its nominal event. Bound
# the snap by both local floating precision and a fraction of DT;
# never use a relative epsilon or the half-step Vensim convention.
tolerance = min(dt / 4, 4 * max(math.ulp(time), math.ulp(step_time),
math.ulp(start_time), math.ulp(time - start_time)))
if step_time - time <= tolerance:
return height
return 0.0


def pulse_function(magnitude: float, start_time: float, repeat_interval: float, time: float, dt: float) -> float:
Expand All @@ -62,16 +73,16 @@ def pulse_function(magnitude: float, start_time: float, repeat_interval: float,
return magnitude / dt if phase < dt and dt - phase > tolerance else 0.0


def ramp_function(slope: float, start_time: float, end_time: float, time: float) -> float:
def ramp_function(slope: float, start_time: float, end_time: Optional[float], time: float) -> float:
"""
Ramp function: increases linearly from start_time at rate 'slope'.
If end_time > start_time, ramp stops at end_time.
If end_time <= start_time, ramp continues indefinitely.
An omitted end (None) continues indefinitely. Preserve the native
explicit-end contract: only an end later than start stops the ramp.
"""
if time < start_time:
return 0.0

if end_time > start_time and time > end_time:
if end_time is not None and end_time > start_time and time > end_time:
# Ramp has ended - return final value
return slope * (end_time - start_time)

Expand Down Expand Up @@ -201,7 +212,9 @@ def segment(x1, y1, x2, y2):
"ceil": math.ceil,
"round": round,
"mod": operator.mod,
"modulo": math.fmod, # Vensim/C remainder keeps the dividend's sign.
# Explicit compatibility extensions: truncated, dividend-signed remainder.
"modulo": math.fmod,
"rem": math.fmod,
# Conditional function
"IF_THEN_ELSE": if_then_else,
"IF": if_then_else, # Alias for IF_THEN_ELSE
Expand All @@ -218,7 +231,7 @@ def segment(x1, y1, x2, y2):
"DELAYN": "DELAY_N", "DELAY": "DELAY_FIXED"}
STATEFUL_FUNCTIONS = {"DELAY1", "DELAY3", "DELAY_N", "DELAY_FIXED",
"SMOOTH", "SMOOTH3", "SMOOTH_N"} | set(STATEFUL_ALIASES)
CONTEXT_FUNCTIONS = {"STEP", "PULSE", "RAMP", "LOOKUP", "WITH_LOOKUP", "STARTTIME", "STOPTIME"} | STATEFUL_FUNCTIONS | STOCHASTIC_FUNCTIONS
CONTEXT_FUNCTIONS = {"STEP", "PULSE", "RAMP", "RAMP_VENSIM", "LOOKUP", "WITH_LOOKUP", "STARTTIME", "STOPTIME"} | STATEFUL_FUNCTIONS | STOCHASTIC_FUNCTIONS

SAFE_CONSTANTS: dict[str, float] = {
"pi": math.pi,
Expand Down Expand Up @@ -667,14 +680,14 @@ def _handle_context_function(self, func_name: str, args: list) -> float:
# at the accepted left state during RK4. Only declared test primitives
# use this snapshot; ordinary TIME/algebra and hidden states keep stage
# values. The accepted context has no snapshot, avoiding recursion.
if func_name in {"STEP", "PULSE", "RAMP"} and self.context.input_snapshot is not None:
if func_name in {"STEP", "PULSE", "RAMP", "RAMP_VENSIM"} and self.context.input_snapshot is not None:
accepted_values, accepted_context = self.context.input_snapshot
accepted_context.equation_key = self.context.equation_key
return SafeEvaluator(accepted_values, accepted_context)._handle_context_function(func_name, args)

# For other functions, evaluate all args
evaluated_args = [self.visit(arg) for arg in args]
if func_name in {"STEP", "PULSE", "RAMP"} and not all(
if func_name in {"STEP", "PULSE", "RAMP", "RAMP_VENSIM"} and not all(
math.isfinite(value) for value in evaluated_args
):
raise EquationError(f"{func_name} parameters must be finite")
Expand All @@ -684,7 +697,7 @@ def _handle_context_function(self, func_name: str, args: list) -> float:
if len(evaluated_args) != 2:
raise EquationError("STEP requires 2 arguments: height, step_time", code="function_signature", details={"function": "STEP", "argument_count": len(evaluated_args)})
height, step_time = evaluated_args[0], evaluated_args[1]
return step_function(height, step_time, self.context.input_time)
return step_function(height, step_time, self.context.input_time, self.context.dt, self.context.start_time)

elif func_name == "PULSE":
# PULSE(magnitude, start_time, repeat_interval=0)
Expand All @@ -695,13 +708,26 @@ def _handle_context_function(self, func_name: str, args: list) -> float:
repeat_interval = evaluated_args[2] if len(evaluated_args) > 2 else 0.0
return pulse_function(magnitude, start_time, repeat_interval, self.context.input_time, self.context.dt)

elif func_name == "RAMP_VENSIM":
# Vensim's three-argument dialect, including reversed end times.
# https://www.vensim.com/documentation/fn_ramp.html
if len(evaluated_args) != 3:
raise EquationError("RAMP_VENSIM requires 3 arguments: slope, start_time, end_time",
code="function_signature",
details={"function": func_name, "argument_count": len(evaluated_args)})
slope, start_time, end_time = evaluated_args
time = self.context.input_time
if time <= start_time:
return 0.0
return slope * ((time if time < end_time else end_time) - start_time)

elif func_name == "RAMP":
# RAMP(slope, start_time, end_time=0)
# RAMP(slope, start_time, optional end_time)
if not 2 <= len(evaluated_args) <= 3:
raise EquationError("RAMP requires 2 or 3 arguments: slope, start_time, optional end_time")
slope = evaluated_args[0]
start_time = evaluated_args[1]
end_time = evaluated_args[2] if len(evaluated_args) > 2 else 0.0
end_time = evaluated_args[2] if len(evaluated_args) > 2 else None
return ramp_function(slope, start_time, end_time, self.context.input_time)

raise EquationError(f"Unhandled context function: {func_name}")
Expand Down Expand Up @@ -1110,7 +1136,8 @@ def discover_stateful(equation, variables, context):
evaluator = SafeEvaluator(variables, context)
for node in ast.walk(tree):
if isinstance(node, ast.Call) and isinstance(node.func, ast.Name):
if decode_identifier(node.func.id).upper() in STATEFUL_FUNCTIONS:
requested = decode_identifier(node.func.id)
if requested.upper() in STATEFUL_FUNCTIONS and evaluator._table_name(requested) is None:
evaluator.visit(node)


Expand Down Expand Up @@ -1187,7 +1214,7 @@ def equation_features(equation, table_names=()):
return {"random_calls": sum(name in STOCHASTIC_FUNCTIONS for name in calls),
"fixed_delay": any(name in ("DELAY", "DELAY_FIXED") for name in calls),
"stateful_calls": sorted(set(calls) & STATEFUL_FUNCTIONS),
"test_input_calls": sorted(set(calls) & {"STEP", "PULSE", "RAMP"})}
"test_input_calls": sorted(set(calls) & {"STEP", "PULSE", "RAMP", "RAMP_VENSIM"})}


def _reference_names(tree):
Expand Down
2 changes: 2 additions & 0 deletions app/core/execution_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
MAX_PLAN_EDGES = 100_000
# Deliberately explicit: additions to the evaluator are not automatically eligible.
PURE_FUNCTIONS = frozenset({
'ARCSIN', 'ARCCOS', 'ARCTAN', 'INT', 'TRUNC',
'QUANTUM', 'SAFEDIV', 'ZIDZ', 'XIDZ', 'MODULO', 'REM',
"SIN", "COS", "TAN", "ASIN", "ACOS", "ATAN", "ATAN2", "SIGN",
"EXP", "LOG", "LN", "LOG10", "SQRT", "ABS", "MIN", "MAX", "POW",
"FLOOR", "CEIL", "ROUND", "MOD",
Expand Down
16 changes: 13 additions & 3 deletions app/core/mdl_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,13 @@ def warn(code, message, record=None):
if assignment is None:
fail("Only scalar assignment records are supported; standalone lookup/dimension declarations are unsupported", record=number)
lhs, rhs = head[:assignment].strip(), head[assignment+1:].strip()
if lhs.startswith('"') and lhs.endswith('"'):
quoted_lhs=lhs.startswith('"') and lhs.endswith('"')
if quoted_lhs:
try: name=json.loads(lhs)
except ValueError: fail("Invalid quoted identifier", "invalid_syntax", number)
else:
if any(c in lhs for c in "+-*/^<>,"):
fail("Operator-containing MDL names require double quotes", "invalid_syntax", number)
name=re.sub(r"\s+", " ", lhs).strip()
if not name or any(c in name for c in "[]:~|=()\\") or name.startswith("*"):
fail("Arrays, macros and non-scalar declarations are unsupported", record=number)
Expand Down Expand Up @@ -154,13 +157,18 @@ def numeric_control(key, default):
candidates.update({"time":"TIME","time_step":"DT","initial_time":"STARTTIME","final_time":"STOPTIME"})
patterns=[]
for key,name in candidates.items():
# Quoted declarations remain literal identifiers, but must not consume
# operators in an unquoted expression such as A-B. Keep this dialect's
# lexical rule separate from JSON/XMILE declared-name resolution.
if any(c in key or c in name for c in "+-*/^<>,"):
continue
pattern=r"[ _\n\u00a0]+".join(re.escape(part) for part in key.split("_"))
patterns.append((re.compile(pattern,re.I),key,name))
patterns.sort(key=lambda x:len(x[1]),reverse=True)
funcs={"IF THEN ELSE":"IF_THEN_ELSE",
"SIN":"sin","COS":"cos","TAN":"tan","ARCSIN":"asin","ARCCOS":"acos","ARCTAN":"atan",
"EXP":"exp","LN":"ln","SQRT":"sqrt","ABS":"abs","MIN":"min","MAX":"max","STEP":"STEP",
"LOG":"log","POWER":"pow","INTEGER":"TRUNC","MODULO":"MODULO","ZIDZ":"ZIDZ","XIDZ":"XIDZ","SMOOTH":"SMOOTH","SMOOTHI":"SMOOTH",
"LOG":"log","POWER":"pow","INTEGER":"TRUNC","MODULO":"MODULO","RAMP":"RAMP_VENSIM","ZIDZ":"ZIDZ","XIDZ":"XIDZ","SMOOTH":"SMOOTH","SMOOTHI":"SMOOTH",
"SMOOTH3":"SMOOTH3","SMOOTH3I":"SMOOTH3"}
# Vensim function contracts, not spelling-based aliases. LOG has an explicit
# base; information smooths share the engine's continuous hidden-stock law.
Expand All @@ -174,9 +182,11 @@ def numeric_control(key, default):
# is intentional here in favor of the documented Vensim source semantics.
# MODULO uses its distinct C-remainder primitive, never Python mod.
# https://vensim.com/documentation/fn_modulo.html
# MDL RAMP uses its distinct Vensim operator; native RAMP stays unchanged.
# https://vensim.com/documentation/fn_ramp.html
# Deliberately no PULSE (width/height), RANDOM (seed /
# truncation), SMOOTH N (discrete/order reduction), or material DELAY aliases.
strict_arities={"LOG":2,"POWER":2,"INTEGER":1,"MODULO":2,"ZIDZ":2,"XIDZ":3,"SMOOTH":2,"SMOOTHI":3,"SMOOTH3":2,"SMOOTH3I":3}
strict_arities={"LOG":2,"POWER":2,"INTEGER":1,"MODULO":2,"RAMP":3,"ZIDZ":2,"XIDZ":3,"SMOOTH":2,"SMOOTHI":3,"SMOOTH3":2,"SMOOTH3I":3}
function_patterns=[(re.compile(r"[ _\s]+".join(k.split()),re.I),v) for k,v in sorted(funcs.items(),key=lambda x:-len(x[0]))]
translation_work = 0
def charge_translation(record):
Expand Down
8 changes: 4 additions & 4 deletions app/core/units.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,8 @@ def expr(node, depth=0):
'SAFEDIV':(2,3),'ZIDZ':(2,2),'XIDZ':(3,3),
'ATAN2':(2,2),'EXP':(1,1),'LOG':(1,2),'LN':(1,2),'LOG10':(1,1),'SQRT':(1,1),
'ABS':(1,1),'FLOOR':(1,1),'CEIL':(1,1),'ROUND':(1,2),'SIGN':(1,1),
'POW':(2,3),'MOD':(2,2),'MODULO':(2,2),'IF':(3,3),'IF_THEN_ELSE':(3,3),
'STEP':(2,2),'PULSE':(2,3),'RAMP':(2,3),
'POW':(2,3),'MOD':(2,2),'MODULO':(2,2),'REM':(2,2),'IF':(3,3),'IF_THEN_ELSE':(3,3),
'STEP':(2,2),'PULSE':(2,3),'RAMP':(2,3),'RAMP_VENSIM':(3,3),
'RANDOM':(2,3),'RANDOM_UNIFORM':(2,3),'RANDOM_NORMAL':(2,3),'NORMAL':(2,3),
'RANDOM_BINOMIAL':(2,3),'MIN':(2,1000),'MAX':(2,1000)}
if upper in STATEFUL_FUNCTIONS:
Expand All @@ -258,7 +258,7 @@ def expr(node, depth=0):
note('unknown','unsupported_function','Three-argument POW units are not established')
return None
return visit(ast.BinOp(left=node.args[0], op=ast.Pow(), right=node.args[1]))
if upper in {'MOD', 'MODULO'}:
if upper in {'MOD', 'MODULO', 'REM'}:
return match(args[0],args[1],'MOD arguments')
if upper == 'QUANTUM':
return match(args[0], args[1], 'QUANTUM arguments')
Expand Down Expand Up @@ -303,7 +303,7 @@ def expr(node, depth=0):
if len(args) > initial_index:
match(args[0], args[initial_index], f'{upper} initial value')
return args[0]
if upper in {'STEP','PULSE','RAMP'} and len(args) >= 2:
if upper in {'STEP','PULSE','RAMP','RAMP_VENSIM'} and len(args) >= 2:
for value in args[1:]:
match(value, clock, f'{upper} timing')
return _combine(args[0], clock, -1 if upper == 'PULSE' else 1) if upper != 'STEP' else args[0]
Expand Down
Loading
Loading