def make_table(self, frm, title, time_index, items, row_headers, attributes, column_headers): frm.setWindowTitle(title) tbl = frm.tblGeneric tbl.setRowCount(len(row_headers)) tbl.setColumnCount(len(column_headers)) tbl.setHorizontalHeaderLabels(column_headers) tbl.setVerticalHeaderLabels(row_headers) row = 0 for this_item in items: col = 0 values = this_item.get_all_attributes_at_time(self.output, time_index) for attribute in attributes: val_str = attribute.str(values[attribute.index]) table_cell_widget = QtGui.QTableWidgetItem(val_str) table_cell_widget.setFlags(QtCore.Qt.ItemIsSelectable) # | QtCore.Qt.ItemIsEnabled) table_cell_widget.setTextAlignment(QtCore.Qt.AlignVCenter | QtCore.Qt.AlignRight) tbl.setItem(row, col, table_cell_widget) col += 1 row += 1 if row == 20: tbl.resizeColumnsToContents() frm.show() frm.update() process_events()
def update_progress_days(self, elapsed_days, total_days): int_days = int(elapsed_days) update = False if int_days != self._last_displayed_days: self._last_displayed_days = int_days self.txtDays.setText(str(int_days)) self.txtDays.update() update = True if total_days <= self.SHORT_TERM_LIMIT: float_hours = (elapsed_days - int_days) * 24 int_hours = int(float_hours) minutes = int((float_hours - int_hours) * 60) self.txtHrsMin.setText("{:02}:{:02}".format(int_hours, minutes)) self.txtHrsMin.update() update = True if update: process_events()
def RunQuality(self, total_days): """ Run water quality simulation var err: Integer t, tstep: Longint h: Single slabel: String """ try: self.set_status_text(self.TXT_SOLVING_WQ) self.model_api.ENopenQ() self.model_api.ENinitQ(1) self._last_displayed_days = -1 err = None seconds_this_step = 1 elapsed_days = 0.0 while seconds_this_step != 0 and self.run_status != RunStatus.rsCancelled: # and err <= 100 elapsed_seconds = self.model_api.ENrunQ() # if err: # print("ENrunQ returned " + str(err)) # raise Exception("Error code from model on ENrunQ: " + str(err)) seconds_this_step = self.model_api.ENnextQ() #elapsed_days += (seconds_this_step / 3600) elapsed_days = elapsed_seconds / 3600.0 / 24.0 self.update_progress_days(elapsed_days, total_days) # Add and multiply by 2 to pick up progress where RunHydraulics left off self.update_progress_bar(elapsed_days + total_days, total_days * 2) # print (elapsed_days, elapsed_seconds / 3600, seconds_this_step) # print ("RunQ:", elapsed_days, elapsed_seconds / 3600 / 24, seconds_this_step) process_events() finally: try: self.model_api.ENcloseQ() except: # Ignore exception on close? pass
def RunHydraulics(self, total_days): # // Runs hydraulic simulation # var # err: Integer # t, tstep: Longint # h: Single # slabel: String try: self.set_status_text(self.TXT_SOLVING_HYD) self.model_api.ENopenH() self.model_api.ENinitH(1) self._last_displayed_days = -1 seconds_this_step = 1 elapsed_days = 0.0 while seconds_this_step != 0 and self.run_status != RunStatus.rsCancelled: # and err <= 100 elapsed_seconds = self.model_api.ENrunH() # if err: # print("ENrunH returned " + str(err)) # raise Exception("Error from model on ENrunH: " + str(err)) seconds_this_step = self.model_api.ENnextH() # elapsed_days += (seconds_this_step / 3600) elapsed_days = elapsed_seconds / 3600.0 / 24.0 self.update_progress_days(elapsed_days, total_days) # Multiply total_days by 2 to leave room for progress during RunQuality self.update_progress_bar(elapsed_days, total_days * 2) self.update_progress_days(elapsed_days, total_days) # print ("RunH:", elapsed_days, elapsed_seconds / 3600, seconds_this_step) process_events() finally: try: self.model_api.ENcloseH() except: # Ignore exception on close? pass
def set_status_text(self, text): self.StatusLabel.setText(text) self.StatusLabel.update() process_events()
def Execute(self): """ Call into the SWMM DLL engine to perform a simulation. """ """ TODO? The input, report, and binary output files required by the SWMM engine have already been created in Fmain.pas' RunSimulation procedure through a call to CreateTempFiles. """ self.fraRunning.setVisible(True) self.fraFinished.setVisible(False) self.fraBottom.setVisible(True) self.set_status(RunStatus.rsCompiling) # # Save the current project input data to a temporary file self.set_status_text(self.TXT_SAVING) # S = TStringlist.Create # Input will be placed in a stringlist # try: # Uexport.ExportProject(S) # Write input data to the stringlist # Uexport.ExportTempDir(S) # Add temp. directory name to input # S.SaveToFile(TempInputFile) # Save input to file # finally: # S.Free # # # Have the SWMM solver read the input data file self.set_status_text(self.TXT_READING) self.cmdStop.setVisible(True) self.cmdMinimize.setVisible(True) self.cmdOK.setVisible(False) # self.ProgressLabel.Refresh # InpFile = AnsiString(TempInputFile) # RptFile = AnsiString(TempReportFile) # OutFile = AnsiString(TempOutputFile) # Err = swmm_open(PAnsiChar(InpFile), PAnsiChar(RptFile), PAnsiChar(OutFile)) # # # If there are no input errors, then initialize the simulation # if Err == 0: # self.ProgressLabel.Caption = self.TXT_CHECKING # self.ProgressLabel.Refresh # Err = swmm_start(1) # # # If there are no initialization errors, then... # if Err == 0: # # Get the simulation duration in days self.showNormal() self.set_status(RunStatus.rsCompiling) try: total_days = self.compute_total_days() if total_days: # If we could compute a number of simulation days, prepare progress and date/time controls self.progressBar.setVisible(True) self.lblTime.setVisible(True) self.fraTime.setVisible(True) if total_days >= self.SHORT_TERM_LIMIT: self.lblHrsMin.setVisible(False) self.txtHrsMin.setVisible(False) else: # Prepare the elapsed time display for shorter simulations self.lblHrsMin.setVisible(True) self.txtHrsMin.setVisible(True) self.txtHrsMin.setText("00:00") else: # No computed number of simulation days, so hide progress and date/time controls self.progressBar.setVisible(False) self.lblTime.setVisible(False) self.fraTime.setVisible(False) self.set_status(RunStatus.rsComputing) print("Running SWMM " + str(self.model_api.swmm_getVersion())) self.model_api.swmm_run() self.model_api.swmm_open() self.model_api.swmm_start() date_updated = datetime.now() while self.run_status == RunStatus.rsComputing and self.model_api.errcode == 0: elapsed_days = self.model_api.swmm_step() if elapsed_days > 0: if total_days: date_now = datetime.now() if (date_now - date_updated).microseconds > 100000: self.update_progress_days(elapsed_days, total_days) self.update_progress_bar(elapsed_days, total_days) process_events() date_updated = date_now else: self.model_api.swmm_end() break # Step through each time period until there is no more time left, # an error occurs, or the user stops the run # OldTime = Time # repeat # Application.ProcessMessages # Err = swmm_step(ElapsedTime) # NewTime = Time # if MilliSecondsBetween(NewTime, OldTime) > 100: # UpdateProgressDisplay(ElapsedTime, self.total_days) # OldTime = NewTime # until(ElapsedTime==0) or (Err > 0) or (self.run_status == RunStatus.rsStopped) # # End the simulation and retrieve mass balance errors # swmm_end() # swmm_getMassBalErr(self.ErrRunoff, self.ErrFlow, self.ErrQual) self.ErrRunoff, self.ErrFlow, self.ErrQual = self.model_api.swmm_getMassBalErr( ) if self.model_api.Errflag: print("\n\nSWMM completed. There are errors.\n") self.set_status(RunStatus.rsError) elif self.model_api.Warnflag: print("\n\nSWMM completed. There are warnings.\n") self.set_status(RunStatus.rsWarning) else: print("\n\nSWMM completed.\n") self.set_status(RunStatus.rsSuccess) self.DisplayRunStatus() self.lblSuccessful.setText(self.StatusLabel.text()) self.fraRunning.setVisible(False) self.gbxContinuity.setVisible(True) self.fraFinished.setVisible(True) self.cmdStop.setVisible(False) self.cmdMinimize.setVisible(False) self.cmdOK.setVisible(True) self.update() process_events() if self.model_api.Errflag or self.model_api.Warnflag: self._main_form.report_status() except Exception as e: # Close solver if an exception occurs self.set_status(RunStatus.rsError) self.cmdStop.setVisible(False) self.cmdMinimize.setVisible(False) self.cmdOK.setVisible(True) msg = "Exception running simulation: " + '\n' + str( e) + '\n' + str(traceback.print_exc()) print(msg) QMessageBox.information(None, "SWMM", msg, QMessageBox.Ok) self._main_form.report_status() # self.set_status(RunStatus.rsShutdown) pass finally: try: # self.model_api.swmm_report() self.model_api.swmm_close() except: # Ignore exception closing model object pass
def Execute(self): self.fraRunning.setVisible(True) self.fraFinished.setVisible(False) self.fraBottom.setVisible(True) self.set_status_text(self.TXT_STATUS_COMPILING) self.cmdStop.setVisible(True) self.cmdMinimize.setVisible(True) self.cmdOK.setVisible(False) # var # OldDir: String # begin # // Change to temporary directory # GetDir(0,OldDir) # ChDir(TempDir) # # // Update the form's display #self.Update() # self.showNormal() self.set_status(RunStatus.rsCompiling) # TODO: decide whether to save input file in its own folder (only if modified) or in temp folder # Uexport.ExportDataBase(TempInputFile,False) # # // Open solver and read in network data try: self._last_displayed_days = -1 total_days = self.compute_total_days() if total_days: # If we could compute a number of simulation days, prepare progress and date/time controls self.progressBar.setVisible(True) self.lblTime.setVisible(True) self.fraTime.setVisible(True) if total_days >= self.SHORT_TERM_LIMIT: self.lblHrsMin.setVisible(False) self.txtHrsMin.setVisible(False) else: # Prepare the elapsed time display for shorter simulations self.lblHrsMin.setVisible(True) self.txtHrsMin.setVisible(True) self.txtHrsMin.setText("00:00") else: # No computed number of simulation days, so hide progress and date/time controls self.progressBar.setVisible(False) self.lblTime.setVisible(False) self.fraTime.setVisible(False) self.model_api.ENopen() # Solve for hydraulics & water quality, then close solver if self.run_status != RunStatus.rsCancelled: self.RunHydraulics(total_days) if self.run_status != RunStatus.rsCancelled: self.RunQuality(total_days) self.set_status(RunStatus.rsSuccess) except Exception as e: # Close solver if an exception occurs self.set_status_text(self.TXT_STATUS_ERROR) msg = "Exception running simulation: " + '\n' + str(e) + '\n' + str(traceback.print_exc()) print(msg) QtGui.QMessageBox.information(None, "EPANET", msg, QtGui.QMessageBox.Ok) self.set_status(RunStatus.rsShutdown) finally: try: self.lblSuccessful.setText(self.StatusLabel.text()) self.fraRunning.setVisible(False) self.gbxContinuity.setVisible(False) self.fraFinished.setVisible(True) self.cmdStop.setVisible(False) self.cmdMinimize.setVisible(False) self.cmdOK.setVisible(True) self.update() process_events() except: # Ignore exception closing model object? pass try: self.model_api.ENreport() self.model_api.ENclose() except: # Ignore exception closing model object? pass
def Execute(self): self.run_err_msg = "" self.fraRunning.setVisible(True) self.fraFinished.setVisible(False) self.fraBottom.setVisible(True) self.set_status(RunStatus.rsCompiling) self.cmdStop.setVisible(True) self.cmdMinimize.setVisible(True) self.cmdOK.setVisible(False) # var # OldDir: String # begin # // Change to temporary directory # GetDir(0,OldDir) # ChDir(TempDir) # # // Update the form's display #self.Update() # self.showNormal() self.set_status(RunStatus.rsCompiling) # TODO: decide whether to save input file in its own folder (only if modified) or in temp folder # Uexport.ExportDataBase(TempInputFile,False) # # // Open solver and read in network data try: self._last_displayed_days = -1 total_days = self.compute_total_days() if total_days: # If we could compute a number of simulation days, prepare progress and date/time controls self.progressBar.setVisible(True) self.lblTime.setVisible(True) self.fraTime.setVisible(True) if total_days >= self.SHORT_TERM_LIMIT: self.lblHrsMin.setVisible(False) self.txtHrsMin.setVisible(False) else: # Prepare the elapsed time display for shorter simulations self.lblHrsMin.setVisible(True) self.txtHrsMin.setVisible(True) self.txtHrsMin.setText("00:00") else: # No computed number of simulation days, so hide progress and date/time controls self.progressBar.setVisible(False) self.lblTime.setVisible(False) self.fraTime.setVisible(False) self.model_api.ENopen() # Solve for hydraulics & water quality, then close solver if self.run_status != RunStatus.rsCancelled: self.RunHydraulics(total_days) if self.run_status != RunStatus.rsCancelled: self.RunQuality(total_days) if self.run_status != RunStatus.rsCancelled: self.set_status(RunStatus.rsSuccess) except Exception as e: # Close solver if an exception occurs self.set_status(RunStatus.rsError) self.run_err_msg = "Simulation problem: " + '\n' + str(e) + '\n' + str(traceback.print_exc()) print(self.run_err_msg) # QtGui.QMessageBox.information(None, "EPANET", self.run_err_msg, QtGui.QMessageBox.Ok) self.set_status(RunStatus.rsError) finally: try: self.lblSuccessful.setText(self.StatusLabel.text()) self.fraRunning.setVisible(False) self.gbxContinuity.setVisible(False) self.fraFinished.setVisible(True) self.cmdStop.setVisible(False) self.cmdMinimize.setVisible(False) self.cmdOK.setVisible(True) self.update() process_events() except: # Ignore exception closing model object? pass try: self.model_api.ENreport() self.model_api.ENclose() except: # Ignore exception closing model object? pass
def make_table(self, frm, title, time_index, row_headers, column_headers, property_items, requested_property_attributes, output_items, requested_output_attributes): frm.setWindowTitle(title) tbl = frm.tblGeneric tbl.setRowCount(len(row_headers)) tbl.setColumnCount(len(column_headers)) tbl.setHorizontalHeaderLabels(column_headers) tbl.setVerticalHeaderLabels(row_headers) # first set from properties row = 0 for this_item in property_items: col = 0 for selected_property_attribute in requested_property_attributes: if self.rbnNodes.isChecked(): meta_item = Junction.metadata.meta_item_of_label(selected_property_attribute) else: meta_item = Pipe.metadata.meta_item_of_label(selected_property_attribute) attribute = meta_item.attribute if attribute: # Found an attribute val_str = str(getattr(this_item, attribute, 0)) table_cell_widget = QTableWidgetItem(val_str) table_cell_widget.setFlags(QtCore.Qt.ItemIsSelectable) # | QtCore.Qt.ItemIsEnabled) table_cell_widget.setTextAlignment(QtCore.Qt.AlignVCenter | QtCore.Qt.AlignRight) tbl.setItem(row, col, table_cell_widget) col += 1 row += 1 if row == 20: tbl.resizeColumnsToContents() frm.show() frm.update() process_events() # now set from output values row = 0 for this_item in output_items: col = len(requested_property_attributes) values = this_item.get_all_attributes_at_time(self.output, time_index) for attribute in requested_output_attributes: val_str = attribute.str(values[attribute.index]) table_cell_widget = QTableWidgetItem(val_str) table_cell_widget.setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled) table_cell_widget.setTextAlignment(QtCore.Qt.AlignVCenter | QtCore.Qt.AlignRight) tbl.setItem(row, col, table_cell_widget) col += 1 row += 1 if row == 20: tbl.resizeColumnsToContents() frm.show() frm.update() process_events() # now remove the rows that fail the filter check rows_to_remove = [] for i in range(0, self.lstFilter.count()): word_list = self.lstFilter.item(i).text().split() val = word_list[len(word_list)-1] comp = word_list[len(word_list)-2] qty = '' for word_index in range(0, len(word_list)-2): qty += word_list[word_index] if word_index < len(word_list)-3: qty += ' ' pos = self.cboFilter.findText(qty) if pos > 0: # for each row of the grid for irow in range(0, tbl.rowCount()): remove_me = False value = float(tbl.item(irow, pos).text()) if comp == 'Below': if value >= float(val): remove_me = True elif comp == 'Equal To': if value != float(val): remove_me = True elif comp == 'Above': if value <= float(val): remove_me = True if remove_me and not irow in rows_to_remove: rows_to_remove.append(irow) # now go ahead and remove those rows removed_count = 0 for row_number in rows_to_remove: tbl.removeRow(int(row_number)-removed_count) removed_count += 1 for row in range(tbl.rowCount()): tbl.setRowHeight(row, 10)