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