I want to calculate signed von mises in Python Result. I see there is a long winded way.
If you want to display this in Mechanical, creating a Python Result is the way to go. In that case you'll be using Mechanical DPF (and not PyDPF). There are many examples in this forum that could help.
Thanks Pernelle. What is the operator to extract the sign of largest principal stress in absolute value. I used min_max max by component to get the largest abs (principal stress), but not the sign. Please help.
Perhaps @Ayush Kumar can help?
@DSSim I don't believe we have an operator in DPF to get the sign of the absolute max / min. You can combine DPF and Numpy to get the desired results.
import numpy as np from ansys.dpf import core as dpf dpf.start_local_server(ansys_path=r"C:\Program Files\ANSYS Inc\v252") rst = r"D:\…\file.rst" model = dpf.Model(rst) stress = model.results.stress() s1 = dpf.operators.invariant.principal_invariants_fc(fields_container=stress).eval() arr = s1[0].data # 1. Find the index of the maximum absolute value idx = np.argmax(np.abs(arr)) # 2. Get the sign of the value at that index result_sign = np.sign(arr.flat[idx]) print(f"Sign of highest absolute value: {result_sign}")
Here is an example of doing this as a Pyhon Result in Mechanical. If anyone know a way to operate on the fields containers (rather than looping through the timepoints) let me know. Also note that I have to use the divide operator and divide the von-mises stress by the sign of S1/S3 instead of multiplying, because the multiply operator works differently than the divide which does not make a lot of sense.
def define_dpf_workflow(analysis): import mech_dpf import Ans.DataProcessing as dpf mech_dpf.setExtAPI(ExtAPI) dataSource = dpf.DataSources(analysis.ResultFileName) # Get the model and times from the RST file model = dpf.Model(analysis.ResultFileName) tpts=model.TimeFreqSupport.NumberSets ts = dpf.Scoping() ts.Ids = range(1, tpts+1) # Define the operators for stress and temperature extraction, and the component selector for the stress components s=dpf.operators.result.stress() s.inputs.data_sources.Connect(dataSource) s.inputs.time_scoping.Connect(ts) s.inputs.read_cyclic.Connect(0) s.inputs.requested_location.Connect("Nodal") s_out=s.outputs.fields_container.GetData() # 1) principal stresses prin = dpf.operators.invariant.principal_invariants_fc() prin.inputs.fields_container.Connect(s_out) # 2) S1 and S3 from the principal stress field s1 = prin.outputs.fields_eig_1.GetData() s3 = prin.outputs.fields_eig_3.GetData() # 3) von-Mises stress vm = dpf.operators.invariant.von_mises_eqv_fc() vm.inputs.fields_container.Connect(s_out) vm_out = vm.outputs.fields_container.GetData() # 4) build sign factors with operators abs_s1 = dpf.operators.math.absolute_value_by_component_fc() abs_s1.inputs.fields_container.Connect(s1) abs_s1_out = abs_s1.outputs.fields_container.GetData() abs_s3 = dpf.operators.math.absolute_value_by_component_fc() abs_s3.inputs.fields_container.Connect(s3) abs_s3_out = abs_s3.outputs.fields_container.GetData() sign_s1 = dpf.operators.math.component_wise_divide_fc() sign_s1.inputs.fields_containerA.Connect(s1) sign_s1.inputs.fields_containerB.Connect(abs_s1_out) sign_s1_out = sign_s1.outputs.fields_container.GetData() sign_s3 = dpf.operators.math.component_wise_divide_fc() sign_s3.inputs.fields_containerA.Connect(s3) sign_s3.inputs.fields_containerB.Connect(abs_s3_out) sign_s3_out = sign_s3.outputs.fields_container.GetData() # 5) candidate fields without looping candidate_s1 = dpf.operators.math.component_wise_divide_fc() candidate_s1.inputs.fields_containerA.Connect(vm_out) candidate_s1.inputs.fields_containerB.Connect(sign_s1_out) can_s1_out = candidate_s1.outputs.fields_container.GetData() candidate_s3 = dpf.operators.math.component_wise_divide_fc() candidate_s3.inputs.fields_containerA.Connect(vm_out) candidate_s3.inputs.fields_containerB.Connect(sign_s3_out) can_s3_out = candidate_s3.outputs.fields_container.GetData() for t in range(1, tpts+1): s1absdata = list(abs_s1_out[t-1].Data) s3absdata = list(abs_s3_out[t-1].Data) cand1data = can_s1_out[t-1].Data cand2data = can_s3_out[t-1].Data condition = [x > y for x, y in zip(s1absdata, s3absdata)] signed_vm = [val1 if cond else val2 for val1, cond, val2 in zip(cand1data, condition, cand2data)] # Replace the original von-Mises stress with the signed one vm_out[t-1].Data = signed_vm fwd_op = dpf.operators.utility.forward_fields_container() fwd_op.inputs.fields.Connect(vm_out) dpf_workflow = dpf.Workflow() dpf_workflow.Add(fwd_op) # dpf_workflow.SetInputName(u, 0, 'time') # dpf_workflow.Connect('time', timeScop) dpf_workflow.SetOutputContour(fwd_op) dpf_workflow.Record('wf_id', False) this.WorkflowId = dpf_workflow.GetRecordedId()