Skip to content

Commit

Permalink
simpler column_stack
Browse files Browse the repository at this point in the history
  • Loading branch information
nschloe committed Oct 5, 2021
1 parent f769d9a commit d9657d4
Show file tree
Hide file tree
Showing 11 changed files with 28 additions and 36 deletions.
2 changes: 1 addition & 1 deletion logo/logo.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,5 @@ def create_logo2(y=0.0):
mesh = meshio.Mesh(X, {"triangle": cells})
meshio.svg.write("logo.svg", mesh, image_width=300)

X = np.column_stack([X[:, 0], X[:, 1], np.zeros(X.shape[0])])
X = np.column_stack([X, np.zeros_like(X[:, 0])])
meshio.Mesh(X, {"triangle": cells}).write("logo.vtk")
4 changes: 1 addition & 3 deletions src/meshio/avsucd/_avsucd.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,7 @@ def write(filename, mesh):
"AVS-UCD requires 3D points, but 2D points given. "
"Appending 0 third component."
)
mesh.points = np.column_stack(
[mesh.points[:, 0], mesh.points[:, 1], np.zeros(mesh.points.shape[0])]
)
mesh.points = np.column_stack([mesh.points, np.zeros_like(mesh.points[:, 0])])

with open_file(filename, "w") as f:
# Write meshio version
Expand Down
5 changes: 2 additions & 3 deletions src/meshio/gmsh/_gmsh22.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,9 +315,8 @@ def write(filename, mesh, float_fmt=".16e", binary=True):
def _write_nodes(fh, points, float_fmt, binary):
if points.shape[1] == 2:
# msh2 requires 3D points, but 2D points given. Appending 0 third component.
points = np.column_stack(
[points[:, 0], points[:, 1], np.zeros(points.shape[0])]
)
points = np.column_stack([points, np.zeros_like(points[:, 0])])

fh.write(b"$Nodes\n")
fh.write(f"{len(points)}\n".encode())
if binary:
Expand Down
4 changes: 1 addition & 3 deletions src/meshio/gmsh/_gmsh40.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,9 +272,7 @@ def write(filename, mesh, float_fmt=".16e", binary=True):

def _write_nodes(fh, points, float_fmt, binary):
if points.shape[1] == 2:
points = np.column_stack(
[points[:, 0], points[:, 1], np.zeros(points.shape[0])]
)
points = np.column_stack([points, np.zeros_like(points[:, 0])])

fh.write(b"$Nodes\n")

Expand Down
4 changes: 1 addition & 3 deletions src/meshio/gmsh/_gmsh41.py
Original file line number Diff line number Diff line change
Expand Up @@ -555,9 +555,7 @@ def _write_nodes(fh, points, cells, point_data, float_fmt, binary):
if points.shape[1] == 2:
# msh4 requires 3D points, but 2D points given.
# Appending 0 third component.
points = np.column_stack(
[points[:, 0], points[:, 1], np.zeros(points.shape[0])]
)
points = np.column_stack([points, np.zeros_like(points[:, 0])])

fh.write(b"$Nodes\n")

Expand Down
8 changes: 4 additions & 4 deletions src/meshio/mdpa/_mdpa.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,9 +444,9 @@ def write(filename, mesh, float_fmt=".16e", binary=False):
"mdpa requires 3D points, but 2D points given. "
"Appending 0 third component."
)
mesh.points = np.column_stack(
[mesh.points[:, 0], mesh.points[:, 1], np.zeros(mesh.points.shape[0])]
)
points = np.column_stack([mesh.points, np.zeros_like(mesh.points[:, 0])])
else:
points = mesh.points

# Kratos cells are mostly ordered like VTK, with a few exceptions:
cells = mesh.cells.copy()
Expand Down Expand Up @@ -515,7 +515,7 @@ def write(filename, mesh, float_fmt=".16e", binary=False):
break

# identify entities
_write_nodes(fh, mesh.points, float_fmt, binary)
_write_nodes(fh, points, float_fmt, binary)
_write_elements_and_conditions(fh, cells, tag_data, binary, dimension)
for name, dat in mesh.point_data.items():
_write_data(fh, "NodalData", name, dat, binary)
Expand Down
2 changes: 1 addition & 1 deletion src/meshio/nastran/_nastran.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ def write(filename, mesh, point_format="fixed-large", cell_format="fixed-small")
"Nastran requires 3D points, but 2D points given. "
"Appending 0 third component."
)
points = np.column_stack([mesh.points, np.zeros(mesh.points.shape[0])])
points = np.column_stack([mesh.points, np.zeros_like(mesh.points[:, 0])])
else:
points = mesh.points

