File management¶
Maia supports HDF5/CGNS file reading and writing, see related documention.
The IO functions are provided by the maia.io module. All the high level functions
accepts a legacy parameter used to control the low level CGNS-to-hdf driver:
if
legacy==False(default), hdf calls are performed by the python module h5py.if
legacy==True, hdf calls are performed by Cassiopee.Converter module.
The requested driver should be installed on your computer as well as the hdf5 library compiled with parallel support.
Distributed IO¶
Distributed IO is the privileged way to deal with CGNS files within your maia workflows. Files are loaded as distributed trees, and, inversely, distributed trees can be written into a single CGNS file.
High level IO operations can be performed with the two following functions, which read or write all data they found :
- file_to_dist_tree(filename, comm, legacy=False)¶
Distributed load of a CGNS file.
- Parameters
filename (str) – Path of the file
comm (MPIComm) – MPI communicator
- Returns
CGNSTree – Distributed CGNS tree
- dist_tree_to_file(dist_tree, filename, comm, legacy=False)¶
Distributed write to a CGNS file.
- Parameters
dist_tree (CGNSTree) – Distributed tree to write
filename (str) – Path of the file
comm (MPIComm) – MPI communicator
The example below shows how to uses these high level functions:
from mpi4py import MPI
import maia
# Generate a sample tree
dist_tree = maia.factory.generate_dist_block(10, "Poly", MPI.COMM_WORLD)
# Write
maia.io.dist_tree_to_file(dist_tree, "tree.cgns", MPI.COMM_WORLD)
# Read
tree = maia.io.file_to_dist_tree("tree.cgns", MPI.COMM_WORLD)
Finer control of what is written or loaded can be achieved with the following steps:
For a write operation, the easiest way to write only some nodes in the file is to remove the unwanted nodes from the distributed tree.
For a read operation, the load has to be divided into the following steps:
Loading a size_tree: this tree has only the shape of the distributed data and not the data itself.
Removing unwanted nodes in the size tree;
Fill the filtered tree from the file.
The example below illustrate how to filter the written or loaded nodes:
from mpi4py import MPI
import maia
# Generate a sample tree
dist_tree = maia.factory.generate_dist_block(10, "Poly", MPI.COMM_WORLD)
# Remove the nodes we do not want to write
maia.pytree.rm_nodes_from_name(dist_tree, 'CoordinateZ') #This is a DataArray
maia.pytree.rm_nodes_from_name(dist_tree, 'Zm*') #This is some BC nodes
# Write
maia.io.dist_tree_to_file(dist_tree, "tree.cgns", MPI.COMM_WORLD)
# Read
from maia.io.cgns_io_tree import load_collective_size_tree, fill_size_tree
dist_tree = load_collective_size_tree("tree.cgns", MPI.COMM_WORLD)
#For now dist_tree only contains sizes : let's filter it
maia.pytree.rm_nodes_from_name(dist_tree, 'CoordinateY') #This is a DataArray
maia.pytree.rm_nodes_from_name(dist_tree, 'Ym*') #This is some BC nodes
fill_size_tree(dist_tree, "tree.cgns", MPI.COMM_WORLD)
Writing partitioned trees¶
In some cases, it may be useful to write a partitioned tree (keeping the partitioned zones separated). This can be achieved using the following function:
- part_tree_to_file(part_tree, filename, comm, single_file=False, legacy=False)¶
Gather the partitioned zones managed by all the processes and write it in a unique hdf container.
If
single_fileis True, one file named filename storing all the partitioned zones is written. Otherwise, hdf links are used to produce a main file filename linking to additional subfiles.- Parameters
part_tree (CGNSTree) – Partitioned tree
filename (str) – Path of the output file
comm (MPIComm) – MPI communicator
single_file (bool) – Produce a unique file if True; use CGNS links otherwise.
Example
from mpi4py import MPI import maia dist_tree = maia.factory.generate_dist_block(10, "Poly", MPI.COMM_WORLD) part_tree = maia.factory.partition_dist_tree(dist_tree, MPI.COMM_WORLD) maia.io.part_tree_to_file(part_tree, 'part_tree.cgns', MPI.COMM_WORLD)
Raw IO¶
For debug purpose, trees can also be read or written independently in a sequential manner. Be aware that information added by maia such as Distribution or GlobalNumbering nodes will not be removed.
- read_tree(filename, legacy=False)¶
Sequential load of a CGNS file.
- Parameters
filename (str) – Path of the file
- Returns
CGNSTree – Full (not distributed) CGNS tree
- write_tree(tree, filename, links=[], legacy=False)¶
Sequential write to a CGNS file.
- Parameters
tree (CGNSTree) – Tree to write
filename (str) – Path of the file
links (list) – List of links to create (see SIDS-to-Python guide)
Example
from mpi4py import MPI import maia dist_tree = maia.factory.generate_dist_block(10, "Poly", MPI.COMM_WORLD) if MPI.COMM_WORLD.Get_rank() == 0: maia.io.write_tree(dist_tree, "tree.cgns")
- write_trees(tree, filename, comm, legacy=False)¶
Sequential write to CGNS files.
Write separate trees for each process. Rank id will be automatically inserted in the filename.
- Parameters
tree (CGNSTree) – Tree to write
filename (str) – Path of the file
comm (MPIComm) – MPI communicator
Example
from mpi4py import MPI import maia dist_tree = maia.factory.generate_dist_block(10, "Poly", MPI.COMM_WORLD) maia.io.write_trees(dist_tree, "tree.cgns", MPI.COMM_WORLD)