CustomOperatorBase
#
- class ansys.dpf.core.custom_operator.CustomOperatorBase#
Base class interfacing CPython Custom Operators which can be used as regular DPF Operators in any API.
A CustomOperator is defined by its name, its specification and its run method. These three abstract methods should be implemented to create a CustomOperator.
Examples
Create a Custom Operator which adds an input float value to the data of an input Field.
>>> from ansys.dpf.core.custom_operator import CustomOperatorBase >>> from ansys.dpf.core.operator_specification import CustomSpecification, SpecificationProperties, PinSpecification >>> from ansys.dpf.core import Field >>> class AddFloatToFieldData(CustomOperatorBase): ... def run(self): ... field = self.get_input(0, Field) ... to_add = self.get_input(1, float) ... data = field.data ... data += to_add ... self.set_output(0, field) ... self.set_succeeded() ... ... @property ... def specification(self): ... spec = CustomSpecification() ... spec.description = "Add a custom value to all the data of an input Field" ... spec.inputs = { ... 0: PinSpecification("field", [Field], "Field on which float value is added."), ... 1: PinSpecification("to_add", [float], "Data to add.") } ... spec.outputs = { ... 0: PinSpecification("field", [Field], "Updated field.")} ... spec.properties = SpecificationProperties("custom add to field", "math") ... return spec ... ... @property ... def name(self): ... return "custom_add_to_field"
And record it:
>>> from ansys.dpf.core.custom_operator import record_operator >>> def load_operators(*args): ... record_operator(AddFloatToFieldData, *args)
Overview#
“Implement the Operator’s callback in inheriting subclasses. |
Add an output to this Operator at the given index. |
|
Get an input of a requested type at a given index in the |
|
Set the Operator’s status to “failed”. |
|
Set the Operator’s status to “succeeded”. |
Documents the operator. |
|
Returns the identifier or name of the operator. |
Import detail#
from ansys.dpf.core.custom_operator import CustomOperatorBase
Property detail#
- abstract property CustomOperatorBase.specification#
Documents the operator.
The following are mandatory to have a full support (documentation, code generation and usage) of the new operator: * Description * Supported inputs (a name, a document, a list of accepted types (optional) and/or ellipses) * Supported outputs (a name, a document, a type, and can be ellipsis) * User name * Category
- Returns:
spec
- Return type:
- property CustomOperatorBase.name: str#
- Abstractmethod:
Returns the identifier or name of the operator.
This name can then be used to instantiate the Operator.
Method detail#
- CustomOperatorBase.set_output(index: int, data) None #
Add an output to this Operator at the given index.
To use in the
run
method.- Parameters:
index (int) – Index of the output.
data (int, float, Field, Scoping, DataSources, FieldsContainer...) – Data of any supported type to return.
- CustomOperatorBase.get_input(index, type: CustomOperatorBase.get_input.type)#
Get an input of a requested type at a given index in the
run
method.The correct input type must be connected to this Operator beforehand.
- Parameters:
index (int) – Index of the input.
type (type,
ansys.dpf.core.common.types
) – Expected type of the data.
- Returns:
data
- Return type:
type
- CustomOperatorBase.set_failed() None #
Set the Operator’s status to “failed”.
To use in the
run
method if an error occurred. This “failed” status is automatically set when an exception is raised in therun
method.
- CustomOperatorBase.set_succeeded() None #
Set the Operator’s status to “succeeded”.
To use at the end of the
run
method.
- abstract CustomOperatorBase.run() None #
“Implement the Operator’s callback in inheriting subclasses.
The implementation should first request the inputs with the method
get_input
, compute the output data, then add the outputs with the methodset_output
and finally callset_succeeded
.