• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

tools/mesh/tensor2dx.c

00001 /*
00002  *         del2dx.c
00003  *         apbs
00004  *
00005  *         Created by David Gohara on 3/17/10.
00006  *         Copyright 2010 Washington University. All rights reserved.
00007  *
00008  */
00009 
00010 #include <stdio.h>
00011 #include <stdlib.h>
00012 #include <string.h>
00013 #include <math.h>
00014 
00015 char *usage = "\n\n\
00016 -----------------------------------------------------------------------\n\
00017 tensor2dx\n\
00018 \n\
00019 For converting the a tensor (plotkin) format file to the\n\
00020 OpenDX format.\n\
00021 \n\
00022 Usage:     tensor2dx <x-gpoints> <y-gpoints> <z-gpoints> <originfile> <datafile> <output file>\n\
00023 {xyz}-gpoints are the number of grid points in the x,y,z direction\n\
00024 originfile is the file containing origin and grid spacing information\n\
00025 datafile is the file with the data to use at each grid point\n\
00026 \n\
00027 NOTE: This program only handles isotropic tensor files at the moment.\n\
00028 \n\
00029 -----------------------------------------------------------------------\n\
00030 \n";
00031 
00032 int main(int argc, char **argv) {
00033            
00034            int i,j,k,index;
00035            int nx,ny,nz;
00036            int lcount;
00037            int itmp[3];
00038            
00039            double origin_xyz[3];
00040            double gspace[3];
00041            double datapt[3];
00042            
00043            double tmp[3];
00044            
00045            char *origin = NULL;
00046            char *data = NULL;
00047            
00048            char *outpath = NULL;
00049            
00050            char buffer[1024];
00051            
00052            FILE * pfile1 = NULL;
00053            FILE * pfile2 = NULL;
00054            
00055            FILE * pfile3 = NULL;
00056            
00057            if (argc != 7) {
00058                       printf("\n*** Syntax error: got %d arguments, expected 6.\n\n",argc);
00059                       printf("%s\n", usage);
00060                       return -1;
00061            } else {
00062                       nx = atoi(argv[1]);
00063                       ny = atoi(argv[2]);
00064                       nz = atoi(argv[3]);
00065                       
00066                       origin = argv[4];
00067                       data = argv[5];
00068                       outpath = argv[6];
00069            }
00070            
00071            pfile1 = fopen(origin,"r");
00072            pfile2 = fopen(data,"r");
00073            
00074            //Get the origin and grid spacing information
00075            fscanf(pfile1,"%lf %lf %lf",&origin_xyz[0],&origin_xyz[1],&origin_xyz[2]);
00076            fscanf(pfile1,"%lf %lf %lf",&gspace[0],&gspace[1],&gspace[2]);
00077            fclose(pfile1);
00078            
00079            //Check the line count of the file
00080            lcount = 0;
00081            while(fgets(buffer,1024,pfile2) != NULL) lcount++;
00082            fseek(pfile2, 0L, SEEK_SET);
00083            
00084            //Verify the line count matches the information specified for the grid spacing
00085            if((lcount/4) != nx*ny*nz){
00086                       printf("\n*** %i data points specified, only %i data points read\n\n",nx*ny*nz,lcount);
00087                       printf("%s\n", usage);
00088                       return -1;
00089            }
00090            
00091            //Now write the OpenDX formatted file
00092            pfile3 = fopen(outpath, "w");
00093            
00094            fprintf(pfile3,
00095                          "# Data from tensor2dx (APBS)\n"         \
00096                          "# \n"                                                                    \
00097                          "# \n"                                                                    \
00098                          "object 1 class gridpositions counts %i %i %i\n" \
00099                          "origin %1.6e %1.6e %1.6e\n" \
00100                          "delta %1.6e 0.000000e+00 0.000000e+00\n"                      \
00101                          "delta 0.000000e+00 %1.6e 0.000000e+00\n"                      \
00102                          "delta 0.000000e+00 0.000000e+00 %1.6e\n"                      \
00103                          "object 2 class gridconnections counts %i %i %i\n"\
00104                          "object 3 class array type double rank 0 items %i data follows\n",
00105                                  nx,ny,nz,
00106                                  origin_xyz[0],origin_xyz[1],origin_xyz[2],
00107                                  gspace[0],gspace[1],gspace[2],
00108                                  nx,ny,nz,nx*ny*nz);
00109            
00110            //For the moment I'm assuming the data for the tensor file is row major
00111            //Write out the data
00112            int icol = 0;
00113            for (i=0; i<nx*ny*nz; i++){
00114                       fscanf(pfile2,"%i %i %i",&itmp[0],&itmp[1],&itmp[2]);
00115                       fscanf(pfile2,"%lf %lf %lf",&datapt[0],&tmp[1],&tmp[2]);
00116                       fscanf(pfile2,"%lf %lf %lf",&tmp[0],&datapt[1],&tmp[2]);
00117                       fscanf(pfile2,"%lf %lf %lf",&tmp[0],&tmp[1],&datapt[2]);
00118                       
00119                       //TODO: We write out the point for datapt[0], because,
00120                       //             we only deal with isotropic tensors at the moment.
00121                       //             we'd need to change this code to handle anisotropic
00122                       //             tensors in the future.
00123                       fprintf(pfile3, "%12.6e ",datapt[0]);
00124                       icol++;
00125                       if (icol == 3) {
00126                                  icol = 0;
00127                                  fprintf(pfile3, "\n");
00128                       }
00129            }
00130 
00131            if (icol != 0) fprintf(pfile3, "\n");
00132            fprintf(pfile3, "attribute \"dep\" string \"positions\"\n" \
00133                                  "object \"regular positions regular connections\" class field\n" \
00134                                  "component \"positions\" value 1\n" \
00135                                  "component \"connections\" value 2\n" \
00136                                  "component \"data\" value 3\n");
00137            
00138            fclose(pfile2);
00139            fclose(pfile3);
00140            
00141            return 0;
00142 }

Generated on Wed Oct 20 2010 11:12:20 for APBS by  doxygen 1.7.2