-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGromacs.h
2037 lines (1689 loc) · 53.9 KB
/
Gromacs.h
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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************************
*cr
*cr (C) Copyright 1995-2016 The Board of Trustees of the
*cr University of Illinois
*cr All Rights Reserved
*cr
***************************************************************************/
/***************************************************************************
* RCS INFORMATION:
* $RCSfile: Gromacs.h,v $
* $Author: johns $ $Locker: $ $State: Exp $
* $Revision: 1.34 $ $Date: 2016/11/28 05:01:53 $
***************************************************************************/
/*
* GROMACS file format reader for VMD
*
* This code provides a high level I/O library for reading
* and writing the following file formats:
* gro GROMACS format or trajectory
* g96 GROMOS-96 format or trajectory
* trj Trajectory - x, v and f (binary, full precision)
* trr Trajectory - x, v and f (binary, full precision, portable)
* xtc Trajectory - x only (compressed, portable, any precision)
* top
* Currently supported: gro trj trr g96 [xtc]
*
* TODO list
* o velocities are ignored because VMD doesn't use them, but some other
* program might ...
* o gro_rec() assumes positions in .gro files are nanometers and
* converts to angstroms, whereas they really could be any unit
*/
#ifndef GROMACS_H
#define GROMACS_H
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#if defined(_AIX)
#include <strings.h>
#endif
#include "endianswap.h"
#if defined(WIN32) || defined(WIN64)
#define strcasecmp stricmp
#endif
#ifndef M_PI_2
#define M_PI_2 1.57079632679489661922
#endif
// Error codes for mdio_errno
#define MDIO_SUCCESS 0
#define MDIO_BADFORMAT 1
#define MDIO_EOF 2
#define MDIO_BADPARAMS 3
#define MDIO_IOERROR 4
#define MDIO_BADPRECISION 5
#define MDIO_BADMALLOC 6
#define MDIO_CANTOPEN 7
#define MDIO_BADEXTENSION 8
#define MDIO_UNKNOWNFMT 9
#define MDIO_CANTCLOSE 10
#define MDIO_WRONGFORMAT 11
#define MDIO_SIZEERROR 12
#define MDIO_UNKNOWNERROR 1000
#define MDIO_READ 0
#define MDIO_WRITE 1
#define MDIO_MAX_ERRVAL 11
// Format extensions
const char *mdio_fmtexts[] = {
"",
".gro",
".trr",
".g96",
".trj",
".xtc",
NULL
};
static int mdio_errcode; // Last error code
#define TRX_MAGIC 1993 // Magic number for .trX files
#define XTC_MAGIC 1995 // Magic number for .xtc files
#define MAX_GRO_LINE 500 // Maximum line length of .gro files
#define MAX_G96_LINE 500 // Maximum line length of .g96 files
#define MAX_TRX_TITLE 80 // Maximum length of a title in .trX
#define MAX_MDIO_TITLE 80 // Maximum supported title length
#define ANGS_PER_NM 10 // Unit conversion factor
#define ANGS2_PER_NM2 100 // Unit conversion factor
// All the supported file types and their respective extensions
#define MDFMT_GRO 1
#define MDFMT_TRR 2
#define MDFMT_G96 3
#define MDFMT_TRJ 4
#define MDFMT_XTC 5
// A structure to hold .trX file format header information. This
// is an optional member of the md_file structure that is used
// when .trX files are being dealt with.
typedef struct {
int version; // File version number
char title[MAX_TRX_TITLE + 1]; // File title
int ir_size;
int e_size;
int box_size;
int vir_size;
int pres_size;
int top_size;
int sym_size;
int x_size; // Positions of atoms
int v_size; // Velocities of atoms
int f_size;
int natoms; // Number of atoms in the system
int step;
int nre;
float t;
float lambda;
} trx_hdr;
// A generic i/o structure that contains information about the
// file itself and the input/output state
typedef struct {
FILE * f; // Pointer to the file
int fmt; // The file format
int prec; // Real number precision
int rev; // Reverse endiannism?
trx_hdr * trx; // Trx files require a great deal more
// header data to be stored.
} md_file;
// A format-independent structure to hold header data from files
typedef struct {
char title[MAX_MDIO_TITLE + 1];
int natoms;
float timeval;
} md_header;
// A format-independent structure to hold unit cell data
typedef struct {
float A, B, C, alpha, beta, gamma;
} md_box;
// Timestep information
typedef struct {
float *pos; // Position array (3 * natoms)
//float *vel; // Velocity array ** (VMD doesn't use this) **
//float *f; // Force array ** (VMD doesn't use this) **
//float *box; // Computational box ** (VMD doesn't use this) **
int natoms; // Number of atoms
int step; // Simulation step
float time; // Time of simulation
md_box *box;
} md_ts;
// Atom information
typedef struct {
char resid[7]; // Residue index number
char resname[7]; // Residue name
int atomnum; // Atom index number
char atomname[7]; // Atom name
float pos[3]; // Position array (3 * natoms)
//float vel[3]; // Velocity array ** (VMD doesn't use this) **
} md_atom;
// Open a molecular dynamics file. The second parameter specifies
// the format of the file. If it is zero, the format is determined
// from the file extension. the third argument (if given) decides
// whether to read (==0) or to write (!= 0).
// using a default argument set to read for backward compatibility.
static md_file *mdio_open(const char *, const int, const int=MDIO_READ);
// Closes a molecular dynamics file.
static int mdio_close(md_file *);
// Format-independent file I/O routines
static int mdio_header(md_file *, md_header *);
static int mdio_timestep(md_file *, md_ts *);
// .gro file functions
static int gro_header(md_file *, char *, int, float *, int *, int = 1);
static int gro_rec(md_file *, md_atom *);
static int gro_timestep(md_file *, md_ts *);
// .trX file functions
static int trx_header(md_file *, int = 0);
static int trx_int(md_file *, int *);
static int trx_real(md_file *, float *);
static int trx_rvector(md_file *, float *);
static int trx_string(md_file *, char *, int);
static int trx_timestep(md_file *, md_ts *);
// .g96 file functions
static int g96_header(md_file *, char *, int, float *);
static int g96_timestep(md_file *, md_ts *);
static int g96_rec(md_file *, md_atom *);
static int g96_countatoms(md_file *);
// .xtc file functions
static int xtc_int(md_file *, int *);
static int xtc_float(md_file *, float *);
/*
static int xtc_receivebits(int *, int);
static void xtc_receiveints(int *, int, int, const unsigned *, int *);
*/
static int xtc_timestep(md_file *, md_ts *);
static int xtc_3dfcoord(md_file *, float *, int *, float *);
/* begin by:bing 2017-07-27 */
static int is_compressed(md_file *);
static int xtc_uc_data(md_file *, char *, int);
/* end by:bing 2017-07-27 */
// Error reporting functions
static int mdio_errno(void);
static const char *mdio_errmsg(int);
static int mdio_seterror(int);
// Miscellaneous functions
static int strip_white(char *);
static int mdio_readline(md_file *, char *, int, int = 1);
static int mdio_tsfree(md_ts *, int = 0);
static int mdio_readbox(md_box *, float *, float *, float *);
static int xtc_receivebits(int *, int);
// Error descriptions for mdio_errno
static const char *mdio_errdescs[] = {
"no error",
"file does not match format",
"unexpected end-of-file reached",
"function called with bad parameters",
"file i/o error",
"unsupported precision",
"out of memory",
"cannot open file",
"bad file extension",
"unknown file format",
"cannot close file",
"wrong file format for this function",
"binary i/o error: sizeof(int) != 4",
NULL
};
/*! \fn static inline bool host_is_little_endian(void)
* detect endiannes of host machine. returns true on little endian machines. */
static inline int host_is_little_endian(void)
{
const union { unsigned char c[4]; unsigned int i; }
fixed = { { 0x10 , 0x20 , 0x40 , 0x80 } };
const unsigned int i = 0x80402010U;
if (fixed.i == i) {
return 1;
}
return 0;
}
// Open a molecular dynamics file. The second parameter specifies
// the format of the file. If it is zero, the format is determined
// from the file extension.
md_file *mdio_open(const char *fn, const int fmt, const int rw) {
md_file *mf;
if (!fn) {
mdio_seterror(MDIO_BADPARAMS);
return NULL;
}
// Allocate memory
mf = (md_file *) malloc(sizeof(md_file));
if (!mf) {
mdio_seterror(MDIO_BADMALLOC);
return NULL;
}
// Zero out the structure
memset(mf, 0, sizeof(md_file));
// Determine the file type from the extension
if (!fmt) {
char *p;
int n;
// Seek to the extension part of the filename
for (p = (char *) &fn[strlen(fn) - 1]; *p != '.' && p > fn; p--);
if (p == fn) {
free(mf);
mdio_seterror(MDIO_BADEXTENSION);
return NULL;
}
// Check the extension against known extensions
for (n = 1; mdio_fmtexts[n]; n++)
if (!strcasecmp(p, mdio_fmtexts[n])) break;
// If !mdio_fmtexts[n], we failed (unknown ext)
if (!mdio_fmtexts[n]) {
free(mf);
mdio_seterror(MDIO_UNKNOWNFMT);
return NULL;
}
// All set
mf->fmt = n;
}
else {
mf->fmt = fmt;
}
// Differentiate between binary and ascii files. Also,
// .trX files need a header information structure allocated.
switch (mf->fmt) {
case MDFMT_GRO:
case MDFMT_G96: /* fallthrough */
if (rw)
mf->f = fopen(fn, "wt");
else
mf->f = fopen(fn, "rt");
break;
case MDFMT_TRR:
case MDFMT_TRJ: /* fallthrough */
// Allocate the trx header data struct
mf->trx = (trx_hdr *) malloc(sizeof(trx_hdr));
if (!mf->trx) {
free(mf);
mdio_seterror(MDIO_BADMALLOC);
return NULL;
}
memset(mf->trx, 0, sizeof(trx_hdr));
case MDFMT_XTC: /* fallthrough */
// Finally, open the file
if (rw)
mf->f = fopen(fn, "wb");
else
mf->f = fopen(fn, "rb");
break;
default:
free(mf);
mdio_seterror(MDIO_UNKNOWNFMT);
return NULL;
}
// Check for opening error
if (!mf->f) {
if (mf->trx) free(mf->trx);
free(mf);
mdio_seterror(MDIO_CANTOPEN);
return NULL;
}
// File is opened, we're all set!
mdio_seterror(MDIO_SUCCESS);
return mf;
}
// Closes a molecular dynamics file.
static int mdio_close(md_file *mf) {
if (!mf) return mdio_seterror(MDIO_BADPARAMS);
if (fclose(mf->f) == EOF) return mdio_seterror(MDIO_CANTCLOSE);
// Free the dynamically allocated memory
if (mf->trx) free(mf->trx);
free(mf);
return mdio_seterror(MDIO_SUCCESS);
}
// Returns the last error code reported by any of the mdio functions
static int mdio_errno(void) {
return mdio_errcode;
}
// Returns a textual message regarding an mdio error code
static const char *mdio_errmsg(int n) {
if (n < 0 || n > MDIO_MAX_ERRVAL) return (char *) "unknown error";
else return mdio_errdescs[n];
}
// Sets the error code and returns an appropriate return value
// for the calling function to return to its parent
static int mdio_seterror(int code) {
mdio_errcode = code;
return code ? -1 : 0;
}
// Reads a line from the text file, strips leading/trailing whitespace
// and newline, checks for errors, and returns the number of characters
// in the string on success or -1 on error.
static int mdio_readline(md_file *mf, char *buf, int n, int strip) {
if (!buf || n < 1 || !mf) return mdio_seterror(MDIO_BADPARAMS);
// Read the line
fgets(buf, n, mf->f);
// End of file reached?
if (feof(mf->f)) return mdio_seterror(MDIO_EOF);
// File I/O error?
if (ferror(mf->f)) return mdio_seterror(MDIO_IOERROR);
// comment line?
if (buf[0] == '#') return mdio_readline(mf,buf,n,strip);
// Strip whitespace
if (strip) strip_white(buf);
return strlen(buf);
}
// Strips leading and trailing whitespace from a string. Tabs,
// spaces, newlines and carriage returns are stripped. Example:
// "\n hello\t \r" becomes "hello".
static int strip_white(char *buf) {
int i, j, k;
// Protect against NULL pointer
if (!buf) return -1;
if (!strlen(buf)) return -1;
// Kill trailing whitespace first
for (i = strlen(buf) - 1;
buf[i] == ' ' || buf[i] == '\t' ||
buf[i] == '\n' || buf[i] == '\r';
i--)
buf[i] = 0;
// Skip past leading whitespace
for (i = 0; buf[i] == ' ' || buf[i] == '\t' ||
buf[i] == '\n' || buf[i] == '\r'; i++);
if (i) {
k = 0;
for (j = i; buf[j]; j++)
buf[k++] = buf[j];
buf[k] = 0;
}
return strlen(buf);
}
// Frees the memory allocated in a ts structure. The holderror
// parameter defaults to zero. Programs that are calling this
// function because of an error reported by another function should
// set holderror so that mdio_tsfree() does not overwrite the error
// code with mdio_seterror().
static int mdio_tsfree(md_ts *ts, int holderror) {
if (!ts) {
if (holderror) return -1;
else return mdio_seterror(MDIO_BADPARAMS);
}
if (ts->pos && ts->natoms > 0) free(ts->pos);
if (ts->box) free(ts->box);
if (holderror) return -1;
else return mdio_seterror(MDIO_SUCCESS);
}
// Converts box basis vectors to A, B, C, alpha, beta, and gamma.
// Stores values in md_box struct, which should be allocated before calling
// this function.
static int mdio_readbox(md_box *box, float *x, float *y, float *z) {
float A, B, C;
if (!box) {
return mdio_seterror(MDIO_BADPARAMS);
}
// A, B, C are the lengths of the x, y, z vectors, respectively
A = sqrt( x[0]*x[0] + x[1]*x[1] + x[2]*x[2] ) * ANGS_PER_NM;
B = sqrt( y[0]*y[0] + y[1]*y[1] + y[2]*y[2] ) * ANGS_PER_NM;
C = sqrt( z[0]*z[0] + z[1]*z[1] + z[2]*z[2] ) * ANGS_PER_NM;
if ((A<=0) || (B<=0) || (C<=0)) {
/* Use zero-length box size and set angles to 90. */
box->A = box->B = box->C = 0;
box->alpha = box->beta = box->gamma = 90;
} else {
box->A = A;
box->B = B;
box->C = C;
// gamma, beta, alpha are the angles between the x & y, x & z, y & z
// vectors, respectively
box->gamma = acos( (x[0]*y[0]+x[1]*y[1]+x[2]*y[2])*ANGS2_PER_NM2/(A*B) ) * 90.0/M_PI_2;
box->beta = acos( (x[0]*z[0]+x[1]*z[1]+x[2]*z[2])*ANGS2_PER_NM2/(A*C) ) * 90.0/M_PI_2;
box->alpha = acos( (y[0]*z[0]+y[1]*z[1]+y[2]*z[2])*ANGS2_PER_NM2/(B*C) ) * 90.0/M_PI_2;
}
return mdio_seterror(MDIO_SUCCESS);
}
// Reads the header of a file (format independent)
static int mdio_header(md_file *mf, md_header *mdh) {
int n;
if (!mf || !mdh) return mdio_seterror(MDIO_BADPARAMS);
if (!mf->f) return mdio_seterror(MDIO_BADPARAMS);
switch (mf->fmt) {
case MDFMT_GRO:
if (gro_header(mf, mdh->title, MAX_MDIO_TITLE,
&mdh->timeval, &mdh->natoms, 1) < 0)
return -1;
return 0;
case MDFMT_TRR:
case MDFMT_TRJ: /* fallthrough */
if (trx_header(mf, 1) < 0) return -1;
mdh->natoms = mf->trx->natoms;
mdh->timeval = (float) mf->trx->t;
strncpy(mdh->title, mf->trx->title, MAX_MDIO_TITLE);
return 0;
case MDFMT_G96:
if (g96_header(mf, mdh->title, MAX_MDIO_TITLE,
&mdh->timeval) < 0) return -1;
mdh->natoms = -1;
return 0;
case MDFMT_XTC:
memset(mdh, 0, sizeof(md_header));
// Check magic number
if (xtc_int(mf, &n) < 0) return -1;
if (n != XTC_MAGIC) return mdio_seterror(MDIO_BADFORMAT);
// Get number of atoms
if (xtc_int(mf, &n) < 0) return -1;
mdh->natoms = n;
rewind(mf->f);
return 0;
default:
return mdio_seterror(MDIO_UNKNOWNFMT);
}
}
// Reads in a timestep from a file (format independent)
static int mdio_timestep(md_file *mf, md_ts *ts) {
if (!mf || !ts) return mdio_seterror(MDIO_BADPARAMS);
if (!mf->f) return mdio_seterror(MDIO_BADPARAMS);
switch (mf->fmt) {
case MDFMT_GRO:
return gro_timestep(mf, ts);
case MDFMT_TRR:
case MDFMT_TRJ: /* fallthrough */
return trx_timestep(mf, ts);
case MDFMT_G96:
return g96_timestep(mf, ts);
case MDFMT_XTC:
return xtc_timestep(mf, ts);
default:
return mdio_seterror(MDIO_UNKNOWNFMT);
}
}
static int g96_header(md_file *mf, char *title, int titlelen, float *timeval) {
char buf[MAX_G96_LINE + 1];
char *p;
// Check parameters
if (!mf) return mdio_seterror(MDIO_BADPARAMS);
// The header consists of blocks. The title block
// is mandatory, and a TIMESTEP block is optional.
// Example:
//
// TITLE
// Generated by trjconv : t= 90.00000
// more title info
// .
// .
// .
// END
// .
// .
// .
if (mdio_readline(mf, buf, MAX_G96_LINE + 1) < 0) return -1;
if (strcasecmp(buf, "TITLE")) return mdio_seterror(MDIO_BADFORMAT);
// Read in the title itself
if (mdio_readline(mf, buf, MAX_G96_LINE + 1) < 0) return -1;
// The timevalue can be included in the title string
// after a "t=" prefix.
if ((p = (char *) strstr(buf, "t="))) {
char *q = p;
*(q--) = 0;
// Skip the `t=' and strip whitespace from
// the resulting strings
p += 2;
strip_white(p);
strip_white(buf);
// Grab the timevalue from the title string
if (timeval) *timeval = (float) atof(p);
}
else {
// No timevalue - just copy the string and strip
// any leading/trailing whitespace
if (timeval) *timeval = 0;
strip_white(buf);
}
// Copy the title string
if (title && titlelen) strncpy(title, buf, titlelen);
// Now ignore subsequent title lines and get the END string
while (strcasecmp(buf, "END"))
if (mdio_readline(mf, buf, MAX_G96_LINE + 1) < 0) return -1;
// Done!
return mdio_seterror(MDIO_SUCCESS);
}
// Used to determine the number of atoms in a g96 file, because
// VMD needs to know this for some reason.
static int g96_countatoms(md_file *mf) {
char buf[MAX_G96_LINE + 1];
int natoms;
int n;
long fpos;
float lastf;
if (!mf) return mdio_seterror(MDIO_BADPARAMS);
fpos = ftell(mf->f);
natoms = 0;
for (;;) {
if (mdio_readline(mf, buf, MAX_G96_LINE + 1, 0) < 0)
break;
n = sscanf(buf, "%*6c%*6c%*6c%*6c %*f %*f %f", &lastf);
if (n == 1) natoms++;
else {
strip_white(buf);
if (!strcasecmp(buf, "END")) break;
}
}
fseek(mf->f, fpos, SEEK_SET);
return natoms;
}
// Reads an atom line from the G96 file
static int g96_rec(md_file *mf, md_atom *ma) {
char buf[MAX_G96_LINE + 1];
char atomnum[7];
int n;
// Check parameters
if (!mf || !ma) return mdio_seterror(MDIO_BADPARAMS);
// Read in a line, assuming it is an atom line
do {
if (mdio_readline(mf, buf, MAX_G96_LINE + 1, 0) < 0) return -1;
} while (buf[0] == '#' || strlen(buf) == 0);
n = sscanf(buf, "%6c%6c%6c%6c %f %f %f",
ma->resid, ma->resname, ma->atomname, atomnum,
&ma->pos[0], &ma->pos[1], &ma->pos[2]);
if (n == 7) {
atomnum[6] = 0;
ma->resid[6] = 0;
ma->resname[6] = 0;
ma->atomname[6] = 0;
strip_white(atomnum);
strip_white(ma->resid);
strip_white(ma->resname);
strip_white(ma->atomname);
ma->atomnum = atoi(atomnum);
ma->pos[0] *= ANGS_PER_NM;
ma->pos[1] *= ANGS_PER_NM;
ma->pos[2] *= ANGS_PER_NM;
return 0;
}
return mdio_seterror(MDIO_BADFORMAT);
}
// Reads a timestep from a G96 file and stores the data in
// the generic md_ts structure. Returns 0 on success or a
// negative number on error and sets mdio_errcode.
static int g96_timestep(md_file *mf, md_ts *ts) {
char buf[MAX_G96_LINE + 1];
char stripbuf[MAX_G96_LINE + 1];
float pos[3], x[3], y[3], z[3], *currAtom;
long fpos;
int n, i, boxItems;
// Check parameters
if (!mf || !ts) return mdio_seterror(MDIO_BADPARAMS);
// Allocate data space for the timestep, using the number of atoms
// determined by open_g96_read().
ts->pos = (float *) malloc(sizeof(float) * 3 * ts->natoms);
if (!ts->pos) {
return mdio_seterror(MDIO_BADMALLOC);
}
currAtom = ts->pos;
// The timesteps follow the header in a fixed block
// format:
//
// TIMESTEP
// <step number> <time value>
// END
// POSITIONRED
// <x float> <y float> <z float>
// . . .
// . . .
// . . .
// END
// VELOCITYRED
// <x float> <y float> <z float>
// . . .
// . . .
// . . .
// END
// BOX
// <x float> <y float> <z float>
// END
//
// -----
//
// The TIMESTEP, VELOCITY and BOX blocks are optional.
// Floats are written in 15.9 precision.
//
// Reference: GROMACS 2.0 user manual
// http://rugmd4.chem.rug.nl/~gmx/online2.0/g96.html
// First, look for an (optional) title block and skip it
if (mdio_readline(mf, buf, MAX_G96_LINE + 1) < 0) return -1;
if (!strcasecmp(buf, "TITLE")) {
// skip over the text until we reach 'END'
while (strcasecmp(buf, "END")) {
if (mdio_readline(mf, buf, MAX_G96_LINE + 1) < 0) return -1;
}
// Read in the next line
if (mdio_readline(mf, buf, MAX_G96_LINE + 1) < 0) return -1;
}
// Next, look for a timestep block
if (!strcasecmp(buf, "TIMESTEP")) {
// Read in the value line
if (mdio_readline(mf, buf, MAX_G96_LINE + 1) < 0) return -1;
// Extract the time value and the timestep index
n = sscanf(buf, "%d %f", &ts->step, &ts->time);
if (n != 2) return mdio_seterror(MDIO_BADFORMAT);
// Read the "END" line
if (mdio_readline(mf, buf, MAX_G96_LINE + 1) < 0) return -1;
if (strcasecmp(buf, "END"))
return mdio_seterror(MDIO_BADFORMAT);
// Read in the next line
if (mdio_readline(mf, buf, MAX_G96_LINE + 1) < 0) return -1;
}
else {
// No timestep specified -- set to zero
ts->step = 0;
ts->time = 0;
}
// At this point a POSITION or POSITIONRED block
// is REQUIRED by the format
if (!strcasecmp(buf, "POSITIONRED")) {
// So now we read in some atoms
i = 0;
while (i < ts->natoms) {
// Read in an atom
if (mdio_readline(mf, buf, MAX_G96_LINE + 1) < 0)
return -1;
// We shouldn't reach the end yet
if (!strcasecmp(buf, "END"))
return mdio_seterror(MDIO_BADFORMAT);
// Get the x,y,z coordinates
n = sscanf(buf, "%f %f %f", &pos[0], &pos[1], &pos[2]);
// Ignore improperly formatted lines
if (n == 3) {
pos[0] *= ANGS_PER_NM;
pos[1] *= ANGS_PER_NM;
pos[2] *= ANGS_PER_NM;
// Copy the atom data into the array
memcpy(currAtom, pos, sizeof(float) * 3);
currAtom += 3;
i++;
}
}
}
else if (!strcasecmp(buf, "POSITION") || !strcasecmp(buf, "REFPOSITION")) {
/*
char resnum[7];
char resname[7];
char atomname[7];
char atomnum[7];
*/
// So now we read in some atoms
i = 0;
while (i < ts->natoms) {
// Read in the first line
if (mdio_readline(mf, buf, MAX_G96_LINE + 1, 0) < 0)
return -1;
// We shouldn't reach the end yet
strcpy(stripbuf, buf);
strip_white(stripbuf);
if (!strcasecmp(stripbuf, "END"))
return mdio_seterror(MDIO_BADFORMAT);
// Get the x,y,z coordinates and name data
n = sscanf(buf, "%*6c%*6c%*6c%*6c %f %f %f",
&pos[0], &pos[1], &pos[2]);
// Ignore improperly formatted lines
if (n == 3) {
pos[0] *= ANGS_PER_NM;
pos[1] *= ANGS_PER_NM;
pos[2] *= ANGS_PER_NM;
// Copy the atom data into the linked list item
memcpy(currAtom, pos, sizeof(float) * 3);
currAtom += 3;
i++;
}
}
}
else {
return mdio_seterror(MDIO_BADFORMAT);
}
// Read the END keyword
if (mdio_readline(mf, buf, MAX_G96_LINE + 1) < 0)
return -1;
if (strcasecmp(buf, "END"))
return mdio_seterror(MDIO_BADFORMAT);
// ... another problem: there may or may not be a VELOCITY
// block or a BOX block, so we need to read one line beyond
// the POSITION block to determine this. If neither VEL. nor
// BOX are present we've read a line too far and infringed
// on the next timestep, so we need to keep track of the
// position now for a possible fseek() later to backtrack.
fpos = ftell(mf->f);
// Now we must read in the velocities and the box, if present
if (mdio_readline(mf, buf, MAX_G96_LINE + 1) < 0) {
// It's okay if we end the file here; any other errors need to be
// reported.
if (mdio_errcode == MDIO_EOF)
return mdio_seterror(MDIO_SUCCESS);
else
return -1;
}
// Is there a velocity block present ?
if (!strcasecmp(buf, "VELOCITY") || !strcasecmp(buf, "VELOCITYRED")) {
// Ignore all the coordinates - VMD doesn't use them
for (;;) {
if (mdio_readline(mf, buf, MAX_G96_LINE + 1) < 0)
return -1;
if (!strcasecmp(buf, "END")) break;
}
// Again, record our position because we may need
// to fseek here later if we read too far.
fpos = ftell(mf->f);
// Go ahead and read the next line.
if (mdio_readline(mf, buf, MAX_G96_LINE + 1) < 0) return -1;
}
// Is there a box present ?
if (!strcasecmp(buf, "BOX")) {
if (mdio_readline(mf, buf, MAX_G96_LINE + 1) < 0) return -1;
boxItems = sscanf(buf, " %f %f %f %f %f %f %f %f %f",
&x[0], &y[1], &z[2], &x[1], &x[2], &y[0], &y[2], &z[0], &z[1]);
if (boxItems == 3) {
x[1] = x[2] = 0;
y[0] = y[2] = 0;
z[0] = z[1] = 0;
}
else if (boxItems != 9)
return mdio_seterror(MDIO_BADFORMAT);
// Allocate the box and convert the vectors.
ts->box = (md_box *) malloc(sizeof(md_box));
if (mdio_readbox(ts->box, x, y, z) < 0) {
free(ts->box);
ts->box = NULL;
return mdio_seterror(MDIO_BADFORMAT);
}
if (mdio_readline(mf, buf, MAX_G96_LINE + 1) < 0) {
free(ts->box);
ts->box = NULL;
return -1;
}
if (strcasecmp(buf, "END")) {
free(ts->box);
ts->box = NULL;
return mdio_seterror(MDIO_BADFORMAT);
}
}
else {
// We have read too far, so fseek back to the
// last known safe position so we don't return
// with the file pointer set infringing on the
// next timestep data.
fseek(mf->f, fpos, SEEK_SET);
}
// We're done!
return mdio_seterror(MDIO_SUCCESS);
}
// Attempts to read header data from a GROMACS structure file
// The GROMACS header format is as follows (fixed, 2 lines ASCII):
// <title> [ n= <timevalue> ]
// <num atoms>
static int gro_header(md_file *mf, char *title, int titlelen, float *timeval,
int *natoms, int rewind) {
char buf[MAX_GRO_LINE + 1];
long fpos;
char *p;
// Check parameters
if (!mf)
return mdio_seterror(MDIO_BADPARAMS);
// Get the current file position for rewinding later
fpos = ftell(mf->f);
// The header consists of 2 lines - get the first line
if (mdio_readline(mf, buf, MAX_GRO_LINE + 1) < 0) return -1;