Exemple #1
0
def test_pyswmm_exception():
    swmmobject = PySWMM(*get_model_files(MODEL_WEIR_SETTING_PATH))
    with pytest.raises(PYSWMMException):
        swmmobject.swmm_open()
        print(swmmobject.fileLoaded)
        swmmobject.swmm_open()
        swmmobject.swmm_close()
def test_nodes_4():
    swmmobject = PySWMM(*get_model_files(MODEL_WEIR_SETTING_PATH))
    swmmobject.swmm_open()

    print(swmmobject.getNodeParam('J2', tka.NodeParams.invertElev))
    swmmobject.setNodeParam('J2', tka.NodeParams.invertElev, 19)
    print(swmmobject.getNodeParam('J2', tka.NodeParams.invertElev))

    print(swmmobject.getLinkParam('C1:C2', tka.LinkParams.offset1))
    swmmobject.setLinkParam('C1:C2', tka.LinkParams.offset1, 19)
    print(swmmobject.getLinkParam('C1:C2', tka.LinkParams.offset1))

    print(swmmobject.getSubcatchParam('S2', tka.SubcParams.area))
    swmmobject.setSubcatchParam('S2', tka.SubcParams.area, 19)
    print(swmmobject.getSubcatchParam('S2', tka.SubcParams.area))

    swmmobject.swmm_start()
    i = 0
    while (True):
        time = swmmobject.swmm_stride(600)
        i += 1

        if (time <= 0.0):
            break

        if i % 144 == 0:
            print(i)

        swmmobject.setLinkParam('C1:C2', tka.LinkParams.qLimit, 0.25)

    swmmobject.swmm_end()
    swmmobject.swmm_close()
    print("swmm_step() Check Passed")
Exemple #3
0
def test_set_current_time():
    swmmobject = PySWMM(*get_model_files(MODEL_WEIR_SETTING_PATH))
    swmmobject.swmm_open()

    print(swmmobject.getSimulationDateTime(0))
    print(swmmobject.getSimulationDateTime(0) - timedelta(days=10))
    swmmobject.setSimulationDateTime(
        0,
        swmmobject.getSimulationDateTime(0) - timedelta(days=10),
    )
    print(swmmobject.getSimulationDateTime(0))

    print(swmmobject.getSimulationDateTime(1))
    print(swmmobject.getSimulationDateTime(1) - timedelta(days=10))
    swmmobject.setSimulationDateTime(
        1,
        swmmobject.getSimulationDateTime(1) - timedelta(days=10),
    )
    print(swmmobject.getSimulationDateTime(1))

    print(swmmobject.getSimulationDateTime(2))
    print(swmmobject.getSimulationDateTime(2) - timedelta(days=10))
    swmmobject.setSimulationDateTime(
        2,
        swmmobject.getSimulationDateTime(2) - timedelta(days=10),
    )
    print(swmmobject.getSimulationDateTime(2))

    swmmobject.swmm_start()

    i = 0
    while (True):
        time = swmmobject.swmm_stride(600)
        i += 1
        print(swmmobject.getCurrentSimulationTime())
        print(type(swmmobject.getCurrentSimulationTime()))

        if (time <= 0.0):
            break

        if i % 144 == 0:
            print(i)

    swmmobject.swmm_end()
    swmmobject.swmm_close()
Exemple #4
0
def test_current_time():
    swmmobject = PySWMM(*get_model_files(MODEL_WEIR_SETTING_PATH))
    swmmobject.swmm_open()
    date = swmmobject.getSimulationDateTime(0)
    print(date)
    swmmobject.swmm_start()

    i = 0
    while (True):
        time = swmmobject.swmm_stride(600)
        print(time)
        i += 1
        current_time = swmmobject.getCurrentSimualationTime()
        print(current_time)
        print(type(swmmobject.getCurrentSimualationTime()))

        if (time <= 0.0):
            break
        if i % 144 == 0:
            print(i)

    swmmobject.swmm_end()
    swmmobject.swmm_report()
    swmmobject.swmm_close()
