-
Notifications
You must be signed in to change notification settings - Fork 0
Add add_device_terms_to_expression! driver for device-injection methods #112
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
+61
−0
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not convinced passing a function here is a clean option. This function can't never compile and cause invalidations. This implementation is very pythonic
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, there's definitely some code smell. I agree that this solution feels clunky. I didn't check the compilation consequences of having a function stub in a hotloop.
I can remove the function closures and eliminate
add_device_terms_to_expression!here in IOM. However, I'd like to keep the target resolution code in the POM PR. Concretely: in the variable case, we'd havewhereas right now on
main, the network model specifics inside_balance_expression_targetsare copy-pasted (ref-bus lookup forCopperPlate, area lookup forAreaBalance, etc.) and_apply_term_to_targets!is 1 (non-PTDF) or 2 (PTDF) calls toadd_proportional_to_jump_expression!Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wait. I saw
_apply_term_to_targets!(::Tuple{}, ::_BalanceTermValue, ::Float64, ::Int) = nothingand thought I had a stub function, but no, this is tail recursion. I'm convinced there is no actual compilation or type stability issue here.Full reasoning
For a single call site (one concrete `T` , `network_model`, term source):targets_fn::F, term_fn::G— the where{F<:Function, G<:Function}forces Julia to specialize the driver per closure type.F/Gare concrete inside any given specialization, not abstractFunction. No dynamic dispatch to call them.targets = targets_fn(d)— dispatches to exactly one_balance_expression_targetsmethod (Tandnetwork_modelare concrete), so the tuple structure (1- vs 2-element, and each element's index type) is fixed and concrete._apply_term_to_targets!recursion — operates on a concrete 1-or-2 tuple;Base.tailshortens it by one each step, terminating at the::Tuple{}method. This fully unrolls at compile time and handles the heterogeneous 2-tuple (system-keyed + bus-keyed) correctly.scale::Sas a positional type parameter —isnothing(scale)folds to a compile-time constant, so the dead branch is pruned. No instability, noscale(d)dynamic call.name,multiplier,variable,refsare each assigned exactly once before the innert -> …is built. No reassignment of a captured variable → noCore.Box, no boxing instability.Two closures branch and return different inner closures,
t -> ...lambdas with distinct types. That's a little messy:term = term_fn(d)infers asUnion{Closure1, Closure2}. Thanks to union-splitting, though, the performance cost should be next to zero.I can go run some profiling to confirm. If the code smell is too much, I'm on board with letting this go and sticking with what I suggested above...but I'm pretty confident that compilation/invalidations here aren't an issue.