General Mechanical

General Mechanical

Topics related to Mechanical Enterprise, Motion, Additive Print and more.

Automated Import of Load history data uisng scripts (Python or Mapdl)

    • user deleted
      Subscriber

      I am trying to design multiple signals for ansys transient-structural. 

      My signals are many, and this makes it tedious for me to import the data before each run.

      Is there a way to automate the import process?

      I already have a code to modify the.XML file 

    • Erik Kostson
      Ansys Employee

      Hi
      You can use scripting for something like that (automate load application workflow), but again do not understand exactly what you want to do (you mention xml, where does that come in??, and what is multiple signals - there is nothing called signal(s) in Ansys Transient Analysis).


      This post gives an idea how to do something like importing data from a csv file and applying it as a load.
      https://discuss.ansys.com/discussion/comment/2028#Comment_2028?utm_source=community-search&utm_medium=organic-search&utm_term=import+load+and+csv

      We hope this helps.
      All the best
      Erik

       

    • dlooman
      Ansys Employee

      The MAPDL way is with *TREAD.  The commands below read a csv file named time_adj.csv with 101 rows of data to create the table array, time_adj, which is a function of time.  This array can be used as transient input: d,all,ux,%time_adj%

      *dim,time_adj,table,101,1,1,TIME
      *tread,time_adj(1,0),time_adj,csv

      The first few lines of the csv file look like below.  There is a row 0 with arbitrary integers 0 and 1.  The data could also be space delimited.

      0   1
      0,0.018315639
      0.1,0.021459239
      0.2,0.025062063
      0.3,0.029176257
      0.4,0.033857322
      0.5,0.039163895
      0.6,0.04515745
      0.7,0.051901894
      0.8,0.05946306
      0.9,0.067908097
      1,0.07730474
      1.1,0.08772047
      1.2,0.099221555

      ...

       

    • user deleted
      Subscriber

      Hi, @Erik and @Dave

      Thank you for your reply.

      This is what I am trying to do.

      In my simulation, I have two force inputs; these inputs are defined as components, and the Y component is a Tabular Data. 

      1. I exported a .xml from each force input (this process is to have an Ansys .xml file format that I can use for the steps that follow).

      2. I design an excitation signal in Python and then put this signal data, which is a function of time, in the .xml file.

      3. Go back to the Ansys workbench and import this .xml file (using the Import Load History Data. since I have two forces input, I do this twice).

      4. Run the simulation.

      5. Export the acceleration probe data to a .csv using a Python script.

       

      My Challenge: I test different excitation signal configurations and types, and each time I generate a signal, I have to import for all input point which is tedious.

       

      My Question: This import and assignment process (import .xml, select force data in the imported xml file) can I automate it such that when I run the simulation, it reads the excitation data from somewhere and assigns it to a particular force input?

      @Dave

      I previously tried the Tread technique, but I don't know how to assign it to a force input in my model. 

      Thank you so much in advance.

       

       

       

    • user deleted
      Subscriber
    • user deleted
      Subscriber

      Also, I have written the code to export the acceleration probe data (Python in the mechanical scripting environment).

      After each simulation I have to manually run the code. 

       

      Can I make the code automatically run at the end of the simulation?

      Thank you

    • user deleted
      Subscriber

       

       

      Hey @Erik and @Dave,

      Thank you. I figured out the automated import of load history data.

      I have the code running in the mechanical script.

      Is it possible to have it run automatically at the beginning of the simulation?


      This is what I have tried:
      Using the code from the mechanical script, I tried this: Script Tip Friday- "Python Code"​ object in Mechanical | Ansys Developer Portal
      but I did not get any results

       

       

       

    • user deleted
      Subscriber
      This is my code for the Input force
      import csv
      filePath = 'FilePath'
      timesteps = [] #Create empty list to store timestep values
      loads = [] #Create empty list to store load values
      with open(filePath, 'r') as csvFile:
          csvRead = csv.reader(csvFile, delimiter=',')
          next(csvRead) #Skip first line (assumed headers)
          
          for row in csvRead:
              timesteps.append(row[0]) #Populate list with first column's values
              loads.append(row[1]) #Populate list with second column's values
       
      #Configure lists
      timeUnit = '[sec]' #Timestep unit
      loadUnit = '[N]' #Load unit
      timesteps = [str(time) + timeUnit for time in timesteps] #Convert values to strings and append unit
      loads = [str(load) + loadUnit for load in loads] #Convert values to strings and append unit
      #Create load
      analysis_104 = DataModel.GetObjectById(207)
      force_207 = DataModel.GetObjectById(207)
      force_207.Magnitude.Inputs[0].DiscreteValues = [Quantity(time) for time in timesteps]
      force_207.Magnitude.Output.DiscreteValues = [Quantity(load) for load in loads]
       
    • dlooman
      Ansys Employee

      If you were applying the force to a single node, the command to input the MAPDL table would be f,node_numb,fx,%time_adj%.  If you are applying it to an area you could convert your csv data to a pressure by dividing by the area in excel and use the commands:

      cmsel,s,named_selection     ! Mechanical named selection for the face.  Or could select some other way

      sf,all,pres,%time_adj%

      allsel

    • user deleted
      Subscriber

      Thank you, Dave

      I already got the Python code to import the Load history data.

      My problem is making it run automatically at the beginning of the solve. 

       

    • user deleted
      Subscriber

      For anyone looking through this thread in the future

      This is the code adapted to componentwise force import (in this case Y direction)

      import csv
      filePath = 'filepath\\force.csv'
      timesteps = [] #Create empty list to store timestep values
      loads = [] #Create empty list to store load values
      with open(filePath, 'r') as csvFile:
          csvRead = csv.reader(csvFile, delimiter=',')
          next(csvRead) #Skip first line (assumed headers)
          
          for row in csvRead:
              timesteps.append(row[0]) #Populate list with first column's values
              loads.append(row[1]) #Populate list with second column's values
       
      #Configure lists
      timeUnit = '[sec]' #Timestep unit
      loadUnit = '[N]' #Load unit
      timesteps = [str(time) + timeUnit for time in timesteps] #Convert values to strings and append unit
      loads = [str(load) + loadUnit for load in loads] #Convert values to strings and append unit
      #Create load
      analysis_104 = DataModel.GetObjectById(207)
      force_207 = DataModel.GetObjectById(207)
      selection = ExtAPI.SelectionManager.CreateSelectionInfo(SelectionTypeEnum.GeometryEntities)
      selection.Ids = [23]
      force_207.Location = selection
      force_207.DefineBy = LoadDefineBy.ComponentY
      force_207.YComponent.Inputs[0].DiscreteValues = [Quantity(time) for time in timesteps]
      force_207.YComponent.Output.DiscreteValues = [Quantity(load) for load in loads]
       
    • dlooman
      Ansys Employee

      Thanks for sharing that with the forum.

Viewing 11 reply threads
  • The topic ‘Automated Import of Load history data uisng scripts (Python or Mapdl)’ is closed to new replies.