Writing Text Files With - VWRITE - PADT, Inc. - The Blog
Writing Text Files With - VWRITE - PADT, Inc. - The Blog
– The Blog
Search
Menu
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 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.
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will
1. *CFOPEN,fname, ext, –, Loc
assume that you are happy with it.
This opens the specified file for writing with *cfwrite and *vwrite commands. This is
Ok Privacy - Terms
the preferred method.
http://www.padtinc.com/blog/the-focus/writing-text-files-with-vwrite 1/12
3/22/2019 Writing Text files with *VWRITE – PADT, Inc. – The Blog
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.
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):
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
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:
Array Parameters
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will
assume that you are happy with it.
Ok Privacy - Terms
http://www.padtinc.com/blog/the-focus/writing-text-files-with-vwrite 2/12
3/22/2019 Writing Text files with *VWRITE – PADT, Inc. – The Blog
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:
*VLEN,nrow,ninc
This will only write out nrow times, skipping based on ninc (defaults to 1)
As an example, if you want to write just the fourth value in array A() you
would do:
*VLEN,1
*VWRITE,A(4)
(G16.8)
*VMASK,Par
You make a mask array of 0’s and 1 that is the same size as your array, and supply it
to *VMASK. *VWRITE will only write out values for your array if the mask array is
not zero for the same index.
You can have a multiple dimensions on your array. *VWRITE only increments the first
index. Say you specify X, Y, and Z coordinates in an array call xyz. It would look like:
*VWRITE,xyz(1,1),XYZ(1,3),XY
Z(1,3)
(3G16.8)
String Parameters
Being an older program, you are limited in what you can do with character parameters.
You are limited to 8 characters. So you just use a long string parameter several times and
increment the index by 8:
1: *dim,mystring,string,80
3: *cfopen,vw2.out
5: (5A)
6: *cfclose
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will
Kind of hokey, but it works.
assume that you are happy with it.
Ok Privacy - Terms
Integers
http://www.padtinc.com/blog/the-focus/writing-text-files-with-vwrite 3/12
3/22/2019 Writing Text files with *VWRITE – PADT, Inc. – The Blog
Sigh. This is the one thing that I’m not fond of in *VWRITE. The original command did not
support outputting integer values. That is because the FORTRAN I descriptor was not
supported, and ANSYS stores everything as an integer anyhow. But people needed to
output integer values so they took the ‘C’ format routines for *MSG and made them work
with *VWRITE. So you can do a %I. See the section on ‘C’ formatting below for more
information on this.
Before you can do anything with the file you create you need to close it. Not to hard:
*CFCLOSE does the trick.
Don’t put a blank in there. If you do, *vwrite stops looking at parameters. So if you need
a blank in your file, put in a space ‘ ‘ or use the X FORTRAN descriptor.
Be aware of *CFWRITE as well. It is a way to write APDL commands to a file. If what you
want to do is have your macro write a macro, *CFWRITE is better because you don’t have
to do format statements. And those can be a pain when you need commas.
If your arrays are of different lengths, *VWRITE will loop for the length of the longest
array. Any shorter arrays will be replaced with zeros for number arrays and blanks for
character/string arrays.
You can not use *VWRITE by pasting or typing into the command line. You have to read it
from a file.
Formatting
The key, and the difficult part of using *VWRITE is the format statement. We recommend
that you use the FORTRAN formatting when you are writing out large amounts of
columnar data, and use the ‘C’ format if you are writing out text rich information for a
report or to inform the user of something.
Many users today may not even know what a FORTRAN statement looks like. A good
place to look is:
http://www.stanford.edu/class/me200c/tutorial_77/17_format.html
Just remember that you can’t use the Integer (I) format. The list directed format (*) also
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will
does not work. If you are new to it also remember everything goes in parenthesis and it
assume that you are happy with it.
has to fit on one line. It does not have to start in column 8 (if you think that is funny, you
Ok Privacy - Terms
are old)
http://www.padtinc.com/blog/the-focus/writing-text-files-with-vwrite 4/12
3/22/2019 Writing Text files with *VWRITE – PADT, Inc. – The Blog
As to ‘C’ formatting, you have a lot more options but we have found that the behavior is
not as consistent between Linux and windows as the FORTRAN. But if you are more
comfortable with ‘C’, do that. Not all of ‘C’ formatting works in APDL, but what does is
actually documented under the *MSG command.
Making it Work
We always recommend you work out your *VWRITE issues in a small macro that you can
run over and over again as you work out the formatting. If you are writing a snippet for
ANSYS Mechanical. Go ahead and save your model, bring it up in MAPDL, then work on
your *VWRITE statement till you get it right.
Keep things simple. Don’t try and format a novel or an HTML page with *VWRITE.
You probably could, but there are better tools for that.
Make sure you understand arrays, how they work in APDL, and how you have yours
dimensioned.
Get familiar with *VMASK and *VLEN. They are useful
Share this:
Like this:
Like
Be the first to like this.
CC
JULY 23, 2013 AT 3:36 AM
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will
Hi Eric. assume that you are happy with it.
There is an easier way to write large strings that makes use of the the C format
Ok Privacy - Terms
http://www.padtinc.com/blog/the-focus/writing-text-files-with-vwrite 5/12
3/22/2019 Writing Text files with *VWRITE – PADT, Inc. – The Blog
descriptor:
*dim,mystring,string,80
mystring(1) = ‘This is a string of up to 80 characters’
*CFOPEN,’file’,txt
*VWRITE,mystring(1)
%C
*CFCLOSE
Kind regards
Christian
Log in to Reply
Google search
LINKS
PADT Website
Contact PADT
PADTMarket.com
Manage PADT Subscriptions
SUBSCRIBE
E-Mail
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will
assume that you are happy with it.
Subscribe Ok Privacy - Terms
http://www.padtinc.com/blog/the-focus/writing-text-files-with-vwrite 6/12
3/22/2019 Writing Text files with *VWRITE – PADT, Inc. – The Blog
Learn APDL with this workshop based book written by PADT’s Technical Support Team.
The new and improved Second Edition contains additional chapters on APDL Math and
APDL in ANSYS Mechanical.
More…
SIMULATION SERVICES
PADT’s simulation engineers are true experts in virtual prototyping. Trust the people you come to for
ANSYS expertise to handle your simulation outsourcing needs.
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will
ANSYS STARTUP PROGRAM assume that you are happy with it.
Ok Privacy - Terms
http://www.padtinc.com/blog/the-focus/writing-text-files-with-vwrite 7/12
3/22/2019 Writing Text files with *VWRITE – PADT, Inc. – The Blog
NIMBIX
Let the people who write “The Focus” train you and your team. Check out our
schedule or contact us to set up a class at your place or something custom.
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will
ANSYS ACADEMIC PROGRAM assume that you are happy with it.
Ok Privacy - Terms
http://www.padtinc.com/blog/the-focus/writing-text-files-with-vwrite 8/12
3/22/2019 Writing Text files with *VWRITE – PADT, Inc. – The Blog
RSS SUBSCRIBE:
RECENT POSTS:
Using Command Snippets in Solution (And a cool new ACT Extension to make life easier)
All Things ANSYS 032 – What’s New in ANSYS Mechanical: Updates Made in 2019 R1
PADT Spins-Off Successful 3D Printing Support Removal Equipment Line Into a Separate
Company, Oryx Additive
CATEGORIES
Additive Manufacturing
ANSYS Discovery
ANSYS R 18
Carbon
Education
Events
Fun
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will
Getting To Know PADT assume that you are happy with it.
Ok Privacy - Terms
http://www.padtinc.com/blog/the-focus/writing-text-files-with-vwrite 9/12
3/22/2019 Writing Text files with *VWRITE – PADT, Inc. – The Blog
News
Nimbix
PADT Medical
Podcast
Product Development
Publications
Startups
Stratasys Marketing
The Focus
Uncategorized
Webinar
UPCOMING EVENTS
03/21/2019
PADT's 25th Anniversary Celebration
03/27/2019
Webinar - Using ANSYS to Model LiDAR Components
03/28/2019
2018 Medtech Conference and Expo
03/31/2019 - 04/04/2019
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will
AMUG 2019
assume that you are happy with it.
Ok Privacy - Terms
http://www.padtinc.com/blog/the-focus/writing-text-files-with-vwrite 10/12
3/22/2019 Writing Text files with *VWRITE – PADT, Inc. – The Blog
04/08/2019 - 04/11/2019
35th Space Symposium
04/10/2019
Webinar - Simulate Multibody Dynamics More Accurately with ANSYS Motion
04/29/2019 - 05/02/2019
AeroDef Manufacturing 2019
05/08/2019
Webinar - 3D Design Updates in ANSYS 2019 R1
05/20/2019 - 05/23/2019
RAPID + TCT 2019
11/30/2019
Webinar - Secondary air system modelling with Flownex
Where do you start? How do you keep it going? Where do I get ideas for posts? Should I
use humor?
These questions and many others are answered in the book that was inspired by the
success of PADT's blog:
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will
assume that you are happy with it.
PADT EMAIL
Ok Privacy - Terms
http://www.padtinc.com/blog/the-focus/writing-text-files-with-vwrite 11/12
3/22/2019 Writing Text files with *VWRITE – PADT, Inc. – The Blog
State/Province
Company
∗ Email Lists
PADT Additive & Advanced Manufacturing Email List
PADT General Information Email List
PADT Product Development and Testing Email List
PADT Simulation Email List
By submitting this form, you are consenting to receive marketing emails from: Phoenix Analysis and Design Technologies, 7755 S.
Research Dr., Suite 110, Tempe, AZ, 85284, US, http://www.padtinc.com. You can revoke your consent to receive emails at any
time by using the SafeUnsubscribe® link, found at the bottom of every email. Emails are serviced by Constant Contact.
Sign Up!
© 2019 PADT, Inc. – The Blog • Privacy Policy for the PADT Blog • Powered by WordPress • Theme: Gillian
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will
assume that you are happy with it.
Ok Privacy - Terms
http://www.padtinc.com/blog/the-focus/writing-text-files-with-vwrite 12/12