Merge pull request #51 from fneum/solve_network
Add `extra_functionality` and `extra_postprocessing` to solve_network()
This commit is contained in:
commit
bf12d25879
@ -229,7 +229,10 @@ def fix_branches(n, lines_s_nom=None, links_p_nom=None):
|
||||
if isinstance(n.opt, pypsa.opf.PersistentSolver):
|
||||
n.opt.update_var(n.model.link_p_nom)
|
||||
|
||||
def solve_network(n, config=None, solver_log=None, opts=None, callback=None):
|
||||
def solve_network(n, config=None, solver_log=None, opts=None, callback=None,
|
||||
skip_iterating=False,
|
||||
extra_functionality=None, extra_functionality_args=None,
|
||||
extra_postprocessing=None):
|
||||
if config is None:
|
||||
config = snakemake.config['solving']
|
||||
solve_opts = config['options']
|
||||
@ -239,7 +242,9 @@ def solve_network(n, config=None, solver_log=None, opts=None, callback=None):
|
||||
solver_log = snakemake.log.solver
|
||||
solver_name = solver_options.pop('name')
|
||||
|
||||
def extra_postprocessing(n, snapshots, duals):
|
||||
if extra_postprocessing is None:
|
||||
|
||||
def get_line_limit_duals(n, snapshots, duals):
|
||||
if hasattr(n, 'line_volume_limit') and hasattr(n.model, 'line_volume_constraint'):
|
||||
cdata = pd.Series(list(n.model.line_volume_constraint.values()),
|
||||
index=list(n.model.line_volume_constraint.keys()))
|
||||
@ -250,6 +255,8 @@ def solve_network(n, config=None, solver_log=None, opts=None, callback=None):
|
||||
index=list(n.model.line_cost_constraint.keys()))
|
||||
n.line_cost_limit_dual = -cdata.map(duals).sum()
|
||||
|
||||
extra_postprocessing = get_line_limit_duals
|
||||
|
||||
def run_lopf(n, allow_warning_status=False, fix_ext_lines=False):
|
||||
free_output_series_dataframes(n)
|
||||
|
||||
@ -261,6 +268,9 @@ def solve_network(n, config=None, solver_log=None, opts=None, callback=None):
|
||||
add_lv_constraint(n)
|
||||
add_lc_constraint(n)
|
||||
|
||||
if extra_functionality is not None:
|
||||
extra_functionality(n, n.snapshots, *extra_functionality_args)
|
||||
|
||||
pypsa.opf.network_lopf_prepare_solver(n, solver_name=solver_name)
|
||||
|
||||
if fix_ext_lines:
|
||||
@ -287,6 +297,7 @@ def solve_network(n, config=None, solver_log=None, opts=None, callback=None):
|
||||
|
||||
return status, termination_condition
|
||||
|
||||
if not skip_iterating:
|
||||
iteration = 0
|
||||
lines_ext_b = n.lines.s_nom_extendable
|
||||
if lines_ext_b.any():
|
||||
@ -351,6 +362,8 @@ def solve_network(n, config=None, solver_log=None, opts=None, callback=None):
|
||||
|
||||
iteration += 1
|
||||
status, termination_condition = run_lopf(n, fix_ext_lines=True)
|
||||
else:
|
||||
status, termination_condition = run_lopf(n, fix_ext_lines=False)
|
||||
if callback is not None: callback(n, iteration, status)
|
||||
|
||||
return n
|
||||
|
Loading…
Reference in New Issue
Block a user