Skip to content

Commit

Permalink
Exposition of the mesh_info_provider in Python and output as derived …
Browse files Browse the repository at this point in the history
…class (#1007)

* data sources namespace

* fix

* add test and docstring

* add generic data container api

* operator gdc connect & getoutput

* exposed get-property

* any new_from & cast; gdc set_property

* add documentation

* add get-property-[names/types]

* add missing docstring

* fix type reflection

* update any ansys release version

* fix coding style

* fix coding style

* remove datasources changes

* add server version decorator

* fix coding style

* fix retro compatibility errors

* add server version check in op connect

* fix deleter crash

* fix get_output

* fix deleter crash

* fix typo

* fix operator gdc test

* revert all changes

* remove gdc

* add back any & gdc

* add back operator connect test

* revert operators test changes

* add back any tests

* Exposition of the mesh_info in Python: MeshInfo class and tests

* Exposition of the mesh_info in Python

* Revert "revert all changes"

This reverts commit 2f42b6e.

# Conflicts:
#	src/ansys/dpf/core/__init__.py
#	src/ansys/dpf/core/common.py
#	src/ansys/dpf/core/dpf_operator.py

* Revert commits "revert all changes" from feat/grpc-api-propagation

* Reformat code with pre-commit

* Complete the tests and locally correct some issues concerning the GDC (fix in another PR)

* mesh_info_provider test with fluent file

* Complete mesh_info tests with mesh_info_provider in CFF

* Reformat and rename mesh_info

* Exposition of the mesh_info_provider

Changes in the formating of the operator generation

* Fix some issues: naming and argument order, conditionnal derive_class argument.

Cleaning the code.

* Fix test_mesh_info issues

* Pre-commit reformat

* Pre commit reformat

* Pre commit reformat

* Update the PR : expose the output as the derived class.

Implementation of the mesh_info in models.

* Reformat code

* Update the PR : reformat and implement setter/getter for mesh_info

* Update the Pr : Reformat code

* Update the PR : avoid circular import of the MeshInfo and reformat code

* Update the PR : filter the CFF tests for Linux

* Update the PR : consistency with DPF side with the use of lower case for derived class name

* Update the PR : still skipped Linux for CFF tests in tests_mesh_info

* Update mesh_info test

* Update the PR : fix retro compatibility issues

* Update PR : reformat

* Update the PR : retro compatibility

* Update the PR : retro compatibility issues

* Update Pr : retro compatibility issues

* Update the PR : retro compatibility issues

* Update the PR : retro compatibility issues

* Update PR : Linux issues

* Update the PR : call the new API for pins with derived class

* Update the PR : reformat code

* Update the PR : fix Linux issues

* Update the PR : reformat code

* test Linux issues : catch if the CFF plugin is loaded

* Update the PR : remove Linux test issue

* Update the PR : complete mesh_info tests

* Update the PR : test fix Linux issues

* Revert last commit

* Update the PR : fix test_mesh_info issue

* Update the PR : test Linux fix

* Update the PR : fix Linux issue

* Update the PR : complete mesh_info tests with loading cff operator test

* Update PR : complete mesh_info tests by adding test concerning the load of CFF operators and model

* Update the PR : correct typo

* Update the PR : run Linux pipeline testing result_info with fluent model in Grpc

* Revert previous test commit

* Update the PR : fix conftests issues for CFF fixture fluent_axial_comp

* Update the PR : reformat code

* Update the PR : add in Process tests in Linux

* Update the PR : reformat code

* Update the PR : removed Linux skip and update fluent fixture

* Update the PR : reformat code

* Update the PR : un-unskip Linux test for result_info

* Update test_resultinfo.py

* Update the PR : reformat code

* Update the PR : fix Linux tests for result_info using CFF model

* Update the PR : fix issue for Linux test in result_info

* Update the PR : un unskip Linux test for result_info

* Update the PR : reviews

* Update the PR : fix issues concerning deep_copy of the MeshInfo class

* Update the PR : satisfy Codacy Static Code Analysis

* Update the PR : server consistency

* Update the PR : fix server issue

---------

Co-authored-by: cbellot <[email protected]>
Co-authored-by: Jose A. Henriquez Roa <[email protected]>
Co-authored-by: Paul Profizi <[email protected]>
  • Loading branch information
4 people authored Jul 12, 2023
1 parent 2b44761 commit bc651fa
Show file tree
Hide file tree
Showing 15 changed files with 898 additions and 65 deletions.
3 changes: 2 additions & 1 deletion src/ansys/dpf/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,9 @@
LicenseContextManager
)
from ansys.dpf.core.unit_system import UnitSystem, unit_systems
from ansys.dpf.core.any import Any
from ansys.dpf.core.mesh_info import MeshInfo
from ansys.dpf.core.generic_data_container import GenericDataContainer
from ansys.dpf.core.any import Any

