Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 62 additions & 44 deletions src/main/python/DockWidgets/DockWidgetMaterialStream.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,22 @@ def __init__(self, name, comptype, obj, container, parent=None):
self.name_type = None
self.container = container

header = QTreeWidgetItem(['Compound', 'Value', 'Unit'])
self.mTreeWidget.setHeaderItem(header)
self.lTreeWidget.setHeaderItem(header)
self.vTreeWidget.setHeaderItem(header)
# dropdown
self._composition_basis_options = [
'Mole Fraction', 'Mass Fraction', 'Mole Flow', 'Mass Flow'
]
for opt in self._composition_basis_options:
self.cbCompositionBasis.addItem(opt)
self.cbCompositionBasis.setCurrentIndex(0)
self.cbCompositionBasis.currentIndexChanged.connect(
self._on_composition_basis_changed
)

# Storage for amounts results: { basis_name: { phase_no: [(compound, value)] } }
# phase_no: 1=Mixture, 2=Liquid, 3=Vapour
self._amounts_data = {}
for basis in self._composition_basis_options:
self._amounts_data[basis] = {1: [], 2: [], 3: []}

# --- Corrected Thermo Package file reading ---
if getattr(sys, 'frozen', False):
Expand Down Expand Up @@ -298,12 +310,17 @@ def show_result(ms_lst):
i.results_category(i.name)

def clear_results(self):
self.mTreeWidget.clear()
# Clear amounts tables
self.amountsMixtureTable.setRowCount(0)
self.amountsLiquidTable.setRowCount(0)
self.amountsVapourTable.setRowCount(0)
# Clear phase properties tables
self.mTableWidget.setRowCount(0)
self.lTreeWidget.clear()
self.lTableWidget.setRowCount(0)
self.vTreeWidget.clear()
self.vTableWidget.setRowCount(0)
# Reset stored amounts data
for basis in self._composition_basis_options:
self._amounts_data[basis] = {1: [], 2: [], 3: []}

# result data tab
def results_category(self,name):
Expand All @@ -325,59 +342,41 @@ def _safe_result(val):
p = {"Pressure":"P", "Temperature":"T","Vapour Phase Mole Fraction":"xvap", "Phase Molar Enthalpy":"H_p",
"Phase Molar Entropy":"S_p", "Molar Flow Rate":"F_p","Mass Flow Rate":"Fm_p"}

# Amounts Tab
# Amounts Tab — populate _amounts_data dict
if obj.type == 'MaterialStream':
# Reset stored data
for basis in self._composition_basis_options:
self._amounts_data[basis] = {1: [], 2: [], 3: []}

ll = [] # list for basis names
for basis in d:
propertyname = name + '.' + d[basis]
#print("basis ", basis, propertyname)
for i in result[0]:
if (propertyname in i):
ll.append(i)
#print(ll)

j = 0
namee = 'none'
#print("namee ", namee)
#initialization for treewidgets
lroot = 1
mroot = 1
vroot = 1


for i,k in enumerate(ll):
for i, k in enumerate(ll):
ind = result[0].index(k)
#print("index ", ind)
#print("str ", k)
resultval = str(result[-1][ind])
#print("######Resultsfetch####",resultval)
#print(k[k.find(".")+1:k.find("[")])
obj.variables[k.split('.')[1]]['value'] = resultval

if namee not in k:
mroot = QTreeWidgetItem(self.mTreeWidget, [ms_lst[j]])
lroot = QTreeWidgetItem(self.lTreeWidget, [ms_lst[j]])
vroot = QTreeWidgetItem(self.vTreeWidget, [ms_lst[j]])
namee = klst[j]

phase_no = int(k[k.index(',') - 1]) # phase no is from modelica list
compound_no = int(k[k.index(',') + 1]) - 1 # compound is from python list

if phase_no == 1:
child = QTreeWidgetItem(mroot, [compound_selected[compound_no], _safe_result(resultval),
obj.variables[k.split('.')[1]]['unit']])
elif phase_no == 2:
child = QTreeWidgetItem(lroot, [compound_selected[compound_no], _safe_result(resultval),
obj.variables[k.split('.')[1]]['unit']])
elif phase_no == 3:
child = QTreeWidgetItem(vroot, [compound_selected[compound_no], _safe_result(resultval),
obj.variables[k.split('.')[1]]['unit']])
if (compound_no + 1) == len(compound_selected):
j += 1
# drop down default behaviour is set to expanded
self.mTreeWidget.expandAll()
self.lTreeWidget.expandAll()
self.vTreeWidget.expandAll()
phase_no = int(k[k.index(',') - 1]) # phase no from modelica list
compound_no = int(k[k.index(',') + 1]) - 1 # compound from python list

self._amounts_data[ms_lst[j]][phase_no].append(
(compound_selected[compound_no], _safe_result(resultval))
)

if phase_no == 3 and (compound_no + 1) == len(compound_selected):
j += 1

# Refresh the visible amounts tables for the currently selected basis
self._on_composition_basis_changed()

# Phase Properties Tab
phaseResLst = []
Expand Down Expand Up @@ -513,4 +512,23 @@ def normalize(self):
except Exception as e:
print(f"[UI] Submit failed for {self.name}: {e}")
print(e)


# ------------------- Amounts: Composition Basis switching -------------------
def _on_composition_basis_changed(self, index=None):
"""Refresh the three Amounts phase tables for the currently selected basis."""
basis = self.cbCompositionBasis.currentText()
data = self._amounts_data.get(basis, {1: [], 2: [], 3: []})
self._populate_amounts_table(self.amountsMixtureTable, data.get(1, []))
self._populate_amounts_table(self.amountsLiquidTable, data.get(2, []))
self._populate_amounts_table(self.amountsVapourTable, data.get(3, []))

@staticmethod
def _populate_amounts_table(table, rows):
"""Fill a QTableWidget with (compound, value) rows."""
table.setRowCount(0)
for compound, value in rows:
row_pos = table.rowCount()
table.insertRow(row_pos)
table.setItem(row_pos, 0, QTableWidgetItem(compound))
table.setItem(row_pos, 1, QTableWidgetItem(str(value)))
table.resizeColumnsToContents()
Loading