How to plot elemental custom results coming from a txt file in Mechanical?

Options
Javier Vique
Javier Vique Member, Employee Posts: 76
First Answer First Anniversary Name Dropper 5 Likes
edited June 2023 in Structures

I am an standard user of Mechanical, with no DPF background. I have got an in-house tool which makes a evaluation of my structure giving elemental custom results which can be written into a txt/csv file. I want to plot these elemental custom results in Mechanical.

Tagged:

Answers

  • Javier Vique
    Javier Vique Member, Employee Posts: 76
    First Answer First Anniversary Name Dropper 5 Likes
    Options

    Python Result object allows users to plot elemental custom results in a very easily and intuitive way. The script given below just need a txt file called my_result.txt in the users_file folder of WB project.

    def define_dpf_workflow(analysis):
        import mech_dpf
        import Ans.DataProcessing as dpf
        import os
    
        analysis = ExtAPI.DataModel.Project.Model.Analyses[0]
        user_files_dir = os.path.join(analysis.WorkingDir,'../../../user_files')
        ResultFile = os.path.join(user_files_dir, 'my_result.txt')
        
        elements = []
        result = []
        
        f = open(ResultFile, "r")
        Lines = f.readlines()
        for Line in Lines:
            Line = Line.split('\t')
            elements.append(float(Line[0]))
            result.append(float(Line[1].strip()))
        
        my_field = dpf.FieldsFactory.CreateScalarField(len(elements),'Elemental')
        [my_field.Add(id = elements[i], data = [result[i]]) for i in range(len(elements))]
        
        op = dpf.operators.utility.forward_fields_container()
        op.inputs.fields.Connect(my_field)
    
        dpf_workflow = dpf.Workflow()
        dpf_workflow.Add(op)
        # dpf_workflow.SetInputName(u, 0, 'time')
        # dpf_workflow.Connect('time', timeScop)
        dpf_workflow.SetOutputContour(op)
        dpf_workflow.Record('wf_id', False)
        this.WorkflowId = dpf_workflow.GetRecordedId()
    
  • amusthaf
    amusthaf Member Posts: 16
    First Anniversary Name Dropper First Comment
    edited October 2023
    Options

    Hello,

    I used the same script to plot the results from a .txt file in Mechanical.

    But how do I include the units in the same display?

    @Pernelle Marone-Hitz @Ayush Kumar @Mike.Thompson @Javier Vique any ideas?

  • Javier Vique
    Javier Vique Member, Employee Posts: 76
    First Answer First Anniversary Name Dropper 5 Likes
    Options

    Hi @amusthaf !
    Any field has some units associated. You can check them using my_field.Unit.
    So if you want your custom field to have mm as unit, it is as simple as including an additional line with:

    my_field.Unit = 'mm'

    Regards,
    Javier.

  • amusthaf
    amusthaf Member Posts: 16
    First Anniversary Name Dropper First Comment
    Options

    Hi @Javier Vique !!! Thank you for your reply! :)

    I tried the same and it is working fine! But, when I try like:

    my_field.Unit = '%'

    It is not working. Any ideas here? Any documentations to refer?

  • Javier Vique
    Javier Vique Member, Employee Posts: 76
    First Answer First Anniversary Name Dropper 5 Likes
    Options

    Hi @amusthaf,
    % is not an unit, so you cannot expect that working.
    What you can do is to keep your field unitless and rename your Python Code with "MyResult in %", replacing MyResult to whatever you want.
    Regards, Javier.