Source code for tracesuite.post.dataset
"""Provides a class to wrap a cpp POST dataset."""
from __future__ import annotations
import collections
import typing
from tracesuite import FamilyType
from . import _post, types
from .block import Block
if typing.TYPE_CHECKING:
from tracesuite import simulation_setup as setup
from tracesuite.trace import _trace
[docs]
class DataSet(types.setup.Setup):
"""Representation of a POST dataset including blocks and metadata."""
def __init__(self, post_dataset: _post.Dataset):
self._ds = post_dataset
self._rows = None
self._stages = None
self._components = None
@property
def block_groups(self) -> collections.ChainMap[str, setup.BlockGroup]:
"""Returns dictionary of all blocks groups."""
self._find_turbomachinery_relationships()
return collections.ChainMap(self._rows, self._stages, self._components)
@property
def blocks(self) -> dict[str, Block]:
"""Return all blocks of the current DataSet.
:returns: A dictionary of all blocks.
"""
return {block.name: Block(self._ds, block.name) for block in self._ds.blocks}
@property
def rows(self) -> dict[str, types.Row]:
"""Returns dictionary of all rows."""
self._find_turbomachinery_relationships()
return self._rows
@property
def stages(self) -> dict[str, types.Stage]:
"""Returns dictionary of all stages."""
self._find_turbomachinery_relationships()
return self._stages
@property
def components(self) -> dict[str, types.Component]:
"""Returns dictionary of all components."""
self._find_turbomachinery_relationships()
return self._components
@property
def boundaries(self) -> dict[str, types.Boundary]:
"""Returns dictionary of all boundaries."""
boundary_types = {FamilyType.BOUNDARY, FamilyType.BANDED_BOUNDARY}
return {b.name: types.Boundary(self._ds, b) for b in self._ds.families if b.type in boundary_types}
def _find_turbomachinery_relationships(self) -> None:
"""Fill turbomachinery structured everytime the structure is accessed."""
self._components = {c.name: types.Component(self._ds, c) for c in self._ds.families if c.type == FamilyType.COMPONENT}
self._stages = {s.name: types.Stage(self._ds, s) for s in self._ds.families if s.type == FamilyType.STAGE}
self._rows = {r.name: types.Row(self._ds, r, self._components, self._stages) for r in self._ds.families
if r.type == FamilyType.BLOCK_GROUP}
for row in self._rows.values():
if row.parent_component is not None:
row.parent_component._rows[row.name] = row
if row.parent_stage is not None:
row.parent_stage._rows[row.name] = row
if row.parent_stage is not None and row.parent_component is not None:
row.parent_stage._component = row.parent_component
row.parent_component._stages[row.parent_stage.name] = row.parent_stage
@property
def analysis_surfaces(self) -> dict[str, types.AnalysisSurface]:
"""Returns dictionary of all analysis panel groups."""
return {a.name: types.AnalysisSurface(self._ds, a) for a in self._ds.families if a.type == FamilyType.ANALYSIS}
@property
def volume_sources(self) -> dict[str, types.VolumeSource]:
"""Returns dictionary of all volume sources."""
return {v.name: types.VolumeSource(self._ds, v) for v in self._ds.families if v.type == FamilyType.VOLUME_SOURCE}
# @todo: property is complete if all post family enums (FamilyType) are included
@property
def families(self) -> collections.ChainMap[str, types.Family]:
"""Returns dictionary of all post families."""
self._find_turbomachinery_relationships()
return collections.ChainMap(self._components, self._stages, self._rows, self.boundaries, self.analysis_surfaces, self.volume_sources)
[docs]
def copy(self) -> DataSet:
"""Create a (deep) copy of the dataset."""
# @todo: Implement __copy__ and __deepcopy__ instead?
if not self.is_valid:
msg = "The data set to be copied is already destroyed."
raise ValueError(msg)
return DataSet(_post.duplicate_post_data_set(self._ds))
[docs]
def schedule(self, post_cntl: _post.PostCntl, n_input_datasets: int, *, rewind: bool = False) -> None:
"""Add the dataset to the POST scheduler.
:param post_cntl: POST control instance
:param n_input_datasets: number of input datasets
:param rewind: True for accumulating datasets
"""
post_cntl.scheduler_add_dataset(self._ds, rewind=rewind)
if post_cntl.scheduler_will_consume_datasets(n_input_datasets):
self._ds = None
[docs]
def check_for_post_field(self, solution_name: str, field_name: str) -> bool:
"""Check if a field specified by field_name is present in a solution on all blocks.
:param solution_name: Name of the solution to search in.
:param field_name: Name of the field to search for.
:returns: True if field exists at all blocks in specified solution.
"""
return self._ds.post_field_exists_on_all_blocks(solution_name, field_name) # @todo: Implement here
[docs]
def has_harmonic_flow_solutions(self) -> bool:
"""Check if any block has a harmonic solution.
:returns: True if a harmonic solution exists in the data set
"""
return self._ds.is_harmonic_flow_solution_in_post_dataset() # @todo: Implement here
[docs]
def merge_radial_distributions(self, *radial_distributions: _trace.OutputBufferRadialDistribution) -> None:
"""Merge the radial distributions from TRACE into the dataset.
:param radial_distributions: the radial distribution(s); can also contain 0D data
"""
for radial_distribution in radial_distributions:
self._ds.merge_radial_distribution(radial_distribution)
def __repr__(self):
return f"<DataSet@{self._ds}>"
@property
def is_valid(self) -> bool:
"""Return True if the dataset is valid."""
return self._ds is not None
def __del__(self):
"""Destroy c++ side of dataset."""
if self.is_valid:
self._ds.dispose_post_dataset()
class AccumulatorDataSet(DataSet):
"""Representation of a POST dataset including blocks and metadata.
Also contains accumulation task execution counters.
"""
def __init__(self, post_dataset: _post.Dataset, i_execution: int, n_executions: int):
"""Create a new accumulator dataset.
:param post_dataset: post dataset
:param i_execution: current execution
:param n_executions: total number of executions
"""
super().__init__(post_dataset)
self._i_execution = i_execution
self._n_executions = n_executions
@property
def i_execution(self) -> int:
"""Return the current execution."""
return self._i_execution
@property
def n_executions(self) -> int:
"""Return the total number of executions."""
return self._n_executions
def copy(self) -> AccumulatorDataSet:
"""Create a (deep) copy of the dataset."""
# @todo: Implement __copy__ and __deepcopy__ instead?
if not self.is_valid:
msg = "The data set to be copied is already destroyed."
raise ValueError(msg)
return AccumulatorDataSet(_post.duplicate_post_data_set(self._ds), self._i_execution, self._n_executions)