# Description of Gromacs format

**URL:** <https://gromacs.bioexcel.eu/t/description-of-gromacs-format/6888>\
**Category:** Developers discussions\
**Created:** [July 26, 2023, 9:13am UTC](https://gromacs.bioexcel.eu/t/description-of-gromacs-format/6888 "2023-07-26T09:13:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Linkey](https://dub1.discourse-cdn.com/flex017/user_avatar/gromacs.bioexcel.eu/linkey/32/2313_2.png) [@Linkey](https://gromacs.bioexcel.eu/u/Linkey)\
**Post date:** [July 26, 2023, 9:13am UTC](https://gromacs.bioexcel.eu/t/description-of-gromacs-format/6888/1 "2023-07-26T09:13:15Z")

</div>

Hello,  
I was uncertain at which category should I post this message, maybe you need to move it . I am the author of Chemcraft program (visualization tool). Currently I try to implement visualization of .xtc files in Chemcraft. Firstly I was able to read the magic number and the atoms count, but after that I was confused: it is difficult to understand the format, it seems that Gromacs uses some packing since the size of each trajectory step in bytes is not constant. Can you please help? I program in Delphi, so I am unable to include the xtcio.h library in my code. I need to know the algorithm of data encription in Gromacs.

---

<div class="post-metadata">

**Author:** ![al42and](https://dub1.discourse-cdn.com/flex017/user_avatar/gromacs.bioexcel.eu/al42and/32/1393_2.png) [@al42and](https://gromacs.bioexcel.eu/u/al42and)\
**Post date:** [August 11, 2023, 4:06pm UTC](https://gromacs.bioexcel.eu/t/description-of-gromacs-format/6888/2 "2023-08-11T16:06:47Z")

</div>

Hi!

The brief description of the XTC format can be found here: [https://manual.gromacs.org/current/reference-manual/file-formats.html#xtc](https://manual.gromacs.org/current/reference-manual/file-formats.html#xtc)

Your findings are correct, XTC uses compression scheme based on XDR library to save space, so the size of each frame is different.

I am not aware of any existing Delphi XTC/XDR readers. You will likely have to write your own, based on our C++ code:

- [src/gromacs/fileio/xtcio.cpp · 97340bd599169e6206f3713e7c8136c92f7d9c88 · GROMACS / GROMACS · GitLab](https://gitlab.com/gromacs/gromacs/-/blob/97340bd599169e6206f3713e7c8136c92f7d9c88/src/gromacs/fileio/xtcio.cpp#L464-464)
- [src/gromacs/fileio/gmx\_internal\_xdr.cpp · 97340bd599169e6206f3713e7c8136c92f7d9c88 · GROMACS / GROMACS · GitLab](https://gitlab.com/gromacs/gromacs/-/blob/97340bd599169e6206f3713e7c8136c92f7d9c88/src/gromacs/fileio/gmx_internal_xdr.cpp#L464-464)

or the Python implementation from MDAnalysis:

- [https://github.com/MDAnalysis/mdanalysis/blob/1eca65591fa402584dd29b6d1a02111af30e68eb/package/MDAnalysis/coordinates/XTC.py](https://github.com/MDAnalysis/mdanalysis/blob/1eca65591fa402584dd29b6d1a02111af30e68eb/package/MDAnalysis/coordinates/XTC.py)
- [https://github.com/MDAnalysis/mdanalysis/blob/1eca65591fa402584dd29b6d1a02111af30e68eb/package/MDAnalysis/coordinates/XDR.py](https://github.com/MDAnalysis/mdanalysis/blob/1eca65591fa402584dd29b6d1a02111af30e68eb/package/MDAnalysis/coordinates/XDR.py)

---

<div class="post-metadata">

**Author:** ![ericirrgang](https://dub1.discourse-cdn.com/flex017/user_avatar/gromacs.bioexcel.eu/ericirrgang/32/438_2.png) [@ericirrgang](https://gromacs.bioexcel.eu/u/ericirrgang)\
**Post date:** [August 11, 2023, 5:05pm UTC](https://gromacs.bioexcel.eu/t/description-of-gromacs-format/6888/3 "2023-08-11T17:05:46Z")

</div>

Note that XDR is a [fairly simple standard](https://en.wikipedia.org/wiki/External_Data_Representation) supported in various environments. But XDR isn’t really “self-describing” of a particular data layout, so you have to refer to the XTC source code to understand the schema in use.

---

<div class="post-metadata">

**Author:** ![Linkey](https://dub1.discourse-cdn.com/flex017/user_avatar/gromacs.bioexcel.eu/linkey/32/2313_2.png) [@Linkey](https://gromacs.bioexcel.eu/u/Linkey)\
**Post date:** [September 16, 2023, 9:34am UTC](https://gromacs.bioexcel.eu/t/description-of-gromacs-format/6888/4 "2023-09-16T09:34:54Z")

</div>

I have found a file libxdrf.c and try analyze the code in it. It contains some encoding functions; in particular, two functions are used to determine the number of bits needed to describe some digits of specified maximum size: sizeofint and sizeofints:

```auto
static int sizeofint(const int size) {
    unsigned int num = 1;
    int num_of_bits = 0;
    
    while (size >= num && num_of_bits < 32) {
	num_of_bits++;
	num <<= 1;
    }
    return num_of_bits;
}

```

```auto
static int sizeofints( const int num_of_ints, unsigned int sizes[]) {
    int i, num;
    unsigned int num_of_bytes, num_of_bits, bytes[32], bytecnt, tmp;
    num_of_bytes = 1;
    bytes[0] = 1;
    num_of_bits = 0;
    for (i=0; i < num_of_ints; i++) {	
	tmp = 0;
	for (bytecnt = 0; bytecnt < num_of_bytes; bytecnt++) {
	    tmp = bytes[bytecnt] * sizes[i] + tmp;
	    bytes[bytecnt] = tmp & 0xff;
	    tmp >>= 8;
	}
	while (tmp != 0) {
	    bytes[bytecnt++] = tmp & 0xff;
	    tmp >>= 8;
	}
	num_of_bytes = bytecnt;
    }
    num = 1;
    num_of_bytes--;
    while (bytes[num_of_bytes] >= num) {
	num_of_bits++;
	num *= 2;
    }
    return num_of_bits + num_of_bytes * 8;

}

```

The first is simple – e.g. we need 2 bits to describe a digit from 0 to 2, 4 bits to describe a digit from 0 to 9, etc. The second function is more difficult to understand. When reading xtc files, usually the second function is used?
