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 * Green Red Orange Magenta Azure Cyan Skyblue
39 /* This module provides routines to read/write sparse or full storage
40 * matrices from/to files. It is normally used for the Hessian matrix
41 * in normal mode analysis.
48 #include "gmx_fatal.h"
52 /* Just a number to identify our file type */
53 #define GMX_MTXIO_MAGIC_NUMBER 0x34ce8fd2
55 #define GMX_MTXIO_FULL_MATRIX 0
56 #define GMX_MTXIO_SPARSE_MATRIX 1
60 /* Matrix file format definition:
62 * All entries are stored in XDR format.
64 * 1. Magic number integer, should be GMX_MTXIO_MAGIC_NUMBER
65 * 2. An XDR string specifying the Gromacs version used to generate the file.
66 * 3. Integer to denote precision. 1 if double, 0 if single precision.
67 * 4. Two integers specifying number of rows and columns.
68 * 5. Integer to denote storage type:
69 * GMX_MTXIO_FULL_MATRIX or GMX_MTXIO_SPARSE_MATRIX
72 * a) In case of full matrix, this is nrow*ncol floating-point values.
73 * b) In case of sparse matrix the data is:
74 * - Integer specifying compressed_symmetric format (1=yes, 0=no)
75 * - Integer specifying number of rows (again)
76 * - nrow integers specifying the number of data entries on each row ("ndata")
77 * - All the actual entries for each row, stored contiguous.
78 * Each entry consists of an integer column index and floating-point data value.
82 gmx_mtxio_write(const char * filename
,
86 gmx_sparsematrix_t
* sparse_matrix
)
95 if(full_matrix
!=NULL
&& sparse_matrix
!=NULL
)
97 gmx_fatal(FARGS
,"Both full AND sparse matrix specified to gmx_mtxio_write().\n");
100 fd
= gmx_fio_open(filename
,"w");
102 xd
= gmx_fio_getxdr(fd
);
104 /* Write magic number */
105 i
= GMX_MTXIO_MAGIC_NUMBER
;
108 /* Write generating Gromacs version */
109 do_string(GromacsVersion());
111 /* Write 1 for double, 0 for single precision */
112 if(sizeof(real
)==sizeof(double))
121 if(full_matrix
!=NULL
)
123 /* Full matrix storage format */
124 i
= GMX_MTXIO_FULL_MATRIX
;
127 ndo_real(full_matrix
,sz
,bDum
);
132 i
= GMX_MTXIO_SPARSE_MATRIX
;
135 do_int(sparse_matrix
->compressed_symmetric
);
136 do_int(sparse_matrix
->nrow
);
137 if(sparse_matrix
->nrow
!= nrow
)
139 gmx_fatal(FARGS
,"Internal inconsistency in sparse matrix.\n");
141 ndo_int(sparse_matrix
->ndata
,sparse_matrix
->nrow
,bDum
);
142 for(i
=0;i
<sparse_matrix
->nrow
;i
++)
144 for(j
=0;j
<sparse_matrix
->ndata
[i
];j
++)
146 do_int(sparse_matrix
->data
[i
][j
].col
);
147 do_real(sparse_matrix
->data
[i
][j
].value
);
156 gmx_mtxio_read (const char * filename
,
160 gmx_sparsematrix_t
** sparse_matrix
)
170 fd
= gmx_fio_open(filename
,"r");
172 xd
= gmx_fio_getxdr(fd
);
174 /* Read and check magic number */
175 i
= GMX_MTXIO_MAGIC_NUMBER
;
178 if(i
!=GMX_MTXIO_MAGIC_NUMBER
)
181 "No matrix data found in file. Note that the Hessian matrix format changed\n"
182 "in Gromacs 3.3 to enable portable files and sparse matrix storage.\n");
185 /* Read generating Gromacs version */
188 /* Write 1 for double, 0 for single precision */
189 if(sizeof(real
)==sizeof(double))
195 fprintf(stderr
,"Reading %s precision matrix generated by Gromacs %s\n",
196 (prec
== 1) ? "double" : "single",gmxver
);
205 if(i
==GMX_MTXIO_FULL_MATRIX
)
207 printf("Full matrix storage format, nrow=%d, ncols=%d\n",*nrow
,*ncol
);
209 sz
= (*nrow
) * (*ncol
);
210 snew((*full_matrix
),sz
);
211 ndo_real((*full_matrix
),sz
,bDum
);
216 printf("Sparse matrix storage format, nrow=%d, ncols=%d\n",*nrow
,*ncol
);
218 snew((*sparse_matrix
),1);
219 do_int((*sparse_matrix
)->compressed_symmetric
);
220 do_int((*sparse_matrix
)->nrow
);
221 if((*sparse_matrix
)->nrow
!= *nrow
)
223 gmx_fatal(FARGS
,"Internal inconsistency in sparse matrix.\n");
225 snew((*sparse_matrix
)->ndata
,(*sparse_matrix
)->nrow
);
226 snew((*sparse_matrix
)->nalloc
,(*sparse_matrix
)->nrow
);
227 snew((*sparse_matrix
)->data
,(*sparse_matrix
)->nrow
);
228 ndo_int((*sparse_matrix
)->ndata
,(*sparse_matrix
)->nrow
,bDum
);
230 for(i
=0;i
<(*sparse_matrix
)->nrow
;i
++)
232 (*sparse_matrix
)->nalloc
[i
] = (*sparse_matrix
)->ndata
[i
] + 10;
233 snew(((*sparse_matrix
)->data
[i
]),(*sparse_matrix
)->nalloc
[i
]);
235 for(j
=0;j
<(*sparse_matrix
)->ndata
[i
];j
++)
237 do_int((*sparse_matrix
)->data
[i
][j
].col
);
238 do_real((*sparse_matrix
)->data
[i
][j
].value
);