PyPSA icon indicating copy to clipboard operation
PyPSA copied to clipboard

save extra_functionality in pypsa network

Open lisazeyen opened this issue 3 years ago • 4 comments

saving the extra functionality in the pypsa network (maybe as a string?)

Describe the feature you'd like to see

Please give a clear and concise description and provide context why the feature would be useful. Also, we'd appreciate any implementation ideas and references you already have.

lisazeyen avatar Dec 09 '21 10:12 lisazeyen

The idea is nice, but storing the extra functionality itself as a string is a bit tricky -> consider the case when the code syntax changes a bit, then the function strings do not work anymore. Debugging this could be very annoying.

I'd rather propose that with the linopy integration we store the solving model itself together with the network in a nc file. It would 'only' require adding the model netcdf writing into nc importer/exporter of pypsa (with a optional flag of course). It might bloat the nc file a bit, but it would be very clean. What do you think?

FabianHofmann avatar Dec 14 '21 12:12 FabianHofmann

That sound nice! Could become large, but I wouldn't mind if it brings the convenience and is optional.

fneum avatar Dec 15 '21 10:12 fneum

Just thought about it again. Sketch of the alternative solution:

n.extra_functionality = """
    def extra_functionality(n, sns):
        discharger_bool = n.links.index.str.contains("battery discharger")
        charger_bool = n.links.index.str.contains("battery charger")
        dischargers_ext= n.links[discharger_bool].query("p_nom_extendable").index
        chargers_ext= n.links[charger_bool].query("p_nom_extendable").index
        eff = n.links.efficiency[dischargers_ext].values
        lhs = n.model["Link-p_nom"].loc[chargers_ext] - n.model["Link-p_nom"].loc[dischargers_ext] * eff
        n.model.add_constraints(lhs == 0, name="Link-charger_ratio")
    """

# somewhere in n.lopf / n.optimize
if hasattr(n, "extra_functionality"):
    exec(n.extra_functionality)

fneum avatar Mar 05 '23 18:03 fneum

I think in any case it makes sense to store the code of the extra_functionality -- even if it is not re-executed -- for model documentation.

fneum avatar Mar 05 '23 18:03 fneum