From 62a18cdeb2ecc415adbca1865272c8f65189ec50 Mon Sep 17 00:00:00 2001 From: kaklise Date: Mon, 13 Jan 2025 10:18:30 -0800 Subject: [PATCH] updated model dev demo --- .../model_development_v2.ipynb | 107 +++++++++++------- 1 file changed, 69 insertions(+), 38 deletions(-) diff --git a/examples/demos/.virtual_documents/model_development_v2.ipynb b/examples/demos/.virtual_documents/model_development_v2.ipynb index 75527ef3..39b80db7 100644 --- a/examples/demos/.virtual_documents/model_development_v2.ipynb +++ b/examples/demos/.virtual_documents/model_development_v2.ipynb @@ -39,7 +39,6 @@ wntr.network.io.write_geojson(wn0, "../data/ky4", crs=crs) -# Option 1. Create a WaterNetworkModel from the GeoJSON files geojson_files = { "junctions": "../data/ky4_junctions.geojson", "tanks": "../data/ky4_tanks.geojson", @@ -51,31 +50,6 @@ geojson_files = { wn = wntr.network.read_geojson(geojson_files) -# Option 2, Create a WaterNetworkModel from GeoDataFrames -junctions = gpd.read_file("../data/ky4_junctions.geojson", crs=crs) -tanks = gpd.read_file("../data/ky4_tanks.geojson", crs=crs) -reservoirs = gpd.read_file("../data/ky4_reservoirs.geojson", crs=crs) -pipes = gpd.read_file("../data/ky4_pipes.geojson", crs=crs) -pumps = gpd.read_file("../data/ky4_pumps.geojson", crs=crs) - -junctions.set_index("name", inplace=True) -tanks.set_index("name", inplace=True) -reservoirs.set_index("name", inplace=True) -pipes.set_index("name", inplace=True) -pumps.set_index("name", inplace=True) - -gis_data = wntr.gis.WaterNetworkGIS( - { - "junctions": junctions, - "tanks": tanks, - "reservoirs": reservoirs, - "pipes": pipes, - "pumps": pumps, - } -) -wn = wntr.network.from_gis(gis_data) - - @@ -124,6 +98,11 @@ pressure = results.node["pressure"].loc[0, :] wntr.graphics.plot_network(wn, node_attribute=pressure, node_size=30, title="Pressure") +wn0.describe() +wn1.describe() +# compare RMSE pressure at a specific time + + @@ -134,14 +113,18 @@ wntr.graphics.plot_network(wn, node_attribute=pressure, node_size=30, title="Pre diconnected_pipes = gpd.read_file("../data/ky4_disconnected_pipes.geojson", crs=crs) -diconnected_tanks = gpd.read_file("../data/ky4_tanks.geojson", crs=crs) -diconnected_reservoirs = gpd.read_file("../data/ky4_reservoirs.geojson", crs=crs) -diconnected_pumps = gpd.read_file("../data/ky4_pumps.geojson", crs=crs) +tanks = gpd.read_file("../data/ky4_tanks.geojson", crs=crs) +reservoirs = gpd.read_file("../data/ky4_reservoirs.geojson", crs=crs) +pumps = gpd.read_file("../data/ky4_pumps.geojson", crs=crs) diconnected_pipes.set_index("index", inplace=True) # This needs to be updated to name -diconnected_tanks.set_index("name", inplace=True) -diconnected_reservoirs.set_index("name", inplace=True) -diconnected_pumps.set_index("name", inplace=True) +tanks.set_index("name", inplace=True) +reservoirs.set_index("name", inplace=True) +pumps.set_index("name", inplace=True) + +# TODO remove start_node_name end_node_name and pumps -- not sure how we add these back in, but I don't think the names should exist here +del pumps['start_node_name'] +del pumps['end_node_name'] # Define distance threshold used to refine data distance_threshold = 100.0 @@ -151,7 +134,9 @@ distance_threshold = 100.0 pipes, junctions = wntr.gis.geospatial.connect_lines(diconnected_pipes, distance_threshold) +print(pipes.head()) +# TODO, change to plotly or interactive so we can zoom in fig, ax = plt.subplots() diconnected_pipes.plot(color="b", linewidth=4, ax=ax) pipes.plot(color="r", linewidth=2, ax=ax) @@ -161,16 +146,57 @@ junctions.plot(color="k", ax=ax) +# TODO use sample_raster function junctions["elevation"] = 100 -print(junctions) +print(junctions.head()) + + + + + +snap_reservoirs = wntr.gis.snap(reservoirs, junctions, distance_threshold) +print(reservoirs.head()) +print(snap_reservoirs) + +snap_tanks = wntr.gis.snap(tanks, junctions, distance_threshold) +print(tanks.head()) +print(snap_tanks) +# TODO, how do we add the pump? snap to the nearest pipe? why is the pump a point? does this work with the perfect data above? +snap_pumps = wntr.gis.snap(pumps, junctions, distance_threshold) +print(pumps.head()) +print(snap_pumps) -reservoirs = wntr.gis.snap(diconnected_reservoirs, junctions, distance_threshold) -tanks = wntr.gis.snap(diconnected_tanks, junctions, distance_threshold) -pumps = wntr.gis.snap(diconnected_pumps, junctions, distance_threshold) + +def add_connector(snap_attribute, pipes): + for name, row in snap_attribute.iterrows(): + attributes = {'CV': 0, + 'diameter': 0.3, + 'status': 'Open', + 'length': 1, + 'minor_loss': 0, + 'roughness': 150, + 'geometry': row['geometry'], + 'start_node_name': row['node'], + 'end_node_name': name} + pipes.loc[name+'_connector'] = attributes + return pipes + +add_connector(snap_reservoirs, pipes) +add_connector(snap_tanks, pipes) + + + + + +# TODO +# For each pump +# Determine upstream direction using the graph +# Add upstream junction (junction_upstream) +# Add start_node_name and end_node_name to pump @@ -186,6 +212,8 @@ snap_buildings = wntr.gis.snap(buildings, junctions, distance_threshold) buildings["junction"] = None buildings.loc[snap_buildings.index, "junction"] = snap_buildings.loc[:, "node"] +print(buildings.head()) + @@ -193,8 +221,6 @@ buildings.loc[snap_buildings.index, "junction"] = snap_buildings.loc[:, "node"] -# build the wn_gis object -# TODO, add back in tanks, reservoirs, and pumps gis_data = wntr.gis.WaterNetworkGIS( { "junctions": junctions, @@ -259,3 +285,8 @@ sim = wntr.sim.EpanetSimulator(wn) results = sim.run_sim() pressure = results.node["pressure"].loc[0, :] wntr.graphics.plot_network(wn, node_attribute=pressure, node_size=30, title="Pressure") + + +wn0.describe() +wn2.describe() +# compare RMSE pressure at a specific time