##############################################################################
# Institute for the Design of Advanced Energy Systems Process Systems
# Engineering Framework (IDAES PSE Framework) Copyright (c) 2018, by the
# software owners: The Regents of the University of California, through
# Lawrence Berkeley National Laboratory, National Technology & Engineering
# Solutions of Sandia, LLC, Carnegie Mellon University, West Virginia
# University Research Corporation, et al. All rights reserved.
#
# Please see the files COPYRIGHT.txt and LICENSE.txt for full copyright and
# license information, respectively. Both files are also available online
# at the URL "https://github.com/IDAES/idaes".
##############################################################################
"""
Stream Utilities
"""
from __future__ import division, print_function, absolute_import
import pandas as pd
import pyomo.environ as pe
from idaes.core.stream import Stream
__author__ = "John Eslick"
[docs]def make_stream_table(model, attrs,
t=0, idx={}, streams=None, ignore_missing=False):
"""
This function makes a stream table contained in a Pandas data frame.
Args:
model: Pyomo block to search for streams in.
attrs: Pyomo port components to add to stream table
t: Time index for the stream table data
idx: Dictionary of index lists for indexed attributes
streams: Explicit list of steam objects to include. If this is None
all streams in model will be included
ignore_missing: If this is True and streams do not have all the
attributes requested, the steam will be included anyway with missing
data. If this option is false, streams that do not contain all
required information will be excluded.
Returns:
A pandas data frame with port variables in columns and streams in
rows.
Examples:
table = make_stream_table(
model=model,
attrs=["flow_component", "temperature", "pressure"],
idx={"flow_component":["CO2", "H2O", "N2", "O2", "Ar"]},
t=0,
ignore_missing=False)
"""
cols = []
for i in attrs:
if i in idx and idx[i] is not None:
for j in idx[i]:
cols.append("{}[{}]".format(i, j))
else:
cols.append(i)
stream_table = pd.DataFrame(columns=cols)
if streams is None:
streams = model.component_objects(pe.Block, descend_into=True)
for b in streams:
if isinstance(b, Stream):
for i in b:
row = []
src = b[i].config.source
src_idx = b[i].config.source_idx
if src_idx is None:
src_idx = t
else:
src_idx = (t, src_idx)
for a in attrs:
if a in idx and idx[a] is not None:
for j in idx[a]:
try:
row.append(src[src_idx].vars[a][j].value)
except KeyError: #stream doesn't have the right stuff (is fine)
row.append(None)
else:
try:
row.append(src[src_idx].vars[a][None].value)
except:
row.append(None)
if ignore_missing:
stream_table.loc[b[i].name] = row
elif None not in row:
stream_table.loc[b[i].name] = row
return stream_table