Files¶
A mesh of arbitrary geometry is read from file with meshio, in any format it supports, and results are written back the same way:
from torchfem.io import export_mesh, import_mesh
model = import_mesh("plate.vtu", material)
u, f, sigma, epsilon, state = model.solve()
export_mesh(model, "result.vtu", nodal_data={"u": u})
torchfem.io.import_mesh(filename, material, thickness=1.0)
¶
import_mesh(filename: PathLike, material: MechanicsMaterial | Laminate, thickness: float = 1.0) -> Mechanics
import_mesh(filename: PathLike, material: HeatMaterial, thickness: float = 1.0) -> Heat
Imports a mesh file and returns the matching model type.
The model type follows from the geometry and the element type: a mesh whose nodes
all lie in the z=0 plane becomes a Planar model, a non-planar triangle mesh a
Shell, and a tetrahedral or hexahedral mesh a Solid. Use import_shell(...)
to require a shell.
A HeatMaterial gives the heat model of that geometry, and a Laminate
section needs a surface mesh, as only a Shell takes one.
Parameters:
-
filename(PathLike) –Path to a mesh file in any format meshio can read.
-
material(Material | Laminate) –Material or section assigned to the model.
-
thickness(float, default:1.0) –Thickness of a planar or shell model. Unused for a solid.
Returns:
-
FEM(FEM) –A model of the type described above.
Raises:
-
Exception–If the file holds more than one element type, or if its element type has no corresponding model.
The model type follows from the geometry and the element type:
| Mesh | Model |
|---|---|
Any element type, all nodes at z = 0 |
Planar |
Triangles or quadrilaterals that leave the z = 0 plane |
Shell |
| Tetrahedra or hexahedra | Solid |
torchfem.io.import_shell(filename, material, thickness=1.0, offset=0.0)
¶
import_shell(filename: PathLike, material: MechanicsMaterial | Laminate, thickness: float = 1.0, offset: float = 0.0) -> Shell
import_shell(filename: PathLike, material: HeatMaterial, thickness: float = 1.0, offset: float = 0.0) -> ShellHeat
Import a triangle or quadrilateral mesh as a shell model, flat or not.
A HeatMaterial gives a ShellHeat, anything else a Shell.
import_mesh(...) reads a flat surface mesh as Planar instead. offset
places the reference surface within the section, as a fraction of thickness
from the mid-plane along the element normal, so +0.5 puts it on the top
face and the section hangs below the mesh.
Raises:
-
TypeError–If the mesh is not a surface mesh.
A flat surface mesh reads as Planar in the table above, so import_shell(...) is how a flat shell is requested explicitly.
torchfem.io.export_mesh(mesh, filename, nodal_data=None, elem_data=None, compress=True)
¶
Writes a model and optional result fields to a mesh file.
The file format is inferred from the suffix of filename by meshio. All tensors
are detached and moved to the CPU before writing.
Parameters:
-
mesh(FEM) –Model providing the nodes and elements to write.
-
filename(str | PathLike) –Output path. Its suffix selects the format.
-
nodal_data(dict[str, Tensor], default:None) –Point data to attach, keyed by name. Shape:
(n_nod, ...)per entry. -
elem_data(dict[str, list[Tensor]], default:None) –Cell data to attach, keyed by name. Each value is a list with one tensor per cell block, so a single-block model takes a one-element list, e.g.
{"rho": [rho]}. Shape:(n_elem, ...)per tensor. -
compress(bool, default:True) –Compress the payload. Applies to
.vtu(zlib) and.xdmf/.xmf(gzip) only, and is ignored for other formats.
torchfem.data.get_data(file)
¶
Resolves the path to an example mesh bundled with torch-fem.
The meshes live in torchfem/data and are meant to be passed straight to
import_mesh(...), e.g. import_mesh(get_data("plate_hole.vtk"), material).
Parameters:
-
file(str) –File name of the bundled mesh, including its suffix.
Returns:
-
Path(Path) –Absolute path to the file inside the installed package. The path is returned without checking that the file exists.
The bundled meshes are the ones the Examples import.