if __name__ == "__main__": n = pypsa.Network(snakemake.input[0]) assert_operational_problem(n) solver_options = snakemake.config["solver"] sclopf_kwargs = { "pyomo": False, "branch_outages": get_branch_outages(n), "solver_name": solver_options.pop("name"), "solver_options": solver_options, "formulation": "kirchhoff", } with memory_logger(filename=snakemake.log.memory) as mem, timer( "solving time") as tim: group_size = snakemake.config["group_size"] for i in range(int(len(n.snapshots) / group_size)): snapshots = n.snapshots[group_size * i:group_size * i + group_size] n.sclopf(snapshots=snapshots, **sclopf_kwargs) pd.Series({ "time [sec]": tim.usec / 1e6, "memory [MB]": mem.mem_usage[0] }).to_csv(snakemake.log.stats) n.export_to_netcdf(snakemake.output[0])
if __name__ == "__main__": if 'snakemake' not in globals(): from _helpers import mock_snakemake snakemake = mock_snakemake('solve_network', network='elec', simpl='', clusters='5', ll='copt', opts='Co2L-BAU-CCL-24H') configure_logging(snakemake) tmpdir = snakemake.config['solving'].get('tmpdir') if tmpdir is not None: Path(tmpdir).mkdir(parents=True, exist_ok=True) opts = snakemake.wildcards.opts.split('-') solve_opts = snakemake.config['solving']['options'] fn = getattr(snakemake.log, 'memory', None) with memory_logger(filename=fn, interval=30.) as mem: n = pypsa.Network(snakemake.input[0]) n = prepare_network(n, solve_opts) n = solve_network(n, snakemake.config, opts, solver_dir=tmpdir, solver_logfile=snakemake.log.solver) n.export_to_netcdf(snakemake.output[0]) logger.info("Maximum memory usage: {}".format(mem.mem_usage))
extra_functionality=extra_functionality, **kwargs) return n if __name__ == "__main__": logging.basicConfig(filename=snakemake.log.python, level=snakemake.config["logging_level"]) opts = [ o for o in snakemake.wildcards.opts.split("-") if not re.match(r"^\d+h$", o, re.IGNORECASE) ] with memory_logger(filename=getattr(snakemake.log, "memory", None), interval=30.0) as mem: n = pypsa.Network(snakemake.input[0]) n = prepare_network(n, solve_opts=snakemake.config["solving"]["options"]) # # catch and tag numerical issues # try: # n.numerical_issue = 0 # except: # n.numerical_issue = 1 n = solve_network( n, config=snakemake.config, solver_log=snakemake.log.solver,
return n if __name__ == "__main__": # Detect running outside of snakemake and mock snakemake for testing if 'snakemake' not in globals(): from vresutils.snakemake import MockSnakemake, Dict snakemake = MockSnakemake( wildcards=dict(network='elec', simpl='', clusters='45', lv='1.0', opts='Co2L-3H'), input=["networks/{network}_s{simpl}_{clusters}_lv{lv}_{opts}.nc"], output=["results/networks/s{simpl}_{clusters}_lv{lv}_{opts}.nc"], log=dict(solver="logs/{network}_s{simpl}_{clusters}_lv{lv}_{opts}_solver.log", python="logs/{network}_s{simpl}_{clusters}_lv{lv}_{opts}_python.log") ) tmpdir = snakemake.config['solving'].get('tmpdir') if tmpdir is not None: patch_pyomo_tmpdir(tmpdir) logging.basicConfig(filename=snakemake.log.python, level=snakemake.config['logging_level']) with memory_logger(filename=getattr(snakemake.log, 'memory', None), interval=30.) as mem: n = pypsa.Network(snakemake.input[0]) n = prepare_network(n) n = solve_network(n) n.export_to_netcdf(snakemake.output[0]) logger.info("Maximum memory usage: {}".format(mem.mem_usage))