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,2021, 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 "gromacs/utility/enumerationhelpers.h"
46 #include "swapcoords.h"
56 #include "gromacs/domdec/domdec_struct.h"
57 #include "gromacs/domdec/localatomset.h"
58 #include "gromacs/domdec/localatomsetmanager.h"
59 #include "gromacs/fileio/confio.h"
60 #include "gromacs/fileio/gmxfio.h"
61 #include "gromacs/fileio/xvgr.h"
62 #include "gromacs/gmxlib/network.h"
63 #include "gromacs/math/vec.h"
64 #include "gromacs/mdlib/groupcoord.h"
65 #include "gromacs/mdrunutility/handlerestart.h"
66 #include "gromacs/mdtypes/commrec.h"
67 #include "gromacs/mdtypes/imdmodule.h"
68 #include "gromacs/mdtypes/inputrec.h"
69 #include "gromacs/mdtypes/md_enums.h"
70 #include "gromacs/mdtypes/mdrunoptions.h"
71 #include "gromacs/mdtypes/observableshistory.h"
72 #include "gromacs/mdtypes/state.h"
73 #include "gromacs/mdtypes/swaphistory.h"
74 #include "gromacs/pbcutil/pbc.h"
75 #include "gromacs/timing/wallcycle.h"
76 #include "gromacs/topology/mtop_lookup.h"
77 #include "gromacs/topology/topology.h"
78 #include "gromacs/utility/cstringutil.h"
79 #include "gromacs/utility/fatalerror.h"
80 #include "gromacs/utility/pleasecite.h"
81 #include "gromacs/utility/smalloc.h"
82 #include "gromacs/utility/snprintf.h"
84 static const char* SwS = { "SWAP:" }; /**< For output that comes from the swap module */
85 static const char* SwSEmpty = { " " }; /**< Placeholder for multi-line output */
86 static const char* CompStr[eCompNR] = { "A", "B" }; /**< Compartment name */
87 static constexpr gmx::EnumerationArray<SwapType, const char*> SwapStr = { "", "X-", "Y-", "Z-" }; /**< Name for the swap types. */
88 static const char* DimStr[DIM + 1] = { "X", "Y", "Z", nullptr }; /**< Name for the swap dimension. */
90 /** Keep track of through which channel the ions have passed */
98 static const char* ChannelString[eChHistNr] = { "none", "channel0", "channel1" }; /**< Name for the channels */
100 /*! \brief Domain identifier.
102 * Keeps track of from which compartment the ions came before passing the
112 static const char* DomainString[eDomainNr] = { "not_assigned", "Domain_A", "Domain_B" }; /**< Name for the domains */
117 extern template LocalAtomSet LocalAtomSetManager::add<void, void>(ArrayRef<const int> globalAtomIndex);
120 * \brief Implement Computational Electrophysiology swapping.
122 class SwapCoordinates final : public IMDModule
125 IMdpOptionProvider* mdpOptionProvider() override { return nullptr; }
126 IMDOutputProvider* outputProvider() override { return nullptr; }
127 void initForceProviders(ForceProviders* /* forceProviders */) override {}
128 void subscribeToSimulationSetupNotifications(MdModulesNotifier* /* notifier */) override {}
129 void subscribeToPreProcessingNotifications(MdModulesNotifier* /* notifier */) override {}
132 std::unique_ptr<IMDModule> createSwapCoordinatesModule()
134 return std::make_unique<SwapCoordinates>();
141 * Structure containing compartment-specific data.
143 typedef struct swap_compartment
145 int nMol; /**< Number of ion or water molecules detected
146 in this compartment. */
147 int nMolBefore; /**< Number of molecules before swapping. */
148 int nMolReq; /**< Requested number of molecules in compartment. */
149 real nMolAv; /**< Time-averaged number of molecules matching
150 the compartment conditions. */
151 int* nMolPast; /**< Past molecule counts for time-averaging. */
152 int* ind; /**< Indices to collective array of atoms. */
153 real* dist; /**< Distance of atom to bulk layer, which is
154 normally the center layer of the compartment */
155 int nalloc; /**< Allocation size for ind array. */
156 int inflow_net; /**< Net inflow of ions into this compartment. */
161 * This structure contains data needed for the groups involved in swapping:
162 * split group 0, split group 1, solvent group, ion groups.
164 typedef struct swap_group
166 /*!\brief Construct a swap group given the managed swap atoms.
168 * \param[in] atomset Managed indices of atoms that are part of the swap group.
170 swap_group(const gmx::LocalAtomSet& atomset);
171 char* molname = nullptr; /**< Name of the group or ion type */
172 int apm = 0; /**< Number of atoms in each molecule */
173 gmx::LocalAtomSet atomset; /**< The atom indices in the swap group */
174 rvec* xc = nullptr; /**< Collective array of group atom positions (size nat) */
175 ivec* xc_shifts = nullptr; /**< Current (collective) shifts (size nat) */
176 ivec* xc_eshifts = nullptr; /**< Extra shifts since last DD step (size nat) */
177 rvec* xc_old = nullptr; /**< Old (collective) positions (size nat) */
178 real q = 0.; /**< Total charge of one molecule of this group */
179 real* m = nullptr; /**< Masses (can be omitted, size apm) */
180 unsigned char* comp_from = nullptr; /**< (Collective) Stores from which compartment this
181 molecule has come. This way we keep track of
182 through which channel an ion permeates
183 (size nMol = nat/apm) */
184 unsigned char* comp_now = nullptr; /**< In which compartment this ion is now (size nMol) */
185 unsigned char* channel_label = nullptr; /**< Which channel was passed at last by this ion?
187 rvec center; /**< Center of the group; COM if masses are used */
188 t_compartment comp[eCompNR]; /**< Distribution of particles of this group across
189 the two compartments */
190 real vacancy[eCompNR]; /**< How many molecules need to be swapped in? */
191 int fluxfromAtoB[eChanNR]; /**< Net flux of ions per channel */
192 int nCyl[eChanNR]; /**< Number of ions residing in a channel */
193 int nCylBoth = 0; /**< Ions assigned to cyl0 and cyl1. Not good. */
196 t_swapgrp::swap_group(const gmx::LocalAtomSet& atomset) : atomset{ atomset }
201 for (int compartment = eCompA; compartment < eCompNR; ++compartment)
203 comp[compartment] = {};
204 vacancy[compartment] = 0;
206 for (int channel = eChan0; channel < eChanNR; ++channel)
208 fluxfromAtoB[channel] = 0;
214 * Main (private) data structure for the position swapping protocol.
218 int swapdim; /**< One of XX, YY, ZZ */
219 t_pbc* pbc; /**< Needed to make molecules whole. */
220 FILE* fpout; /**< Output file. */
221 int ngrp; /**< Number of t_swapgrp groups */
222 std::vector<t_swapgrp> group; /**< Separate groups for channels, solvent, ions */
223 int fluxleak; /**< Flux not going through any of the channels. */
224 real deltaQ; /**< The charge imbalance between the compartments. */
228 /*! \brief Check whether point is in channel.
230 * A channel is a cylinder defined by a disc
231 * with radius r around its center c. The thickness of the cylinder is
238 * <---------c--------->
244 * \param[in] point The position (xyz) under consideration.
245 * \param[in] center The center of the cylinder.
246 * \param[in] d_up The upper extension of the cylinder.
247 * \param[in] d_down The lower extension.
248 * \param[in] r_cyl2 Cylinder radius squared.
249 * \param[in] pbc Structure with info about periodic boundary conditions.
250 * \param[in] normal The membrane normal direction is typically 3, i.e. z, but can be x or y also.
252 * \returns Whether the point is inside the defined cylindric channel.
254 static gmx_bool is_in_channel(rvec point, rvec center, real d_up, real d_down, real r_cyl2, t_pbc* pbc, int normal)
257 int plane1, plane2; /* Directions tangential to membrane */
260 plane1 = (normal + 1) % 3; /* typically 0, i.e. XX */
261 plane2 = (normal + 2) % 3; /* typically 1, i.e. YY */
263 /* Get the distance vector dr between the point and the center of the cylinder */
264 pbc_dx(pbc, point, center, dr); /* This puts center in the origin */
266 /* Check vertical direction */
267 if ((dr[normal] > d_up) || (dr[normal] < -d_down))
272 /* Check radial direction */
273 if ((dr[plane1] * dr[plane1] + dr[plane2] * dr[plane2]) > r_cyl2)
278 /* All check passed, this point is in the cylinder */
283 /*! \brief Prints output to CompEL output file.
285 * Prints to swap output file how many ions are in each compartment,
286 * where the centers of the split groups are, and how many ions of each type
287 * passed the channels.
289 static void print_ionlist(t_swap* s, double time, const char comment[])
292 fprintf(s->fpout, "%12.5e", time);
294 // Output number of molecules and difference to reference counts for each
295 // compartment and ion type
296 for (int iComp = 0; iComp < eCompNR; iComp++)
298 for (int ig = static_cast<int>(SwapGroupSplittingType::Count); ig < s->ngrp; ig++)
300 t_compartment* comp = &s->group[ig].comp[iComp];
302 fprintf(s->fpout, "%10d%10.1f%10d", comp->nMol, comp->nMolAv - comp->nMolReq, comp->inflow_net);
306 // Output center of split groups
309 s->group[static_cast<int>(SwapGroupSplittingType::Split0)].center[s->swapdim],
310 s->group[static_cast<int>(SwapGroupSplittingType::Split1)].center[s->swapdim]);
312 // Output ion flux for each channel and ion type
313 for (int iChan = 0; iChan < eChanNR; iChan++)
315 for (int ig = static_cast<int>(SwapGroupSplittingType::Count); ig < s->ngrp; ig++)
317 t_swapgrp* g = &s->group[ig];
318 fprintf(s->fpout, "%10d", g->fluxfromAtoB[iChan]);
322 /* Output the number of molecules that leaked from A to B */
323 fprintf(s->fpout, "%10d", s->fluxleak);
325 fprintf(s->fpout, "%s\n", comment);
329 /*! \brief Get the center of a group of nat atoms.
331 * Since with PBC an atom group might not be whole, use the first atom as the
332 * reference atom and determine the center with respect to this reference.
334 static void get_molecule_center(rvec x[], int nat, const real* weights, rvec center, t_pbc* pbc)
337 rvec weightedPBCimage;
339 rvec reference, correctPBCimage, dx;
342 /* Use the first atom as the reference and put other atoms near that one */
343 /* This does not work for large molecules that span > half of the box! */
344 copy_rvec(x[0], reference);
346 /* Calculate either the weighted center or simply the center of geometry */
349 for (i = 0; i < nat; i++)
351 /* PBC distance between position and reference */
352 pbc_dx(pbc, x[i], reference, dx);
354 /* Add PBC distance to reference */
355 rvec_add(reference, dx, correctPBCimage);
357 /* Take weight into account */
358 if (nullptr == weights)
367 svmul(wi, correctPBCimage, weightedPBCimage);
370 rvec_inc(center, weightedPBCimage);
374 svmul(1.0 / wsum, center, center);
378 /*! \brief Return TRUE if position x of ion (or water) is found in the compartment,
379 * i.e. between w1 and w2.
381 * One can define and additional offset "b" if one wants to exchange ions/water
382 * to or from a plane not directly in the middle of w1 and w2. The offset can be
383 * in ]-1.0, ..., +1.0 [.
384 * A bulkOffset of 0.0 means 'no offset', so the swap-layer is directly in the
385 * middle between w1 and w2. Offsets -1.0 < b < 0.0 will yield swaps nearer to w1,
386 * whereas offsets 0.0 < 0 < +1.0 will yield swaps nearer to w2.
390 * ||--------------+-------------|-------------+------------------------||
391 * w1 ? ? ? ? ? ? ? ? ? ? ? w2
392 * ||--------------+-------------|----b--------+------------------------||
397 * \param[in] w1 Position of 'wall' atom 1.
398 * \param[in] w2 Position of 'wall' atom 2.
399 * \param[in] x Position of the ion or the water molecule under consideration.
400 * \param[in] l Length of the box, from || to || in the sketch.
401 * \param[in] bulkOffset Where is the bulk layer "b" to be found between w1 and w2?
402 * \param[out] distance_from_b Distance of x to the bulk layer "b".
404 * \returns TRUE if x is between w1 and w2.
406 * Also computes the distance of x to the compartment center (the layer that is
407 * normally situated in the middle of w1 and w2 that would be considered as having
408 * the bulk concentration of ions).
410 static gmx_bool compartment_contains_atom(real w1, real w2, real x, real l, real bulkOffset, real* distance_from_b)
416 /* First set the origin in the middle of w1 and w2 */
423 /* Now choose the PBC image of x that is closest to the origin: */
434 *distance_from_b = static_cast<real>(fabs(x - bulkOffset * 0.5 * width));
436 /* Return TRUE if we now are in area "????" */
437 return (x >= w1) && (x < w2);
441 /*! \brief Updates the time-averaged number of ions in a compartment. */
442 static void update_time_window(t_compartment* comp, int values, int replace)
448 /* Put in the new value */
451 comp->nMolPast[replace] = comp->nMol;
454 /* Compute the new time-average */
456 for (i = 0; i < values; i++)
458 average += comp->nMolPast[i];
461 comp->nMolAv = average;
465 /*! \brief Add the atom with collective index ci to the atom list in compartment 'comp'.
467 * \param[in] ci Index of this ion in the collective xc array.
468 * \param[inout] comp Compartment to add this atom to.
469 * \param[in] distance Shortest distance of this atom to the bulk layer,
470 * from which ion/water pairs are selected for swapping.
472 static void add_to_list(int ci, t_compartment* comp, real distance)
476 if (nr >= comp->nalloc)
478 comp->nalloc = over_alloc_dd(nr + 1);
479 srenew(comp->ind, comp->nalloc);
480 srenew(comp->dist, comp->nalloc);
483 comp->dist[nr] = distance;
488 /*! \brief Determine the compartment boundaries from the channel centers. */
489 static void get_compartment_boundaries(int c, t_swap* s, const matrix box, real* left, real* right)
492 real leftpos, rightpos, leftpos_orig;
497 gmx_fatal(FARGS, "No compartment %c.", c + 'A');
500 pos0 = s->group[static_cast<int>(SwapGroupSplittingType::Split0)].center[s->swapdim];
501 pos1 = s->group[static_cast<int>(SwapGroupSplittingType::Split1)].center[s->swapdim];
514 /* This gets us the other compartment: */
517 leftpos_orig = leftpos;
519 rightpos = leftpos_orig + box[s->swapdim][s->swapdim];
527 /*! \brief Determine the per-channel ion flux.
529 * To determine the flux through the individual channels, we
530 * remember the compartment and channel history of each ion. An ion can be
531 * either in channel0 or channel1, or in the remaining volume of compartment
535 * +-----------------+
538 * ||||||||||0|||||||| bilayer with channel 0
543 * |||||1||||||||||||| bilayer with channel 1
546 * +-----------------+
550 static void detect_flux_per_channel(t_swapgrp* g,
554 unsigned char* comp_now,
555 unsigned char* comp_from,
556 unsigned char* channel_label,
557 const t_swapcoords* sc,
566 gmx_bool in_cyl0, in_cyl1;
572 /* Check whether ion is inside any of the channels */
573 in_cyl0 = is_in_channel(atomPosition,
574 s->group[static_cast<int>(SwapGroupSplittingType::Split0)].center,
580 in_cyl1 = is_in_channel(atomPosition,
581 s->group[static_cast<int>(SwapGroupSplittingType::Split1)].center,
588 if (in_cyl0 && in_cyl1)
590 /* Ion appears to be in both channels. Something is severely wrong! */
592 *comp_now = eDomainNotset;
593 *comp_from = eDomainNotset;
594 *channel_label = eChHistPassedNone;
598 /* Ion is in channel 0 now */
599 *channel_label = eChHistPassedCh0;
600 *comp_now = eDomainNotset;
605 /* Ion is in channel 1 now */
606 *channel_label = eChHistPassedCh1;
607 *comp_now = eDomainNotset;
612 /* Ion is not in any of the channels, so it must be in domain A or B */
615 *comp_now = eDomainA;
619 *comp_now = eDomainB;
623 /* Only take action, if ion is now in domain A or B, and was before
624 * in the other domain!
626 if (eDomainNotset == *comp_from)
628 /* Maybe we can set the domain now */
629 *comp_from = *comp_now; /* Could still be eDomainNotset, though */
631 else if ((*comp_now != eDomainNotset) /* if in channel */
632 && (*comp_from != *comp_now))
634 /* Obviously the ion changed its domain.
635 * Count this for the channel through which it has passed. */
636 switch (*channel_label)
638 case eChHistPassedNone:
642 " %s Warning! Step %s, ion %d moved from %s to %s\n",
644 gmx_step_str(step, buf),
646 DomainString[*comp_from],
647 DomainString[*comp_now]);
650 fprintf(stderr, ", possibly due to a swap in the original simulation.\n");
655 "but did not pass cyl0 or cyl1 as defined in the .mdp file.\n"
656 "Do you have an ion somewhere within the membrane?\n");
657 /* Write this info to the CompEL output file: */
659 " # Warning: step %s, ion %d moved from %s to %s (probably through the "
661 gmx_step_str(step, buf),
663 DomainString[*comp_from],
664 DomainString[*comp_now]);
667 case eChHistPassedCh0:
668 case eChHistPassedCh1:
669 if (*channel_label == eChHistPassedCh0)
678 if (eDomainA == *comp_from)
680 g->fluxfromAtoB[chan_nr]++;
684 g->fluxfromAtoB[chan_nr]--;
686 fprintf(fpout, "# Atom nr. %d finished passing %s.\n", iAtom, ChannelString[*channel_label]);
689 gmx_fatal(FARGS, "%s Unknown channel history entry for ion type '%s'\n", SwS, g->molname);
692 /* This ion has moved to the _other_ compartment ... */
693 *comp_from = *comp_now;
694 /* ... and it did not pass any channel yet */
695 *channel_label = eChHistPassedNone;
700 /*! \brief Determines which ions or solvent molecules are in compartment A and B */
701 static void sortMoleculesIntoCompartments(t_swapgrp* g,
703 const t_swapcoords* sc,
711 int nMolNotInComp[eCompNR]; /* consistency check */
712 real cyl0_r2 = sc->cyl0r * sc->cyl0r;
713 real cyl1_r2 = sc->cyl1r * sc->cyl1r;
715 /* Get us a counter that cycles in the range of [0 ... sc->nAverage[ */
716 int replace = (step / sc->nstswap) % sc->nAverage;
718 for (int comp = eCompA; comp <= eCompB; comp++)
722 /* Get lists of atoms that match criteria for this compartment */
723 get_compartment_boundaries(comp, s, box, &left, &right);
725 /* First clear the ion molecule lists */
726 g->comp[comp].nMol = 0;
727 nMolNotInComp[comp] = 0; /* consistency check */
729 /* Loop over the molecules and atoms of this group */
730 for (int iMol = 0, iAtom = 0; iAtom < static_cast<int>(g->atomset.numAtomsGlobal());
731 iAtom += g->apm, iMol++)
736 /* Is this first atom of the molecule in the compartment that we look at? */
737 if (compartment_contains_atom(
738 left, right, g->xc[iAtom][sd], box[sd][sd], sc->bulkOffset[comp], &dist))
740 /* Add the first atom of this molecule to the list of molecules in this compartment */
741 add_to_list(iAtom, &g->comp[comp], dist);
743 /* Master also checks for ion groups through which channel each ion has passed */
744 if (MASTER(cr) && (g->comp_now != nullptr) && !bIsSolvent)
746 int globalAtomNr = g->atomset.globalIndex()[iAtom] + 1; /* PDB index starts at 1 ... */
747 detect_flux_per_channel(g,
753 &g->channel_label[iMol],
765 nMolNotInComp[comp]++;
768 /* Correct the time-averaged number of ions in the compartment */
771 update_time_window(&g->comp[comp], sc->nAverage, replace);
775 /* Flux detection warnings */
776 if (MASTER(cr) && !bIsSolvent)
782 "%s Warning: %d atoms were detected as being in both channels! Probably your "
784 "%s cylinder is way too large, or one compartment has collapsed (step "
791 fprintf(s->fpout, "Warning: %d atoms were assigned to both channels!\n", g->nCylBoth);
797 if (bIsSolvent && nullptr != fpout)
800 "# Solv. molecules in comp.%s: %d comp.%s: %d\n",
802 g->comp[eCompA].nMol,
804 g->comp[eCompB].nMol);
807 /* Consistency checks */
808 const auto numMolecules = static_cast<int>(g->atomset.numAtomsGlobal() / g->apm);
809 if (nMolNotInComp[eCompA] + nMolNotInComp[eCompB] != numMolecules)
812 "%s Warning: Inconsistency while assigning '%s' molecules to compartments. !inA: "
813 "%d, !inB: %d, total molecules %d\n",
816 nMolNotInComp[eCompA],
817 nMolNotInComp[eCompB],
821 int sum = g->comp[eCompA].nMol + g->comp[eCompB].nMol;
822 if (sum != numMolecules)
825 "%s Warning: %d molecules are in group '%s', but altogether %d have been assigned "
826 "to the compartments.\n",
835 /*! \brief Find out how many group atoms are in the compartments initially */
836 static void get_initial_ioncounts(const t_inputrec* ir,
838 const rvec x[], /* the initial positions */
848 /* Loop over the user-defined (ion) groups */
849 for (int ig = static_cast<int>(SwapGroupSplittingType::Count); ig < s->ngrp; ig++)
853 /* Copy the initial positions of the atoms in the group
854 * to the collective array so that we can compartmentalize */
855 for (size_t i = 0; i < g->atomset.numAtomsGlobal(); i++)
857 int ind = g->atomset.globalIndex()[i];
858 copy_rvec(x[ind], g->xc[i]);
861 /* Set up the compartments and get lists of atoms in each compartment */
862 sortMoleculesIntoCompartments(g, cr, sc, s, box, 0, s->fpout, bRerun, FALSE);
864 /* Set initial molecule counts if requested (as signaled by "-1" value) */
865 for (int ic = 0; ic < eCompNR; ic++)
867 int requested = sc->grp[ig].nmolReq[ic];
870 g->comp[ic].nMolReq = g->comp[ic].nMol;
874 g->comp[ic].nMolReq = requested;
878 /* Check whether the number of requested molecules adds up to the total number */
879 int req = g->comp[eCompA].nMolReq + g->comp[eCompB].nMolReq;
880 int tot = g->comp[eCompA].nMol + g->comp[eCompB].nMol;
885 "Mismatch of the number of %s ions summed over both compartments.\n"
886 "You requested a total of %d ions (%d in A and %d in B),\n"
887 "but there are a total of %d ions of this type in the system.\n",
890 g->comp[eCompA].nMolReq,
891 g->comp[eCompB].nMolReq,
895 /* Initialize time-averaging:
896 * Write initial concentrations to all time bins to start with */
897 for (int ic = 0; ic < eCompNR; ic++)
899 g->comp[ic].nMolAv = g->comp[ic].nMol;
900 for (int i = 0; i < sc->nAverage; i++)
902 g->comp[ic].nMolPast[i] = g->comp[ic].nMol;
909 /*! \brief Copy history of ion counts from checkpoint file.
911 * When called, the checkpoint file has already been read in. Here we copy
912 * over the values from .cpt file to the swap data structure.
914 static void get_initial_ioncounts_from_cpt(const t_inputrec* ir,
916 swaphistory_t* swapstate,
928 /* Copy the past values from the checkpoint values that have been read in already */
931 fprintf(stderr, "%s Copying values from checkpoint\n", SwS);
934 for (int ig = static_cast<int>(SwapGroupSplittingType::Count); ig < s->ngrp; ig++)
937 gs = &swapstate->ionType[ig - static_cast<int>(SwapGroupSplittingType::Count)];
939 for (int ic = 0; ic < eCompNR; ic++)
941 g->comp[ic].nMolReq = gs->nMolReq[ic];
942 g->comp[ic].inflow_net = gs->inflow_net[ic];
947 "%s ... Influx netto: %d Requested: %d Past values: ",
949 g->comp[ic].inflow_net,
950 g->comp[ic].nMolReq);
953 for (int j = 0; j < sc->nAverage; j++)
955 g->comp[ic].nMolPast[j] = gs->nMolPast[ic][j];
958 fprintf(stderr, "%d ", g->comp[ic].nMolPast[j]);
963 fprintf(stderr, "\n");
971 /*! \brief The master lets all others know about the initial ion counts. */
972 static void bc_initial_concentrations(t_commrec* cr, t_swapcoords* swap, t_swap* s)
978 for (ig = static_cast<int>(SwapGroupSplittingType::Count); ig < s->ngrp; ig++)
982 for (ic = 0; ic < eCompNR; ic++)
984 gmx_bcast(sizeof(g->comp[ic].nMolReq), &(g->comp[ic].nMolReq), cr->mpi_comm_mygroup);
985 gmx_bcast(sizeof(g->comp[ic].nMol), &(g->comp[ic].nMol), cr->mpi_comm_mygroup);
986 gmx_bcast(swap->nAverage * sizeof(g->comp[ic].nMolPast[0]), g->comp[ic].nMolPast, cr->mpi_comm_mygroup);
992 /*! \brief Ensure that each atom belongs to at most one of the swap groups. */
993 static void check_swap_groups(t_swap* s, int nat, gmx_bool bVerbose)
995 int* nGroup = nullptr; /* This array counts for each atom in the MD system to
996 how many swap groups it belongs (should be 0 or 1!) */
998 int nMultiple = 0; /* Number of atoms belonging to multiple groups */
1003 fprintf(stderr, "%s Making sure each atom belongs to at most one of the swap groups.\n", SwS);
1006 /* Add one to the group count of atoms belonging to a swap group: */
1008 for (int i = 0; i < s->ngrp; i++)
1010 t_swapgrp* g = &s->group[i];
1011 for (size_t j = 0; j < g->atomset.numAtomsGlobal(); j++)
1013 /* Get the global index of this atom of this group: */
1014 ind = g->atomset.globalIndex()[j];
1018 /* Make sure each atom belongs to at most one of the groups: */
1019 for (int i = 0; i < nat; i++)
1031 "%s Cannot perform swapping since %d atom%s allocated to more than one swap "
1033 "%s Each atom must be allocated to at most one of the split groups, the swap "
1034 "groups, or the solvent.\n"
1035 "%s Check the .mdp file settings regarding the swap index groups or the index "
1036 "groups themselves.\n",
1039 (1 == nMultiple) ? " is" : "s are",
1046 /*! \brief Get the number of atoms per molecule for this group.
1048 * Also ensure that all the molecules in this group have this number of atoms.
1050 static int get_group_apm_check(int igroup, t_swap* s, gmx_bool bVerbose, const gmx_mtop_t& mtop)
1052 t_swapgrp* g = &s->group[igroup];
1053 const int* ind = s->group[igroup].atomset.globalIndex().data();
1054 int nat = s->group[igroup].atomset.numAtomsGlobal();
1056 /* Determine the number of solvent atoms per solvent molecule from the
1057 * first solvent atom: */
1059 mtopGetMolblockIndex(mtop, ind[0], &molb, nullptr, nullptr);
1060 int apm = mtop.moleculeBlockIndices[molb].numAtomsPerMolecule;
1065 "%s Checking whether all %s molecules consist of %d atom%s\n",
1069 apm > 1 ? "s" : "");
1072 /* Check whether this is also true for all other solvent atoms */
1073 for (int i = 1; i < nat; i++)
1075 mtopGetMolblockIndex(mtop, ind[i], &molb, nullptr, nullptr);
1076 if (apm != mtop.moleculeBlockIndices[molb].numAtomsPerMolecule)
1078 gmx_fatal(FARGS, "Not all molecules of swap group %d consist of %d atoms.", igroup, apm);
1082 // TODO: check whether charges and masses of each molecule are identical!
1087 /*! \brief Print the legend to the swap output file.
1089 * Also print the initial values of ion counts and position of split groups.
1091 static void print_ionlist_legend(const t_inputrec* ir, t_swap* s, const gmx_output_env_t* oenv)
1093 const char** legend;
1097 int nIonTypes = ir->swap->ngrp - static_cast<int>(SwapGroupSplittingType::Count);
1098 snew(legend, eCompNR * nIonTypes * 3 + 2 + eChanNR * nIonTypes + 1);
1100 // Number of molecules and difference to reference counts for each
1101 // compartment and ion type
1102 for (int ic = count = 0; ic < eCompNR; ic++)
1104 for (int ig = static_cast<int>(SwapGroupSplittingType::Count); ig < s->ngrp; ig++)
1106 t_swapGroup* g = &ir->swap->grp[ig];
1107 real q = s->group[ig].q;
1109 snprintf(buf, STRLEN, "%s %s ions (charge %s%g)", CompStr[ic], g->molname, q > 0 ? "+" : "", q);
1110 legend[count++] = gmx_strdup(buf);
1114 "%s av. mismatch to %d %s ions",
1116 s->group[ig].comp[ic].nMolReq,
1118 legend[count++] = gmx_strdup(buf);
1120 snprintf(buf, STRLEN, "%s net %s ion influx", CompStr[ic], g->molname);
1121 legend[count++] = gmx_strdup(buf);
1125 // Center of split groups
1128 "%scenter of %s of split group 0",
1129 SwapStr[ir->eSwapCoords],
1130 (nullptr != s->group[static_cast<int>(SwapGroupSplittingType::Split0)].m) ? "mass" : "geometry");
1131 legend[count++] = gmx_strdup(buf);
1134 "%scenter of %s of split group 1",
1135 SwapStr[ir->eSwapCoords],
1136 (nullptr != s->group[static_cast<int>(SwapGroupSplittingType::Split1)].m) ? "mass" : "geometry");
1137 legend[count++] = gmx_strdup(buf);
1139 // Ion flux for each channel and ion type
1140 for (int ic = 0; ic < eChanNR; ic++)
1142 for (int ig = static_cast<int>(SwapGroupSplittingType::Count); ig < s->ngrp; ig++)
1144 t_swapGroup* g = &ir->swap->grp[ig];
1145 snprintf(buf, STRLEN, "A->ch%d->B %s permeations", ic, g->molname);
1146 legend[count++] = gmx_strdup(buf);
1150 // Number of molecules that leaked from A to B
1151 snprintf(buf, STRLEN, "leakage");
1152 legend[count++] = gmx_strdup(buf);
1154 xvgr_legend(s->fpout, count, legend, oenv);
1157 "# Instantaneous ion counts and time-averaged differences to requested numbers\n");
1159 // We add a simple text legend helping to identify the columns with xvgr legend strings
1160 fprintf(s->fpout, "# time (ps)");
1161 for (int i = 0; i < count; i++)
1163 snprintf(buf, STRLEN, "s%d", i);
1164 fprintf(s->fpout, "%10s", buf);
1166 fprintf(s->fpout, "\n");
1171 /*! \brief Initialize channel ion flux detection routine.
1173 * Initialize arrays that keep track of where the ions come from and where
1176 static void detect_flux_per_channel_init(t_swap* s, swaphistory_t* swapstate, const bool isRestart)
1179 swapstateIons_t* gs;
1181 /* All these flux detection routines run on the master only */
1182 if (swapstate == nullptr)
1187 for (int ig = static_cast<int>(SwapGroupSplittingType::Count); ig < s->ngrp; ig++)
1190 gs = &swapstate->ionType[ig - static_cast<int>(SwapGroupSplittingType::Count)];
1192 /******************************************************/
1193 /* Channel and domain history for the individual ions */
1194 /******************************************************/
1195 if (isRestart) /* set the pointers right */
1197 g->comp_from = gs->comp_from;
1198 g->channel_label = gs->channel_label;
1200 else /* allocate memory for molecule counts */
1202 snew(g->comp_from, g->atomset.numAtomsGlobal() / g->apm);
1203 gs->comp_from = g->comp_from;
1204 snew(g->channel_label, g->atomset.numAtomsGlobal() / g->apm);
1205 gs->channel_label = g->channel_label;
1207 snew(g->comp_now, g->atomset.numAtomsGlobal() / g->apm);
1209 /* Initialize the channel and domain history counters */
1210 for (size_t i = 0; i < g->atomset.numAtomsGlobal() / g->apm; i++)
1212 g->comp_now[i] = eDomainNotset;
1215 g->comp_from[i] = eDomainNotset;
1216 g->channel_label[i] = eChHistPassedNone;
1220 /************************************/
1221 /* Channel fluxes for both channels */
1222 /************************************/
1223 g->nCyl[eChan0] = 0;
1224 g->nCyl[eChan1] = 0;
1230 fprintf(stderr, "%s Copying channel fluxes from checkpoint file data\n", SwS);
1234 // Loop over ion types (and both channels)
1235 for (int ig = static_cast<int>(SwapGroupSplittingType::Count); ig < s->ngrp; ig++)
1238 gs = &swapstate->ionType[ig - static_cast<int>(SwapGroupSplittingType::Count)];
1240 for (int ic = 0; ic < eChanNR; ic++)
1242 fprintf(stderr, "%s Channel %d flux history for ion type %s (charge %g): ", SwS, ic, g->molname, g->q);
1245 g->fluxfromAtoB[ic] = gs->fluxfromAtoB[ic];
1249 g->fluxfromAtoB[ic] = 0;
1252 fprintf(stderr, "%d molecule%s", g->fluxfromAtoB[ic], g->fluxfromAtoB[ic] == 1 ? "" : "s");
1253 fprintf(stderr, "\n");
1257 /* Set pointers for checkpoint writing */
1258 swapstate->fluxleak_p = &s->fluxleak;
1259 for (int ig = static_cast<int>(SwapGroupSplittingType::Count); ig < s->ngrp; ig++)
1262 gs = &swapstate->ionType[ig - static_cast<int>(SwapGroupSplittingType::Count)];
1264 for (int ic = 0; ic < eChanNR; ic++)
1266 gs->fluxfromAtoB_p[ic] = &g->fluxfromAtoB[ic];
1272 /*! \brief Outputs the initial structure to PDB file for debugging reasons.
1274 * Output the starting structure so that in case of multimeric channels
1275 * the user can check whether we have the correct PBC image for all atoms.
1276 * If this is not correct, the ion counts per channel will be very likely
1279 static void outputStartStructureIfWanted(const gmx_mtop_t& mtop, rvec* x, PbcType pbcType, const matrix box)
1281 char* env = getenv("GMX_COMPELDUMP");
1286 "\n%s Found env.var. GMX_COMPELDUMP, will output CompEL starting structure made "
1288 "%s In case of multimeric channels, please check whether they have the correct PBC "
1289 "representation.\n",
1293 write_sto_conf_mtop(
1294 "CompELAssumedWholeConfiguration.pdb", *mtop.name, mtop, x, nullptr, pbcType, box);
1299 /*! \brief Initialize the swapstate structure, used for checkpoint writing.
1301 * The swapstate struct stores the information we need to make the channels
1302 * whole again after restarts from a checkpoint file. Here we do the following:
1303 * a) If we did not start from .cpt, we prepare the struct for proper .cpt writing,
1304 * b) if we did start from .cpt, we copy over the last whole structures from .cpt,
1305 * c) in any case, for subsequent checkpoint writing, we set the pointers in
1306 * swapstate to the x_old arrays, which contain the correct PBC representation of
1307 * multimeric channels at the last time step.
1309 static void init_swapstate(swaphistory_t* swapstate,
1312 const gmx_mtop_t& mtop,
1313 const rvec* x, /* the initial positions */
1315 const t_inputrec* ir)
1317 rvec* x_pbc = nullptr; /* positions of the whole MD system with molecules made whole */
1321 /* We always need the last whole positions such that
1322 * in the next time step we can make the channels whole again in PBC */
1323 if (swapstate->bFromCpt)
1325 /* Copy the last whole positions of each channel from .cpt */
1326 g = &(s->group[static_cast<int>(SwapGroupSplittingType::Split0)]);
1327 for (size_t i = 0; i < g->atomset.numAtomsGlobal(); i++)
1329 copy_rvec(swapstate->xc_old_whole[eChan0][i], g->xc_old[i]);
1331 g = &(s->group[static_cast<int>(SwapGroupSplittingType::Split1)]);
1332 for (size_t i = 0; i < g->atomset.numAtomsGlobal(); i++)
1334 copy_rvec(swapstate->xc_old_whole[eChan1][i], g->xc_old[i]);
1339 swapstate->eSwapCoords = ir->eSwapCoords;
1341 /* Set the number of ion types and allocate memory for checkpointing */
1342 swapstate->nIonTypes = s->ngrp - static_cast<int>(SwapGroupSplittingType::Count);
1343 snew(swapstate->ionType, swapstate->nIonTypes);
1345 /* Store the total number of ions of each type in the swapstateIons
1346 * structure that is accessible during checkpoint writing */
1347 for (int ii = 0; ii < swapstate->nIonTypes; ii++)
1349 swapstateIons_t* gs = &swapstate->ionType[ii];
1350 gs->nMol = sc->grp[ii + static_cast<int>(SwapGroupSplittingType::Count)].nat;
1353 /* Extract the initial split group positions. */
1355 /* Remove pbc, make molecule whole. */
1356 snew(x_pbc, mtop.natoms);
1357 copy_rvecn(x, x_pbc, 0, mtop.natoms);
1359 /* This can only make individual molecules whole, not multimers */
1360 do_pbc_mtop(ir->pbcType, box, &mtop, x_pbc);
1362 /* Output the starting structure? */
1363 outputStartStructureIfWanted(mtop, x_pbc, ir->pbcType, box);
1365 /* If this is the first run (i.e. no checkpoint present) we assume
1366 * that the starting positions give us the correct PBC representation */
1367 for (int ig = static_cast<int>(SwapGroupSplittingType::Split0);
1368 ig <= static_cast<int>(SwapGroupSplittingType::Split1);
1371 g = &(s->group[ig]);
1372 for (size_t i = 0; i < g->atomset.numAtomsGlobal(); i++)
1374 copy_rvec(x_pbc[g->atomset.globalIndex()[i]], g->xc_old[i]);
1379 /* Prepare swapstate arrays for later checkpoint writing */
1380 swapstate->nat[eChan0] =
1381 s->group[static_cast<int>(SwapGroupSplittingType::Split0)].atomset.numAtomsGlobal();
1382 swapstate->nat[eChan1] =
1383 s->group[static_cast<int>(SwapGroupSplittingType::Split1)].atomset.numAtomsGlobal();
1386 /* For subsequent checkpoint writing, set the swapstate pointers to the xc_old
1387 * arrays that get updated at every swapping step */
1388 swapstate->xc_old_whole_p[eChan0] = &s->group[static_cast<int>(SwapGroupSplittingType::Split0)].xc_old;
1389 swapstate->xc_old_whole_p[eChan1] = &s->group[static_cast<int>(SwapGroupSplittingType::Split1)].xc_old;
1392 /*! \brief Determine the total charge imbalance resulting from the swap groups */
1393 static real getRequestedChargeImbalance(t_swap* s)
1398 real particle_charge;
1399 real particle_number[eCompNR];
1401 // s->deltaQ = ( (-1) * s->comp[eCompA][eIonNEG].nat_req + s->comp[eCompA][eIonPOS].nat_req )
1402 // - ( (-1) * s->comp[eCompB][eIonNEG].nat_req + s->comp[eCompB][eIonPOS].nat_req );
1404 for (ig = static_cast<int>(SwapGroupSplittingType::Count); ig < s->ngrp; ig++)
1408 particle_charge = g->q;
1409 particle_number[eCompA] = g->comp[eCompA].nMolReq;
1410 particle_number[eCompB] = g->comp[eCompB].nMolReq;
1412 DeltaQ += particle_charge * (particle_number[eCompA] - particle_number[eCompB]);
1419 /*! \brief Sorts anions and cations into two separate groups
1421 * This routine should be called for the 'anions' and 'cations' group,
1422 * of which the indices were lumped together in the older version of the code.
1424 static void copyIndicesToGroup(const int* indIons, int nIons, t_swapGroup* g, t_commrec* cr)
1428 /* If explicit ion counts were requested in the .mdp file
1429 * (by setting positive values for the number of ions),
1430 * we can make an additional consistency check here */
1431 if ((g->nmolReq[eCompA] < 0) && (g->nmolReq[eCompB] < 0))
1433 if (g->nat != (g->nmolReq[eCompA] + g->nmolReq[eCompB]))
1435 gmx_fatal_collective(FARGS,
1438 "%s Inconsistency while importing swap-related data from an old "
1439 "input file version.\n"
1440 "%s The requested ion counts in compartments A (%d) and B (%d)\n"
1441 "%s do not add up to the number of ions (%d) of this type for the "
1453 srenew(g->ind, g->nat);
1454 for (int i = 0; i < g->nat; i++)
1456 g->ind[i] = indIons[i];
1461 /*! \brief Converts old .tpr file CompEL contents to new data layout.
1463 * If we have read an old .tpr file (tpxv <= tpxv_CompElPolyatomicIonsAndMultipleIonTypes),
1464 * anions and cations are stored together in group #3. In the new
1465 * format we store each ion type in a separate group.
1466 * The 'classic' groups are:
1467 * #0 split group 0 - OK
1468 * #1 split group 1 - OK
1470 * #3 anions - contains also cations, needs to be converted
1471 * #4 cations - empty before conversion
1474 static void convertOldToNewGroupFormat(t_swapcoords* sc, const gmx_mtop_t& mtop, gmx_bool bVerbose, t_commrec* cr)
1476 t_swapGroup* g = &sc->grp[3];
1478 /* Loop through the atom indices of group #3 (anions) and put all indices
1479 * that belong to cations into the cation group.
1483 int* indAnions = nullptr;
1484 int* indCations = nullptr;
1485 snew(indAnions, g->nat);
1486 snew(indCations, g->nat);
1489 for (int i = 0; i < g->nat; i++)
1491 const t_atom& atom = mtopGetAtomParameters(mtop, g->ind[i], &molb);
1494 // This is an anion, add it to the list of anions
1495 indAnions[nAnions++] = g->ind[i];
1499 // This is a cation, add it to the list of cations
1500 indCations[nCations++] = g->ind[i];
1507 "%s Sorted %d ions into separate groups of %d anions and %d cations.\n",
1515 /* Now we have the correct lists of anions and cations.
1516 * Copy it to the right groups.
1518 copyIndicesToGroup(indAnions, nAnions, g, cr);
1520 copyIndicesToGroup(indCations, nCations, g, cr);
1526 /*! \brief Returns TRUE if we started from an old .tpr
1528 * Then we need to re-sort anions and cations into separate groups */
1529 static gmx_bool bConvertFromOldTpr(t_swapcoords* sc)
1531 // If the last group has no atoms it means we need to convert!
1532 return (sc->ngrp >= 5) && (0 == sc->grp[4].nat);
1536 t_swap* init_swapcoords(FILE* fplog,
1537 const t_inputrec* ir,
1539 const gmx_mtop_t& mtop,
1540 const t_state* globalState,
1541 ObservablesHistory* oh,
1543 gmx::LocalAtomSetManager* atomSets,
1544 const gmx_output_env_t* oenv,
1545 const gmx::MdrunOptions& mdrunOptions,
1546 const gmx::StartingBehavior startingBehavior)
1549 swapstateIons_t* gs;
1550 swaphistory_t* swapstate = nullptr;
1552 if ((PAR(cr)) && !DOMAINDECOMP(cr))
1554 gmx_fatal(FARGS, "Position swapping is only implemented for domain decomposition!");
1558 auto s = new t_swap();
1560 if (mdrunOptions.rerun)
1565 "%s This module does not support reruns in parallel\nPlease request a serial "
1566 "run with -nt 1 / -np 1\n",
1570 fprintf(stderr, "%s Rerun - using every available frame\n", SwS);
1572 sc->nAverage = 1; /* averaging makes no sense for reruns */
1575 if (MASTER(cr) && startingBehavior == gmx::StartingBehavior::NewSimulation)
1577 fprintf(fplog, "\nInitializing ion/water position exchanges\n");
1578 please_cite(fplog, "Kutzner2011b");
1581 switch (ir->eSwapCoords)
1583 case SwapType::X: s->swapdim = XX; break;
1584 case SwapType::Y: s->swapdim = YY; break;
1585 case SwapType::Z: s->swapdim = ZZ; break;
1586 default: s->swapdim = -1; break;
1589 const gmx_bool bVerbose = mdrunOptions.verbose;
1591 // For compatibility with old .tpr files
1592 if (bConvertFromOldTpr(sc))
1594 convertOldToNewGroupFormat(sc, mtop, bVerbose && MASTER(cr), cr);
1597 /* Copy some data and pointers to the group structures for convenience */
1598 /* Number of atoms in the group */
1600 for (int i = 0; i < s->ngrp; i++)
1602 s->group.emplace_back(atomSets->add(
1603 gmx::ArrayRef<const int>(sc->grp[i].ind, sc->grp[i].ind + sc->grp[i].nat)));
1604 s->group[i].molname = sc->grp[i].molname;
1607 /* Check for overlapping atoms */
1608 check_swap_groups(s, mtop.natoms, bVerbose && MASTER(cr));
1610 /* Allocate space for the collective arrays for all groups */
1611 /* For the collective position array */
1612 for (int i = 0; i < s->ngrp; i++)
1615 snew(g->xc, g->atomset.numAtomsGlobal());
1617 /* For the split groups (the channels) we need some extra memory to
1618 * be able to make the molecules whole even if they span more than
1619 * half of the box size. */
1620 if ((i == static_cast<int>(SwapGroupSplittingType::Split0))
1621 || (i == static_cast<int>(SwapGroupSplittingType::Split1)))
1623 snew(g->xc_shifts, g->atomset.numAtomsGlobal());
1624 snew(g->xc_eshifts, g->atomset.numAtomsGlobal());
1625 snew(g->xc_old, g->atomset.numAtomsGlobal());
1631 if (oh->swapHistory == nullptr)
1633 oh->swapHistory = std::make_unique<swaphistory_t>(swaphistory_t{});
1635 swapstate = oh->swapHistory.get();
1637 init_swapstate(swapstate, sc, s, mtop, globalState->x.rvec_array(), globalState->box, ir);
1640 /* After init_swapstate we have a set of (old) whole positions for our
1641 * channels. Now transfer that to all nodes */
1644 for (int ig = static_cast<int>(SwapGroupSplittingType::Split0);
1645 ig <= static_cast<int>(SwapGroupSplittingType::Split1);
1648 g = &(s->group[ig]);
1649 gmx_bcast((g->atomset.numAtomsGlobal()) * sizeof((g->xc_old)[0]), g->xc_old, cr->mpi_comm_mygroup);
1653 /* Make sure that all molecules in the solvent and ion groups contain the
1654 * same number of atoms each */
1655 for (int ig = static_cast<int>(SwapGroupSplittingType::Solvent); ig < s->ngrp; ig++)
1659 g = &(s->group[ig]);
1660 g->apm = get_group_apm_check(ig, s, MASTER(cr) && bVerbose, mtop);
1662 /* Since all molecules of a group are equal, we only need enough space
1663 * to determine properties of a single molecule at at time */
1664 snew(g->m, g->apm); /* For the center of mass */
1665 charge = 0; /* To determine the charge imbalance */
1667 for (int j = 0; j < g->apm; j++)
1669 const t_atom& atom = mtopGetAtomParameters(mtop, g->atomset.globalIndex()[j], &molb);
1673 /* Total charge of one molecule of this group: */
1678 /* Need mass-weighted center of split group? */
1679 for (int j = static_cast<int>(SwapGroupSplittingType::Split0);
1680 j <= static_cast<int>(SwapGroupSplittingType::Split1);
1684 if (sc->massw_split[j])
1686 /* Save the split group masses if mass-weighting is requested */
1687 snew(g->m, g->atomset.numAtomsGlobal());
1689 for (size_t i = 0; i < g->atomset.numAtomsGlobal(); i++)
1691 g->m[i] = mtopGetAtomMass(mtop, g->atomset.globalIndex()[i], &molb);
1696 /* Make a t_pbc struct on all nodes so that the molecules
1697 * chosen for an exchange can be made whole. */
1700 bool restartWithAppending = (startingBehavior == gmx::StartingBehavior::RestartWithAppending);
1705 fprintf(stderr, "%s Opening output file %s%s\n", SwS, fn, restartWithAppending ? " for appending" : "");
1708 s->fpout = gmx_fio_fopen(fn, restartWithAppending ? "a" : "w");
1710 if (!restartWithAppending)
1712 xvgr_header(s->fpout, "Molecule counts", "Time (ps)", "counts", exvggtXNY, oenv);
1714 for (int ig = 0; ig < s->ngrp; ig++)
1716 auto enumValue = static_cast<SwapGroupSplittingType>(ig);
1717 g = &(s->group[ig]);
1719 "# %s group '%s' contains %d atom%s",
1720 ig < static_cast<int>(SwapGroupSplittingType::Count) ? enumValueToString(enumValue)
1723 static_cast<int>(g->atomset.numAtomsGlobal()),
1724 (g->atomset.numAtomsGlobal() > 1) ? "s" : "");
1725 if (!(SwapGroupSplittingType::Split0 == enumValue
1726 || SwapGroupSplittingType::Split1 == enumValue))
1729 " with %d atom%s in each molecule of charge %g",
1731 (g->apm > 1) ? "s" : "",
1734 fprintf(s->fpout, ".\n");
1737 fprintf(s->fpout, "#\n# Initial positions of split groups:\n");
1740 for (int j = static_cast<int>(SwapGroupSplittingType::Split0);
1741 j <= static_cast<int>(SwapGroupSplittingType::Split1);
1744 auto enumValue = static_cast<SwapGroupSplittingType>(j);
1746 for (size_t i = 0; i < g->atomset.numAtomsGlobal(); i++)
1748 copy_rvec(globalState->x[sc->grp[j].ind[i]], g->xc[i]);
1750 /* xc has the correct PBC representation for the two channels, so we do
1751 * not need to correct for that */
1752 get_center(g->xc, g->m, g->atomset.numAtomsGlobal(), g->center);
1753 if (!restartWithAppending)
1756 "# %s group %s-center %5f nm\n",
1757 enumValueToString(enumValue),
1759 g->center[s->swapdim]);
1763 if (!restartWithAppending)
1765 if ((0 != sc->bulkOffset[eCompA]) || (0 != sc->bulkOffset[eCompB]))
1767 fprintf(s->fpout, "#\n");
1769 "# You provided an offset for the position of the bulk layer(s).\n");
1771 "# That means the layers to/from which ions and water molecules are "
1774 "# are not midway (= at 0.0) between the compartment-defining layers (at "
1776 fprintf(s->fpout, "# bulk-offsetA = %g\n", sc->bulkOffset[eCompA]);
1777 fprintf(s->fpout, "# bulk-offsetB = %g\n", sc->bulkOffset[eCompB]);
1780 fprintf(s->fpout, "#\n");
1782 "# Split0 cylinder radius %f nm, up %f nm, down %f nm\n",
1787 "# Split1 cylinder radius %f nm, up %f nm, down %f nm\n",
1792 fprintf(s->fpout, "#\n");
1793 if (!mdrunOptions.rerun)
1796 "# Coupling constant (number of swap attempt steps to average over): %d "
1797 "(translates to %f ps).\n",
1799 sc->nAverage * sc->nstswap * ir->delta_t);
1800 fprintf(s->fpout, "# Threshold is %f\n", sc->threshold);
1801 fprintf(s->fpout, "#\n");
1803 "# Remarks about which atoms passed which channel use global atoms numbers "
1804 "starting at one.\n");
1813 /* Allocate memory to remember the past particle counts for time averaging */
1814 for (int ig = static_cast<int>(SwapGroupSplittingType::Count); ig < s->ngrp; ig++)
1816 g = &(s->group[ig]);
1817 for (int ic = 0; ic < eCompNR; ic++)
1819 snew(g->comp[ic].nMolPast, sc->nAverage);
1823 /* Get the initial particle concentrations and let the other nodes know */
1826 if (startingBehavior != gmx::StartingBehavior::NewSimulation)
1828 get_initial_ioncounts_from_cpt(ir, s, swapstate, cr, bVerbose);
1832 fprintf(stderr, "%s Determining initial numbers of ions per compartment.\n", SwS);
1833 get_initial_ioncounts(
1834 ir, s, globalState->x.rvec_array(), globalState->box, cr, mdrunOptions.rerun);
1837 /* Prepare (further) checkpoint writes ... */
1838 if (startingBehavior != gmx::StartingBehavior::NewSimulation)
1840 /* Consistency check */
1841 if (swapstate->nAverage != sc->nAverage)
1844 "%s Ion count averaging steps mismatch! checkpoint: %d, tpr: %d",
1846 swapstate->nAverage,
1852 swapstate->nAverage = sc->nAverage;
1854 fprintf(stderr, "%s Setting pointers for checkpoint writing\n", SwS);
1855 for (int ic = 0; ic < eCompNR; ic++)
1857 for (int ig = static_cast<int>(SwapGroupSplittingType::Count); ig < s->ngrp; ig++)
1860 gs = &swapstate->ionType[ig - static_cast<int>(SwapGroupSplittingType::Count)];
1862 gs->nMolReq_p[ic] = &(g->comp[ic].nMolReq);
1863 gs->nMolPast_p[ic] = &(g->comp[ic].nMolPast[0]);
1864 gs->inflow_net_p[ic] = &(g->comp[ic].inflow_net);
1868 /* Determine the total charge imbalance */
1869 s->deltaQ = getRequestedChargeImbalance(s);
1873 fprintf(stderr, "%s Requested charge imbalance is Q(A) - Q(B) = %g e.\n", SwS, s->deltaQ);
1875 if (!restartWithAppending)
1877 fprintf(s->fpout, "# Requested charge imbalance is Q(A)-Q(B) = %g e.\n", s->deltaQ);
1883 bc_initial_concentrations(cr, ir->swap, s);
1886 /* Update the time-averaged number of molecules for all groups and compartments */
1887 for (int ig = static_cast<int>(SwapGroupSplittingType::Count); ig < sc->ngrp; ig++)
1890 for (int ic = 0; ic < eCompNR; ic++)
1892 update_time_window(&g->comp[ic], sc->nAverage, -1);
1896 /* Initialize arrays that keep track of through which channel the ions go */
1897 detect_flux_per_channel_init(s, swapstate, startingBehavior != gmx::StartingBehavior::NewSimulation);
1899 /* We need to print the legend if we open this file for the first time. */
1900 if (MASTER(cr) && !restartWithAppending)
1902 print_ionlist_legend(ir, s, oenv);
1908 void finish_swapcoords(t_swap* s)
1916 // Close the swap output file
1917 gmx_fio_fclose(s->fpout);
1921 /*! \brief Do we need to swap a molecule in any of the ion groups with a water molecule at this step?
1923 * From the requested and average molecule counts we determine whether a swap is needed
1924 * at this time step.
1926 static gmx_bool need_swap(const t_swapcoords* sc, t_swap* s)
1931 for (ig = static_cast<int>(SwapGroupSplittingType::Count); ig < sc->ngrp; ig++)
1935 for (ic = 0; ic < eCompNR; ic++)
1937 if (g->comp[ic].nMolReq - g->comp[ic].nMolAv >= sc->threshold)
1947 /*! \brief Return the index of an atom or molecule suitable for swapping.
1949 * Returns the index of an atom that is far off the compartment boundaries,
1950 * that is near to the bulk layer to/from which the swaps take place.
1951 * Other atoms of the molecule (if any) will directly follow the returned index.
1953 * \param[in] comp Structure containing compartment-specific data.
1954 * \param[in] molname Name of the molecule.
1956 * \returns Index of the first atom of the molecule chosen for a position exchange.
1958 static int get_index_of_distant_atom(t_compartment* comp, const char molname[])
1961 real d = GMX_REAL_MAX;
1964 /* comp->nat contains the original number of atoms in this compartment
1965 * prior to doing any swaps. Some of these atoms may already have been
1966 * swapped out, but then they are marked with a distance of GMX_REAL_MAX
1968 for (int iMol = 0; iMol < comp->nMolBefore; iMol++)
1970 if (comp->dist[iMol] < d)
1973 d = comp->dist[ibest];
1980 "Could not get index of %s atom. Compartment contains %d %s molecules before "
1987 /* Set the distance of this index to infinity such that it won't get selected again in
1990 comp->dist[ibest] = GMX_REAL_MAX;
1992 return comp->ind[ibest];
1996 /*! \brief Swaps centers of mass and makes molecule whole if broken */
1997 static void translate_positions(rvec* x, int apm, rvec old_com, rvec new_com, t_pbc* pbc)
2000 rvec reference, dx, correctPBCimage;
2003 /* Use the first atom as the reference for PBC */
2004 copy_rvec(x[0], reference);
2006 for (i = 0; i < apm; i++)
2008 /* PBC distance between position and reference */
2009 pbc_dx(pbc, x[i], reference, dx);
2011 /* Add PBC distance to reference */
2012 rvec_add(reference, dx, correctPBCimage);
2014 /* Subtract old_com from correct image and add new_com */
2015 rvec_dec(correctPBCimage, old_com);
2016 rvec_inc(correctPBCimage, new_com);
2018 copy_rvec(correctPBCimage, x[i]);
2023 /*! \brief Write back the modified local positions from the collective array to the official positions. */
2024 static void apply_modified_positions(swap_group* g, rvec x[])
2026 auto collectiveIndex = g->atomset.collectiveIndex().begin();
2027 for (const auto localIndex : g->atomset.localIndex())
2029 /* Copy the possibly modified position */
2030 copy_rvec(g->xc[*collectiveIndex], x[localIndex]);
2036 gmx_bool do_swapcoords(t_commrec* cr,
2039 const t_inputrec* ir,
2041 gmx_wallcycle* wcycle,
2047 const t_swapcoords* sc = ir->swap;
2048 int j, ic, ig, nswaps;
2049 int thisC, otherC; /* Index into this compartment and the other one */
2050 gmx_bool bSwap = FALSE;
2051 t_swapgrp * g, *gsol;
2053 rvec com_solvent, com_particle; /* solvent and swap molecule's center of mass */
2056 wallcycle_start(wcycle, ewcSWAP);
2058 set_pbc(s->pbc, ir->pbcType, box);
2060 /* Assemble the positions of the split groups, i.e. the channels.
2061 * Here we also pass a shifts array to communicate_group_positions(), so that it can make
2062 * the molecules whole even in cases where they span more than half of the box in
2064 for (ig = static_cast<int>(SwapGroupSplittingType::Split0);
2065 ig <= static_cast<int>(SwapGroupSplittingType::Split1);
2068 g = &(s->group[ig]);
2069 communicate_group_positions(cr,
2075 g->atomset.numAtomsGlobal(),
2076 g->atomset.numAtomsLocal(),
2077 g->atomset.localIndex().data(),
2078 g->atomset.collectiveIndex().data(),
2082 get_center(g->xc, g->m, g->atomset.numAtomsGlobal(), g->center); /* center of split groups == channels */
2085 /* Assemble the positions of the ions (ig = 3, 4, ...). These molecules should
2086 * be small and we can always make them whole with a simple distance check.
2087 * Therefore we pass NULL as third argument. */
2088 for (ig = static_cast<int>(SwapGroupSplittingType::Count); ig < s->ngrp; ig++)
2090 g = &(s->group[ig]);
2091 communicate_group_positions(cr,
2097 g->atomset.numAtomsGlobal(),
2098 g->atomset.numAtomsLocal(),
2099 g->atomset.localIndex().data(),
2100 g->atomset.collectiveIndex().data(),
2104 /* Determine how many ions of this type each compartment contains */
2105 sortMoleculesIntoCompartments(g, cr, sc, s, box, step, s->fpout, bRerun, FALSE);
2108 /* Output how many ions are in the compartments */
2111 print_ionlist(s, t, "");
2114 /* If we are doing a rerun, we are finished here, since we cannot perform
2121 /* Do we have to perform a swap? */
2122 bSwap = need_swap(sc, s);
2125 /* Since we here know that we have to perform ion/water position exchanges,
2126 * we now assemble the solvent positions */
2127 g = &(s->group[static_cast<int>(SwapGroupSplittingType::Solvent)]);
2128 communicate_group_positions(cr,
2134 g->atomset.numAtomsGlobal(),
2135 g->atomset.numAtomsLocal(),
2136 g->atomset.localIndex().data(),
2137 g->atomset.collectiveIndex().data(),
2141 /* Determine how many molecules of solvent each compartment contains */
2142 sortMoleculesIntoCompartments(g, cr, sc, s, box, step, s->fpout, bRerun, TRUE);
2144 /* Save number of solvent molecules per compartment prior to any swaps */
2145 g->comp[eCompA].nMolBefore = g->comp[eCompA].nMol;
2146 g->comp[eCompB].nMolBefore = g->comp[eCompB].nMol;
2148 for (ig = static_cast<int>(SwapGroupSplittingType::Count); ig < s->ngrp; ig++)
2150 g = &(s->group[ig]);
2152 for (ic = 0; ic < eCompNR; ic++)
2154 /* Determine in which compartment ions are missing and where they are too many */
2155 g->vacancy[ic] = g->comp[ic].nMolReq - g->comp[ic].nMolAv;
2157 /* Save number of ions per compartment prior to swaps */
2158 g->comp[ic].nMolBefore = g->comp[ic].nMol;
2162 /* Now actually perform the particle exchanges, one swap group after another */
2163 gsol = &s->group[static_cast<int>(SwapGroupSplittingType::Solvent)];
2164 for (ig = static_cast<int>(SwapGroupSplittingType::Count); ig < s->ngrp; ig++)
2168 for (thisC = 0; thisC < eCompNR; thisC++)
2170 /* Index to the other compartment */
2171 otherC = (thisC + 1) % eCompNR;
2173 while (g->vacancy[thisC] >= sc->threshold)
2175 /* Swap in an ion */
2177 /* Get the xc-index of the first atom of a solvent molecule of this compartment */
2178 isol = get_index_of_distant_atom(&gsol->comp[thisC], gsol->molname);
2180 /* Get the xc-index of a particle from the other compartment */
2181 iion = get_index_of_distant_atom(&g->comp[otherC], g->molname);
2183 get_molecule_center(&gsol->xc[isol], gsol->apm, gsol->m, com_solvent, s->pbc);
2184 get_molecule_center(&g->xc[iion], g->apm, g->m, com_particle, s->pbc);
2186 /* Subtract solvent molecule's center of mass and add swap particle's center of mass */
2187 translate_positions(&gsol->xc[isol], gsol->apm, com_solvent, com_particle, s->pbc);
2188 /* Similarly for the swap particle, subtract com_particle and add com_solvent */
2189 translate_positions(&g->xc[iion], g->apm, com_particle, com_solvent, s->pbc);
2191 /* Keep track of the changes */
2192 g->vacancy[thisC]--;
2193 g->vacancy[otherC]++;
2194 g->comp[thisC].nMol++;
2195 g->comp[otherC].nMol--;
2196 g->comp[thisC].inflow_net++;
2197 g->comp[otherC].inflow_net--;
2198 /* Correct the past time window to still get the right averages from now on */
2199 g->comp[thisC].nMolAv++;
2200 g->comp[otherC].nMolAv--;
2201 for (j = 0; j < sc->nAverage; j++)
2203 g->comp[thisC].nMolPast[j]++;
2204 g->comp[otherC].nMolPast[j]--;
2206 /* Clear ion history */
2209 int iMol = iion / g->apm;
2210 g->channel_label[iMol] = eChHistPassedNone;
2211 g->comp_from[iMol] = eDomainNotset;
2213 /* That was the swap */
2218 if (nswaps && bVerbose)
2221 "%s Performed %d swap%s in step %" PRId64 " for iontype %s.\n",
2224 nswaps > 1 ? "s" : "",
2230 if (s->fpout != nullptr)
2232 print_ionlist(s, t, " # after swap");
2235 /* For the solvent and user-defined swap groups, each rank writes back its
2236 * (possibly modified) local positions to the official position array. */
2237 for (ig = static_cast<int>(SwapGroupSplittingType::Solvent); ig < s->ngrp; ig++)
2240 apply_modified_positions(g, x);
2243 } /* end of if(bSwap) */
2245 wallcycle_stop(wcycle, ewcSWAP);