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