NAMD
Functions
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.

15  {
16 
17 int fd1, fd2;
18 struct stat statbuf;
19 int i, j, n1, n2;
20 off_t s1, s2;
21 int imax, usetol;
22 double dmax, dtol;
23 
24 if ( argc < 3 || argc > 4 ) {
25  fprintf(stderr,"Returns the maximum distance between two binary pdb files.\n");
26  fprintf(stderr,"Optionally lists all differences greater than [tolerance].\n");
27  fprintf(stderr,"Usage: %s <filename1> <filename2> [tolerance]\n",argv[0]);
28  exit(-1);
29 }
30 
31 if ( ( fd1 = open(argv[1], O_RDONLY) ) < 0 ) {
32  fprintf(stderr,"Can't open %s for reading.\n",argv[1]);
33  exit(-1);
34 }
35 
36 if ( fstat(fd1,&statbuf) < 0 ) {
37  fprintf(stderr,"Can't stat %s.\n",argv[1]);
38  exit(-1);
39 }
40 
41 s1 = statbuf.st_size;
42 
43 if ( (s1 < 4) || ((s1-4) % 24) ) {
44  fprintf(stderr,"Size %ld of %s is not 4 plus a multiple of 24.\n",s1,argv[1]);
45  exit(-1);
46 }
47 
48 read(fd1,&n1,4);
49 
50 if ( s1 != 4 + (off_t)n1 * 24 ) {
51  fprintf(stderr,"Size %ld of %s is not 4 plus %d times 24.\n",s1,argv[1],n1);
52  exit(-1);
53 }
54 
55 if ( ( fd2 = open(argv[2], O_RDONLY) ) < 0 ) {
56  fprintf(stderr,"Can't open %s for reading.\n",argv[2]);
57  exit(-1);
58 }
59 
60 if ( fstat(fd2,&statbuf) < 0 ) {
61  fprintf(stderr,"Can't stat %s.\n",argv[2]);
62  exit(-1);
63 }
64 
65 s2 = statbuf.st_size;
66 
67 if ( (s2 < 4) || ((s2-4) % 24) ) {
68  fprintf(stderr,"Size %ld of %s is not 4 plus a multiple of 24.\n",s2,argv[2]);
69  exit(-1);
70 }
71 
72 read(fd2,&n2,4);
73 
74 if ( s2 != 4 + (off_t)n2 * 24 ) {
75  fprintf(stderr,"Size %ld of %s is not 4 plus %d times 24.\n",s2,argv[2],n2);
76  exit(-1);
77 }
78 
79 if ( n1 != n2 ) {
80  fprintf(stderr,"%s atomcount %d does not match %s atomcount %d..\n",
81  argv[1],n1,argv[2],n2);
82  exit(-1);
83 }
84 
85 usetol = 0;
86 dtol = 0;
87 
88 if ( argc == 4 ) {
89  if ( sscanf(argv[3],"%lf",&dtol) != 1 ) {
90  fprintf(stderr,"Unable to parse tolerance argument %s\n",argv[3]);
91  fprintf(stderr,"Usage: %s <filename1> <filename2> [tolerance]\n",argv[0]);
92  exit(-1);
93  }
94  usetol = 1;
95  if ( dtol > 0. ) dtol *= dtol; /* preserve negative values */
96 }
97 
98 imax = 0;
99 dmax = 0;
100 
101 int bufatoms = 1000000;
102 if ( bufatoms > n1 ) bufatoms = n1;
103 
104 double *c1 = malloc(bufatoms*24);
105 double *c2 = malloc(bufatoms*24);
106 
107 if ( ! c1 || ! c2 ) {
108  fprintf(stderr,"Memory allocation failed.\n");
109 }
110 
111 for ( i=0; i<n1; i+=bufatoms ) {
112  if ( i + bufatoms > n1 ) bufatoms = n1 - i;
113  if ( read(fd1,c1,bufatoms*24) != bufatoms*24 ) {
114  fprintf(stderr,"Error reading %s.\n",argv[1]);
115  exit(-1);
116  }
117  if ( read(fd2,c2,bufatoms*24) != bufatoms*24 ) {
118  fprintf(stderr,"Error reading %s.\n",argv[2]);
119  exit(-1);
120  }
121 
122  for ( j=0; j<bufatoms; ++j ) {
123  double dx = c1[3*j+0] - c2[3*j+0];
124  double dy = c1[3*j+1] - c2[3*j+1];
125  double dz = c1[3*j+2] - c2[3*j+2];
126  double d = dx*dx + dy*dy + dz*dz;
127  if ( d > dmax ) { imax = i+j; dmax = d; }
128  if ( usetol && d > dtol ) {
129  printf("%lg at %d\n",sqrt(d),i+j);
130  }
131  }
132 }
133 
134 dmax = sqrt(dmax);
135 
136 if ( dtol ) printf("MAX: ");
137 printf("%lg at %d\n",dmax,imax);
138 
139 exit(0);
140 
141 }