{ "cells": [ { "cell_type": "markdown", "id": "5b7dbabf", "metadata": {}, "source": [ "# Tutorial 7: Work with Groups" ] }, { "cell_type": "markdown", "id": "8899e69a", "metadata": {}, "source": [ "This Tutorial demonstrates how to reassign element to differnt groups and create new elements and directley assign them to groups. For more advanced group operations see: [Tutorial 81](https://3sconsult.github.io/sir3stoolkit/examples.html#tutorial-81-groups)" ] }, { "cell_type": "markdown", "id": "a271e499-a4e8-4cf5-a510-7008fc501fa9", "metadata": {}, "source": [ "## SIR 3S Installation" ] }, { "cell_type": "code", "execution_count": 1, "id": "9241d47b-641e-4c68-b8f8-adad4806d086", "metadata": {}, "outputs": [], "source": [ "SIR3S_SIRGRAF_DIR = r\"C:\\3S\\SIR 3S\\SirGraf-90-15-00-21_Quebec-Upd2\" #change to local path" ] }, { "cell_type": "markdown", "id": "7887be8d", "metadata": {}, "source": [ "## Imports" ] }, { "cell_type": "markdown", "id": "bbfb80b5", "metadata": {}, "source": [ "Note: The SIR 3S Toolkit requires the Sir3S_Toolkit.dll included in SIR 3S installations (version Quebec and higher)." ] }, { "cell_type": "code", "execution_count": 2, "id": "48372080", "metadata": {}, "outputs": [], "source": [ "import sir3stoolkit" ] }, { "cell_type": "markdown", "id": "e12d61e9", "metadata": {}, "source": [ "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." ] }, { "cell_type": "code", "execution_count": 3, "id": "f1baf699", "metadata": {}, "outputs": [], "source": [ "from sir3stoolkit.core import wrapper" ] }, { "cell_type": "code", "execution_count": 4, "id": "89da1276", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sir3stoolkit" ] }, { "cell_type": "markdown", "id": "6d982834", "metadata": {}, "source": [ "The [wrapper package](https://3sconsult.github.io/sir3stoolkit/references/sir3stoolkit.core.html#sir3stoolkit.core.wrapper.Initialize_Toolkit) has to be initialized with reference to a SIR 3S (SirGraf) installation." ] }, { "cell_type": "code", "execution_count": 5, "id": "817b0ebf", "metadata": {}, "outputs": [], "source": [ "wrapper.Initialize_Toolkit(SIR3S_SIRGRAF_DIR)" ] }, { "cell_type": "markdown", "id": "0352dd43", "metadata": {}, "source": [ "## Initialization" ] }, { "cell_type": "markdown", "id": "e14ffe06", "metadata": {}, "source": [ "The SIR 3S Toolkit contains two classes: [SIR3S_Model](https://3sconsult.github.io/sir3stoolkit/references/sir3stoolkit.core.html#sir3stoolkit.core.wrapper.SIR3S_Model) (model and data) and [SIR3S_View](https://3sconsult.github.io/sir3stoolkit/references/sir3stoolkit.core.html#sir3stoolkit.core.wrapper.SIR3S_View) (depiction in SIR Graf). All SIR 3S Toolkit functionality is accessed via the methods of these classes." ] }, { "cell_type": "code", "execution_count": 6, "id": "7e40a5af", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Initialization complete\n" ] } ], "source": [ "s3s = wrapper.SIR3S_Model()" ] }, { "cell_type": "code", "execution_count": 7, "id": "3770c93f", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Initialization complete\n" ] } ], "source": [ "s3s_view = wrapper.SIR3S_View()" ] }, { "cell_type": "markdown", "id": "34309a14", "metadata": {}, "source": [ "# Open Model" ] }, { "cell_type": "code", "execution_count": 8, "id": "0918738e", "metadata": {}, "outputs": [], "source": [ "dbFilePath=r\"Toolkit_Tutorial7_Model.db3\"" ] }, { "cell_type": "code", "execution_count": 9, "id": "e984b61e", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Model is open for further operation\n" ] } ], "source": [ "s3s.OpenModel(dbName=dbFilePath,\n", " providerType=s3s.ProviderTypes.SQLite,\n", " Mid=\"M-1-0-1\",\n", " saveCurrentlyOpenModel=False,\n", " namedInstance=\"\",\n", " userID=\"\",\n", " password=\"\")" ] }, { "cell_type": "code", "execution_count": 10, "id": "8e3adb92", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "NetworkType.Water\n" ] } ], "source": [ "print(s3s.GetNetworkType()) # to check that the correct model is responsive, model we are trying to open was created with type Undefined" ] }, { "cell_type": "markdown", "id": "1f2c3cb8", "metadata": {}, "source": [ "# View existing Groups and their content" ] }, { "cell_type": "markdown", "id": "4173b54e", "metadata": {}, "source": [ "## Finding and accessing groups" ] }, { "cell_type": "code", "execution_count": 11, "id": "6f381c71", "metadata": {}, "outputs": [], "source": [ "group_tks=s3s.GetTksofElementType(s3s.ObjectTypes.LAYR_Layer)" ] }, { "cell_type": "code", "execution_count": 12, "id": "7d03c90f", "metadata": {}, "outputs": [], "source": [ "props = s3s.GetPropertiesofElementType(s3s.ObjectTypes.LAYR_Layer)" ] }, { "cell_type": "code", "execution_count": 13, "id": "5a043cb1", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['Name',\n", " 'Zeigen',\n", " 'Setzen',\n", " 'Lfdnr',\n", " 'Idreferenz',\n", " 'ObjsString',\n", " 'Tk',\n", " 'Pk',\n", " 'InVariant']" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "props" ] }, { "cell_type": "code", "execution_count": 14, "id": "fe0dc9c3", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Tk: 4999103085880923178, Name: Vorlauf, ObjsString: KNOT~4968737254281413660\tKNOT~5130290893660176749\tROHR~5316743965501398998\tKNOT~4906600779019373637\t\n", "Tk: 5238820536187712277, Name: Rücklauf, ObjsString: KNOT~5470637040092884723\tROHR~5736978569585339616\t\n" ] } ], "source": [ "for tk in group_tks:\n", " print(f\"Tk: {tk}, Name: {s3s.GetValue(tk, props[0])[0]}, ObjsString: {s3s.GetValue(tk, props[5])[0]}\")" ] }, { "cell_type": "markdown", "id": "825de658", "metadata": {}, "source": [ "We can see that this model only has the groups Vorlauf (Supply) and Rücklauf (Return). Both containing nodes and pipes." ] }, { "cell_type": "markdown", "id": "71c85114", "metadata": {}, "source": [ "## ObjsString" ] }, { "cell_type": "markdown", "id": "fe47f2ad", "metadata": {}, "source": [ "As we can see the ObjsString has an odd format. We will turn it into a python list." ] }, { "cell_type": "code", "execution_count": 15, "id": "342dcaa8", "metadata": {}, "outputs": [], "source": [ "supply_object_string = s3s.GetValue(group_tks[0], \"ObjsString\")[0]" ] }, { "cell_type": "code", "execution_count": 16, "id": "c06bc431", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'KNOT~4968737254281413660\\tKNOT~5130290893660176749\\tROHR~5316743965501398998\\tKNOT~4906600779019373637\\t'" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "supply_object_string" ] }, { "cell_type": "code", "execution_count": 17, "id": "b676f222", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "str" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(supply_object_string)" ] }, { "cell_type": "code", "execution_count": 18, "id": "d324a298", "metadata": {}, "outputs": [], "source": [ "return_object_string = s3s.GetValue(group_tks[1], \"ObjsString\")[0]" ] }, { "cell_type": "code", "execution_count": 19, "id": "951f1d1a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'KNOT~5470637040092884723\\tROHR~5736978569585339616\\t'" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "return_object_string" ] }, { "cell_type": "markdown", "id": "ef1b7220", "metadata": {}, "source": [ "### Change format" ] }, { "cell_type": "code", "execution_count": 20, "id": "ddae3a47", "metadata": {}, "outputs": [], "source": [ "supply_elements_tks = []\n", "for element in supply_object_string.split('\\t'):\n", " if element: \n", " obj_type, obj_id = element.split('~')\n", " supply_elements_tks.append((obj_type, obj_id))\n" ] }, { "cell_type": "code", "execution_count": 21, "id": "8993d429", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[('KNOT', '4968737254281413660'),\n", " ('KNOT', '5130290893660176749'),\n", " ('ROHR', '5316743965501398998'),\n", " ('KNOT', '4906600779019373637')]" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "supply_elements_tks" ] }, { "cell_type": "markdown", "id": "a82013c3", "metadata": {}, "source": [ "Now we have a list of tuples with the element type and the tk of the object. We keep the element type, because if we want to insert of the elements into another group, we need to know it. With this we could continue into more complex python operations, but that is out of scope of this tutorial. see: [Tutorial 81](https://3sconsult.github.io/sir3stoolkit/examples.html#tutorial-81-groups)" ] }, { "cell_type": "markdown", "id": "1f519902", "metadata": {}, "source": [ "# Edit " ] }, { "cell_type": "markdown", "id": "9a499e6c", "metadata": {}, "source": [ "In this model there is a defect, the last node in the supply group actually belongs to the return lets fix this." ] }, { "cell_type": "code", "execution_count": 22, "id": "dda924e3", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'KNOT~4968737254281413660\\tKNOT~5130290893660176749\\tROHR~5316743965501398998\\tKNOT~4906600779019373637\\t'" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "supply_object_string" ] }, { "cell_type": "code", "execution_count": 23, "id": "f6930393", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'KNOT~5470637040092884723\\tROHR~5736978569585339616\\t'" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "return_object_string" ] }, { "cell_type": "markdown", "id": "44d03ff4", "metadata": {}, "source": [ "For such short groups and simple operations it is the easiest to just reset the manually edited object strings." ] }, { "cell_type": "code", "execution_count": 24, "id": "76604ec3", "metadata": {}, "outputs": [], "source": [ "supply_object_string = 'KNOT~4968737254281413660\\tKNOT~5130290893660176749\\tROHR~5316743965501398998\\t'" ] }, { "cell_type": "code", "execution_count": 25, "id": "e8bb3e87", "metadata": {}, "outputs": [], "source": [ "return_object_string = 'KNOT~5470637040092884723\\tROHR~5736978569585339616\\tKNOT~4906600779019373637\\t'" ] }, { "cell_type": "code", "execution_count": 26, "id": "463ec78a", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Value is set\n" ] } ], "source": [ "s3s.SetValue(group_tks[0], \"ObjsString\", supply_object_string)" ] }, { "cell_type": "code", "execution_count": 27, "id": "ebdf55b1", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Value is set\n" ] } ], "source": [ "s3s.SetValue(group_tks[1], \"ObjsString\", return_object_string)" ] }, { "cell_type": "markdown", "id": "4bdd2384", "metadata": {}, "source": [ "# Insert newly added elements into groups" ] }, { "cell_type": "markdown", "id": "a29cd6df", "metadata": {}, "source": [ "If we want to determine the groups elements will be inserted into when newly edited, we can set the attribute \"Setzen\" of the groups, we want them to be added to, to 1." ] }, { "cell_type": "markdown", "id": "9f3aa747", "metadata": {}, "source": [ "## Add a node to Vorlauf (supply) group" ] }, { "cell_type": "code", "execution_count": 28, "id": "97647a11", "metadata": {}, "outputs": [], "source": [ "groups_to_add_to = group_tks[0] # Vorlauf (supply) group" ] }, { "cell_type": "code", "execution_count": 29, "id": "5b46cd7b", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Value is set\n", "Value is set\n" ] } ], "source": [ "for tk in group_tks:\n", " if tk in groups_to_add_to:\n", " s3s.SetValue(tk, \"Setzen\", \"1\")\n", " else:\n", " s3s.SetValue(tk, \"Setzen\", \"0\")" ] }, { "cell_type": "code", "execution_count": 30, "id": "e6feb592", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Element inserted successfully into the model with Tk: 4919077416104559424\n" ] }, { "data": { "text/plain": [ "'4919077416104559424'" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s3s.InsertElement(ElementType=s3s.ObjectTypes.Node, IdRef=\"-1\") # Newly create element" ] }, { "cell_type": "code", "execution_count": 31, "id": "2ec830a1", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Tk: 4999103085880923178, Name: Vorlauf, ObjsString: KNOT~4968737254281413660\tKNOT~5130290893660176749\tROHR~5316743965501398998\tKNOT~4919077416104559424\t\n", "Tk: 5238820536187712277, Name: Rücklauf, ObjsString: KNOT~5470637040092884723\tROHR~5736978569585339616\tKNOT~4906600779019373637\t\n" ] } ], "source": [ "for tk in group_tks:\n", " print(f\"Tk: {tk}, Name: {s3s.GetValue(tk, props[0])[0]}, ObjsString: {s3s.GetValue(tk, props[5])[0]}\")" ] }, { "cell_type": "markdown", "id": "bc7f3f07", "metadata": {}, "source": [ "## Add a node to Vorlauf (supply) and Rücklauf (return) group" ] }, { "cell_type": "code", "execution_count": 32, "id": "5c8c56a8", "metadata": {}, "outputs": [], "source": [ "groups_to_add_to = group_tks # Vorlauf (supply) and Rücklauf (return) group" ] }, { "cell_type": "code", "execution_count": 33, "id": "bee1e5f6", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Value is set\n", "Value is set\n" ] } ], "source": [ "for tk in group_tks:\n", " if tk in groups_to_add_to:\n", " s3s.SetValue(tk, \"Setzen\", \"1\")\n", " else:\n", " s3s.SetValue(tk, \"Setzen\", \"0\")" ] }, { "cell_type": "code", "execution_count": 34, "id": "bf0b5d79", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Element inserted successfully into the model with Tk: 4695903655171808515\n" ] }, { "data": { "text/plain": [ "'4695903655171808515'" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s3s.InsertElement(ElementType=s3s.ObjectTypes.Node, IdRef=\"-1\") # Newly create element" ] }, { "cell_type": "code", "execution_count": 35, "id": "6e55682f", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Tk: 4999103085880923178, Name: Vorlauf, ObjsString: KNOT~4968737254281413660\tKNOT~5130290893660176749\tROHR~5316743965501398998\tKNOT~4919077416104559424\tKNOT~4695903655171808515\t\n", "Tk: 5238820536187712277, Name: Rücklauf, ObjsString: KNOT~5470637040092884723\tROHR~5736978569585339616\tKNOT~4906600779019373637\tKNOT~4695903655171808515\t\n" ] } ], "source": [ "for tk in group_tks:\n", " print(f\"Tk: {tk}, Name: {s3s.GetValue(tk, props[0])[0]}, ObjsString: {s3s.GetValue(tk, props[5])[0]}\")" ] }, { "cell_type": "code", "execution_count": 36, "id": "6fafdeff", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s3s.CloseModel(False)" ] } ], "metadata": { "kernelspec": { "display_name": "base", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.8" } }, "nbformat": 4, "nbformat_minor": 5 }