Skip to content

Commit

Permalink
Merge pull request #27 from tZ3ma/nox_poc
Browse files Browse the repository at this point in the history
Streamline Tessif
  • Loading branch information
mathias-ammon authored Feb 14, 2023
2 parents e8299db + 70fa2c1 commit 24b1372
Show file tree
Hide file tree
Showing 24 changed files with 4,294 additions and 6,041 deletions.
2 changes: 1 addition & 1 deletion .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
[flake8]
application-import-names = tessif,tests,noxfile
# B,B9 = Bugbear; BLK= black warning; C = mccabe complexity; D = Docstring; F = pyflake errors; W,E = pycodestyle; I = import order; S=Securtiy
select = B,B9,B950,BLK,C,D,DAR,E,F,I,N,RST,S,W
select = B,B9,B950,BLK,C,D,DAR,E,F,N,RST,S,W
docstring-convention = numpy
# ignore incompatible black warnings:
# E203 (Whitespace before ‘:')
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ repos:
args: [--py310-plus]
- id: isort
name: Sort python imports using isort through poetry.
entry: poetry run isort
entry: poetry run isort --profile black
language: system
types: [python]
args: [-v]
Expand Down
349 changes: 99 additions & 250 deletions poetry.lock

Large diffs are not rendered by default.

21 changes: 9 additions & 12 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "tessif"
version = "0.0.23"
version = "0.0.24"
description = "Transforming Energy Supply System modell*I*ng Framework"
authors = ["Mathias Ammon <mathias.ammon@tuhh.de>"]
license = "MIT"
Expand All @@ -11,12 +11,11 @@ keywords = ["energy technology", "energy supply systems"]

[tool.poetry.dependencies]
python = "^3.8"
requests = "^2.27.1"
nox-poetry = "^1.0.2"
click = "^8.1.3"
tessif-examples = "^0.2.2"
matplotlib = "^3.6.3"
click = "^8"
matplotlib = "^3"
strutils = "^0.1.0"
networkx = "^3"
dcttools = "^0.1.3"

[tool.poetry.dev-dependencies]
pytest = "^7.1.2"
Expand All @@ -37,16 +36,14 @@ pyupgrade = "^2.32.1"
sphinx-paramlinks = "^0.5.2"
pylint = "^2.14.5"
nox = "^2022.8.7"

[tool.poetry.group.oemof-0-4-4.dependencies]
oemof-solph = "0.4.4"
tessif-oemof-4-4 = ">=0.1.5"
click = "^8.1.3"
strutils = "^0.1.0"
nox-poetry = "^1.0.2"

[tool.poetry.scripts]
tessif = "tessif.cli:main_cli_entry"

[tool.isort]
profile = "black"

[tool.coverage.paths]
source = ["src", "*/site-packages"]

Expand Down
105 changes: 67 additions & 38 deletions src/tessif/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@
"""Module providing the Tessif Command Line Interface (CLI)."""
import importlib
import os
import venv
import pickle
import subprocess
import venv

import click

PLUGIN = "tessif-oemof-4-4"
import tessif.logging as tessif_logging # nopep8
from tessif.frused.paths import tessif_dir

tessif_dir = os.path.join(os.path.expanduser("~"), ".tessif.d")
venv_dir = os.path.join(tessif_dir, "plugin-venvs", PLUGIN)
python_bin = os.path.join(venv_dir, "bin", "python")
logger = tessif_logging.logger


@click.group()
Expand All @@ -20,21 +21,42 @@ def main_cli_entry():


@main_cli_entry.command()
def greet():
"""Greet the world."""
click.echo("Hello World!")
@click.option(
"-d",
"--tessif_directory",
help="(Optional) Venv directory the plugin will be installed into.",
default="~/.emacs.d/",
show_default=True,
)
def init(tessif_directory=None):
"""Initialize Tessif's working directory."""
if not tessif_directory:
tessif_directory = os.path.join(os.path.expanduser("~"), ".tessif.d")
click.echo(f"Initializing Tessif's main directory at {tessif_directory}")


