Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def read(filename):
"""Reads a KratosMultiphysics mdpa file.
"""
# if (have_kratos is True): # TODO: Implement natively
# pass
# else:
with open_file(filename, "rb") as f:
mesh = read_buffer(f)
return mesh
def write(filename, mesh, float_fmt=".16e", binary=False):
"""Write FLAC3D f3grid grid file."""
if not any(c.type in meshio_only.keys() for c in mesh.cells):
raise WriteError("FLAC3D format only supports 3D cells")
mode = "wb" if binary else "w"
with open_file(filename, mode) as f:
if binary:
f.write(
struct.pack("<2I", 1375135718, 3)
) # Don't know what these values represent
else:
f.write("* FLAC3D grid produced by meshio v{}\n".format(version))
f.write("* {}\n".format(time.ctime()))
_write_points(f, mesh.points, binary, float_fmt)
_write_cells(f, mesh.points, mesh.cells, binary)
_write_zgroups(f, mesh.cell_data, mesh.field_data, binary)
if binary:
f.write(struct.pack("<2I", 0, 0)) # No face and face group
def read(filename): # noqa: C901
# Initialize the data optional data fields
field_data = {}
cell_data = {}
point_data = {}
points = []
cells = []
first_point_index_overall = None
last_point_index = None
# read file in binary mode since some data might be binary
with open_file(filename, "rb") as f:
while True:
line = f.readline().decode("utf-8")
if not line:
break
if line.strip() == "":
continue
# expect the line to have the form
# ( [...]
out = re.match("\\s*\\(\\s*([0-9]+).*", line)
if not out:
raise ReadError()
index = out.group(1)
if index == "0":
def write(filename, mesh):
file_type = determine_file_type(filename)
with open_file(filename, "w") as f:
_write_buffer(f, file_type, mesh)
def read(filename):
with open_file(filename, "r") as f:
out = read_buffer(f)
return out
def write(filename, mesh, binary=True): # noqa: C901
with open_file(filename, "wb") as fh:
fh.write(b"ply\n")
fh.write(b"comment Created by meshio\n")
if binary:
fh.write(
"format binary_{}_endian 1.0\n".format(sys.byteorder).encode("utf-8")
)
else:
fh.write(b"format ascii 1.0\n")
# counts
fh.write("element vertex {:d}\n".format(mesh.points.shape[0]).encode("utf-8"))
#
dim_names = ["x", "y", "z"]
# From :
#
for k, v in mesh.cell_data.items():
if k not in {"X", "Y", "Z", "x", "y", "z"}:
v = numpy.concatenate([v[ic] for ic in cell_blocks])
if v.ndim == 1:
variables += [k]
data += [v]
varrange[1] += 1
elif v.ndim == 2:
for i, vv in enumerate(v.T):
variables += [f"{k}_{i}"]
data += [vv]
varrange[1] += 1
else:
logging.warning(f"Skipping cell data '{k}'.")
with open_file(filename, "w") as f:
# Title
f.write(f'TITLE = "Written by meshio v{version}"\n')
# Variables
variables_str = ", ".join(f'"{var}"' for var in variables)
f.write(f"VARIABLES = {variables_str}\n")
# Zone record
num_nodes = len(mesh.points)
num_cells = sum(len(mesh.cells[ic].data) for ic in cell_blocks)
f.write(f"ZONE NODES = {num_nodes}, ELEMENTS = {num_cells},\n")
f.write(f"DATAPACKING = BLOCK, ZONETYPE = {zone_type}")
if varrange[0] < varrange[1]:
f.write(",\n")
varlocation_str = (
f"{varrange[0]}"
def read(filename):
"""Reads a Abaqus inp file.
"""
with open_file(filename, "r") as f:
out = read_buffer(f)
return out
)
mesh.point_data[name] = pad(values)
for name, data in mesh.cell_data.items():
for k, values in enumerate(data):
if len(values.shape) == 2 and values.shape[1] == 2:
logging.warning(
"VTK requires 3D vectors, but 2D vectors given. "
"Appending 0 third component to {}.".format(name)
)
data[k] = pad(data[k])
if not binary:
logging.warning("VTK ASCII files are only meant for debugging.")
with open_file(filename, "wb") as f:
f.write(b"# vtk DataFile Version 4.2\n")
f.write("written by meshio v{}\n".format(__version__).encode("utf-8"))
f.write(("BINARY\n" if binary else "ASCII\n").encode("utf-8"))
f.write(b"DATASET UNSTRUCTURED_GRID\n")
# write points and cells
_write_points(f, points, binary)
_write_cells(f, mesh.cells, binary)
# write point data
if mesh.point_data:
num_points = mesh.points.shape[0]
f.write("POINT_DATA {}\n".format(num_points).encode("utf-8"))
_write_field_data(f, mesh.point_data, binary)
# write cell data
def write_binary_file(f, mesh):
with open_file(f, "wb") as fh:
version = 3
itype = "i4"
postype = "i8"
ftype = "f8"
# according to manual keywords are always written as i4 independently of
# the file version
keytype = "i4"
# if we store internally 64bit integers upgrade file version
has_big_ints = False
for _, data in mesh.cells:
if data.dtype.itemsize == 8:
has_big_ints = True
break