2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2013,2014,2015,2016,2017 by the GROMACS development team.
5 * Copyright (c) 2018,2019,2020, by the GROMACS development team, led by
6 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
7 * and including many others, as listed in the AUTHORS file in the
8 * top-level source directory and at http://www.gromacs.org.
10 * GROMACS is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public License
12 * as published by the Free Software Foundation; either version 2.1
13 * of the License, or (at your option) any later version.
15 * GROMACS is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with GROMACS; if not, see
22 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
23 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 * If you want to redistribute modifications to GROMACS, please
26 * consider that scientific software is very special. Version
27 * control is crucial - bugs must be traceable. We will be happy to
28 * consider code for inclusion in the official distribution, but
29 * derived work must not be called official GROMACS. Details are found
30 * in the README & COPYING files - if they are missing, get the
31 * official version at http://www.gromacs.org.
33 * To help us fund GROMACS development, we humbly ask that you cite
34 * the research papers on the package. Check out http://www.gromacs.org.
38 * Implements functions in swapcoords.h.
40 * \author Carsten Kutzner <ckutzne@gwdg.de>
41 * \ingroup module_swap
45 #include "swapcoords.h"
55 #include "gromacs/domdec/domdec_struct.h"
56 #include "gromacs/domdec/localatomset.h"
57 #include "gromacs/domdec/localatomsetmanager.h"
58 #include "gromacs/fileio/confio.h"
59 #include "gromacs/fileio/gmxfio.h"
60 #include "gromacs/fileio/xvgr.h"
61 #include "gromacs/gmxlib/network.h"
62 #include "gromacs/math/vec.h"
63 #include "gromacs/mdlib/groupcoord.h"
64 #include "gromacs/mdrunutility/handlerestart.h"
65 #include "gromacs/mdtypes/commrec.h"
66 #include "gromacs/mdtypes/imdmodule.h"
67 #include "gromacs/mdtypes/inputrec.h"
68 #include "gromacs/mdtypes/md_enums.h"
69 #include "gromacs/mdtypes/mdrunoptions.h"
70 #include "gromacs/mdtypes/observableshistory.h"
71 #include "gromacs/mdtypes/state.h"
72 #include "gromacs/mdtypes/swaphistory.h"
73 #include "gromacs/pbcutil/pbc.h"
74 #include "gromacs/timing/wallcycle.h"
75 #include "gromacs/topology/mtop_lookup.h"
76 #include "gromacs/topology/topology.h"
77 #include "gromacs/utility/cstringutil.h"
78 #include "gromacs/utility/fatalerror.h"
79 #include "gromacs/utility/pleasecite.h"
80 #include "gromacs/utility/smalloc.h"
81 #include "gromacs/utility/snprintf.h"
83 static const char* SwS = { "SWAP:" }; /**< For output that comes from the swap module */
84 static const char* SwSEmpty = { " " }; /**< Placeholder for multi-line output */
85 static const char* CompStr[eCompNR] = { "A", "B" }; /**< Compartment name */
86 static const char* SwapStr[eSwapTypesNR + 1] = { "", "X-", "Y-", "Z-", nullptr }; /**< Name for the swap types. */
87 static const char* DimStr[DIM + 1] = { "X", "Y", "Z", nullptr }; /**< Name for the swap dimension. */
89 /** Keep track of through which channel the ions have passed */
97 static const char* ChannelString[eChHistNr] = { "none", "channel0", "channel1" }; /**< Name for the channels */
99 /*! \brief Domain identifier.
101 * Keeps track of from which compartment the ions came before passing the
111 static const char* DomainString[eDomainNr] = { "not_assigned", "Domain_A", "Domain_B" }; /**< Name for the domains */
116 extern template LocalAtomSet LocalAtomSetManager::add<void, void>(ArrayRef<const int> globalAtomIndex);
119 * \brief Implement Computational Electrophysiology swapping.
121 class SwapCoordinates final : public IMDModule
124 IMdpOptionProvider* mdpOptionProvider() override { return nullptr; }
125 IMDOutputProvider* outputProvider() override { return nullptr; }
126 void initForceProviders(ForceProviders* /* forceProviders */) override {}
127 void subscribeToSimulationSetupNotifications(MdModulesNotifier* /* notifier */) override {}
128 void subscribeToPreProcessingNotifications(MdModulesNotifier* /* notifier */) override {}
131 std::unique_ptr<IMDModule> createSwapCoordinatesModule()
133 return std::make_unique<SwapCoordinates>();
140 * Structure containing compartment-specific data.
142 typedef struct swap_compartment
144 int nMol; /**< Number of ion or water molecules detected
145 in this compartment. */
146 int nMolBefore; /**< Number of molecules before swapping. */
147 int nMolReq; /**< Requested number of molecules in compartment. */
148 real nMolAv; /**< Time-averaged number of molecules matching
149 the compartment conditions. */
150 int* nMolPast; /**< Past molecule counts for time-averaging. */
151 int* ind; /**< Indices to collective array of atoms. */
152 real* dist; /**< Distance of atom to bulk layer, which is
153 normally the center layer of the compartment */
154 int nalloc; /**< Allocation size for ind array. */
155 int inflow_net; /**< Net inflow of ions into this compartment. */
160 * This structure contains data needed for the groups involved in swapping:
161 * split group 0, split group 1, solvent group, ion groups.
163 typedef struct swap_group
165 /*!\brief Construct a swap group given the managed swap atoms.
167 * \param[in] atomset Managed indices of atoms that are part of the swap group.
169 swap_group(const gmx::LocalAtomSet& atomset);
170 char* molname = nullptr; /**< Name of the group or ion type */
171 int apm = 0; /**< Number of atoms in each molecule */
172 gmx::LocalAtomSet atomset; /**< The atom indices in the swap group */
173 rvec* xc = nullptr; /**< Collective array of group atom positions (size nat) */
174 ivec* xc_shifts = nullptr; /**< Current (collective) shifts (size nat) */
175 ivec* xc_eshifts = nullptr; /**< Extra shifts since last DD step (size nat) */
176 rvec* xc_old = nullptr; /**< Old (collective) positions (size nat) */
177 real q = 0.; /**< Total charge of one molecule of this group */
178 real* m = nullptr; /**< Masses (can be omitted, size apm) */
179 unsigned char* comp_from = nullptr; /**< (Collective) Stores from which compartment this
180 molecule has come. This way we keep track of
181 through which channel an ion permeates
182 (size nMol = nat/apm) */
183 unsigned char* comp_now = nullptr; /**< In which compartment this ion is now (size nMol) */
184 unsigned char* channel_label = nullptr; /**< Which channel was passed at last by this ion?
186 rvec center; /**< Center of the group; COM if masses are used */
187 t_compartment comp[eCompNR]; /**< Distribution of particles of this group across
188 the two compartments */
189 real vacancy[eCompNR]; /**< How many molecules need to be swapped in? */
190 int fluxfromAtoB[eChanNR]; /**< Net flux of ions per channel */
191 int nCyl[eChanNR]; /**< Number of ions residing in a channel */
192 int nCylBoth = 0; /**< Ions assigned to cyl0 and cyl1. Not good. */
195 t_swapgrp::swap_group(const gmx::LocalAtomSet& atomset) : atomset{ atomset }
200 for (int compartment = eCompA; compartment < eCompNR; ++compartment)
202 comp[compartment] = {};
203 vacancy[compartment] = 0;
205 for (int channel = eChan0; channel < eChanNR; ++channel)
207 fluxfromAtoB[channel] = 0;
213 * Main (private) data structure for the position swapping protocol.
217 int swapdim; /**< One of XX, YY, ZZ */
218 t_pbc* pbc; /**< Needed to make molecules whole. */
219 FILE* fpout; /**< Output file. */
220 int ngrp; /**< Number of t_swapgrp groups */
221 std::vector<t_swapgrp> group; /**< Separate groups for channels, solvent, ions */
222 int fluxleak; /**< Flux not going through any of the channels. */
223 real deltaQ; /**< The charge imbalance between the compartments. */
227 /*! \brief Check whether point is in channel.
229 * A channel is a cylinder defined by a disc
230 * with radius r around its center c. The thickness of the cylinder is
237 * <---------c--------->
243 * \param[in] point The position (xyz) under consideration.
244 * \param[in] center The center of the cylinder.
245 * \param[in] d_up The upper extension of the cylinder.
246 * \param[in] d_down The lower extension.
247 * \param[in] r_cyl2 Cylinder radius squared.
248 * \param[in] pbc Structure with info about periodic boundary conditions.
249 * \param[in] normal The membrane normal direction is typically 3, i.e. z, but can be x or y also.
251 * \returns Whether the point is inside the defined cylindric channel.
253 static gmx_bool is_in_channel(rvec point, rvec center, real d_up, real d_down, real r_cyl2, t_pbc* pbc, int normal)
256 int plane1, plane2; /* Directions tangential to membrane */
259 plane1 = (normal + 1) % 3; /* typically 0, i.e. XX */
260 plane2 = (normal + 2) % 3; /* typically 1, i.e. YY */
262 /* Get the distance vector dr between the point and the center of the cylinder */
263 pbc_dx(pbc, point, center, dr); /* This puts center in the origin */
265 /* Check vertical direction */
266 if ((dr[normal] > d_up) || (dr[normal] < -d_down))
271 /* Check radial direction */
272 if ((dr[plane1] * dr[plane1] + dr[plane2] * dr[plane2]) > r_cyl2)
277 /* All check passed, this point is in the cylinder */
282 /*! \brief Prints output to CompEL output file.
284 * Prints to swap output file how many ions are in each compartment,
285 * where the centers of the split groups are, and how many ions of each type
286 * passed the channels.
288 static void print_ionlist(t_swap* s, double time, const char comment[])
291 fprintf(s->fpout, "%12.5e", time);
293 // Output number of molecules and difference to reference counts for each
294 // compartment and ion type
295 for (int iComp = 0; iComp < eCompNR; iComp++)
297 for (int ig = eSwapFixedGrpNR; ig < s->ngrp; ig++)
299 t_compartment* comp = &s->group[ig].comp[iComp];
301 fprintf(s->fpout, "%10d%10.1f%10d", comp->nMol, comp->nMolAv - comp->nMolReq, comp->inflow_net);
305 // Output center of split groups
308 s->group[eGrpSplit0].center[s->swapdim],
309 s->group[eGrpSplit1].center[s->swapdim]);
311 // Output ion flux for each channel and ion type
312 for (int iChan = 0; iChan < eChanNR; iChan++)
314 for (int ig = eSwapFixedGrpNR; ig < s->ngrp; ig++)
316 t_swapgrp* g = &s->group[ig];
317 fprintf(s->fpout, "%10d", g->fluxfromAtoB[iChan]);
321 /* Output the number of molecules that leaked from A to B */
322 fprintf(s->fpout, "%10d", s->fluxleak);
324 fprintf(s->fpout, "%s\n", comment);
328 /*! \brief Get the center of a group of nat atoms.
330 * Since with PBC an atom group might not be whole, use the first atom as the
331 * reference atom and determine the center with respect to this reference.
333 static void get_molecule_center(rvec x[], int nat, const real* weights, rvec center, t_pbc* pbc)
336 rvec weightedPBCimage;
338 rvec reference, correctPBCimage, dx;
341 /* Use the first atom as the reference and put other atoms near that one */
342 /* This does not work for large molecules that span > half of the box! */
343 copy_rvec(x[0], reference);
345 /* Calculate either the weighted center or simply the center of geometry */
348 for (i = 0; i < nat; i++)
350 /* PBC distance between position and reference */
351 pbc_dx(pbc, x[i], reference, dx);
353 /* Add PBC distance to reference */
354 rvec_add(reference, dx, correctPBCimage);
356 /* Take weight into account */
357 if (nullptr == weights)
366 svmul(wi, correctPBCimage, weightedPBCimage);
369 rvec_inc(center, weightedPBCimage);
373 svmul(1.0 / wsum, center, center);
377 /*! \brief Return TRUE if position x of ion (or water) is found in the compartment,
378 * i.e. between w1 and w2.
380 * One can define and additional offset "b" if one wants to exchange ions/water
381 * to or from a plane not directly in the middle of w1 and w2. The offset can be
382 * in ]-1.0, ..., +1.0 [.
383 * A bulkOffset of 0.0 means 'no offset', so the swap-layer is directly in the
384 * middle between w1 and w2. Offsets -1.0 < b < 0.0 will yield swaps nearer to w1,
385 * whereas offsets 0.0 < 0 < +1.0 will yield swaps nearer to w2.
389 * ||--------------+-------------|-------------+------------------------||
390 * w1 ? ? ? ? ? ? ? ? ? ? ? w2
391 * ||--------------+-------------|----b--------+------------------------||
396 * \param[in] w1 Position of 'wall' atom 1.
397 * \param[in] w2 Position of 'wall' atom 2.
398 * \param[in] x Position of the ion or the water molecule under consideration.
399 * \param[in] l Length of the box, from || to || in the sketch.
400 * \param[in] bulkOffset Where is the bulk layer "b" to be found between w1 and w2?
401 * \param[out] distance_from_b Distance of x to the bulk layer "b".
403 * \returns TRUE if x is between w1 and w2.
405 * Also computes the distance of x to the compartment center (the layer that is
406 * normally situated in the middle of w1 and w2 that would be considered as having
407 * the bulk concentration of ions).
409 static gmx_bool compartment_contains_atom(real w1, real w2, real x, real l, real bulkOffset, real* distance_from_b)
415 /* First set the origin in the middle of w1 and w2 */
422 /* Now choose the PBC image of x that is closest to the origin: */
433 *distance_from_b = static_cast<real>(fabs(x - bulkOffset * 0.5 * width));
435 /* Return TRUE if we now are in area "????" */
436 return (x >= w1) && (x < w2);
440 /*! \brief Updates the time-averaged number of ions in a compartment. */
441 static void update_time_window(t_compartment* comp, int values, int replace)
447 /* Put in the new value */
450 comp->nMolPast[replace] = comp->nMol;
453 /* Compute the new time-average */
455 for (i = 0; i < values; i++)
457 average += comp->nMolPast[i];
460 comp->nMolAv = average;
464 /*! \brief Add the atom with collective index ci to the atom list in compartment 'comp'.
466 * \param[in] ci Index of this ion in the collective xc array.
467 * \param[inout] comp Compartment to add this atom to.
468 * \param[in] distance Shortest distance of this atom to the bulk layer,
469 * from which ion/water pairs are selected for swapping.
471 static void add_to_list(int ci, t_compartment* comp, real distance)
475 if (nr >= comp->nalloc)
477 comp->nalloc = over_alloc_dd(nr + 1);
478 srenew(comp->ind, comp->nalloc);
479 srenew(comp->dist, comp->nalloc);
482 comp->dist[nr] = distance;
487 /*! \brief Determine the compartment boundaries from the channel centers. */
488 static void get_compartment_boundaries(int c, t_swap* s, const matrix box, real* left, real* right)
491 real leftpos, rightpos, leftpos_orig;
496 gmx_fatal(FARGS, "No compartment %c.", c + 'A');
499 pos0 = s->group[eGrpSplit0].center[s->swapdim];
500 pos1 = s->group[eGrpSplit1].center[s->swapdim];
513 /* This gets us the other compartment: */
516 leftpos_orig = leftpos;
518 rightpos = leftpos_orig + box[s->swapdim][s->swapdim];
526 /*! \brief Determine the per-channel ion flux.
528 * To determine the flux through the individual channels, we
529 * remember the compartment and channel history of each ion. An ion can be
530 * either in channel0 or channel1, or in the remaining volume of compartment
534 * +-----------------+
537 * ||||||||||0|||||||| bilayer with channel 0
542 * |||||1||||||||||||| bilayer with channel 1
545 * +-----------------+
549 static void detect_flux_per_channel(t_swapgrp* g,
553 unsigned char* comp_now,
554 unsigned char* comp_from,
555 unsigned char* channel_label,
565 gmx_bool in_cyl0, in_cyl1;
571 /* Check whether ion is inside any of the channels */
572 in_cyl0 = is_in_channel(
573 atomPosition, s->group[eGrpSplit0].center, sc->cyl0u, sc->cyl0l, cyl0_r2, s->pbc, sd);
574 in_cyl1 = is_in_channel(
575 atomPosition, s->group[eGrpSplit1].center, sc->cyl1u, sc->cyl1l, cyl1_r2, s->pbc, sd);
577 if (in_cyl0 && in_cyl1)
579 /* Ion appears to be in both channels. Something is severely wrong! */
581 *comp_now = eDomainNotset;
582 *comp_from = eDomainNotset;
583 *channel_label = eChHistPassedNone;
587 /* Ion is in channel 0 now */
588 *channel_label = eChHistPassedCh0;
589 *comp_now = eDomainNotset;
594 /* Ion is in channel 1 now */
595 *channel_label = eChHistPassedCh1;
596 *comp_now = eDomainNotset;
601 /* Ion is not in any of the channels, so it must be in domain A or B */
604 *comp_now = eDomainA;
608 *comp_now = eDomainB;
612 /* Only take action, if ion is now in domain A or B, and was before
613 * in the other domain!
615 if (eDomainNotset == *comp_from)
617 /* Maybe we can set the domain now */
618 *comp_from = *comp_now; /* Could still be eDomainNotset, though */
620 else if ((*comp_now != eDomainNotset) /* if in channel */
621 && (*comp_from != *comp_now))
623 /* Obviously the ion changed its domain.
624 * Count this for the channel through which it has passed. */
625 switch (*channel_label)
627 case eChHistPassedNone:
631 " %s Warning! Step %s, ion %d moved from %s to %s\n",
633 gmx_step_str(step, buf),
635 DomainString[*comp_from],
636 DomainString[*comp_now]);
639 fprintf(stderr, ", possibly due to a swap in the original simulation.\n");
644 "but did not pass cyl0 or cyl1 as defined in the .mdp file.\n"
645 "Do you have an ion somewhere within the membrane?\n");
646 /* Write this info to the CompEL output file: */
648 " # Warning: step %s, ion %d moved from %s to %s (probably through the "
650 gmx_step_str(step, buf),
652 DomainString[*comp_from],
653 DomainString[*comp_now]);
656 case eChHistPassedCh0:
657 case eChHistPassedCh1:
658 if (*channel_label == eChHistPassedCh0)
667 if (eDomainA == *comp_from)
669 g->fluxfromAtoB[chan_nr]++;
673 g->fluxfromAtoB[chan_nr]--;
675 fprintf(fpout, "# Atom nr. %d finished passing %s.\n", iAtom, ChannelString[*channel_label]);
678 gmx_fatal(FARGS, "%s Unknown channel history entry for ion type '%s'\n", SwS, g->molname);
681 /* This ion has moved to the _other_ compartment ... */
682 *comp_from = *comp_now;
683 /* ... and it did not pass any channel yet */
684 *channel_label = eChHistPassedNone;
689 /*! \brief Determines which ions or solvent molecules are in compartment A and B */
690 static void sortMoleculesIntoCompartments(t_swapgrp* g,
700 int nMolNotInComp[eCompNR]; /* consistency check */
701 real cyl0_r2 = sc->cyl0r * sc->cyl0r;
702 real cyl1_r2 = sc->cyl1r * sc->cyl1r;
704 /* Get us a counter that cycles in the range of [0 ... sc->nAverage[ */
705 int replace = (step / sc->nstswap) % sc->nAverage;
707 for (int comp = eCompA; comp <= eCompB; comp++)
711 /* Get lists of atoms that match criteria for this compartment */
712 get_compartment_boundaries(comp, s, box, &left, &right);
714 /* First clear the ion molecule lists */
715 g->comp[comp].nMol = 0;
716 nMolNotInComp[comp] = 0; /* consistency check */
718 /* Loop over the molecules and atoms of this group */
719 for (int iMol = 0, iAtom = 0; iAtom < static_cast<int>(g->atomset.numAtomsGlobal());
720 iAtom += g->apm, iMol++)
725 /* Is this first atom of the molecule in the compartment that we look at? */
726 if (compartment_contains_atom(
727 left, right, g->xc[iAtom][sd], box[sd][sd], sc->bulkOffset[comp], &dist))
729 /* Add the first atom of this molecule to the list of molecules in this compartment */
730 add_to_list(iAtom, &g->comp[comp], dist);
732 /* Master also checks for ion groups through which channel each ion has passed */
733 if (MASTER(cr) && (g->comp_now != nullptr) && !bIsSolvent)
735 int globalAtomNr = g->atomset.globalIndex()[iAtom] + 1; /* PDB index starts at 1 ... */
736 detect_flux_per_channel(g,
742 &g->channel_label[iMol],
754 nMolNotInComp[comp]++;
757 /* Correct the time-averaged number of ions in the compartment */
760 update_time_window(&g->comp[comp], sc->nAverage, replace);
764 /* Flux detection warnings */
765 if (MASTER(cr) && !bIsSolvent)
771 "%s Warning: %d atoms were detected as being in both channels! Probably your "
773 "%s cylinder is way too large, or one compartment has collapsed (step "
780 fprintf(s->fpout, "Warning: %d atoms were assigned to both channels!\n", g->nCylBoth);
786 if (bIsSolvent && nullptr != fpout)
789 "# Solv. molecules in comp.%s: %d comp.%s: %d\n",
791 g->comp[eCompA].nMol,
793 g->comp[eCompB].nMol);
796 /* Consistency checks */
797 const auto numMolecules = static_cast<int>(g->atomset.numAtomsGlobal() / g->apm);
798 if (nMolNotInComp[eCompA] + nMolNotInComp[eCompB] != numMolecules)
801 "%s Warning: Inconsistency while assigning '%s' molecules to compartments. !inA: "
802 "%d, !inB: %d, total molecules %d\n",
805 nMolNotInComp[eCompA],
806 nMolNotInComp[eCompB],
810 int sum = g->comp[eCompA].nMol + g->comp[eCompB].nMol;
811 if (sum != numMolecules)
814 "%s Warning: %d molecules are in group '%s', but altogether %d have been assigned "
815 "to the compartments.\n",
824 /*! \brief Find out how many group atoms are in the compartments initially */
825 static void get_initial_ioncounts(const t_inputrec* ir,
827 const rvec x[], /* the initial positions */
837 /* Loop over the user-defined (ion) groups */
838 for (int ig = eSwapFixedGrpNR; ig < s->ngrp; ig++)
842 /* Copy the initial positions of the atoms in the group
843 * to the collective array so that we can compartmentalize */
844 for (size_t i = 0; i < g->atomset.numAtomsGlobal(); i++)
846 int ind = g->atomset.globalIndex()[i];
847 copy_rvec(x[ind], g->xc[i]);
850 /* Set up the compartments and get lists of atoms in each compartment */
851 sortMoleculesIntoCompartments(g, cr, sc, s, box, 0, s->fpout, bRerun, FALSE);
853 /* Set initial molecule counts if requested (as signaled by "-1" value) */
854 for (int ic = 0; ic < eCompNR; ic++)
856 int requested = sc->grp[ig].nmolReq[ic];
859 g->comp[ic].nMolReq = g->comp[ic].nMol;
863 g->comp[ic].nMolReq = requested;
867 /* Check whether the number of requested molecules adds up to the total number */
868 int req = g->comp[eCompA].nMolReq + g->comp[eCompB].nMolReq;
869 int tot = g->comp[eCompA].nMol + g->comp[eCompB].nMol;
874 "Mismatch of the number of %s ions summed over both compartments.\n"
875 "You requested a total of %d ions (%d in A and %d in B),\n"
876 "but there are a total of %d ions of this type in the system.\n",
879 g->comp[eCompA].nMolReq,
880 g->comp[eCompB].nMolReq,
884 /* Initialize time-averaging:
885 * Write initial concentrations to all time bins to start with */
886 for (int ic = 0; ic < eCompNR; ic++)
888 g->comp[ic].nMolAv = g->comp[ic].nMol;
889 for (int i = 0; i < sc->nAverage; i++)
891 g->comp[ic].nMolPast[i] = g->comp[ic].nMol;
898 /*! \brief Copy history of ion counts from checkpoint file.
900 * When called, the checkpoint file has already been read in. Here we copy
901 * over the values from .cpt file to the swap data structure.
903 static void get_initial_ioncounts_from_cpt(const t_inputrec* ir,
905 swaphistory_t* swapstate,
917 /* Copy the past values from the checkpoint values that have been read in already */
920 fprintf(stderr, "%s Copying values from checkpoint\n", SwS);
923 for (int ig = eSwapFixedGrpNR; ig < s->ngrp; ig++)
926 gs = &swapstate->ionType[ig - eSwapFixedGrpNR];
928 for (int ic = 0; ic < eCompNR; ic++)
930 g->comp[ic].nMolReq = gs->nMolReq[ic];
931 g->comp[ic].inflow_net = gs->inflow_net[ic];
936 "%s ... Influx netto: %d Requested: %d Past values: ",
938 g->comp[ic].inflow_net,
939 g->comp[ic].nMolReq);
942 for (int j = 0; j < sc->nAverage; j++)
944 g->comp[ic].nMolPast[j] = gs->nMolPast[ic][j];
947 fprintf(stderr, "%d ", g->comp[ic].nMolPast[j]);
952 fprintf(stderr, "\n");
960 /*! \brief The master lets all others know about the initial ion counts. */
961 static void bc_initial_concentrations(t_commrec* cr, t_swapcoords* swap, t_swap* s)
967 for (ig = eSwapFixedGrpNR; ig < s->ngrp; ig++)
971 for (ic = 0; ic < eCompNR; ic++)
973 gmx_bcast(sizeof(g->comp[ic].nMolReq), &(g->comp[ic].nMolReq), cr->mpi_comm_mygroup);
974 gmx_bcast(sizeof(g->comp[ic].nMol), &(g->comp[ic].nMol), cr->mpi_comm_mygroup);
975 gmx_bcast(swap->nAverage * sizeof(g->comp[ic].nMolPast[0]), g->comp[ic].nMolPast, cr->mpi_comm_mygroup);
981 /*! \brief Ensure that each atom belongs to at most one of the swap groups. */
982 static void check_swap_groups(t_swap* s, int nat, gmx_bool bVerbose)
984 int* nGroup = nullptr; /* This array counts for each atom in the MD system to
985 how many swap groups it belongs (should be 0 or 1!) */
987 int nMultiple = 0; /* Number of atoms belonging to multiple groups */
992 fprintf(stderr, "%s Making sure each atom belongs to at most one of the swap groups.\n", SwS);
995 /* Add one to the group count of atoms belonging to a swap group: */
997 for (int i = 0; i < s->ngrp; i++)
999 t_swapgrp* g = &s->group[i];
1000 for (size_t j = 0; j < g->atomset.numAtomsGlobal(); j++)
1002 /* Get the global index of this atom of this group: */
1003 ind = g->atomset.globalIndex()[j];
1007 /* Make sure each atom belongs to at most one of the groups: */
1008 for (int i = 0; i < nat; i++)
1020 "%s Cannot perform swapping since %d atom%s allocated to more than one swap "
1022 "%s Each atom must be allocated to at most one of the split groups, the swap "
1023 "groups, or the solvent.\n"
1024 "%s Check the .mdp file settings regarding the swap index groups or the index "
1025 "groups themselves.\n",
1028 (1 == nMultiple) ? " is" : "s are",
1035 /*! \brief Get the number of atoms per molecule for this group.
1037 * Also ensure that all the molecules in this group have this number of atoms.
1039 static int get_group_apm_check(int igroup, t_swap* s, gmx_bool bVerbose, gmx_mtop_t* mtop)
1041 t_swapgrp* g = &s->group[igroup];
1042 const int* ind = s->group[igroup].atomset.globalIndex().data();
1043 int nat = s->group[igroup].atomset.numAtomsGlobal();
1045 /* Determine the number of solvent atoms per solvent molecule from the
1046 * first solvent atom: */
1048 mtopGetMolblockIndex(mtop, ind[0], &molb, nullptr, nullptr);
1049 int apm = mtop->moleculeBlockIndices[molb].numAtomsPerMolecule;
1054 "%s Checking whether all %s molecules consist of %d atom%s\n",
1058 apm > 1 ? "s" : "");
1061 /* Check whether this is also true for all other solvent atoms */
1062 for (int i = 1; i < nat; i++)
1064 mtopGetMolblockIndex(mtop, ind[i], &molb, nullptr, nullptr);
1065 if (apm != mtop->moleculeBlockIndices[molb].numAtomsPerMolecule)
1067 gmx_fatal(FARGS, "Not all molecules of swap group %d consist of %d atoms.", igroup, apm);
1071 // TODO: check whether charges and masses of each molecule are identical!
1076 /*! \brief Print the legend to the swap output file.
1078 * Also print the initial values of ion counts and position of split groups.
1080 static void print_ionlist_legend(const t_inputrec* ir, t_swap* s, const gmx_output_env_t* oenv)
1082 const char** legend;
1086 int nIonTypes = ir->swap->ngrp - eSwapFixedGrpNR;
1087 snew(legend, eCompNR * nIonTypes * 3 + 2 + eChanNR * nIonTypes + 1);
1089 // Number of molecules and difference to reference counts for each
1090 // compartment and ion type
1091 for (int ic = count = 0; ic < eCompNR; ic++)
1093 for (int ig = eSwapFixedGrpNR; ig < s->ngrp; ig++)
1095 t_swapGroup* g = &ir->swap->grp[ig];
1096 real q = s->group[ig].q;
1098 snprintf(buf, STRLEN, "%s %s ions (charge %s%g)", CompStr[ic], g->molname, q > 0 ? "+" : "", q);
1099 legend[count++] = gmx_strdup(buf);
1103 "%s av. mismatch to %d %s ions",
1105 s->group[ig].comp[ic].nMolReq,
1107 legend[count++] = gmx_strdup(buf);
1109 snprintf(buf, STRLEN, "%s net %s ion influx", CompStr[ic], g->molname);
1110 legend[count++] = gmx_strdup(buf);
1114 // Center of split groups
1117 "%scenter of %s of split group 0",
1118 SwapStr[ir->eSwapCoords],
1119 (nullptr != s->group[eGrpSplit0].m) ? "mass" : "geometry");
1120 legend[count++] = gmx_strdup(buf);
1123 "%scenter of %s of split group 1",
1124 SwapStr[ir->eSwapCoords],
1125 (nullptr != s->group[eGrpSplit1].m) ? "mass" : "geometry");
1126 legend[count++] = gmx_strdup(buf);
1128 // Ion flux for each channel and ion type
1129 for (int ic = 0; ic < eChanNR; ic++)
1131 for (int ig = eSwapFixedGrpNR; ig < s->ngrp; ig++)
1133 t_swapGroup* g = &ir->swap->grp[ig];
1134 snprintf(buf, STRLEN, "A->ch%d->B %s permeations", ic, g->molname);
1135 legend[count++] = gmx_strdup(buf);
1139 // Number of molecules that leaked from A to B
1140 snprintf(buf, STRLEN, "leakage");
1141 legend[count++] = gmx_strdup(buf);
1143 xvgr_legend(s->fpout, count, legend, oenv);
1146 "# Instantaneous ion counts and time-averaged differences to requested numbers\n");
1148 // We add a simple text legend helping to identify the columns with xvgr legend strings
1149 fprintf(s->fpout, "# time (ps)");
1150 for (int i = 0; i < count; i++)
1152 snprintf(buf, STRLEN, "s%d", i);
1153 fprintf(s->fpout, "%10s", buf);
1155 fprintf(s->fpout, "\n");
1160 /*! \brief Initialize channel ion flux detection routine.
1162 * Initialize arrays that keep track of where the ions come from and where
1165 static void detect_flux_per_channel_init(t_swap* s, swaphistory_t* swapstate, const bool isRestart)
1168 swapstateIons_t* gs;
1170 /* All these flux detection routines run on the master only */
1171 if (swapstate == nullptr)
1176 for (int ig = eSwapFixedGrpNR; ig < s->ngrp; ig++)
1179 gs = &swapstate->ionType[ig - eSwapFixedGrpNR];
1181 /******************************************************/
1182 /* Channel and domain history for the individual ions */
1183 /******************************************************/
1184 if (isRestart) /* set the pointers right */
1186 g->comp_from = gs->comp_from;
1187 g->channel_label = gs->channel_label;
1189 else /* allocate memory for molecule counts */
1191 snew(g->comp_from, g->atomset.numAtomsGlobal() / g->apm);
1192 gs->comp_from = g->comp_from;
1193 snew(g->channel_label, g->atomset.numAtomsGlobal() / g->apm);
1194 gs->channel_label = g->channel_label;
1196 snew(g->comp_now, g->atomset.numAtomsGlobal() / g->apm);
1198 /* Initialize the channel and domain history counters */
1199 for (size_t i = 0; i < g->atomset.numAtomsGlobal() / g->apm; i++)
1201 g->comp_now[i] = eDomainNotset;
1204 g->comp_from[i] = eDomainNotset;
1205 g->channel_label[i] = eChHistPassedNone;
1209 /************************************/
1210 /* Channel fluxes for both channels */
1211 /************************************/
1212 g->nCyl[eChan0] = 0;
1213 g->nCyl[eChan1] = 0;
1219 fprintf(stderr, "%s Copying channel fluxes from checkpoint file data\n", SwS);
1223 // Loop over ion types (and both channels)
1224 for (int ig = eSwapFixedGrpNR; ig < s->ngrp; ig++)
1227 gs = &swapstate->ionType[ig - eSwapFixedGrpNR];
1229 for (int ic = 0; ic < eChanNR; ic++)
1231 fprintf(stderr, "%s Channel %d flux history for ion type %s (charge %g): ", SwS, ic, g->molname, g->q);
1234 g->fluxfromAtoB[ic] = gs->fluxfromAtoB[ic];
1238 g->fluxfromAtoB[ic] = 0;
1241 fprintf(stderr, "%d molecule%s", g->fluxfromAtoB[ic], g->fluxfromAtoB[ic] == 1 ? "" : "s");
1242 fprintf(stderr, "\n");
1246 /* Set pointers for checkpoint writing */
1247 swapstate->fluxleak_p = &s->fluxleak;
1248 for (int ig = eSwapFixedGrpNR; ig < s->ngrp; ig++)
1251 gs = &swapstate->ionType[ig - eSwapFixedGrpNR];
1253 for (int ic = 0; ic < eChanNR; ic++)
1255 gs->fluxfromAtoB_p[ic] = &g->fluxfromAtoB[ic];
1261 /*! \brief Outputs the initial structure to PDB file for debugging reasons.
1263 * Output the starting structure so that in case of multimeric channels
1264 * the user can check whether we have the correct PBC image for all atoms.
1265 * If this is not correct, the ion counts per channel will be very likely
1268 static void outputStartStructureIfWanted(gmx_mtop_t* mtop, rvec* x, PbcType pbcType, const matrix box)
1270 char* env = getenv("GMX_COMPELDUMP");
1275 "\n%s Found env.var. GMX_COMPELDUMP, will output CompEL starting structure made "
1277 "%s In case of multimeric channels, please check whether they have the correct PBC "
1278 "representation.\n",
1282 write_sto_conf_mtop(
1283 "CompELAssumedWholeConfiguration.pdb", *mtop->name, mtop, x, nullptr, pbcType, box);
1288 /*! \brief Initialize the swapstate structure, used for checkpoint writing.
1290 * The swapstate struct stores the information we need to make the channels
1291 * whole again after restarts from a checkpoint file. Here we do the following:
1292 * a) If we did not start from .cpt, we prepare the struct for proper .cpt writing,
1293 * b) if we did start from .cpt, we copy over the last whole structures from .cpt,
1294 * c) in any case, for subsequent checkpoint writing, we set the pointers in
1295 * swapstate to the x_old arrays, which contain the correct PBC representation of
1296 * multimeric channels at the last time step.
1298 static void init_swapstate(swaphistory_t* swapstate,
1302 const rvec* x, /* the initial positions */
1304 const t_inputrec* ir)
1306 rvec* x_pbc = nullptr; /* positions of the whole MD system with molecules made whole */
1310 /* We always need the last whole positions such that
1311 * in the next time step we can make the channels whole again in PBC */
1312 if (swapstate->bFromCpt)
1314 /* Copy the last whole positions of each channel from .cpt */
1315 g = &(s->group[eGrpSplit0]);
1316 for (size_t i = 0; i < g->atomset.numAtomsGlobal(); i++)
1318 copy_rvec(swapstate->xc_old_whole[eChan0][i], g->xc_old[i]);
1320 g = &(s->group[eGrpSplit1]);
1321 for (size_t i = 0; i < g->atomset.numAtomsGlobal(); i++)
1323 copy_rvec(swapstate->xc_old_whole[eChan1][i], g->xc_old[i]);
1328 swapstate->eSwapCoords = ir->eSwapCoords;
1330 /* Set the number of ion types and allocate memory for checkpointing */
1331 swapstate->nIonTypes = s->ngrp - eSwapFixedGrpNR;
1332 snew(swapstate->ionType, swapstate->nIonTypes);
1334 /* Store the total number of ions of each type in the swapstateIons
1335 * structure that is accessible during checkpoint writing */
1336 for (int ii = 0; ii < swapstate->nIonTypes; ii++)
1338 swapstateIons_t* gs = &swapstate->ionType[ii];
1339 gs->nMol = sc->grp[ii + eSwapFixedGrpNR].nat;
1342 /* Extract the initial split group positions. */
1344 /* Remove pbc, make molecule whole. */
1345 snew(x_pbc, mtop->natoms);
1346 copy_rvecn(x, x_pbc, 0, mtop->natoms);
1348 /* This can only make individual molecules whole, not multimers */
1349 do_pbc_mtop(ir->pbcType, box, mtop, x_pbc);
1351 /* Output the starting structure? */
1352 outputStartStructureIfWanted(mtop, x_pbc, ir->pbcType, box);
1354 /* If this is the first run (i.e. no checkpoint present) we assume
1355 * that the starting positions give us the correct PBC representation */
1356 for (int ig = eGrpSplit0; ig <= eGrpSplit1; ig++)
1358 g = &(s->group[ig]);
1359 for (size_t i = 0; i < g->atomset.numAtomsGlobal(); i++)
1361 copy_rvec(x_pbc[g->atomset.globalIndex()[i]], g->xc_old[i]);
1366 /* Prepare swapstate arrays for later checkpoint writing */
1367 swapstate->nat[eChan0] = s->group[eGrpSplit0].atomset.numAtomsGlobal();
1368 swapstate->nat[eChan1] = s->group[eGrpSplit1].atomset.numAtomsGlobal();
1371 /* For subsequent checkpoint writing, set the swapstate pointers to the xc_old
1372 * arrays that get updated at every swapping step */
1373 swapstate->xc_old_whole_p[eChan0] = &s->group[eGrpSplit0].xc_old;
1374 swapstate->xc_old_whole_p[eChan1] = &s->group[eGrpSplit1].xc_old;
1377 /*! \brief Determine the total charge imbalance resulting from the swap groups */
1378 static real getRequestedChargeImbalance(t_swap* s)
1383 real particle_charge;
1384 real particle_number[eCompNR];
1386 // s->deltaQ = ( (-1) * s->comp[eCompA][eIonNEG].nat_req + s->comp[eCompA][eIonPOS].nat_req )
1387 // - ( (-1) * s->comp[eCompB][eIonNEG].nat_req + s->comp[eCompB][eIonPOS].nat_req );
1389 for (ig = eSwapFixedGrpNR; ig < s->ngrp; ig++)
1393 particle_charge = g->q;
1394 particle_number[eCompA] = g->comp[eCompA].nMolReq;
1395 particle_number[eCompB] = g->comp[eCompB].nMolReq;
1397 DeltaQ += particle_charge * (particle_number[eCompA] - particle_number[eCompB]);
1404 /*! \brief Sorts anions and cations into two separate groups
1406 * This routine should be called for the 'anions' and 'cations' group,
1407 * of which the indices were lumped together in the older version of the code.
1409 static void copyIndicesToGroup(const int* indIons, int nIons, t_swapGroup* g, t_commrec* cr)
1413 /* If explicit ion counts were requested in the .mdp file
1414 * (by setting positive values for the number of ions),
1415 * we can make an additional consistency check here */
1416 if ((g->nmolReq[eCompA] < 0) && (g->nmolReq[eCompB] < 0))
1418 if (g->nat != (g->nmolReq[eCompA] + g->nmolReq[eCompB]))
1420 gmx_fatal_collective(FARGS,
1423 "%s Inconsistency while importing swap-related data from an old "
1424 "input file version.\n"
1425 "%s The requested ion counts in compartments A (%d) and B (%d)\n"
1426 "%s do not add up to the number of ions (%d) of this type for the "
1438 srenew(g->ind, g->nat);
1439 for (int i = 0; i < g->nat; i++)
1441 g->ind[i] = indIons[i];
1446 /*! \brief Converts old .tpr file CompEL contents to new data layout.
1448 * If we have read an old .tpr file (tpxv <= tpxv_CompElPolyatomicIonsAndMultipleIonTypes),
1449 * anions and cations are stored together in group #3. In the new
1450 * format we store each ion type in a separate group.
1451 * The 'classic' groups are:
1452 * #0 split group 0 - OK
1453 * #1 split group 1 - OK
1455 * #3 anions - contains also cations, needs to be converted
1456 * #4 cations - empty before conversion
1459 static void convertOldToNewGroupFormat(t_swapcoords* sc, gmx_mtop_t* mtop, gmx_bool bVerbose, t_commrec* cr)
1461 t_swapGroup* g = &sc->grp[3];
1463 /* Loop through the atom indices of group #3 (anions) and put all indices
1464 * that belong to cations into the cation group.
1468 int* indAnions = nullptr;
1469 int* indCations = nullptr;
1470 snew(indAnions, g->nat);
1471 snew(indCations, g->nat);
1474 for (int i = 0; i < g->nat; i++)
1476 const t_atom& atom = mtopGetAtomParameters(mtop, g->ind[i], &molb);
1479 // This is an anion, add it to the list of anions
1480 indAnions[nAnions++] = g->ind[i];
1484 // This is a cation, add it to the list of cations
1485 indCations[nCations++] = g->ind[i];
1492 "%s Sorted %d ions into separate groups of %d anions and %d cations.\n",
1500 /* Now we have the correct lists of anions and cations.
1501 * Copy it to the right groups.
1503 copyIndicesToGroup(indAnions, nAnions, g, cr);
1505 copyIndicesToGroup(indCations, nCations, g, cr);
1511 /*! \brief Returns TRUE if we started from an old .tpr
1513 * Then we need to re-sort anions and cations into separate groups */
1514 static gmx_bool bConvertFromOldTpr(t_swapcoords* sc)
1516 // If the last group has no atoms it means we need to convert!
1517 return (sc->ngrp >= 5) && (0 == sc->grp[4].nat);
1521 t_swap* init_swapcoords(FILE* fplog,
1522 const t_inputrec* ir,
1525 const t_state* globalState,
1526 ObservablesHistory* oh,
1528 gmx::LocalAtomSetManager* atomSets,
1529 const gmx_output_env_t* oenv,
1530 const gmx::MdrunOptions& mdrunOptions,
1531 const gmx::StartingBehavior startingBehavior)
1534 swapstateIons_t* gs;
1535 swaphistory_t* swapstate = nullptr;
1537 if ((PAR(cr)) && !DOMAINDECOMP(cr))
1539 gmx_fatal(FARGS, "Position swapping is only implemented for domain decomposition!");
1543 auto s = new t_swap();
1545 if (mdrunOptions.rerun)
1550 "%s This module does not support reruns in parallel\nPlease request a serial "
1551 "run with -nt 1 / -np 1\n",
1555 fprintf(stderr, "%s Rerun - using every available frame\n", SwS);
1557 sc->nAverage = 1; /* averaging makes no sense for reruns */
1560 if (MASTER(cr) && startingBehavior == gmx::StartingBehavior::NewSimulation)
1562 fprintf(fplog, "\nInitializing ion/water position exchanges\n");
1563 please_cite(fplog, "Kutzner2011b");
1566 switch (ir->eSwapCoords)
1568 case eswapX: s->swapdim = XX; break;
1569 case eswapY: s->swapdim = YY; break;
1570 case eswapZ: s->swapdim = ZZ; break;
1571 default: s->swapdim = -1; break;
1574 const gmx_bool bVerbose = mdrunOptions.verbose;
1576 // For compatibility with old .tpr files
1577 if (bConvertFromOldTpr(sc))
1579 convertOldToNewGroupFormat(sc, mtop, bVerbose && MASTER(cr), cr);
1582 /* Copy some data and pointers to the group structures for convenience */
1583 /* Number of atoms in the group */
1585 for (int i = 0; i < s->ngrp; i++)
1587 s->group.emplace_back(atomSets->add(
1588 gmx::ArrayRef<const int>(sc->grp[i].ind, sc->grp[i].ind + sc->grp[i].nat)));
1589 s->group[i].molname = sc->grp[i].molname;
1592 /* Check for overlapping atoms */
1593 check_swap_groups(s, mtop->natoms, bVerbose && MASTER(cr));
1595 /* Allocate space for the collective arrays for all groups */
1596 /* For the collective position array */
1597 for (int i = 0; i < s->ngrp; i++)
1600 snew(g->xc, g->atomset.numAtomsGlobal());
1602 /* For the split groups (the channels) we need some extra memory to
1603 * be able to make the molecules whole even if they span more than
1604 * half of the box size. */
1605 if ((i == eGrpSplit0) || (i == eGrpSplit1))
1607 snew(g->xc_shifts, g->atomset.numAtomsGlobal());
1608 snew(g->xc_eshifts, g->atomset.numAtomsGlobal());
1609 snew(g->xc_old, g->atomset.numAtomsGlobal());
1615 if (oh->swapHistory == nullptr)
1617 oh->swapHistory = std::make_unique<swaphistory_t>(swaphistory_t{});
1619 swapstate = oh->swapHistory.get();
1621 init_swapstate(swapstate, sc, s, mtop, globalState->x.rvec_array(), globalState->box, ir);
1624 /* After init_swapstate we have a set of (old) whole positions for our
1625 * channels. Now transfer that to all nodes */
1628 for (int ig = eGrpSplit0; ig <= eGrpSplit1; ig++)
1630 g = &(s->group[ig]);
1631 gmx_bcast((g->atomset.numAtomsGlobal()) * sizeof((g->xc_old)[0]), g->xc_old, cr->mpi_comm_mygroup);
1635 /* Make sure that all molecules in the solvent and ion groups contain the
1636 * same number of atoms each */
1637 for (int ig = eGrpSolvent; ig < s->ngrp; ig++)
1641 g = &(s->group[ig]);
1642 g->apm = get_group_apm_check(ig, s, MASTER(cr) && bVerbose, mtop);
1644 /* Since all molecules of a group are equal, we only need enough space
1645 * to determine properties of a single molecule at at time */
1646 snew(g->m, g->apm); /* For the center of mass */
1647 charge = 0; /* To determine the charge imbalance */
1649 for (int j = 0; j < g->apm; j++)
1651 const t_atom& atom = mtopGetAtomParameters(mtop, g->atomset.globalIndex()[j], &molb);
1655 /* Total charge of one molecule of this group: */
1660 /* Need mass-weighted center of split group? */
1661 for (int j = eGrpSplit0; j <= eGrpSplit1; j++)
1664 if (sc->massw_split[j])
1666 /* Save the split group masses if mass-weighting is requested */
1667 snew(g->m, g->atomset.numAtomsGlobal());
1669 for (size_t i = 0; i < g->atomset.numAtomsGlobal(); i++)
1671 g->m[i] = mtopGetAtomMass(mtop, g->atomset.globalIndex()[i], &molb);
1676 /* Make a t_pbc struct on all nodes so that the molecules
1677 * chosen for an exchange can be made whole. */
1680 bool restartWithAppending = (startingBehavior == gmx::StartingBehavior::RestartWithAppending);
1685 fprintf(stderr, "%s Opening output file %s%s\n", SwS, fn, restartWithAppending ? " for appending" : "");
1688 s->fpout = gmx_fio_fopen(fn, restartWithAppending ? "a" : "w");
1690 if (!restartWithAppending)
1692 xvgr_header(s->fpout, "Molecule counts", "Time (ps)", "counts", exvggtXNY, oenv);
1694 for (int ig = 0; ig < s->ngrp; ig++)
1696 g = &(s->group[ig]);
1698 "# %s group '%s' contains %d atom%s",
1699 ig < eSwapFixedGrpNR ? eSwapFixedGrp_names[ig] : "Ion",
1701 static_cast<int>(g->atomset.numAtomsGlobal()),
1702 (g->atomset.numAtomsGlobal() > 1) ? "s" : "");
1703 if (!(eGrpSplit0 == ig || eGrpSplit1 == ig))
1706 " with %d atom%s in each molecule of charge %g",
1708 (g->apm > 1) ? "s" : "",
1711 fprintf(s->fpout, ".\n");
1714 fprintf(s->fpout, "#\n# Initial positions of split groups:\n");
1717 for (int j = eGrpSplit0; j <= eGrpSplit1; j++)
1720 for (size_t i = 0; i < g->atomset.numAtomsGlobal(); i++)
1722 copy_rvec(globalState->x[sc->grp[j].ind[i]], g->xc[i]);
1724 /* xc has the correct PBC representation for the two channels, so we do
1725 * not need to correct for that */
1726 get_center(g->xc, g->m, g->atomset.numAtomsGlobal(), g->center);
1727 if (!restartWithAppending)
1730 "# %s group %s-center %5f nm\n",
1731 eSwapFixedGrp_names[j],
1733 g->center[s->swapdim]);
1737 if (!restartWithAppending)
1739 if ((0 != sc->bulkOffset[eCompA]) || (0 != sc->bulkOffset[eCompB]))
1741 fprintf(s->fpout, "#\n");
1743 "# You provided an offset for the position of the bulk layer(s).\n");
1745 "# That means the layers to/from which ions and water molecules are "
1748 "# are not midway (= at 0.0) between the compartment-defining layers (at "
1750 fprintf(s->fpout, "# bulk-offsetA = %g\n", sc->bulkOffset[eCompA]);
1751 fprintf(s->fpout, "# bulk-offsetB = %g\n", sc->bulkOffset[eCompB]);
1754 fprintf(s->fpout, "#\n");
1756 "# Split0 cylinder radius %f nm, up %f nm, down %f nm\n",
1761 "# Split1 cylinder radius %f nm, up %f nm, down %f nm\n",
1766 fprintf(s->fpout, "#\n");
1767 if (!mdrunOptions.rerun)
1770 "# Coupling constant (number of swap attempt steps to average over): %d "
1771 "(translates to %f ps).\n",
1773 sc->nAverage * sc->nstswap * ir->delta_t);
1774 fprintf(s->fpout, "# Threshold is %f\n", sc->threshold);
1775 fprintf(s->fpout, "#\n");
1777 "# Remarks about which atoms passed which channel use global atoms numbers "
1778 "starting at one.\n");
1787 /* Allocate memory to remember the past particle counts for time averaging */
1788 for (int ig = eSwapFixedGrpNR; ig < s->ngrp; ig++)
1790 g = &(s->group[ig]);
1791 for (int ic = 0; ic < eCompNR; ic++)
1793 snew(g->comp[ic].nMolPast, sc->nAverage);
1797 /* Get the initial particle concentrations and let the other nodes know */
1800 if (startingBehavior != gmx::StartingBehavior::NewSimulation)
1802 get_initial_ioncounts_from_cpt(ir, s, swapstate, cr, bVerbose);
1806 fprintf(stderr, "%s Determining initial numbers of ions per compartment.\n", SwS);
1807 get_initial_ioncounts(
1808 ir, s, globalState->x.rvec_array(), globalState->box, cr, mdrunOptions.rerun);
1811 /* Prepare (further) checkpoint writes ... */
1812 if (startingBehavior != gmx::StartingBehavior::NewSimulation)
1814 /* Consistency check */
1815 if (swapstate->nAverage != sc->nAverage)
1818 "%s Ion count averaging steps mismatch! checkpoint: %d, tpr: %d",
1820 swapstate->nAverage,
1826 swapstate->nAverage = sc->nAverage;
1828 fprintf(stderr, "%s Setting pointers for checkpoint writing\n", SwS);
1829 for (int ic = 0; ic < eCompNR; ic++)
1831 for (int ig = eSwapFixedGrpNR; ig < s->ngrp; ig++)
1834 gs = &swapstate->ionType[ig - eSwapFixedGrpNR];
1836 gs->nMolReq_p[ic] = &(g->comp[ic].nMolReq);
1837 gs->nMolPast_p[ic] = &(g->comp[ic].nMolPast[0]);
1838 gs->inflow_net_p[ic] = &(g->comp[ic].inflow_net);
1842 /* Determine the total charge imbalance */
1843 s->deltaQ = getRequestedChargeImbalance(s);
1847 fprintf(stderr, "%s Requested charge imbalance is Q(A) - Q(B) = %g e.\n", SwS, s->deltaQ);
1849 if (!restartWithAppending)
1851 fprintf(s->fpout, "# Requested charge imbalance is Q(A)-Q(B) = %g e.\n", s->deltaQ);
1857 bc_initial_concentrations(cr, ir->swap, s);
1860 /* Update the time-averaged number of molecules for all groups and compartments */
1861 for (int ig = eSwapFixedGrpNR; ig < sc->ngrp; ig++)
1864 for (int ic = 0; ic < eCompNR; ic++)
1866 update_time_window(&g->comp[ic], sc->nAverage, -1);
1870 /* Initialize arrays that keep track of through which channel the ions go */
1871 detect_flux_per_channel_init(s, swapstate, startingBehavior != gmx::StartingBehavior::NewSimulation);
1873 /* We need to print the legend if we open this file for the first time. */
1874 if (MASTER(cr) && !restartWithAppending)
1876 print_ionlist_legend(ir, s, oenv);
1882 void finish_swapcoords(t_swap* s)
1890 // Close the swap output file
1891 gmx_fio_fclose(s->fpout);
1895 /*! \brief Do we need to swap a molecule in any of the ion groups with a water molecule at this step?
1897 * From the requested and average molecule counts we determine whether a swap is needed
1898 * at this time step.
1900 static gmx_bool need_swap(t_swapcoords* sc, t_swap* s)
1905 for (ig = eSwapFixedGrpNR; ig < sc->ngrp; ig++)
1909 for (ic = 0; ic < eCompNR; ic++)
1911 if (g->comp[ic].nMolReq - g->comp[ic].nMolAv >= sc->threshold)
1921 /*! \brief Return the index of an atom or molecule suitable for swapping.
1923 * Returns the index of an atom that is far off the compartment boundaries,
1924 * that is near to the bulk layer to/from which the swaps take place.
1925 * Other atoms of the molecule (if any) will directly follow the returned index.
1927 * \param[in] comp Structure containing compartment-specific data.
1928 * \param[in] molname Name of the molecule.
1930 * \returns Index of the first atom of the molecule chosen for a position exchange.
1932 static int get_index_of_distant_atom(t_compartment* comp, const char molname[])
1935 real d = GMX_REAL_MAX;
1938 /* comp->nat contains the original number of atoms in this compartment
1939 * prior to doing any swaps. Some of these atoms may already have been
1940 * swapped out, but then they are marked with a distance of GMX_REAL_MAX
1942 for (int iMol = 0; iMol < comp->nMolBefore; iMol++)
1944 if (comp->dist[iMol] < d)
1947 d = comp->dist[ibest];
1954 "Could not get index of %s atom. Compartment contains %d %s molecules before "
1961 /* Set the distance of this index to infinity such that it won't get selected again in
1964 comp->dist[ibest] = GMX_REAL_MAX;
1966 return comp->ind[ibest];
1970 /*! \brief Swaps centers of mass and makes molecule whole if broken */
1971 static void translate_positions(rvec* x, int apm, rvec old_com, rvec new_com, t_pbc* pbc)
1974 rvec reference, dx, correctPBCimage;
1977 /* Use the first atom as the reference for PBC */
1978 copy_rvec(x[0], reference);
1980 for (i = 0; i < apm; i++)
1982 /* PBC distance between position and reference */
1983 pbc_dx(pbc, x[i], reference, dx);
1985 /* Add PBC distance to reference */
1986 rvec_add(reference, dx, correctPBCimage);
1988 /* Subtract old_com from correct image and add new_com */
1989 rvec_dec(correctPBCimage, old_com);
1990 rvec_inc(correctPBCimage, new_com);
1992 copy_rvec(correctPBCimage, x[i]);
1997 /*! \brief Write back the modified local positions from the collective array to the official positions. */
1998 static void apply_modified_positions(swap_group* g, rvec x[])
2000 auto collectiveIndex = g->atomset.collectiveIndex().begin();
2001 for (const auto localIndex : g->atomset.localIndex())
2003 /* Copy the possibly modified position */
2004 copy_rvec(g->xc[*collectiveIndex], x[localIndex]);
2010 gmx_bool do_swapcoords(t_commrec* cr,
2015 gmx_wallcycle* wcycle,
2022 int j, ic, ig, nswaps;
2023 int thisC, otherC; /* Index into this compartment and the other one */
2024 gmx_bool bSwap = FALSE;
2025 t_swapgrp * g, *gsol;
2027 rvec com_solvent, com_particle; /* solvent and swap molecule's center of mass */
2030 wallcycle_start(wcycle, ewcSWAP);
2034 set_pbc(s->pbc, ir->pbcType, box);
2036 /* Assemble the positions of the split groups, i.e. the channels.
2037 * Here we also pass a shifts array to communicate_group_positions(), so that it can make
2038 * the molecules whole even in cases where they span more than half of the box in
2040 for (ig = eGrpSplit0; ig <= eGrpSplit1; ig++)
2042 g = &(s->group[ig]);
2043 communicate_group_positions(cr,
2049 g->atomset.numAtomsGlobal(),
2050 g->atomset.numAtomsLocal(),
2051 g->atomset.localIndex().data(),
2052 g->atomset.collectiveIndex().data(),
2056 get_center(g->xc, g->m, g->atomset.numAtomsGlobal(), g->center); /* center of split groups == channels */
2059 /* Assemble the positions of the ions (ig = 3, 4, ...). These molecules should
2060 * be small and we can always make them whole with a simple distance check.
2061 * Therefore we pass NULL as third argument. */
2062 for (ig = eSwapFixedGrpNR; ig < s->ngrp; ig++)
2064 g = &(s->group[ig]);
2065 communicate_group_positions(cr,
2071 g->atomset.numAtomsGlobal(),
2072 g->atomset.numAtomsLocal(),
2073 g->atomset.localIndex().data(),
2074 g->atomset.collectiveIndex().data(),
2078 /* Determine how many ions of this type each compartment contains */
2079 sortMoleculesIntoCompartments(g, cr, sc, s, box, step, s->fpout, bRerun, FALSE);
2082 /* Output how many ions are in the compartments */
2085 print_ionlist(s, t, "");
2088 /* If we are doing a rerun, we are finished here, since we cannot perform
2095 /* Do we have to perform a swap? */
2096 bSwap = need_swap(sc, s);
2099 /* Since we here know that we have to perform ion/water position exchanges,
2100 * we now assemble the solvent positions */
2101 g = &(s->group[eGrpSolvent]);
2102 communicate_group_positions(cr,
2108 g->atomset.numAtomsGlobal(),
2109 g->atomset.numAtomsLocal(),
2110 g->atomset.localIndex().data(),
2111 g->atomset.collectiveIndex().data(),
2115 /* Determine how many molecules of solvent each compartment contains */
2116 sortMoleculesIntoCompartments(g, cr, sc, s, box, step, s->fpout, bRerun, TRUE);
2118 /* Save number of solvent molecules per compartment prior to any swaps */
2119 g->comp[eCompA].nMolBefore = g->comp[eCompA].nMol;
2120 g->comp[eCompB].nMolBefore = g->comp[eCompB].nMol;
2122 for (ig = eSwapFixedGrpNR; ig < s->ngrp; ig++)
2124 g = &(s->group[ig]);
2126 for (ic = 0; ic < eCompNR; ic++)
2128 /* Determine in which compartment ions are missing and where they are too many */
2129 g->vacancy[ic] = g->comp[ic].nMolReq - g->comp[ic].nMolAv;
2131 /* Save number of ions per compartment prior to swaps */
2132 g->comp[ic].nMolBefore = g->comp[ic].nMol;
2136 /* Now actually perform the particle exchanges, one swap group after another */
2137 gsol = &s->group[eGrpSolvent];
2138 for (ig = eSwapFixedGrpNR; ig < s->ngrp; ig++)
2142 for (thisC = 0; thisC < eCompNR; thisC++)
2144 /* Index to the other compartment */
2145 otherC = (thisC + 1) % eCompNR;
2147 while (g->vacancy[thisC] >= sc->threshold)
2149 /* Swap in an ion */
2151 /* Get the xc-index of the first atom of a solvent molecule of this compartment */
2152 isol = get_index_of_distant_atom(&gsol->comp[thisC], gsol->molname);
2154 /* Get the xc-index of a particle from the other compartment */
2155 iion = get_index_of_distant_atom(&g->comp[otherC], g->molname);
2157 get_molecule_center(&gsol->xc[isol], gsol->apm, gsol->m, com_solvent, s->pbc);
2158 get_molecule_center(&g->xc[iion], g->apm, g->m, com_particle, s->pbc);
2160 /* Subtract solvent molecule's center of mass and add swap particle's center of mass */
2161 translate_positions(&gsol->xc[isol], gsol->apm, com_solvent, com_particle, s->pbc);
2162 /* Similarly for the swap particle, subtract com_particle and add com_solvent */
2163 translate_positions(&g->xc[iion], g->apm, com_particle, com_solvent, s->pbc);
2165 /* Keep track of the changes */
2166 g->vacancy[thisC]--;
2167 g->vacancy[otherC]++;
2168 g->comp[thisC].nMol++;
2169 g->comp[otherC].nMol--;
2170 g->comp[thisC].inflow_net++;
2171 g->comp[otherC].inflow_net--;
2172 /* Correct the past time window to still get the right averages from now on */
2173 g->comp[thisC].nMolAv++;
2174 g->comp[otherC].nMolAv--;
2175 for (j = 0; j < sc->nAverage; j++)
2177 g->comp[thisC].nMolPast[j]++;
2178 g->comp[otherC].nMolPast[j]--;
2180 /* Clear ion history */
2183 int iMol = iion / g->apm;
2184 g->channel_label[iMol] = eChHistPassedNone;
2185 g->comp_from[iMol] = eDomainNotset;
2187 /* That was the swap */
2192 if (nswaps && bVerbose)
2195 "%s Performed %d swap%s in step %" PRId64 " for iontype %s.\n",
2198 nswaps > 1 ? "s" : "",
2204 if (s->fpout != nullptr)
2206 print_ionlist(s, t, " # after swap");
2209 /* For the solvent and user-defined swap groups, each rank writes back its
2210 * (possibly modified) local positions to the official position array. */
2211 for (ig = eGrpSolvent; ig < s->ngrp; ig++)
2214 apply_modified_positions(g, x);
2217 } /* end of if(bSwap) */
2219 wallcycle_stop(wcycle, ewcSWAP);