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