Skip to content

Velocity color gradient

Arrow3D

Bases: FancyArrowPatch

3D FancyArrowPatch proyection

Source code in phaseportrait/streamlines/velocity_color_gradient.py
11
12
13
14
15
16
17
18
19
20
21
22
23
class Arrow3D(FancyArrowPatch):
    """
    3D FancyArrowPatch proyection
    """
    def __init__(self, posA, posB, *args, **kwargs):
        super().__init__((0,0), (0,0), *args, **kwargs)
        self._verts3d = [[posA[i], posB[i]] for i in range(len(posA))]

    def do_3d_projection(self, renderer=None):
        xs3d, ys3d, zs3d = self._verts3d
        xs, ys, zs = proj3d.proj_transform(xs3d, ys3d, zs3d, self.axes.M)
        self.set_positions((xs[0],ys[0]),(xs[1],ys[1]))
        return np.min(zs)

Streamlines_Velocity_Color_Gradient

Streamlines

Creates trajectories given a dF function. Using Euler integrator.

Integrated in: - PhasePortrait2D

Source code in phaseportrait/streamlines/velocity_color_gradient.py
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
class Streamlines_Velocity_Color_Gradient:
    """
    Streamlines
    --------
    Creates trajectories given a `dF` function. Using Euler integrator.

    Integrated in:
    - PhasePortrait2D
    """



    def __init__(
        self, dF, X, Y, *Z, maxLen=500, dF_args=None, **kargs
    ):
        """
        Compute a set of streamlines given velocity function `dF`.

        Args:
            X (list, list[list]): arrays of the grid points. The mesh spacing is assumed to be uniform in each dimension.
            Y (list, list[list]): arrays of the grid points. The mesh spacing is assumed to be uniform in each dimension.
            maxLen (int default=500): The maximum length of an individual streamline segment.
            dF_args (dict|None default=None) : dF_args of `dF` function.
        """
        if not Z:
            self.proyection="2d"
            self.stream_base = Streamlines_base2D(dF, X, Y, maxLen, dF_args=dF_args, **kargs)
        else:
            self.proyection="3d"
            self.stream_base = Streamlines_base3D(dF, X, Y, Z[0], maxLen, dF_args=dF_args, **kargs)


    def _velocity_normalization(self):
        """ Returns a colour normalization function for colouring the stream lines. """
        vmax = None
        vmin = None

        for streamline in self.stream_base.streamlines:
            if streamline is not None:
                *_, v = streamline
                vmax = max(np.concatenate((v, ([vmax] if vmax is not None else []))))
                vmin = min(np.concatenate((v, ([vmin] if vmin is not None else []))))

        return Normalize(vmin, vmax)

    def plot(self, ax, cmap, cnorm, *, linewidth=None, arrowsize=1, arrowstyle='-|>'):
        # n_segments = 0
        # for streamline in self.stream_base.streamlines:
        #     x, *_ = streamline
        #     n_segments += len(x)



        if self.proyection == "2d":
            LineClass = LineCollection
            ArrowClass = FancyArrowPatch
        if self.proyection == "3d":
            LineClass = Line3DCollection
            ArrowClass = Arrow3D

        if linewidth is None:
            linewidth = matplotlib.rcParams['lines.linewidth']

        line_kw = {}
        arrow_kw = dict(arrowstyle=arrowstyle, mutation_scale=10 * arrowsize)

        for streamline in self.stream_base.streamlines:
            if streamline is None:
                continue
            coords, v = streamline
            if len(v) == 1:
                continue
            points = np.array(coords).reshape(-1 , 1, 2 if self.proyection=="2d" else 3)
            segments = np.concatenate([points[:-1], points[1:]], axis=1)

            s = np.sqrt(np.sum(np.square(points), 2))
            s = np.cumsum(s)
            n = np.searchsorted(s, s[-1] / 2.)

            arrows = []

            arrows_C = cmap(cnorm(v[n]))

            arrow_tail = points[n][0]
            arrow_head = np.mean(points[n:n + 2], 0)[0]

            arrows.append(ArrowClass(arrow_tail, arrow_head, color=arrows_C, **arrow_kw))

            C = cmap(cnorm(v))

            line = LineClass(segments, color=C, **line_kw)
            ax.add_collection(line)

            for a in arrows:
                ax.add_patch(a)

__init__(dF, X, Y, *Z, maxLen=500, dF_args=None, **kargs)

Compute a set of streamlines given velocity function dF.

Parameters:

Name Type Description Default
X list, list[list]

arrays of the grid points. The mesh spacing is assumed to be uniform in each dimension.

required
Y list, list[list]

arrays of the grid points. The mesh spacing is assumed to be uniform in each dimension.

required
maxLen int default=500

The maximum length of an individual streamline segment.

500
dF_args dict|None default=None)

dF_args of dF function.

None
Source code in phaseportrait/streamlines/velocity_color_gradient.py
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
def __init__(
    self, dF, X, Y, *Z, maxLen=500, dF_args=None, **kargs
):
    """
    Compute a set of streamlines given velocity function `dF`.

    Args:
        X (list, list[list]): arrays of the grid points. The mesh spacing is assumed to be uniform in each dimension.
        Y (list, list[list]): arrays of the grid points. The mesh spacing is assumed to be uniform in each dimension.
        maxLen (int default=500): The maximum length of an individual streamline segment.
        dF_args (dict|None default=None) : dF_args of `dF` function.
    """
    if not Z:
        self.proyection="2d"
        self.stream_base = Streamlines_base2D(dF, X, Y, maxLen, dF_args=dF_args, **kargs)
    else:
        self.proyection="3d"
        self.stream_base = Streamlines_base3D(dF, X, Y, Z[0], maxLen, dF_args=dF_args, **kargs)