Ansys pyDPF Logical statements

InterGas Member Posts: 2
First Comment


I am new to the world of PyDPF and was wondering if there was an if statement or something equivalent as a operator in PyDPF.

I'm looking to compare stresses on a node by node basis against a critical value and if the stress at the node exceeds a value I would like to set that value to 0. I noticed that there was an high pass filter operator but it appears that as an output the filter just deletes the value rather than setting the data to 0.


Best Answer

  • Ayush Kumar
    Ayush Kumar Member, Moderator, Employee Posts: 409
    First Anniversary Ansys Employee Solution Developer Community of Practice Member First Answer
    edited October 2023 Answer ✓

    Hi @InterGas, DPF field arrays are based on NumPy arrays, so you can use NumPy methods as well. For example, copy the data of your stress field in a new array and use the following:

    field_data_new =
    field_data_new[field_data_new < 0.0] = -1.0
    # Create a new field based on the data from "field_data_new"

    This would replace all values below zero with -1.0 and the new array can be forwarded to a DPF field.
    Please refer to PyDPF documentation on how to create new fields -


  • Pernelle Marone-Hitz
    Pernelle Marone-Hitz Member, Moderator, Employee Posts: 804
    First Comment First Anniversary Ansys Employee Solution Developer Community of Practice Member

    Hi @InterGas , PyDPF is based on Python so you can directly use the standard Python statements such as if / while / ... loops.

  • InterGas
    InterGas Member Posts: 2
    First Comment

    Thanks for answering, thought that might be the case

  • Mike.Thompson
    Mike.Thompson Member, Employee Posts: 280
    First Anniversary First Comment 5 Likes Ansys Employee

    @InterGas ,

    This is not an obvious solution, but I think it would work to use this operator:

    math: make one on component
    take the input field's scoping and create a field full of zeros, except for the indexes from pin 1 that will hold 1.0.

    You can use a low pass filter to sift out the low values, leaving the high. Then do a scoping: rescope operator on the original field per the scoping Ids in the low pass filter result. Now you have a field of only the low values, and you can use the "make one on component" operator to make a new field, but with all 0 in the data and the same scoping Ids as the "low value field"

    You can also just use another low pass filter instead of the rescoping operator.

    The last operation is to use: utility: merge fields on the High Values field (original values) and the Field with the low scoping Id, and values ==0.

    This is some code that I have not tested, but I think would do what you are looking to do:

    Get only the low values

    HPop = dpf.operators.filter.field_high_pass() # operator instantiation

    Make the low values data all = 0

    MakeOneOp= dpf.operators.math.make_one_on_comp() # operator instantiation

    Get the High values

    LPop= dpf.operators.filter.field_low_pass() # operator instantiation

    Merge all this back to one field.

    MergeOp = dpf.operators.utility.merge_fields()
    my_merged_field = MergeOp.outputs.merged_field.GetData()