-
Notifications
You must be signed in to change notification settings - Fork 4
/
ctest.c
executable file
·152 lines (131 loc) · 4.08 KB
/
ctest.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include <limits.h>
#include <math.h>
#include <rpc/rpc.h>
#include <rpc/xdr.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include "xdrf.h"
/*________________________________________________________________________
|
| read_frame - ad hoc implementation to read ascii data
|
| given a open file handle and the number of coordinates, this routine
| reads sets of three floating point numbers and stores the result in
| coord. the coord array should be preallocated to hold 3 * num_of_coord
| coordinates. Minval and maxval will return the minumum and maximum
| values of the coordinates.
| Return value will be -1 when eof is reached, and 0 on success
*/
static int read_frame(FILE *in, int num_of_coord, float coord[]) {
int i;
double d[3];
char line[1024];
for (i = 0; i < num_of_coord; i++) {
if (fgets(line, 1024 -1, in) == NULL) {
if (i == 0) return -1;
fprintf(stderr,"incomplete coordinate frame during read %d", i);
exit (1);
}
sscanf(line," %lf %lf %lf", &d[0], &d[1], &d[2]);
*coord++ = (float) d[0];
*coord++ = (float) d[1];
*coord++ = (float) d[2];
}
return 0;
}
int main() {
XDR xd, xd2;
int i, j;
float prec = 1000.0;
float *coord, *coord2;
int num_of_coord;
char *line;
int framecnt = 0;
double maxdiff = 0;
float d0, d1, d2, d3;
FILE *fgmx, *fout;
line = (char *) malloc(1024);
/* open file containing 3d coordinates */
fgmx = fopen("test.gmx","r");
if (fgmx == NULL) {
perror("could open gmx test data");
exit(1);
}
if (fgets(line, 1024 -1, fgmx) == NULL) {
perror("cannot read gmx test data");
exit (1);
}
/* read first line which contains number of coordinates */
sscanf(line," %d %f %f %f %f", &num_of_coord, &d0, &d1, &d2, &d3);
coord = (float *)malloc(num_of_coord * 3 * sizeof(float));
coord2 = (float *)malloc(num_of_coord * 3 * sizeof(float));
/* open xdr file to which compressed coordinates will be written */
if (xdropen(&xd, "test.xdr","a") == 0) {
fprintf(stderr,"failed to open file\n");
}
/* just as test write the first line using normal xdr routine */
xdr_int(&xd, &num_of_coord);
xdr_float(&xd, &d0);
xdr_float(&xd, &d1);
xdr_float(&xd, &d2);
xdr_float(&xd, &d3);
/* read all frames from the data and write compressed coordinates */
while ( read_frame(fgmx, num_of_coord, coord) == 0 ) {
framecnt++;
if (xdr3dfcoord(&xd, coord, &num_of_coord, &prec) == 0) {
fprintf(stderr,"error while writing coordinates\n");
}
}
xdrclose(&xd);
fclose(fgmx);
/* Now do the inverse ! */
/* open file to write decompressed data */
fout = fopen("test.out", "w+");
if (fout == NULL) {
perror("could not open test.out to write data\n");
exit(1);
}
if (xdropen(&xd2, "test.xdr","r") == 0) {
fprintf(stderr,"error while opening test.xdr for read\n");
exit(1);
}
*line = '\0';
xdr_int(&xd2, &num_of_coord);
xdr_float(&xd2, &d0);
xdr_float(&xd2, &d1);
xdr_float(&xd2, &d2);
xdr_float(&xd2, &d3);
fprintf(fout, "%5d%8.3f%8.3f%8.3f%8.3f\n", num_of_coord, d0, d1, d2, d3);
for (i = 0; i < framecnt ; i++) {
if (xdr3dfcoord(&xd2, (float *)coord2, &num_of_coord, &prec) == 0) {
fprintf(stderr, "error while reading coordinates\n");
}
for (j=0; j < num_of_coord * 3; j += 3) {
fprintf(fout, "%8.3f %8.3f %8.3f\n", coord2[j],
coord2[j+1] ,coord2[j+2]);
}
}
xdrclose(&xd2);
fclose(fout);
/* And compare the result */
fgmx = fopen("test.gmx", "r");
fout = fopen("test.out", "r");
maxdiff = 0;
fgets(line, 1024 -1, fgmx);
fgets(line, 1024 -1, fout);
for (i = 0; i < framecnt ; i++) {
read_frame(fgmx, num_of_coord, coord);
read_frame(fout, num_of_coord, coord2);
for (j=0; j < num_of_coord * 3; j++) {
if (fabs(coord[j] - coord2[j]) > maxdiff)
maxdiff = fabs(coord[j] - coord2[j]) ;
}
}
fprintf(stderr,"\nmaxdiff = %f\n", maxdiff);
free(coord);
free(coord2);
free(line);
return 0;
}