Skip to content

Symbolic indexing rework - #1988

Merged
ChrisRackauckas merged 8 commits into
SciML:masterfrom
AayushSabharwal:syms_rework
Dec 12, 2022
Merged

ChrisRackauckas merged 8 commits into
SciML:masterfrom
AayushSabharwal:syms_rework

Conversation

@AayushSabharwal

Copy link
Copy Markdown
Member

Is this interface appropriate?

@ChrisRackauckas

Copy link
Copy Markdown
Member

Those look good. Is that all that's required for the full interface?

@AayushSabharwal

Copy link
Copy Markdown
Member Author

I just realised this is a bit of a circular dependency problem. SciMLBase can't use these functions, because MTK depends on it and not the other way around.

@ChrisRackauckas

Copy link
Copy Markdown
Member

Yes, any interface functions must be defined in SciMLBase and extended downstream.

@AayushSabharwal

Copy link
Copy Markdown
Member Author

Do I move AbstractSystem to SciMLBase too? Or just let the functions be Any-typed

@ChrisRackauckas

Copy link
Copy Markdown
Member

Define stub functions and use them in the interface, and they get extended downstream.

@AayushSabharwal

Copy link
Copy Markdown
Member Author

With some changes (which I'm cleaning up) symbolic indexing of solutions works. However, DiffEqArrays generated by sol(0.0:1.0:10.0) are not symbolically indexable since they (currently) require all the syms/paramsyms/observed to be passed to them.

@ChrisRackauckas

Copy link
Copy Markdown
Member

Then we should define the interface in RecursiveArrayTools as something about DiffEqArrays and AbstractVectorOfArray, and bubble it up.

@AayushSabharwal

AayushSabharwal commented Dec 6, 2022 •

Copy link
Copy Markdown
Member Author

The solutions I've come up with so far:

  • DiffEqArray just takes a scimlfunction instead of syms/observed/etc. This ends up giving it a lot more data than it needs, and is a sort of weak circular dependency so it doesn't seem like the best idea
  • DiffEqArray has another field for sys, and uses it if it isn't nothing, basically like how scimlfunctions work.
  • RecursiveArrayTools defines a struct SymbolCache (name off the top of my head) containing syms, indepsym and paramsyms which implements the interface in this PR. MTK implements this interface for systems. DiffEqArray now takes anything that implements this interface for symbolic indexing, instead of syms and indepsym. Side effect: DiffEqArrays also allow indexing parameters. I also get the feeling the DiffEqArray indexing code and solution indexing code would be near identical

@AayushSabharwal

AayushSabharwal commented Dec 6, 2022 •

Copy link
Copy Markdown
Member Author

DiffEqArray doesn't seem to support indexing with multiple symbols. The following doesn't work (example uses literal symbols instead of symbolic variables for convenience):

julia> dx = DiffEqArray([[f(x) f2(x)] for x in t], t, [:a,:b])
julia> dx[[:a,:b]]
ERROR: DimensionMismatch: arrays could not be broadcast to a common size; got a dimension with lengths 11 and 2
...

Is this intentional?

@AayushSabharwal

AayushSabharwal commented Dec 8, 2022 •

Copy link
Copy Markdown
Member Author

I've messed about with this a lot (going the SymbolCache route) and it seems independent_variables at the very least needs to be hoisted to RecursiveArrayTools since getindepsym won't work here. At this point, we might as well hoist states and parameters as well.

@ChrisRackauckas

Copy link
Copy Markdown
Member

At that point it's a whole package. SymbolicIndexingInterface.jl? Document it well enough and it can be a thing.

@AayushSabharwal

Copy link
Copy Markdown
Member Author

That sounds like a good idea. It would be pretty small to start: states, parameters, independent_variables, the new interface described here, and maybe SymbolCache. But I have a feeling we'll end up chucking a lot more in there eventually. Should I just make this work for now (hoisting to RecursiveArrayTools) and refactor into the new package later, or just create the repo now and see where we get.

@ChrisRackauckas

Copy link
Copy Markdown
Member

Make it work, get it tested, and then in a last step refactor to a new package and we move it to SciML to register.

- Don't pass syms to `SciMLFunction`s
- Import, reexport and overload interface methods from
  RecursiveArrayTools
- FIXME: Currently unrelated `states`/`parameters` are also overloaded
@AayushSabharwal

Copy link
Copy Markdown
Member Author

SciMLBase tests pass if the following is done:

@ChrisRackauckas

Copy link
Copy Markdown
Member

Alright cool, seems like a good time to make that extra package then.

@AayushSabharwal

AayushSabharwal commented Dec 8, 2022 •

Copy link
Copy Markdown
Member Author

Everything that needs to be in the package is in src/symbolic_indexing_interface.jl in RecursiveArrayTools. Should be simple enough to move it over. Should I make the package and then transfer it over to SciML?

@AayushSabharwal

Copy link
Copy Markdown
Member Author

@AayushSabharwal

Copy link
Copy Markdown
Member Author

Initiated the transfer. Github says it might take a couple minutes

@AayushSabharwal

Copy link
Copy Markdown
Member Author

Should I tag a release for SymbolicIndexingInterface, so the other packages can depend on it?

@ChrisRackauckas

Copy link
Copy Markdown
Member

I set it to go.

- SymbolicIndexingInterface.jl is registered, and contains all the
  interface methods previously defined in RecursiveArrayTools
- SymbolicIndexingInterface is imported and not `using` so unexported
  `states` and `parameters` functions shouldn't need to unnecessarily
  overload the interface
@AayushSabharwal

AayushSabharwal commented Dec 11, 2022 •

Copy link
Copy Markdown
Member Author

This should work. For some reason I can't install SymbolicIndexingInterface locally yet. It won't work in CI, however, since it doesn't know to depend on the specific SciMLBase and RecursiveArrayTools branches without Manifest.toml

@AayushSabharwal

Copy link
Copy Markdown
Member Author

Can I rename the other states functions (which aren't intended to implement the interface) to _states (such as the one in src/systems/connectors.jl:164)? Or do I just overload it and ignore the fact that it's not the intended use of the function? Fairly certain we need one or the other, since the interface states method is used a lot and adding SymbolicIndexingInterface. to all those places seems a bit redundant. It's also why precompilation will fail.

@ChrisRackauckas

Copy link
Copy Markdown
Member

Can I rename the other states functions (which aren't intended to implement the interface) to _states (such as the one in src/systems/connectors.jl:164)?

yes

@AayushSabharwal

AayushSabharwal commented Dec 12, 2022 •

Copy link
Copy Markdown
Member Author

Is there a case where states(sys) has two variables, but size(sol, 1) == 1? It's happening for test/components.jl:100. states(sys2) == [capacitor.v, capacitor.i] but size(sol2) == (1, 15)

EDIT: Same thing in test/structural_transformation/tearing.jl:179

@AayushSabharwal

Copy link
Copy Markdown
Member Author

Everything should work now, bar the tests mentioned above, if the relevant branches of the other repositories are used. I haven't been able to run the distributed tests since Distributed tends to segfault on my system for reasons I haven't figured out yet.

@AayushSabharwal
AayushSabharwal marked this pull request as ready for review December 12, 2022 07:53
@AayushSabharwal

Copy link
Copy Markdown
Member Author

Should this PR still pass syms/indepsym/paramsyms to SciMLFunctions? It doesn't currently, but I realised it probably should so it can be merged without SciMLBase, then after SciMLBase gets merged they can be removed in a separate PR to make 100% sure everything works.

@ChrisRackauckas

Copy link
Copy Markdown
Member

Yes, and then do an upper bound in the second PR. That way it's all done safely.

@AayushSabharwal

AayushSabharwal commented Dec 12, 2022 •

Copy link
Copy Markdown
Member Author

Done. Now SciMLBase can be updated to 1.80 without MTK trying to use it

EDIT: Wait, I got it wrong. There's no need to restrict this since SciMLBase should support the old method anyway

@AayushSabharwal

Copy link
Copy Markdown
Member Author

Once this merges, SciMLBase can depend on it, so those changes can be tested and then merged

@ChrisRackauckas
ChrisRackauckas merged commit e1d27d7 into SciML:master Dec 12, 2022
@AayushSabharwal
AayushSabharwal deleted the syms_rework branch February 29, 2024 12:12
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.

2 participants