ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/tcProps/readWrite.c
Revision: 1056
Committed: Tue Feb 17 14:30:48 2004 UTC (20 years, 6 months ago) by mmeineke
Content type: text/plain
File size: 11257 byte(s)
Log Message:
lmost finished with the readWrite routines

File Contents

# User Rev Content
1 mmeineke 1052 #define _FILE_OFFSET_BITS 64
2    
3     #include <stdio.h>
4     #include <stdlib.h>
5     #include <sys/types.h>
6     #include <sys/stat.h>
7     #include <string.h>
8    
9     #include "params.h"
10     #include "tcProps.h"
11     #include "readWrite.h"
12    
13    
14 mmeineke 1053 #define BUFFER_SIZE 2000
15 mmeineke 1052 int isScanned;
16 mmeineke 1056 int fileOpen;
17     int nFrames;
18     double *frameTimes;
19 mmeineke 1052 FILE* inFile;
20    
21    
22     struct linkedPos{
23    
24     fpos_t *myPos;
25     double timeStamp;
26     double Hmat[3][3];
27     struct linkedPos* next;
28     };
29    
30     struct staticPos{
31    
32     fpos_t *myPos;
33     double Hmat[3][3];
34     };
35     struct staticPos* posArray;
36    
37 mmeineke 1056 void parseDumpLine( char readBuffer[BUFFER_SIZE], int index,
38     struct atomCoord* atoms );
39 mmeineke 1052
40 mmeineke 1056 void openFile( char* inName ){
41    
42     inFile = fopen(inName);
43     if(inFile ==NULL){
44     fprintf(stderr,
45     "Error opening file \"%s\"\n",
46     inName);
47     exit(0);
48     }
49    
50     fileOpen = 1;
51     }
52    
53 mmeineke 1052 void closeFile( void ){
54    
55     fclose( inFile );
56 mmeineke 1056
57     fileOpen = 0;
58 mmeineke 1052 }
59    
60 mmeineke 1056 int setFrames( void ){
61 mmeineke 1052
62     int i,j,k;
63     struct linkedPos* headPos;
64     struct linkedPos* currPos;
65     fpos_t *currPT;
66 mmeineke 1053 char readBuffer[BUFFER_SIZE];
67 mmeineke 1055 char trash[BUFFER_SIZE];
68 mmeineke 1053 char* foo;
69     int lineNum = 0;
70    
71    
72 mmeineke 1056 if( !fileOpen ){
73 mmeineke 1052 fprintf(stderr,
74 mmeineke 1056 "Attempt to scan the file without first opening the file.\n" );
75 mmeineke 1052 exit(0);
76     }
77    
78 mmeineke 1056
79     nFrames = 0;
80 mmeineke 1053 headPos = (struct linkedPos*)malloc(sizeof(struct linkedPos));
81 mmeineke 1055 currPos = headPos;
82 mmeineke 1052 while( !feof( inFile ) ){
83    
84 mmeineke 1053 currPT = (fpos_t *)malloc(sizeof(fpos_t));
85     fgetpos(inFile, currPT);
86    
87     fgets( readBuffer, sizeof( readBuffer ), inFile );
88     lineNum++;
89     if( feof( inFile ) ){
90     fprintf( stderr,
91     "File \"%s\" ended unexpectedly at line %d\n",
92     inName,
93     lineNum );
94     exit(0);
95     }
96    
97 mmeineke 1055 currPos->next = (struct linkedPos*)malloc(sizeof(struct linkedPos));
98     currPos = currPos->next;
99     currPos->myPos = currPT;
100     nFrames++;
101    
102 mmeineke 1053 i = atoi(readBuffer);
103 mmeineke 1055
104     fgets( readBuffer, sizeof( readBuffer ), inFile );
105     lineNum++;
106     if( feof( inFile ) ){
107     fprintf( stderr,
108     "File \"%s\" ended unexpectedly at line %d\n",
109     inName,
110     lineNum );
111     exit(0);
112     }
113 mmeineke 1053
114    
115 mmeineke 1055 // parse the comment line
116    
117     foo = strtok(readBuffer, " ,;\t");
118    
119     if(foo == NULL){
120     fprintf(stderr,
121     "error in reading time from %s at line %d\n",
122     inName, lineNum );
123     exit(0);
124     }
125     currPos->timeStamp = atof( foo );
126    
127     // get the Hx vector
128    
129     foo = strtok(NULL, " ,;\t");
130     if(foo == NULL){
131     fprintf(stderr,
132     "error in reading Hx[0] from %s at line %d\n",
133     inName, lineNum );
134     exit(0);
135     }
136     currPos->Hmat[0][0] = atof( foo );
137    
138     foo = strtok(NULL, " ,;\t");
139     if(foo == NULL){
140     fprintf(stderr,
141     "error in reading Hx[1] from %s at line %d\n",
142     inName, lineNum );
143     exit(0);
144     }
145     currPos->Hmat[1][0] = atof( foo );
146    
147     foo = strtok(NULL, " ,;\t");
148     if(foo == NULL){
149     fprintf(stderr,
150     "error in reading Hx[2] from %s at line %d\n",
151     inName, lineNum );
152     exit(0);
153     }
154     currPos->Hmat[2][0] = atof( foo );
155    
156     // get the Hy vector
157    
158     foo = strtok(NULL, " ,;\t");
159     if(foo == NULL){
160     fprintf(stderr,
161     "error in reading Hy[0] from %s at line %d\n",
162     inName, lineNum );
163     exit(0);
164     }
165     currPos->Hmat[0][1] = atof( foo );
166    
167     foo = strtok(NULL, " ,;\t");
168     if(foo == NULL){
169     fprintf(stderr,
170     "error in reading Hy[1] from %s at line %d\n",
171     inName, lineNum );
172     exit(0);
173     }
174     currPos->Hmat[1][1] = atof( foo );
175    
176     foo = strtok(NULL, " ,;\t");
177     if(foo == NULL){
178     fprintf(stderr,
179     "error in reading Hy[2] from %s at line %d\n",
180     inName, lineNum );
181     exit(0);
182     }
183     currPos->Hmat[2][1] = atof( foo );
184    
185     // get the Hz vector
186    
187     foo = strtok(NULL, " ,;\t");
188     if(foo == NULL){
189     fprintf(stderr,
190     "error in reading Hz[0] from %s at line %d\n",
191     inName, lineNum );
192     exit(0);
193     }
194     currPos->Hmat[0][2] = atof( foo );
195    
196     foo = strtok(NULL, " ,;\t");
197     if(foo == NULL){
198     fprintf(stderr,
199     "error in reading Hz[1] from %s at line %d\n",
200     inName, lineNum );
201     exit(0);
202     }
203     currPos->Hmat[1][2] = atof( foo );
204    
205     foo = strtok(NULL, " ,;\t");
206     if(foo == NULL){
207     fprintf(stderr,
208     "error in reading Hz[2] from %s at line %d\n",
209     inName, lineNum );
210     exit(0);
211     }
212     currPos->Hmat[2][2] = atof( foo );
213    
214     // skip the atoms
215    
216     for(j=0; j<i; j++){
217    
218     fgets( readBuffer, sizeof( readBuffer ), inFile );
219     lineNum++;
220     if( feof( inFile ) ){
221     fprintf( stderr,
222     "File \"%s\" ended unexpectedly at line %d,"
223     " with atom %d\n",
224     inName,
225     lineNum,
226     j );
227     exit(0);
228     }
229     }
230 mmeineke 1053 }
231 mmeineke 1056
232     // allocate the static position array
233 mmeineke 1052
234 mmeineke 1056 posArray = (struct staticPos*)calloc(nFrames, sizeof(struct staticPos));
235     frameTimes = (double *)calloc(nFrames, sizeof(double) );
236    
237     i=0;
238     currPos = headPos->next;
239     while( currPos != NULL ){
240    
241     if(i>=nFrames){
242    
243     fprintf( stderr,
244     "The number of frame pointers exceeded the number of frames.\n"
245     " OOPS!\n" );
246     exit(0);
247     }
248    
249     frameTimes[i] = currPos->timeStamp;
250     posArray[i].myPos = currPos->myPos;
251     for(j=0;j<3;j++){
252     for(k=0;k<3;k++){
253     posArray[i].Hmat[j][k] = currPos->Hmat[j][k];
254     }
255     }
256    
257     i++;
258     currPos = currPos->next;
259     }
260    
261     // clean up the linked list
262    
263     currPos = headPos;
264     while( currPos != NULL ){
265     headPos = currPos;
266     currPos = headPos->next;
267     free(headPos);
268     }
269    
270    
271 mmeineke 1052 isScanned = 1;
272     }
273 mmeineke 1056
274    
275     void readFrame(int theFrame, struct atomCoord* atoms, double Hmat[3][3] ){
276    
277     // list of 'a priori' constants
278    
279     const int nLipAtoms = NL_ATOMS;
280     const int nBonds = NBONDS;
281     const int nLipids = NLIPIDS;
282     const int nSSD = NSSD;
283     const int nAtoms = nLipAtoms * nLipids + nSSD;
284    
285     fpos_t *framePos;
286     char readBuffer[BUFFER_SIZE];
287    
288     int i, j, k;
289    
290    
291     // quick checks
292    
293     if (theFrame >= nFrames){
294    
295     fprintf(stderr,
296     "frame %d, is out of range\n",
297     theFrame );
298     exit(0);
299     }
300    
301     if( !fileOpen ){
302    
303     fprintf( stderr,
304     "File is closed, cannot read frame %d.\n"
305     theFrame );
306     exit(0);
307     }
308    
309     // access the frame
310    
311     framePos = posArray[theFrame].myPos;
312     fsetPos( inFile, framePos );
313    
314     for(j=0;j<3;j++){
315     for(k=0;k<3;k++){
316     Hmat[j][k] = posArray[theFrame].Hmat[j][k];
317     }
318     }
319    
320     fgets( readBuffer, sizeof( readBuffer ), inFile );
321     if( feof( inFile ) ){
322     fprintf( stderr,
323     "File \"%s\" ended unexpectedly in Frame %d\n",
324     inName,
325     theFrame );
326     exit(0);
327     }
328    
329     i = atoi( readBuffer );
330     if( i != nAtoms ){
331     fprintf( stderr,
332     "Error in frame %d: frame atoms, %d, does not params atoms, %d\n",
333     theFrame,
334     i,
335     nAtoms );
336     exit(0);
337     }
338    
339     // read and toss the comment line
340    
341     fgets( readBuffer, sizeof( readBuffer ), inFile );
342     if( feof( inFile ) ){
343     fprintf( stderr,
344     "File \"%s\" ended unexpectedly in Frame %d\n",
345     inName,
346     theFrame );
347     exit(0);
348     }
349    
350     // read the atoms
351    
352     for( i=0; i < nAtoms; i++){
353    
354     fgets( readBuffer, sizeof( readBuffer ), inFile );
355     if( feof( inFile ) ){
356     fprintf( stderr,
357     "File \"%s\" ended unexpectedly in Frame %d at atom %d\n",
358     inName,
359     theFrame,
360     i );
361     exit(0);
362     }
363    
364    
365     parseDumpLine( readBuffer, i, atoms );
366     }
367     }
368    
369     void parseDumpLine( char readLine[BUFFER_SIZE], int index,
370     struct atomCoord* atoms ){
371     char* foo;
372     double q[4];
373    
374     // set the string tokenizer
375    
376     foo = strtok(readLine, " ,;\t");
377    
378     // check the atom ID
379    
380     switch( atoms[index].type ){
381    
382     case HEAD:
383     if( strcmp(foo, "HEAD") ){
384     fprintf( stderr,
385     "Atom %s does not match master array type \"HEAD\".\n"
386     foo );
387     exit(0);
388     }
389     break;
390    
391     case CH:
392     if( strcmp(foo, "CH") ){
393     fprintf( stderr,
394     "Atom %s does not match master array type \"CH\".\n"
395     foo );
396     exit(0);
397     }
398     break;
399    
400     case CH2:
401     if( strcmp(foo, "CH2") ){
402     fprintf( stderr,
403     "Atom %s does not match master array type \"CH2\".\n"
404     foo );
405     exit(0);
406     }
407     break;
408    
409     case CH3:
410     if( strcmp(foo, "CH3") ){
411     fprintf( stderr,
412     "Atom %s does not match master array type \"CH3\".\n"
413     foo );
414     exit(0);
415     }
416     break;
417    
418     case SSD:
419     if( strcmp(foo, "SSD") ){
420     fprintf( stderr,
421     "Atom %s does not match master array type \"SSD\".\n"
422     foo );
423     exit(0);
424     }
425     break;
426    
427     default:
428     fprintf(stderr,
429     "Atom %d is an unrecognized enumeration\n",
430     index );
431     exit(0);
432     }
433    
434     // get the positions
435    
436     foo = strtok(NULL, " ,;\t");
437     if(foo == NULL){
438     fprintf( stderr,
439     "error in reading postition x from %s\n"
440     "natoms = %d, index = %d\n",
441     inName, nAtoms, index );
442     exit(0);
443     }
444     atoms[i].pos[0] = atof( foo );
445    
446     foo = strtok(NULL, " ,;\t");
447     if(foo == NULL){
448     fprintf( stderr,
449     "error in reading postition y from %s\n"
450     "natoms = %d, index = %d\n",
451     inName, nAtoms, index );
452     exit(0);
453     }
454     atoms[i].pos[1] = atof( foo );
455    
456     foo = strtok(NULL, " ,;\t");
457     if(foo == NULL){
458     fprintf( stderr,
459     "error in reading postition z from %s\n"
460     "natoms = %d, index = %d\n",
461     inName, nAtoms, index );
462     exit(0);
463     }
464     atoms[i].pos[2] = atof( foo );
465    
466    
467     // get the velocities
468    
469     foo = strtok(NULL, " ,;\t");
470     if(foo == NULL){
471     fprintf( stderr,
472     "error in reading velocity x from %s\n"
473     "natoms = %d, index = %d\n",
474     inName, nAtoms, index );
475     exit(0);
476     }
477     atoms[i].vel[0] = atof( foo );
478    
479     foo = strtok(NULL, " ,;\t");
480     if(foo == NULL){
481     fprintf( stderr,
482     "error in reading velocity y from %s\n"
483     "natoms = %d, index = %d\n",
484     inName, nAtoms, index );
485     exit(0);
486     }
487     atoms[i].vel[1] = atof( foo );
488    
489     foo = strtok(NULL, " ,;\t");
490     if(foo == NULL){
491     fprintf( stderr,
492     "error in reading velocity z from %s\n"
493     "natoms = %d, index = %d\n",
494     inName, nAtoms, index );
495     exit(0);
496     }
497     atoms[i].vel[2] = atof( foo );
498    
499    
500     // get the quaternions
501    
502     if( (atoms[i].type == HEAD) || (atoms[i].type == SSD) ){
503    
504     foo = strtok(NULL, " ,;\t");
505     if(foo == NULL){
506     sprintf(painCave.errMsg,
507     "error in reading quaternion 0 from %s\n"
508     "natoms = %d, index = %d\n",
509     inName, nAtoms, index );
510     exit(0);
511     }
512     q[0] = atof( foo );
513    
514     foo = strtok(NULL, " ,;\t");
515     if(foo == NULL){
516     fprintf( stderr,
517     "error in reading quaternion 1 from %s\n"
518     "natoms = %d, index = %d\n",
519     inName, nAtoms, index );
520     exit(0);
521     }
522     q[1] = atof( foo );
523    
524     foo = strtok(NULL, " ,;\t");
525     if(foo == NULL){
526     fprintf( stderr,
527     "error in reading quaternion 2 from %s\n"
528     "natoms = %d, index = %d\n",
529     inName, nAtoms, index );
530     exit(0);
531     }
532     q[2] = atof( foo );
533    
534     foo = strtok(NULL, " ,;\t");
535     if(foo == NULL){
536     fprintf( stderr,
537     "error in reading quaternion 3 from %s\n"
538     "natoms = %d, index = %d\n",
539     inName, nAtoms, index );
540     exit(0);
541     }
542     q[3] = atof( foo );
543     }
544    
545    
546    
547    
548    
549     }