Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations GregLocock on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

APDL Helpful Tip: MD5 Checksum for Input File Validation

Status
Not open for further replies.

flash3780

Mechanical
Dec 11, 2009
829
Recently, I've been using Python to generate APDL run scripts for (somewhat complicated) mission analyses in ANSYS. File management can be difficult for such analyses, and it is helpful to be able to easily verify that a particular set of input files was run for a given set of outputs.

To do this validation, I have found that it is useful to generate a checksum file using an MD5 hashing algorithm when I generate my inputs with Python. Alternatively, you can generate such a file with CYGWIN BASH or in a Linux environment using the [tt]md5sum[/tt] command:

Bash:
md5sum inputfile.inp database.db *.load *.pres *.temp *.etc>checksum.md5

What the heck is a hash function?
An MD5 hash can be thought of simply as a 32-digit hexadecimal fingerprint for a blob of data. Identical files will always produce the same hash, however a small change in the data will result in a very different hash.

For example:
Bash:
> echo "Hello world"|md5sum
f0ef7081e1539ac00ef5b761b4fb01b3 *-
> echo "Hello world."|md5sum
fa093de5fc603823f08524f9801f0546 *-

Hashing functions are often used to check the validity of files downloaded from the internet. A user can be certain that a file or program wasn't tampered with by a third party if its hash checks out. There are many hashing algorithms; the MD5 is adequate for checking file validity.

Back to ANSYS...
Generating a checksum of a bunch of files is trivial (see above); I've found that it is useful to store that checksum in my ANSYS database files at runtime. I include a few lines of code in my main run script:

Code:
...
resume, database, db
/filname, loadstep1
/inquire,chklines,lines,checksum,md5
*sread, chksum, checksum, md5,,80,,chklines
/sys, md5sum -c checksum.md5>validate_inputs.out
... ! Apply some loads
save
solve
...

The data is stored as a string array; you can pull it back out again later with a simple macro. Alas, the system call may not work on all operating systems; Windows users can download a utility to perform MD5 or SHA1 checksums instead.

Microsoft File Checksum Integrity Verifier

Retrieving your checksum later...
Of course, the reason for storing the checksum in the ANSYS database is to verify that your output matches a certain set of inputs. To do so, you'll need to be able to pull the checksum back out of the database. I'd recommend something like this:

Code:
! Retrieve checksum from database

*cfopen,checksum,md5
*vlen,chklines
*vwrite,chksum(1)
%80S
*cfclose
/sys,sed --in-place -e 's/ *//g' checksum.md5

ANSYS kicks out your checksum file with leading whitespace to make each line 80 characters. I use SED to strip off leading the whitespace. Windows users have several different options to accomplish this.

SED in Windows

You can then use a utility to run your checksum against a set of input files to easily verify that they were used to generate the output.

Bash:
md5sum -c checksum.md5

Quick shout-out
I found the following PADT post very helpful to figure out the best way to store and retrieve the checksum in ANSYS:

Happy holidays,

//signed//
Christopher K. Hubley
Mechanical Engineer
Cincinnati, Ohio
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor