Fix #1187: Allow cyclical subexpressions with 'constant over dt' flag#1782
Conversation
…r dt' flag - Exclude 'constant over dt' subexpressions from cycle detection - Add test_cyclical_subexpressions() to verify the fix - These subexpressions are evaluated once per timestep, not recursively
(basically a dummy commit to trigger test suite)
Previous implementation incorrectly set 'constant over dt' subexpressions to have no dependencies, which broke proper ordering and caused test failures. Changes: 1. Keep all dependencies for accurate ordering 2. Separate regular and 'constant over dt' subexpressions 3. Sort regular ones with strict topsort (no cycles allowed) 4. Sort 'constant over dt' ones with cycle-tolerant algorithm 5. Combine the two sorted lists Also fixes test_namespace_warnings test isolation issue by filtering to only count resolution_conflict warnings, making the test robust to garbage collection timing differences. Fixes: - test_constant_subexpression_order failure - test_namespace_warnings test isolation issue - Maintains test_cyclical_subexpressions functionality
|
Hi @nishantraghuvanshi Thanks for the PR update. Could you please briefly comment on why your last changes were necessary and give a short description of them? Thanks! |
|
Hi @mstimberg, The last commit fixes a subexpression ordering issue caused by the initial implementation. Problem: Setting "constant over dt" subexpressions to have no dependencies broke their ordering. Fix: Now I:
This allows cycles for "constant over dt" while maintaining correct ordering. Also fixed a test isolation issue. |
|
[Sorry, I meant to send this last week, but it was still sitting in one of my browser tabs |
Fixed issue where subexpression ordering broke cross-dependencies between regular and 'constant over dt' subexpressions. The old code separated and sorted the two groups independently, then combined them as [all regular] + [all constant], which broke dependencies like: s1(constant) -> s2(regular) -> s3(constant). The fix sorts all subexpressions together, then checks if cycles only involve 'constant over dt' subexpressions (allowed) or regular ones (error). This preserves old behavior for non-cyclical cases while allowing cyclical 'constant over dt' subexpressions as intended in issue brian-team#1187.
|
Hi @mstimberg, Thank you for the careful review. You were absolutely right to be concerned about the order of operations. The BugThe current implementation separated subexpressions into two groups and sorted them independently:
This breaks cross-dependencies. Example: The problem: when s3 is computed in SubexpressionUpdater, it needs s2 (which is inlined). But s2 references s1, which is found as a parameter in the model with its OLD value instead of the newly computed value. The FixI've rewritten _sort_subexpressions() to:
This preserves the old behavior for non-cyclical cases while still allowing cyclical "constant over dt" subexpressions. |
fixes Allow cyclical subexpressions if marked "constant over dt" #1187