Exemplo n.º 1
0
    def step_output(self, n, ei, **kwargs):
        nondims = calc_Fr_CFL_Bern(self)
        if nondims['CFL'].max() >= 0.95:
            self.snapshot_figure()
            self.stop = True
            raise Exception("CFL too high: %.3f" % (nondims['CFL'].max()))

        self.max_d_uj = self.max_d_eta = 0.0
        if self.last_uj is not None:
            self.max_d_uj = np.abs((self.uj - self.last_uj) / self.dt).max()
        if self.last_ei is not None:
            self.max_d_eta = np.abs((self.ei - self.last_ei) / self.dt).max()
        self.last_uj = self.uj.copy()
        self.last_ei = self.ei.copy()

        self.history = utils.array_append(self.history)
        self.history['eta_var'][-1] = np.var(self.ei)
        phi = nondims['phi'][(nondims['x'] > 1) & (nondims['x'] < 19)]
        self.history['phi_range'][-1] = phi.max() - phi.min()
        self.history['t'][-1] = self.t

        Qbc = self.W * self.upstream_flow
        Qcalc = (self.uj * self.aj)[self.intern]
        self.history['Q_max_error'][-1] = np.abs(Qbc - Qcalc).max()

        plot_interval = 1.0
        if self.t - self.last_plot < plot_interval:
            return
        self.last_plot = self.t
Exemplo n.º 2
0
def run_low_flow(advection='no_adv'):
    """
    bump test case, all subcritical.
    """
    sim = SwampyBump1D(
        cg_tol=1e-10,
        # nx=int(20/0.5),dt=0.5,
        nx=int(20 / 0.10),
        dt=0.10,
        upstream_flow=0.05)
    if advection == 'no_adv':
        sim.get_fu = sim.get_fu_no_adv
    elif advection == 'orig':
        sim.get_fu = sim.get_fu_orig
    else:
        raise Exception("Bad advection: " + advection)

    sim.set_grid()
    sim.set_initial_conditions()
    sim.set_bcs()
    sim.prepare_to_run()

    c, j_Q, Q = sim.bcs[1].cell_edge_flow()

    assert np.all(sim.aj[j_Q] > 0.0)

    sim.run_until(t_end=2000)

    assert np.all(sim.aj[j_Q] > 0.0)

    V = calc_Fr_CFL_Bern(sim)
    assert V['Fr'].max() < 1.0, "Should be entirely subcritical"
    # coarse grid, no advection, I get delta of 0.0061 here
    # so 0.002 is reasonable, though not a really strong check
    # on advection.
    # Discard the ends -- maybe once momentum is advected at
    # boundaries this won't be a problem

    phi = V['phi'][(V['x'] > 1) & (V['x'] < 19)]

    adv_passes = (phi.max() - phi.min()) < 0.002
    print("phi max-min", V['phi'].max() - V['phi'].min())
    if advection == 'no_adv':
        assert not adv_passes, "Bernoulli function should be variable w/o advection"
    else:
        assert adv_passes, "Bernoulli function is too variable"

    Q = (sim.uj * sim.aj)[sim.intern]
    assert np.all(
        np.abs(sim.W * sim.upstream_flow - Q) < 0.002), "Flow not uniform"

    # basic test that center_vel is not crazy
    # approximate comparison between edge velocity and the cell-centered
    # x-velocity averaged from the adjacent cells
    ui_at_j = V['ui'][sim.grd.edges['cells'][sim.intern, :]].mean(axis=1)
    delta = sim.uj[sim.intern] - ui_at_j
    ui_rel_difference = np.mean(np.abs(delta)) / np.mean(
        abs(sim.uj[sim.intern]))
    # was 0.014 in a quick test
    assert ui_rel_difference < 0.05
Exemplo n.º 3
0
    def snapshot_figure(self, **kwargs):
        """
        Plot current model state with solution
        """
        def stop(event, self=self):
            self.stop = True

        if self.snap is None:
            self.snap = {}
            fig = self.snap['fig'] = plt.figure(4)
            fig.clf()
            self.snap['ax'] = fig.add_subplot(3, 1, 1)
            self.snap['ax2'] = fig.add_subplot(3, 1, 2)
            axstop = fig.add_axes([0.8, 0.93, 0.09, 0.06])
            self.snap['btn'] = Button(axstop, 'Stop')
            self.snap['btn'].on_clicked(stop)

            self.snap['ax'].plot(self.grd.cells_center()[:, 0],
                                 self.zi_agg['min'],
                                 'b-o',
                                 ms=3)
            self.snap['t_label'] = self.snap['ax'].text(
                0.05, 0.85, 'time', transform=self.snap['ax'].transAxes)

            self.snap['ax_t'] = fig.add_subplot(3, 1, 3)

        ax = self.snap['ax']
        del ax.lines[1:]
        ax.plot(self.grd.cells_center()[:, 0], self.ei, 'g-')
        self.snap['t_label'].set_text("%.4f" % self.t)
        # add analytical or measured.

        ax2 = self.snap['ax2']
        ax2.cla()
        nondims = calc_Fr_CFL_Bern(self)
        ax2.plot(nondims['x'], nondims['Fr'], label='Fr')
        ax2.plot(nondims['x'], nondims['CFL'], label='CFL')
        ax.plot(
            nondims['x'],
            nondims['phi'],  # / nondims['phi'].mean(),
            'r-',
            label='phi')
        ax2.axis(ymin=0, ymax=1.05)
        ax2.legend(loc='upper right')

        ax = self.snap['ax_t']
        ax.cla()
        ax.plot(self.history['t'], self.history['eta_var'], label='var(eta)')
        ax.plot(self.history['t'],
                self.history['Q_max_error'],
                label='Q nonuniform')
        ax.plot(self.history['t'],
                self.history['phi_range'],
                label='max(phi)-min(phi)')
        ax.legend(loc='upper right')
        return self.snap['fig']
Exemplo n.º 4
0
    def step_output(self, n, ei, **kwargs):
        nondims = calc_Fr_CFL_Bern(self)
        if nondims['CFL'].max() >= 0.95:
            self.snapshot_figure()
            self.stop = True
            raise Exception("CFL too high: %.3f" % (nondims['CFL'].max()))

        plot_interval = 1.0
        if self.t - self.last_plot < plot_interval:
            return
        self.last_plot = self.t
Exemplo n.º 5
0
    def snapshot_figure(self, **kwargs):
        """
        Plot current model state with solution
        """
        def stop(event, self=self):
            self.stop = True

        if self.snap is None:
            self.snap = {}
            fig = self.snap['fig'] = plt.figure(3)
            fig.clf()
            self.snap['ax'] = fig.add_subplot(3, 1, 1)
            self.snap['ax_u'] = fig.add_subplot(3, 1, 2)
            self.snap['ax_nondim'] = fig.add_subplot(3, 1, 3)
            axstop = fig.add_axes([0.8, 0.93, 0.09, 0.06])
            self.snap['btn'] = Button(axstop, 'Stop')
            self.snap['btn'].on_clicked(stop)

            self.snap['ax'].plot(self.grd.cells_center()[:, 0],
                                 self.zi_agg['mean'],
                                 'b-o',
                                 ms=3)

            ui = self.get_center_vel(self.uj)
            self.snap['ax_u'].plot(self.grd.cells_center()[:, 0], ui, 'b-o')

            self.snap['t_label'] = self.snap['ax'].text(
                0.05, 0.85, 'time', transform=self.snap['ax'].transAxes)

        ax = self.snap['ax']
        del ax.lines[1:]
        ax.plot(self.grd.cells_center()[:, 0], self.ei, 'g-')
        self.snap['t_label'].set_text("%.4f" % self.t)
        # add analytical or measured.

        del self.snap['ax_u'].lines[0:]
        ui = self.get_center_vel(self.uj)
        self.snap['ax_u'].plot(self.grd.cells_center()[:, 0], ui[:, 0], 'b-o')

        ax2 = self.snap['ax_nondim']
        ax2.cla()
        nondims = calc_Fr_CFL_Bern(sim)
        ax2.plot(nondims['x'], nondims['Fr'], 'o-', label='Fr')
        ax2.plot(nondims['x'], nondims['CFL'], 'o-', label='CFL')
        ax2.legend(loc='upper right')
        return self.snap['fig']
Exemplo n.º 6
0
def test_med_flow():
    """
    Bump test case, small hydraulic jump.
    Just tests for stability, and that flow has 
    sub - super - sub critical transitions
    """
    # used to be dt=0.04, but that violates CFL
    # condition.
    # 0.02 is stable, if slow.
    sim = SwampyBump1D(cg_tol=1e-10, dt=0.02, upstream_flow=0.10)
    sim.set_grid()
    sim.set_initial_conditions()
    sim.set_bcs()
    sim.run(t_end=50)

    Fr = calc_Fr_CFL_Bern(sim)['Fr']
    assert Fr[0] < 1.0, "Inflow is not subcritical"
    assert Fr[-1] < 1.0, "Outflow is not subcritical"
    assert Fr.max() > 1.0, "Did not reach supercritical"