Displays — triflow 0.4.2 documentation

The notebook is available via nbviewer, and we recommend to download and launch it as it contains interactive and jupyter-specific display.

# Displays

    In [1]:

    from triflow import Model, Simulation
    import numpy as np

    model = Model("dxxU", "U")
    parameters = dict(periodic=False)

    x = np.linspace(0, 10, 50, endpoint=True)
    U = x ** 2

## displays objects

The displays are objects with a `__call__` method. They can be add to a simulation with the `Simulation.add_display` method, or just called at every step during the simulation.

A “null” display will be written as

    In [2]:

    class NullDisplay():
        def __init__(self, simul):
            pass

        def __call__(self, t, fields):
            pass

this display will not change the simulation behavior.

A very simple display could be a one printing the time after each step

    In [3]:

    class TimeDisplay():
        def __init__(self, simul):
            pass

        def __call__(self, t, fields):
            print(f"simulation time: {t:g}")

And can be used with

    In [4]:

    t = 0
    fields = model.fields_template(x=x, U=U)
    simul = Simulation(model, t, fields, parameters,
                       dt=10, tmax=35, tol=1E-1)

    display = TimeDisplay(simul)

    display(t, fields)
    for t, fields in simul:
        display(t, fields)

    simulation time: 0
    simulation time: 10
    simulation time: 20
    simulation time: 30
    simulation time: 40

or

    In [5]:

    t = 0
    fields = model.fields_template(x=x, U=U)
    simul = Simulation(model, t, fields, parameters,
                       dt=10, tmax=35, tol=1E-1)

    simul.add_display(TimeDisplay)

    for t, fields in simul:
        pass

    simulation time: 0
    simulation time: 10
    simulation time: 20
    simulation time: 30
    simulation time: 40

## built-in displays

### bokeh displays

#### field display

This display allow a real-time plot in a jupyter notebook. The `keys` argument allow to choose which fields will be plotted. All fields are plotted on specific figure, and the `line_kwargs` and `fig_kwargs` allow us to customize each bokeh figure and line.

    In [6]:

    from triflow import Model, Simulation, displays
    import numpy as np

    model = Model(["dxxU", "dxxV"], ["U", "V"])
    parameters = dict(periodic=False)

    x = np.linspace(0, 10, 50, endpoint=True)
    U = x ** 2
    V = x ** 2

    fields = model.fields_template(x=x, U=U, V=V)
    simul = Simulation(model, 0, fields, parameters,
                       dt=1, tmax=50, tol=1E-1)

    display = displays.bokeh_fields_update(simul, keys=["U", "V"],
                                           fig_kwargs={"U":
                                                       {"width": 600,
                                                        "height": 200,
                                                        "x_range": (0, 10),
                                                        "y_range": (0, 100)},
                                                       "V":
                                                       {"width": 600,
                                                        "height": 200}},
                                           line_kwargs={"U":
                                                        {"color": "darkred",
                                                         "line_alpha": .8}})

    for t, fields in simul:
        display(t, fields)

Loading BokehJS ...

#### probe display

The same way, this display give the possibility to plot in real time a probe, a 0D post process data.

The probes are given as a dictionary with the name of the probe as key and a callable as value. This callable take `(t, fields)` as argument and return a scalar. Like the fields display, it is possible to customize the bokeh figure and line via two dictionnary.

    In [7]:

    from triflow import Model, Simulation, displays
    import numpy as np

    model = Model("dxxU", "U")
    parameters = dict(periodic=False)

    x = np.linspace(0, 10, 50, endpoint=True)
    U = x ** 2

    fields = model.fields_template(x=x, U=U)
    simul = Simulation(model, 0, fields, parameters,
                       dt=1, tmax=50, tol=1E-1)

    def std_probe(t, fields):
        return np.std(fields.U)

    display = displays.bokeh_probes_update(simul,
                                           {"std": std_probe},
                                           fig_kwargs={'std': {"width": 600,
                                                               "height": 200}})

    for t, fields in simul:
        display(t, fields)

Loading BokehJS ...