ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/tcProps/readWrite.c
(Generate patch)

Comparing trunk/tcProps/readWrite.c (file contents):
Revision 1055 by mmeineke, Mon Feb 16 21:50:34 2004 UTC vs.
Revision 1056 by mmeineke, Tue Feb 17 14:30:48 2004 UTC

# Line 13 | Line 13 | FILE* inFile;
13  
14   #define BUFFER_SIZE 2000
15   int isScanned;
16 + int fileOpen;
17 + int nFrames;
18 + double *frameTimes;
19   FILE* inFile;
20  
21  
# Line 27 | Line 30 | struct staticPos{
30   struct staticPos{
31      
32    fpos_t *myPos;
30  double timeStamp;
33    double Hmat[3][3];
34   };
35   struct staticPos* posArray;
36  
37 + void parseDumpLine( char readBuffer[BUFFER_SIZE], int index,
38 +                    struct atomCoord* atoms );
39  
40 + 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   void closeFile( void ){
54  
55    fclose( inFile );
56 +
57 +  fileOpen = 0;
58   }
59  
60 < int setFrames( char* inFile ){
60 > int setFrames( void ){
61  
43  int nFrames = 0;
62    int i,j,k;
63    struct linkedPos* headPos;
64    struct linkedPos* currPos;
# Line 51 | Line 69 | int setFrames( char* inFile ){
69    int lineNum = 0;
70    
71    
72 <
55 <  inFile = fopen(inName);
56 <  if(inFile ==NULL){
72 >  if( !fileOpen ){
73      fprintf(stderr,
74 <            "Error opening file \"%s\"\n",
59 <            inName);
74 >            "Attempt to scan the file without first opening the file.\n" );
75      exit(0);
76    }
77  
78 <  
78 >
79 >  nFrames = 0;
80    headPos = (struct linkedPos*)malloc(sizeof(struct linkedPos));
81    currPos = headPos;
82    while( !feof( inFile ) ){
# Line 212 | Line 228 | int setFrames( char* inFile ){
228        }
229      }
230    }
231 +  
232 +  // allocate the static position array
233  
234 +  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    isScanned = 1;
217  return nFrames;
272   }
273 +
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 + }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines