Beispiel #1
0
def test_reshape():
    a = np.array([
        [1, 2, 3],
        [4, 5, 6],
        [7, 8, 9],
        [10, 11, 12],
    ])
    b = cas.DM(a)

    test_inputs = [
        -1,
        (4, 3),
        (3, 4),
        (12, 1),
        (1, 12),
        (-1),
        (12, -1),
        (-1, 12),
    ]

    for i in test_inputs:
        ra = np.reshape(a, i)
        rb = np.reshape(b, i)
        if len(ra.shape) == 1:
            ra = ra.reshape(-1, 1)

        assert np.all(ra == rb)
Beispiel #2
0
    def draw(
        self,
        scalar_to_plot:
        str = "potential",  # "potential", "streamfunction", "xvel", "yvel", "velmag", "Cp"
        x_points: np.ndarray = np.linspace(-10, 10, 400),
        y_points: np.ndarray = np.linspace(-10, 10, 300),
        percentiles_to_include=99.7,
        show=True,
    ):
        X, Y = np.meshgrid(x_points, y_points)
        X_r = np.reshape(X, -1)
        Y_r = np.reshape(Y, -1)
        points = np.vstack((X_r, Y_r)).T

        if scalar_to_plot == "potential":
            scalar_to_plot_value = sum(
                [object.get_potential_at(points) for object in self.objects])
        elif scalar_to_plot == "streamfunction":
            scalar_to_plot_value = sum([
                object.get_streamfunction_at(points) for object in self.objects
            ])
        elif scalar_to_plot == "xvel":
            scalar_to_plot_value = sum(
                [object.get_x_velocity_at(points) for object in self.objects])
        elif scalar_to_plot == "yvel":
            scalar_to_plot_value = sum(
                [object.get_y_velocity_at(points) for object in self.objects])
        elif scalar_to_plot == "velmag":
            x_vels = sum(
                [object.get_x_velocity_at(points) for object in self.objects])
            y_vels = sum(
                [object.get_y_velocity_at(points) for object in self.objects])
            scalar_to_plot_value = np.sqrt(x_vels**2 + y_vels**2)
        elif scalar_to_plot == "Cp":
            x_vels = sum(
                [object.get_x_velocity_at(points) for object in self.objects])
            y_vels = sum(
                [object.get_y_velocity_at(points) for object in self.objects])
            V = np.sqrt(x_vels**2 + y_vels**2)
            scalar_to_plot_value = 1 - V**2
        else:
            raise ValueError("Bad value of `scalar_to_plot`!")

        min = np.nanpercentile(scalar_to_plot_value,
                               50 - percentiles_to_include / 2)
        max = np.nanpercentile(scalar_to_plot_value,
                               50 + percentiles_to_include / 2)

        contour(x_points,
                y_points,
                scalar_to_plot_value.reshape(X.shape),
                levels=np.linspace(min, max, 80),
                linelabels=False,
                cmap=plt.get_cmap("rainbow"),
                contour_kwargs={
                    "linestyles": 'solid',
                    "alpha": 0.4
                })
        plt.gca().set_aspect("equal", adjustable='box')
        show_plot(f"Potential Flow: {scalar_to_plot}", "$x$", "$y$", show=show)
def convert_mesh_to_polydata_format(
        points,
        faces
):
    """
    Pyvista uses a slightly different convention for the standard (points, faces) format as described above. They
    give `faces` as a single 1D vector of roughly length (M*3), or (M*4) in the case of quadrilateral meshing.
    Basically, the mesh displayer goes down the `faces` array, and when it sees a number N, it interprets that as the
    number of vertices in the following face. Then, the next N entries are interpreted as integer references to the
    vertices of the face.

    This has the benefit of allowing for mixed tri/quad meshes.

    Args:
        points: `points` array of the original standard-format mesh
        faces: `faces` array of the original standard-format mesh

    Returns:

        (points, faces), except that `faces` is now in a pyvista.PolyData compatible format.

    """
    faces = [
        [len(face), *face]
        for face in faces
    ]
    faces = np.array(faces)
    faces = np.reshape(faces, -1)
    return points, faces
 def reshape(value):
     try:
         return np.reshape(value, shape_for_reshaping)
     except ValueError:
         if isinstance(value, int) or isinstance(value, float) or value.shape == tuple() or np.product(
                 value.shape) == 1:
             return value * np.ones(shape_for_reshaping)
     raise ValueError("Could not reshape value of one of the inputs!")
    def __call__(self, x):
        if isinstance(self.x_data_coordinates, dict):
            def get_shape(value):
                try:
                    return value.shape
                except AttributeError:
                    return tuple()

            shape = np.broadcast_shapes(
                *[get_shape(v) for v in x.values()]
            )
            shape_for_reshaping = (int(np.product(shape)),)

            def reshape(value):
                try:
                    return np.reshape(value, shape_for_reshaping)
                except ValueError:
                    if isinstance(value, int) or isinstance(value, float) or value.shape == tuple() or np.product(
                            value.shape) == 1:
                        return value * np.ones(shape_for_reshaping)
                raise ValueError("Could not reshape value of one of the inputs!")

            x = np.stack(tuple(
                reshape(x[k])
                for k, v in self.x_data_coordinates.items()
            ))

        output = np.interpn(
            points=self.x_data_coordinates_values,
            values=self.y_data_structured,
            xi=x,
            method=self.method,
            bounds_error=False,  # Can't be set true if general MX-type inputs are to be expected.
            fill_value=self.fill_value
        )
        try:
            return np.reshape(output, shape)
        except UnboundLocalError:
            return output
def convert_mesh_to_polydata_format(points, faces):
    faces = [[len(face), *face] for face in faces]
    faces = np.array(faces)
    faces = np.reshape(faces, -1)
    return points, faces
Beispiel #7
0
 def tall(array):
     return np.reshape(array, (-1, 1))
Beispiel #8
0
 def wide(array):
     return np.reshape(array, (1, -1))
Beispiel #9
0
        x_field=Xf,
        y_field=Yf,
        z_field=Zf,
        x_left=left[0],
        y_left=left[1],
        z_left=left[2],
        x_right=right[0],
        y_right=right[1],
        z_right=right[2],
        gamma=1,
    )

    pos = np.stack((Xf, Yf, Zf)).T
    dir = np.stack((Uf, Vf, Wf)).T

    dir_norm = np.reshape(np.linalg.norm(dir, axis=1), (-1, 1))

    dir = dir / dir_norm * dir_norm ** 0.2

    import pyvista as pv

    pv.set_plot_theme('dark')
    plotter = pv.Plotter()
    plotter.add_arrows(
        cent=pos,
        direction=dir,
        mag=0.15
    )
    plotter.add_lines(
        lines=np.array([
            [Xf.max(), left[1], left[2]],