9db70030d29f742ca5a50c15269a9ba18bbf6c28
[alexxy/gromacs.git] / src / gromacs / gmxlib / mvdata.c
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5  * Copyright (c) 2001-2004, The GROMACS development team.
6  * Copyright (c) 2013,2014, by the GROMACS development team, led by
7  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8  * and including many others, as listed in the AUTHORS file in the
9  * top-level source directory and at http://www.gromacs.org.
10  *
11  * GROMACS is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public License
13  * as published by the Free Software Foundation; either version 2.1
14  * of the License, or (at your option) any later version.
15  *
16  * GROMACS is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with GROMACS; if not, see
23  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
25  *
26  * If you want to redistribute modifications to GROMACS, please
27  * consider that scientific software is very special. Version
28  * control is crucial - bugs must be traceable. We will be happy to
29  * consider code for inclusion in the official distribution, but
30  * derived work must not be called official GROMACS. Details are found
31  * in the README & COPYING files - if they are missing, get the
32  * official version at http://www.gromacs.org.
33  *
34  * To help us fund GROMACS development, we humbly ask that you cite
35  * the research papers on the package. Check out http://www.gromacs.org.
36  */
37 /* This file is completely threadsafe - keep it that way! */
38 #include "gmxpre.h"
39
40 #include "config.h"
41
42 #include <string.h>
43
44 #include "gromacs/legacyheaders/typedefs.h"
45 #include "gromacs/legacyheaders/main.h"
46 #include "gromacs/legacyheaders/mvdata.h"
47 #include "gromacs/legacyheaders/types/commrec.h"
48 #include "gromacs/legacyheaders/network.h"
49 #include "gromacs/math/vec.h"
50 #include "gromacs/legacyheaders/tgroup.h"
51
52 #include "gromacs/topology/symtab.h"
53 #include "gromacs/utility/fatalerror.h"
54 #include "gromacs/utility/smalloc.h"
55
56 #define   block_bc(cr,   d) gmx_bcast(     sizeof(d),     &(d), (cr))
57 /* Probably the test for (nr) > 0 in the next macro is only needed
58  * on BlueGene(/L), where IBM's MPI_Bcast will segfault after
59  * dereferencing a null pointer, even when no data is to be transferred. */
60 #define  nblock_bc(cr, nr, d) { if ((nr) > 0) {gmx_bcast((nr)*sizeof((d)[0]), (d), (cr)); }}
61 #define    snew_bc(cr, d, nr) { if (!MASTER(cr)) {snew((d), (nr)); }}
62 /* Dirty macro with bAlloc not as an argument */
63 #define nblock_abc(cr, nr, d) { if (bAlloc) {snew((d), (nr)); } nblock_bc(cr, (nr), (d)); }
64
65 static void bc_string(const t_commrec *cr, t_symtab *symtab, char ***s)
66 {
67     int handle;
68
69     if (MASTER(cr))
70     {
71         handle = lookup_symtab(symtab, *s);
72     }
73     block_bc(cr, handle);
74     if (!MASTER(cr))
75     {
76         *s = get_symtab_handle(symtab, handle);
77     }
78 }
79
80 static void bc_strings(const t_commrec *cr, t_symtab *symtab, int nr, char ****nm)
81 {
82     int     i;
83     int    *handle;
84     char ***NM;
85
86     snew(handle, nr);
87     if (MASTER(cr))
88     {
89         NM = *nm;
90         for (i = 0; (i < nr); i++)
91         {
92             handle[i] = lookup_symtab(symtab, NM[i]);
93         }
94     }
95     nblock_bc(cr, nr, handle);
96
97     if (!MASTER(cr))
98     {
99         snew_bc(cr, *nm, nr);
100         NM = *nm;
101         for (i = 0; (i < nr); i++)
102         {
103             (*nm)[i] = get_symtab_handle(symtab, handle[i]);
104         }
105     }
106     sfree(handle);
107 }
108
109 static void bc_strings_resinfo(const t_commrec *cr, t_symtab *symtab,
110                                int nr, t_resinfo *resinfo)
111 {
112     int   i;
113     int  *handle;
114
115     snew(handle, nr);
116     if (MASTER(cr))
117     {
118         for (i = 0; (i < nr); i++)
119         {
120             handle[i] = lookup_symtab(symtab, resinfo[i].name);
121         }
122     }
123     nblock_bc(cr, nr, handle);
124
125     if (!MASTER(cr))
126     {
127         for (i = 0; (i < nr); i++)
128         {
129             resinfo[i].name = get_symtab_handle(symtab, handle[i]);
130         }
131     }
132     sfree(handle);
133 }
134
135 static void bc_symtab(const t_commrec *cr, t_symtab *symtab)
136 {
137     int       i, nr, len;
138     t_symbuf *symbuf;
139
140     block_bc(cr, symtab->nr);
141     nr = symtab->nr;
142     snew_bc(cr, symtab->symbuf, 1);
143     symbuf          = symtab->symbuf;
144     symbuf->bufsize = nr;
145     snew_bc(cr, symbuf->buf, nr);
146     for (i = 0; i < nr; i++)
147     {
148         if (MASTER(cr))
149         {
150             len = strlen(symbuf->buf[i]) + 1;
151         }
152         block_bc(cr, len);
153         snew_bc(cr, symbuf->buf[i], len);
154         nblock_bc(cr, len, symbuf->buf[i]);
155     }
156 }
157
158 static void bc_block(const t_commrec *cr, t_block *block)
159 {
160     block_bc(cr, block->nr);
161     snew_bc(cr, block->index, block->nr+1);
162     nblock_bc(cr, block->nr+1, block->index);
163 }
164
165 static void bc_blocka(const t_commrec *cr, t_blocka *block)
166 {
167     block_bc(cr, block->nr);
168     snew_bc(cr, block->index, block->nr+1);
169     nblock_bc(cr, block->nr+1, block->index);
170     block_bc(cr, block->nra);
171     if (block->nra)
172     {
173         snew_bc(cr, block->a, block->nra);
174         nblock_bc(cr, block->nra, block->a);
175     }
176 }
177
178 static void bc_grps(const t_commrec *cr, t_grps grps[])
179 {
180     int i;
181
182     for (i = 0; (i < egcNR); i++)
183     {
184         block_bc(cr, grps[i].nr);
185         snew_bc(cr, grps[i].nm_ind, grps[i].nr);
186         nblock_bc(cr, grps[i].nr, grps[i].nm_ind);
187     }
188 }
189
190 static void bc_atoms(const t_commrec *cr, t_symtab *symtab, t_atoms *atoms)
191 {
192     int dummy;
193
194     block_bc(cr, atoms->nr);
195     snew_bc(cr, atoms->atom, atoms->nr);
196     nblock_bc(cr, atoms->nr, atoms->atom);
197     bc_strings(cr, symtab, atoms->nr, &atoms->atomname);
198     block_bc(cr, atoms->nres);
199     snew_bc(cr, atoms->resinfo, atoms->nres);
200     nblock_bc(cr, atoms->nres, atoms->resinfo);
201     bc_strings_resinfo(cr, symtab, atoms->nres, atoms->resinfo);
202     /* QMMM requires atomtypes to be known on all nodes as well */
203     bc_strings(cr, symtab, atoms->nr, &atoms->atomtype);
204     bc_strings(cr, symtab, atoms->nr, &atoms->atomtypeB);
205 }
206
207 static void bc_groups(const t_commrec *cr, t_symtab *symtab,
208                       int natoms, gmx_groups_t *groups)
209 {
210     int dummy;
211     int g, n;
212
213     bc_grps(cr, groups->grps);
214     block_bc(cr, groups->ngrpname);
215     bc_strings(cr, symtab, groups->ngrpname, &groups->grpname);
216     for (g = 0; g < egcNR; g++)
217     {
218         if (MASTER(cr))
219         {
220             if (groups->grpnr[g])
221             {
222                 n = natoms;
223             }
224             else
225             {
226                 n = 0;
227             }
228         }
229         block_bc(cr, n);
230         if (n == 0)
231         {
232             groups->grpnr[g] = NULL;
233         }
234         else
235         {
236             snew_bc(cr, groups->grpnr[g], n);
237             nblock_bc(cr, n, groups->grpnr[g]);
238         }
239     }
240     if (debug)
241     {
242         fprintf(debug, "after bc_groups\n");
243     }
244 }
245
246 void bcast_state(const t_commrec *cr, t_state *state)
247 {
248     int      i, nnht, nnhtp;
249     gmx_bool bAlloc;
250
251     if (!PAR(cr))
252     {
253         return;
254     }
255
256     /* Broadcasts the state sizes and flags from the master to all nodes
257      * in cr->mpi_comm_mygroup. The arrays are not broadcasted. */
258     block_bc(cr, state->natoms);
259     block_bc(cr, state->ngtc);
260     block_bc(cr, state->nnhpres);
261     block_bc(cr, state->nhchainlength);
262     block_bc(cr, state->flags);
263     if (state->lambda == NULL)
264     {
265         snew_bc(cr, state->lambda, efptNR)
266     }
267
268     if (cr->dd)
269     {
270         /* We allocate dynamically in dd_partition_system. */
271         return;
272     }
273     /* The code below is reachable only by TPI and NM, so it is not
274        tested by anything. */
275
276     nnht  = (state->ngtc)*(state->nhchainlength);
277     nnhtp = (state->nnhpres)*(state->nhchainlength);
278
279     /* We still need to allocate the arrays in state for non-master
280      * ranks, which is done (implicitly via bAlloc) in the dirty,
281      * dirty nblock_abc macro. */
282     bAlloc = !MASTER(cr);
283     if (bAlloc)
284     {
285         state->nalloc = state->natoms;
286     }
287     for (i = 0; i < estNR; i++)
288     {
289         if (state->flags & (1<<i))
290         {
291             switch (i)
292             {
293                 case estLAMBDA:  nblock_bc(cr, efptNR, state->lambda); break;
294                 case estFEPSTATE: block_bc(cr, state->fep_state); break;
295                 case estBOX:     block_bc(cr, state->box); break;
296                 case estBOX_REL: block_bc(cr, state->box_rel); break;
297                 case estBOXV:    block_bc(cr, state->boxv); break;
298                 case estPRES_PREV: block_bc(cr, state->pres_prev); break;
299                 case estSVIR_PREV: block_bc(cr, state->svir_prev); break;
300                 case estFVIR_PREV: block_bc(cr, state->fvir_prev); break;
301                 case estNH_XI:   nblock_abc(cr, nnht, state->nosehoover_xi); break;
302                 case estNH_VXI:  nblock_abc(cr, nnht, state->nosehoover_vxi); break;
303                 case estNHPRES_XI:   nblock_abc(cr, nnhtp, state->nhpres_xi); break;
304                 case estNHPRES_VXI:  nblock_abc(cr, nnhtp, state->nhpres_vxi); break;
305                 case estTC_INT:  nblock_abc(cr, state->ngtc, state->therm_integral); break;
306                 case estVETA:    block_bc(cr, state->veta); break;
307                 case estVOL0:    block_bc(cr, state->vol0); break;
308                 case estX:       nblock_abc(cr, state->natoms, state->x); break;
309                 case estV:       nblock_abc(cr, state->natoms, state->v); break;
310                 case estSDX:     nblock_abc(cr, state->natoms, state->sd_X); break;
311                 case estCGP:     nblock_abc(cr, state->natoms, state->cg_p); break;
312                 case estDISRE_INITF: block_bc(cr, state->hist.disre_initf); break;
313                 case estDISRE_RM3TAV:
314                     block_bc(cr, state->hist.ndisrepairs);
315                     nblock_abc(cr, state->hist.ndisrepairs, state->hist.disre_rm3tav);
316                     break;
317                 case estORIRE_INITF: block_bc(cr, state->hist.orire_initf); break;
318                 case estORIRE_DTAV:
319                     block_bc(cr, state->hist.norire_Dtav);
320                     nblock_abc(cr, state->hist.norire_Dtav, state->hist.orire_Dtav);
321                     break;
322                 default:
323                     gmx_fatal(FARGS,
324                               "Communication is not implemented for %s in bcast_state",
325                               est_names[i]);
326             }
327         }
328     }
329 }
330
331 static void bc_ilists(const t_commrec *cr, t_ilist *ilist)
332 {
333     int ftype;
334
335     /* Here we only communicate the non-zero length ilists */
336     if (MASTER(cr))
337     {
338         for (ftype = 0; ftype < F_NRE; ftype++)
339         {
340             if (ilist[ftype].nr > 0)
341             {
342                 block_bc(cr, ftype);
343                 block_bc(cr, ilist[ftype].nr);
344                 nblock_bc(cr, ilist[ftype].nr, ilist[ftype].iatoms);
345             }
346         }
347         ftype = -1;
348         block_bc(cr, ftype);
349     }
350     else
351     {
352         for (ftype = 0; ftype < F_NRE; ftype++)
353         {
354             ilist[ftype].nr = 0;
355         }
356         do
357         {
358             block_bc(cr, ftype);
359             if (ftype >= 0)
360             {
361                 block_bc(cr, ilist[ftype].nr);
362                 snew_bc(cr, ilist[ftype].iatoms, ilist[ftype].nr);
363                 nblock_bc(cr, ilist[ftype].nr, ilist[ftype].iatoms);
364             }
365         }
366         while (ftype >= 0);
367     }
368
369     if (debug)
370     {
371         fprintf(debug, "after bc_ilists\n");
372     }
373 }
374
375 static void bc_cmap(const t_commrec *cr, gmx_cmap_t *cmap_grid)
376 {
377     int i, j, nelem, ngrid;
378
379     block_bc(cr, cmap_grid->ngrid);
380     block_bc(cr, cmap_grid->grid_spacing);
381
382     ngrid = cmap_grid->ngrid;
383     nelem = cmap_grid->grid_spacing * cmap_grid->grid_spacing;
384
385     if (ngrid > 0)
386     {
387         snew_bc(cr, cmap_grid->cmapdata, ngrid);
388
389         for (i = 0; i < ngrid; i++)
390         {
391             snew_bc(cr, cmap_grid->cmapdata[i].cmap, 4*nelem);
392             nblock_bc(cr, 4*nelem, cmap_grid->cmapdata[i].cmap);
393         }
394     }
395 }
396
397 static void bc_ffparams(const t_commrec *cr, gmx_ffparams_t *ffp)
398 {
399     int i;
400
401     block_bc(cr, ffp->ntypes);
402     block_bc(cr, ffp->atnr);
403     snew_bc(cr, ffp->functype, ffp->ntypes);
404     snew_bc(cr, ffp->iparams, ffp->ntypes);
405     nblock_bc(cr, ffp->ntypes, ffp->functype);
406     nblock_bc(cr, ffp->ntypes, ffp->iparams);
407     block_bc(cr, ffp->reppow);
408     block_bc(cr, ffp->fudgeQQ);
409     bc_cmap(cr, &ffp->cmap_grid);
410 }
411
412 static void bc_grpopts(const t_commrec *cr, t_grpopts *g)
413 {
414     int i, n;
415
416     block_bc(cr, g->ngtc);
417     block_bc(cr, g->ngacc);
418     block_bc(cr, g->ngfrz);
419     block_bc(cr, g->ngener);
420     snew_bc(cr, g->nrdf, g->ngtc);
421     snew_bc(cr, g->tau_t, g->ngtc);
422     snew_bc(cr, g->ref_t, g->ngtc);
423     snew_bc(cr, g->acc, g->ngacc);
424     snew_bc(cr, g->nFreeze, g->ngfrz);
425     snew_bc(cr, g->egp_flags, g->ngener*g->ngener);
426
427     nblock_bc(cr, g->ngtc, g->nrdf);
428     nblock_bc(cr, g->ngtc, g->tau_t);
429     nblock_bc(cr, g->ngtc, g->ref_t);
430     nblock_bc(cr, g->ngacc, g->acc);
431     nblock_bc(cr, g->ngfrz, g->nFreeze);
432     nblock_bc(cr, g->ngener*g->ngener, g->egp_flags);
433     snew_bc(cr, g->annealing, g->ngtc);
434     snew_bc(cr, g->anneal_npoints, g->ngtc);
435     snew_bc(cr, g->anneal_time, g->ngtc);
436     snew_bc(cr, g->anneal_temp, g->ngtc);
437     nblock_bc(cr, g->ngtc, g->annealing);
438     nblock_bc(cr, g->ngtc, g->anneal_npoints);
439     for (i = 0; (i < g->ngtc); i++)
440     {
441         n = g->anneal_npoints[i];
442         if (n > 0)
443         {
444             snew_bc(cr, g->anneal_time[i], n);
445             snew_bc(cr, g->anneal_temp[i], n);
446             nblock_bc(cr, n, g->anneal_time[i]);
447             nblock_bc(cr, n, g->anneal_temp[i]);
448         }
449     }
450
451     /* QMMM stuff, see inputrec */
452     block_bc(cr, g->ngQM);
453     snew_bc(cr, g->QMmethod, g->ngQM);
454     snew_bc(cr, g->QMbasis, g->ngQM);
455     snew_bc(cr, g->QMcharge, g->ngQM);
456     snew_bc(cr, g->QMmult, g->ngQM);
457     snew_bc(cr, g->bSH, g->ngQM);
458     snew_bc(cr, g->CASorbitals, g->ngQM);
459     snew_bc(cr, g->CASelectrons, g->ngQM);
460     snew_bc(cr, g->SAon, g->ngQM);
461     snew_bc(cr, g->SAoff, g->ngQM);
462     snew_bc(cr, g->SAsteps, g->ngQM);
463
464     if (g->ngQM)
465     {
466         nblock_bc(cr, g->ngQM, g->QMmethod);
467         nblock_bc(cr, g->ngQM, g->QMbasis);
468         nblock_bc(cr, g->ngQM, g->QMcharge);
469         nblock_bc(cr, g->ngQM, g->QMmult);
470         nblock_bc(cr, g->ngQM, g->bSH);
471         nblock_bc(cr, g->ngQM, g->CASorbitals);
472         nblock_bc(cr, g->ngQM, g->CASelectrons);
473         nblock_bc(cr, g->ngQM, g->SAon);
474         nblock_bc(cr, g->ngQM, g->SAoff);
475         nblock_bc(cr, g->ngQM, g->SAsteps);
476         /* end of QMMM stuff */
477     }
478 }
479
480 static void bc_cosines(const t_commrec *cr, t_cosines *cs)
481 {
482     block_bc(cr, cs->n);
483     snew_bc(cr, cs->a, cs->n);
484     snew_bc(cr, cs->phi, cs->n);
485     if (cs->n > 0)
486     {
487         nblock_bc(cr, cs->n, cs->a);
488         nblock_bc(cr, cs->n, cs->phi);
489     }
490 }
491
492 static void bc_pull_group(const t_commrec *cr, t_pull_group *pgrp)
493 {
494     block_bc(cr, *pgrp);
495     if (pgrp->nat > 0)
496     {
497         snew_bc(cr, pgrp->ind, pgrp->nat);
498         nblock_bc(cr, pgrp->nat, pgrp->ind);
499     }
500     if (pgrp->nweight > 0)
501     {
502         snew_bc(cr, pgrp->weight, pgrp->nweight);
503         nblock_bc(cr, pgrp->nweight, pgrp->weight);
504     }
505 }
506
507 static void bc_pull(const t_commrec *cr, t_pull *pull)
508 {
509     int g;
510
511     block_bc(cr, *pull);
512     snew_bc(cr, pull->group, pull->ngroup);
513     for (g = 0; g < pull->ngroup; g++)
514     {
515         bc_pull_group(cr, &pull->group[g]);
516     }
517     snew_bc(cr, pull->coord, pull->ncoord);
518     nblock_bc(cr, pull->ncoord, pull->coord);
519 }
520
521 static void bc_rotgrp(const t_commrec *cr, t_rotgrp *rotg)
522 {
523     block_bc(cr, *rotg);
524     if (rotg->nat > 0)
525     {
526         snew_bc(cr, rotg->ind, rotg->nat);
527         nblock_bc(cr, rotg->nat, rotg->ind);
528         snew_bc(cr, rotg->x_ref, rotg->nat);
529         nblock_bc(cr, rotg->nat, rotg->x_ref);
530     }
531 }
532
533 static void bc_rot(const t_commrec *cr, t_rot *rot)
534 {
535     int g;
536
537     block_bc(cr, *rot);
538     snew_bc(cr, rot->grp, rot->ngrp);
539     for (g = 0; g < rot->ngrp; g++)
540     {
541         bc_rotgrp(cr, &rot->grp[g]);
542     }
543 }
544
545 static void bc_adress(const t_commrec *cr, t_adress *adress)
546 {
547     block_bc(cr, *adress);
548     if (adress->n_tf_grps > 0)
549     {
550         snew_bc(cr, adress->tf_table_index, adress->n_tf_grps);
551         nblock_bc(cr, adress->n_tf_grps, adress->tf_table_index);
552     }
553     if (adress->n_energy_grps > 0)
554     {
555         snew_bc(cr, adress->group_explicit, adress->n_energy_grps);
556         nblock_bc(cr, adress->n_energy_grps, adress->group_explicit);
557     }
558 }
559
560 static void bc_imd(const t_commrec *cr, t_IMD *imd)
561 {
562     int g;
563
564     block_bc(cr, *imd);
565     snew_bc(cr, imd->ind, imd->nat);
566     nblock_bc(cr, imd->nat, imd->ind);
567 }
568
569 static void bc_fepvals(const t_commrec *cr, t_lambda *fep)
570 {
571     gmx_bool bAlloc = TRUE;
572     int      i;
573
574     block_bc(cr, fep->nstdhdl);
575     block_bc(cr, fep->init_lambda);
576     block_bc(cr, fep->init_fep_state);
577     block_bc(cr, fep->delta_lambda);
578     block_bc(cr, fep->edHdLPrintEnergy);
579     block_bc(cr, fep->n_lambda);
580     if (fep->n_lambda > 0)
581     {
582         snew_bc(cr, fep->all_lambda, efptNR);
583         nblock_bc(cr, efptNR, fep->all_lambda);
584         for (i = 0; i < efptNR; i++)
585         {
586             snew_bc(cr, fep->all_lambda[i], fep->n_lambda);
587             nblock_bc(cr, fep->n_lambda, fep->all_lambda[i]);
588         }
589     }
590     block_bc(cr, fep->sc_alpha);
591     block_bc(cr, fep->sc_power);
592     block_bc(cr, fep->sc_r_power);
593     block_bc(cr, fep->sc_sigma);
594     block_bc(cr, fep->sc_sigma_min);
595     block_bc(cr, fep->bScCoul);
596     nblock_bc(cr, efptNR, &(fep->separate_dvdl[0]));
597     block_bc(cr, fep->dhdl_derivatives);
598     block_bc(cr, fep->dh_hist_size);
599     block_bc(cr, fep->dh_hist_spacing);
600     if (debug)
601     {
602         fprintf(debug, "after bc_fepvals\n");
603     }
604 }
605
606 static void bc_expandedvals(const t_commrec *cr, t_expanded *expand, int n_lambda)
607 {
608     gmx_bool bAlloc = TRUE;
609     int      i;
610
611     block_bc(cr, expand->nstexpanded);
612     block_bc(cr, expand->elamstats);
613     block_bc(cr, expand->elmcmove);
614     block_bc(cr, expand->elmceq);
615     block_bc(cr, expand->equil_n_at_lam);
616     block_bc(cr, expand->equil_wl_delta);
617     block_bc(cr, expand->equil_ratio);
618     block_bc(cr, expand->equil_steps);
619     block_bc(cr, expand->equil_samples);
620     block_bc(cr, expand->lmc_seed);
621     block_bc(cr, expand->minvar);
622     block_bc(cr, expand->minvar_const);
623     block_bc(cr, expand->c_range);
624     block_bc(cr, expand->bSymmetrizedTMatrix);
625     block_bc(cr, expand->nstTij);
626     block_bc(cr, expand->lmc_repeats);
627     block_bc(cr, expand->lmc_forced_nstart);
628     block_bc(cr, expand->gibbsdeltalam);
629     block_bc(cr, expand->wl_scale);
630     block_bc(cr, expand->wl_ratio);
631     block_bc(cr, expand->init_wl_delta);
632     block_bc(cr, expand->bInit_weights);
633     snew_bc(cr, expand->init_lambda_weights, n_lambda);
634     nblock_bc(cr, n_lambda, expand->init_lambda_weights);
635     block_bc(cr, expand->mc_temp);
636     if (debug)
637     {
638         fprintf(debug, "after bc_expandedvals\n");
639     }
640 }
641
642 static void bc_simtempvals(const t_commrec *cr, t_simtemp *simtemp, int n_lambda)
643 {
644     gmx_bool bAlloc = TRUE;
645     int      i;
646
647     block_bc(cr, simtemp->simtemp_low);
648     block_bc(cr, simtemp->simtemp_high);
649     block_bc(cr, simtemp->eSimTempScale);
650     snew_bc(cr, simtemp->temperatures, n_lambda);
651     nblock_bc(cr, n_lambda, simtemp->temperatures);
652     if (debug)
653     {
654         fprintf(debug, "after bc_simtempvals\n");
655     }
656 }
657
658
659 static void bc_swapions(const t_commrec *cr, t_swapcoords *swap)
660 {
661     int i;
662
663
664     block_bc(cr, *swap);
665
666     /* Broadcast ion group atom indices */
667     snew_bc(cr, swap->ind, swap->nat);
668     nblock_bc(cr, swap->nat, swap->ind);
669
670     /* Broadcast split groups atom indices */
671     for (i = 0; i < 2; i++)
672     {
673         snew_bc(cr, swap->ind_split[i], swap->nat_split[i]);
674         nblock_bc(cr, swap->nat_split[i], swap->ind_split[i]);
675     }
676
677     /* Broadcast solvent group atom indices */
678     snew_bc(cr, swap->ind_sol, swap->nat_sol);
679     nblock_bc(cr, swap->nat_sol, swap->ind_sol);
680 }
681
682
683 static void bc_inputrec(const t_commrec *cr, t_inputrec *inputrec)
684 {
685     gmx_bool bAlloc = TRUE;
686     int      i;
687
688     block_bc(cr, *inputrec);
689
690     bc_grpopts(cr, &(inputrec->opts));
691
692     /* even if efep is efepNO, we need to initialize to make sure that
693      * n_lambda is set to zero */
694
695     snew_bc(cr, inputrec->fepvals, 1);
696     if (inputrec->efep != efepNO || inputrec->bSimTemp)
697     {
698         bc_fepvals(cr, inputrec->fepvals);
699     }
700     /* need to initialize this as well because of data checked for in the logic */
701     snew_bc(cr, inputrec->expandedvals, 1);
702     if (inputrec->bExpanded)
703     {
704         bc_expandedvals(cr, inputrec->expandedvals, inputrec->fepvals->n_lambda);
705     }
706     snew_bc(cr, inputrec->simtempvals, 1);
707     if (inputrec->bSimTemp)
708     {
709         bc_simtempvals(cr, inputrec->simtempvals, inputrec->fepvals->n_lambda);
710     }
711     if (inputrec->ePull != epullNO)
712     {
713         snew_bc(cr, inputrec->pull, 1);
714         bc_pull(cr, inputrec->pull);
715     }
716     if (inputrec->bRot)
717     {
718         snew_bc(cr, inputrec->rot, 1);
719         bc_rot(cr, inputrec->rot);
720     }
721     if (inputrec->bIMD)
722     {
723         snew_bc(cr, inputrec->imd, 1);
724         bc_imd(cr, inputrec->imd);
725     }
726     for (i = 0; (i < DIM); i++)
727     {
728         bc_cosines(cr, &(inputrec->ex[i]));
729         bc_cosines(cr, &(inputrec->et[i]));
730     }
731     if (inputrec->eSwapCoords != eswapNO)
732     {
733         snew_bc(cr, inputrec->swap, 1);
734         bc_swapions(cr, inputrec->swap);
735     }
736     if (inputrec->bAdress)
737     {
738         snew_bc(cr, inputrec->adress, 1);
739         bc_adress(cr, inputrec->adress);
740     }
741 }
742
743 static void bc_moltype(const t_commrec *cr, t_symtab *symtab,
744                        gmx_moltype_t *moltype)
745 {
746     bc_string(cr, symtab, &moltype->name);
747     bc_atoms(cr, symtab, &moltype->atoms);
748     if (debug)
749     {
750         fprintf(debug, "after bc_atoms\n");
751     }
752
753     bc_ilists(cr, moltype->ilist);
754     bc_block(cr, &moltype->cgs);
755     bc_blocka(cr, &moltype->excls);
756 }
757
758 static void bc_molblock(const t_commrec *cr, gmx_molblock_t *molb)
759 {
760     gmx_bool bAlloc = TRUE;
761
762     block_bc(cr, molb->type);
763     block_bc(cr, molb->nmol);
764     block_bc(cr, molb->natoms_mol);
765     block_bc(cr, molb->nposres_xA);
766     if (molb->nposres_xA > 0)
767     {
768         snew_bc(cr, molb->posres_xA, molb->nposres_xA);
769         nblock_bc(cr, molb->nposres_xA*DIM, molb->posres_xA[0]);
770     }
771     block_bc(cr, molb->nposres_xB);
772     if (molb->nposres_xB > 0)
773     {
774         snew_bc(cr, molb->posres_xB, molb->nposres_xB);
775         nblock_bc(cr, molb->nposres_xB*DIM, molb->posres_xB[0]);
776     }
777     if (debug)
778     {
779         fprintf(debug, "after bc_molblock\n");
780     }
781 }
782
783 static void bc_atomtypes(const t_commrec *cr, t_atomtypes *atomtypes)
784 {
785     int nr;
786
787     block_bc(cr, atomtypes->nr);
788
789     nr = atomtypes->nr;
790
791     snew_bc(cr, atomtypes->radius, nr);
792     snew_bc(cr, atomtypes->vol, nr);
793     snew_bc(cr, atomtypes->surftens, nr);
794     snew_bc(cr, atomtypes->gb_radius, nr);
795     snew_bc(cr, atomtypes->S_hct, nr);
796
797     nblock_bc(cr, nr, atomtypes->radius);
798     nblock_bc(cr, nr, atomtypes->vol);
799     nblock_bc(cr, nr, atomtypes->surftens);
800     nblock_bc(cr, nr, atomtypes->gb_radius);
801     nblock_bc(cr, nr, atomtypes->S_hct);
802 }
803
804
805 void bcast_ir_mtop(const t_commrec *cr, t_inputrec *inputrec, gmx_mtop_t *mtop)
806 {
807     int i;
808     if (debug)
809     {
810         fprintf(debug, "in bc_data\n");
811     }
812     bc_inputrec(cr, inputrec);
813     if (debug)
814     {
815         fprintf(debug, "after bc_inputrec\n");
816     }
817     bc_symtab(cr, &mtop->symtab);
818     if (debug)
819     {
820         fprintf(debug, "after bc_symtab\n");
821     }
822     bc_string(cr, &mtop->symtab, &mtop->name);
823     if (debug)
824     {
825         fprintf(debug, "after bc_name\n");
826     }
827
828     bc_ffparams(cr, &mtop->ffparams);
829
830     block_bc(cr, mtop->nmoltype);
831     snew_bc(cr, mtop->moltype, mtop->nmoltype);
832     for (i = 0; i < mtop->nmoltype; i++)
833     {
834         bc_moltype(cr, &mtop->symtab, &mtop->moltype[i]);
835     }
836
837     block_bc(cr, mtop->nmolblock);
838     snew_bc(cr, mtop->molblock, mtop->nmolblock);
839     for (i = 0; i < mtop->nmolblock; i++)
840     {
841         bc_molblock(cr, &mtop->molblock[i]);
842     }
843
844     block_bc(cr, mtop->natoms);
845
846     bc_atomtypes(cr, &mtop->atomtypes);
847
848     bc_block(cr, &mtop->mols);
849     bc_groups(cr, &mtop->symtab, mtop->natoms, &mtop->groups);
850 }