def display(self, prefix="", ostream=None): """ Print component state information This duplicates logic in Component.pprint() """ if not self.active: return if ostream is None: ostream = sys.stdout tab = " " ostream.write(prefix + self.local_name + " : ") ostream.write("Size=" + str(len(self))) ostream.write("\n") def _line_generator(k,v): for _k, _v in sorted(iteritems(v.vars)): if _v is None: _val = '-' elif not _v.is_indexed(): _val = str(value(_v)) else: _val = "{%s}" % ( ', '.join('%r: %r' % ( x, value(_v[x])) for x in sorted(_v._data))) yield _k, _val tabular_writer(ostream, prefix+tab, ((k, v) for k, v in iteritems(self._data)), ("Name", "Value"), _line_generator)
def display(self, prefix="", ostream=None): """ Print component state information This duplicates logic in Component.pprint() """ if not self.active: return if ostream is None: ostream = sys.stdout tab = " " ostream.write(prefix + self.local_name + " : ") ostream.write("Size=" + str(len(self))) ostream.write("\n") def _line_generator(k, v): for _k, _v in sorted(iteritems(v.vars)): if _v is None: _val = '-' elif not hasattr(_v, 'is_indexed') or not _v.is_indexed(): _val = str(value(_v)) else: _val = "{%s}" % (', '.join('%r: %r' % (x, value(_v[x])) for x in sorted(_v._data)), ) yield _k, _val tabular_writer(ostream, prefix + tab, ((k, v) for k, v in iteritems(self._data)), ("Name", "Value"), _line_generator)
def _pprint_base_impl(self, ostream, verbose, prefix, _name, _doc, _constructed, _attr, _data, _header, _fcn): if ostream is None: ostream = sys.stdout if prefix: ostream = StreamIndenter(ostream, prefix) # FIXME: HACK for backwards compatability with suppressing the # header for the top block if not _attr and self.parent_block() is None: _name = '' # We only indent everything if we printed the header if _attr or _name or _doc: ostream = StreamIndenter(ostream, self._PPRINT_INDENT) # The first line should be a hanging indent (i.e., not indented) ostream.newline = False if _name: ostream.write(_name + " : ") if _doc: ostream.write(_doc + '\n') if _attr: ostream.write(", ".join("%s=%s" % (k, v) for k, v in _attr)) if _attr or _name or _doc: ostream.write("\n") if not _constructed: # HACK: for backwards compatability, Abstract blocks will # still print their assigned components. Should we instead # always pprint unconstructed components (possibly # suppressing the table header if the table is empty)? if self.parent_block() is not None: ostream.write("Not constructed\n") return if type(_fcn) is tuple: _fcn, _fcn2 = _fcn else: _fcn2 = None if _header is not None: if _fcn2 is not None: _data_dict = dict(_data) _data = iteritems(_data_dict) tabular_writer(ostream, '', _data, _header, _fcn) if _fcn2 is not None: for _key in sorted_robust(_data_dict): _fcn2(ostream, _key, _data_dict[_key]) elif _fcn is not None: _data_dict = dict(_data) for _key in sorted_robust(_data_dict): _fcn(ostream, _key, _data_dict[_key]) elif _data is not None: ostream.write(_data)
def test_unicode_table(self): # Test that an embedded unicode character does not foul up the # table alignment os = StringIO() data = {1: ("a", 1), (2, 3): ("∧", 2)} tabular_writer(os, "", iteritems(data), ["s", "val"], lambda k, v: v) ref = u""" Key : s : val 1 : a : 1 (2, 3) : ∧ : 2 """ self.assertEqual(ref.strip(), os.getvalue().strip())
def display(self, prefix="", ostream=None): """TODO""" if not self.active: return if ostream is None: ostream = sys.stdout tab = " " ostream.write(prefix + self.local_name + " : ") ostream.write("Size=" + str(len(self))) ostream.write("\n") tabular_writer( ostream, prefix+tab, ((k,v) for k,v in iteritems(self._data)), ( "Key","Value" ), lambda k, v: \ [k, "Undefined" if v.expr is None else v()])
def display(self, prefix="", ostream=None): """TODO""" if not self.active: return if ostream is None: ostream = sys.stdout tab=" " ostream.write(prefix+self.local_name+" : ") ostream.write("Size="+str(len(self))) ostream.write("\n") tabular_writer( ostream, prefix+tab, ((k,v) for k,v in iteritems(self._data)), ( "Key","Value" ), lambda k, v: \ [k, "Undefined" if v.expr is None else v()])
def pprint(self, ostream=None, verbose=False, prefix=""): """Print component information""" if ostream is None: ostream = sys.stdout tab = " " ostream.write(prefix + self.local_name + " : ") if self.doc is not None: ostream.write(self.doc + '\n' + prefix + tab) _attr, _data, _header, _fcn = self._pprint() ostream.write(", ".join("%s=%s" % (k, v) for k, v in _attr)) ostream.write("\n") if not self._constructed: ostream.write(prefix + tab + "Not constructed\n") return if _data is not None: tabular_writer(ostream, prefix + tab, _data, _header, _fcn)
def display(self, prefix="", ostream=None): """Provide a verbose display of this object""" if not self.active: return tab = " " if ostream is None: ostream = sys.stdout ostream.write(prefix+self.local_name+" : ") ostream.write(", ".join("%s=%s" % (k,v) for k,v in [ ("Size", len(self)), ("Index", self._index if self.is_indexed() else None), ("Active", self.active), ] )) ostream.write("\n") tabular_writer( ostream, prefix+tab, ((k,v) for k,v in self._data.items() if v.active), ( "Active","Value" ), lambda k, v: [ v.active, value(v), ] )
def pprint(self, ostream=None, verbose=False, prefix=""): """Print component information""" if ostream is None: ostream = sys.stdout tab=" " ostream.write(prefix+self.local_name+" : ") if self.doc is not None: ostream.write(self.doc+'\n'+prefix+tab) _attr, _data, _header, _fcn = self._pprint() ostream.write(", ".join("%s=%s" % (k,v) for k,v in _attr)) ostream.write("\n") if not self._constructed: ostream.write(prefix+tab+"Not constructed\n") return if _data is not None: tabular_writer( ostream, prefix+tab, _data, _header, _fcn )
def display(self, prefix="", ostream=None): """ Print component state information This duplicates logic in Component.pprint() """ if not self.active: return if ostream is None: ostream = sys.stdout tab = " " ostream.write(prefix + self.local_name + " : ") ostream.write("Size=" + str(len(self))) ostream.write("\n") tabular_writer(ostream, prefix + tab, ((k, v) for k, v in iteritems(self._data) if v.active), ("Body",), lambda k, v: [v.body(), ])
def display(self, prefix="", ostream=None): """Provide a verbose display of this object""" if not self.active: return tab = " " if ostream is None: ostream = sys.stdout ostream.write(prefix+self.local_name+" : ") ostream.write(", ".join("%s=%s" % (k,v) for k,v in [ ("Size", len(self)), ("Index", self._index \ if self._index != UnindexedComponent_set else None), ("Active", self.active), ] )) ostream.write("\n") tabular_writer( ostream, prefix+tab, ((k,v) for k,v in iteritems(self._data) if v.active), ( "Active","Value" ), lambda k, v: [ v.active, value(v), ] )
def display(self, prefix="", ostream=None): """ Print component state information This duplicates logic in Component.pprint() """ if not self.active: return if ostream is None: ostream = sys.stdout tab=" " ostream.write(prefix+self.local_name+" : ") ostream.write("Size="+str(len(self))) ostream.write("\n") tabular_writer( ostream, prefix+tab, ((k,v) for k,v in iteritems(self._data) if v.active), ( "Lower","Body","Upper" ), lambda k, v: [ value(v.lower), v.body(), value(v.upper), ] )
def display(self, prefix="", ostream=None): """Provide a verbose display of this object""" if not self.active: return tab = " " if ostream is None: ostream = sys.stdout ostream.write(prefix + self.cname() + " : ") ostream.write(", ".join("%s=%s" % (k,v) for k,v in [ ("Size", len(self)), ("Index", self._index \ if self._index != UnindexedComponent_set else None), ("Active", self.active), ] )) ostream.write("\n") tabular_writer(ostream, prefix + tab, ((k, v) for k, v in iteritems(self._data) if v.active), ("Key", "Active", "Value"), lambda k, v: [ k, v.active, value(v), ])
def report(self, index=(0), true_state=False, dof=False, ostream=None, prefix=""): """ Default report method for StateBlocks. Returns a Block report populated with either the display or state variables defined in the StateBlockData class. Args: index : tuple of Block indices indicating which point in time (and space if applicable) to report state at. true_state : whether to report the display variables (False default) or the actual state variables (True) dof : whether to show local degrees of freedom in the report (default=False) ostream : output stream to write report to prefix : string to append to the beginning of all output lines Returns: Printed output to ostream """ if ostream is None: ostream = sys.stdout # Get DoF and model stats if dof: dof_stat = degrees_of_freedom(self[index]) nv = number_variables(self[index]) nc = number_activated_constraints(self[index]) nb = number_activated_blocks(self[index]) # Create stream table if true_state: disp_dict = self[index].define_state_vars() else: disp_dict = self[index].define_display_vars() stream_attributes = {} for k in disp_dict: for i in disp_dict[k]: if i is None: stream_attributes[k] = disp_dict[k][i] else: stream_attributes[k + " " + i] = disp_dict[k][i] # Write output max_str_length = 84 tab = " " * 4 ostream.write("\n" + "=" * max_str_length + "\n") lead_str = f"{prefix}State : {self.name}" trail_str = f"Index: {index}" mid_str = " " * (max_str_length - len(lead_str) - len(trail_str)) ostream.write(lead_str + mid_str + trail_str) if dof: ostream.write("\n" + "=" * max_str_length + "\n") ostream.write(f"{prefix}{tab}Local Degrees of Freedom: {dof_stat}") ostream.write('\n') ostream.write(f"{prefix}{tab}Total Variables: {nv}{tab}" f"Activated Constraints: {nc}{tab}" f"Activated Blocks: {nb}") ostream.write("\n" + "-" * max_str_length + "\n") ostream.write(f"{prefix}{tab}State Report") if any(isinstance(v, _VarData) for k, v in stream_attributes.items()): ostream.write("\n" * 2) ostream.write(f"{prefix}{tab}Variables: \n\n") tabular_writer( ostream, prefix + tab, ((k, v) for k, v in stream_attributes.items() if isinstance(v, _VarData)), ("Value", "Fixed", "Bounds"), lambda k, v: ["{:#.5g}".format(value(v)), v.fixed, v.bounds]) if any( isinstance(v, _ExpressionData) for k, v in stream_attributes.items()): ostream.write("\n" * 2) ostream.write(f"{prefix}{tab}Expressions: \n\n") tabular_writer(ostream, prefix + tab, ((k, v) for k, v in stream_attributes.items() if isinstance(v, _ExpressionData)), ("Value", ), lambda k, v: ["{:#.5g}".format(value(v))]) ostream.write("\n" + "=" * max_str_length + "\n")
def report(self, time_point=0, dof=False, ostream=None, prefix=""): time_point = float(time_point) if ostream is None: ostream = sys.stdout # Get DoF and model stats if dof: dof_stat = degrees_of_freedom(self) nv = number_variables(self) nc = number_activated_constraints(self) nb = number_activated_blocks(self) # Get components to report in performance section performance = self._get_performance_contents(time_point=time_point) # Get stream table stream_table = self._get_stream_table_contents(time_point=time_point) # Set model type output if hasattr(self, "is_flowsheet") and self.is_flowsheet: model_type = "Flowsheet" else: model_type = "Unit" # Write output max_str_length = 84 tab = " " * 4 ostream.write("\n" + "=" * max_str_length + "\n") lead_str = f"{prefix}{model_type} : {self.name}" trail_str = f"Time: {time_point}" mid_str = " " * (max_str_length - len(lead_str) - len(trail_str)) ostream.write(lead_str + mid_str + trail_str) if dof: ostream.write("\n" + "=" * max_str_length + "\n") ostream.write(f"{prefix}{tab}Local Degrees of Freedom: {dof_stat}") ostream.write('\n') ostream.write(f"{prefix}{tab}Total Variables: {nv}{tab}" f"Activated Constraints: {nc}{tab}" f"Activated Blocks: {nb}") if performance is not None: ostream.write("\n" + "-" * max_str_length + "\n") ostream.write(f"{prefix}{tab}Unit Performance") ostream.write("\n" * 2) if "vars" in performance.keys() and len(performance["vars"]) > 0: ostream.write(f"{prefix}{tab}Variables: \n\n") tabular_writer( ostream, prefix + tab, ((k, v) for k, v in performance["vars"].items()), ("Value", "Fixed", "Bounds"), lambda k, v: ["{:#.5g}".format(value(v)), v.fixed, v.bounds]) if "exprs" in performance.keys() and len(performance["exprs"]) > 0: ostream.write("\n") ostream.write(f"{prefix}{tab}Expressions: \n\n") tabular_writer(ostream, prefix + tab, ((k, v) for k, v in performance["exprs"].items()), ("Value", ), lambda k, v: ["{:#.5g}".format(value(v))]) if ("params" in performance.keys() and len(performance["params"]) > 0): ostream.write("\n") ostream.write(f"{prefix}{tab}Parameters: \n\n") tabular_writer(ostream, prefix + tab, ((k, v) for k, v in performance["params"].items()), ("Value", "Mutable"), lambda k, v: [value(v), not v.is_constant()]) if stream_table is not None: ostream.write("\n" + "-" * max_str_length + "\n") ostream.write(f"{prefix}{tab}Stream Table") ostream.write('\n') ostream.write( textwrap.indent(stream_table_dataframe_to_string(stream_table), prefix + tab)) ostream.write("\n" + "=" * max_str_length + "\n")