# for matplotlib
# solves "QApplication: invalid style override passed, ignoring it."
Expand Down
3 changes: 3 additions & 0 deletions src/ansys/dpf/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ class types(Enum):
string_field = 22
custom_type_field = 23
generic_data_container = 24
mesh_info = 25
# Types not from grpc proto, added in Python
fields_container = -1
scopings_container = -2
Expand Down Expand Up @@ -121,6 +122,7 @@ def types_enum_to_types():
workflow,
streams_container,
generic_data_container,
mesh_info,
)
from ansys.dpf.gate import dpf_vector

Expand Down Expand Up @@ -150,6 +152,7 @@ def types_enum_to_types():
types.custom_type_field: custom_type_field.CustomTypeField,
types.streams_container: streams_container.StreamsContainer,
types.generic_data_container: generic_data_container.GenericDataContainer,
types.mesh_info: mesh_info.MeshInfo,
}


Expand Down
5 changes: 5 additions & 0 deletions src/ansys/dpf/core/dpf_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ def _type_to_output_method(self):
collection,
streams_container,
generic_data_container,
mesh_info,
)

out = [
Expand Down Expand Up @@ -369,6 +370,10 @@ def _type_to_output_method(self):
self._api.operator_getoutput_time_freq_support,
"time_freq_support",
),
(
mesh_info.MeshInfo,
"mesh_info",
),
(workflow.Workflow, self._api.operator_getoutput_workflow, "workflow"),
(data_tree.DataTree, self._api.operator_getoutput_data_tree, "data_tree"),
(Operator, self._api.operator_getoutput_operator, "operator"),
Expand Down
1 change: 1 addition & 0 deletions src/ansys/dpf/core/generic_data_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def __init__(self, generic_data_container=None, server=None):

# step 2: if object exists, take the instance, else create it
self._api_instance = None
self._api.init_generic_data_container_environment(self) # creates stub when gRPC

self._api.init_generic_data_container_environment(self)

Expand Down
198 changes: 198 additions & 0 deletions src/ansys/dpf/core/mesh_info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
"""
MeshInfo
==========
"""
from ansys.dpf.core import server as server_module
from ansys.dpf.core.generic_data_container import GenericDataContainer
from ansys.dpf.core.scoping import Scoping
from ansys.dpf.core.string_field import StringField


class MeshInfo:
"""Hold the information relative to a mesh region.
This class describes the available mesh information.
Parameters
----------
server : ansys.dpf.core.server, optional
Server with the channel connected to the remote or local instance.
The default is ``None``, in which case an attempt is made to use the
global server.
generic_data_container : ansys.dpf.core.generic_data_container, optional
Generic data container that is wrapped into the mesh info.
mesh_info : optional
Hold the information of the mesh region into a generic data container.
Examples
--------
Explore the mesh info from the model
>>> from ansys.dpf import core as dpf
>>> from ansys.dpf.core import examples
>>> fluent = examples.fluid_axial_model()
>>> model = dpf.Model(fluent)
>>> mesh_info = model.metadata.mesh_info
"""

def __init__(
self,
generic_data_container=None,
mesh_info=None,
server=None,
):
"""Initialize with a MeshInfo message"""
# ############################
# step 1: get server

if generic_data_container is None and mesh_info is None:
self._server = server_module.get_or_create_server(server)
self._generic_data_container = GenericDataContainer(server=self._server)
elif generic_data_container is not None and mesh_info is None:
self._server = generic_data_container._server
self._generic_data_container = generic_data_container
elif generic_data_container is None and MeshInfo is not None:
self._server = mesh_info.generic_data_container._server
self._generic_data_container = mesh_info.generic_data_container
else:
raise ValueError(
"Arguments generic_data_container and mesh_info are mutually exclusive."
)

@property
def generic_data_container(self) -> GenericDataContainer:
"""GenericDataContainer wrapped into the MeshInfo
that contains all the relative information of the derived class.
Returns
-------
:class:`ansys.dpf.core.generic_data_container.GenericDataContainer`
"""

return self._generic_data_container

@generic_data_container.setter
def generic_data_container(self, value: GenericDataContainer):
"""GenericDataContainer wrapped into the MeshInfo
that contains all the relative information of the derived class.
"""

