Replies: 2 comments
-
Hi! We have now moved our discussions to Discourse. Please post your question there. |
Beta Was this translation helpful? Give feedback.
0 replies
-
Hey @MoritzB92 , let's chat about this on Discourse but in general:
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
General Idea
I would like to call PyBaMM from MATLAB and advance the simulation by one time step in each function call using the
step()
method (sim.step()
). During this process, I want to adapt certain input values such as current, temperature, and time step, and return some values such as voltage in each time step to MATLAB. The overall purpose is to use PyBaMM for a control application in an existing MATLAB/Simulink environment.To ensure that the polarization remains intact between function calls, I also want to return the entire solution object to MATLAB and then pass it back to PyBaMM in the next function call. This will allow me to use the solution object for re-initialization of the model, and the solution for the next time step will be appended to the existing solution object.
Issues
I have attempted this setup in two different versions (Version 1 and Version 2), as shown in the attached example, but I face two overall issues:
Issue 1: Handling of intervals from stepping the solver resulting in double entries for all time steps
[0, 1, 1, 2, 2, 3, 3, 4, 4, 5]
[0, 1, 2, 3, 4, 5]
When calling the same functions (from MATLAB or PyBaMM), appending the interval from the last time step results in double entries for Version 1 and single entries (as desired) for Version 2. How can I avoid this for Version 1 when calling the PyBaMM function from MATLAB? Why is the handling of appending the new values from one step to the solution object different between MATLAB and Python when looping through the same function?
Issue 2: Access Solution Variables
The second and more important issue is accessing some variables from the solution object after a single overall time step or after all time steps have been performed.
It seems that when returning the solution object to MATLAB and passing it back to Python, the functionality to create output variables just in time, such as
solution["Voltage [V].entries"]
, is compromised.In my example, I export the final solution object to a pickle file and read it in another Python file to allow me to access it in Python and debug it (Step2_reload_solution.py). This is just a workaround to access the solution object.
My question is: What happens to the solution object when it's passed between MATLAB and Python, and how does this functionality work in general? When debugging the solution object, I get a traceback error for the relevant variables in the solution object.
Structure of the Example
To recreate the problem, run Step1_main_sim_pybamm.m, which calls the MATLAB function run_pybamm_step_V1.m for Version 1 and run_pybamm_step_V2.py for Version 2. Both versions call the run_single_time_step_simulation function in pybamm_model.py. After looping through a few steps, the solution objects are saved to pickle files, which are then imported in Step2_reload_solution.py for post-processing.
Alternative Approaches
Besides fixing the issues in the examples, I am wondering about alternative approaches to the problem. One obvious choice would be to create all relevant variables within the PyBaMM function and return them directly to MATLAB, then appending the solution array for each variable in each time step. While I might do that in some way, I still would like to understand how I can properly serialize the solution object for any further use.
Do you have any other ideas, such as keeping the PyBaMM simulation running while adapting the input variables or using closures in python? Any help on the example above, as well as alternative approaches, would be much appreciated!
Matlab_pybamm_integration_test.zip
Beta Was this translation helpful? Give feedback.
All reactions