3 * This source code is part of
7 * GROningen MAchine for Chemical Simulations
10 * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
11 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
12 * Copyright (c) 2001-2004, The GROMACS development team,
13 * check out http://www.gromacs.org for more information.
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
20 * If you want to redistribute modifications, please consider that
21 * scientific software is very special. Version control is crucial -
22 * bugs must be traceable. We will be happy to consider code for
23 * inclusion in the official distribution, but derived work must not
24 * be called official GROMACS. Details are found in the README & COPYING
25 * files - if they are missing, get the official version at www.gromacs.org.
27 * To help us fund GROMACS development, we humbly ask that you cite
28 * the papers on the package - you can find them in the top README file.
30 * For more info, check our website at http://www.gromacs.org
33 * GROningen Mixture of Alchemy and Childrens' Stories
48 #include "gmx_fatal.h"
53 # define gmx_fseek(A,B,C) fseeko(A,B,C)
54 # define gmx_ftell(A) ftello(A)
55 # define gmx_off_t off_t
57 # define gmx_fseek(A,B,C) fseek(A,B,C)
58 # define gmx_ftell(A) ftell(A)
59 # define gmx_off_t int
64 /* This is just for clarity - it can never be anything but 4! */
65 #define XDR_INT_SIZE 4
67 /* same order as the definition of xdr_datatype */
68 const char *xdr_datatype_names
[] =
81 /* NOTE: DO NOT USE THESE ANYWHERE IN GROMACS ITSELF.
82 These are necessary for the backward-compatile io routines for 3d party
85 static FILE *xdrfiles
[MAXID
];
86 static XDR
*xdridptr
[MAXID
];
87 static char xdrmodes
[MAXID
];
88 static unsigned int cnt
;
90 /* we need this because of the global variables above for FORTRAN binding.
91 The I/O operations are going to be slow. */
92 static tMPI_Thread_mutex_t xdr_fortran_mutex
=TMPI_THREAD_MUTEX_INITIALIZER
;
95 static void xdr_fortran_lock(void)
98 tMPI_Thread_mutex_lock(&xdr_fortran_mutex
);
101 static void xdr_fortran_unlock(void)
104 tMPI_Thread_mutex_unlock(&xdr_fortran_mutex
);
110 /* the open&close prototypes */
111 static int xdropen(XDR
*xdrs
, const char *filename
, const char *type
);
112 static int xdrclose(XDR
*xdrs
);
114 typedef void (* F77_FUNC(xdrfproc
,XDRFPROC
))(int *, void *, int *);
116 int ftocstr(char *ds
, int dl
, char *ss
, int sl
)
124 while ( --p
>= ss
&& *p
== ' ' );
137 int ctofstr(char *ds
, int dl
, char *ss
)
139 /* max dest length */
140 /* src string (0-term) */
152 F77_FUNC(xdrfbool
,XDRFBOOL
)(int *xdrid
, int *pb
, int *ret
)
155 *ret
= xdr_bool(xdridptr
[*xdrid
], pb
);
157 xdr_fortran_unlock();
161 F77_FUNC(xdrfchar
,XDRFCHAR
)(int *xdrid
, char *cp
, int *ret
)
164 *ret
= xdr_char(xdridptr
[*xdrid
], cp
);
166 xdr_fortran_unlock();
170 F77_FUNC(xdrfdouble
,XDRFDOUBLE
)(int *xdrid
, double *dp
, int *ret
)
173 *ret
= xdr_double(xdridptr
[*xdrid
], dp
);
174 cnt
+= sizeof(double);
175 xdr_fortran_unlock();
179 F77_FUNC(xdrffloat
,XDRFFLOAT
)(int *xdrid
, float *fp
, int *ret
)
182 *ret
= xdr_float(xdridptr
[*xdrid
], fp
);
183 cnt
+= sizeof(float);
184 xdr_fortran_unlock();
188 F77_FUNC(xdrfint
,XDRFINT
)(int *xdrid
, int *ip
, int *ret
)
191 *ret
= xdr_int(xdridptr
[*xdrid
], ip
);
193 xdr_fortran_unlock();
197 F77_FUNC(xdrfshort
,XDRFSHORT
)(int *xdrid
, short *sp
, int *ret
)
200 *ret
= xdr_short(xdridptr
[*xdrid
], sp
);
202 xdr_fortran_unlock();
206 F77_FUNC(xdrfuchar
,XDRFUCHAR
)(int *xdrid
, unsigned char *ucp
, int *ret
)
209 *ret
= xdr_u_char(xdridptr
[*xdrid
], (u_char
*)ucp
);
211 xdr_fortran_unlock();
216 F77_FUNC(xdrfushort
,XDRFUSHORT
)(int *xdrid
, unsigned short *usp
, int *ret
)
219 *ret
= xdr_u_short(xdridptr
[*xdrid
], (unsigned short *)usp
);
220 cnt
+= sizeof(unsigned short);
221 xdr_fortran_unlock();
225 F77_FUNC(xdrf3dfcoord
,XDRF3DFCOORD
)(int *xdrid
, float *fp
, int *size
, float *precision
, int *ret
)
228 *ret
= xdr3dfcoord(xdridptr
[*xdrid
], fp
, size
, precision
);
229 xdr_fortran_unlock();
233 F77_FUNC(xdrfstring
,XDRFSTRING
)(int *xdrid
, char * sp_ptr
,
234 int *maxsize
, int *ret
, int sp_len
)
239 tsp
= (char*) malloc((size_t)(((sp_len
) + 1) * sizeof(char)));
244 if (ftocstr(tsp
, *maxsize
+1, sp_ptr
, sp_len
)) {
247 xdr_fortran_unlock();
250 *ret
= xdr_string(xdridptr
[*xdrid
], (char **) &tsp
, (unsigned int) *maxsize
);
251 ctofstr( sp_ptr
, sp_len
, tsp
);
254 xdr_fortran_unlock();
258 F77_FUNC(xdrfwrapstring
,XDRFWRAPSTRING
)(int *xdrid
, char *sp_ptr
,
259 int *ret
, int sp_len
)
265 maxsize
= (sp_len
) + 1;
266 tsp
= (char*) malloc((size_t)(maxsize
* sizeof(char)));
270 xdr_fortran_unlock();
272 if (ftocstr(tsp
, maxsize
, sp_ptr
, sp_len
)) {
276 xdr_fortran_unlock();
278 *ret
= xdr_string(xdridptr
[*xdrid
], (char **) &tsp
, (u_int
)maxsize
);
279 ctofstr( sp_ptr
, sp_len
, tsp
);
282 xdr_fortran_unlock();
286 F77_FUNC(xdrfopaque
,XDRFOPAQUE
)(int *xdrid
, caddr_t
*cp
, int *ccnt
, int *ret
)
289 *ret
= xdr_opaque(xdridptr
[*xdrid
], (caddr_t
)*cp
, (u_int
)*ccnt
);
291 xdr_fortran_unlock();
295 F77_FUNC(xdrfsetpos
,XDRFSETPOS
)(int *xdrid
, int *pos
, int *ret
)
298 *ret
= xdr_setpos(xdridptr
[*xdrid
], (u_int
) *pos
);
299 xdr_fortran_unlock();
304 F77_FUNC(xdrf
,XDRF
)(int *xdrid
, int *pos
)
307 *pos
= xdr_getpos(xdridptr
[*xdrid
]);
308 xdr_fortran_unlock();
312 F77_FUNC(xdrfvector
,XDRFVECTOR
)(int *xdrid
, char *cp
, int *size
, F77_FUNC(xdrfproc
,XDRFPROC
) elproc
, int *ret
)
317 for (lcnt
= 0; lcnt
< *size
; lcnt
++) {
318 elproc(xdrid
, (cp
+cnt
) , ret
);
320 xdr_fortran_unlock();
325 F77_FUNC(xdrfclose
,XDRFCLOSE
)(int *xdrid
, int *ret
)
328 *ret
= xdrclose(xdridptr
[*xdrid
]);
330 xdr_fortran_unlock();
334 F77_FUNC(xdrfopen
,XDRFOPEN
)(int *xdrid
, char *fp_ptr
, char *mode_ptr
,
335 int *ret
, int fp_len
, int mode_len
)
341 if (ftocstr(fname
, sizeof(fname
), fp_ptr
, fp_len
)) {
344 if (ftocstr(fmode
, sizeof(fmode
), mode_ptr
,
349 *xdrid
= xdropen(NULL
, fname
, fmode
);
354 xdr_fortran_unlock();
357 /*__________________________________________________________________________
359 | xdropen - open xdr file
361 | This versions differs from xdrstdio_create, because I need to know
362 | the state of the file (read or write) and the file descriptor
363 | so I can close the file (something xdr_destroy doesn't do).
365 | It assumes xdr_fortran_mutex is locked.
367 | NOTE: THIS FUNCTION IS NOW OBSOLETE AND ONLY PROVIDED FOR BACKWARD
368 | COMPATIBILITY OF 3D PARTY TOOLS. IT SHOULD NOT BE USED ANYWHERE
372 int xdropen(XDR
*xdrs
, const char *filename
, const char *type
) {
373 static int init_done
= 0;
380 if (!tMPI_Thread_mutex_trylock( &xdr_fortran_mutex
))
382 tMPI_Thread_mutex_unlock( &xdr_fortran_mutex
);
383 gmx_incons("xdropen called without locked mutex. NEVER call this function.");
387 if (init_done
== 0) {
388 for (xdrid
= 1; xdrid
< MAXID
; xdrid
++) {
389 xdridptr
[xdrid
] = NULL
;
394 while (xdrid
< MAXID
&& xdridptr
[xdrid
] != NULL
) {
397 if (xdrid
== MAXID
) {
400 if (*type
== 'w' || *type
== 'W')
402 xdrmodes
[xdrid
] = 'w';
403 strcpy(newtype
, "wb+");
406 else if (*type
== 'a' || *type
== 'A')
408 xdrmodes
[xdrid
] = 'a';
409 strcpy(newtype
, "ab+");
412 else if (gmx_strncasecmp(type
, "r+", 2) == 0)
414 xdrmodes
[xdrid
] = 'a';
415 strcpy(newtype
, "rb+");
420 xdrmodes
[xdrid
] = 'r';
421 strcpy(newtype
, "rb");
424 xdrfiles
[xdrid
] = fopen(filename
, newtype
);
426 if (xdrfiles
[xdrid
] == NULL
) {
431 /* next test isn't useful in the case of C language
432 * but is used for the Fortran interface
433 * (C users are expected to pass the address of an already allocated
437 xdridptr
[xdrid
] = (XDR
*) malloc((size_t)sizeof(XDR
));
438 xdrstdio_create(xdridptr
[xdrid
], xdrfiles
[xdrid
], lmode
);
440 xdridptr
[xdrid
] = xdrs
;
441 xdrstdio_create(xdrs
, xdrfiles
[xdrid
], lmode
);
445 /*_________________________________________________________________________
447 | xdrclose - close a xdr file
449 | This will flush the xdr buffers, and destroy the xdr stream.
450 | It also closes the associated file descriptor (this is *not*
451 | done by xdr_destroy).
453 | It assumes xdr_fortran_mutex is locked.
455 | NOTE: THIS FUNCTION IS NOW OBSOLETE AND ONLY PROVIDED FOR BACKWARD
456 | COMPATIBILITY OF 3D PARTY TOOLS. IT SHOULD NOT BE USED ANYWHERE
460 int xdrclose(XDR
*xdrs
) {
465 if (!tMPI_Thread_mutex_trylock( &xdr_fortran_mutex
))
467 tMPI_Thread_mutex_unlock( &xdr_fortran_mutex
);
468 gmx_incons("xdropen called without locked mutex. NEVER call this function");
473 fprintf(stderr
, "xdrclose: passed a NULL pointer\n");
476 for (xdrid
= 1; xdrid
< MAXID
&& rc
==0; xdrid
++) {
477 if (xdridptr
[xdrid
] == xdrs
) {
480 rc
= fclose(xdrfiles
[xdrid
]);
481 xdridptr
[xdrid
] = NULL
;
482 return !rc
; /* xdr routines return 0 when ok */
485 fprintf(stderr
, "xdrclose: no such open xdr file\n");
488 /* to make some compilers happy: */
492 #endif /* GMX_FORTRAN */
495 /*___________________________________________________________________________
497 | what follows are the C routine to read/write compressed coordinates together
498 | with some routines to assist in this task (those are marked
499 | static and cannot be called from user programs)
501 #define MAXABS INT_MAX-2
504 #define MIN(x,y) ((x) < (y) ? (x):(y))
507 #define MAX(x,y) ((x) > (y) ? (x):(y))
510 #define SQR(x) ((x)*(x))
512 static const int magicints
[] = {
513 0, 0, 0, 0, 0, 0, 0, 0, 0,
514 8, 10, 12, 16, 20, 25, 32, 40, 50, 64,
515 80, 101, 128, 161, 203, 256, 322, 406, 512, 645,
516 812, 1024, 1290, 1625, 2048, 2580, 3250, 4096, 5060, 6501,
517 8192, 10321, 13003, 16384, 20642, 26007, 32768, 41285, 52015, 65536,
518 82570, 104031, 131072, 165140, 208063, 262144, 330280, 416127, 524287, 660561,
519 832255, 1048576, 1321122, 1664510, 2097152, 2642245, 3329021, 4194304, 5284491, 6658042,
520 8388607, 10568983, 13316085, 16777216 };
523 /* note that magicints[FIRSTIDX-1] == 0 */
524 #define LASTIDX (sizeof(magicints) / sizeof(*magicints))
527 /*____________________________________________________________________________
529 | sendbits - encode num into buf using the specified number of bits
531 | This routines appends the value of num to the bits already present in
532 | the array buf. You need to give it the number of bits to use and you
533 | better make sure that this number of bits is enough to hold the value
534 | Also num must be positive.
538 static void sendbits(int buf
[], int num_of_bits
, int num
) {
540 unsigned int cnt
, lastbyte
;
542 unsigned char * cbuf
;
544 cbuf
= ((unsigned char *)buf
) + 3 * sizeof(*buf
);
545 cnt
= (unsigned int) buf
[0];
547 lastbyte
=(unsigned int) buf
[2];
548 while (num_of_bits
>= 8) {
549 lastbyte
= (lastbyte
<< 8) | ((num
>> (num_of_bits
-8)) /* & 0xff*/);
550 cbuf
[cnt
++] = lastbyte
>> lastbits
;
553 if (num_of_bits
> 0) {
554 lastbyte
= (lastbyte
<< num_of_bits
) | num
;
555 lastbits
+= num_of_bits
;
558 cbuf
[cnt
++] = lastbyte
>> lastbits
;
565 cbuf
[cnt
] = lastbyte
<< (8 - lastbits
);
569 /*_________________________________________________________________________
571 | sizeofint - calculate bitsize of an integer
573 | return the number of bits needed to store an integer with given max size
577 static int sizeofint(const int size
) {
581 while (size
>= num
&& num_of_bits
< 32) {
588 /*___________________________________________________________________________
590 | sizeofints - calculate 'bitsize' of compressed ints
592 | given the number of small unsigned integers and the maximum value
593 | return the number of bits needed to read or write them with the
594 | routines receiveints and sendints. You need this parameter when
595 | calling these routines. Note that for many calls I can use
596 | the variable 'smallidx' which is exactly the number of bits, and
597 | So I don't need to call 'sizeofints for those calls.
600 static int sizeofints( const int num_of_ints
, unsigned int sizes
[]) {
603 unsigned int num_of_bytes
, num_of_bits
, bytecnt
, tmp
;
607 for (i
=0; i
< num_of_ints
; i
++) {
609 for (bytecnt
= 0; bytecnt
< num_of_bytes
; bytecnt
++) {
610 tmp
= bytes
[bytecnt
] * sizes
[i
] + tmp
;
611 bytes
[bytecnt
] = tmp
& 0xff;
615 bytes
[bytecnt
++] = tmp
& 0xff;
618 num_of_bytes
= bytecnt
;
622 while (bytes
[num_of_bytes
] >= num
) {
626 return num_of_bits
+ num_of_bytes
* 8;
630 /*____________________________________________________________________________
632 | sendints - send a small set of small integers in compressed format
634 | this routine is used internally by xdr3dfcoord, to send a set of
635 | small integers to the buffer.
636 | Multiplication with fixed (specified maximum ) sizes is used to get
637 | to one big, multibyte integer. Allthough the routine could be
638 | modified to handle sizes bigger than 16777216, or more than just
639 | a few integers, this is not done, because the gain in compression
640 | isn't worth the effort. Note that overflowing the multiplication
641 | or the byte buffer (32 bytes) is unchecked and causes bad results.
645 static void sendints(int buf
[], const int num_of_ints
, const int num_of_bits
,
646 unsigned int sizes
[], unsigned int nums
[]) {
648 int i
, num_of_bytes
, bytecnt
;
649 unsigned int bytes
[32], tmp
;
654 bytes
[num_of_bytes
++] = tmp
& 0xff;
658 for (i
= 1; i
< num_of_ints
; i
++) {
659 if (nums
[i
] >= sizes
[i
]) {
660 fprintf(stderr
,"major breakdown in sendints num %u doesn't "
661 "match size %u\n", nums
[i
], sizes
[i
]);
664 /* use one step multiply */
666 for (bytecnt
= 0; bytecnt
< num_of_bytes
; bytecnt
++) {
667 tmp
= bytes
[bytecnt
] * sizes
[i
] + tmp
;
668 bytes
[bytecnt
] = tmp
& 0xff;
672 bytes
[bytecnt
++] = tmp
& 0xff;
675 num_of_bytes
= bytecnt
;
677 if (num_of_bits
>= num_of_bytes
* 8) {
678 for (i
= 0; i
< num_of_bytes
; i
++) {
679 sendbits(buf
, 8, bytes
[i
]);
681 sendbits(buf
, num_of_bits
- num_of_bytes
* 8, 0);
683 for (i
= 0; i
< num_of_bytes
-1; i
++) {
684 sendbits(buf
, 8, bytes
[i
]);
686 sendbits(buf
, num_of_bits
- (num_of_bytes
-1) * 8, bytes
[i
]);
691 /*___________________________________________________________________________
693 | receivebits - decode number from buf using specified number of bits
695 | extract the number of bits from the array buf and construct an integer
696 | from it. Return that value.
700 static int receivebits(int buf
[], int num_of_bits
) {
702 int cnt
, num
, lastbits
;
703 unsigned int lastbyte
;
704 unsigned char * cbuf
;
705 int mask
= (1 << num_of_bits
) -1;
707 cbuf
= ((unsigned char *)buf
) + 3 * sizeof(*buf
);
709 lastbits
= (unsigned int) buf
[1];
710 lastbyte
= (unsigned int) buf
[2];
713 while (num_of_bits
>= 8) {
714 lastbyte
= ( lastbyte
<< 8 ) | cbuf
[cnt
++];
715 num
|= (lastbyte
>> lastbits
) << (num_of_bits
- 8);
718 if (num_of_bits
> 0) {
719 if (lastbits
< num_of_bits
) {
721 lastbyte
= (lastbyte
<< 8) | cbuf
[cnt
++];
723 lastbits
-= num_of_bits
;
724 num
|= (lastbyte
>> lastbits
) & ((1 << num_of_bits
) -1);
733 /*____________________________________________________________________________
735 | receiveints - decode 'small' integers from the buf array
737 | this routine is the inverse from sendints() and decodes the small integers
738 | written to buf by calculating the remainder and doing divisions with
739 | the given sizes[]. You need to specify the total number of bits to be
740 | used from buf in num_of_bits.
744 static void receiveints(int buf
[], const int num_of_ints
, int num_of_bits
,
745 unsigned int sizes
[], int nums
[]) {
747 int i
, j
, num_of_bytes
, p
, num
;
749 bytes
[1] = bytes
[2] = bytes
[3] = 0;
751 while (num_of_bits
> 8) {
752 bytes
[num_of_bytes
++] = receivebits(buf
, 8);
755 if (num_of_bits
> 0) {
756 bytes
[num_of_bytes
++] = receivebits(buf
, num_of_bits
);
758 for (i
= num_of_ints
-1; i
> 0; i
--) {
760 for (j
= num_of_bytes
-1; j
>=0; j
--) {
761 num
= (num
<< 8) | bytes
[j
];
764 num
= num
- p
* sizes
[i
];
768 nums
[0] = bytes
[0] | (bytes
[1] << 8) | (bytes
[2] << 16) | (bytes
[3] << 24);
771 /*____________________________________________________________________________
773 | xdr3dfcoord - read or write compressed 3d coordinates to xdr file.
775 | this routine reads or writes (depending on how you opened the file with
776 | xdropen() ) a large number of 3d coordinates (stored in *fp).
777 | The number of coordinates triplets to write is given by *size. On
778 | read this number may be zero, in which case it reads as many as were written
779 | or it may specify the number if triplets to read (which should match the
781 | Compression is achieved by first converting all floating numbers to integer
782 | using multiplication by *precision and rounding to the nearest integer.
783 | Then the minimum and maximum value are calculated to determine the range.
784 | The limited range of integers so found, is used to compress the coordinates.
785 | In addition the differences between succesive coordinates is calculated.
786 | If the difference happens to be 'small' then only the difference is saved,
787 | compressing the data even more. The notion of 'small' is changed dynamically
788 | and is enlarged or reduced whenever needed or possible.
789 | Extra compression is achieved in the case of GROMOS and coordinates of
790 | water molecules. GROMOS first writes out the Oxygen position, followed by
791 | the two hydrogens. In order to make the differences smaller (and thereby
792 | compression the data better) the order is changed into first one hydrogen
793 | then the oxygen, followed by the other hydrogen. This is rather special, but
794 | it shouldn't harm in the general case.
798 int xdr3dfcoord(XDR
*xdrs
, float *fp
, int *size
, float *precision
)
804 /* preallocate a small buffer and ip on the stack - if we need more
805 we can always malloc(). This is faster for small values of size: */
806 int prealloc_size
=3*16;
807 int prealloc_ip
[3*16], prealloc_buf
[3*20];
808 int we_should_free
=0;
810 int minint
[3], maxint
[3], mindiff
, *lip
, diff
;
811 int lint1
, lint2
, lint3
, oldlint1
, oldlint2
, oldlint3
, smallidx
;
813 unsigned sizeint
[3], sizesmall
[3], bitsizeint
[3], size3
, *luip
;
815 int smallnum
, smaller
, larger
, i
, is_small
, is_smaller
, run
, prevrun
;
817 int tmp
, *thiscoord
, prevcoord
[3];
818 unsigned int tmpcoord
[30];
820 int bufsize
, xdrid
, lsize
;
821 unsigned int bitsize
;
826 bRead
= (xdrs
->x_op
== XDR_DECODE
);
827 bitsizeint
[0] = bitsizeint
[1] = bitsizeint
[2] = 0;
828 prevcoord
[0] = prevcoord
[1] = prevcoord
[2] = 0;
832 /* xdrs is open for writing */
834 if (xdr_int(xdrs
, size
) == 0)
837 /* when the number of coordinates is small, don't try to compress; just
838 * write them as floats using xdr_vector
841 return (xdr_vector(xdrs
, (char *) fp
, (unsigned int)size3
,
842 (unsigned int)sizeof(*fp
), (xdrproc_t
)xdr_float
));
845 if(xdr_float(xdrs
, precision
) == 0)
848 if (size3
<= prealloc_size
)
856 bufsize
= size3
* 1.2;
857 ip
= (int *)malloc((size_t)(size3
* sizeof(*ip
)));
858 buf
= (int *)malloc((size_t)(bufsize
* sizeof(*buf
)));
859 if (ip
== NULL
|| buf
==NULL
)
861 fprintf(stderr
,"malloc failed\n");
865 /* buf[0-2] are special and do not contain actual data */
866 buf
[0] = buf
[1] = buf
[2] = 0;
867 minint
[0] = minint
[1] = minint
[2] = INT_MAX
;
868 maxint
[0] = maxint
[1] = maxint
[2] = INT_MIN
;
873 oldlint1
= oldlint2
= oldlint3
= 0;
874 while(lfp
< fp
+ size3
) {
875 /* find nearest integer */
877 lf
= *lfp
* *precision
+ 0.5;
879 lf
= *lfp
* *precision
- 0.5;
880 if (fabs(lf
) > MAXABS
) {
881 /* scaling would cause overflow */
885 if (lint1
< minint
[0]) minint
[0] = lint1
;
886 if (lint1
> maxint
[0]) maxint
[0] = lint1
;
890 lf
= *lfp
* *precision
+ 0.5;
892 lf
= *lfp
* *precision
- 0.5;
893 if (fabs(lf
) > MAXABS
) {
894 /* scaling would cause overflow */
898 if (lint2
< minint
[1]) minint
[1] = lint2
;
899 if (lint2
> maxint
[1]) maxint
[1] = lint2
;
903 lf
= *lfp
* *precision
+ 0.5;
905 lf
= *lfp
* *precision
- 0.5;
906 if (fabs(lf
) > MAXABS
) {
907 /* scaling would cause overflow */
911 if (lint3
< minint
[2]) minint
[2] = lint3
;
912 if (lint3
> maxint
[2]) maxint
[2] = lint3
;
915 diff
= abs(oldlint1
-lint1
)+abs(oldlint2
-lint2
)+abs(oldlint3
-lint3
);
916 if (diff
< mindiff
&& lfp
> fp
+ 3)
922 if ( (xdr_int(xdrs
, &(minint
[0])) == 0) ||
923 (xdr_int(xdrs
, &(minint
[1])) == 0) ||
924 (xdr_int(xdrs
, &(minint
[2])) == 0) ||
925 (xdr_int(xdrs
, &(maxint
[0])) == 0) ||
926 (xdr_int(xdrs
, &(maxint
[1])) == 0) ||
927 (xdr_int(xdrs
, &(maxint
[2])) == 0))
937 if ((float)maxint
[0] - (float)minint
[0] >= MAXABS
||
938 (float)maxint
[1] - (float)minint
[1] >= MAXABS
||
939 (float)maxint
[2] - (float)minint
[2] >= MAXABS
) {
940 /* turning value in unsigned by subtracting minint
941 * would cause overflow
945 sizeint
[0] = maxint
[0] - minint
[0]+1;
946 sizeint
[1] = maxint
[1] - minint
[1]+1;
947 sizeint
[2] = maxint
[2] - minint
[2]+1;
949 /* check if one of the sizes is to big to be multiplied */
950 if ((sizeint
[0] | sizeint
[1] | sizeint
[2] ) > 0xffffff) {
951 bitsizeint
[0] = sizeofint(sizeint
[0]);
952 bitsizeint
[1] = sizeofint(sizeint
[1]);
953 bitsizeint
[2] = sizeofint(sizeint
[2]);
954 bitsize
= 0; /* flag the use of large sizes */
956 bitsize
= sizeofints(3, sizeint
);
959 luip
= (unsigned int *) ip
;
961 while (smallidx
< LASTIDX
&& magicints
[smallidx
] < mindiff
) {
964 if(xdr_int(xdrs
, &smallidx
) == 0)
974 maxidx
= MIN(LASTIDX
, smallidx
+ 8) ;
975 minidx
= maxidx
- 8; /* often this equal smallidx */
976 smaller
= magicints
[MAX(FIRSTIDX
, smallidx
-1)] / 2;
977 smallnum
= magicints
[smallidx
] / 2;
978 sizesmall
[0] = sizesmall
[1] = sizesmall
[2] = magicints
[smallidx
];
979 larger
= magicints
[maxidx
] / 2;
983 thiscoord
= (int *)(luip
) + i
* 3;
984 if (smallidx
< maxidx
&& i
>= 1 &&
985 abs(thiscoord
[0] - prevcoord
[0]) < larger
&&
986 abs(thiscoord
[1] - prevcoord
[1]) < larger
&&
987 abs(thiscoord
[2] - prevcoord
[2]) < larger
) {
989 } else if (smallidx
> minidx
) {
995 if (abs(thiscoord
[0] - thiscoord
[3]) < smallnum
&&
996 abs(thiscoord
[1] - thiscoord
[4]) < smallnum
&&
997 abs(thiscoord
[2] - thiscoord
[5]) < smallnum
) {
998 /* interchange first with second atom for better
999 * compression of water molecules
1001 tmp
= thiscoord
[0]; thiscoord
[0] = thiscoord
[3];
1003 tmp
= thiscoord
[1]; thiscoord
[1] = thiscoord
[4];
1005 tmp
= thiscoord
[2]; thiscoord
[2] = thiscoord
[5];
1011 tmpcoord
[0] = thiscoord
[0] - minint
[0];
1012 tmpcoord
[1] = thiscoord
[1] - minint
[1];
1013 tmpcoord
[2] = thiscoord
[2] - minint
[2];
1015 sendbits(buf
, bitsizeint
[0], tmpcoord
[0]);
1016 sendbits(buf
, bitsizeint
[1], tmpcoord
[1]);
1017 sendbits(buf
, bitsizeint
[2], tmpcoord
[2]);
1019 sendints(buf
, 3, bitsize
, sizeint
, tmpcoord
);
1021 prevcoord
[0] = thiscoord
[0];
1022 prevcoord
[1] = thiscoord
[1];
1023 prevcoord
[2] = thiscoord
[2];
1024 thiscoord
= thiscoord
+ 3;
1028 if (is_small
== 0 && is_smaller
== -1)
1030 while (is_small
&& run
< 8*3) {
1031 if (is_smaller
== -1 && (
1032 SQR(thiscoord
[0] - prevcoord
[0]) +
1033 SQR(thiscoord
[1] - prevcoord
[1]) +
1034 SQR(thiscoord
[2] - prevcoord
[2]) >= smaller
* smaller
)) {
1038 tmpcoord
[run
++] = thiscoord
[0] - prevcoord
[0] + smallnum
;
1039 tmpcoord
[run
++] = thiscoord
[1] - prevcoord
[1] + smallnum
;
1040 tmpcoord
[run
++] = thiscoord
[2] - prevcoord
[2] + smallnum
;
1042 prevcoord
[0] = thiscoord
[0];
1043 prevcoord
[1] = thiscoord
[1];
1044 prevcoord
[2] = thiscoord
[2];
1047 thiscoord
= thiscoord
+ 3;
1050 abs(thiscoord
[0] - prevcoord
[0]) < smallnum
&&
1051 abs(thiscoord
[1] - prevcoord
[1]) < smallnum
&&
1052 abs(thiscoord
[2] - prevcoord
[2]) < smallnum
) {
1056 if (run
!= prevrun
|| is_smaller
!= 0) {
1058 sendbits(buf
, 1, 1); /* flag the change in run-length */
1059 sendbits(buf
, 5, run
+is_smaller
+1);
1061 sendbits(buf
, 1, 0); /* flag the fact that runlength did not change */
1063 for (k
=0; k
< run
; k
+=3) {
1064 sendints(buf
, 3, smallidx
, sizesmall
, &tmpcoord
[k
]);
1066 if (is_smaller
!= 0) {
1067 smallidx
+= is_smaller
;
1068 if (is_smaller
< 0) {
1070 smaller
= magicints
[smallidx
-1] / 2;
1073 smallnum
= magicints
[smallidx
] / 2;
1075 sizesmall
[0] = sizesmall
[1] = sizesmall
[2] = magicints
[smallidx
];
1078 if (buf
[1] != 0) buf
[0]++;
1079 /* buf[0] holds the length in bytes */
1080 if(xdr_int(xdrs
, &(buf
[0])) == 0)
1091 rc
=errval
* (xdr_opaque(xdrs
, (char *)&(buf
[3]), (unsigned int)buf
[0]));
1101 /* xdrs is open for reading */
1103 if (xdr_int(xdrs
, &lsize
) == 0)
1105 if (*size
!= 0 && lsize
!= *size
) {
1106 fprintf(stderr
, "wrong number of coordinates in xdr3dfcoord; "
1107 "%d arg vs %d in file", *size
, lsize
);
1113 return (xdr_vector(xdrs
, (char *) fp
, (unsigned int)size3
,
1114 (unsigned int)sizeof(*fp
), (xdrproc_t
)xdr_float
));
1116 if(xdr_float(xdrs
, precision
) == 0)
1119 if (size3
<= prealloc_size
)
1127 bufsize
= size3
* 1.2;
1128 ip
= (int *)malloc((size_t)(size3
* sizeof(*ip
)));
1129 buf
= (int *)malloc((size_t)(bufsize
* sizeof(*buf
)));
1130 if (ip
== NULL
|| buf
==NULL
)
1132 fprintf(stderr
,"malloc failed\n");
1137 buf
[0] = buf
[1] = buf
[2] = 0;
1139 if ( (xdr_int(xdrs
, &(minint
[0])) == 0) ||
1140 (xdr_int(xdrs
, &(minint
[1])) == 0) ||
1141 (xdr_int(xdrs
, &(minint
[2])) == 0) ||
1142 (xdr_int(xdrs
, &(maxint
[0])) == 0) ||
1143 (xdr_int(xdrs
, &(maxint
[1])) == 0) ||
1144 (xdr_int(xdrs
, &(maxint
[2])) == 0))
1154 sizeint
[0] = maxint
[0] - minint
[0]+1;
1155 sizeint
[1] = maxint
[1] - minint
[1]+1;
1156 sizeint
[2] = maxint
[2] - minint
[2]+1;
1158 /* check if one of the sizes is to big to be multiplied */
1159 if ((sizeint
[0] | sizeint
[1] | sizeint
[2] ) > 0xffffff) {
1160 bitsizeint
[0] = sizeofint(sizeint
[0]);
1161 bitsizeint
[1] = sizeofint(sizeint
[1]);
1162 bitsizeint
[2] = sizeofint(sizeint
[2]);
1163 bitsize
= 0; /* flag the use of large sizes */
1165 bitsize
= sizeofints(3, sizeint
);
1168 if (xdr_int(xdrs
, &smallidx
) == 0)
1178 maxidx
= MIN(LASTIDX
, smallidx
+ 8) ;
1179 minidx
= maxidx
- 8; /* often this equal smallidx */
1180 smaller
= magicints
[MAX(FIRSTIDX
, smallidx
-1)] / 2;
1181 smallnum
= magicints
[smallidx
] / 2;
1182 sizesmall
[0] = sizesmall
[1] = sizesmall
[2] = magicints
[smallidx
] ;
1183 larger
= magicints
[maxidx
];
1185 /* buf[0] holds the length in bytes */
1187 if (xdr_int(xdrs
, &(buf
[0])) == 0)
1198 if (xdr_opaque(xdrs
, (char *)&(buf
[3]), (unsigned int)buf
[0]) == 0)
1210 buf
[0] = buf
[1] = buf
[2] = 0;
1213 inv_precision
= 1.0 / * precision
;
1217 while ( i
< lsize
) {
1218 thiscoord
= (int *)(lip
) + i
* 3;
1221 thiscoord
[0] = receivebits(buf
, bitsizeint
[0]);
1222 thiscoord
[1] = receivebits(buf
, bitsizeint
[1]);
1223 thiscoord
[2] = receivebits(buf
, bitsizeint
[2]);
1225 receiveints(buf
, 3, bitsize
, sizeint
, thiscoord
);
1229 thiscoord
[0] += minint
[0];
1230 thiscoord
[1] += minint
[1];
1231 thiscoord
[2] += minint
[2];
1233 prevcoord
[0] = thiscoord
[0];
1234 prevcoord
[1] = thiscoord
[1];
1235 prevcoord
[2] = thiscoord
[2];
1238 flag
= receivebits(buf
, 1);
1241 run
= receivebits(buf
, 5);
1242 is_smaller
= run
% 3;
1248 for (k
= 0; k
< run
; k
+=3) {
1249 receiveints(buf
, 3, smallidx
, sizesmall
, thiscoord
);
1251 thiscoord
[0] += prevcoord
[0] - smallnum
;
1252 thiscoord
[1] += prevcoord
[1] - smallnum
;
1253 thiscoord
[2] += prevcoord
[2] - smallnum
;
1255 /* interchange first with second atom for better
1256 * compression of water molecules
1258 tmp
= thiscoord
[0]; thiscoord
[0] = prevcoord
[0];
1260 tmp
= thiscoord
[1]; thiscoord
[1] = prevcoord
[1];
1262 tmp
= thiscoord
[2]; thiscoord
[2] = prevcoord
[2];
1264 *lfp
++ = prevcoord
[0] * inv_precision
;
1265 *lfp
++ = prevcoord
[1] * inv_precision
;
1266 *lfp
++ = prevcoord
[2] * inv_precision
;
1268 prevcoord
[0] = thiscoord
[0];
1269 prevcoord
[1] = thiscoord
[1];
1270 prevcoord
[2] = thiscoord
[2];
1272 *lfp
++ = thiscoord
[0] * inv_precision
;
1273 *lfp
++ = thiscoord
[1] * inv_precision
;
1274 *lfp
++ = thiscoord
[2] * inv_precision
;
1277 *lfp
++ = thiscoord
[0] * inv_precision
;
1278 *lfp
++ = thiscoord
[1] * inv_precision
;
1279 *lfp
++ = thiscoord
[2] * inv_precision
;
1281 smallidx
+= is_smaller
;
1282 if (is_smaller
< 0) {
1284 if (smallidx
> FIRSTIDX
) {
1285 smaller
= magicints
[smallidx
- 1] /2;
1289 } else if (is_smaller
> 0) {
1291 smallnum
= magicints
[smallidx
] / 2;
1293 sizesmall
[0] = sizesmall
[1] = sizesmall
[2] = magicints
[smallidx
] ;
1306 /******************************************************************
1308 XTC files have a relatively simple structure.
1309 They have a header of 16 bytes and the rest are
1310 the compressed coordinates of the files. Due to the
1311 compression 00 is not present in the coordinates.
1312 The first 4 bytes of the header are the magic number
1313 1995 (0x000007CB). If we find this number we are guaranteed
1314 to be in the header, due to the presence of so many zeros.
1315 The second 4 bytes are the number of atoms in the frame, and is
1316 assumed to be constant. The third 4 bytes are the frame number.
1317 The last 4 bytes are a floating point representation of the time.
1319 ********************************************************************/
1321 /* Must match definition in xtcio.c */
1323 #define XTC_MAGIC 1995
1326 static const int header_size
= 16;
1328 /* Check if we are at the header start.
1329 At the same time it will also read 1 int */
1330 static int xtc_at_header_start(FILE *fp
, XDR
*xdrs
,
1331 int natoms
, int * timestep
, float * time
){
1338 if((off
= gmx_ftell(fp
)) < 0){
1341 /* read magic natoms and timestep */
1343 if(!xdr_int(xdrs
, &(i_inp
[i
]))){
1344 gmx_fseek(fp
,off
+XDR_INT_SIZE
,SEEK_SET
);
1349 if(i_inp
[0] != XTC_MAGIC
){
1350 if(gmx_fseek(fp
,off
+XDR_INT_SIZE
,SEEK_SET
)){
1355 /* read time and box */
1356 for(i
= 0;i
<10;i
++){
1357 if(!xdr_float(xdrs
, &(f_inp
[i
]))){
1358 gmx_fseek(fp
,off
+XDR_INT_SIZE
,SEEK_SET
);
1362 /* Make a rigourous check to see if we are in the beggining of a header
1363 Hopefully there are no ambiguous cases */
1364 /* This check makes use of the fact that the box matrix has 3 zeroes on the upper
1365 right triangle and that the first element must be nonzero unless the entire matrix is zero
1367 if(i_inp
[1] == natoms
&&
1368 ((f_inp
[1] != 0 && f_inp
[6] == 0) ||
1369 (f_inp
[1] == 0 && f_inp
[5] == 0 && f_inp
[9] == 0))){
1370 if(gmx_fseek(fp
,off
+XDR_INT_SIZE
,SEEK_SET
)){
1374 *timestep
= i_inp
[2];
1377 if(gmx_fseek(fp
,off
+XDR_INT_SIZE
,SEEK_SET
)){
1384 xtc_get_next_frame_number(FILE *fp
, XDR
*xdrs
, int natoms
)
1391 if((off
= gmx_ftell(fp
)) < 0){
1395 /* read one int just to make sure we dont read this frame but the next */
1396 xdr_int(xdrs
,&step
);
1398 ret
= xtc_at_header_start(fp
,xdrs
,natoms
,&step
,&time
);
1400 if(gmx_fseek(fp
,off
,SEEK_SET
)){
1404 }else if(ret
== -1){
1405 if(gmx_fseek(fp
,off
,SEEK_SET
)){
1414 static float xtc_get_next_frame_time(FILE *fp
, XDR
*xdrs
, int natoms
,
1423 if ((off
= gmx_ftell(fp
)) < 0)
1427 /* read one int just to make sure we dont read this frame but the next */
1428 xdr_int(xdrs
, &step
);
1431 ret
= xtc_at_header_start(fp
, xdrs
, natoms
, &step
, &time
);
1435 if (gmx_fseek(fp
,off
,SEEK_SET
))
1444 if (gmx_fseek(fp
,off
,SEEK_SET
))
1456 xtc_get_current_frame_time(FILE *fp
, XDR
*xdrs
, int natoms
, gmx_bool
* bOK
)
1464 if ((off
= gmx_ftell(fp
)) < 0)
1471 ret
= xtc_at_header_start(fp
, xdrs
, natoms
, &step
, &time
);
1475 if (gmx_fseek(fp
,off
,SEEK_SET
))
1484 if (gmx_fseek(fp
,off
,SEEK_SET
))
1493 if (gmx_fseek(fp
,-2*XDR_INT_SIZE
,SEEK_CUR
))
1502 /* Currently not used, just for completeness */
1504 xtc_get_current_frame_number(FILE *fp
,XDR
*xdrs
,int natoms
, gmx_bool
* bOK
)
1512 if((off
= gmx_ftell(fp
)) < 0){
1518 ret
= xtc_at_header_start(fp
,xdrs
,natoms
,&step
,&time
);
1521 if(gmx_fseek(fp
,off
,SEEK_SET
)){
1526 }else if(ret
== -1){
1527 if(gmx_fseek(fp
,off
,SEEK_SET
)){
1534 if(gmx_fseek(fp
,-2*XDR_INT_SIZE
,SEEK_CUR
)){
1544 static gmx_off_t
xtc_get_next_frame_start(FILE *fp
, XDR
*xdrs
, int natoms
)
1551 /* read one int just to make sure we dont read this frame but the next */
1552 xdr_int(xdrs
,&step
);
1555 ret
= xtc_at_header_start(fp
,xdrs
,natoms
,&step
,&time
);
1557 if((res
= gmx_ftell(fp
)) >= 0){
1558 return res
- XDR_INT_SIZE
;
1562 }else if(ret
== -1){
1572 xdr_xtc_estimate_dt(FILE *fp
, XDR
*xdrs
, int natoms
, gmx_bool
* bOK
)
1578 if((off
= gmx_ftell(fp
)) < 0){
1582 tinit
= xtc_get_current_frame_time(fp
,xdrs
,natoms
,bOK
);
1591 res
= xtc_get_next_frame_time(fp
,xdrs
,natoms
,bOK
);
1599 if(gmx_fseek(fp
,off
,SEEK_SET
)){
1608 xdr_xtc_seek_frame(int frame
, FILE *fp
, XDR
*xdrs
, int natoms
)
1614 /* round to 4 bytes */
1617 if(gmx_fseek(fp
,0,SEEK_END
)){
1621 if((high
= gmx_ftell(fp
)) < 0){
1625 /* round to 4 bytes */
1626 high
/= XDR_INT_SIZE
;
1627 high
*= XDR_INT_SIZE
;
1628 offset
= ((high
/2)/XDR_INT_SIZE
)*XDR_INT_SIZE
;
1630 if(gmx_fseek(fp
,offset
,SEEK_SET
)){
1636 fr
= xtc_get_next_frame_number(fp
,xdrs
,natoms
);
1641 if(fr
!= frame
&& abs(low
-high
) > header_size
)
1651 /* round to 4 bytes */
1652 offset
= (((high
+low
)/2)/4)*4;
1654 if(gmx_fseek(fp
,offset
,SEEK_SET
)){
1663 if(offset
<= header_size
)
1668 if(gmx_fseek(fp
,offset
,SEEK_SET
)){
1672 if((pos
= xtc_get_next_frame_start(fp
,xdrs
,natoms
))< 0){
1673 /* we probably hit an end of file */
1677 if(gmx_fseek(fp
,pos
,SEEK_SET
)){
1686 int xdr_xtc_seek_time(real time
, FILE *fp
, XDR
*xdrs
, int natoms
)
1692 gmx_off_t high
, offset
, pos
;
1696 if (gmx_fseek(fp
,0,SEEK_END
))
1701 if ((high
= gmx_ftell(fp
)) < 0)
1706 high
/= XDR_INT_SIZE
;
1707 high
*= XDR_INT_SIZE
;
1708 offset
= ((high
/ 2) / XDR_INT_SIZE
) * XDR_INT_SIZE
;
1710 if (gmx_fseek(fp
,offset
,SEEK_SET
))
1717 * No need to call xdr_xtc_estimate_dt here - since xdr_xtc_estimate_dt is called first thing in the loop
1718 dt = xdr_xtc_estimate_dt(fp, xdrs, natoms, &bOK);
1728 dt
= xdr_xtc_estimate_dt(fp
, xdrs
, natoms
, &bOK
);
1739 /* Found a place in the trajectory that has positive time step while
1740 other has negative time step */
1749 /* Found a place in the trajectory that has positive time step while
1750 other has negative time step */
1756 t
= xtc_get_next_frame_time(fp
, xdrs
, natoms
, &bOK
);
1762 /* If we are before the target time and the time step is positive or 0, or we have
1763 after the target time and the time step is negative, or the difference between
1764 the current time and the target time is bigger than dt and above all the distance between high
1765 and low is bigger than 1 frame, then do another step of binary search. Otherwise stop and check
1766 if we reached the solution */
1767 if ((((t
< time
&& dt_sign
>= 0) || (t
> time
&& dt_sign
== -1)) || ((t
1768 - time
) >= dt
&& dt_sign
>= 0)
1769 || ((time
- t
) >= -dt
&& dt_sign
< 0)) && (abs(low
- high
)
1772 if (dt
>= 0 && dt_sign
!= -1)
1783 else if (dt
<= 0 && dt_sign
== -1)
1796 /* We should never reach here */
1799 /* round to 4 bytes and subtract header*/
1800 offset
= (((high
+ low
) / 2) / XDR_INT_SIZE
) * XDR_INT_SIZE
;
1801 if (gmx_fseek(fp
,offset
,SEEK_SET
))
1808 if (abs(low
- high
) <= header_size
)
1812 /* re-estimate dt */
1813 if (xdr_xtc_estimate_dt(fp
, xdrs
, natoms
, &bOK
) != dt
)
1817 dt
= xdr_xtc_estimate_dt(fp
, xdrs
, natoms
, &bOK
);
1820 if (t
>= time
&& t
- time
< dt
)
1827 if (offset
<= header_size
)
1832 gmx_fseek(fp
,offset
,SEEK_SET
);
1834 if ((pos
= xtc_get_next_frame_start(fp
, xdrs
, natoms
)) < 0)
1839 if (gmx_fseek(fp
,pos
,SEEK_SET
))
1847 xdr_xtc_get_last_frame_time(FILE *fp
, XDR
*xdrs
, int natoms
, gmx_bool
* bOK
)
1853 off
= gmx_ftell(fp
);
1859 if( (res
= gmx_fseek(fp
,-3*XDR_INT_SIZE
,SEEK_END
)) != 0){
1864 time
= xtc_get_current_frame_time(fp
, xdrs
, natoms
, bOK
);
1869 if( (res
= gmx_fseek(fp
,off
,SEEK_SET
)) != 0){
1878 xdr_xtc_get_last_frame_number(FILE *fp
, XDR
*xdrs
, int natoms
, gmx_bool
* bOK
)
1885 if((off
= gmx_ftell(fp
)) < 0){
1891 if(gmx_fseek(fp
,-3*XDR_INT_SIZE
,SEEK_END
)){
1896 frame
= xtc_get_current_frame_number(fp
, xdrs
, natoms
, bOK
);
1902 if(gmx_fseek(fp
,off
,SEEK_SET
)){