[go: up one dir, main page]

Menu

[r79]: / trunk / xmdview-1.1 / cdcorlt.c  Maximize  Restore  History

Download this file

517 lines (417 with data), 12.6 kB

  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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
/*
************************************************************************
History
************************************************************************
*/
/*
8 Jan 1997
Added test in ReadCorFile to return if end-of-file encountered
when reading first string. This was a problem encountered
when trying to read a COR file with more than one step.
*/
/*
************************************************************************
Compile Switches
************************************************************************
*/
/*
************************************************************************
File Includes
************************************************************************
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "cdcor.h"
/*
Import
Structure Particle_t
*/
#include "particle.h"
/*
Import
CleanAfterError()
*/
#include "cdhouse.h"
/*
Import
ReallocateParticle()
*/
#include "cdalloc.h"
/*
************************************************************************
Defines
************************************************************************
*/
/* Boolean values */
#define BOOLEAN int
#define TRUE 1
#define FALSE 0
/*
Define types for standardizing file reads between machines
- Following definitions valid for PC, Linux and AIX
*/
#define INT2 short
#define INT4 int
#define WORD1 unsigned char
#define WORD2 unsigned short
#define WORD4 unsigned long
#define REAL4 float
#define REAL8 double
/* Maximum value of word */
#define MAX_WORD2 0xffff
/* Default string length */
#define STRING_LENGTH 256
/*
************************************************************************
Macros
************************************************************************
*/
/* Macro for reversing byte order */
#define REVERSE(VAR) \
ReverseVar (&(VAR), sizeof(VAR));
/* Macro for block write */
#define FWRITE(VAR,FILE,N) \
fwrite (&(VAR), sizeof(VAR), (N), (FILE));
/* Macro for block read */
#define FREAD(VAR,FILE,N) \
fread (&(VAR), sizeof(VAR), (N), (FILE));
/* Macro for string write */
#define FWRITESTR(STRING,FILE) \
fwrite (STRING, strlen(STRING), (1), (FILE));
/*
************************************************************************
Type Definitions
************************************************************************
*/
typedef struct
{
INT2 EndianWord;
INT4 Run;
INT4 Step;
REAL8 Time;
WORD2 BoundaryType;
WORD1 Surf[NDIR];
REAL8 Box [NDIR];
REAL8 Etot;
REAL8 Epot;
REAL8 Ekin;
REAL8 Ebath;
REAL8 Min [NDIR];
REAL8 Max [NDIR];
INT4 Np;
}
CorFileHeader_t;
/*
************************************************************************
Module-wide Variables
************************************************************************
*/
INT2 EndianWord_m = 0x0001;
INT2 SwitchedEndianWord_m = 0x0100;
char *VersionStr_m = "COR FILE: Version 1.0.0";
char *CompareStr_m = "COR FILE";
/*
************************************************************************
Local Function Prototypes
************************************************************************
*/
void GetNextStringFromFile (char *InputString, int Length, FILE *InputFile);
void ReverseVar (void *WordPtr, int SizeVar);
void ReverseHeader (CorFileHeader_t *FileHeader);
/*
************************************************************************
Exported Functions
************************************************************************
*/
void ReadCorFile (FILE *InputFile, Particle_t *a)
{
int ititle;
int ipart;
int idir;
int InputNP;
WORD2 WordCoord[NDIR];
double Min [NDIR];
double Max [NDIR];
double Scale[NDIR];
BOOLEAN IsReverse;
char InputChar;
char InputStr[STRING_LENGTH];
CorFileHeader_t FileHeader;
/*
*********************
Read Text Format Data
*********************
*/
/* Determine if input file is a cor file */
GetNextStringFromFile (InputStr, STRING_LENGTH, InputFile);
/* Test for end of file */
if (feof(InputFile))
{
return;
}
if (strncmp(InputStr, CompareStr_m, strlen(CompareStr_m))!=0)
{
printf ("ERROR READING COR FILE: \n");
printf (" Input file is not a COR file.\n");
CleanAfterError();
}
/* Read input file version */
if (strcmp(InputStr, VersionStr_m)!=0)
{
printf ("ERROR READING COR FILE: \n");
printf (" Input file is version: %s\n", InputStr);
printf (" Can only read version: %s\n", VersionStr_m);
CleanAfterError();
}
/* Skip date, run, step, title, strings */
GetNextStringFromFile (InputStr, STRING_LENGTH, InputFile);
GetNextStringFromFile (InputStr, STRING_LENGTH, InputFile);
GetNextStringFromFile (InputStr, STRING_LENGTH, InputFile);
GetNextStringFromFile (InputStr, STRING_LENGTH, InputFile);
/* Read title lines */
LOOP (ititle, 8)
{
GetNextStringFromFile (InputStr, STRING_LENGTH, InputFile);
strcpy (a->title[ititle], InputStr);
}
/* Next character should be DOS End-of-file character */
FREAD (InputChar, InputFile, 1)
if (InputChar != '\032')
{
printf ("ERROR: COR file is corrupted.\n");
CleanAfterError();
}
/*
************************
Read Machine Format Data
************************
*/
FREAD (FileHeader.EndianWord, InputFile, 1)
FREAD (FileHeader.Run, InputFile, 1)
FREAD (FileHeader.Step, InputFile, 1)
FREAD (FileHeader.Time, InputFile, 1)
FREAD (FileHeader.BoundaryType, InputFile, 1)
FREAD (FileHeader.Surf[X], InputFile, 1)
FREAD (FileHeader.Surf[Y], InputFile, 1)
FREAD (FileHeader.Surf[Z], InputFile, 1)
FREAD (FileHeader.Box[X], InputFile, 1)
FREAD (FileHeader.Box[Y], InputFile, 1)
FREAD (FileHeader.Box[Z], InputFile, 1)
FREAD (FileHeader.Etot, InputFile, 1)
FREAD (FileHeader.Epot, InputFile, 1)
FREAD (FileHeader.Ekin, InputFile, 1)
FREAD (FileHeader.Ebath, InputFile, 1)
FREAD (FileHeader.Min[X], InputFile, 1)
FREAD (FileHeader.Min[Y], InputFile, 1)
FREAD (FileHeader.Min[Z], InputFile, 1)
FREAD (FileHeader.Max[X], InputFile, 1)
FREAD (FileHeader.Max[Y], InputFile, 1)
FREAD (FileHeader.Max[Z], InputFile, 1)
FREAD (FileHeader.Np, InputFile, 1)
WRITEVAR(FileHeader.Run,%i)
WRITEVAR(FileHeader.Step,%i)
WRITEVAR(FileHeader.Time,%le)
WRITEVAR(FileHeader.Box[X],%le)
WRITEVAR(FileHeader.Box[Y],%le)
WRITEVAR(FileHeader.Box[Z],%le)
/*
Set flag for reversing byte order
(necessary if file written on CPU of opposite Endian Type
*/
IsReverse = (FileHeader.EndianWord != EndianWord_m);
if (IsReverse)
ReverseHeader (&FileHeader);
/* Copy header to Particle_t data */
a->run = FileHeader.Run;
a->step = FileHeader.Step;
a->time = FileHeader.Time;
a->surf[X] = FileHeader.Surf[X];
a->surf[Y] = FileHeader.Surf[Y];
a->surf[Z] = FileHeader.Surf[Z];
a->bcur[X] = FileHeader.Box[X];
a->bcur[Y] = FileHeader.Box[Y];
a->bcur[Z] = FileHeader.Box[Z];
a->etot = FileHeader.Etot;
a->epot = FileHeader.Epot;
a->ekin = FileHeader.Ekin;
a->ebath = FileHeader.Ebath;
Min[X] = FileHeader.Min[X];
Min[Y] = FileHeader.Min[Y];
Min[Z] = FileHeader.Min[Z];
Max[X] = FileHeader.Max[X];
Max[Y] = FileHeader.Max[Y];
Max[Z] = FileHeader.Max[Z];
InputNP = (int) FileHeader.Np;
/* Initialize variables */
a->coordflag = TRUE;
a->boxflag = TRUE;
/*
************************
Allocate space as needed
************************
*/
/* Allocate space for types and particles */
/* Reallocate particles if new number different from previous number */
if (InputNP != a->np)
ReallocateParticle (a, InputNP);
/* Reallocate tag info if keeping it */
if (a->selkeep)
{
if (a->tag==NULL)
a->tag = (BYTE *) calloc ( (BYTE) InputNP, sizeof(BYTE) );
}
/* .. else remove tag info, clear select */
else
{
/* Remove tag */
if (a->tag!=NULL)
{
free (a->tag);
a->tag = NULL;
}
/* Clear select */
if (a->Selection != NULL)
LOOP (ipart, InputNP)
REMOVE_SELECT(ipart);
}
a->np = InputNP;
/*
**********************************
Read particle type and coordinates
**********************************
*/
/*
Read particle types (these are individual bytes ->
don't need to reverse byte order due to endian mis-match)
*/
FREAD (a->type[0], InputFile, a->np)
/* Find scale for particles */
LOOP (idir, NDIR)
{
WRITEVAR(Min[idir],%le)
WRITEVAR(Max[idir],%le)
if (Max[idir]==Min[idir])
Scale[idir] = 0.0;
else
Scale[idir] = (Max[idir] - Min[idir]) / MAX_WORD2;
}
/* Read particle coordinates */
LOOP (ipart, a->np)
{
/* Read scaled word coordinates */
FREAD (WordCoord[X], InputFile, NDIR)
/* Reverse byte order if necessary */
if (IsReverse)
{
REVERSE (WordCoord[X])
REVERSE (WordCoord[Y])
REVERSE (WordCoord[Z])
}
/* Scale to real coordinates */
LOOP (idir, NDIR)
{
a->cur[NDIR*ipart+idir] = Scale[idir]*WordCoord[idir] + Min[idir];
}
}
}
/* ... ReadCorFile */
/*
************************************************************************
Local Functions
************************************************************************
*/
/* Reverse bytes of word (works for any length WORD) */
void ReverseVars (void *VarPtr, int SizeVar, int NumVar)
{
BYTE *BytePtr;
BYTE TempByte;
int FirstIndex;
int LastIndex;
int ivar;
/* Set BYTE pointer to WORD array */
BytePtr = (BYTE *) VarPtr;
LOOP (ivar, NumVar)
{
/* Find start and end of current word in list */
FirstIndex = ivar * SizeVar;
LastIndex = FirstIndex + SizeVar - 1;
while (FirstIndex<LastIndex)
{
TempByte = BytePtr[FirstIndex];
BytePtr[FirstIndex] = BytePtr[LastIndex];
BytePtr[LastIndex ] = TempByte;
FirstIndex++;
LastIndex--;
}
}
}
/* .. Reverse Vars */
/* Read binary file into string until LineFeed encountered */
void GetNextStringFromFile (char *InputString, int Length, FILE *InputFile)
{
int iread;
char InputChar;
iread = 0;
do
{
FREAD (InputChar, InputFile, 1);
if (InputChar!='\r' && InputChar!='\n' && iread < Length-1)
{
InputString[iread] = InputChar;
iread++;
}
}
while (InputChar != '\r' && !feof(InputFile));
InputString[iread] = '\0';
}
/* Reverse bytes of variable */
void ReverseVar (void *VarPtr, int SizeVar)
{
BYTE *BytePtr;
BYTE TempByte;
int FirstIndex;
int LastIndex;
/* Return if no bytes to switch */
if (SizeVar<2)
return;
/* Set BYTE pointer to WORD array */
BytePtr = (BYTE *) VarPtr;
/* Find start and end of current word in list */
FirstIndex = 0;
LastIndex = FirstIndex + SizeVar - 1;
while (FirstIndex<LastIndex)
{
TempByte = BytePtr[FirstIndex];
BytePtr[FirstIndex] = BytePtr[LastIndex];
BytePtr[LastIndex ] = TempByte;
FirstIndex++;
LastIndex--;
}
}
/* .. Reverse Var */
void ReverseHeader (CorFileHeader_t *FileHeader)
{
REVERSE (FileHeader->Run)
REVERSE (FileHeader->Step)
REVERSE (FileHeader->Time)
REVERSE (FileHeader->BoundaryType)
REVERSE (FileHeader->Surf[X])
REVERSE (FileHeader->Surf[Y])
REVERSE (FileHeader->Surf[Z])
REVERSE (FileHeader->Box[X])
REVERSE (FileHeader->Box[Y])
REVERSE (FileHeader->Box[Z])
REVERSE (FileHeader->Etot)
REVERSE (FileHeader->Epot)
REVERSE (FileHeader->Ekin)
REVERSE (FileHeader->Ebath)
REVERSE (FileHeader->Min[X])
REVERSE (FileHeader->Min[Y])
REVERSE (FileHeader->Min[Z])
REVERSE (FileHeader->Max[X])
REVERSE (FileHeader->Max[Y])
REVERSE (FileHeader->Max[Z])
REVERSE (FileHeader->Np)
}