Hey Nestor,
sorry it took so long for me to answer...
Adding custom variables is possible via the argument `extra_functionality` which is passed to the `lopf`. This argument needs to be a function as shown in this example (
https://pypsa.readthedocs.io/en/latest/examples/lopf_with_pyomo_False.html) and is called after the definition of the default optimization problem. I assume you use the setting `pyomo=False` in the lopf (which is faster and more memory-efficient).
Then,
- the function for adding variables is called `pypsa.linopt.define_variables`, have a look at its docstring for further information.
- the function for adding terms to the objective function is called `pypsa.linopt.write_objective` which takes a linear expression as argument created with function `pypsa.linopt.linexpr`. Per default this function adds terms to the existing objective function. If you want to overwrite the whole objective functions, you have to call n.lopf(pyomo=False, skip_objective=True, extra_functionality=extra_functionality) in the beginning.
Note, both things have to be called in the passed extra_functionality.
The optimization framework will in the future change to make things more user-friendly and flexible using the package linopy (
https://github.com/PyPSA/linopy)
Hope that helps!
Best,
Fabian Hofmann