Custom Operator Base#

Contains utilities allowing you to implement and record custom Python operators.

ansys.dpf.core.custom_operator.update_virtual_environment_for_custom_operators(restore_original: bool = False)#

Updates the dpf-site.zip file used to start a venv for Python custom operators to run in.

It updates the site-packages in dpf-site.zip with the site-packages of the current venv. It stores the original dpf-site.zip for future restoration.

Note

This feature is only available InProcess to ensure compatibility of the current venv client-side with the machine where the server is run.

Parameters:

restore_original (bool, default: False) – If True, restores the original dpf-site.zip.

ansys.dpf.core.custom_operator.record_operator(operator_type, *args)#

Add an operator (with its name, run callback, and specification) to the DPF core registry.

Parameters:
  • operator_type (type, CustomOperatorBase) – Class type inheriting from CustomOperatorBase. name and specification properties are called and run method callback is given to DataProcessingCore.

  • *args – Forwarded arguments passed in load_operators method

Return type:

None

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)
set_output(index: int, data)#

Add an output to this Operator at the given index. To use in the run method.

Parameters:
Return type:

None

get_input(index, type: type)#

Method used to 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:
Returns:

data

Return type:

type

set_failed()#

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 the run method.

Return type:

None

set_succeeded()#

Set the Operator’s status to “succeeded”. To use at the end of the run method.

Return type:

None

abstract run()#

Callback of the Operator to implement. The implementation should first request the inputs with the method get_input, compute the output data, then add the outputs with the method set_output and finally call set_succeeded.

Return type:

None

abstract property 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:

CustomSpecification

abstract property name: str#

Returns the identifier or name of the operator. This name can then be used to instantiate the Operator.