@main_cli_entry.command()
@click.argument("plugin")
def install_plugin(plugin):
@click.option(
"-vd",
"--venv_dir",
help="(Optional) Venv directory the plugin will be installed into.",
default=None,
show_default=True,
)
def install(plugin, venv_dir=None):
"""Install ESSMOS-Plugin."""
if not venv_dir:
venv_dir = os.path.join(tessif_dir, "plugin-venvs", plugin)
python_bin = os.path.join(venv_dir, "bin", "python")
venv.create(venv_dir, upgrade_deps=True, with_pip=True)
subprocess.run(
[
python_bin,
"-m",
"pip",
"install",
"-U", # update plugin
plugin,
]
)
Expand All @@ -44,62 +66,69 @@ def install_plugin(plugin):
@click.option(
"--directory",
help="Directory the system_model.tsf and the results are/will be stored in",
default=tessif_dir,
show_default=True,
)
@click.option(
"-q",
"--quiet",
help="Silences the stdout info level logging",
is_flag=True,
)
@click.argument("plugin")
def tropp(directory, plugin):
"""TRansform Optimize and Post-Process."""
def tropp(directory, plugin, quiet):
"""Transform Optimize and Post-Process."""
if quiet:
tessif_logging.set_logger_level(logger, tessif_logging.logging.WARNING)

system_model_location = os.path.join(directory, "tessif_system_model.tsf")
all_resultier_location = os.path.join(directory, "all_resutlier.alr")
igr_resultier_location = os.path.join(directory, "igr_resultier.igr")
opt_sysmod_location = os.path.join(directory, "optimized_system_model.osm")

click.echo("TRansform Optimize and Post-Process!\n")
logger.info("TRansform Optimize and Post-Process!\n")

module_name = plugin.replace("-", "_")
plugin_module = importlib.import_module(module_name)

import oemof.solph

click.echo("Using Plugin:")
click.echo(f"{plugin} version {plugin_module.__version__}\n")
click.echo("Using the modelling suit:")
click.echo(f"oemof.solph version {oemof.solph.__version__}\n")
logger.info("Using Plugin:")
logger.info(f"{plugin} version {plugin_module.__version__}\n")
logger.info("Using the modelling suit:")
logger.info(f"oemof.solph version {oemof.solph.__version__}\n")

click.echo("On System Model Stored In:")
click.echo(system_model_location)
click.echo()
logger.info("On System Model Stored In:")
logger.info(system_model_location)
logger.info("")

from tessif.model.energy_system import AbstractEnergySystem
from tessif.system_model import AbstractEnergySystem

restored_es = AbstractEnergySystem(uid="This Instance Is Restored")
msg = restored_es.unpickle(system_model_location)
click.echo("Succesfully deserialized tessif system model!")
click.echo("Nodes present in the tessif system model:")
click.echo([node.uid.name for node in restored_es.nodes])
click.echo()
restored_es.unpickle(system_model_location)
logger.info("Succesfully deserialized tessif system model!")
logger.info("Nodes present in the tessif system model:")
logger.info([node.uid.name for node in restored_es.nodes])
logger.info("")

# t2o = importlib.import_module(f"{module_name}.tsf2omf")
tool_system_model = plugin_module.transform(restored_es)

click.echo("Nodes present in the oemof system model:")
click.echo([str(node) for node in tool_system_model.nodes])
logger.info("Nodes present in the oemof system model:")
logger.info([str(node) for node in tool_system_model.nodes])

optimized_system_model = plugin_module.optimize(tool_system_model)

click.echo("Optimization Succesfull!")
click.echo(f"Global Costs: {optimized_system_model.results['global']['costs']}")
logger.info("Optimization Succesfull!")
pickle.dump(optimized_system_model, open(opt_sysmod_location, "wb"))
logger.info(f"Stored optmized system model to {opt_sysmod_location}")

click.echo("Post-Process Tool System Model")
logger.info("Post-Process Tool System Model")
post_process = importlib.import_module(f"{module_name}.post_process")
global_resultier = post_process.IntegratedGlobalResultier(
optimized_system_model)
global_resultier = post_process.IntegratedGlobalResultier(optimized_system_model)
global_resultier.pickle(igr_resultier_location)
click.echo(f"Stored global results to {igr_resultier_location}")
logger.info(f"Stored global results to {igr_resultier_location}")

all_resultier = post_process.AllResultier(optimized_system_model)
all_resultier.pickle(all_resultier_location)
click.echo(f"Stored all other results to {all_resultier_location}")


# if __name__ == '__main__':
# main_cli_entry()
logger.info(f"Stored all other results to {all_resultier_location}")
Loading

0 comments on commit 24b1372

Please sign in to comment.