fish2eod.xdmf package¶
Submodules¶
fish2eod.xdmf.load module¶
Load solutions in a filterable form from xdmf/manifests.
- class fish2eod.xdmf.load.DataSolution(domain_map, variables, data, s_var=None)¶
Bases:
Solution- load_data()¶
- spawn_from_variable(new_selected_variable, new_data)¶
- class fish2eod.xdmf.load.H5Solution(h5_file, parameter_levels, domain_map, variables, data, s_param=(), s_var=None)¶
Bases:
SolutionStore the solution or filtered subset.
A solution is a collection of datasets. This can be filtered on parameter or variable. Once subset further processing sub setting is needed until a single solution remains
- Parameters:
h5_file (
Path) – Path to the h5 data setparameter_levels (
Dict[str,Tuple[int]]) – Dictionary of parameter : levels of parametervariables (
Tuple[str]) – Possible variablesdata (
List[DataSet]) – List of DataSet objectss_param (
Tuple[str,...]) – List of selected_parameters if any else Nones_var (
Optional[str]) – Selected variable if selected else None
- filter_parameters(parameter, value)¶
Filter the solution where parameter = value.
- Parameters:
parameter (
str) – Name of the parametervalue (
str) – String of selected parameter index
- Return type:
- Returns:
Filtered solution
- load_data()¶
Load data.
Checks if loadable i.e. no other parameters or variables to select Loads either a namedtuple for misc data or a tuple of arrays for fem dataset
- Returns:
Loaded data
- property parameters: Tuple[str]¶
Get the parameter names.
- Return type:
Tuple[str]- Returns:
Tuple of parameter names - no particular order
- spawn_from_variable(new_selected_variable, new_data)¶
- class fish2eod.xdmf.load.Solution¶
Bases:
object- filter_variables(variable)¶
Extract the variable from the solution.
- Parameters:
variable (
str) – Name of the variable- Return type:
- Returns:
Filtered solution
- abstract load_data()¶
- abstract spawn_from_variable(new_selected_variable, new_data)¶
- fish2eod.xdmf.load.domain_map_from_data(path)¶
Extract the domain map from the data.
TODO duplication in constant map across time steps
- Parameters:
path (
Path) – Path to the manifest- Return type:
Dict[str,int]- Returns:
Map of domains name to number
- fish2eod.xdmf.load.extract_all_data(path)¶
Load data from each xdmf file.
- Parameters:
path (
Path) – Path to manifest- Return type:
Iterator[DataSet]- Returns:
Iterator of DataSets
- fish2eod.xdmf.load.extract_path_from_grid(grid, parent)¶
Extract h5 data paths from the grid.
- Parameters:
grid – The grid to search (et.Element)
parent (
str) – The parent to get paths of (Attribute for example for data)
- Return type:
Iterator[Tuple[str,Optional[str]]]- Returns:
Sequence of child paths and the name of the dataset or None if not available
- fish2eod.xdmf.load.extract_unique_from_grid(grid, term)¶
Extract a unique term from the grid.
A unique term like <Time /> or <Metadata /> only is entered once per grid
- Parameters:
grid – The grid (et.Element) to search
term (
str) – The term to search for
- Return type:
Dict[str,Any]- Returns:
The attributes of the found element
- fish2eod.xdmf.load.find_manifest(path, search_name='manifest.xml')¶
Find the manifest file in the given path.
Since the path can be specified as either the path to the manifest or the containing directory this will check if the specified path is the manifest or if the folder given contains the manifest.
- Parameters:
path (
Union[str,Path]) – Path to manifest or search pathsearch_name – Name of file to search for
- Return type:
Path- Returns:
Path to the manifest
- fish2eod.xdmf.load.load_fem_data(h5_file, data_set)¶
Load fem data from the h5 file.
FEM data is a topology, geometry and an array representation of function TODO named tuple
- Parameters:
h5_file (
Path) – Path to the h5 filedata_set (
DataSet) – DataSet to load
- Return type:
Tuple[ndarray,...]- Returns:
The tuple of data topology, geometry, data
- fish2eod.xdmf.load.load_from_file(path)¶
User-facing api to load the data.
- Parameters:
path (
Union[str,Path]) – Path to manifest.xml or the model folder- Return type:
- Returns:
The Solution object
- fish2eod.xdmf.load.load_from_h5(h5_file, *data_paths)¶
Load data from the h5_file with a sequence of paths.
Nx1 data is converted to an array of length N and unsignedintegers are converted to signed integers
- Parameters:
h5_file (
Path) – Path to the h5 filedata_paths (
str) – Arbitrary number of data_paths to load
- Return type:
Iterator[ndarray]- Returns:
Iterator of arrays for each data_path
- fish2eod.xdmf.load.load_misc_data(h5_file, data_set)¶
Load misc data from the h5 file.
- Parameters:
h5_file (
Path) – Path to the h5 filedata_set (
DataSet) – DataSet to load
- Return type:
Tuple- Returns:
The misc_data in its named tuple
- fish2eod.xdmf.load.load_xdmf(xdmf_file)¶
Load an xdmf file.
- Parameters:
xdmf_file (
Path) – The path to the xdmf file- Return type:
Iterator[DataSet]- Returns:
Sequence of DataSet objects
- fish2eod.xdmf.load.misc_attribute_to_data_set(grid)¶
Return data_set compatible structure from misc attributes.
The goal is for each (super) attribute to have a structure ([(sub_attr1, path1), …], name_of_super_attribute, …) where each super attribute behaves like a normal attribute and the path with be a list of subpaths and subnames
- Parameters:
grid – Grid to convert (et.Element)
- Return type:
Iterator[Tuple[List,str]]- Returns:
Structured misc data
- fish2eod.xdmf.load.parameter_levels_from_data(path)¶
Extract all parameter levels (and parameters) from a dataset.
- Parameters:
path (
Path) – Path to the manifest- Return type:
Dict[str,Tuple[int]]- Returns:
Parameters and corresponding levels
- fish2eod.xdmf.load.parse_manifest(manifest)¶
Parse the xdmf/h5 files from the manifest.
- Parameters:
manifest (
Path) – Manifest path- Return type:
Tuple[Iterator[Path],Path]- Returns:
The xdmf paths and the h5 file
- fish2eod.xdmf.load.variables_from_data(path)¶
Extract all the variables from a dataset.
- Parameters:
path (
Path) – Path to the manifest- Return type:
Tuple[str,...]- Returns:
Variable names
fish2eod.xdmf.save module¶
Save solutions to an expandable structure.
Saves 1D data I.e. function defined on boundaries and 2D data I.e. function defined on domains/cells and misc data (see later)
There is one manifest file which stores the the locations of the different data types which are stored in separate XDMF(ish) files for paraview compatibility (not strictly XDMF as they contain additional tags but could be considered a superset of XDMF).
# <Manifest> # <data type=’1d’ path=”PATH_TO_1D_DATA.xdmf”/> # <data type=’2d’ path=”PATH_TO_2D_DATA.xdmf”/> # <data type=’misc’ path=”PATH_TO_misc.xdmf”/> # <h5 path=”PATH_TO_h5_file.h5”/> # </Manifest>
1D dataset contains boundary functions
The XDMF tag contains a domain containing a grid which specifies a “set of parameters” with each parameter being stored in a nested grid tag. Within a simulation the topology (what connects to what) and the geometry (where was is) and the is stored in topology and geometry tags with the function defined on the Attribute tag
# <!DOCTYPE Xdmf SYSTEM “Xdmf.dtd” []> # <Xdmf xmlns:xi=”http://www.w3.org/2001/XInclude” Version=”3.0”> # <Domain> # <Grid Name=”TimeSeries” GridType=”Collection” CollectionType=”Temporal”> # <Grid Name=”mesh” GridType=”Uniform”> # <Time Value=”0”/> # <Metadata TimeName=”ParaViewTime” step=”0” location=”0” amp=”0”/> # <Topology NumberOfElements=”173” TopologyType=”PolyLine” NodesPerElement=”2”> # <DataItem Dimensions=”173 2” NumberType=”UInt” Format=”HDF”>MODEL.h5:/Mesh/2/mesh/edges</DataItem> # </Topology> # <Geometry GeometryType=”XY”> # <DataItem Dimensions=”62 2” Format=”HDF”>MODEL.h5:/Mesh/1/mesh/geometry</DataItem> # </Geometry> # <Attribute Name=”boundary” AttributeType=”Scalar” Center=”Cell”> # <DataItem Dimensions=”173 1” Format=”HDF”>MODEL.h5:/VisualisationVector/3</DataItem> # </Attribute> # </Grid> # </domain> # </Xdmf>
2D dataset contains domain/cell functions
The XDMF tag contains a domain containing a grid which specifies a “set of parameters” with each parameter being stored in a nested grid tag. Within a simulation the topology (what connects to what) and the geometry (where was is) and the is stored in topology and geometry tags with the function defined on the Attribute tag
# <!DOCTYPE Xdmf SYSTEM “Xdmf.dtd” []> # <Xdmf xmlns:xi=”http://www.w3.org/2001/XInclude” Version=”3.0”> # <Domain> # <Grid Name=”mesh” GridType=”Uniform”> # <Time Value=”0”/> # <Metadata TimeName=”ParaViewTime” step=”0” location=”0” amp=”0”/> # <Topology NumberOfElements=”114” TopologyType=”Triangle” NodesPerElement=”3”> # <DataItem Dimensions=”114 3” NumberType=”UInt” Format=”HDF”>MODEL.h5:/Mesh/0/mesh/topology</DataItem> # </Topology> # <Geometry GeometryType=”XY”> # <DataItem Dimensions=”62 2” Format=”HDF”>MODEL.h5:/Mesh/1/mesh/geometry</DataItem> # </Geometry> # <Attribute Name=”V” AttributeType=”Scalar” Center=”Node”> # <DataItem Dimensions=”62 1” Format=”HDF”>MODEL.h5:/VisualisationVector/5</DataItem> # </Attribute> # <Attribute Name=”sigma” AttributeType=”Scalar” Center=”Node”> # <DataItem Dimensions=”62 1” Format=”HDF”>MODEL.h5:/VisualisationVector/6</DataItem> # </Attribute> # <Attribute Name=”domain” AttributeType=”Scalar” Center=”Cell”> # <DataItem Dimensions=”114 1” Format=”HDF”>MODEL.h5:/VisualisationVector/7</DataItem> # </Attribute> # </Grid> # </domain> # </Xdmf>
- class fish2eod.xdmf.save.Saver(model_name, save_path)¶
Bases:
objectSaves relevant data from a model into paraview-compatible set of xdmf files.
- Parameters:
model_name (
str) – Name of the modelsave_path (
Union[Path,str]) – Path to save the model to (model will be saves to save_path/model_name)
- add_data(f)¶
Add the dataset to the h5 file and the reference it in the XDMF file.
If the data
fis a tuple of misc data its passed to add_misc_data otherwise its passed to add_fencis_data- Parameters:
f – Data to save Meshfunction/function/tuple
- Return type:
None
- add_fenics_data(f)¶
Add the dataset to the h5 file and the reference it in the XDMF file.
If the data
fis a tuple of misc data its passed to add_misc_data otherwise its passed to add_fencis_data- Parameters:
f – Data to save Meshfunction/function
- Return type:
None
- add_misc_data(d)¶
Add the miscellaneous data to the xdmf and the h5 file.
Misc data is treated separately because for each data structure there is an outer Attribute which holds a collection of data sets from d (namedtuple)
- Parameters:
d – Miscelaneous data container, must be a namedtuple
- Return type:
None- Returns:
None
- create_next_grid(num_topology_elements_1d=None, num_topology_elements_2d=None, num_geometry_elements=None, metadata=None, domain_map=None)¶
Create grid for a the next step for the 1d and 2d xdmf files.
Creates the grid and assigns them as a pointer in current_grid_1d and current_grid_2d attributes
# TODO refactor the duplication out of this function. Any loop right now will be awkward
- Parameters:
num_geometry_elements – Number of elements in the geometry (number of nodes)
num_topology_elements_1d – Number of elements in the 1d topology (lines)
num_topology_elements_2d – Number of elements in the 2d topology (triangles)
metadata – Metadata dictionary
- property geometry_path: str¶
H5 path for the geometry arrays (/Geometry/step).
- Return type:
str
- save()¶
Save the XDMF files.
Because XDMF are so small - this overwrites the entire XDMF file with the new data: which is the old data + new
- save_model(model, metadata=None)¶
Save the specified model to an XDMF representation.
- Parameters:
model (
Model) – The model to savemetadata – Metadata (parameter states) to save along with the data
- property topology_path_1d: str¶
H5 path for the 1d topology arrays (/Topology/1d/step).
- Return type:
str
- property topology_path_2d: str¶
H5 path for the 2d topology arrays (/Topology/2d/step).
- Return type:
str
- write_geometry(geometry)¶
Write the geometry (coordinates) to the h5 file.
- Parameters:
geometry (
ndarray) – Array of x,y coordinates for the geometry- Return type:
None
- write_h5(path, d)¶
Write to the h5 file.
- Parameters:
path (
str) – Internal h5 path to write tod (
ndarray) – Dataset to write
- write_topology(topology)¶
Write the topology to the dimension appropriate h5 file path.
- Parameters:
topology (
ndarray) – Array of topology- Return type:
None
fish2eod.xdmf.xml_tools module¶
Common tools for working with XDMF/XML files.
- fish2eod.xdmf.xml_tools.create_minimal_xdmf()¶
Create the minimal XDMF template.
Most of these properties are necessary for Paraview compatibility. Some may not be required but they’re used in the Fenics XDMF files.
- fish2eod.xdmf.xml_tools.create_timestep_grid(*, step_number, num_topology_elements, dim, data_location_topology, num_geometry_elements, data_location_geometry, metadata, domain_map)¶
Create the inner grid defining a time-step.
This function has many inputs which must be specified as keywords. Additionally they must all be specified non-None type. The assert_all_defined decorator ensures this.
- Parameters:
step_number – Current iteration of the model. Paraview will plot in this order
num_topology_elements – Number of elements in the Topology
dim – Dimension of the data
data_location_topology – h5 location of the Topology
num_geometry_elements – Number of elements in the geometry
data_location_geometry – h5 location of the geometry
metadata – Dictionary of parameter_name:value to be saved along with the simulation
- Returns:
Inner grid (et.Element)
- fish2eod.xdmf.xml_tools.create_timestep_grid_misc(*, step_number, metadata, domain_map)¶
Create the grid for a “time-step” in the miscellaneous data-set.
Time step refers to an individual simulation which could be time or some parameter sweep Typical parameters like topology and dimension are not specified due to miscellaneous dataset.
- Parameters:
step_number – Current iteration of the model. Paraview will plot in this order
metadata – Dictionary of parameter_name:value to be saved along with the simulation
- Returns:
Newly created grid (et.Element)
- fish2eod.xdmf.xml_tools.get_outer_grid(xdmf, parent_tag='Domain')¶
Get the outer grid element from the XDMF structure.
- Parameters:
xdmf – The xdmf structure to parse (et.Element)
parent_tag – The name of the domain tag (parent)
- fish2eod.xdmf.xml_tools.make_geometry(num_elements, location)¶
Create the XDMF geometry element.
- Parameters:
num_elements (
int) – Number of elements in the geometry (# nodes)location (
str) – Location of the geometry in the h5 file
- Returns:
The geometry element (et.Element)
- fish2eod.xdmf.xml_tools.make_manifest_file(model_name, save_path, data_folder='DATA')¶
Create the manifest file.
The manifest file holds the paths to the 1d, 2d, misc data as well as the path to the h5 file
- Parameters:
model_name (
str) – Name of the modelsave_path (
Path) – Path of parent folder of the modeldata_folder – Optional argument specifying name of the sub-folder containing the data
- Return type:
None- Returns:
None
- fish2eod.xdmf.xml_tools.make_topology(num_elements, dim, location)¶
Create the XDMF topology element.
- Parameters:
num_elements (
int) – Number of elements in the topology (# triangles/lines)dim (
int) – Dimension of the solutionlocation (
str) – Location of the topology in the h5 file
- Returns:
The topology element (et.Element)
- fish2eod.xdmf.xml_tools.write_xml(save_path, xml_obj, xml_declaration=True, doctype=None)¶
Write an ElementTree object to an xml/xdmf file.
- Parameters:
save_path (
Path) – Path to save the file toxml_obj – The ElementTree to save
xml_declaration – Include XML declaration
doctype – Optional doctype for XDMF files (None for no doctype)
- Return type:
None- Returns:
None