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

tools/mesh/del2dx.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 del2dx\n\
00018 \n\
00019 For converting the DelPhi format of the electrostatic potential to the\n\
00020 OpenDX format.\n\
00021 \n\
00022 Usage:  del2dx delphi_file opendx_file\n\
00023 where delphi_file is a file in DelPhi format and opendx_file is the\n\
00024 file to be written in OpenDX format.\n\
00025 -----------------------------------------------------------------------\n\
00026 \n";
00027 
00028 int main(int argc, char **argv) {
00029            
00030            int i,j,k,index;
00031            char *inpath = NULL;
00032     char *outpath = NULL;
00033            
00034            char buffer[1024];
00035            
00036            int igrid, tot_grid;
00037            float val, scale, oldmid[3];
00038            
00039            FILE * pfile = NULL;
00040            
00041     if (argc != 3) {
00042         printf("\n*** Syntax error: got %d arguments, expected 3.\n\n",argc);
00043         printf("%s\n", usage);
00044         return -1;
00045     } else {
00046         inpath = argv[1];
00047         outpath = argv[2];
00048     }
00049            
00050            pfile = fopen(inpath, "r+b");
00051            
00052            //Get the grid length (all dimensions are the same)
00053            fseek(pfile, sizeof(int) * -1, SEEK_END);
00054            fread(&igrid, 1, sizeof(int), pfile);
00055            tot_grid = igrid*igrid*igrid;
00056            
00057            float * data = (float *)calloc(tot_grid, sizeof(float));
00058            
00059            rewind(pfile);
00060            
00061            //Skip the first 20 characters, they simply say
00062            // "now starting phimap "
00063            fseek(pfile, sizeof(char) * 20, SEEK_CUR);
00064            
00065            //get the map file time: potential or concentrat
00066            memset(buffer, 0, 1024);
00067            fread(buffer, 1, sizeof(char) * 10, pfile);
00068            printf("DelPhi %s map grid dimensions are: %i %i %i\n",
00069                          buffer,igrid,igrid,igrid);
00070            
00071            //Now skip ahead 60 characters to the data set.
00072            fseek(pfile, sizeof(char) * 60, SEEK_CUR);
00073            
00074            //Read in the actual data (float)
00075            fread(data, tot_grid, sizeof(float), pfile);
00076            
00077            //Skip over the text " end of phimap "
00078            fseek(pfile, sizeof(char) * 16, SEEK_CUR);
00079            
00080            //Get the scale
00081            fread(&scale, 1, sizeof(float), pfile);
00082            
00083            //Get the old midpoints?
00084            fread(oldmid, 3, sizeof(float), pfile);
00085            
00086            printf("The DelPhi %s map scale is: %f\n" \
00087                          "Old midpoint values: %f %f %f\n",
00088                          buffer,scale,oldmid[0],oldmid[1],oldmid[2]);
00089            
00090            fclose(pfile);
00091            
00092            //Now calculate some numbers needed for writing the DX header
00093            float temp = 0.;
00094            float xmax = 0.;
00095            for(i=0;i<3;i++){
00096                       temp = fabsf(oldmid[i]);
00097                       xmax = (xmax < temp) ? temp : xmax;
00098            }
00099            
00100            float range = ((float)igrid - 1.) / (2. * scale);
00101            float extent = range + xmax;
00102            float origin[3], delta[3];
00103            
00104            for(i=0;i<3;i++) origin[i] = oldmid[i] - range;
00105            for(i=0;i<3;i++) delta[i] = (range * 2.) / (float)igrid;
00106            
00107            //Now write the OpenDX formatted file
00108            pfile = fopen(outpath, "w");
00109            
00110            fprintf(pfile,
00111                          "# Data from del2dx (APBS 1.3)\n"        \
00112                          "# \n"                                                                    \
00113                          "# POTENTIAL (kT/e)\n"                              \
00114                          "# \n"                                                                    \
00115                          "object 1 class gridpositions counts %i %i %i\n"    \
00116                          "origin %1.6e %1.6e %1.6e\n"  \
00117                          "delta %1.6e 0.000000e+00 0.000000e+00\n"                      \
00118                          "delta 0.000000e+00 %1.6e 0.000000e+00\n"                      \
00119                          "delta 0.000000e+00 0.000000e+00 %1.6e\n"                      \
00120                          "object 2 class gridconnections counts %i %i %i\n"\
00121                          "object 3 class array type double rank 0 items %i data follows\n",
00122                                  igrid,igrid,igrid,
00123                                  origin[0],origin[1],origin[2],
00124                                  delta[0],delta[1],delta[2],
00125                                  igrid,igrid,igrid,tot_grid);
00126            
00127            //For the moment I'm assuming the data for DelPhi is row major
00128            //Write out the data
00129            int icol = 0;
00130            for (i=0; i<igrid; i++) {
00131                       for (j=0; j<igrid; j++) { 
00132                                  for (k=0; k<igrid; k++) {
00133                                             index = k*(igrid)*(igrid)+j*(igrid)+i;
00134                                             fprintf(pfile, "%12.6e ", data[index]);
00135                                             icol++;
00136                                             if (icol == 3) {
00137                                                        icol = 0;
00138                                                        fprintf(pfile, "\n");
00139                                             }
00140                                  }
00141                       }
00142            }
00143            if (icol != 0) fprintf(pfile, "\n");
00144            fprintf(pfile, "attribute \"dep\" string \"positions\"\n" \
00145                                  "object \"regular positions regular connections\" class field\n" \
00146                                  "component \"positions\" value 1\n" \
00147                                  "component \"connections\" value 2\n" \
00148                                  "component \"data\" value 3\n");
00149            
00150            free(data);
00151            
00152            return 0;
00153 }

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