if not isinstance(value, GenericDataContainer):
raise ValueError("Input value must be a GenericDataContainer.")
self._generic_data_container = value
self._server = self._generic_data_container._server

def deep_copy(self, server=None):
"""Create a deep copy of the mesh_info's data on a given server.
This method is useful for passing data from one server instance to another.
Parameters
----------
server : ansys.dpf.core.server, optional
Server with the channel connected to the remote or local instance.
The default is ``None``, in which case an attempt is made to use the
global server.
Returns
-------
mesh_info_copy : MeshInfo
"""
mesh_info = MeshInfo(server=server)
mesh_info.generic_data_container = self._generic_data_container.deep_copy(server)
return mesh_info

def get_property(self, property_name, output_type):
"""Get property with given name.
Parameters
----------
property_name : str
Property name.
output_type : :class:`ansys.dpf.core.common.types`
Returns
-------
type
Property object instance.
"""
return self.generic_data_container.get_property(property_name, output_type)

def set_property(self, property_name, prop):
"""Register given property with the given name.
Parameters
----------
property_name : str
Property name.
prop : Int, String, Float, Field, StringField, GenericDataContainer, Scoping
object instance.
"""

return self.generic_data_container.set_property(property_name, prop)

@property
def get_number_nodes(self):
"""
Returns
-------
number_nodes : int
Number of nodes of the mesh.
"""

return self.generic_data_container.get_property("num_nodes", int)

@property
def get_number_elements(self):
"""
Returns
-------
number_elements : int
Number of elements of the mesh.
"""

return self.generic_data_container.get_property("num_elements", int)

@property
def get_splittable_by(self):
"""
Returns
-------
splittable by which entity : StringField
Name of the properties according to which the mesh can be split by.
"""

return self.generic_data_container.get_property("splittable_by", StringField)

@property
def get_available_elem_types(self):
"""
Returns
-------
available element types : Scoping
element type available for the mesh.
"""

return self.generic_data_container.get_property("avalaible_elem_type", Scoping)

def set_number_nodes(self, number_of_nodes):
"""Set the number of nodes in the mesh"""

return self.generic_data_container.set_property("num_nodes", number_of_nodes)

def set_number_elements(self, number_of_elements):
"""Set the number of elements in the mesh"""

return self.generic_data_container.set_property("num_elements", number_of_elements)

def set_splittable_by(self, split):
"""Set name of the properties according to which the mesh can be split by"""

return self.generic_data_container.set_property("splittable_by", split)

def set_available_elem_types(self, available_elem_types):
"""Set the available element types"""

return self.generic_data_container.set_property("avalaible_elem_type", available_elem_types)
37 changes: 37 additions & 0 deletions src/ansys/dpf/core/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ def metadata(self):
- ``time_freq_support``
- ``result_info``
- ``mesh_provider``
- ``mesh_info``
Returns
-------
Expand Down Expand Up @@ -268,6 +269,7 @@ def __init__(self, data_sources, server):
self._meshed_region = None
self._meshes_container = None
self._result_info = None
self._mesh_info = None
self._stream_provider = None
self._time_freq_support = None
self._mesh_selection_manager = None
Expand All @@ -279,6 +281,11 @@ def _cache_result_info(self):
if not self._result_info:
self._result_info = self._load_result_info()

def _cache_mesh_info(self):
"""Store mesh information."""
if not self._mesh_info:
self._mesh_info = self._load_mesh_info()

def _cache_streams_provider(self):
"""Create a stream provider and cache it."""
from ansys.dpf.core import operators
Expand Down Expand Up @@ -424,6 +431,22 @@ def _load_result_info(self):
return None
return result_info

def _load_mesh_info(self):
"""Returns a mesh info object"""
op = Operator("mesh_info_provider", server=self._server)
op.inputs.connect(self._stream_provider.outputs)
try:
mesh_info = op.outputs.mesh_info()
except Exception as e:
# give the user a more helpful error
if "results file is not defined in the Data sources" in e.args:
raise RuntimeError("Unable to open result file") from None
else:
raise e
except:
return None
return mesh_info

@property
@protect_source_op_not_found
def meshed_region(self):
Expand Down Expand Up @@ -482,6 +505,20 @@ def result_info(self):

return self._result_info

@property
@version_requires("7.0")
@protect_source_op_not_found
def mesh_info(self):
"""Mesh Info instance.
Returns
-------
mesh_info : :class:`ansys.dpf.core.mesh_info.MeshInfo`
"""
self._cache_mesh_info()

return self._mesh_info

@property
@version_requires("4.0")
def meshes_container(self):
Expand Down
Loading

0 comments on commit bc651fa

Please sign in to comment.