Convert PlotData of User Defined Result to a DPF field and plot it

Member, Moderator, Employee Posts: 898
100 Answers 500 Comments 250 Likes Second Anniversary
✭✭✭✭
edited June 2023 in Structures

In Mechanical scripting, how can I retrieve the data from a user-defined result and plot it as a Python Result ?

Tagged:

Welcome!

It looks like you're new here. Sign in or register to get started.

Answers

  • Member, Moderator, Employee Posts: 898
    100 Answers 500 Comments 250 Likes Second Anniversary
    ✭✭✭✭
    edited March 2023

    A user-defined result (UDR) is inserted in Mechanical :

    image.png

    A property is added to the Python Result for the user to provide the name of the UDR:

    image.png

    This is achieved by modifying the Property Provider of the Python Result:

    image.png

    The piece of code of interest here is:

    1. def reload_props():
    2.     this.PropertyProvider = None
    3.  
    4.     # Create the property instance
    5.     provider = Provider()
    6.     
    7.     input_group = provider.AddGroup("Input")
    8.     ns_prop = input_group.AddProperty("UDR Name", Control.Expression)
    9.     
    10.     # Connects the provider instance back to the object by setting the PropertyProvider member on this, 'this' being the 
    11.     # current instance of the Python Code object.
    12.     this.PropertyProvider = provider
    13.  
    14.  

    Then, the Script part of the Python Result is edited:

    image.png

    The code that is needed here is :

    1. def post_started(sender, analysis):# Do not edit this line
    2.     define_dpf_workflow(analysis)
    3.  
    4. def define_dpf_workflow(analysis):
    5.     
    6.     # import DPF
    7.     import mech_dpf
    8.     import Ans.DataProcessing as dpf
    9.     mech_dpf.setExtAPI(ExtAPI)
    10.     
    11.     # Connect to result file
    12.     dataSource = dpf.DataSources(analysis.ResultFileName)
    13.     
    14.     # Retrieve mesh 
    15.     model=dpf.Model(dataSource)
    16.     mesh=model.Mesh # whole mesh
    17.     
    18.     # Retrieve property values
    19.     udr_name = str(this.GetCustomPropertyByPath("Input/UDR Name").Value)
    20.     
    21.     # Access User Defined Result and create a field out of it
    22.     udr = ExtAPI.DataModel.GetObjectsByName(udr_name)[0]
    23.     nb_nodes = len(udr.PlotData['Node']) # get number of nodes 
    24.     
    25.     udr_field = dpf.FieldsFactory.CreateScalarField(nb_nodes) # instanciate field
    26.     udr_field.MeshedRegionSupport = mesh # attach mesh
    27.     udr_field.ScopingIds = udr.PlotData['Node'] # give list of nodes
    28.     udr_field.Data = [0. for i in range(0,nb_nodes)]# fill field with zeros
    29.     node_ids = udr.PlotData['Node']
    30.     values = [value for value in udr.PlotData['Values']]
    31.     for ii in range(0,nb_nodes):
    32.         index=udr_field.ScopingIds.IndexOf(node_ids[ii])         # get index of node in created field
    33.         udr_field.UpdateEntityDataByEntityIndex(index,[values[ii]])  
    34.     
    35.     # Create field operator for plotting
    36.     forward = dpf.operators.utility.forward_field()
    37.     forward.inputs.field.Connect(udr_field)
    38.     
    39.     dpf_workflow = dpf.Workflow()
    40.     dpf_workflow.Add(forward)
    41.     dpf_workflow.SetOutputContour(forward)
    42.     dpf_workflow.Record('wf_id', False)
    43.     this.WorkflowId = dpf_workflow.GetRecordedId()

    This code grabs the UDR, extract the values in it thanks to PlotData, and passes those values to a DPF Field to be able to plot it in the Python Result object.

  • Member Posts: 5
    First Anniversary First Comment
    **

    Pernelle:
    Thanks so much for posting this!
    I was able to do some (work) cool application by mouse click applying it :) but it failed with automation :(
    Why? DPF Python Result is "def post_started(sender, analysis):", i.e. it runs before UDR data is populated by post. On the other hand, a Python Code object is "def after_post(this, solution)", i.e. it runs after UDR data is populated by post - what is needed to use UDR results in DPF with automation. Do you have any suggestions for how to do this? I tried putting time.sleep(time_sleep) in the DPF code but it did not work.
    Cordially,
    Ray

  • Member, Moderator, Employee Posts: 898
    100 Answers 500 Comments 250 Likes Second Anniversary
    ✭✭✭✭

    Hi @Ray_Greene , glad you found that post useful! Indeed I hadn't realized this would not work in an automated way. There is unfortunately no workaround possible, the only solution would be to execute the two objects manually and in a successive manner.
    I also tried having a Python Code object in "after post" unsuppress the Python Result and evaluate, but Python Results do not have a Suppressed / Unsuppressed status so this is not a viable path either.
    Looks like something that could be improved, I'll let the developers know.

Welcome!

It looks like you're new here. Sign in or register to get started.