f588a2a64b12c86d8e74fb415108d7c2c6c6cbca
[alexxy/gromacs.git] / src / gmxlib / mvdata.c
1 /* -*- mode: c; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; c-file-style: "stroustrup"; -*-
2  * 
3  *                This source code is part of
4  * 
5  *                 G   R   O   M   A   C   S
6  * 
7  *          GROningen MAchine for Chemical Simulations
8  * 
9  *                        VERSION 3.2.0
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.
14
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.
19  * 
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.
26  * 
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.
29  * 
30  * For more info, check our website at http://www.gromacs.org
31  * 
32  * And Hey:
33  * GROningen Mixture of Alchemy and Childrens' Stories
34  */
35 /* This file is completely threadsafe - keep it that way! */
36 #ifdef HAVE_CONFIG_H
37 #include <config.h>
38 #endif
39
40 #include <sysstuff.h>
41 #include <string.h>
42 #include "typedefs.h"
43 #include "main.h"
44 #include "mvdata.h"
45 #include "network.h"
46 #include "smalloc.h"
47 #include "gmx_fatal.h"
48 #include "symtab.h"
49 #include "vec.h"
50 #include "tgroup.h"
51
52 #define   block_bc(cr,   d) gmx_bcast(     sizeof(d),     &(d),(cr))
53 #define  nblock_bc(cr,nr,d) { if ((nr) > 0) gmx_bcast((nr)*sizeof((d)[0]), (d),(cr)); }
54 #define    snew_bc(cr,d,nr) { if (!MASTER(cr)) snew((d),(nr)); }
55 /* Dirty macro with bAlloc not as an argument */
56 #define nblock_abc(cr,nr,d) { if (bAlloc) snew((d),(nr)); nblock_bc(cr,(nr),(d)); }
57
58 static void bc_string(const t_commrec *cr,t_symtab *symtab,char ***s)
59 {
60   int handle;
61   
62   if (MASTER(cr)) {
63     handle = lookup_symtab(symtab,*s);
64   }
65   block_bc(cr,handle);
66   if (!MASTER(cr)) {
67     *s = get_symtab_handle(symtab,handle);
68   }
69 }
70
71 static void bc_strings(const t_commrec *cr,t_symtab *symtab,int nr,char ****nm)
72 {
73   int  i;
74   int  *handle;
75   char ***NM;
76
77   snew(handle,nr);
78   if (MASTER(cr)) {
79     NM = *nm;
80     for(i=0; (i<nr); i++)
81       handle[i] = lookup_symtab(symtab,NM[i]);
82   }
83   nblock_bc(cr,nr,handle);
84
85   if (!MASTER(cr)) {
86     snew_bc(cr,*nm,nr);
87     NM = *nm;
88     for (i=0; (i<nr); i++) 
89       (*nm)[i] = get_symtab_handle(symtab,handle[i]);
90   }
91   sfree(handle);
92 }
93
94 static void bc_strings_resinfo(const t_commrec *cr,t_symtab *symtab,
95                                int nr,t_resinfo *resinfo)
96 {
97   int  i;
98   int  *handle;
99
100   snew(handle,nr);
101   if (MASTER(cr)) {
102     for(i=0; (i<nr); i++)
103       handle[i] = lookup_symtab(symtab,resinfo[i].name);
104   }
105   nblock_bc(cr,nr,handle);
106
107   if (!MASTER(cr)) {
108     for (i=0; (i<nr); i++) 
109       resinfo[i].name = get_symtab_handle(symtab,handle[i]);
110   }
111   sfree(handle);
112 }
113
114 static void bc_symtab(const t_commrec *cr,t_symtab *symtab)
115 {
116   int i,nr,len;
117   t_symbuf *symbuf;
118
119   block_bc(cr,symtab->nr);
120   nr = symtab->nr;
121   snew_bc(cr,symtab->symbuf,1);
122   symbuf = symtab->symbuf;
123   symbuf->bufsize = nr;
124   snew_bc(cr,symbuf->buf,nr);
125   for (i=0; i<nr; i++) {
126     if (MASTER(cr))
127       len = strlen(symbuf->buf[i]) + 1;
128     block_bc(cr,len);
129     snew_bc(cr,symbuf->buf[i],len);
130     nblock_bc(cr,len,symbuf->buf[i]);
131   }
132 }
133
134 static void bc_block(const t_commrec *cr,t_block *block)
135 {
136   block_bc(cr,block->nr);
137   snew_bc(cr,block->index,block->nr+1);
138   nblock_bc(cr,block->nr+1,block->index);
139 }
140
141 static void bc_blocka(const t_commrec *cr,t_blocka *block)
142 {
143   block_bc(cr,block->nr);
144   snew_bc(cr,block->index,block->nr+1);
145   nblock_bc(cr,block->nr+1,block->index);
146   block_bc(cr,block->nra);
147   if (block->nra) {
148     snew_bc(cr,block->a,block->nra);
149     nblock_bc(cr,block->nra,block->a);
150   }
151 }
152
153 static void bc_grps(const t_commrec *cr,t_grps grps[])
154 {
155   int i;
156   
157   for(i=0; (i<egcNR); i++) {
158     block_bc(cr,grps[i].nr);
159     snew_bc(cr,grps[i].nm_ind,grps[i].nr);
160     nblock_bc(cr,grps[i].nr,grps[i].nm_ind);
161   }
162 }
163
164 static void bc_atoms(const t_commrec *cr,t_symtab *symtab,t_atoms *atoms)
165 {
166   int dummy;
167
168   block_bc(cr,atoms->nr);
169   snew_bc(cr,atoms->atom,atoms->nr);
170   nblock_bc(cr,atoms->nr,atoms->atom);
171   bc_strings(cr,symtab,atoms->nr,&atoms->atomname);
172   block_bc(cr,atoms->nres);
173   snew_bc(cr,atoms->resinfo,atoms->nres);
174   nblock_bc(cr,atoms->nres,atoms->resinfo);
175   bc_strings_resinfo(cr,symtab,atoms->nres,atoms->resinfo);
176   /* QMMM requires atomtypes to be known on all nodes as well */
177   bc_strings(cr,symtab,atoms->nr,&atoms->atomtype);
178   bc_strings(cr,symtab,atoms->nr,&atoms->atomtypeB);
179 }
180
181 static void bc_groups(const t_commrec *cr,t_symtab *symtab,
182                       int natoms,gmx_groups_t *groups)
183 {
184   int dummy;
185   int g,n;
186
187   bc_grps(cr,groups->grps);
188   block_bc(cr,groups->ngrpname);
189   bc_strings(cr,symtab,groups->ngrpname,&groups->grpname);
190   for(g=0; g<egcNR; g++) {
191     if (MASTER(cr)) {
192       if (groups->grpnr[g]) {
193         n = natoms;
194       } else {
195         n = 0;
196       }
197     }
198     block_bc(cr,n);
199     if (n == 0) {
200       groups->grpnr[g] = NULL;
201     } else {
202       snew_bc(cr,groups->grpnr[g],n);
203       nblock_bc(cr,n,groups->grpnr[g]);
204     }
205   }
206   if (debug) fprintf(debug,"after bc_groups\n");
207 }
208
209 void bcast_state_setup(const t_commrec *cr,t_state *state)
210 {
211   block_bc(cr,state->natoms);
212   block_bc(cr,state->ngtc);
213   block_bc(cr,state->nnhpres);
214   block_bc(cr,state->nhchainlength);
215   block_bc(cr,state->nrng);
216   block_bc(cr,state->nrngi);
217   block_bc(cr,state->flags);
218 }
219
220 void bcast_state(const t_commrec *cr,t_state *state,bool bAlloc)
221 {
222   int i,nnht,nnhtp;
223
224   bcast_state_setup(cr,state);
225
226   nnht = (state->ngtc)*(state->nhchainlength); 
227   nnhtp = (state->nnhpres)*(state->nhchainlength); 
228
229   if (MASTER(cr)) {
230     bAlloc = FALSE;
231   }
232   if (bAlloc) {
233     state->nalloc = state->natoms;
234   }
235   for(i=0; i<estNR; i++) {
236     if (state->flags & (1<<i)) {
237       switch (i) {
238       case estLAMBDA:  block_bc(cr,state->lambda); break;
239       case estBOX:     block_bc(cr,state->box); break;
240       case estBOX_REL: block_bc(cr,state->box_rel); break;
241       case estBOXV:    block_bc(cr,state->boxv); break;
242       case estPRES_PREV: block_bc(cr,state->pres_prev); break;
243       case estSVIR_PREV: block_bc(cr,state->svir_prev); break;
244       case estFVIR_PREV: block_bc(cr,state->fvir_prev); break;
245       case estNH_XI:   nblock_abc(cr,nnht,state->nosehoover_xi); break;
246       case estNH_VXI:  nblock_abc(cr,nnht,state->nosehoover_vxi); break;
247       case estNHPRES_XI:   nblock_abc(cr,nnhtp,state->nhpres_xi); break;
248       case estNHPRES_VXI:  nblock_abc(cr,nnhtp,state->nhpres_vxi); break;
249       case estTC_INT:  nblock_abc(cr,state->ngtc,state->therm_integral); break;
250       case estVETA:    block_bc(cr,state->veta); break;
251       case estVOL0:    block_bc(cr,state->vol0); break;
252       case estX:       nblock_abc(cr,state->natoms,state->x); break;
253       case estV:       nblock_abc(cr,state->natoms,state->v); break;
254       case estSDX:     nblock_abc(cr,state->natoms,state->sd_X); break;
255       case estCGP:     nblock_abc(cr,state->natoms,state->cg_p); break;
256           case estLD_RNG:  if(state->nrngi == 1) nblock_abc(cr,state->nrng,state->ld_rng); break;
257           case estLD_RNGI: if(state->nrngi == 1) nblock_abc(cr,state->nrngi,state->ld_rngi); break;
258       case estDISRE_INITF: block_bc(cr,state->hist.disre_initf); break;
259       case estDISRE_RM3TAV:
260           block_bc(cr,state->hist.ndisrepairs);
261           nblock_abc(cr,state->hist.ndisrepairs,state->hist.disre_rm3tav);
262           break;
263       case estORIRE_INITF: block_bc(cr,state->hist.orire_initf); break;
264       case estORIRE_DTAV:
265           block_bc(cr,state->hist.norire_Dtav);
266           nblock_abc(cr,state->hist.norire_Dtav,state->hist.orire_Dtav);
267           break;
268       default:
269           gmx_fatal(FARGS,
270                     "Communication is not implemented for %s in bcast_state",
271                     est_names[i]);
272       }
273     }
274   }
275 }
276
277 static void bc_ilists(const t_commrec *cr,t_ilist *ilist)
278 {
279   int ftype;
280
281   /* Here we only communicate the non-zero length ilists */
282   if (MASTER(cr)) {
283     for(ftype=0; ftype<F_NRE; ftype++) {
284       if (ilist[ftype].nr > 0) {
285         block_bc(cr,ftype);
286         block_bc(cr,ilist[ftype].nr);
287         nblock_bc(cr,ilist[ftype].nr,ilist[ftype].iatoms);
288       }
289     }
290     ftype = -1;
291     block_bc(cr,ftype);
292   } else {
293     for(ftype=0; ftype<F_NRE; ftype++) {
294       ilist[ftype].nr = 0;
295     }
296     do {
297       block_bc(cr,ftype);
298       if (ftype >= 0) {
299         block_bc(cr,ilist[ftype].nr);
300         snew_bc(cr,ilist[ftype].iatoms,ilist[ftype].nr);
301         nblock_bc(cr,ilist[ftype].nr,ilist[ftype].iatoms);
302       }
303     } while (ftype >= 0);
304   }
305
306   if (debug) fprintf(debug,"after bc_ilists\n");
307 }
308
309 static void bc_idef(const t_commrec *cr,t_idef *idef)
310 {
311   block_bc(cr,idef->ntypes);
312   block_bc(cr,idef->atnr);
313   snew_bc(cr,idef->functype,idef->ntypes);
314   snew_bc(cr,idef->iparams,idef->ntypes);
315   nblock_bc(cr,idef->ntypes,idef->functype);
316   nblock_bc(cr,idef->ntypes,idef->iparams);
317   block_bc(cr,idef->fudgeQQ);
318   bc_ilists(cr,idef->il);
319   block_bc(cr,idef->ilsort);
320 }
321
322 static void bc_cmap(const t_commrec *cr, gmx_cmap_t *cmap_grid)
323 {
324         int i,j,nelem,ngrid;
325         
326         block_bc(cr,cmap_grid->ngrid);
327         block_bc(cr,cmap_grid->grid_spacing);
328         
329         ngrid = cmap_grid->ngrid;
330         nelem = cmap_grid->grid_spacing * cmap_grid->grid_spacing;
331         
332         if(ngrid>0)
333         {
334                 snew_bc(cr,cmap_grid->cmapdata,ngrid);
335                 
336                 for(i=0;i<ngrid;i++)
337                 {
338                         snew_bc(cr,cmap_grid->cmapdata[i].cmap,4*nelem);
339                         nblock_bc(cr,4*nelem,cmap_grid->cmapdata[i].cmap);
340                 }
341         }
342 }
343
344 static void bc_ffparams(const t_commrec *cr,gmx_ffparams_t *ffp)
345 {
346   int i;
347   
348   block_bc(cr,ffp->ntypes);
349   block_bc(cr,ffp->atnr);
350   snew_bc(cr,ffp->functype,ffp->ntypes);
351   snew_bc(cr,ffp->iparams,ffp->ntypes);
352   nblock_bc(cr,ffp->ntypes,ffp->functype);
353   nblock_bc(cr,ffp->ntypes,ffp->iparams);
354   block_bc(cr,ffp->reppow);
355   block_bc(cr,ffp->fudgeQQ);
356   bc_cmap(cr,&ffp->cmap_grid);
357 }
358
359 static void bc_grpopts(const t_commrec *cr,t_grpopts *g)
360 {
361     int i,n;
362     
363     block_bc(cr,g->ngtc);
364     block_bc(cr,g->ngacc);
365     block_bc(cr,g->ngfrz);
366     block_bc(cr,g->ngener);
367     snew_bc(cr,g->nrdf,g->ngtc);
368     snew_bc(cr,g->tau_t,g->ngtc);
369     snew_bc(cr,g->ref_t,g->ngtc);
370     snew_bc(cr,g->acc,g->ngacc);
371     snew_bc(cr,g->nFreeze,g->ngfrz);
372     snew_bc(cr,g->egp_flags,g->ngener*g->ngener);
373     
374     nblock_bc(cr,g->ngtc,g->nrdf);
375     nblock_bc(cr,g->ngtc,g->tau_t);
376     nblock_bc(cr,g->ngtc,g->ref_t);
377     nblock_bc(cr,g->ngacc,g->acc);
378     nblock_bc(cr,g->ngfrz,g->nFreeze);
379     nblock_bc(cr,g->ngener*g->ngener,g->egp_flags);
380     snew_bc(cr,g->annealing,g->ngtc);
381     snew_bc(cr,g->anneal_npoints,g->ngtc);
382     snew_bc(cr,g->anneal_time,g->ngtc);
383     snew_bc(cr,g->anneal_temp,g->ngtc);
384     nblock_bc(cr,g->ngtc,g->annealing);
385     nblock_bc(cr,g->ngtc,g->anneal_npoints);
386     for(i=0;(i<g->ngtc); i++) {
387         n = g->anneal_npoints[i];
388         if (n > 0) {
389           snew_bc(cr,g->anneal_time[i],n);
390           snew_bc(cr,g->anneal_temp[i],n);
391           nblock_bc(cr,n,g->anneal_time[i]);
392           nblock_bc(cr,n,g->anneal_temp[i]);
393         }
394     }
395     
396     /* QMMM stuff, see inputrec */
397     block_bc(cr,g->ngQM);
398     snew_bc(cr,g->QMmethod,g->ngQM);
399     snew_bc(cr,g->QMbasis,g->ngQM);
400     snew_bc(cr,g->QMcharge,g->ngQM);
401     snew_bc(cr,g->QMmult,g->ngQM);
402     snew_bc(cr,g->bSH,g->ngQM);
403     snew_bc(cr,g->CASorbitals,g->ngQM);
404     snew_bc(cr,g->CASelectrons,g->ngQM);
405     snew_bc(cr,g->SAon,g->ngQM);
406     snew_bc(cr,g->SAoff,g->ngQM);
407     snew_bc(cr,g->SAsteps,g->ngQM);
408     
409     if (g->ngQM)
410     {
411         nblock_bc(cr,g->ngQM,g->QMmethod);
412         nblock_bc(cr,g->ngQM,g->QMbasis);
413         nblock_bc(cr,g->ngQM,g->QMcharge);
414         nblock_bc(cr,g->ngQM,g->QMmult);
415         nblock_bc(cr,g->ngQM,g->bSH);
416         nblock_bc(cr,g->ngQM,g->CASorbitals);
417         nblock_bc(cr,g->ngQM,g->CASelectrons);
418         nblock_bc(cr,g->ngQM,g->SAon);
419         nblock_bc(cr,g->ngQM,g->SAoff);
420         nblock_bc(cr,g->ngQM,g->SAsteps);
421         /* end of QMMM stuff */
422     }
423 }
424
425 static void bc_cosines(const t_commrec *cr,t_cosines *cs)
426 {
427   block_bc(cr,cs->n);
428   snew_bc(cr,cs->a,cs->n);
429   snew_bc(cr,cs->phi,cs->n);
430   if (cs->n > 0) {
431     nblock_bc(cr,cs->n,cs->a);
432     nblock_bc(cr,cs->n,cs->phi);
433   }
434 }
435
436 static void bc_pullgrp(const t_commrec *cr,t_pullgrp *pgrp)
437 {
438   block_bc(cr,*pgrp);
439   if (pgrp->nat > 0) {
440     snew_bc(cr,pgrp->ind,pgrp->nat);
441     nblock_bc(cr,pgrp->nat,pgrp->ind);
442   }
443   if (pgrp->nweight > 0) {
444     snew_bc(cr,pgrp->weight,pgrp->nweight);
445     nblock_bc(cr,pgrp->nweight,pgrp->weight);
446   }
447 }
448
449 static void bc_pull(const t_commrec *cr,t_pull *pull)
450 {
451   int g;
452
453   block_bc(cr,*pull);
454   snew_bc(cr,pull->grp,pull->ngrp+1);
455   for(g=0; g<pull->ngrp+1; g++)
456   {
457       bc_pullgrp(cr,&pull->grp[g]);
458   }
459 }
460
461 static void bc_inputrec(const t_commrec *cr,t_inputrec *inputrec)
462 {
463   bool bAlloc=TRUE;
464   int i;
465   
466   block_bc(cr,*inputrec);
467   snew_bc(cr,inputrec->flambda,inputrec->n_flambda);
468   nblock_bc(cr,inputrec->n_flambda,inputrec->flambda);
469   bc_grpopts(cr,&(inputrec->opts));
470   if (inputrec->ePull != epullNO) {
471     snew_bc(cr,inputrec->pull,1);
472     bc_pull(cr,inputrec->pull);
473   }
474   for(i=0; (i<DIM); i++) {
475     bc_cosines(cr,&(inputrec->ex[i]));
476     bc_cosines(cr,&(inputrec->et[i]));
477   }
478 }
479
480 static void bc_moltype(const t_commrec *cr,t_symtab *symtab,
481                        gmx_moltype_t *moltype)
482 {
483   bc_string(cr,symtab,&moltype->name);
484   bc_atoms(cr,symtab,&moltype->atoms);
485   if (debug) fprintf(debug,"after bc_atoms\n");
486
487   bc_ilists(cr,moltype->ilist);
488   bc_block(cr,&moltype->cgs);
489   bc_blocka(cr,&moltype->excls);
490 }
491
492 static void bc_molblock(const t_commrec *cr,gmx_molblock_t *molb)
493 {
494   bool bAlloc=TRUE;
495   
496   block_bc(cr,molb->type);
497   block_bc(cr,molb->nmol);
498   block_bc(cr,molb->natoms_mol);
499   block_bc(cr,molb->nposres_xA);
500   if (molb->nposres_xA > 0) {
501     snew_bc(cr,molb->posres_xA,molb->nposres_xA);
502     nblock_bc(cr,molb->nposres_xA*DIM,molb->posres_xA[0]);
503   }
504   block_bc(cr,molb->nposres_xB);
505   if (molb->nposres_xB > 0) {
506     snew_bc(cr,molb->posres_xB,molb->nposres_xB);
507     nblock_bc(cr,molb->nposres_xB*DIM,molb->posres_xB[0]);
508   }
509   if (debug) fprintf(debug,"after bc_molblock\n");
510 }
511
512 static void bc_atomtypes(const t_commrec *cr, t_atomtypes *atomtypes)
513 {
514   int nr;
515
516   block_bc(cr,atomtypes->nr);
517
518   nr = atomtypes->nr;
519
520   snew_bc(cr,atomtypes->radius,nr);
521   snew_bc(cr,atomtypes->vol,nr);
522   snew_bc(cr,atomtypes->surftens,nr);
523   snew_bc(cr,atomtypes->gb_radius,nr);
524   snew_bc(cr,atomtypes->S_hct,nr);
525
526   nblock_bc(cr,nr,atomtypes->radius);
527   nblock_bc(cr,nr,atomtypes->vol);
528   nblock_bc(cr,nr,atomtypes->surftens);
529   nblock_bc(cr,nr,atomtypes->gb_radius);
530   nblock_bc(cr,nr,atomtypes->S_hct);
531 }
532
533
534 void bcast_ir_mtop(const t_commrec *cr,t_inputrec *inputrec,gmx_mtop_t *mtop)
535 {
536   int i; 
537   if (debug) fprintf(debug,"in bc_data\n");
538   bc_inputrec(cr,inputrec);
539   if (debug) fprintf(debug,"after bc_inputrec\n");
540   bc_symtab(cr,&mtop->symtab);
541   if (debug) fprintf(debug,"after bc_symtab\n");
542   bc_string(cr,&mtop->symtab,&mtop->name);
543   if (debug) fprintf(debug,"after bc_name\n");
544
545   bc_ffparams(cr,&mtop->ffparams);
546
547   block_bc(cr,mtop->nmoltype);
548   snew_bc(cr,mtop->moltype,mtop->nmoltype);
549   for(i=0; i<mtop->nmoltype; i++) {
550     bc_moltype(cr,&mtop->symtab,&mtop->moltype[i]);
551   }
552
553   block_bc(cr,mtop->nmolblock);
554   snew_bc(cr,mtop->molblock,mtop->nmolblock);
555   for(i=0; i<mtop->nmolblock; i++) {
556     bc_molblock(cr,&mtop->molblock[i]);
557   }
558
559   block_bc(cr,mtop->natoms);
560
561   bc_atomtypes(cr,&mtop->atomtypes);
562
563   bc_block(cr,&mtop->mols);
564   bc_groups(cr,&mtop->symtab,mtop->natoms,&mtop->groups);
565 }