Writing Text files with *VWRITE

A very common need in the world of ANSYS FEA simulation is to write text to a text file from within Mechanical APDL. Sometimes you are running in MAPDL, sometimes you are using ANSYS Mechanical but you still need to write stuff out using APDL with a code snippet. The way most people do that is through writing text files with *VWRITE.

Originally written to write out data in arrays, it is a very flexible and powerful command that can be used to write pretty much any type of formatted output. Something that every ANSYS user should have in their back pocket.

The Command

*VWRITE, Par1, Par2, Par3, Par4, Par5, Par6, Par7, Par8, Par9, Par10, Par11, Par12, Par13, Par14, Par15, Par16, Par17, Par18, Par19

Looks pretty simple right, just *vwrite and list what you want printed. But there is a lot more to this command.

A Lot More

First off you need to open up a file to write to. You have a couple of options.

  1. *CFOPEN,fname, ext, –, Loc
    This opens the specified file for writing with *cfwrite and *vwrite commands. This is the preferred method.
  2. /output,fname, ext, –, Loc
    By default *VWRITE output to standard output – the jobname.out (batch) file or the command window (interactive). So if you use /output you can redirect to a file instead of the *.out or screen. We don’t recommend this because other stuff might get written as well to the file.

Now you have a place to write to, next you need to use *VWRITE to write. *VWRITE is a unique command because it actually uses two lines. The first contains *VWRITE and a list of parameters and/or arrays to write and the second contains a format statement. We will cover the first line first, and the format second.

Parameter Arguments for *VWRITE

As you can see from the command, you can have up to 19 parameters listed on a *VWRITE command. PAR1 through PAR19 can be array, scalar, or character parameters. They can also be a constant. This is where the real flexibility comes in. You can do something like (just look at the *VWRITE line, we will talk about the rest further on):

 1: adiv = ' | '
 2: *dim,nds, ,10
 3: *dim,temps,,10
 4: *vfill,nds(1),ramp,1,1
 5: *vfill,temps(1),rand,70,1500
 6: *cfopen,vw1.out
 7: *VWRITE,'Temp: ',nds(1),temps(1),adiv, 'TREF: ',70
 8: (A6,F8.0,g16.8,A3,A6,F10.4)
 9: *cfclose

This mixes characters, arrays, and constants in one command. As output you get:

Temp: 1. 429.56308 | TREF: 70.0000 Temp: 2. 263.55403 | TREF: 70.0000 Temp: 3. 1482.8411 | TREF: 70.0000 Temp: 4. 605.95819 | TREF: 70.0000 Temp: 5. 782.33391 | TREF: 70.0000 Temp: 6. 1301.1332 | TREF: 70.0000 Temp: 7. 1119.4253 | TREF: 70.0000 Temp: 8. 202.87298 | TREF: 70.0000 Temp: 9. 1053.4121 | TREF: 70.0000 Temp: 10. 805.71033 | TREF: 70.0000

The first thing you will notice is no *do loop. If you supply an array parameter, *vwrite loops on the parameter from the given index (1 in this case) to the end of the array. But if you don’t want the whole array written, you can control by placing *VLEN and/or *VMASK in front of the *VWRITE command: