Quick Tip: Concatenating Text Files Using ANSYS Mechanical APDL

So you have text output from some ANSYS analysis and you wish you could just do this:

cat lift.txt  cop.txt drag.txt >> results.txt

and you are writing an ANSYS macro and want it to run on all platforms.  The following macro will use APDL commands to join the files together. 

macro1.mac

/inquire,linesin1,lines,lift,txt

*sread,str1array,lift,txt,,80,,linesin1

/inquire,linesin2,lines,cop,txt

*sread,str2array,cop,txt,,80,,linesin2

/inquire,linesin3,lines,drag,txt

*sread,str3array,drag,txt,,80,,linesin3

*cfopen,results,txt

*vlen,linesin1

*vwrite,str1array(1)

%80S

*vlen,linesin2

*vwrite,str2array(1)

%80S

*vlen,linesin3

*vwrite,str3array(1)

%80S

*cfclose

Bonus: If you want to strip some lines off of the top or read less than all the lines, you can pass additional arguments to *sread:

/inquire,linesin1,lines,lift,txt

Lines_skip=5

Lines_read=linesin1-lines_skip

*sread,str1array,lift,txt,,80,lines_skip,lines_read

 

Serial and Parallel ANSYS Mechanical APDL Simulations

ANSYS-APDL-Macro-PeDALThere are times when you want to study the effects of varying parameters.  If you have an existing MAPDL script that is parameterized, the following procedure will allow you to easily run many variations in an organized manner. 

Let’s assume a parameterized MAPDL macro called build_solve that does something you want to simulate many times and has 2 variables called power and scale which are set with argument 1 and 2 respectively.  Running this macro with the classic interface, with power=30 and scale=2.5 would look like this:

build_solve,30,2.5

Next, create a MAPDL macro to launch all of the simulations.  This script could be named control.mac.  The first thing to do here is to create arrays of your parameters and assign values to them.  This example will vary power and scale.  Here are the arrays of values that will be passed to build_solve:

*dim,power,array,4

power(1)=10,20,40,80

*dim,scale,array,6

scale(1)=1,2,3,5,10,20

Most of the control.mac commands will be put inside of nested *do loops.  There will be a *do loop for each of parameters being varied.

*do,ii,1,4

*do,jj,1,6

Next, use *cfopen to set up the arguments to be passed to build_solve.  Each time through the *do loops will create a new run1.mac

*cfopen,run1,mac

  a=power(ii)

  b=scale(jj)

  *vwrite,a,b

  build_solve,%G,%G

*cfclose

One of the key features of this approach is to run anywhere and build directories below the working directory.  Use the /inquire command to store the current directory name.

/INQUIRE,dir_,DIRECTORY

Use *cfopen to create a string that will be used for the directory name.  By using the variables as part of the string, the directories will have unique names.  A time or date stamp could also be included in this string.  This macro is executed immediately to create the string dirnam for use in the commands subsequently.

*dim,power,array,4

*cfopen,temp1,mac

*vwrite,a,b

dirnam='power_%G_scale_%G'

*cfclose

/input,temp1,mac

Eventually, the resulting directory structure will look something like the image below.  Each directory will contain a separate simulation with the arguments of power and scale set respectively.

mapdl-script-automation

The last *cfopen creates a windows batch file which will (when executed)

  1. Create the new directory

  2. Copy all of the macro files from the working directory into the new directory (including run1.mac)

  3. Change into the new directory using CD

  4. Launch ansys in batch mode, in this case using a gpu and 12 cpus, using the run1.mac input and outputting to f.out

  5. Change back to the working directory (ready to do it all again)

The code for the windows batch file is:

*cfopen,rfile,bat

*vwrite,dir_(1),dirnam

MKDIR "%C\%S"

*vwrite,dir_(1),dirnam

COPY *.mac "%C\%S"

*vwrite,dir_(1),dirnam

CD "%C\%S"

*vwrite,

"C:\Program Files\ANSYS Inc\v150\ansys\bin\winx64\ansys150" -b -acc nvidia -np 12 -i run1.mac -o f.out

*vwrite,dir_(1)

CD "%C"

*cfclose

The last step is to run the windows batch file.  /sys is used to make this system call.  If the simulation is not well parallelized and you have enough licenses available, run the simulations in low priority mode immediately.  This will launch all of your simulations in parallel:

  • /sys,start /b /low rfile.bat

If the model is well parallelized (in other words, it will use your system’s gpu/cpus/RAM efficiently) or you only have 1 license available, launch the batch files in high priority mode and use the /wait option which will insure that windows waits for the job to finish before launching the next simulation.

  • /sys,start /b /high /wait rfile.bat

You can download and view the examples control.mac and build_solve.mac from this zip file: build_solve-control-macros.zip

Checking Hyper-Elastic Material Models

non-linear-thumbWhen using hyper-elastic materials, analyst often have little material data to assist them. Fortunate engineers will have a tensile stress-strain curve; a lucky few will also have a simple shear stress-strain curve as well. Where do you start?

To gain confidence in the procedure which is typically used, a set of FEA models were run in a closed loop. The loop consists of assuming some material parameters, running FEA models based upon those parameters, and then using the FEA results to recover the material parameters using ANSYS’s built in hyper-elastic curve fitting.

To isolate the material model from boundary conditions effects, simple FEA models that are 3D but have 1D stress states are used. The figures below show tensile and shear models that can be used to verify material models.
cs1

For this article, a 2 Parameter Mooney-Rivlin material model with values consistent with typical Imperial units was selected. The figure below shows the data entry including a value of zero for d which indicates that the material is fully incompressible.
cs2

The tensile test FEA model was run with this 2 parameter MR model. The engineering stress-strain results were extracted from the results using /post26 APDL. The results are graphed and listed in the figure below. We use APDL because there are some calculations involved with getting engineering results. For example, the engineering stress was calculated by dividing the reaction force at node n1 by the original area like this:

RFORCE,2,n1,F,z,Fz_2
QUOT,3,2, , , , , ,-1/area_,1,

cs3This test data was then used in ANSYS’s curve fitting routine. The results of the curve fitting are shown below. The parameters from the curve fitting results are < 0.01% different than the assumed inputs. This is a reassuring result. Note that this is one instance in ANSYS that you are required to use engineering data (for hyper-elastic curve fitting only).
cs4

In recent versions of ANSYS, a hyper-elastic response function was introduced. This allows the user to enter the test data and use it without curve fitting. The figure below shows how uniaxial tension test data is entered and the response function activated to use it.
cs5

As expected, the response function matched the /post26 output exactly. This method offers a clear advantage in that the user doesn’t need to assume a material model.
cs6

The next step in this verification process was to run some simple shear FEA models to compare the curve fitting results. The plot below shows the engineering shear stress-strain curve using the 2 parameter MR model from above.
cs7

The data was curve fitted as shown in the figure below. This time both the uniaxial tension and simple shear data are entered. The resulting 2 parameter model differs (<2%) from the entered model.
cs8

These new values were used in the FEA models. As shown in the figures below, the change in material parameters (<2%) did not significantly change the tensile or shear stress-strain results (<1%). This raises some interesting questions regarding the 2 parameter MR model that will be explored at a later date.
cs9