SPModel: postProcessing/spm2vtk/spm2vtk.c Source File
VPAC - Computational Software Development
Main | SPModel | StGermain FrameWork |
Main Page | Alphabetical List | Class List | Directories | File List | Class Members | File Members

spm2vtk.c

Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <limits.h>
00003 #include <assert.h>
00004 
00005 
00006 void ConvertLoop( unsigned int loop, double time );
00007 
00008 char        path[PATH_MAX];
00009 FILE*       meshIn;
00010 FILE*       coordIn;
00011 
00012 unsigned int    nodeLocalCount;
00013 unsigned int    elementLocalCount;
00014 
00015 int main() {
00016     char        tmpBuf[PATH_MAX];
00017     FILE*       simIn;
00018     FILE*       loopIn;
00019     unsigned int    rank = 0;
00020     unsigned int    loop;
00021     double      time;
00022     
00023     
00024     /* TODO, get from arg 1 */
00025     sprintf( path, "./" );
00026     
00027     /* open the input files */
00028     sprintf( tmpBuf, "%s/sim.%u", path, rank );
00029     if( (simIn = fopen( tmpBuf, "r" )) == NULL ) {
00030         assert( simIn /* failed to open file for reading */ );
00031     }
00032     sprintf( tmpBuf, "%s/mesh.%u", path, rank );
00033     if( (meshIn = fopen( tmpBuf, "r" )) == NULL ) {
00034         assert( meshIn /* failed to open file for reading */ );
00035     }
00036     sprintf( tmpBuf, "%s/loop.%u", path, rank );
00037     if( (loopIn = fopen( tmpBuf, "r" )) == NULL ) {
00038         assert( loopIn /* failed to open file for reading */ );
00039     }
00040     sprintf( tmpBuf, "%s/coord.%u", path, rank );
00041     if( (coordIn = fopen( tmpBuf, "r" )) == NULL ) {
00042         assert( coordIn /* failed to open file for reading */ );
00043     }
00044     
00045     
00046     /* Read in simulation information... TODO: assumes nproc=1 */
00047     fscanf( simIn, "%u\n", &nodeLocalCount );
00048     fscanf( simIn, "%u\n", &elementLocalCount );
00049     
00050     /* Read in loop information */
00051     while( !feof( loopIn ) ) {
00052         fscanf( loopIn, "%16u %16lg\n", &loop, &time );
00053         ConvertLoop( loop, time );
00054     }
00055     
00056     /* Close the input files */
00057     fclose( coordIn );
00058     fclose( loopIn );
00059     fclose( meshIn );
00060     fclose( simIn );
00061     
00062     return 0;
00063 }
00064 
00065 
00066 void ConvertLoop( unsigned int loop, double time ) {
00067     char        tmpBuf[PATH_MAX];
00068     FILE*       vtkOut;
00069     unsigned int    node_I;
00070     unsigned int    element_I;
00071     
00072     /* open the output file */  
00073     sprintf( tmpBuf, "%s/spm.%04u.vtk", path, loop );
00074     if( (vtkOut = fopen( tmpBuf, "w+" )) == NULL ) {
00075         assert( vtkOut /* failed to open file for writing */ );
00076     }
00077     
00078     /* Write out simulation information */
00079     fprintf( vtkOut, "# vtk DataFile Version 3.0\nSPModel simulation output\nASCII\n" );
00080     
00081     /* Write out node information */
00082     fprintf( vtkOut, "\nDATASET POLYDATA\nPOINTS %u float\n", nodeLocalCount );
00083     fseek( coordIn, loop * nodeLocalCount * sizeof(float) * 3, SEEK_SET );
00084     for( node_I = 0; node_I < nodeLocalCount; node_I++ ) {
00085         float       coord[3];
00086         
00087         fread( &coord, sizeof(float), 3, coordIn );
00088         fprintf( vtkOut, "%g %g %g\n", coord[0], coord[1], coord[2] );
00089     }
00090     fprintf( vtkOut, "\n" );
00091     
00092     /* Write out element information */
00093     fprintf( vtkOut, "POLYGONS %u %u\n", elementLocalCount, (1 + 3) * elementLocalCount );
00094     fseek( meshIn, loop * elementLocalCount * sizeof(unsigned int) * 3, SEEK_SET );
00095     for( element_I = 0; element_I < elementLocalCount; element_I++ ) {
00096         unsigned int    elementNode[3];
00097         
00098         fread( &elementNode, sizeof(unsigned int), 3, meshIn );
00099         fprintf( vtkOut, "3 %u %u %u\n", elementNode[0], elementNode[1], elementNode[2] );
00100     }
00101     fprintf( vtkOut, "\n" );
00102     
00103     /* Close the input files */
00104     fclose( vtkOut );
00105 }