Beispiel #1
0
def plot_snapshots(solutions, time_partition, field, label, **kwargs):
    """
    Plot a sequence of snapshots associated with
    ``solutions.field.label`` and :class:`TimePartition`
    ``time_partition``.

    Any keyword arguments are passed to ``tricontourf``.

    :arg solutions: :class:`AttrDict` of solutions
        computed by solving a forward or adjoint
        problem
    :arg time_partition: the :class:`TimePartition`
        object used to solve the problem
    :arg field: solution field of choice
    :arg label: choose from ``'forward'``, ``'forward_old'``
        ``'adjoint'`` and ``'adjoint_next'``
    """
    P = time_partition
    rows = P.exports_per_subinterval[0] - 1
    cols = P.num_subintervals
    fig, axes = plt.subplots(rows, cols, sharex="col", figsize=(6 * cols, 24 // cols))
    for i, sols_step in enumerate(solutions[field][label]):
        ax = axes[0] if cols == 1 else axes[0, i]
        ax.set_title(f"Mesh {i+1}")
        for j, sol in enumerate(sols_step):
            ax = axes[j] if cols == 1 else axes[j, i]
            tricontourf(sol, axes=ax, **kwargs)
            ax.annotate(
                f"t={i*P.end_time/cols + j*P.timesteps_per_export[i]*P.timesteps[i]:.2f}",
                (0.05, 0.05),
                color="white",
            )
    plt.tight_layout()
    return fig, axes
Beispiel #2
0
def test__plot_from_checkpoint(tmpdir):
    """Plot solution loaded from a checkpoint."""
    sim = sapphire.simulations.examples.heat_driven_cavity.Simulation(
        element_degrees=(1, 2, 2),
        mesh_dimensions=(40, 40),
        output_directory_path=tmpdir)

    sim.solution = sim.solve()

    sim.write_checkpoint()

    solution = fe.Function(function_space=sim.solution.function_space(),
                           name=sim.solution.name())

    states = [
        {
            "solution": solution,
            "time": sim.state["time"],
            "index": sim.state["index"]
        },
    ]

    states = sapphire.output.read_checkpoint(states=states,
                                             dirpath=tmpdir,
                                             filename="checkpoints")

    p, u, T = solution.split()

    fe.tricontourf(T)

    filepath = tmpdir + "/T.png"

    print("Writing plot to {}".format(filepath))

    matplotlib.pyplot.savefig(filepath)
Beispiel #3
0
def test__heat_driven_cavity_with_water(tmpdir):

    sim = sapphire.simulations.examples.\
        heat_driven_cavity_with_water.Simulation(
            mesh_dimensions = (20, 20),
            element_degrees = (1, 2, 2),
            output_directory_path = tmpdir)

    sim.solution = sim.solve_with_continuation_on_grashof_number()

    p, u, T = sim.solution.split()

    fe.tricontourf(T)

    fe.quiver(u)

    filepath = tmpdir + "/T_and_u.png"

    print("Writing plot to {}".format(filepath))

    matplotlib.pyplot.savefig(filepath)

    dot, grad = fe.dot, fe.grad

    ds = fe.ds(subdomain_id=sim.coldwall_id)

    nhat = fe.FacetNormal(sim.mesh)

    p, u, T = fe.split(sim.solution)

    coldwall_heatflux = fe.assemble(dot(grad(T), nhat) * ds)

    print("Integrated cold wall heat flux = {}".format(coldwall_heatflux))

    assert (round(coldwall_heatflux, 0) == -8.)
Beispiel #4
0
def tricontourf(function, *args, **kwargs):
    r"""Create a filled contour plot of a finite element field"""
    return firedrake.tricontourf(_project_to_2d(function), *args, **kwargs)