diffbinpdb.c File Reference

#include "largefiles.h"
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

Go to the source code of this file.

Functions

int main (int argc, char *argv[])

Function Documentation

int main ( int  argc,
char *  argv[] 
)

Definition at line 15 of file diffbinpdb.c.

References j.

00015                                  {
00016 
00017 int fd1, fd2;
00018 struct stat statbuf;
00019 int i, j, n1, n2;
00020 off_t s1, s2;
00021 int imax, usetol;
00022 double dmax, dtol;
00023 
00024 if ( argc < 3 || argc > 4 ) {
00025   fprintf(stderr,"Returns the maximum distance between two binary pdb files.\n");
00026   fprintf(stderr,"Optionally lists all differences greater than [tolerance].\n");
00027   fprintf(stderr,"Usage: %s <filename1> <filename2> [tolerance]\n",argv[0]);
00028   exit(-1);
00029 }
00030 
00031 if ( ( fd1 = open(argv[1], O_RDONLY) ) < 0 ) {
00032   fprintf(stderr,"Can't open %s for reading.\n",argv[1]);
00033   exit(-1);
00034 }
00035 
00036 if ( fstat(fd1,&statbuf) < 0 ) {
00037   fprintf(stderr,"Can't stat %s.\n",argv[1]);
00038   exit(-1);
00039 }
00040 
00041 s1 = statbuf.st_size;
00042 
00043 if ( (s1 < 4) || ((s1-4) % 24) ) {
00044   fprintf(stderr,"Size %ld of %s is not 4 plus a multiple of 24.\n",s1,argv[1]);
00045   exit(-1);
00046 }
00047 
00048 read(fd1,&n1,4);
00049 
00050 if ( s1 != 4 + (off_t)n1 * 24 ) {
00051   fprintf(stderr,"Size %ld of %s is not 4 plus %d times 24.\n",s1,argv[1],n1);
00052   exit(-1);
00053 }
00054 
00055 if ( ( fd2 = open(argv[2], O_RDONLY) ) < 0 ) {
00056   fprintf(stderr,"Can't open %s for reading.\n",argv[2]);
00057   exit(-1);
00058 }
00059 
00060 if ( fstat(fd2,&statbuf) < 0 ) {
00061   fprintf(stderr,"Can't stat %s.\n",argv[2]);
00062   exit(-1);
00063 }
00064 
00065 s2 = statbuf.st_size;
00066 
00067 if ( (s2 < 4) || ((s2-4) % 24) ) {
00068   fprintf(stderr,"Size %ld of %s is not 4 plus a multiple of 24.\n",s2,argv[2]);
00069   exit(-1);
00070 }
00071 
00072 read(fd2,&n2,4);
00073 
00074 if ( s2 != 4 + (off_t)n2 * 24 ) {
00075   fprintf(stderr,"Size %ld of %s is not 4 plus %d times 24.\n",s2,argv[2],n2);
00076   exit(-1);
00077 }
00078 
00079 if ( n1 != n2 ) {
00080   fprintf(stderr,"%s atomcount %d does not match %s atomcount %d..\n",
00081           argv[1],n1,argv[2],n2);
00082   exit(-1);
00083 }
00084 
00085 usetol = 0;
00086 dtol = 0;
00087 
00088 if ( argc == 4 ) {
00089   if ( sscanf(argv[3],"%lf",&dtol) != 1 ) {
00090     fprintf(stderr,"Unable to parse tolerance argument %s\n",argv[3]);
00091     fprintf(stderr,"Usage: %s <filename1> <filename2> [tolerance]\n",argv[0]);
00092     exit(-1);
00093   }
00094   usetol = 1;
00095   if ( dtol > 0. ) dtol *= dtol;  /* preserve negative values */
00096 }
00097 
00098 imax = 0;
00099 dmax = 0;
00100 
00101 int bufatoms = 1000000;
00102 if ( bufatoms > n1 ) bufatoms = n1;
00103 
00104 double *c1 = malloc(bufatoms*24);
00105 double *c2 = malloc(bufatoms*24);
00106 
00107 if ( ! c1 || ! c2 ) {
00108   fprintf(stderr,"Memory allocation failed.\n");
00109 }
00110 
00111 for ( i=0; i<n1; i+=bufatoms ) {
00112   if ( i + bufatoms > n1 ) bufatoms = n1 - i;
00113   if ( read(fd1,c1,bufatoms*24) != bufatoms*24 ) {
00114     fprintf(stderr,"Error reading %s.\n",argv[1]);
00115     exit(-1);
00116   }
00117   if ( read(fd2,c2,bufatoms*24) != bufatoms*24 ) {
00118     fprintf(stderr,"Error reading %s.\n",argv[2]);
00119     exit(-1);
00120   }
00121 
00122   for ( j=0; j<bufatoms; ++j ) {
00123     double dx = c1[3*j+0] - c2[3*j+0];
00124     double dy = c1[3*j+1] - c2[3*j+1];
00125     double dz = c1[3*j+2] - c2[3*j+2];
00126     double d = dx*dx + dy*dy + dz*dz;
00127     if ( d > dmax ) { imax = i+j; dmax = d; }
00128     if ( usetol && d > dtol ) {
00129       printf("%lg at %d\n",sqrt(d),i+j);
00130     }
00131   }
00132 }
00133 
00134 dmax = sqrt(dmax);
00135 
00136 if ( dtol ) printf("MAX: ");
00137 printf("%lg at %d\n",dmax,imax);
00138 
00139 exit(0);
00140 
00141 }


Generated on 8 Dec 2019 for NAMD by  doxygen 1.6.1