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

Use casadi map to more quickly evaluate processed variables #2456

Closed
valentinsulzer opened this issue Nov 14, 2022 · 7 comments
Closed

Use casadi map to more quickly evaluate processed variables #2456

valentinsulzer opened this issue Nov 14, 2022 · 7 comments
Labels
difficulty: medium Will take a few days optimization priority: medium To be resolved if time allows

Comments

@valentinsulzer
Copy link
Member

https://web.casadi.org/docs/#map

@valentinsulzer valentinsulzer added the difficulty: medium Will take a few days label Nov 20, 2022
@jaskiratsingh2000
Copy link

Hi @tinosulzer ! Does the casadi map has to be used in the place of "base_variable_casadis" because, I feel that that's what getting processed.

@valentinsulzer
Copy link
Member Author

instead of this for loop

for ts, ys, inputs, base_var_casadi in zip(
self.all_ts, self.all_ys, self.all_inputs_casadi, self.base_variables_casadi
):
for inner_idx, t in enumerate(ts):
t = ts[inner_idx]
y = ys[:, inner_idx]
entries[idx] = float(base_var_casadi(t, y, inputs))
idx += 1

@jaskiratsingh2000
Copy link

Hi @tinosulzer Do you thinnk that something this should work for us?

for ts, ys, inputs, base_var_casadi in zip(
        self.all_ts, self.all_ys, self.all_inputs_casadi, self.base_variables_casadi
):
    num_times = len(ts)
    num_y_vals = ys.shape[0]
    num_entries = num_times * num_y_vals

    # Create a function to evaluate the base_var_casadi over a list of inputs
    def evaluate_base_var_casadi(t, y, inputs):
        return float(base_var_casadi(t, y, inputs))

    # Create casadi functions for the inputs and the base_var_casadi
    input_func = casadi.Function('inputs', [self.input_time] + self.input_vars, ['input'], ['cdense'])
    base_var_func = casadi.Function('base_var_casadi', [self.input_time, self.input_y] + input_func(self.input_time), ['output'], ['cdense'])

    # Evaluate the inputs
    input_vals = [np.array([t] + list(y)) for t, y in zip(ts, ys.T)]
    inputs_casadi = input_func(casadi.horzcat(*input_vals)).full().T

    # Evaluate the processed variables using casadi.map
    entries = casadi.map(evaluate_base_var_casadi, ts, ys.T, inputs_casadi).full().ravel()

    # Set the entries for this output variable
    self.entries[idx:idx + num_entries] = entries

    idx += num_entries

@jaskiratsingh2000
Copy link

@tinosulzer Any updates on above what I mentioned?

@valentinsulzer
Copy link
Member Author

I'm not sure exactly the right way to do it since it needs to be optimized to minimize the calculation time, which needs to be tested locally. This is not a good issue to start with. Please check issues labelled "easy"

@jaskiratsingh2000
Copy link

Hi @tinosulzer Thanks for letting me know. I'm gently trying to understand the deep of PyBaMM that's why I thought to work on such issues along side issues with labelled "easy"

@rtimms rtimms added the priority: medium To be resolved if time allows label May 15, 2023
@rtimms
Copy link
Contributor

rtimms commented Nov 22, 2024

closing, see #4464

@rtimms rtimms closed this as completed Nov 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
difficulty: medium Will take a few days optimization priority: medium To be resolved if time allows
Projects
None yet
Development

No branches or pull requests

3 participants