Examples#
IO#
Lazy loading data#
"""Lazy loading of Elastica++ simulation data."""
import os
from pathlib import Path
from elastica_pipelines import io
# Metadata file written by Elastica++.
metadata_fn = Path("..") / "tests" / "io" / "data" / "elastica_metadata.h5"
# Disable libhdf5 file locking since we only read files
# This needs to be done before any import of h5py, so before reading a series
os.environ["HDF5_USE_FILE_LOCKING"] = "FALSE"
# Create read only access to data written by Elastica++.
series = io.series(metadata=metadata_fn)
# use series like a python Mapping.
for t, snapshot in series.iterations():
print(f"Iteration: {t.iterate} at time {t.time}")
# Snapshot is a mapping contain system types such as CosseratRods & Spheres.
# Here we access only cosserat rods only.
for (
rod_id,
rod,
) in snapshot.cosserat_rods().items(): # snapshot['CosseratRod'] also works!
if rod_id == 0:
print(f" Rod {rod_id!r} attributes:")
# Even rod is a Mapping, so we get its keys...
print(f" {list(rod.keys())}")
# and then access it values.
print(f" Rod {rod_id!r} position:", rod["Position"])
# To access all rod types (CosseratRod, CosseratRodWithoutDamping etc.),
# use the rods() method
for (
rod_id,
rod,
) in snapshot.rods().items():
# and then access it values.
print(f" Rod {rod_id!r} position:", rod["Position"])
Selection#
"""Selecting subsets of Elastica++ data and lazy loading."""
import os
from pathlib import Path
from elastica_pipelines import io
metadata_fn = Path("..") / "tests" / "io" / "data" / "elastica_metadata.h5"
# Disable libhdf5 file locking since we only read files
# This needs to be done before any import of h5py, so before reading a series
os.environ["HDF5_USE_FILE_LOCKING"] = "FALSE"
series = io.series(metadata=metadata_fn)
# Suppose we are only interested in the evolution of rods with rod_id 1, 3.
# We explicitly request access using `temporal_select`
# CosseratRodRecordIndex is necessary to indicate we are interested in rods
# and not, say spheres
subset = series.temporal_select(io.CosseratRodRecordIndex([1, 3]))
# You can also pass in a single index like so...
subset = series.temporal_select(io.CosseratRodRecordIndex(1))
# Or even slices.
subset = series.temporal_select(io.CosseratRodRecordIndex(slice(1, 5, 2)))
# To select another system type, such as a sphere use
# subset = series.temporal_select(io.SphereRecordIndex([1, 3]))
# Use subset with the exact same interface as a io series.
for t, snapshot in subset.iterations():
print(f"Iteration: {t.iterate} at time {t.time}")
# Use the same map interface!
for rod_id, rod in snapshot.items():
print(f" Rod {rod_id!r} elements:")
print(" Rod elements:", rod["NElement"])
Transforms#
"""Adding transforms to Elastica++ series."""
import os
from pathlib import Path
from elastica_pipelines import io
metadata_fn = Path("..") / "tests" / "io" / "data" / "elastica_metadata.h5"
# Disable libhdf5 file locking since we only read files
# This needs to be done before any import of h5py, so before reading a series
os.environ["HDF5_USE_FILE_LOCKING"] = "FALSE"
# Add transformations to the data using the transforms module.
# ToArray() converts the data to a numpy array
series = io.series(metadata=metadata_fn, transforms=io.transforms.ToArray())
# You can also write your own transformations and compose them using Compose
# We convert the data to numpy and then transpose it here.