Ansys pyDPF Logical statements
Hi,
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.
Thanks
Best 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 = stress_field.data 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 - https://dpf.docs.pyansys.com/version/stable/examples/00-basic/03-create_entities.html2
Answers
-
Hi @InterGas , PyDPF is based on Python so you can directly use the standard Python statements such as if / while / ... loops.
1 -
Thanks for answering, thought that might be the case
0 -
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
HPop.inputs.field.Connect(my_field)
HPop.inputs.threshold.Connect(my_threshold)Make the low values data all = 0
MakeOneOp= dpf.operators.math.make_one_on_comp() # operator instantiation
MakeOneOp.inputs.fieldA.Connect(HPop)Get the High values
LPop= dpf.operators.filter.field_low_pass() # operator instantiation
LPop.inputs.field.Connect(my_field)
LPop.inputs.threshold.Connect(my_threshold)Merge all this back to one field.
MergeOp = dpf.operators.utility.merge_fields()
MergeOp.inputs.fields1.Connect(LPop)
MergeOp.inputs.fields2.Connect(MakeOneOp)
my_merged_field = MergeOp.outputs.merged_field.GetData()0