Exporting results to text file in ANSYS APDL
Let's say that we've solved 10 load steps in a large finite element model of a WTG foundation. Our objective is to extract the vertical stresses (sy) along a horizontal line in the soil, consisting of over 100 nodes.
Manually extracting these values is not only cumbersome but also prone to errors. Moreover, if this task needs to be repeated frequently, automating the process is a must. Let's see how we can achieve this using ANSYS APDL.
Selecting nodes
The line we're interested in is located at the top of the soil, specifically at the interface with the foundation in the central section. It extends from x = -13
to x = +13
, with fixed coordinates at y = -3
, z = 0
.
To select the nodes along this line, we'll use the command NSEL
, which stands for nodes selection. First we'll create a new set of selected nodes with NSEL, S
, and then we'll re-select subsets of nodes with NSEL, R
.
ALLSEL
NSEL, S, LOC, X, -13, 13
NSEL, R, LOC, Z, 0
NSEL, R, LOC, Y, -3
Looping through the results
Now we're going to create a two-dimensional array to store the results. Each row will contain the information of a node, totaling 100 rows. The columns will include the node number, the x-coordinate, and 10 additional columns, one for each load step, resulting in 12 columns.
First, we'll declare the RESULTS
array:
*DIM, RESULTS, ARRAY, 100, 2+10
We can populate the first column of our RESULTS
array with the selected node numbers using the command *VGET
:
*VGET, RESULTS(1, 1), NODE, , NLIST
Then, we loop through the load steps and nodes to retrieve the vertical stresses.
*DO, loadstep, 1, 10
set,loadstep
*DO, node_index, 1, 100
node_num = RESULTS(node_index, 1)
*GET, RESULTS(node_index, 2), NODE, node_num, LOC, X
*GET, RESULTS(node_index, 2+loadstep), NODE, node_num, S, Y
*ENDDO
*ENDDO
A nice improvement here could be to define the number of nodes with a parameter. By using the *GET
command, we can assign the number of selected nodes to a variable, further automating our code:
*GET, num_nodes, NODE, , COUNT
Now, we can create the results array using this variable, and incorporate it into the loop:
*DIM, RESULTS, ARRAY, num_nodes, 2+10
*DO, loadstep, 1, 10
set,loadstep
*DO, node_index, 1, num_nodes
node_num = RESULTS(node_index, 1)
*GET, RESULTS(node_index, 2), NODE, node_num, LOC, X
*GET, RESULTS(node_index, 2+loadstep), NODE, node_num, S, Y
*ENDDO
*ENDDO
Saving an array as a text file
Once the RESULTS
array is filled with the information we need, we have to export it to a text file. We use the *MWRITE
command. This command requires two lines: one for the command itself, and one for the format specification.
The syntax for the command line:
*MWRITE, ParR, Fname, Ext, --, Label, n1, n2, n3
The second line specifies the number of columns, the space reserved for each column (including sign and decimal point), and the number of decimal places. For instance, to define two columns with 10 reserved for numbers with 3 deciaml digits, we use:
%10.3F %10.3
In our example, we'll use integers without decimals for the node numbers and floats with 6 decimal digits for the remaining columns:
*MWRITE, RESULTS(1,1), Soil_SY, txt
%10.0F %10.6F %15.6F %15.6F %15.6F %15.6F %15.6F %15.6F %15.6F %15.6F %15.6F %15.6F
Complete code
Combining all the steps and including clarifying comments, we achieve our automated post-processing of soil stress for the WTG foundation. Below is the complete code:
/POST1
num_loadsteps=10
! Select nodes
ALLSEL
NSEL, S, LOC, X, -13, 13
NSEL, R, LOC, Z, 0
NSEL, R, LOC, Y, -3
! Define array to store results
*GET, num_nodes, NODE, , COUNT
*DIM, RESULTS, ARRAY, num_nodes, 2+num_loadsteps
! Get the node numbers into the first column of the array
*VGET, RESULTS(1, 1), NODE, , NLIST
! Loop through the load steps
*DO, loadstep, 1, num_loadsteps
! Load the current load step
set,loadstep
! Loop through the nodes
*DO, node_index, 1, num_nodes
! Read the node number
node_num = RESULTS(node_index, 1)
! Get the x-coordinate of the node
*GET, RESULTS(node_index, 2), NODE, node_num, LOC, X
! Get the vertical stress (sy) at the node for the current load step
*GET, RESULTS(node_index, 2+loadstep), NODE, node_num, S, Y
*ENDDO
*ENDDO
! Save the results array to a text file.
! Columns in results file: node_num, X, SY load step 1 to load step 10
*MWRITE, RESULTS(1,1), Soil_SY, txt
%10.0F %10.6F %15.6F %15.6F %15.6F %15.6F %15.6F %15.6F %15.6F %15.6F %15.6F %15.6F
By the way, if you're interested in learning all the ins and outs of automating finite element modelling and analysis with ANSYS APDL, we're preparing a course that will interest you. Sign up for the ANSYS APDL course whitelist.