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

tools/mesh/multivalue.c

00001 /*
00002 * Name:    multivalue.c
00003 * Author(s):          Adrian Kaats
00004 *                     Effectively, Nathan Baker as well (the majority of this program is directly copied from
00005 *                     value.c distributed in apbs/tools/mesh with the source version of apbs-0.4.0 - please see
00006 *                     this function for reference and more importantly for any licensing issues).
00007 * Date:    November 29, 2006
00008 *
00009 * Description:        Accepts a CSV (with no column titles) file containing 3D coordinates for which the
00010 *                                value from a DX-formatted grid is evaluated.  The original coordinates and their its
00011 *                                values are output to a text file formatted exactly like the input but with one extra
00012 *                                column containing the calculated value at the point.  This program will also either
00013 *                                append an existing io.mc, or create it if it doesn't exist in the directory from which
00014 *                                it is called.
00015 *
00016 * Usage:              > multivalue csvCoordinatesFile dxFormattedFile outputFile
00017 *
00018 *                                Example of input file contents:  123.234,23.8E03,9.6e-4
00019 *                                                                                       5.9,6.2,0.3
00020 *                                                                                       -7e3,91,0.6
00021 *
00022 *                                           NOTE       -No white space allowed anywhere on a line (ex. no space after commas)
00023 *                                                      -No blank lines allowed at the top of the file (blank lines OK at the end)
00024 *                                                      -No column headers/titles
00025 *
00026 * Programming notes:
00027 *          1)         This program uses the convention that exiting with a 0 represents success (even in the case
00028 *                     where the program can't do anything: example --> user input coordinate file contains no data).
00029 *                     The program exits with a 1 upon failure (ex. any kind of I/O error, or error code generated by
00030 *                     external routines such as Vgrid routines).
00031 *          2)         Vgrid routines use convention that returning 1 is success.  Also note that Vgrid routines,
00032 *                     I believe, use VASSERT which can terminate the entire program with an abort() signal which is
00033 *                     not handled here.
00034 *          3)         The original 'value.c' routine written by Nathan Baker uses the convention that returning 0
00035 *                     represents success - that convention is carried on here...
00036 *          4)         To compile this program, you will need to have apbscfg.h and vclist.h in an appropriate include
00037 *                     directory - these do not seem to be created by RPM installations of APBS or its tools.
00038 *
00039 *          MANY THANKS to David Gohara without whom I (Adrian) would have spent untold ages trying to compile this
00040 */
00041 
00042 #include "apbscfg.h"
00043 #include "apbs/apbs.h"
00044 
00045 void multivalue_usage(void){
00046            Vnm_print(1,"\n");
00047            Vnm_print(1,"Usage: > multivalue csvCoordinatesFile dxFormattedFile outputFile\n\n");
00048            Vnm_print(1,"Example input file:\n\n\t");
00049            Vnm_print(1,"123.234,23.8E03,9.6e-4\n\t");
00050            Vnm_print(1,"5.9,6.2,0.3\n\t");
00051            Vnm_print(1,"-7e3,91,0.6\n\n\t");
00052            Vnm_print(1,"NOTE\t-No white space allowed anywhere on\n\t\t ");
00053            Vnm_print(1,"a line (ex. no space after commas)\n\t\t");
00054            Vnm_print(1,"-No blank lines allowed at the top of\n\t\t ");
00055            Vnm_print(1,"the file (blank lines OK at the end)\n\t\t");
00056            Vnm_print(1,"-No column headers/titles\n");
00057 }
00058 
00059 int main(int argc, char *argv[]){
00060            char *inputFileName, *dxFileName, *outputFileName;
00061            int scanNum = 0;
00062            double pt[3], val;
00063            FILE *inputFileStream, *outputFileStream;
00064            Vgrid *grid;
00065 
00066            /* *************** CHECK INVOCATION ******************* */
00067            Vio_start();
00068            Vnm_redirect(1);
00069            Vnm_print(1, "\n");
00070            if(argc != 4){
00071                       Vnm_print(2,"Invalid number of arguments, # of arguments received: %d\n",argc);
00072                       multivalue_usage();
00073                       exit(0);
00074            }
00075            
00076            inputFileName = argv[1];
00077            dxFileName = argv[2];
00078            outputFileName = argv[3];
00079 
00080            Vnm_print(1,"Input file:\t%s\ndx file:\t%s\nOutput file:\t%s\n", inputFileName, dxFileName, outputFileName);
00081            
00082            /* *************** READ DATA ******************* */
00083            Vnm_print(1,"Reading data from %s...\n",dxFileName);
00084            grid = Vgrid_ctor(0, 0, 0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, VNULL);
00085            if (!Vgrid_readDX(grid, "FILE", "ASC", VNULL,dxFileName)) {
00086                       Vnm_print(2, "Error reading dx file: %s\n",dxFileName);
00087                       exit(1);
00088            }
00089            
00090            /*
00091            try opening input file - upon error, exit with error flag
00092            */
00093            if((inputFileStream = fopen(inputFileName,"r")) == NULL){
00094                       Vnm_print(2,"Error opening input file: %s\n",inputFileName);
00095                       exit(1);
00096            }
00097            
00098            /*
00099            try opening output file - upon error, try closing input file and exit with error flag
00100            */
00101            if((outputFileStream = fopen(outputFileName,"w")) == NULL){
00102                       Vnm_print(2,"Error opening output file: %s\n",outputFileName);
00103                       /*
00104                       since an error occured creating an output file, at least try closing input
00105                       file before exiting
00106                       */
00107                       if(fclose(inputFileStream) == EOF){
00108                                  Vnm_print(2,"Error closing input file: %s",inputFileName);
00109                       }
00110                       exit(1);
00111            }
00112            
00113            /*
00114            read lines of input file placing comma separated values into x, y and z
00115            NOTE does not check for valid data, stops on EOF returned by scanf which
00116            can happen if input file lines don't match format string or upon an actual EOF
00117            */
00118            scanNum = fscanf(inputFileStream,"%lg%*c%lg%*c%lg",&pt[0],&pt[1],&pt[2]);
00119            /*
00120            Exit without error if first scan of input file results in EOF
00121            */
00122            if(scanNum == EOF){
00123                       Vnm_print(1,"Invalid data in the input file: %s\n",inputFileName);
00124                       multivalue_usage();
00125                       exit(0);
00126            }
00127            else{
00128                       Vnm_print(1,"Getting values...\n");
00129                       Vnm_print(1,"Displayed and written to output file as x,y,z,value\n");
00130            }
00131            while(scanNum != EOF){
00132                       /*
00133                       perform Vgrid_value --> 1) check if point is within mesh bounds, if not set value to 0 and return;
00134                       2) if point is actually on a mesh point, give mesh pt value; 3) otherwise use trilinear interpolation
00135                       to get value
00136                       */
00137                       if(Vgrid_value(grid, pt, &val)){
00138                                  Vnm_print(1,"%e,%e,%e,%e\n",pt[0],pt[1],pt[2],val);
00139                                  /*
00140                                  write line of output file (maybe should implement error checking on fprintf,
00141                                  but that's seems like overkill)
00142                                  */
00143                                  fprintf(outputFileStream,"%e,%e,%e,%e\n",pt[0],pt[1],pt[2],val);
00144                       }
00145                       else{
00146                                  Vnm_print(1,"%e,%e,%e,%s\n",pt[0],pt[1],pt[2],"NaN");
00147                                  /*
00148                                  write line of output file with string 'NaN' for value - this is done to avoid
00149                                  the case where certain machines may not implement NaN or do so in a weird way?
00150                                  */
00151                                  fprintf(outputFileStream,"%e,%e,%e,%s\n",pt[0],pt[1],pt[2],"NaN");
00152                       }
00153                       
00154                       /*
00155                       scan in next line of input file
00156                       */
00157                       scanNum = fscanf(inputFileStream,"%lg%*c%lg%*c%lg",&pt[0],&pt[1],&pt[2]);
00158            }
00159            
00160            /*
00161            close input file
00162            */
00163            if(fclose(inputFileStream) == EOF){
00164                       Vnm_print(2,"Error closing input file: %s\n",inputFileName);
00165                       exit(1);
00166            }
00167            
00168            /*
00169            close output file
00170            */
00171            if(fclose(outputFileStream) == EOF){
00172                       Vnm_print(2,"Error closing output file: %s\n",outputFileName);
00173                       exit(1);
00174            }
00175            
00176            exit(0);
00177 }
00178 

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