4 * This source code is part of
8 * GROningen MAchine for Chemical Simulations
12 * Copyright (c) 1991-1999
13 * BIOSON Research Institute, Dept. of Biophysical Chemistry
14 * University of Groningen, The Netherlands
17 * GROMACS: A message-passing parallel molecular dynamics implementation
18 * H.J.C. Berendsen, D. van der Spoel and R. van Drunen
19 * Comp. Phys. Comm. 91, 43-56 (1995)
21 * Also check out our WWW page:
22 * http://md.chem.rug.nl/~gmx
27 * Green Red Orange Magenta Azure Cyan Skyblue
29 static char *SRCID_tpxio_c
= "$Id$";
49 /* This number should be increased whenever the file format changes! */
50 static int tpx_version
= 11;
51 /* This number should be the most recent incompatible version */
52 static int tpx_incompatible_version
= 9;
53 /* This is the version of the file we are reading */
54 static int file_version
= 0;
57 void _do_section(int fp
,int key
,bool bRead
,char *src
,int line
)
62 if (fio_getftp(fp
) == efTPA
) {
64 do_string(itemstr
[key
]);
65 bDbg
= fio_getdebug(fp
);
66 fio_setdebug(fp
,FALSE
);
67 do_string(comment_str
[key
]);
68 fio_setdebug(fp
,bDbg
);
72 fprintf(stderr
,"Looking for section %s (%s, %d)",
73 itemstr
[key
],src
,line
);
77 } while ((strcasecmp(buf
,itemstr
[key
]) != 0));
79 if (strcasecmp(buf
,itemstr
[key
]) != 0)
80 fatal_error(0,"\nCould not find section heading %s",itemstr
[key
]);
81 else if (fio_getdebug(fp
))
82 fprintf(stderr
," and found it\n");
87 #define do_section(key,bRead) _do_section(fp,key,bRead,__FILE__,__LINE__)
89 /**************************************************************
91 * Now the higer level routines that do io of the structures and arrays
93 **************************************************************/
94 static void do_inputrec(t_inputrec
*ir
,bool bRead
)
99 if (file_version
!= tpx_version
) {
100 /* Give a warning about features that are not accessible */
101 fprintf(stderr
,"Note: tpx file_version %d, software version %d\n",
102 file_version
,tpx_version
);
105 if (file_version
>= 1) {
106 /* Basic inputrec stuff */
113 do_int(ir
->bDomDecomp
);
114 do_int(ir
->decomp_dir
);
116 do_int(ir
->nstcgsteep
);
121 do_int(ir
->nstenergy
);
122 do_int(ir
->nstxtcout
);
124 do_real(ir
->delta_t
);
125 do_real(ir
->xtcprec
);
126 do_int(ir
->solvent_opt
);
130 do_int(ir
->coulombtype
);
131 do_real(ir
->rcoulomb_switch
);
132 do_real(ir
->rcoulomb
);
134 do_real(ir
->rvdw_switch
);
136 do_int(ir
->bDispCorr
);
137 do_real(ir
->epsilon_r
);
141 do_int(ir
->pme_order
);
142 do_real(ir
->ewald_rtol
);
144 do_int(ir
->bUncStart
);
146 do_int(ir
->ntcmemory
);
148 do_int(ir
->npcmemory
);
151 do_rvec(ir
->compress
);
153 do_real(ir
->zero_temp_time
);
154 do_real(ir
->epsilon_r
);
155 do_real(ir
->shake_tol
);
156 do_real(ir
->fudgeQQ
);
158 do_real(ir
->init_lambda
);
159 do_real(ir
->delta_lambda
);
160 do_int(ir
->eDisreWeighting
);
161 do_int(ir
->bDisreMixed
);
164 do_int(ir
->nstdisreout
);
165 do_real(ir
->em_stepsize
);
167 if (file_version
>= 11)
171 fprintf(stderr
,"Note: niter not in run input file, setting it to %d\n",
174 do_int(ir
->eConstrAlg
);
175 do_int(ir
->nProjOrder
);
176 do_real(ir
->LincsWarnAngle
);
177 do_int(ir
->nstLincsout
);
178 do_real(ir
->ld_temp
);
179 do_real(ir
->ld_fric
);
181 do_int(ir
->userint1
);
182 do_int(ir
->userint2
);
183 do_int(ir
->userint3
);
184 do_int(ir
->userint4
);
185 do_real(ir
->userreal1
);
186 do_real(ir
->userreal2
);
187 do_real(ir
->userreal3
);
188 do_real(ir
->userreal4
);
191 do_int(ir
->opts
.ngtc
);
192 do_int(ir
->opts
.ngacc
);
193 do_int(ir
->opts
.ngfrz
);
194 do_int(ir
->opts
.ngener
);
196 snew(ir
->opts
.nrdf
, ir
->opts
.ngtc
);
197 snew(ir
->opts
.ref_t
, ir
->opts
.ngtc
);
198 snew(ir
->opts
.tau_t
, ir
->opts
.ngtc
);
199 snew(ir
->opts
.nFreeze
,ir
->opts
.ngfrz
);
200 snew(ir
->opts
.acc
, ir
->opts
.ngacc
);
202 if (ir
->opts
.ngtc
> 0) {
203 ndo_int (ir
->opts
.nrdf
, ir
->opts
.ngtc
,bDum
);
204 ndo_real(ir
->opts
.ref_t
,ir
->opts
.ngtc
,bDum
);
205 ndo_real(ir
->opts
.tau_t
,ir
->opts
.ngtc
,bDum
);
207 if (ir
->opts
.ngfrz
> 0)
208 ndo_ivec(ir
->opts
.nFreeze
,ir
->opts
.ngfrz
,bDum
);
209 if (ir
->opts
.ngacc
> 0)
210 ndo_rvec(ir
->opts
.acc
,ir
->opts
.ngacc
);
212 /* Cosine stuff for electric fields */
213 for(j
=0; (j
<DIM
); j
++) {
214 do_int (ir
->ex
[j
].n
);
215 do_int (ir
->et
[j
].n
);
217 snew(ir
->ex
[j
].a
, ir
->ex
[j
].n
);
218 snew(ir
->ex
[j
].phi
,ir
->ex
[j
].n
);
219 snew(ir
->et
[j
].a
, ir
->et
[j
].n
);
220 snew(ir
->et
[j
].phi
,ir
->et
[j
].n
);
222 ndo_real(ir
->ex
[j
].a
, ir
->ex
[j
].n
,bDum
);
223 ndo_real(ir
->ex
[j
].phi
,ir
->ex
[j
].n
,bDum
);
224 ndo_real(ir
->et
[j
].a
, ir
->et
[j
].n
,bDum
);
225 ndo_real(ir
->et
[j
].phi
,ir
->et
[j
].n
,bDum
);
228 /* set things which are in tpx_version but not in a previous version */
231 if (file_version < tpx_version) {
237 static void do_harm(t_iparams
*iparams
,bool bRead
)
239 do_real(iparams
->harmonic
.rA
);
240 do_real(iparams
->harmonic
.krA
);
241 do_real(iparams
->harmonic
.rB
);
242 do_real(iparams
->harmonic
.krB
);
245 void do_iparams(t_functype ftype
,t_iparams
*iparams
,bool bRead
)
251 set_comment(interaction_function
[ftype
].name
);
260 do_harm(iparams
,bRead
);
263 do_real(iparams
->bham
.a
);
264 do_real(iparams
->bham
.b
);
265 do_real(iparams
->bham
.c
);
268 do_real(iparams
->morse
.b0
);
269 do_real(iparams
->morse
.cb
);
270 do_real(iparams
->morse
.beta
);
273 do_real(iparams
->wpol
.kx
);
274 do_real(iparams
->wpol
.ky
);
275 do_real(iparams
->wpol
.kz
);
276 do_real(iparams
->wpol
.rOH
);
277 do_real(iparams
->wpol
.rHH
);
278 do_real(iparams
->wpol
.rOD
);
281 do_real(iparams
->lj
.c6
);
282 do_real(iparams
->lj
.c12
);
285 do_real(iparams
->lj14
.c6A
);
286 do_real(iparams
->lj14
.c12A
);
287 do_real(iparams
->lj14
.c6B
);
288 do_real(iparams
->lj14
.c12B
);
291 do_real(iparams
->pdihs
.phiA
);
292 do_real(iparams
->pdihs
.cpA
);
293 do_real(iparams
->pdihs
.phiB
);
294 do_real(iparams
->pdihs
.cpB
);
295 do_int (iparams
->pdihs
.mult
);
298 do_int (iparams
->disres
.index
);
299 do_int (iparams
->disres
.type
);
300 do_real(iparams
->disres
.low
);
301 do_real(iparams
->disres
.up1
);
302 do_real(iparams
->disres
.up2
);
303 do_real(iparams
->disres
.fac
);
306 do_rvec(iparams
->posres
.pos0
);
307 do_rvec(iparams
->posres
.fc
);
310 ndo_real(iparams
->rbdihs
.rbc
,NR_RBDIHS
,bDum
);
314 do_real(iparams
->shake
.dA
);
315 do_real(iparams
->shake
.dB
);
318 do_real(iparams
->settle
.doh
);
319 do_real(iparams
->settle
.dhh
);
322 do_real(iparams
->dummy
.a
);
327 do_real(iparams
->dummy
.a
);
328 do_real(iparams
->dummy
.b
);
332 do_real(iparams
->dummy
.a
);
333 do_real(iparams
->dummy
.b
);
334 do_real(iparams
->dummy
.c
);
337 fatal_error(0,"unknown function type %d (%s) in %s line %d",
338 ftype
,interaction_function
[ftype
].name
,__FILE__
,__LINE__
);
344 static void do_ilist(t_ilist
*ilist
,bool bRead
,char *name
)
351 ndo_int(ilist
->multinr
,MAXPROC
,bDum
);
354 snew(ilist
->iatoms
,ilist
->nr
);
355 ndo_int(ilist
->iatoms
,ilist
->nr
,bDum
);
360 static void do_idef(t_idef
*idef
,bool bRead
)
367 do_int(idef
->ntypes
);
369 snew(idef
->functype
,idef
->ntypes
);
370 snew(idef
->iparams
,idef
->ntypes
);
372 ndo_int(idef
->functype
,idef
->ntypes
,bDum
);
374 for (i
=0; (i
<idef
->ntypes
); i
++)
375 do_iparams(idef
->functype
[i
],&idef
->iparams
[i
],bRead
);
377 for(j
=0; (j
<F_NRE
); j
++) {
378 if ((bRead
&& (file_version
< 6)) &&
379 ((j
== F_G96ANGLES
) || (j
== F_G96BONDS
))) {
380 fprintf(stderr
,"Warning: file_version %d < 6: no GROMOS96 Force field\n"
383 idef
->il
[j
].multinr
[0] = 0;
384 idef
->il
[j
].iatoms
= NULL
;
387 do_ilist(&idef
->il
[j
],bRead
,interaction_function
[j
].name
);
391 static void do_block(t_block
*block
,bool bRead
)
396 ndo_int(block
->multinr
,MAXPROC
,bDum
);
400 snew(block
->index
,block
->nr
+1);
401 snew(block
->a
,block
->nra
);
403 ndo_int(block
->index
,block
->nr
+1,bDum
);
404 ndo_int(block
->a
,block
->nra
,bDum
);
407 static void do_atom(t_atom
*atom
,bool bRead
)
415 do_ushort(atom
->type
);
416 do_ushort(atom
->typeB
);
417 do_int (atom
->ptype
);
418 do_int (atom
->resnr
);
419 do_nuchar(atom
->grpnr
,ngrp
);
422 static void do_grps(int ngrp
,t_grps grps
[],bool bRead
)
427 for(j
=0; (j
<ngrp
); j
++) {
430 snew(grps
[j
].nm_ind
,grps
[j
].nr
);
431 ndo_int(grps
[j
].nm_ind
,grps
[j
].nr
,bDum
);
435 static void do_symstr(char ***nm
,bool bRead
,t_symtab
*symtab
)
441 *nm
= get_symtab_handle(symtab
,ls
);
444 ls
= lookup_symtab(symtab
,*nm
);
449 static void do_strstr(int nstr
,char ***nm
,bool bRead
,t_symtab
*symtab
)
453 for (j
=0; (j
<nstr
); j
++)
454 do_symstr(&(nm
[j
]),bRead
,symtab
);
457 static void do_atoms(t_atoms
*atoms
,bool bRead
,t_symtab
*symtab
)
463 do_int(atoms
->ngrpname
);
465 snew(atoms
->atom
,atoms
->nr
);
466 snew(atoms
->atomname
,atoms
->nr
);
467 snew(atoms
->resname
,atoms
->nres
);
468 snew(atoms
->grpname
,atoms
->ngrpname
);
469 atoms
->pdbinfo
= NULL
;
471 for(i
=0; (i
<atoms
->nr
); i
++)
472 do_atom(&atoms
->atom
[i
],bRead
);
473 do_strstr(atoms
->nr
,atoms
->atomname
,bRead
,symtab
);
474 do_strstr(atoms
->nres
,atoms
->resname
,bRead
,symtab
);
475 do_strstr(atoms
->ngrpname
,atoms
->grpname
,bRead
,symtab
);
477 do_grps(egcNR
,atoms
->grps
,bRead
);
479 do_block(&(atoms
->excl
),bRead
);
482 static void do_symtab(t_symtab
*symtab
,bool bRead
)
491 snew(symtab
->symbuf
,1);
492 symbuf
= symtab
->symbuf
;
493 symbuf
->bufsize
= nr
;
494 snew(symbuf
->buf
,nr
);
495 for (i
=0; (i
<nr
); i
++) {
497 symbuf
->buf
[i
]=strdup(buf
);
501 symbuf
= symtab
->symbuf
;
502 while (symbuf
!=NULL
) {
503 for (i
=0; (i
<symbuf
->bufsize
) && (i
<nr
); i
++)
504 do_string(symbuf
->buf
[i
]);
509 fatal_error(0,"nr of symtab strings left: %d",nr
);
513 static void make_chain_identifiers(t_atoms
*atoms
,t_block
*mols
)
517 #define CHAIN_MIN_ATOMS 15
520 for(m
=0; m
<mols
->nr
; m
++) {
523 if ((a1
-a0
>= CHAIN_MIN_ATOMS
) && (chain
<= 'Z')) {
529 atoms
->atom
[mols
->a
[a
]].chain
=c
;
532 for(a
=0; a
<atoms
->nr
; a
++)
533 atoms
->atom
[a
].chain
=' ';
536 static void do_top(t_topology
*top
,bool bRead
)
540 do_symtab(&(top
->symtab
),bRead
);
541 do_symstr(&(top
->name
),bRead
,&(top
->symtab
));
542 do_atoms (&(top
->atoms
),bRead
,&(top
->symtab
));
543 do_idef (&(top
->idef
),bRead
);
544 for(i
=0; (i
<ebNR
); i
++)
545 do_block(&(top
->blocks
[i
]),bRead
);
547 close_symtab(&(top
->symtab
));
548 make_chain_identifiers(&(top
->atoms
),&(top
->blocks
[ebMOLS
]));
552 static void do_tpxheader(int fp
,bool bRead
,t_tpxheader
*tpx
)
559 fio_setdebug(fp
,bDebugMode());
561 /* NEW! XDR tpb file */
562 precision
= sizeof(real
);
565 if (strncmp(buf
,"VERSION",7))
566 fatal_error(0,"Can not read file %s,\n"
567 " this file is from a Gromacs version which is older than 2.0\n"
568 " Make a new one with grompp or use a gro or pdb file, if possible",
571 bDouble
= (precision
== sizeof(double));
572 if ((precision
!= sizeof(float)) && !bDouble
)
573 fatal_error(0,"Unknown precision in file %s: real is %d bytes "
574 "instead of %d or %d",
575 fio_getname(fp
),precision
,sizeof(float),sizeof(double));
576 fio_setprecision(fp
,bDouble
);
577 fprintf(stderr
,"Reading file %s, %s (%s precision)\n",
578 fio_getname(fp
),buf
,bDouble
? "double" : "single");
581 do_string(GromacsVersion());
582 bDouble
= (precision
== sizeof(double));
583 fio_setprecision(fp
,bDouble
);
585 file_version
= tpx_version
;
588 /* Check versions! */
589 do_int(file_version
);
590 if ((file_version
<= tpx_incompatible_version
) ||
591 (file_version
> tpx_version
))
592 fatal_error(0,"reading tpx file (%s) version %d with version %d program",
593 fio_getname(fp
),file_version
,tpx_version
);
595 do_section(eitemHEADER
,bRead
);
596 do_int (tpx
->natoms
);
599 do_real(tpx
->lambda
);
608 static void do_tpx(int fp
,bool bRead
,int *step
,real
*t
,real
*lambda
,
609 t_inputrec
*ir
,rvec
*box
,int *natoms
,
610 rvec
*x
,rvec
*v
,rvec
*f
,t_topology
*top
)
617 tpx
.natoms
= *natoms
;
620 tpx
.lambda
= *lambda
;
621 tpx
.bIr
= (ir
!= NULL
);
622 tpx
.bTop
= (top
!= NULL
);
623 tpx
.bX
= (x
!= NULL
);
624 tpx
.bV
= (v
!= NULL
);
625 tpx
.bF
= (f
!= NULL
);
626 tpx
.bBox
= (box
!= NULL
);
629 do_tpxheader(fp
,bRead
,&tpx
);
632 *natoms
= tpx
.natoms
;
635 *lambda
= tpx
.lambda
;
638 #define do_test(b,p) if (bRead && (p!=NULL) && !b) fatal_error(0,"No %s in %s",#p,fio_getname(fp))
640 do_test(tpx
.bBox
,box
);
641 do_section(eitemBOX
,bRead
);
642 if (tpx
.bBox
) ndo_rvec(box
,DIM
);
645 do_section(eitemIR
,bRead
);
648 do_inputrec(ir
,bRead
);
650 init_inputrec(&dum_ir
);
651 do_inputrec (&dum_ir
,bRead
);
652 done_inputrec(&dum_ir
);
655 do_test(tpx
.bTop
,top
);
656 do_section(eitemTOP
,bRead
);
661 do_top(&dum_top
,bRead
);
666 do_section(eitemX
,bRead
);
667 if (tpx
.bX
) ndo_rvec(x
,*natoms
);
670 do_section(eitemV
,bRead
);
671 if (tpx
.bV
) ndo_rvec(v
,*natoms
);
674 do_section(eitemF
,bRead
);
675 if (tpx
.bF
) ndo_rvec(f
,*natoms
);
678 /************************************************************
680 * The following routines are the exported ones
682 ************************************************************/
684 int open_tpx(char *fn
,char *mode
)
686 return fio_open(fn
,mode
);
689 void close_tpx(int fp
)
694 void read_tpxheader(char *fn
,t_tpxheader
*tpx
)
698 fp
= open_tpx(fn
,"r");
699 do_tpxheader(fp
,TRUE
,tpx
);
703 void write_tpx(char *fn
,int step
,real t
,real lambda
,
704 t_inputrec
*ir
,rvec
*box
,int natoms
,
705 rvec
*x
,rvec
*v
,rvec
*f
,t_topology
*top
)
709 fp
= open_tpx(fn
,"w");
710 do_tpx(fp
,FALSE
,&step
,&t
,&lambda
,ir
,box
,&natoms
,x
,v
,f
,top
);
714 void read_tpx(char *fn
,int *step
,real
*t
,real
*lambda
,
715 t_inputrec
*ir
,rvec
*box
,int *natoms
,
716 rvec
*x
,rvec
*v
,rvec
*f
,t_topology
*top
)
720 fp
= open_tpx(fn
,"r");
721 do_tpx(fp
,TRUE
,step
,t
,lambda
,ir
,box
,natoms
,x
,v
,f
,top
);
725 void fwrite_tpx(int fp
,int step
,real t
,real lambda
,
726 t_inputrec
*ir
,rvec
*box
,int natoms
,
727 rvec
*x
,rvec
*v
,rvec
*f
,t_topology
*top
)
729 do_tpx(fp
,FALSE
,&step
,&t
,&lambda
,ir
,box
,&natoms
,x
,v
,f
,top
);
733 void fread_tpx(int fp
,int *step
,real
*t
,real
*lambda
,
734 t_inputrec
*ir
,rvec
*box
,int *natoms
,
735 rvec
*x
,rvec
*v
,rvec
*f
,t_topology
*top
)
737 do_tpx(fp
,TRUE
,step
,t
,lambda
,ir
,box
,natoms
,x
,v
,f
,top
);
740 bool fn2bTPX(char *file
)
742 switch (fn2ftp(file
)) {
752 bool read_tps_conf(char *infile
,char *title
,t_topology
*top
,
753 rvec
**x
,rvec
**v
,matrix box
,bool bMass
)
760 bTop
=fn2bTPX(infile
);
762 read_tpxheader(infile
,&header
);
763 snew(*x
,header
.natoms
);
765 snew(*v
,header
.natoms
);
766 read_tpx(infile
,&step
,&t
,&lambda
,NULL
,box
,&natoms
,
767 *x
,(v
==NULL
) ? NULL
: *v
,NULL
,top
);
768 strcpy(title
,*top
->name
);
771 get_stx_coordnum(infile
,&natoms
);
772 init_t_atoms(&top
->atoms
,natoms
,FALSE
);
776 read_stx_conf(infile
,title
,&top
->atoms
,*x
,(v
==NULL
) ? NULL
: *v
,box
);
778 for(i
=0; i
<natoms
; i
++)
779 top
->atoms
.atom
[i
].m
=
780 get_mass(*top
->atoms
.resname
[top
->atoms
.atom
[i
].resnr
],
781 *top
->atoms
.atomname
[i
]);