Skip to content

Commit

Permalink
add new scenario class decorator for convenience of managing multiple…
Browse files Browse the repository at this point in the history
… scenarios in process models
  • Loading branch information
yoelcortes committed Jan 4, 2025
1 parent 9c84483 commit f572b69
Showing 1 changed file with 56 additions and 1 deletion.
57 changes: 56 additions & 1 deletion biosteam/process_tools/process_model.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,69 @@
# -*- coding: utf-8 -*-
"""
"""
from dataclasses import dataclass
__all__ = ('ProcessModel', 'scenario')

__all__ = ('ProcessModel',)
def display_scenario(scenario):
slots = scenario.__slots__
units_of_measure = scenario.units_of_measure
arguments = []
for i in slots:
j = getattr(scenario, i)
if isinstance(j, (bool, str)):
arg = f"{i}={j!r},"
else:
try:
arg = f"{i}={j:.3g},"
except:
arg = f"{i}={j},"
if i in units_of_measure:
arg += ' # ' + units_of_measure[i]
arguments.append(
arg
)
if arguments:
clsname = type(scenario).__name__
N_spaces = len(clsname) +1
if N_spaces > 4:
arguments = '\n '.join(arguments)
print(
f"{clsname}("
f"\n {arguments}\n"
")"

)
else:
spaces = N_spaces * ' '
arguments = f'\n{spaces}'.join(arguments)
print(
f"{clsname}({arguments})"

)
else:
print(
f"{type(scenario).__name__}()"
)

def scenario(cls=None, **units_of_measure):
if cls is None: return lambda cls: scenario(cls, **units_of_measure)
cls = dataclass(cls,
init=True, repr=True, eq=True, unsafe_hash=False, frozen=True,
match_args=True, slots=True,
)
cls.units_of_measure = units_of_measure
cls._ipython_display_ = cls.show = display_scenario
return cls

class ProcessModel:
"""Abstract class for setting up an all-in-one handle with easy
access to all objects related to a process model, including chemicals,
streams, units, systems, and model parameters and metrics."""

def baseline(self):
sample = self.model.get_baseline_sample()
return sample, self.model(sample)

def load_system(self, system):
self.system = system
self.flowsheet = flowsheet = system.flowsheet
Expand Down

0 comments on commit f572b69

Please sign in to comment.