Repository navigation
Support Symbolic Struct handling in mtkcompile and solve calls - #5008
Conversation
| if !fast_path | ||
| op = operating_point_preprocess(sys, op) | ||
| end | ||
| split_record_entries!(op) |
There was a problem hiding this comment.
I'm really not sold on this approach. Ideally struct variables should be considered atomic the same way arrays are. AtomicArrayDict should validate that keys are not field accesses. Otherwise, people can specify struct initial conditions in multiple ways: foo => MyStruct(..) or [foo.x => 1, foo.y => [2, 3] and so on. An even worse case is foo.x[1].y.z => 3. Maintaining atomicity simplifies it a bit. I think fixpoint_sub with constant-folding should work if foo is substituted for a concrete value.
| which stays whole because ordinary array atomicity already covers it. It is the same | ||
| granularity `Initial` uses, which is what the initialization system pairs `op` keys with. |
There was a problem hiding this comment.
Initial and AtomicArrayDict should agree about atomicity, otherwise e.g. root is atomic but Initial(root) isn't. Downstream should just work if SymbolicGetproperty constant-folds properly? Does it not?
There was a problem hiding this comment.
This isn't addressed yet.
There was a problem hiding this comment.
- They do agree about atomicity, lowering drops to the individual fields to apply init conditions and build equations.
- We don't fold, and there are many weird cases here (as it stands, initial(P).x is different from initial(P.x), we design so that we apply conditions to the individual fields, rather than providing one full initial struct).
| end | ||
| # Records are the right granularity for resolving what was asked for, but nothing | ||
| # downstream expects a record key, so lower before returning. | ||
| lower_record_entries!(dd, default) |
There was a problem hiding this comment.
As in #5008 (comment), I think structs should be considered atomic here. What breaks if they are not?
There was a problem hiding this comment.
So from a user perspective, at the moment, ICs can be passed both as a full struct or as individual components. I think this is reasonable, because if the whole struct is known, then they give that directly, otherwise if they have some guesses they can mix that in individually as well. On the output side, we currently only give per-component solution states. I think this second half motivates the lack of atomicity and the decomposition here. We can reassemble the full state after as a later feature, but since we integrate per component, I believe lowering is reasonable here. The distinction between this and arrays is that I can always integrate a numeric array directly, but a struct has to be flattened into something array-like before I can integrate it.
There was a problem hiding this comment.
The reason atomicity exists is to avoid edge cases and unintentional abuse of API. If you have a struct variable and don't make it atomic, people can add all sorts of weird initial conditions/bindings. They might bind the entire struct and then add an initial condition for a field, which is wrong and an edge case we need to catch here that I don't think we do. There can be many such cases that need to be handled in many different places, and we cannot rely on future development work to always account for this.
There was a problem hiding this comment.
I've applied a fix, it utilizes JuliaSymbolics/Symbolics.jl#1997 to construct the literal object. If you could review when you have a chance, that would be amazing!
Verdict: changes needed before merge (reviewed at Adds support for Risk assessment
Findings
Push a fix and the PR is reviewed again automatically at the new head. 🤖 Posted by an AI agent — harness: Claude Code · model: claude-opus-5-5[1m] (fleet master); review by Devin CLI (Mac) / fusion-claude-opus-5-5-high-sidekick-swe-2-medium |
AayushSabharwal
left a comment
There was a problem hiding this comment.
Apart from this, the general discussion about atomic structs is still pending iirc.
Co-authored-by: Aayush Sabharwal <[email protected]>
Co-authored-by: Aayush Sabharwal <[email protected]>
Co-authored-by: Aayush Sabharwal <[email protected]>
AayushSabharwal
left a comment
There was a problem hiding this comment.
This might be a bit annoying 😅 I think I wasn't clear in my communication earlier. We should store records in an atomic manner, and initial_conditions and bindings can thus only be atomic. However, users can specify late initial conditions (ones passed to ODEProblem) partially.
To give an example, consider:
@variables x(t)[1:2]
@mtkcompile sys = System([D(x[1]) ~ t, x[2] ~ 2x[1]], t)Here, x[1] is an unknown but x[2] is an observed. If the user can only specify an initial condition for x as a whole, it is impossible to build a fully determined initialization. So we allow ODEProblem(sys, [x[1] => 1], ...). Technically I think MTK will be fine with
@mtkcompile sys = System([D(x[1]) ~ t, x[2] ~ 2x[1]], t; initial_conditions = [x => [1, nothing]])as well. This is not something I advertise, because the immediate follow up is (among many other edge cases) typically "is this possible for bindings" which it isn't. The reason we enforce atomic storage is because MTK then knows it has exactly one place to look for the initial value of x[i] for any i. Otherwise, when building the operating point we have to reconcile things like the user storing x in initial conditions but providing a partial override in ODEProblem or vice versa (there are more edge cases here too). This leads to a slow mess like https://github.com/SciML/ModelingToolkit.jl/blob/backport-v10/src/systems/problem_utils.jl#L121, which took several iterations to get right and does not scale well to large arrays.
MTK handles this for arrays by getting AtomicArrayDict to ensure its keys are atomic, and then providing the get_possibly_indexed and write_possibly_indexed! helpers to do partial reads/writes. I believe that extending the atomicity validation to structs, and updating these helpers and other similar utilities in that file to handle partial struct inputs should be all we need for this PR. validate_whole_record_keys then is also not necessary.
Thanks for persisting with this. We're almost across the finish line. Please feel free to ping me as frequently as you need.
| """ | ||
| function nearest_present_ancestor(dd::AtomicArrayDict, k::SymbolicT) | ||
| x = k | ||
| while iscall(x) |
There was a problem hiding this comment.
The @match pattern is applicable here too.
| f = operation(leaf) | ||
| args = arguments(leaf) | ||
| base = record_leaf_entry(args[1]::SymbolicT, node, value) | ||
| isconst = SU.isconst(base) | ||
| if f isa Symbolics.SymbolicGetproperty | ||
| isconst || return f(base)::SymbolicT | ||
| return BSImpl.Const{VartypeT}(getproperty(unwrap_const(base), Symbolics.field_name(f)::Symbol)) | ||
| elseif f === getindex | ||
| idxs = unwrap_const.(@view args[2:end]) | ||
| isconst || return base[idxs...]::SymbolicT | ||
| return BSImpl.Const{VartypeT}(unwrap_const(base)[idxs...]) | ||
| end |
There was a problem hiding this comment.
@match can be used here.
AayushSabharwal
left a comment
There was a problem hiding this comment.
Apart from the existing comments and these ones, I think this looks great! Scalarizing in generate_initializesystem_* is not ideal but I also can't think of a cleaner solution right now, so I think it's acceptable.
Co-authored-by: Aayush Sabharwal <[email protected]>
Co-authored-by: Aayush Sabharwal <[email protected]>
Yeah this is fixed with the state selection PR, trying to see if someone can review that so we have a sanity check before merging. |
|
Does that PR not depend on this one? |
|
Typically the way I've done this is to merge the MTKBase changes first, then stateselection/mtktearing, then MTK. So the pieces are released in dependency order with compat bumps. |
|
No, that PR is independent, the dep is only one way. |
AayushSabharwal
left a comment
There was a problem hiding this comment.
Also InterfaceI is failing
Co-authored-by: Aayush Sabharwal <[email protected]>
|
@AayushSabharwal this should be all set now! |
|
Amazing, thank you for persisting through this. One final change: please format 😅 to keep Runic happy. I'll merge immediately after. |
|
Amazing! Thanks for the hard work @Shreyas-Ekanathan! Can we get a patch release @AayushSabharwal? 🥺 |
|
I triggered registration |
More to come 😁 |
Checklist
contributor guidelines, in particular the SciML Style Guide and
COLPRAC.
Additional context
depends on JuliaComputing/StateSelection.jl#153 and JuliaSymbolics/Symbolics.jl#1959