1 /* -*- mode: c; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; c-file-style: "stroustrup"; -*-
4 * This source code is part of
8 * GROningen MAchine for Chemical Simulations
11 * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
12 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
13 * Copyright (c) 2001-2004, The GROMACS development team,
14 * check out http://www.gromacs.org for more information.
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License
18 * as published by the Free Software Foundation; either version 2
19 * of the License, or (at your option) any later version.
21 * If you want to redistribute modifications, please consider that
22 * scientific software is very special. Version control is crucial -
23 * bugs must be traceable. We will be happy to consider code for
24 * inclusion in the official distribution, but derived work must not
25 * be called official GROMACS. Details are found in the README & COPYING
26 * files - if they are missing, get the official version at www.gromacs.org.
28 * To help us fund GROMACS development, we humbly ask that you cite
29 * the papers on the package - you can find them in the top README file.
31 * For more info, check our website at http://www.gromacs.org
34 * GROningen Mixture of Alchemy and Childrens' Stories
36 /* This file is completely threadsafe - keep it that way! */
48 #include "thread_mpi.h"
51 /* The source code in this file should be thread-safe.
52 Please keep it that way. */
56 static bool bOverAllocDD
=FALSE
;
58 static tMPI_Thread_mutex_t over_alloc_mutex
=TMPI_THREAD_MUTEX_INITIALIZER
;
62 void set_over_alloc_dd(bool set
)
65 tMPI_Thread_mutex_lock(&over_alloc_mutex
);
66 /* we just make sure that we don't set this at the same time.
67 We don't worry too much about reading this rarely-set variable */
71 tMPI_Thread_mutex_unlock(&over_alloc_mutex
);
75 int over_alloc_dd(int n
)
78 return OVER_ALLOC_FAC
*n
+ 100;
83 int gmx_large_int_to_int(gmx_large_int_t step
,const char *warn
)
89 if (warn
!= NULL
&& (step
< INT_MIN
|| step
> INT_MAX
)) {
90 fprintf(stderr
,"\nWARNING during %s:\n",warn
);
91 fprintf(stderr
,"step value ");
92 fprintf(stderr
,gmx_large_int_pfmt
,step
);
93 fprintf(stderr
," does not fit in int, converted to %d\n\n",i
);
99 char *gmx_step_str(gmx_large_int_t i
,char *buf
)
101 sprintf(buf
,gmx_large_int_pfmt
,i
);
106 void init_block(t_block
*block
)
111 block
->nalloc_index
= 1;
112 snew(block
->index
,block
->nalloc_index
);
116 void init_blocka(t_blocka
*block
)
122 block
->nalloc_index
= 1;
123 snew(block
->index
,block
->nalloc_index
);
129 void init_atom(t_atoms
*at
)
143 void init_atomtypes(t_atomtypes
*at
)
148 at
->atomnumber
= NULL
;
149 at
->gb_radius
= NULL
;
153 void init_groups(gmx_groups_t
*groups
)
157 groups
->ngrpname
= 0;
158 groups
->grpname
= NULL
;
159 for(g
=0; (g
<egcNR
); g
++) {
160 groups
->grps
[g
].nm_ind
= NULL
;
161 groups
->ngrpnr
[g
] = 0;
162 groups
->grpnr
[g
] = NULL
;
167 void init_mtop(gmx_mtop_t
*mtop
)
171 mtop
->moltype
= NULL
;
173 mtop
->molblock
= NULL
;
174 mtop
->maxres_renum
= 0;
176 init_groups(&mtop
->groups
);
177 init_block(&mtop
->mols
);
178 open_symtab(&mtop
->symtab
);
181 void init_top (t_topology
*top
)
186 init_atom (&(top
->atoms
));
187 init_atomtypes(&(top
->atomtypes
));
188 init_block(&top
->cgs
);
189 init_block(&top
->mols
);
190 init_blocka(&top
->excls
);
191 open_symtab(&top
->symtab
);
194 void init_inputrec(t_inputrec
*ir
)
196 memset(ir
,0,(size_t)sizeof(*ir
));
199 void stupid_fill_block(t_block
*grp
,int natom
,bool bOneIndexGroup
)
203 if (bOneIndexGroup
) {
204 grp
->nalloc_index
= 2;
205 snew(grp
->index
,grp
->nalloc_index
);
211 grp
->nalloc_index
= natom
+1;
212 snew(grp
->index
,grp
->nalloc_index
);
213 snew(grp
->index
,natom
+1);
214 for(i
=0; (i
<=natom
); i
++)
220 void stupid_fill_blocka(t_blocka
*grp
,int natom
)
224 grp
->nalloc_a
= natom
;
225 snew(grp
->a
,grp
->nalloc_a
);
226 for(i
=0; (i
<natom
); i
++)
230 grp
->nalloc_index
= natom
+ 1;
231 snew(grp
->index
,grp
->nalloc_index
);
232 for(i
=0; (i
<=natom
); i
++)
237 void copy_blocka(const t_blocka
*src
,t_blocka
*dest
)
242 dest
->nalloc_index
= dest
->nr
+ 1;
243 snew(dest
->index
,dest
->nalloc_index
);
244 for(i
=0; i
<dest
->nr
+1; i
++) {
245 dest
->index
[i
] = src
->index
[i
];
247 dest
->nra
= src
->nra
;
248 dest
->nalloc_a
= dest
->nra
+ 1;
249 snew(dest
->a
,dest
->nalloc_a
);
250 for(i
=0; i
<dest
->nra
+1; i
++) {
251 dest
->a
[i
] = src
->a
[i
];
255 void done_block(t_block
*block
)
259 block
->nalloc_index
= 0;
262 void done_blocka(t_blocka
*block
)
269 block
->nalloc_index
= 0;
273 void done_atom (t_atoms
*at
)
282 void done_atomtypes(t_atomtypes
*atype
)
285 sfree(atype
->radius
);
287 sfree(atype
->surftens
);
288 sfree(atype
->gb_radius
);
292 void done_moltype(gmx_moltype_t
*molt
)
296 done_atom(&molt
->atoms
);
297 done_block(&molt
->cgs
);
298 done_blocka(&molt
->excls
);
300 for(f
=0; f
<F_NRE
; f
++) {
301 sfree(molt
->ilist
[f
].iatoms
);
302 molt
->ilist
[f
].nalloc
= 0;
306 void done_molblock(gmx_molblock_t
*molb
)
308 if (molb
->nposres_xA
> 0) {
309 molb
->nposres_xA
= 0;
310 free(molb
->posres_xA
);
312 if (molb
->nposres_xB
> 0) {
313 molb
->nposres_xB
= 0;
314 free(molb
->posres_xB
);
318 void done_mtop(gmx_mtop_t
*mtop
,bool bDoneSymtab
)
323 done_symtab(&mtop
->symtab
);
326 sfree(mtop
->ffparams
.functype
);
327 sfree(mtop
->ffparams
.iparams
);
329 for(i
=0; i
<mtop
->nmoltype
; i
++) {
330 done_moltype(&mtop
->moltype
[i
]);
332 sfree(mtop
->moltype
);
333 for(i
=0; i
<mtop
->nmolblock
; i
++) {
334 done_molblock(&mtop
->molblock
[i
]);
336 sfree(mtop
->molblock
);
337 done_block(&mtop
->mols
);
340 void done_top(t_topology
*top
)
344 done_atom (&(top
->atoms
));
347 done_atomtypes(&(top
->atomtypes
));
349 done_symtab(&(top
->symtab
));
350 done_block(&(top
->cgs
));
351 done_block(&(top
->mols
));
352 done_blocka(&(top
->excls
));
355 static void done_pullgrp(t_pullgrp
*pgrp
)
358 sfree(pgrp
->ind_loc
);
360 sfree(pgrp
->weight_loc
);
363 static void done_pull(t_pull
*pull
)
367 for(i
=0; i
<pull
->ngrp
+1; i
++) {
368 done_pullgrp(pull
->grp
);
369 done_pullgrp(pull
->dyna
);
373 void done_inputrec(t_inputrec
*ir
)
377 for(m
=0; (m
<DIM
); m
++) {
378 if (ir
->ex
[m
].a
) sfree(ir
->ex
[m
].a
);
379 if (ir
->ex
[m
].phi
) sfree(ir
->ex
[m
].phi
);
380 if (ir
->et
[m
].a
) sfree(ir
->et
[m
].a
);
381 if (ir
->et
[m
].phi
) sfree(ir
->et
[m
].phi
);
384 sfree(ir
->opts
.nrdf
);
385 sfree(ir
->opts
.ref_t
);
386 sfree(ir
->opts
.annealing
);
387 sfree(ir
->opts
.anneal_npoints
);
388 sfree(ir
->opts
.anneal_time
);
389 sfree(ir
->opts
.anneal_temp
);
390 sfree(ir
->opts
.tau_t
);
392 sfree(ir
->opts
.nFreeze
);
393 sfree(ir
->opts
.QMmethod
);
394 sfree(ir
->opts
.QMbasis
);
395 sfree(ir
->opts
.QMcharge
);
396 sfree(ir
->opts
.QMmult
);
398 sfree(ir
->opts
.CASorbitals
);
399 sfree(ir
->opts
.CASelectrons
);
400 sfree(ir
->opts
.SAon
);
401 sfree(ir
->opts
.SAoff
);
402 sfree(ir
->opts
.SAsteps
);
403 sfree(ir
->opts
.bOPT
);
412 static void init_ekinstate(ekinstate_t
*eks
)
417 eks
->ekinh_old
= NULL
;
418 eks
->ekinscalef_nhc
= NULL
;
419 eks
->ekinscaleh_nhc
= NULL
;
420 eks
->vscale_nhc
= NULL
;
425 static void init_energyhistory(energyhistory_t
*enh
)
427 enh
->ener_ave
= NULL
;
428 enh
->ener_sum
= NULL
;
429 enh
->ener_sum_sim
= NULL
;
433 void init_gtc_state(t_state
*state
, int ngtc
, int nnhpres
, int nhchainlength
)
438 state
->nnhpres
= nnhpres
;
439 state
->nhchainlength
= nhchainlength
;
442 snew(state
->nosehoover_xi
,state
->nhchainlength
*state
->ngtc
);
443 snew(state
->nosehoover_vxi
,state
->nhchainlength
*state
->ngtc
);
444 snew(state
->therm_integral
,state
->ngtc
);
445 for(i
=0; i
<state
->ngtc
; i
++)
447 for (j
=0;j
<state
->nhchainlength
;j
++)
449 state
->nosehoover_xi
[i
*state
->nhchainlength
+ j
] = 0.0;
450 state
->nosehoover_vxi
[i
*state
->nhchainlength
+ j
] = 0.0;
453 for(i
=0; i
<state
->ngtc
; i
++) {
454 state
->therm_integral
[i
] = 0.0;
459 state
->nosehoover_xi
= NULL
;
460 state
->nosehoover_vxi
= NULL
;
461 state
->therm_integral
= NULL
;
464 if (state
->nnhpres
> 0)
466 snew(state
->nhpres_xi
,state
->nhchainlength
*nnhpres
);
467 snew(state
->nhpres_vxi
,state
->nhchainlength
*nnhpres
);
468 for(i
=0; i
<nnhpres
; i
++)
470 for (j
=0;j
<state
->nhchainlength
;j
++)
472 state
->nhpres_xi
[i
*nhchainlength
+ j
] = 0.0;
473 state
->nhpres_vxi
[i
*nhchainlength
+ j
] = 0.0;
479 state
->nhpres_xi
= NULL
;
480 state
->nhpres_vxi
= NULL
;
485 void init_state(t_state
*state
, int natoms
, int ngtc
, int nnhpres
, int nhchainlength
)
489 state
->natoms
= natoms
;
494 clear_mat(state
->box
);
495 clear_mat(state
->box_rel
);
496 clear_mat(state
->boxv
);
497 clear_mat(state
->pres_prev
);
498 clear_mat(state
->svir_prev
);
499 clear_mat(state
->fvir_prev
);
500 init_gtc_state(state
,ngtc
,nnhpres
,nhchainlength
);
501 state
->nalloc
= state
->natoms
;
502 if (state
->nalloc
> 0) {
503 snew(state
->x
,state
->nalloc
);
504 snew(state
->v
,state
->nalloc
);
512 init_ekinstate(&state
->ekinstate
);
514 init_energyhistory(&state
->enerhist
);
516 state
->ddp_count
= 0;
517 state
->ddp_count_cg_gl
= 0;
519 state
->cg_gl_nalloc
= 0;
522 void done_state(t_state
*state
)
524 if (state
->nosehoover_xi
) sfree(state
->nosehoover_xi
);
525 if (state
->x
) sfree(state
->x
);
526 if (state
->v
) sfree(state
->v
);
527 if (state
->sd_X
) sfree(state
->sd_X
);
528 if (state
->cg_p
) sfree(state
->cg_p
);
530 if (state
->cg_gl
) sfree(state
->cg_gl
);
531 state
->cg_gl_nalloc
= 0;
534 static void do_box_rel(t_inputrec
*ir
,matrix box_rel
,matrix b
,bool bInit
)
538 for(d
=YY
; d
<=ZZ
; d
++) {
539 for(d2
=XX
; d2
<=(ir
->epct
==epctSEMIISOTROPIC
? YY
: ZZ
); d2
++) {
540 /* We need to check if this box component is deformed
541 * or if deformation of another component might cause
542 * changes in this component due to box corrections.
544 if (ir
->deform
[d
][d2
] == 0 &&
545 !(d
== ZZ
&& d2
== XX
&& ir
->deform
[d
][YY
] != 0 &&
546 (b
[YY
][d2
] != 0 || ir
->deform
[YY
][d2
] != 0))) {
548 box_rel
[d
][d2
] = b
[d
][d2
]/b
[XX
][XX
];
550 b
[d
][d2
] = b
[XX
][XX
]*box_rel
[d
][d2
];
557 void set_box_rel(t_inputrec
*ir
,t_state
*state
)
559 /* Make sure the box obeys the restrictions before we fix the ratios */
560 correct_box(NULL
,0,state
->box
,NULL
);
562 clear_mat(state
->box_rel
);
564 if (PRESERVE_SHAPE(*ir
))
565 do_box_rel(ir
,state
->box_rel
,state
->box
,TRUE
);
568 void preserve_box_shape(t_inputrec
*ir
,matrix box_rel
,matrix b
)
570 if (PRESERVE_SHAPE(*ir
))
571 do_box_rel(ir
,box_rel
,b
,FALSE
);
574 void add_t_atoms(t_atoms
*atoms
,int natom_extra
,int nres_extra
)
580 srenew(atoms
->atomname
,atoms
->nr
+natom_extra
);
581 srenew(atoms
->atom
,atoms
->nr
+natom_extra
);
582 if (NULL
!= atoms
->pdbinfo
)
583 srenew(atoms
->pdbinfo
,atoms
->nr
+natom_extra
);
584 if (NULL
!= atoms
->atomtype
)
585 srenew(atoms
->atomtype
,atoms
->nr
+natom_extra
);
586 if (NULL
!= atoms
->atomtypeB
)
587 srenew(atoms
->atomtypeB
,atoms
->nr
+natom_extra
);
588 for(i
=atoms
->nr
; (i
<atoms
->nr
+natom_extra
); i
++) {
589 atoms
->atomname
[i
] = NULL
;
590 memset(&atoms
->atom
[i
],0,sizeof(atoms
->atom
[i
]));
591 if (NULL
!= atoms
->pdbinfo
)
592 memset(&atoms
->pdbinfo
[i
],0,sizeof(atoms
->pdbinfo
[i
]));
593 if (NULL
!= atoms
->atomtype
)
594 atoms
->atomtype
[i
] = NULL
;
595 if (NULL
!= atoms
->atomtypeB
)
596 atoms
->atomtypeB
[i
] = NULL
;
598 atoms
->nr
+= natom_extra
;
602 srenew(atoms
->resinfo
,atoms
->nres
+nres_extra
);
603 for(i
=atoms
->nres
; (i
<atoms
->nres
+nres_extra
); i
++) {
604 memset(&atoms
->resinfo
[i
],0,sizeof(atoms
->resinfo
[i
]));
606 atoms
->nres
+= nres_extra
;
610 void init_t_atoms(t_atoms
*atoms
, int natoms
, bool bPdbinfo
)
614 snew(atoms
->atomname
,natoms
);
615 atoms
->atomtype
=NULL
;
616 atoms
->atomtypeB
=NULL
;
617 snew(atoms
->resinfo
,natoms
);
618 snew(atoms
->atom
,natoms
);
620 snew(atoms
->pdbinfo
,natoms
);
625 t_atoms
*copy_t_atoms(t_atoms
*src
)
631 init_t_atoms(dst
,src
->nr
,(NULL
!= src
->pdbinfo
));
633 if (NULL
!= src
->atomname
)
634 snew(dst
->atomname
,src
->nr
);
635 if (NULL
!= src
->atomtype
)
636 snew(dst
->atomtype
,src
->nr
);
637 if (NULL
!= src
->atomtypeB
)
638 snew(dst
->atomtypeB
,src
->nr
);
639 for(i
=0; (i
<src
->nr
); i
++) {
640 dst
->atom
[i
] = src
->atom
[i
];
641 if (NULL
!= src
->pdbinfo
)
642 dst
->pdbinfo
[i
] = src
->pdbinfo
[i
];
643 if (NULL
!= src
->atomname
)
644 dst
->atomname
[i
] = src
->atomname
[i
];
645 if (NULL
!= src
->atomtype
)
646 dst
->atomtype
[i
] = src
->atomtype
[i
];
647 if (NULL
!= src
->atomtypeB
)
648 dst
->atomtypeB
[i
] = src
->atomtypeB
[i
];
650 dst
->nres
= src
->nres
;
651 for(i
=0; (i
<src
->nres
); i
++) {
652 dst
->resinfo
[i
] = src
->resinfo
[i
];
657 void t_atoms_set_resinfo(t_atoms
*atoms
,int atom_ind
,t_symtab
*symtab
,
658 const char *resname
,int resnr
,unsigned char ic
,
663 ri
= &atoms
->resinfo
[atoms
->atom
[atom_ind
].resind
];
664 ri
->name
= put_symtab(symtab
,resname
);
671 void free_t_atoms(t_atoms
*atoms
,bool bFreeNames
)
676 for(i
=0; i
<atoms
->nr
; i
++) {
677 sfree(*atoms
->atomname
[i
]);
678 *atoms
->atomname
[i
]=NULL
;
680 for(i
=0; i
<atoms
->nres
; i
++) {
681 sfree(*atoms
->resinfo
[i
].name
);
682 *atoms
->resinfo
[i
].name
=NULL
;
685 sfree(atoms
->atomname
);
686 /* Do we need to free atomtype and atomtypeB as well ? */
687 sfree(atoms
->resinfo
);
690 sfree(atoms
->pdbinfo
);
695 real
max_cutoff(real cutoff1
,real cutoff2
)
697 if (cutoff1
== 0 || cutoff2
== 0)
703 return max(cutoff1
,cutoff2
);