Expand Down
11 changes: 5 additions & 6 deletions src/meshio/off/_off.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ def write(filename, mesh):
"OFF requires 3D points, but 2D points given. "
"Appending 0 as third component."
)
mesh.points = np.column_stack(
[mesh.points[:, 0], mesh.points[:, 1], np.zeros(mesh.points.shape[0])]
)
points = np.column_stack([mesh.points, np.zeros_like(mesh.points[:, 0])])
else:
points = mesh.points

skip = [c for c in mesh.cells if c.type != "triangle"]
if skip:
Expand All @@ -79,9 +79,8 @@ def write(filename, mesh):

# vertices
# np.savetxt(fh, mesh.points, "%r") # slower
out = mesh.points
fmt = " ".join(["{}"] * out.shape[1])
out = "\n".join([fmt.format(*row) for row in out]) + "\n"
fmt = " ".join(["{}"] * points.shape[1])
out = "\n".join([fmt.format(*row) for row in points]) + "\n"
fh.write(out.encode())

# triangles
Expand Down
8 changes: 4 additions & 4 deletions src/meshio/permas/_permas.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,17 +226,17 @@ def write(filename, mesh):
"PERMAS requires 3D points, but 2D points given. "
"Appending 0 third component."
)
mesh.points = np.column_stack(
[mesh.points[:, 0], mesh.points[:, 1], np.zeros(mesh.points.shape[0])]
)
points = np.column_stack([mesh.points, np.zeros_like(mesh.points[:, 0])])
else:
points = mesh.points

with open_file(filename, "wt") as f:
f.write("!PERMAS DataFile Version 18.0\n")
f.write(f"!written by meshio v{__version__}\n")
f.write("$ENTER COMPONENT NAME=DFLT_COMP\n")
f.write("$STRUCTURE\n")
f.write("$COOR\n")
for k, x in enumerate(mesh.points):
for k, x in enumerate(points):
f.write(f"{k + 1} {x[0]} {x[1]} {x[2]}\n")
eid = 0
tria6_order = [0, 3, 1, 4, 2, 5]
Expand Down
8 changes: 4 additions & 4 deletions src/meshio/stl/_stl.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,11 @@ def write(filename, mesh, binary=False):
logging.warning(
"STL requires 3D points, but 2D points given. Appending 0 third component."
)
mesh.points = np.column_stack(
[mesh.points[:, 0], mesh.points[:, 1], np.zeros_like(mesh.points[:, 1])]
)
points = np.column_stack([mesh.points, np.zeros_like(mesh.points[:, 0])])
else:
points = mesh.points

pts = mesh.points[mesh.get_cells_type("triangle")]
pts = points[mesh.get_cells_type("triangle")]
if "facet_normals" in mesh.cell_data:
normals = mesh.get_cell_data("facet_normals", "triangle")
else:
Expand Down
8 changes: 4 additions & 4 deletions src/meshio/vtu/_vtu.py
Original file line number Diff line number Diff line change
Expand Up @@ -622,9 +622,9 @@ def write(filename, mesh, binary=True, compression="zlib", header_type=None):
"VTU requires 3D points, but 2D points given. "
"Appending 0 third component."
)
mesh.points = np.column_stack(
[mesh.points[:, 0], mesh.points[:, 1], np.zeros(mesh.points.shape[0])]
)
points = np.column_stack([mesh.points, np.zeros_like(mesh.points[:, 0])])
else:
points = mesh.points

vtk_file = ET.Element(
"VTKFile",
Expand Down Expand Up @@ -653,7 +653,7 @@ def write(filename, mesh, binary=True, compression="zlib", header_type=None):
# swap the data to match the system byteorder
# Don't use byteswap to make sure that the dtype is changed; see
# <https://github.com/numpy/numpy/issues/10372>.
points = mesh.points.astype(mesh.points.dtype.newbyteorder("="), copy=False)
points = points.astype(points.dtype.newbyteorder("="), copy=False)
for k, (cell_type, data) in enumerate(mesh.cells):
# Treatment of polyhedra is different from other types
if is_polyhedron_grid:
Expand Down

0 comments on commit d9657d4

Please sign in to comment.