Class pylset_io¶
Provides output to VTK files for ParaView, meshing of domain interiors and saving and loading of the pyLSET state in HDF5 files.
Bases: setup_pre
Methods:¶
__init__ ¶
Initialize the I/O class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
domain_size
|
ndarray of float, shape (ndim,)
|
Physical size of the domain along each axis. |
required |
ndiv
|
ndarray of int, shape (ndim,)
|
Number of grid points along each axis. |
required |
config
|
dict
|
Configuration parameters as key-value pairs. See the pyLSET overview for the complete list. |
required |
load_hdf5 ¶
Load a pyLSET HDF5 file written by save_hdf5().
With restore=True, the state in the file is also restored into this
simulation, so that a run can be continued from the file. The simulation
must have the grid of the file. The domains, level set fields, time,
step, the multiplier of the 'Lagrange' correction and, if saved, the
stored energies, orientations and source weights of the domains are
restored, and the properties are recomputed at the next step. Scalar
fields get their values and times. A scalar field that does not exist in
the simulation is created with default properties and insulated faces,
so its properties and conditions should be set again. Settings given as
functions are not saved and must be set by the calling script, before or
after the restore.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
Input file name, with or without the .h5 extension. |
required |
restore
|
bool
|
Restore the state into this simulation (default False). |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
data |
dict
|
Nested dictionary with the keys 'meta', 'simulation', 'grid', 'domains', 'state', 'scalar_fields', 'fields' and 'user'. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the file is not a pyLSET HDF5 file, or with |
load_surface ¶
load_surface(filename: str, scale: float = 1.0, offset: Optional[Sequence[float]] = None, group: Union[str, int, None] = None, mesh_size: Optional[float] = None) -> Tuple[np.ndarray, np.ndarray]
Read a surface as line segments (2D) or triangles (3D) for do_surface_sdf().
The format follows from the extension of the file name.
- '.stl': STL, ASCII or binary. Triangles, so 3D only.
- '.vtp': VTK polygon data. Lines in 2D and polygons in 3D, which are split into triangles.
- '.msh': Gmsh mesh. Line elements in 2D, and triangles and quadrangles in 3D, which are split into triangles. Only the corner nodes of elements of higher order are used.
- '.geo', '.step', '.stp', '.iges', '.igs', '.brep': Gmsh geometry or
CAD files, whose curves (2D) or surfaces (3D) are first meshed by Gmsh
with elements of size
mesh_size.
The Gmsh formats need the gmsh package. In 2D, the third coordinate
of the vertices must be constant and is dropped.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
Name of the file. |
required |
scale
|
float
|
Factor applied to the coordinates of the file, for example to convert millimeters to grid units. |
1.0
|
offset
|
sequence of float
|
Added to the coordinates after scaling, so that the surface lands at the right place in the domain. |
None
|
group
|
str or int
|
Name or tag of a physical group in a Gmsh file. Only its elements are read. Defaults to all elements of the dimension of the surface. |
None
|
mesh_size
|
float
|
Element size in the units of the file when Gmsh meshes a geometry
or CAD file. Defaults to the smallest grid spacing divided by |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
vertices |
ndarray of float, shape (n_vertices, ndim)
|
Vertex coordinates in the coordinates of the grid. |
cells |
ndarray of int, shape (n_cells, ndim)
|
Vertex indices of the segments (2D) or triangles (3D). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the format is not known, does not fit the dimension of the simulation or holds no elements of the requested kind. |
mesh_domain ¶
mesh_domain(domains: Union[int, Sequence[int], None] = None, size_max: Optional[float] = None, size_growth: float = 0.5, surface_step: int = 1, n_workers: int = 1) -> Dict[str, Any]
Mesh the interior of domains with triangles (2D) or tetrahedra (3D).
The boundary of each domain is the zero contour of its level set, found by marching squares (2D) or marching cubes (3D) on the grid. The interior inside this boundary is then meshed by Gmsh, with elements that keep the size of the boundary segments at the boundary and grow towards the inside of the domain. Every element carries the ID of its domain. Whether triangles or tetrahedra are made follows from the dimension of the level set fields.
Domains are meshed one by one, so the meshes of two neighboring domains do not share nodes, and small gaps remain where the zero contours of the domains do not meet, mainly at junctions. Domains that touch the boundary of the grid are closed by it, also on periodic axes, so a domain crossing a periodic boundary is meshed as separate pieces on the two sides. Inside immobile domains, the level sets of mobile domains are ignored.
The mesh is meant for visualization, for example by write_vtk_mesh()
and ParaView, where sets of domains can be selected by their ID.
Requires the gmsh package.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
domains
|
int or list of int
|
ID of the domain, or IDs of the domains, to mesh. By default all domains, mobile and immobile, are meshed. |
None
|
size_max
|
float
|
Largest element size inside a domain, in the length unit of the
grid. Defaults to eight grid spacings times |
None
|
size_growth
|
float
|
Increase of the element size per unit distance from the boundary of the domain. |
0.5
|
surface_step
|
int
|
Step, in grid points, of the marching squares or cubes. A step larger than 1 gives a coarser boundary with fewer elements. |
1
|
n_workers
|
int
|
Number of processes that mesh domains in parallel. Where processes
cannot be forked (Windows), the calling script must guard its main
code by |
1
|
Returns:
| Name | Type | Description |
|---|---|---|
mesh |
dict
|
'points' : ndarray of float, shape (n_points, ndim) Node coordinates. 'cells' : ndarray of int, shape (n_cells, ndim + 1) Node indices of the triangles or tetrahedra. 'domain_id' : ndarray of int, shape (n_cells,) Domain ID of each element. 'failed' : list of int IDs of domains for which Gmsh could not make a mesh. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If a requested domain does not exist or no level set fields are set. |
save_hdf5 ¶
save_hdf5(filename: str, phi: Optional[ndarray] = None, fields: Optional[Dict[str, ndarray]] = None, user_data: Optional[Dict[str, Any]] = None, compression: Optional[str] = 'gzip', compression_level: int = 4) -> None
Save the pyLSET state to an HDF5 file.
The file has the groups /meta (version and time stamp), /simulation
(configuration, time, step and the multiplier of the 'Lagrange'
correction), /grid (domain size, grid size, spacing and boundary
conditions), /domains (domain IDs, field assignment, mobile flags, the
initial domain map, the domain map found last by get_domain_map(), which
is the reference for telling apart domains that share a field, and the
stored energies, orientations and source weights of the domains if set), /state (level set fields),
/scalar_fields (value, time and time scheme of each scalar field),
/fields (additional arrays) and /user (user data). The file holds
everything needed to continue the run with load_hdf5(restore=True),
except settings given as functions, such as property functions,
velocity fields and the properties and conditions of scalar fields.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
Output file name, with or without the .h5 extension. |
required |
phi
|
ndarray
|
Level set fields with shape |
None
|
fields
|
dict of ndarray
|
Additional arrays to store, keyed by name. |
None
|
user_data
|
dict
|
Scalars, strings or arrays to store under |
None
|
compression
|
(gzip, lzf, None)
|
Compression of array datasets. |
'gzip'
|
compression_level
|
int
|
Compression level for gzip (0 to 9). |
4
|
write_info_file ¶
Write a text file with the simulation configuration and grid information.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
Output file name. |
'pylset_simulation.txt'
|
write_vtk_collection ¶
Write a ParaView collection file (.pvd) that groups VTK files as a time series.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
Output file name, with or without the .pvd extension. |
required |
files
|
list of str
|
VTK files (for example .vti or .vtp), given relative to the folder of the collection file. |
required |
times
|
list of float
|
Simulation time of each file. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the numbers of files and times differ. |
write_vtk_image ¶
Save grid data to a VTK image data file (.vti) for visualization in ParaView.
Values are written at cell centers, so the first point sits at half a grid spacing from the origin. 2D data are written as a single layer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
Output file name, with or without the .vti extension. |
required |
array_data
|
list of ndarray
|
Arrays with shape |
required |
array_name
|
list of str
|
Names of the arrays, in the same order as |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the numbers of arrays and names differ or an array has the wrong shape. |
write_vtk_interfaces ¶
Save the interfaces (zero contours of the level set fields) to a VTK PolyData file (.vtp).
In 2D the interfaces are written as polylines (marching squares), in 3D as triangulated surfaces (marching cubes). Each polyline or triangle carries the index of its level set field ('field') and the ID of the domain on its positive side ('domain_id'). An interface between two mobile domains appears once for each of the two domains. Periodic axes are padded by one grid point so that interfaces crossing them are closed.
Unlike the maximum over all fields, which is positive almost everywhere and only touches zero at interfaces, these contours can be shown directly in ParaView, for example colored by 'domain_id'.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
Output file name, with or without the .vtp extension. |
required |
phi
|
ndarray
|
Level set fields with shape |
None
|
write_vtk_mesh ¶
write_vtk_mesh(filename: str, mesh: Dict[str, Any], cell_data: Optional[Dict[str, ndarray]] = None) -> None
Save a mesh from mesh_domain() to a VTK unstructured grid file (.vtu).
Each triangle or tetrahedron carries the ID of its domain ('domain_id') and whether the domain is mobile ('mobile', 1 or 0). In ParaView, sets of domains can for example be shown by a Threshold filter on 'domain_id'.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
Output file name, with or without the .vtu extension. |
required |
mesh
|
dict
|
Mesh with the entries 'points', 'cells' and 'domain_id', as returned
by |
required |
cell_data
|
dict of ndarray
|
Additional arrays with one value per element, by name. Values per
domain are mapped to the elements by their domain ID, for example
|
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If an array in |