-
-
Notifications
You must be signed in to change notification settings - Fork 553
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
[Bug]: IDAKLUSolver
with output_variables
provides accesss to alternative variables
#3790
Comments
IDAKLUSolver
with output_variables
provides access alternative variablesIDAKLUSolver
with output_variables
provides accesss to alternative variables
@BradyPlanden From a quick glance I don't think that the solver is returning the full state matrix - this is not returned to python from IDAKLU when This does leave open a design decision though: 1) strictly only allow access to those variables that were in |
@BradyPlanden I've taken a closer look at this and it is the model variables, or those that do not require access to the state vector, that are being returned. Below in a code snippet that demonstrates this by checking each variable in a full model against a reduced (minimal) model. None of the variables that return in the reduced model reference the state vector (just by identifying any instances of " import pybamm
import numpy as np
input_parameters = {} # Sensitivities dictionary
t_eval = np.linspace(0, 3600, 100)
# construct model
def construct_model():
model = pybamm.lithium_ion.DFN()
geometry = model.default_geometry
param = model.default_parameter_values
param.update({key: "[input]" for key in input_parameters})
param.process_model(model)
param.process_geometry(geometry)
var_pts = {"x_n": 50, "x_s": 50, "x_p": 50, "r_n": 5, "r_p": 5}
mesh = pybamm.Mesh(geometry, model.default_submesh_types, var_pts)
disc = pybamm.Discretisation(mesh, model.default_spatial_methods)
disc.process_model(model)
return model
options = {
'linear_solver': 'SUNLinSol_KLU',
'jacobian': 'sparse',
'num_threads': 4,
}
# Solve for all variables
model_all = construct_model()
solver_all = pybamm.IDAKLUSolver(
atol=1e-8, rtol=1e-8,
options=options,
)
sol_all = solver_all.solve(
model_all,
t_eval,
inputs=input_parameters,
)
# Solve for a subset of variables
output_variables = [
"Current [A]",
]
model = construct_model()
solver = pybamm.IDAKLUSolver(
atol=1e-8, rtol=1e-8,
options=options,
output_variables=output_variables,
)
sol = solver.solve(
model,
t_eval,
inputs=input_parameters,
)
found = 0
assert_failed = 0
for var in model.variables_and_events:
try:
sol[var].data
if not np.allclose(sol[var].data, sol_all[var].data):
print(f"ASSERT FAILED '{var}'")
assert_failed += 1
continue
if "y[" in model.variables[var].__str__():
print("===> eqn does reference state vector")
assert_failed += 1
continue
print(f" '{var}'")
found += 1
except RuntimeError:
if "Event:" not in var and "y[" not in model.variables[var].__str__():
print("===> eqn does not reference state vector")
assert_failed += 1
print(f"NOT FOUND '{var}'")
print(f"Found {found} and failed to find {len(model.variables_and_events) - found} of "
f"{len(model.variables_and_events)} variables.")
print(f"Assert failed {assert_failed} times.") which returns (excerpt)...
I will add some checks to return a more sensible error message when an uncomputed (and unavailable) variable is requested (currently casadi throws a runtime error) and adjust the unit tests to cover these scenarios. I also suspect that there is some cross-contamination of the model instances in the unit tests (between |
PyBaMM Version
[Develop]
Python Version
3.11
Describe the bug
Using the
output_variable
functionality with theIDAKLUSolver
appears to provide access to the full state matrix. It is currently possible to access variables not defined within theoutput_variables
dictionary as shown below.Steps to Reproduce
Relevant log output
No response
The text was updated successfully, but these errors were encountered: