Skip to content

Conversation

@ChrisRackauckas-Claude
Copy link

Fixes #475

Problem

When a user forgot to include a symbolic variable in the dependent variables list of PDESystem, the system transformation would enter an infinite loop with endless "Expanding derivatives" warnings.

For example:

@variables p(..) S(..)
eq = [S(t, v) ~ -p(t, v) - ∂ᵥ(p(t, v)), ∂ₜ(p(t, v)) ~ -∂ᵥ(S(t, v))]
@named sys = PDESystem(eq, bcs, domains, [t, v], [p(t, v)])  # Missing S(t, v)
discretization = MOLFiniteDifference([v => 0.5], t)
prob = discretize(sys, discretization)  # Infinite loop!

This would print endless warnings:

┌ Warning: Expanding derivatives in term Differential(v)(S(t, v)).
└ @ MethodOfLines ...

Solution

Added upfront validation in transform_pde_system! to detect unknown symbolic variables before the transformation loop begins. The implementation:

  1. Scans all equations and boundary conditions for function-like calls
  2. Identifies missing variables - finds variables that appear in equations but not in depvar_ops
  3. Filters out built-in operators - ignores +, -, *, /, etc. and Differential/Integral
  4. Throws a clear error immediately with the names of missing variables and helpful guidance:
ArgumentError: Found unknown symbolic variable(s): S. These variables appear in 
the equations or boundary conditions but were not included in the dependent 
variables list of the PDESystem. Please add them to the dependent variables, 
e.g.: PDESystem(eqs, bcs, domains, ivs, [existing_vars..., S])

Implementation Details

  • Added find_unknown_variables() helper function that traverses symbolic expressions
  • Check runs before the transformation loop to fail fast
  • Kept fallback max iteration check (1000 iterations) for other potential infinite loops
  • Added tracking of seen terms during expansion to detect repetition

Testing

  • ✅ Tested with the exact example from issue Forgetting a variable in PDESystem results in infinite loop #475 - now throws clear error
  • ✅ Verified correct systems (with all variables included) still work properly
  • ✅ Ran existing test suite components to confirm no regressions
  • ✅ Formatted code with JuliaFormatter using SciMLStyle

Example Before/After

Before:

┌ Warning: Expanding derivatives in term Differential(v)(S(t, v)).
└ @ MethodOfLines ...
[infinite loop, user has to kill process]

After:

ERROR: ArgumentError: Found unknown symbolic variable(s): S. These variables 
appear in the equations or boundary conditions but were not included in the 
dependent variables list of the PDESystem. Please add them to the dependent 
variables, e.g.: PDESystem(eqs, bcs, domains, ivs, [existing_vars..., S])

🤖 Generated with Claude Code

Co-Authored-By: Claude [email protected]

Fixes SciML#475

## Problem
When a user forgot to include a symbolic variable in the dependent variables
list of PDESystem, the system transformation would enter an infinite loop with
endless "Expanding derivatives" warnings.

For example:
```julia
@variables p(..) S(..)
eq = [S(t, v) ~ -p(t, v) - ∂ᵥ(p(t, v)), ∂ₜ(p(t, v)) ~ -∂ᵥ(S(t, v))]
@nAmed sys = PDESystem(eq, bcs, domains, [t, v], [p(t, v)])  # Missing S(t, v)
```

## Solution
Added upfront validation in `transform_pde_system!` to detect unknown symbolic
variables before the transformation loop begins. The fix:

1. Scans all equations and boundary conditions for function-like calls
2. Identifies variables that appear in equations but not in `depvar_ops`
3. Filters out built-in operators (+, -, *, /, etc.) and Differential/Integral
4. Throws a clear ArgumentError with the names of missing variables
5. Includes helpful guidance on how to fix the issue

## Testing
- Tested with the exact example from issue SciML#475
- Verified the error is thrown immediately with a helpful message
- Verified that correct systems (with all variables) still work
- Ran existing test suite to confirm no regressions

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Forgetting a variable in PDESystem results in infinite loop

2 participants