Skip to content
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

Enable sensitivity analysis #372

Draft
wants to merge 78 commits into
base: main
Choose a base branch
from
Draft

Enable sensitivity analysis #372

wants to merge 78 commits into from

Conversation

m-bossart
Copy link
Collaborator

@m-bossart m-bossart commented Mar 29, 2024

My goal is to make the building of a simulation and the solution differentiable. This will enable sensitivity analysis of the output of a dynamic simulation wrt some parameters of the system model. The first commit addresses the first step which is to build out the capability of passing and handling parameters as a flat vector (similar to the states).

  • Pass parameters as a flat vector when constructing the differential equation problem.
  • Simulation flow upgrades
  • Changes for compatibility with automatic differentiation (Zygote)
  • Sensitivity analysis API

src/base/device_wrapper.jl Outdated Show resolved Hide resolved
@m-bossart
Copy link
Collaborator Author

An open question is coming up with a set of rules for determining what is a parameter and what is not? For example, non Float values (ie Flags) are not considered parameters. Some saturation values are also not passed as parameters. The only downside is that those values cannot be used in sensitivity analysis.

@m-bossart m-bossart requested a review from jd-lara March 29, 2024 23:13
src/models/device.jl Outdated Show resolved Hide resolved
src/models/device.jl Outdated Show resolved Hide resolved
Copy link
Contributor

github-actions bot commented Mar 30, 2024

Performance Results

Version Precompile Time
Main 1.934365799
This Branch 36.158343493
Version Execute Time
Main-Build ResidualModel 7.578349628
Main-Execute ResidualModel 15.67486802
Main-Build MassMatrixModel 0.556184431
Main-Execute MassMatrixModel 31.311633191
This Branch-Build ResidualModel 65.349190133
This Branch-Execute ResidualModel 48.205859583
This Branch-Build MassMatrixModel 26.630821793
This Branch-Execute MassMatrixModel 165.58050142

ResidualModel and MassMatrixModel performance results should be compared between versions and not between models due to the execution order of the tests

Copy link

codecov bot commented Mar 30, 2024

Codecov Report

Attention: Patch coverage is 97.75785% with 20 lines in your changes missing coverage. Please review.

Project coverage is 87.13%. Comparing base (6a070cb) to head (80bfa14).
Report is 5 commits behind head on main.

Current head 80bfa14 differs from pull request most recent head beae601

Please upload reports for the commit beae601 to get more accurate results.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #372      +/-   ##
==========================================
- Coverage   87.56%   87.13%   -0.43%     
==========================================
  Files          65       66       +1     
  Lines        9156     8958     -198     
==========================================
- Hits         8017     7806     -211     
- Misses       1139     1152      +13     
Flag Coverage Δ
unittests 87.13% <97.75%> (-0.43%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Coverage Δ
src/PowerSimulationsDynamics.jl 100.00% <ø> (ø)
src/base/branch_wrapper.jl 46.66% <100.00%> (-8.51%) ⬇️
src/base/definitions.jl 100.00% <100.00%> (ø)
src/base/jacobian.jl 86.60% <100.00%> (+0.12%) ⬆️
src/base/nlsolve_wrapper.jl 63.36% <100.00%> (+0.36%) ⬆️
src/base/simulation.jl 94.66% <100.00%> (-0.19%) ⬇️
src/base/simulation_initialization.jl 83.04% <100.00%> (-0.10%) ⬇️
src/base/simulation_inputs.jl 93.88% <100.00%> (+0.71%) ⬆️
...rc/initialization/generator_components/init_avr.jl 95.01% <100.00%> (-1.07%) ⬇️
...nitialization/generator_components/init_machine.jl 98.52% <100.00%> (-0.10%) ⬇️
... and 31 more

... and 2 files with indirect coverage changes

src/base/sensitivity_analysis.jl Outdated Show resolved Hide resolved
src/base/sensitivity_analysis.jl Outdated Show resolved Hide resolved
src/base/simulation_inputs.jl Outdated Show resolved Hide resolved
src/utils/psy_utils.jl Outdated Show resolved Hide resolved
test/test_case_sensitivity.jl Show resolved Hide resolved
for entry in device_param_pairs
if isa(entry, Tuple)
label = join((entry[1], "params", entry[2:end]...), ".")
else
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
else
else

label = join((entry[1], "params", entry[2:end]...), ".")
else
label = entry
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
end
end


#Dummy function that is overloaded in PowerSimulationsDynamicsSurrogates.
#Eventually transition to EnzymeAdjoint() and remove Zygote altogether: https://github.com/SciML/OrdinaryDiffEq.jl/pull/2282
function _non_mutating_initialization_of_ml_surrogates(x0, p_new, sim_inputs)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
function _non_mutating_initialization_of_ml_surrogates(x0, p_new, sim_inputs)
function _non_mutating_initialization_of_ml_surrogates(x0, p_new, sim_inputs)

#Eventually transition to EnzymeAdjoint() and remove Zygote altogether: https://github.com/SciML/OrdinaryDiffEq.jl/pull/2282
function _non_mutating_initialization_of_ml_surrogates(x0, p_new, sim_inputs)
return x0, p_new
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
end
end

#_initialize_powerflow_and_devices!(x0, inputs, sys)
elseif init_level == DEVICES_ONLY
@warn "Reinitializing of most devices not supported with Zygote"
x0, p_new = _non_mutating_initialization_of_ml_surrogates(x0, p_new, sim_inputs)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
x0, p_new = _non_mutating_initialization_of_ml_surrogates(x0, p_new, sim_inputs)
x0, p_new =
_non_mutating_initialization_of_ml_surrogates(x0, p_new, sim_inputs)

p_state,
)
#GET GROUND TRUTH DATA
execute!(sim, MethodOfSteps(Rodas5()); abstol = 1e-6, reltol = 1e-6, dtmax = 0.05, saveat = 0.05)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
execute!(sim, MethodOfSteps(Rodas5()); abstol = 1e-6, reltol = 1e-6, dtmax = 0.05, saveat = 0.05)
execute!(
sim,
MethodOfSteps(Rodas5());
abstol = 1e-6,
reltol = 1e-6,
dtmax = 0.05,
saveat = 0.05,
)

sim,
[("generator-102-1", :Shaft, :H)],
[("generator-102-1", :δ)],
MethodOfSteps(Rodas5(autodiff=false)),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
MethodOfSteps(Rodas5(autodiff=false)),
MethodOfSteps(Rodas5(; autodiff = false)),

f_zygote_forward([3.15], [p_state], δ_gt, [])
#@test f_grad(p, [p_state], δ_gt) ==
# Zygote.gradient(p -> f_zygote_forward(p, [p_state], δ_gt), p)[1]
@test Zygote.gradient(p -> f_zygote_forward(p, [p_state], δ_gt, []), p)[1][1] == -10.336102683050685
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
@test Zygote.gradient(p -> f_zygote_forward(p, [p_state], δ_gt, []), p)[1][1] == -10.336102683050685
@test Zygote.gradient(p -> f_zygote_forward(p, [p_state], δ_gt, []), p)[1][1] ==
-10.336102683050685

sim,
[("generator-102-1", :Shaft, :H)],
[("generator-102-1", :δ)],
MethodOfSteps(Rodas5(autodiff=false)),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
MethodOfSteps(Rodas5(autodiff=false)),
MethodOfSteps(Rodas5(; autodiff = false)),

saveat = 0.05,
)


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change

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