def test_plot_weir_setting():
    swmmobject = PySWMM(*get_model_files(MODEL_WEIR_SETTING_PATH))
    swmmobject.swmm_open()
    swmmobject.swmm_start(True)

    fig = plt.figure()
    ax = fig.add_subplot(221)
    ax.set_ylabel('Flow Rate')
    line, = ax.plot([], [], label='C3')
    ax.grid()
    ax.legend()

    ax2 = fig.add_subplot(223, sharex=ax)
    ax2.set_ylabel('Setting')
    line2, = ax2.plot([], [], label='C3')
    ax2.grid()

    xdata, ydata = [], []
    ydata2 = []

    ax3 = fig.add_subplot(2, 2, (2, 4))
    arr_lena = read_png(IMAGE_WEIR_SETTING_PATH)
    imagebox = OffsetImage(arr_lena, zoom=0.45)
    ab = AnnotationBbox(
        imagebox,
        (0.5, 0.5),
        xybox=(0.5, 0.5),
        xycoords='data',
        boxcoords="offset points",
        pad=0.0,
    )
    ax3.add_artist(ab)
    ax3.axis('off')

    def data_gen(t=0):
        i = 0
        while (True):
            time = swmmobject.swmm_stride(300)
            i += 1
            if i == 80:
                swmmobject.setLinkSetting('C3', 0.9)
            elif i == 90:
                swmmobject.setLinkSetting('C3', 0.8)
            elif i == 100:
                swmmobject.setLinkSetting('C3', 0.7)
            elif i == 110:
                swmmobject.setLinkSetting('C3', 0.6)
            elif i == 120:
                swmmobject.setLinkSetting('C3', 0.5)
            elif i == 130:
                swmmobject.setLinkSetting('C3', 0.4)
            elif i == 140:
                swmmobject.setLinkSetting('C3', 0.3)
            elif i == 150:
                swmmobject.setLinkSetting('C3', 0.2)
            elif i == 160:
                swmmobject.setLinkSetting('C3', 0.1)
            elif i == 170:
                swmmobject.setLinkSetting('C3', 0.0)
            elif i == 220:
                swmmobject.setLinkSetting('C3', 1.0)

            if i > 0 and time == 0.0:
                break

            if i > 0 and time > 0:
                yield time

    def run(t):
        xdata.append(t)
        new_y = swmmobject.getLinkResult('C3', tka.LinkResults.newFlow)
        ydata.append(new_y)

        new_y2 = swmmobject.getLinkResult('C3', tka.LinkResults.setting)
        ydata2.append(new_y2)

        xmin, xmax = ax.get_xlim()
        ymin, ymax = ax.get_ylim()
        ymin2, ymax2 = ax2.get_ylim()

        # ax
        if new_y > ymax:
            ax.set_ylim(-0.1, 1.5 * ymax)

        if t >= xmax:
            ax.set_xlim(xmin, 1.5 * xmax)
            ax.figure.canvas.draw()
        line.set_data(xdata, ydata)

        # ax1
        if new_y2 > ymax2:
            ax2.set_ylim(-0.1, 1.2 * ymax2)
        line2.set_data(xdata, ydata2)

    ani = animation.FuncAnimation(fig,
                                  run,
                                  data_gen,
                                  blit=False,
                                  repeat=False,
                                  save_count=800,
                                  interval=10)

    show_fig = False
    if show_fig:
        plt.show()
    else:
        movie_path = os.path.join(DATA_PATH, "weir_setting.mp4")
        print(movie_path, ani)
        # ani.save(movie_path, fps=20, dpi=170, bitrate=50000)

    plt.close()

    swmmobject.swmm_end()
    swmmobject.swmm_close()
    print("Check Passed")
def test_plot_node_inflows():
    swmmobject = PySWMM(*get_model_files(MODEL_NODE_INFLOWS_PATH))
    swmmobject.swmm_open()
    swmmobject.swmm_start()

    fig = plt.figure()
    ax = fig.add_subplot(2, 3, (1, 2))
    ax.set_ylabel('Flow Rate')
    line, = ax.plot([], [], label='C3')
    ax.grid()
    ax.legend()

    ax2 = fig.add_subplot(2, 3, (4, 5), sharex=ax)
    ax2.set_ylabel('Toolkit API\nInflow')
    line2, = ax2.plot([], [], label='J1')
    ax2.grid()

    xdata, ydata = [], []
    ydata2 = []

    ax3 = fig.add_subplot(2, 3, (3, 6))

    arr_lena = read_png(IMAGE_NODE_INFLOWS_PATH)
    imagebox = OffsetImage(arr_lena, zoom=0.67)
    ab = AnnotationBbox(
        imagebox,
        (0.5, 0.5),
        xybox=(0.5, 0.5),
        xycoords='data',
        boxcoords="offset points",
        pad=0.0,
    )

    ax3.add_artist(ab)
    ax3.axis('off')

    def data_gen(t=0):
        i = 0
        while (True):
            if i >= 80:
                swmmobject.setNodeInflow('J1', random.randint(1, 5))
            time = swmmobject.swmm_stride(1500)
            i += 1

            if i > 0 and time == 0.0:
                break
            if i > 0 and time > 0:
                yield time

    def run(t):
        xdata.append(t)
        new_y = swmmobject.getLinkResult('C2', tka.LinkResults.newFlow)
        ydata.append(new_y)

        new_y2 = swmmobject.getNodeResult('J1', tka.NodeResults.newLatFlow)
        ydata2.append(new_y2)

        xmin, xmax = ax.get_xlim()
        ymin, ymax = ax.get_ylim()
        ymin2, ymax2 = ax2.get_ylim()

        # ax
        if new_y > ymax:
            ax.set_ylim(-0.1, 1.5 * ymax)
        if t >= xmax:
            ax.set_xlim(xmin, 1.5 * xmax)
            ax.figure.canvas.draw()
        line.set_data(xdata, ydata)

        # ax1
        if new_y2 > ymax2:
            ax2.set_ylim(-0.1, 1.2 * ymax2)
        line2.set_data(xdata, ydata2)

    ani = animation.FuncAnimation(
        fig,
        run,
        data_gen,
        blit=False,
        repeat=False,
        save_count=800,
        interval=10,
    )

    show_figure = False
    if show_figure:
        plt.show()
    else:
        movie_path = os.path.join(DATA_PATH, "node_inflows.mp4")
        print(movie_path, ani)
        # ani.save(movie_path, fps=20, dpi=170, bitrate=50000)
    plt.close()

    swmmobject.swmm_end()
    swmmobject.swmm_report()
    swmmobject.swmm_close()
    print("Check Passed")
Exemple #7
0
def test_warning():
    swmmobject = PySWMM(*get_model_files(MODEL_WEIR_SETTING_PATH))
    swmmobject.swmm_open()
    swmmobject.getLinkResult('C2', 0)
    swmmobject.swmm_close()