Tutorial 6: Miscellaneous
This Tutorial demonstrates miscellaneous functions of the SIR3S_Model() class that cannot be assigned to one of the previous Tutorial topics.
SIR 3S Installation
[1]:
SIR3S_SIRGRAF_DIR = r"C:\3S\SIR 3S\SirGraf-90-15-00-20x64_Quebec-Upd1" #change to local path
Imports
Note: The SIR 3S Toolkit requires the Sir3S_Toolkit.dll included in SIR 3S installations (version Quebec and higher).
[2]:
import sir3stoolkit
The core of sir3stoolkit is a Python wrapper around basic functionality of SIR 3S, offering a low-level access to the creation, modification and simulation of SIR 3S models. In the future pure python subpackages may be added.
[3]:
from sir3stoolkit.core import wrapper
[4]:
sir3stoolkit
[4]:
<module 'sir3stoolkit' from 'C:\\Users\\aUsername\\3S\\sir3stoolkit\\src\\sir3stoolkit\\__init__.py'>
The wrapper package has to be initialized with reference to a SIR 3S (SirGraf) installation.
[5]:
wrapper.Initialize_Toolkit(SIR3S_SIRGRAF_DIR)
Initialization
The SIR 3S Toolkit contains two classes: SIR3S_Model (model and data) and SIR3S_View (depiction in SIR Graf). All SIR 3S Toolkit functionality is accessed via the methods of these classes.
[6]:
s3s = wrapper.SIR3S_Model()
Initialization complete
[7]:
s3s_view = wrapper.SIR3S_View()
Initialization complete
Open Model
[8]:
dbFilePath=r"Toolkit_Tutorial6_Model.db3"
[9]:
s3s.OpenModel(dbName=dbFilePath,
providerType=s3s.ProviderTypes.SQLite,
Mid="M-1-0-1",
saveCurrentlyOpenModel=False,
namedInstance="",
userID="",
password="")
Model is open for further operation
Now the model has been opened and the previous one has been closed without saving it. All SIR 3S Toolkit commands now apply to this model until another one is opened.
[10]:
print(s3s.GetNetworkType()) # to check that the model is responsive
NetworkType.DistrictHeating
GetTableRows()
We can use the GetTableRows() function to obtain the tks of rows belonging to the table with a given tk.
[11]:
for tk in s3s.GetTksofElementType(s3s.ObjectTypes.PipeTable):
print(tk + ": " + s3s.GetValue(tk, "Name")[0])
5703793333632021763: STDROHR
5016622236907997495: FLEXWELL-F
5072515793845307207: KMR-Daemmr1
4628032391580498906: KMR-Daemmr2
5343398249541580160: KMR-Daemmr3
5358720719472309931: SMR
5207173582970272729: Kellerltg
5567057697892033945: Freileitung
Let’s focus on the STDROHR table
[12]:
tk_STDROHR_table = "5703793333632021763"
[13]:
rows = s3s.GetTableRows(tablePkTk=tk_STDROHR_table)
[14]:
rows[0]
[14]:
<System.String[] object at 0x000002ADF8E3FF40>
We get a .NET array of strings returned, we need to transform it into a python list.
[15]:
row_tks = list(rows[0])
Now, we have the tks of all rows of this table.
We can reconstruct the table via pandas as follows.
[16]:
import pandas as pd
[17]:
properties_rows=s3s.GetPropertiesofElementType(s3s.ObjectTypes.PipeTable_Row)
[18]:
data = {
tk: {prop: s3s.GetValue(tk, prop)[0] for prop in properties_rows}
for tk in row_tks
}
[19]:
df = pd.DataFrame.from_dict(data, orient='index')
[20]:
df.head(5)
[20]:
| Name | Fk | Dn | Di | Da | S | Wsteig | Wtiefe | Kt | Pn | Ausfallzeit | Reparatur | Rehabilitation | Tk | Pk | InVariant | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 5606026146967010825 | DN 3 | 5703793333632021763 | 3 | 3 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 5606026146967010825 | 5606026146967010825 | False |
| 5709880459701024427 | DN 4 | 5703793333632021763 | 4 | 4 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 5709880459701024427 | 5709880459701024427 | False |
| 5653919630703523685 | DN 5 | 5703793333632021763 | 5 | 5 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 5653919630703523685 | 5653919630703523685 | False |
| 5518872245540208156 | DN 6 | 5703793333632021763 | 6 | 6 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 5518872245540208156 | 5518872245540208156 | False |
| 5281245134223674519 | DN 8 | 5703793333632021763 | 8 | 8 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 5281245134223674519 | 5281245134223674519 | False |
AddTableRow()
We can use the AddTableRow() function to add a new row to the bottom of any table.
[21]:
new_table_row_tk = s3s.AddTableRow(tk_STDROHR_table)[0]
Row is added to the table with Tk: 4709308787444856320
Now we can use the already known SetValue() function to propagate the row with the necessary values.
[22]:
s3s.GetPropertiesofElementType(s3s.ObjectTypes.PipeTable_Row)
[22]:
['Name',
'Fk',
'Dn',
'Di',
'Da',
'S',
'Wsteig',
'Wtiefe',
'Kt',
'Pn',
'Ausfallzeit',
'Reparatur',
'Rehabilitation',
'Tk',
'Pk',
'InVariant']
[23]:
s3s.SetValue(new_table_row_tk, "Name", "DN 60")
Value is set
[24]:
s3s.SetValue(new_table_row_tk, "Fk", "5207173582970272729")
Value is set
[25]:
s3s.SetValue(new_table_row_tk, "Dn", "60")
Value is set
… and so on. Then save your changes.
[26]:
#s3s.SaveChanges()
GetHydraulicProfileObjectString()
We can use the function GetHydraulicProfileObjectString() to obtain a string containing all pipe tks part of the longitudinal section.
[27]:
s3s.GetTksofElementType(s3s.ObjectTypes.AGSN_HydraulicProfile)
[27]:
['4999575369425665759']
[28]:
s3s.GetHydraulicProfileObjectString(tkAgsn="4999575369425665759")
[28]:
(True, '\tROHR~5222487322443009246\tROHR~5354178785470006283\n')
We can us regex to get clean list of the pipe tks.
[29]:
import re
[30]:
text = s3s.GetHydraulicProfileObjectString(tkAgsn="4999575369425665759")[1]
[31]:
tks = re.findall(r'ROHR~(\d+)', text)
[32]:
tks
[32]:
['5222487322443009246', '5354178785470006283']
GetCourseOfHydraulicProfile()
We can use the function GetCourseOfHydraulicProfile() to obtain a detailed output for longitudinal sections including branch considerations.
[33]:
hydraulicProfile = s3s.GetCourseOfHydraulicProfile(tkAgsn="4999575369425665759", uid="0")
[34]:
hydraulicProfile
[34]:
hydraulicProfile(childrenUID=<System.Collections.Generic.List[String] object at 0x000002ADFD5663C0>, nodesVL=<System.Collections.Generic.List[String] object at 0x000002ADFD56B100>, linksVL=<System.Collections.Generic.List[String] object at 0x000002ADFD56B140>, xVL=<System.Collections.Generic.List[Double] object at 0x000002ADFD56B980>, nodesRL=<System.Collections.Generic.List[String] object at 0x000002ADFD56B9C0>, linksRL=<System.Collections.Generic.List[String] object at 0x000002ADFD56BA00>, xRL=<System.Collections.Generic.List[Double] object at 0x000002ADFD56BA40>, nrOfBranches=0, xOffSet=0.0, xOffsetRelativeToParent=0.0, length=493.5, tkArticulationNode='-1')
If we have more than one branch in our section we can use the uid parameter to differentiate between them. Otherwise “0” is sufficient.
As the lists that are part of the returned tuple remain NET objets we have to turn them into python lists.
[35]:
nodesVL = list(hydraulicProfile.nodesVL)
[36]:
nodesVL
[36]:
['4905524959962565243', '5517957656259932533', '4852293679410056753']
[37]:
linksVL = list(hydraulicProfile.linksVL)
[38]:
linksVL
[38]:
['5222487322443009246', '5354178785470006283']
[39]:
xVL = list(hydraulicProfile.xVL)
[40]:
xVL
[40]:
[0.0, 316.43719482421875, 493.4965362548828]
[41]:
hydraulicProfile.nrOfBranches
[41]:
0
[42]:
hydraulicProfile.length
[42]:
493.5
SetLogFilePath()
We can use the function SetLogFilePath() to change where the log file, created when SIR 3S executes calculations, is saved.
[43]:
s3s.SetLogFilePath("C:/Users/aUsername/Documents")
Error: DirectoryNotFoundException: Ein Teil des Pfades "C:\Users\aUsername\Documents" konnte nicht gefunden werden.
[43]:
False
EnableOrDisableOutputComments()
We can use the function EnableOrDisableOutputComments() to turn output comments on or off. Default is True.
[44]:
s3s.EnableOrDisableOutputComments(outputComments=True)
[45]:
print(s3s.InsertElement(s3s.ObjectTypes.Node, "-1"))
Element inserted successfully into the model with Tk: 5545345590912933449
5545345590912933449
[46]:
s3s.EnableOrDisableOutputComments(outputComments=False)
[47]:
print(s3s.InsertElement(s3s.ObjectTypes.Node, "-1"))
5629265237157425666
As you can see, after the output comments were disabled the function does not print an output. The printed tk is the return value of the function that is displayed in Jupyter Notebooks.
[48]:
s3s.EnableOrDisableOutputComments(outputComments=True)
AllowSirMessageBox()
We can use the function AllowSirMessageBox() to allow or diallow SIR DB Messages to pop up. Default is True. Generally, not recommeded to turn off, as it could mean missing out on deep error messages.
[49]:
s3s.AllowSirMessageBox(bAllow=True)
[50]:
dbFilePath2=r"Toolkit_Tutorial6_PotsdamVersionModel.db3"
We will try to open a model created in Potsdam Version of SIR 3S. A message window should pop up that asks whether you want the model to be migrated to your current SIR 3S version. Please choose to not migrate. The cell will fail to execute.
[51]:
s3s.OpenModel(dbName=dbFilePath2,
providerType=s3s.ProviderTypes.SQLite,
Mid="M-1-0-1",
saveCurrentlyOpenModel=False,
namedInstance="",
userID="",
password="")
---------------------------------------------------------------------------
SQLiteException Traceback (most recent call last)
Cell In[51], line 1
----> 1 s3s.OpenModel(dbName=dbFilePath2,
2 providerType=s3s.ProviderTypes.SQLite,
3 Mid="M-1-0-1",
4 saveCurrentlyOpenModel=False,
5 namedInstance="",
6 userID="",
7 password="")
File ~\3S\sir3stoolkit\src\sir3stoolkit\core\wrapper.py:391, in SIR3S_Model.OpenModel(self, dbName, providerType, Mid, saveCurrentlyOpenModel, namedInstance, userID, password)
367 """
368 Opens a model from a database file.
369
(...)
388 :description: This is a wrapper method for OpenModel() from toolkit; Watch out for errors for more information.
389 """
390 providerType_net = self.to_dotnet_enum(providerType)
--> 391 result, error = self.toolkit.OpenModel(dbName, providerType_net, Mid, saveCurrentlyOpenModel,
392 namedInstance, userID, password)
393 if result:
394 if self.outputComments:
SQLiteException: code = Error (1), message = System.Data.SQLite.SQLiteException (0x800007BF): SQL logic error
no such table: USCH
bei System.Data.SQLite.SQLite3.Prepare(SQLiteConnection cnn, String strSql, SQLiteStatement previous, UInt32 timeoutMS, String& strRemain)
bei System.Data.SQLite.SQLiteCommand.BuildNextCommand()
bei System.Data.SQLite.SQLiteDataReader.NextResult()
bei System.Data.SQLite.SQLiteDataReader..ctor(SQLiteCommand cmd, CommandBehavior behave)
bei System.Data.SQLite.SQLiteCommand.ExecuteReader(CommandBehavior behavior)
bei System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
bei System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
bei System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, String srcTable)
bei Sir3S_Repository.DataBaseModel.CDbTableTypeA.FillDataSet(DbDataAdapter dbDataAdapter, String fkBa, String fkVa, String fkBz, Int32& iTables)
bei Sir3S_Repository.DataBaseModel.CDataBase.FillDataSet(DbDataAdapter dbDataAdapter, IModel iModel)
bei Sir3S_Repository.DataSetModel.CModelDs.Open(IModel iModel, Boolean& modelWasInUse)
bei Sir3S_Repository.ModelManager.CModelManagerModelListEvents.OpenModel(IDialog iDialog, IModelInfo iModelInfo, Boolean& modelWasInUse)
bei Sir3S_Repository.ModelManager.CModelManager.OpenModel(IModel Model, Boolean& modelWasInUse)
bei Sir3S_Toolkit.Model.CSir3SToolkitModel.OpenModel(String dbName, SirDBProviderType providerType, String Mid, Boolean saveCurrentlyOpenModel, String namedInstance, String userID, String password, String& error)
Now we will attempt the same but with message boxes turned off.
[52]:
s3s.AllowSirMessageBox(bAllow=False)
[53]:
s3s.OpenModel(dbName=dbFilePath2,
providerType=s3s.ProviderTypes.SQLite,
Mid="M-1-0-1",
saveCurrentlyOpenModel=False,
namedInstance="",
userID="",
password="")
Model is open for further operation
As you can see in your file explorer this time the model was migrated without asking. In general during model creation/opening the message boxes should be left on.
You have finished all Tutorials on the SIR3S_Model class of the SIR 3S Toolkit. The SIR3S_View class does not yet have Tutorials.
Feel free to check out the Examples.