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

Pass model to solve as an alternative to sim_func #136

Open
valentinsulzer opened this issue Feb 9, 2022 · 3 comments
Open

Pass model to solve as an alternative to sim_func #136

valentinsulzer opened this issue Feb 9, 2022 · 3 comments
Labels
enhancement New feature or request

Comments

@valentinsulzer
Copy link
Member

Description

Add a model keyword argument to lp.solve where a user can pass in a pybamm model (instead of via sim_func). This is slightly easier than having to write an entire sim_func function, if all you want to change is the model

Motivation

Pro: easier to change the model and leave everything else the same
Con: more ways of doing the same thing

Possible Implementation

lp.solve(
    netlist=netlist,
    parameter_values=param,
    experiment=experiment,
    initial_soc=initial_soc,
    model=model,
)

Additional context

No response

@TomTranter
Copy link
Collaborator

The reason I have set it up as a sim_func is that the function gets run on every sub-process which is less expensive and memory intensive than passing objects to each sub-process. I agree it's not as easy as passing a model and probably for small problems would work fine but for large scale problems I was getting a lot of issues making pybamm objects and passing them around.

@valentinsulzer
Copy link
Member Author

I was thinking that if the user passes a model instead of a sim_func it would automatically create a basic sim_func using that model. Something like

def get_basic_simulation_function(model=None):
    # Create the pybamm model
    if model is None:
        model = pybamm.lithium_ion.SPM()
        
    def basic_simulation(parameter_values=None):
        """
        Create a Basic PyBaMM simulation set up for integration with liionpack

        Args:
            parameter_values (pybamm.ParameterValues):
                The default is None.

        Returns:
            pybamm.Simulation:
                A simulation that can be solved individually or passed into the
                liionpack solve method

        """
        # Add events to the model
        model = lp.add_events_to_model(model)

        # Set up parameter values
        if parameter_values is None:
            param = pybamm.pybamm.ParameterValues("Chen2020")
        else:
            param = parameter_values.copy()

        # Set up solver and simulation
        solver = pybamm.CasadiSolver(mode="safe")
        sim = pybamm.Simulation(
            model=model,
            parameter_values=param,
            solver=solver,
        )
        return sim

    return basic_simulation

@TomTranter
Copy link
Collaborator

@tinosulzer we can set it up and see what the speed is like for a large problem, maybe we could actually get away with passing an unbuilt simulation

@TomTranter TomTranter added the enhancement New feature or request label May 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants