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

Only parameterize and discretize variables when they are called by solution[variable_name] #2242

Open
valentinsulzer opened this issue Aug 17, 2022 · 1 comment
Assignees
Labels
difficulty: hard Will take several weeks

Comments

@valentinsulzer
Copy link
Member

Currently, we only evaluate a variable the first time it is called by by solution[variable_name] (here)

The motivation is that evaluating every single variable is slow and unnecessary if we're not going to evaluate the variables.
We could go further with this and only process (set parameters, discretize) when it is called. Most of the model processing time is spent processing variables, so this would speed up model processing a lot.

This would require storing the ParameterValues and Discretisation objects as attributes of the processed model, which I think @martinjrobins said wasn't ideal, but would it be worth it?

Implementing this should be quite straightforward:

  • remove variable processing (looping over the model.variables dict) in ParameterValues and Discretisation classes
  • store ParameterValues and Discretisation as attributes of the processed model at the end of the process_model function in each of these classes
  • in Solution.update, replace
vars_pybamm = [
       model.variables_and_events[key] for model in self.all_models
]

with something like

vars_pybamm = []
for model in self.all_models:
    var_processed_param = model.parameter_values.process_symbol(model.variables_and_events[key])
    var_processed_disc = model.disc.process_symbol(var_processed_param)
    vars_pybamm.append(var_processed_disc)
@valentinsulzer valentinsulzer added the difficulty: easy A good issue for someone new. Can be done in a few hours label Aug 17, 2022
@valentinsulzer
Copy link
Member Author

Rough timings:
parameterization with variables: .107s
parameterization without variables: .08s

disc with variables: .208s
disc without variables: .09s

In total the script takes .7s, of which 20% is spent processing variables (some of which are then never used!)

@valentinsulzer valentinsulzer self-assigned this Oct 20, 2022
@valentinsulzer valentinsulzer added difficulty: hard Will take several weeks and removed difficulty: easy A good issue for someone new. Can be done in a few hours hacktoberfest labels Oct 20, 2022
@valentinsulzer valentinsulzer added the difficulty: medium Will take a few days label Jul 10, 2023
@valentinsulzer valentinsulzer removed the difficulty: medium Will take a few days label Dec 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
difficulty: hard Will take several weeks
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants