How to retrieve MAPDL matids for each body in a custom result?

Pierre Thieffry
Pierre Thieffry Member, Moderator, Employee Posts: 107
5 Likes Name Dropper First Comment First Answer
✭✭✭✭
edited June 2023 in Structures

Customer wants to retrieve the MatID associated to selected bodies in a custom result in 2020R2. How can he do this?

Tagged:

Best Answers

  • Pierre Thieffry
    Pierre Thieffry Member, Moderator, Employee Posts: 107
    5 Likes Name Dropper First Comment First Answer
    ✭✭✭✭
    Answer ✓

    the solver_data structure would be useful in such a case, as it now has a .GetMaterialSolverId(geoBodyId) function. But... this was not available in 2020R2.

    I found out that the information is written to the ds.dat file in a comment block. So here is a sample code that will retrieve the information from the ds.dat file, write out a text file to user_files and display a message in Mechanical with the body ids corresponding to the ACT object selection. Not the most elegant solution, but it works...

    # Custom Result Object Evaluation 
    def EvaluateMatID(result,stepInfo,collector):      
    
        import os
    
        # Reader initialization 
        reader = result.Analysis.GetResultsData()     
        reader.CurrentResultSet = stepInfo.Set 
    
        solution=result.Parent.Solution
    
        matids_dict={}
        # Write a file with body names and corresponding APDL MatID
        f_out=open(os.path.join(result.Analysis.WorkingDir,'../../../user_files','matids.txt'),'w') # File to be written in user_files folder
    
        datfile=os.path.join(result.Analysis.WorkingDir,'ds.dat') # MAPDL input file to be parsed
    
        with open(datfile,'r') as f: # Parse MAPDL file
            line=f.readline()
            while line.find('!************************* Model Summary ********************')==-1: # This indicates where the correspondance between body names and matid are available
                line=f.readline()
            # read body names and matid
            line=f.readline()
            while line.find('!************************* End Model Summary ********************')==-1:  # Read in the comments
                splitline=line.split(',') # Split with "," to separate information in the comments
                f_out.write(splitline[0].strip('!')+'\t'+splitline[-1])  # Write out first part of line (body name) and last part (matid)
                matids_dict[splitline[0].strip('!')]=int(splitline[-1]) # create dictionary with material ids
                line=f.readline()
            
        f_out.close()   
    
        # display message with matid for selected body(ies) for the ACT object
        propGeo = result.Properties["Geometry"]
        refIds = propGeo.Value.Ids
    
        mat_ids=[]
        for bodyId in refIds:
            bodyName=ExtAPI.DataModel.GeoData.GeoEntityById(bodyId).Name
            mat_ids.append(matids_dict[bodyName])
    
        msg = Ansys.Mechanical.Application.Message('Found the following material Ids: '+str(mat_ids), MessageSeverityType.Warning)
        ExtAPI.Application.Messages.Add(msg)
    

Answers

  • CollinsJnr
    CollinsJnr Member Posts: 17
    10 Comments Name Dropper
    **

    @Pierre Thieffry
    Thank you for these code

    I wanted to ask about the

    Ansys.ACT.Interfaces.Analysis.ISolverData.GetMaterialSolverId(geoID)

    the documentation says only one parameter is required (int32)

    But shell shows this

    Ansys.ACT.Interfaces.Analysis.ISolverData.GetMaterialSolverId(26)
    GetMaterialSolverId() takes exactly 2 arguments (1 given)

    And when I try to give a second parameter it shows this

    Ansys.ACT.Interfaces.Analysis.ISolverData.GetMaterialSolverId(ISolverData,26)
    expected ISolverData, got type

    What is this second parameter (ISolerData) and how can I find it?

    Thank you in advance

  • Mike.Thompson
    Mike.Thompson Member, Employee Posts: 330
    25 Answers First Anniversary 100 Comments 25 Likes
    ✭✭✭✭

    If you are trying to get this information for post processing after the solution is done, you should look at the solution object: Solution.SolverData

    If you are trying to get this before the solve while, the input file is being written. The solver data is passed as an argument to the get solved commands Call Back.

  • Pierre Thieffry
    Pierre Thieffry Member, Moderator, Employee Posts: 107
    5 Likes Name Dropper First Comment First Answer
    ✭✭✭✭

    @CollinsJnr to follow up on @Mike.Thompson ' s reply, you can use this in a Python Code with "Get Solve commands" callback:

    geobody_id=741
    matid_for_a_body=Ansys.ACT.Interfaces.Analysis.ISolverData.GetMaterialSolverId(solver_data,geobody_id)
    

    As Mike mentioned, the 'solver_data' info is an argument passed to the callback as you can see from the comments in the Python Code:

    solver_data -- data stucture that allows you to access information from the model such as current step, contact pair ids, etc.

    Using the function as is in the console will lead to the message on the required argument. Nevertheless, the documentation is indeed misleading. I will report internally.