BioSTEAMDevelopmentGroup / biosteam

The Biorefinery Simulation and Techno-Economic Analysis Modules; Life Cycle Assessment; Chemical Process Simulation Under Uncertainty
Other
172 stars 33 forks source link

Feature request: `replace` function for streams and units #149

Closed yalinli2 closed 1 year ago

yalinli2 commented 1 year ago

This request originates from my recent trials to replace just one stream of a system, but couldn't find a best way to do so. For the corn stover biorefinery, I was hoping to just replace the feedstock from the original cornstover to a new miscanthus stream, however, I'm not sure how to best handle the connections and update the System.streams, System.feeds, etc., so this is what I ended up with:

# The `sys` object is the original cornstover system, `stream` is `bst.main_flowsheet.stream`, `feedstock_kwargs` is used to create the new miscanthus stream as the feedstock
>>> cornstover = stream.search('cornstover')
>>> feedstock_kwargs['ID'] = feedstock_kwargs.get('ID') or 'feedstock'
>>> feedstock_kwargs['total_flow'] = feedstock_kwargs.get('total_flow') or cornstover.F_mass
>>> feedstock = bst.Stream(**feedstock_kwargs)
>>> sink = cornstover.sink
>>> sink.ins[0] = feedstock
>>> new_sys = bst.System(f'{feedstock.ID}_sys', path=sys.units)
>>> F.discard(cornstover)
>>> F.discard(sys)

It works but I don't like how I made it work, so just wondering is there a better way to do so, or could we add a replace function for streams and units (maybe even systems, not sure if it's good)? Thanks!

yoelcortes commented 1 year ago

@yalinli2, sure thing, there is actually a good way to do this:

Systems remember their original configuration by default to enable agile systems and have multiple systems with different configurations. To "refresh" the system, you can pass "update_configuration=True" during simulation.

If you only changed unit connections:

system.simulate(update_configuration=True)

If you also have new units:

system.simulate(update_configuration=True, units=units)

Unit connections are also updated dynamically (i.e., if connections change during simulation). I hope this helps!

yalinli2 commented 1 year ago

ah I see! I tried sys._setup(update_configuration=True) but probably messed up somewhere so it wasn't working as I expected, thanks @yoelcortes !