Skip to content

Commit

Permalink
minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Andreas Lubojanski committed Jul 26, 2023
1 parent 3971e76 commit 0c3b169
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 47 deletions.
Binary file modified dist/InRetEnsys-0.2a6-py3-none-any.whl
Binary file not shown.
3 changes: 0 additions & 3 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@
if tmpfilename.find(".json") > 0:
dumpfile = os.path.join(dumpdir, tmpfilename.replace(".json", ".dump"))
logfile = os.path.join(logdir, tmpfilename.replace(".json", ".log"))
elif tmpfilename.find(".bin") > 0:
dumpfile = os.path.join(dumpdir, tmpfilename.replace(".bin", ".dump"))
logfile = os.path.join(logdir, tmpfilename.replace(".bin", ".log"))
else:
raise Exception("Fileformat is not valid!")

Expand Down
55 changes: 28 additions & 27 deletions src/InRetEnsys/common/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
## container for a configuration
class InRetEnsysConfigContainer(BaseModel):
## pydantic root validator to check and filter all none-type values.
@pydantic.model_validator(mode='before')
def check(cls, values):
retVal = {}
@pydantic.model_validator(mode='after')
def check(cls, data: any) -> any:
#print(data)

for value in values:
if values[value] is not None:
retVal[value] = values[value]
#for value in values:
# if values[value] is None:
# del values[value]

return retVal
return data

## pydantic subclass to add special configurations.
class Config:
Expand All @@ -39,29 +39,30 @@ def build_kwargs(self, energysystem: solph.EnergySystem) -> Dict[str, dict]:

for key in args:
value = args[key]
if key in special_keys:
oemof_io = {}
io_keys = list(value.keys())

for io_key in io_keys:
bus = energysystem.groups[io_key]
if isinstance(value[io_key], float) or isinstance(value[io_key], list):
oemof_io[bus] = value[io_key]
if value is not None:
if key in special_keys:
oemof_io = {}
io_keys = list(value.keys())

for io_key in io_keys:
bus = energysystem.groups[io_key]
if isinstance(value[io_key], float) or isinstance(value[io_key], list):
oemof_io[bus] = value[io_key]
else:
oemof_io[bus] = value[io_key].to_oemof(energysystem)

kwargs[key] = oemof_io

elif key == "nonconvex":
if isinstance(value, bool):
kwargs[key] = value
else:
oemof_io[bus] = value[io_key].to_oemof(energysystem)
kwargs[key] = value.to_oemof(energysystem)

kwargs[key] = oemof_io

elif key == "nonconvex":
if isinstance(value, bool):
kwargs[key] = value
else:
elif key == "investment":
kwargs[key] = value.to_oemof(energysystem)

elif key == "investment":
kwargs[key] = value.to_oemof(energysystem)

else:
kwargs[key] = value
else:
kwargs[key] = value

return kwargs
3 changes: 2 additions & 1 deletion src/InRetEnsys/components/constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ def to_oemof(self) -> Dict[str, dict]:
args = {}
for var in vars(self):
if var != "typ":
args[var] = vars(self)[var]
if vars(self)[var] is not None:
args[var] = vars(self)[var]

return args
22 changes: 6 additions & 16 deletions src/InRetEnsys/modelbuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,33 +42,22 @@ def __init__(self,
os.makedirs(self.DUMPING_DIRECTORY)

# handle various filetypes
logfile = os.path.basename(ConfigFile)

if ConfigFile.find(".json") > 0:
logfile = logfile.replace(".json", ".log")

xf = open(ConfigFile, 'rt')
model_dict = json.load(xf)
model = InRetEnsysModel(**model_dict)
xf.close()
elif ConfigFile.find(".bin") > 0:
xf = open(ConfigFile, 'rb')
model = pickle.load(xf)
xf.close()
else:
raise Exception("Fileformat is not valid!")



logfile = os.path.basename(ConfigFile)
if logfile.find(".json") > 0:
logfile = logfile.replace(".json", ".log")
elif logfile.find(".bin") > 0:
logfile = logfile.replace(".bin", ".log")
else:
raise Exception("Fileformat is not valid!")

tools.logger.define_logging(logpath=self.LOGGING_DIRECTORY, logfile=logfile, file_level=logging.INFO, screen_level=logging.INFO)

InRetEnsysLogger.info("Start Building and solving")

if hasattr(model, "solver_kwargs"):
if hasattr(model, "solver_kwargs") and model.solver_kwargs is not None:
cmdline_opts = model.solver_kwargs
else:
cmdline_opts = {}
Expand Down Expand Up @@ -140,6 +129,7 @@ def BuildEnergySystem(self, es: InRetEnsysEnergysystem, file: str, solver: Solve
##########################################################################
# Add Constraints to the model
##########################################################################
InRetEnsysLogger.info("Adding constraints to the energy system model.")
if hasattr(es, "constraints"):
for constr in es.constraints:
kwargs = constr.to_oemof()
Expand Down

0 comments on commit 0c3b169

Please sign in to comment.