2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2013,2014,2015,2016,2017,2018,2019, by the GROMACS development team, led by
5 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 * and including many others, as listed in the AUTHORS file in the
7 * top-level source directory and at http://www.gromacs.org.
9 * GROMACS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
14 * GROMACS is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with GROMACS; if not, see
21 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * If you want to redistribute modifications to GROMACS, please
25 * consider that scientific software is very special. Version
26 * control is crucial - bugs must be traceable. We will be happy to
27 * consider code for inclusion in the official distribution, but
28 * derived work must not be called official GROMACS. Details are found
29 * in the README & COPYING files - if they are missing, get the
30 * official version at http://www.gromacs.org.
32 * To help us fund GROMACS development, we humbly ask that you cite
33 * the research papers on the package. Check out http://www.gromacs.org.
37 * Implements functions in swapcoords.h.
39 * \author Carsten Kutzner <ckutzne@gwdg.de>
40 * \ingroup module_swap
44 #include "swapcoords.h"
54 #include "gromacs/domdec/domdec_struct.h"
55 #include "gromacs/domdec/localatomset.h"
56 #include "gromacs/domdec/localatomsetmanager.h"
57 #include "gromacs/fileio/confio.h"
58 #include "gromacs/fileio/gmxfio.h"
59 #include "gromacs/fileio/xvgr.h"
60 #include "gromacs/gmxlib/network.h"
61 #include "gromacs/math/vec.h"
62 #include "gromacs/mdlib/groupcoord.h"
63 #include "gromacs/mdrunutility/handlerestart.h"
64 #include "gromacs/mdtypes/commrec.h"
65 #include "gromacs/mdtypes/imdmodule.h"
66 #include "gromacs/mdtypes/inputrec.h"
67 #include "gromacs/mdtypes/md_enums.h"
68 #include "gromacs/mdtypes/mdrunoptions.h"
69 #include "gromacs/mdtypes/observableshistory.h"
70 #include "gromacs/mdtypes/state.h"
71 #include "gromacs/mdtypes/swaphistory.h"
72 #include "gromacs/pbcutil/pbc.h"
73 #include "gromacs/timing/wallcycle.h"
74 #include "gromacs/topology/mtop_lookup.h"
75 #include "gromacs/topology/topology.h"
76 #include "gromacs/utility/cstringutil.h"
77 #include "gromacs/utility/fatalerror.h"
78 #include "gromacs/utility/pleasecite.h"
79 #include "gromacs/utility/smalloc.h"
80 #include "gromacs/utility/snprintf.h"
82 static const char *SwS = {"SWAP:"}; /**< For output that comes from the swap module */
83 static const char *SwSEmpty = {" "}; /**< Placeholder for multi-line output */
84 static const char* CompStr[eCompNR] = {"A", "B" }; /**< Compartment name */
85 static const char *SwapStr[eSwapTypesNR+1] = { "", "X-", "Y-", "Z-", nullptr}; /**< Name for the swap types. */
86 static const char *DimStr[DIM+1] = { "X", "Y", "Z", nullptr}; /**< Name for the swap dimension. */
88 /** Keep track of through which channel the ions have passed */
89 enum eChannelHistory {
90 eChHistPassedNone, eChHistPassedCh0, eChHistPassedCh1, eChHistNr
92 static const char* ChannelString[eChHistNr] = { "none", "channel0", "channel1" }; /**< Name for the channels */
94 /*! \brief Domain identifier.
96 * Keeps track of from which compartment the ions came before passing the
100 eDomainNotset, eDomainA, eDomainB, eDomainNr
102 static const char* DomainString[eDomainNr] = { "not_assigned", "Domain_A", "Domain_B" }; /**< Name for the domains */
107 extern template LocalAtomSet LocalAtomSetManager::add<void, void>(ArrayRef<const int> globalAtomIndex);
110 * \brief Implement Computational Electrophysiology swapping.
112 class SwapCoordinates final : public IMDModule
115 IMdpOptionProvider *mdpOptionProvider() override { return nullptr; }
116 IMDOutputProvider *outputProvider() override { return nullptr; }
117 void initForceProviders(ForceProviders * /* forceProviders */) override {}
120 std::unique_ptr<IMDModule> createSwapCoordinatesModule()
122 return std::make_unique<SwapCoordinates>();
129 * Structure containing compartment-specific data.
131 typedef struct swap_compartment
133 int nMol; /**< Number of ion or water molecules detected
134 in this compartment. */
135 int nMolBefore; /**< Number of molecules before swapping. */
136 int nMolReq; /**< Requested number of molecules in compartment. */
137 real nMolAv; /**< Time-averaged number of molecules matching
138 the compartment conditions. */
139 int *nMolPast; /**< Past molecule counts for time-averaging. */
140 int *ind; /**< Indices to collective array of atoms. */
141 real *dist; /**< Distance of atom to bulk layer, which is
142 normally the center layer of the compartment */
143 int nalloc; /**< Allocation size for ind array. */
144 int inflow_net; /**< Net inflow of ions into this compartment. */
149 * This structure contains data needed for the groups involved in swapping:
150 * split group 0, split group 1, solvent group, ion groups.
152 typedef struct swap_group
154 /*!\brief Construct a swap group given the managed swap atoms.
156 * \param[in] atomset Managed indices of atoms that are part of the swap group.
158 swap_group(const gmx::LocalAtomSet &atomset);
159 char *molname = nullptr; /**< Name of the group or ion type */
160 int apm = 0; /**< Number of atoms in each molecule */
161 gmx::LocalAtomSet atomset; /**< The atom indices in the swap group */
162 rvec *xc = nullptr; /**< Collective array of group atom positions (size nat) */
163 ivec *xc_shifts = nullptr; /**< Current (collective) shifts (size nat) */
164 ivec *xc_eshifts = nullptr; /**< Extra shifts since last DD step (size nat) */
165 rvec *xc_old = nullptr; /**< Old (collective) positions (size nat) */
166 real q = 0.; /**< Total charge of one molecule of this group */
167 real *m = nullptr; /**< Masses (can be omitted, size apm) */
168 unsigned char *comp_from = nullptr; /**< (Collective) Stores from which compartment this
169 molecule has come. This way we keep track of
170 through which channel an ion permeates
171 (size nMol = nat/apm) */
172 unsigned char *comp_now = nullptr; /**< In which compartment this ion is now (size nMol) */
173 unsigned char *channel_label = nullptr; /**< Which channel was passed at last by this ion?
175 rvec center; /**< Center of the group; COM if masses are used */
176 t_compartment comp[eCompNR]; /**< Distribution of particles of this group across
177 the two compartments */
178 real vacancy[eCompNR]; /**< How many molecules need to be swapped in? */
179 int fluxfromAtoB[eChanNR]; /**< Net flux of ions per channel */
180 int nCyl[eChanNR]; /**< Number of ions residing in a channel */
181 int nCylBoth = 0; /**< Ions assigned to cyl0 and cyl1. Not good. */
184 t_swapgrp::swap_group(const gmx::LocalAtomSet& atomset) : atomset {
190 for (int compartment = eCompA; compartment < eCompNR; ++compartment)
192 comp[compartment] = {};
193 vacancy[compartment] = 0;
195 for (int channel = eChan0; channel < eChanNR; ++channel)
197 fluxfromAtoB[channel] = 0;
203 * Main (private) data structure for the position swapping protocol.
207 int swapdim; /**< One of XX, YY, ZZ */
208 t_pbc *pbc; /**< Needed to make molecules whole. */
209 FILE *fpout; /**< Output file. */
210 int ngrp; /**< Number of t_swapgrp groups */
211 std::vector<t_swapgrp> group; /**< Separate groups for channels, solvent, ions */
212 int fluxleak; /**< Flux not going through any of the channels. */
213 real deltaQ; /**< The charge imbalance between the compartments. */
218 /*! \brief Check whether point is in channel.
220 * A channel is a cylinder defined by a disc
221 * with radius r around its center c. The thickness of the cylinder is
228 * <---------c--------->
234 * \param[in] point The position (xyz) under consideration.
235 * \param[in] center The center of the cylinder.
236 * \param[in] d_up The upper extension of the cylinder.
237 * \param[in] d_down The lower extension.
238 * \param[in] r_cyl2 Cylinder radius squared.
239 * \param[in] pbc Structure with info about periodic boundary conditions.
240 * \param[in] normal The membrane normal direction is typically 3, i.e. z, but can be x or y also.
242 * \returns Whether the point is inside the defined cylindric channel.
244 static gmx_bool is_in_channel(
254 int plane1, plane2; /* Directions tangential to membrane */
257 plane1 = (normal + 1) % 3; /* typically 0, i.e. XX */
258 plane2 = (normal + 2) % 3; /* typically 1, i.e. YY */
260 /* Get the distance vector dr between the point and the center of the cylinder */
261 pbc_dx(pbc, point, center, dr); /* This puts center in the origin */
263 /* Check vertical direction */
264 if ( (dr[normal] > d_up) || (dr[normal] < -d_down) )
269 /* Check radial direction */
270 if ( (dr[plane1]*dr[plane1] + dr[plane2]*dr[plane2]) > r_cyl2)
275 /* All check passed, this point is in the cylinder */
280 /*! \brief Prints output to CompEL output file.
282 * Prints to swap output file how many ions are in each compartment,
283 * where the centers of the split groups are, and how many ions of each type
284 * passed the channels.
286 static void print_ionlist(
289 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 = eSwapFixedGrpNR; 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
307 fprintf(s->fpout, "%10g%10g",
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(
341 rvec weightedPBCimage;
343 rvec reference, correctPBCimage, dx;
346 /* Use the first atom as the reference and put other atoms near that one */
347 /* This does not work for large molecules that span > half of the box! */
348 copy_rvec(x[0], reference);
350 /* Calculate either the weighted center or simply the center of geometry */
353 for (i = 0; i < nat; i++)
355 /* PBC distance between position and reference */
356 pbc_dx(pbc, x[i], reference, dx);
358 /* Add PBC distance to reference */
359 rvec_add(reference, dx, correctPBCimage);
361 /* Take weight into account */
362 if (nullptr == weights)
371 svmul(wi, correctPBCimage, weightedPBCimage);
374 rvec_inc(center, weightedPBCimage);
378 svmul(1.0/wsum, center, center);
383 /*! \brief Return TRUE if position x of ion (or water) is found in the compartment,
384 * i.e. between w1 and w2.
386 * One can define and additional offset "b" if one wants to exchange ions/water
387 * to or from a plane not directly in the middle of w1 and w2. The offset can be
388 * in ]-1.0, ..., +1.0 [.
389 * A bulkOffset of 0.0 means 'no offset', so the swap-layer is directly in the
390 * middle between w1 and w2. Offsets -1.0 < b < 0.0 will yield swaps nearer to w1,
391 * whereas offsets 0.0 < 0 < +1.0 will yield swaps nearer to w2.
395 * ||--------------+-------------|-------------+------------------------||
396 * w1 ? ? ? ? ? ? ? ? ? ? ? w2
397 * ||--------------+-------------|----b--------+------------------------||
402 * \param[in] w1 Position of 'wall' atom 1.
403 * \param[in] w2 Position of 'wall' atom 2.
404 * \param[in] x Position of the ion or the water molecule under consideration.
405 * \param[in] l Length of the box, from || to || in the sketch.
406 * \param[in] bulkOffset Where is the bulk layer "b" to be found between w1 and w2?
407 * \param[out] distance_from_b Distance of x to the bulk layer "b".
409 * \returns TRUE if x is between w1 and w2.
411 * Also computes the distance of x to the compartment center (the layer that is
412 * normally situated in the middle of w1 and w2 that would be considered as having
413 * the bulk concentration of ions).
415 static gmx_bool compartment_contains_atom(
421 real *distance_from_b)
427 /* First set the origin in the middle of w1 and w2 */
434 /* Now choose the PBC image of x that is closest to the origin: */
445 *distance_from_b = static_cast<real>(fabs(x - bulkOffset*0.5*width));
447 /* Return TRUE if we now are in area "????" */
448 return (x >= w1) && (x < w2);
452 /*! \brief Updates the time-averaged number of ions in a compartment. */
453 static void update_time_window(t_compartment *comp, int values, int replace)
459 /* Put in the new value */
462 comp->nMolPast[replace] = comp->nMol;
465 /* Compute the new time-average */
467 for (i = 0; i < values; i++)
469 average += comp->nMolPast[i];
472 comp->nMolAv = average;
476 /*! \brief Add the atom with collective index ci to the atom list in compartment 'comp'.
478 * \param[in] ci Index of this ion in the collective xc array.
479 * \param[inout] comp Compartment to add this atom to.
480 * \param[in] distance Shortest distance of this atom to the bulk layer,
481 * from which ion/water pairs are selected for swapping.
483 static void add_to_list(
490 if (nr >= comp->nalloc)
492 comp->nalloc = over_alloc_dd(nr+1);
493 srenew(comp->ind, comp->nalloc);
494 srenew(comp->dist, comp->nalloc);
497 comp->dist[nr] = distance;
502 /*! \brief Determine the compartment boundaries from the channel centers. */
503 static void get_compartment_boundaries(
507 real *left, real *right)
510 real leftpos, rightpos, leftpos_orig;
515 gmx_fatal(FARGS, "No compartment %c.", c+'A');
518 pos0 = s->group[eGrpSplit0].center[s->swapdim];
519 pos1 = s->group[eGrpSplit1].center[s->swapdim];
532 /* This gets us the other compartment: */
535 leftpos_orig = leftpos;
537 rightpos = leftpos_orig + box[s->swapdim][s->swapdim];
545 /*! \brief Determine the per-channel ion flux.
547 * To determine the flux through the individual channels, we
548 * remember the compartment and channel history of each ion. An ion can be
549 * either in channel0 or channel1, or in the remaining volume of compartment
553 * +-----------------+
556 * ||||||||||0|||||||| bilayer with channel 0
561 * |||||1||||||||||||| bilayer with channel 1
564 * +-----------------+
568 static void detect_flux_per_channel(
573 unsigned char *comp_now,
574 unsigned char *comp_from,
575 unsigned char *channel_label,
585 gmx_bool in_cyl0, in_cyl1;
591 /* Check whether ion is inside any of the channels */
592 in_cyl0 = is_in_channel(atomPosition, s->group[eGrpSplit0].center, sc->cyl0u, sc->cyl0l, cyl0_r2, s->pbc, sd);
593 in_cyl1 = is_in_channel(atomPosition, s->group[eGrpSplit1].center, sc->cyl1u, sc->cyl1l, cyl1_r2, s->pbc, sd);
595 if (in_cyl0 && in_cyl1)
597 /* Ion appears to be in both channels. Something is severely wrong! */
599 *comp_now = eDomainNotset;
600 *comp_from = eDomainNotset;
601 *channel_label = eChHistPassedNone;
605 /* Ion is in channel 0 now */
606 *channel_label = eChHistPassedCh0;
607 *comp_now = eDomainNotset;
612 /* Ion is in channel 1 now */
613 *channel_label = eChHistPassedCh1;
614 *comp_now = eDomainNotset;
619 /* Ion is not in any of the channels, so it must be in domain A or B */
622 *comp_now = eDomainA;
626 *comp_now = eDomainB;
630 /* Only take action, if ion is now in domain A or B, and was before
631 * in the other domain!
633 if (eDomainNotset == *comp_from)
635 /* Maybe we can set the domain now */
636 *comp_from = *comp_now; /* Could still be eDomainNotset, though */
638 else if ( (*comp_now != eDomainNotset ) /* if in channel */
639 && (*comp_from != *comp_now) )
641 /* Obviously the ion changed its domain.
642 * Count this for the channel through which it has passed. */
643 switch (*channel_label)
645 case eChHistPassedNone:
648 fprintf(stderr, " %s Warning! Step %s, ion %d moved from %s to %s\n",
649 SwS, gmx_step_str(step, buf), iAtom, DomainString[*comp_from], DomainString[*comp_now]);
652 fprintf(stderr, ", possibly due to a swap in the original simulation.\n");
656 fprintf(stderr, "but did not pass cyl0 or cyl1 as defined in the .mdp file.\n"
657 "Do you have an ion somewhere within the membrane?\n");
658 /* Write this info to the CompEL output file: */
659 fprintf(s->fpout, " # Warning: step %s, ion %d moved from %s to %s (probably through the membrane)\n",
660 gmx_step_str(step, buf), iAtom,
661 DomainString[*comp_from], DomainString[*comp_now]);
665 case eChHistPassedCh0:
666 case eChHistPassedCh1:
667 if (*channel_label == eChHistPassedCh0)
676 if (eDomainA == *comp_from)
678 g->fluxfromAtoB[chan_nr]++;
682 g->fluxfromAtoB[chan_nr]--;
684 fprintf(fpout, "# Atom nr. %d finished passing %s.\n", iAtom, ChannelString[*channel_label]);
687 gmx_fatal(FARGS, "%s Unknown channel history entry for ion type '%s'\n",
691 /* This ion has moved to the _other_ compartment ... */
692 *comp_from = *comp_now;
693 /* ... and it did not pass any channel yet */
694 *channel_label = eChHistPassedNone;
699 /*! \brief Determines which ions or solvent molecules are in compartment A and B */
700 static void sortMoleculesIntoCompartments(
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()); iAtom += g->apm, iMol++)
735 /* Is this first atom of the molecule in the compartment that we look at? */
736 if (compartment_contains_atom(left, right, g->xc[iAtom][sd], box[sd][sd], sc->bulkOffset[comp], &dist) )
738 /* Add the first atom of this molecule to the list of molecules in this compartment */
739 add_to_list(iAtom, &g->comp[comp], dist);
741 /* Master also checks for ion groups through which channel each ion has passed */
742 if (MASTER(cr) && (g->comp_now != nullptr) && !bIsSolvent)
744 int globalAtomNr = g->atomset.globalIndex()[iAtom] + 1; /* PDB index starts at 1 ... */
745 detect_flux_per_channel(g, globalAtomNr, comp, g->xc[iAtom],
746 &g->comp_now[iMol], &g->comp_from[iMol], &g->channel_label[iMol],
747 sc, s, cyl0_r2, cyl1_r2, step, bRerun, fpout);
752 nMolNotInComp[comp]++;
755 /* Correct the time-averaged number of ions in the compartment */
758 update_time_window(&g->comp[comp], sc->nAverage, replace);
762 /* Flux detection warnings */
763 if (MASTER(cr) && !bIsSolvent)
768 "%s Warning: %d atoms were detected as being in both channels! Probably your split\n"
769 "%s cylinder is way too large, or one compartment has collapsed (step %" PRId64 ")\n",
770 SwS, g->nCylBoth, SwS, step);
772 fprintf(s->fpout, "Warning: %d atoms were assigned to both channels!\n", g->nCylBoth);
778 if (bIsSolvent && nullptr != fpout)
780 fprintf(fpout, "# Solv. molecules in comp.%s: %d comp.%s: %d\n",
781 CompStr[eCompA], g->comp[eCompA].nMol,
782 CompStr[eCompB], g->comp[eCompB].nMol);
785 /* Consistency checks */
786 const auto numMolecules = static_cast<int>(g->atomset.numAtomsGlobal() / g->apm);
787 if (nMolNotInComp[eCompA] + nMolNotInComp[eCompB] != numMolecules)
789 fprintf(stderr, "%s Warning: Inconsistency while assigning '%s' molecules to compartments. !inA: %d, !inB: %d, total molecules %d\n",
790 SwS, g->molname, nMolNotInComp[eCompA], nMolNotInComp[eCompB], numMolecules);
793 int sum = g->comp[eCompA].nMol + g->comp[eCompB].nMol;
794 if (sum != numMolecules)
796 fprintf(stderr, "%s Warning: %d molecules are in group '%s', but altogether %d have been assigned to the compartments.\n",
797 SwS, numMolecules, g->molname, sum);
802 /*! \brief Find out how many group atoms are in the compartments initially */
803 static void get_initial_ioncounts(
804 const t_inputrec *ir,
806 const rvec x[], /* the initial positions */
816 /* Loop over the user-defined (ion) groups */
817 for (int ig = eSwapFixedGrpNR; ig < s->ngrp; ig++)
821 /* Copy the initial positions of the atoms in the group
822 * to the collective array so that we can compartmentalize */
823 for (size_t i = 0; i < g->atomset.numAtomsGlobal(); i++)
825 int ind = g->atomset.globalIndex()[i];
826 copy_rvec(x[ind], g->xc[i]);
829 /* Set up the compartments and get lists of atoms in each compartment */
830 sortMoleculesIntoCompartments(g, cr, sc, s, box, 0, s->fpout, bRerun, FALSE);
832 /* Set initial molecule counts if requested (as signaled by "-1" value) */
833 for (int ic = 0; ic < eCompNR; ic++)
835 int requested = sc->grp[ig].nmolReq[ic];
838 g->comp[ic].nMolReq = g->comp[ic].nMol;
842 g->comp[ic].nMolReq = requested;
846 /* Check whether the number of requested molecules adds up to the total number */
847 int req = g->comp[eCompA].nMolReq + g->comp[eCompB].nMolReq;
848 int tot = g->comp[eCompA].nMol + g->comp[eCompB].nMol;
852 gmx_fatal(FARGS, "Mismatch of the number of %s ions summed over both compartments.\n"
853 "You requested a total of %d ions (%d in A and %d in B),\n"
854 "but there are a total of %d ions of this type in the system.\n",
855 g->molname, req, g->comp[eCompA].nMolReq,
856 g->comp[eCompB].nMolReq, tot);
859 /* Initialize time-averaging:
860 * Write initial concentrations to all time bins to start with */
861 for (int ic = 0; ic < eCompNR; ic++)
863 g->comp[ic].nMolAv = g->comp[ic].nMol;
864 for (int i = 0; i < sc->nAverage; i++)
866 g->comp[ic].nMolPast[i] = g->comp[ic].nMol;
873 /*! \brief Copy history of ion counts from checkpoint file.
875 * When called, the checkpoint file has already been read in. Here we copy
876 * over the values from .cpt file to the swap data structure.
878 static void get_initial_ioncounts_from_cpt(
879 const t_inputrec *ir,
881 swaphistory_t *swapstate,
882 t_commrec *cr, gmx_bool bVerbose)
892 /* Copy the past values from the checkpoint values that have been read in already */
895 fprintf(stderr, "%s Copying values from checkpoint\n", SwS);
898 for (int ig = eSwapFixedGrpNR; ig < s->ngrp; ig++)
901 gs = &swapstate->ionType[ig - eSwapFixedGrpNR];
903 for (int ic = 0; ic < eCompNR; ic++)
905 g->comp[ic].nMolReq = gs->nMolReq[ic];
906 g->comp[ic].inflow_net = gs->inflow_net[ic];
910 fprintf(stderr, "%s ... Influx netto: %d Requested: %d Past values: ", SwS,
911 g->comp[ic].inflow_net, g->comp[ic].nMolReq);
914 for (int j = 0; j < sc->nAverage; j++)
916 g->comp[ic].nMolPast[j] = gs->nMolPast[ic][j];
919 fprintf(stderr, "%d ", g->comp[ic].nMolPast[j]);
924 fprintf(stderr, "\n");
932 /*! \brief The master lets all others know about the initial ion counts. */
933 static void bc_initial_concentrations(
942 for (ig = eSwapFixedGrpNR; ig < s->ngrp; ig++)
946 for (ic = 0; ic < eCompNR; ic++)
948 gmx_bcast(sizeof(g->comp[ic].nMolReq), &(g->comp[ic].nMolReq), cr);
949 gmx_bcast(sizeof(g->comp[ic].nMol ), &(g->comp[ic].nMol ), cr);
950 gmx_bcast( swap->nAverage * sizeof(g->comp[ic].nMolPast[0]), g->comp[ic].nMolPast, cr);
956 /*! \brief Ensure that each atom belongs to at most one of the swap groups. */
957 static void check_swap_groups(t_swap *s, int nat, gmx_bool bVerbose)
959 int *nGroup = nullptr; /* This array counts for each atom in the MD system to
960 how many swap groups it belongs (should be 0 or 1!) */
962 int nMultiple = 0; /* Number of atoms belonging to multiple groups */
967 fprintf(stderr, "%s Making sure each atom belongs to at most one of the swap groups.\n", SwS);
970 /* Add one to the group count of atoms belonging to a swap group: */
972 for (int i = 0; i < s->ngrp; i++)
974 t_swapgrp *g = &s->group[i];
975 for (size_t j = 0; j < g->atomset.numAtomsGlobal(); j++)
977 /* Get the global index of this atom of this group: */
978 ind = g->atomset.globalIndex()[j];
982 /* Make sure each atom belongs to at most one of the groups: */
983 for (int i = 0; i < nat; i++)
994 gmx_fatal(FARGS, "%s Cannot perform swapping since %d atom%s allocated to more than one swap index group.\n"
995 "%s Each atom must be allocated to at most one of the split groups, the swap groups, or the solvent.\n"
996 "%s Check the .mdp file settings regarding the swap index groups or the index groups themselves.\n",
997 SwS, nMultiple, (1 == nMultiple) ? " is" : "s are", SwSEmpty, SwSEmpty);
1002 /*! \brief Get the number of atoms per molecule for this group.
1004 * Also ensure that all the molecules in this group have this number of atoms.
1006 static int get_group_apm_check(
1012 t_swapgrp *g = &s->group[igroup];
1013 const int *ind = s->group[igroup].atomset.globalIndex().data();
1014 int nat = s->group[igroup].atomset.numAtomsGlobal();
1016 /* Determine the number of solvent atoms per solvent molecule from the
1017 * first solvent atom: */
1019 mtopGetMolblockIndex(mtop, ind[0], &molb, nullptr, nullptr);
1020 int apm = mtop->moleculeBlockIndices[molb].numAtomsPerMolecule;
1024 fprintf(stderr, "%s Checking whether all %s molecules consist of %d atom%s\n", SwS,
1025 g->molname, apm, apm > 1 ? "s" : "");
1028 /* Check whether this is also true for all other solvent atoms */
1029 for (int i = 1; i < nat; i++)
1031 mtopGetMolblockIndex(mtop, ind[i], &molb, nullptr, nullptr);
1032 if (apm != mtop->moleculeBlockIndices[molb].numAtomsPerMolecule)
1034 gmx_fatal(FARGS, "Not all molecules of swap group %d consist of %d atoms.",
1039 //TODO: check whether charges and masses of each molecule are identical!
1044 /*! \brief Print the legend to the swap output file.
1046 * Also print the initial values of ion counts and position of split groups.
1048 static void print_ionlist_legend(const t_inputrec *ir,
1050 const gmx_output_env_t *oenv)
1052 const char **legend;
1056 int nIonTypes = ir->swap->ngrp - eSwapFixedGrpNR;
1057 snew(legend, eCompNR*nIonTypes*3 + 2 + eChanNR*nIonTypes + 1);
1059 // Number of molecules and difference to reference counts for each
1060 // compartment and ion type
1061 for (int ic = count = 0; ic < eCompNR; ic++)
1063 for (int ig = eSwapFixedGrpNR; ig < s->ngrp; ig++)
1065 t_swapGroup *g = &ir->swap->grp[ig];
1066 real q = s->group[ig].q;
1068 snprintf(buf, STRLEN, "%s %s ions (charge %s%g)", CompStr[ic], g->molname, q > 0 ? "+" : "", q);
1069 legend[count++] = gmx_strdup(buf);
1071 snprintf(buf, STRLEN, "%s av. mismatch to %d %s ions",
1072 CompStr[ic], s->group[ig].comp[ic].nMolReq, g->molname);
1073 legend[count++] = gmx_strdup(buf);
1075 snprintf(buf, STRLEN, "%s net %s ion influx", CompStr[ic], g->molname);
1076 legend[count++] = gmx_strdup(buf);
1080 // Center of split groups
1081 snprintf(buf, STRLEN, "%scenter of %s of split group 0", SwapStr[ir->eSwapCoords], (nullptr != s->group[eGrpSplit0].m) ? "mass" : "geometry");
1082 legend[count++] = gmx_strdup(buf);
1083 snprintf(buf, STRLEN, "%scenter of %s of split group 1", SwapStr[ir->eSwapCoords], (nullptr != s->group[eGrpSplit1].m) ? "mass" : "geometry");
1084 legend[count++] = gmx_strdup(buf);
1086 // Ion flux for each channel and ion type
1087 for (int ic = 0; ic < eChanNR; ic++)
1089 for (int ig = eSwapFixedGrpNR; ig < s->ngrp; ig++)
1091 t_swapGroup *g = &ir->swap->grp[ig];
1092 snprintf(buf, STRLEN, "A->ch%d->B %s permeations", ic, g->molname);
1093 legend[count++] = gmx_strdup(buf);
1097 // Number of molecules that leaked from A to B
1098 snprintf(buf, STRLEN, "leakage");
1099 legend[count++] = gmx_strdup(buf);
1101 xvgr_legend(s->fpout, count, legend, oenv);
1103 fprintf(s->fpout, "# Instantaneous ion counts and time-averaged differences to requested numbers\n");
1105 // We add a simple text legend helping to identify the columns with xvgr legend strings
1106 fprintf(s->fpout, "# time (ps)");
1107 for (int i = 0; i < count; i++)
1109 snprintf(buf, STRLEN, "s%d", i);
1110 fprintf(s->fpout, "%10s", buf);
1112 fprintf(s->fpout, "\n");
1117 /*! \brief Initialize channel ion flux detection routine.
1119 * Initialize arrays that keep track of where the ions come from and where
1122 static void detect_flux_per_channel_init(
1124 swaphistory_t *swapstate,
1125 const bool isRestart)
1128 swapstateIons_t *gs;
1130 /* All these flux detection routines run on the master only */
1131 if (swapstate == nullptr)
1136 for (int ig = eSwapFixedGrpNR; ig < s->ngrp; ig++)
1139 gs = &swapstate->ionType[ig - eSwapFixedGrpNR];
1141 /******************************************************/
1142 /* Channel and domain history for the individual ions */
1143 /******************************************************/
1144 if (isRestart) /* set the pointers right */
1146 g->comp_from = gs->comp_from;
1147 g->channel_label = gs->channel_label;
1149 else /* allocate memory for molecule counts */
1151 snew(g->comp_from, g->atomset.numAtomsGlobal()/g->apm);
1152 gs->comp_from = g->comp_from;
1153 snew(g->channel_label, g->atomset.numAtomsGlobal()/g->apm);
1154 gs->channel_label = g->channel_label;
1156 snew(g->comp_now, g->atomset.numAtomsGlobal()/g->apm);
1158 /* Initialize the channel and domain history counters */
1159 for (size_t i = 0; i < g->atomset.numAtomsGlobal()/g->apm; i++)
1161 g->comp_now[i] = eDomainNotset;
1164 g->comp_from[i] = eDomainNotset;
1165 g->channel_label[i] = eChHistPassedNone;
1169 /************************************/
1170 /* Channel fluxes for both channels */
1171 /************************************/
1172 g->nCyl[eChan0] = 0;
1173 g->nCyl[eChan1] = 0;
1179 fprintf(stderr, "%s Copying channel fluxes from checkpoint file data\n", SwS);
1183 // Loop over ion types (and both channels)
1184 for (int ig = eSwapFixedGrpNR; ig < s->ngrp; ig++)
1187 gs = &swapstate->ionType[ig - eSwapFixedGrpNR];
1189 for (int ic = 0; ic < eChanNR; ic++)
1191 fprintf(stderr, "%s Channel %d flux history for ion type %s (charge %g): ", SwS, ic, g->molname, g->q);
1194 g->fluxfromAtoB[ic] = gs->fluxfromAtoB[ic];
1198 g->fluxfromAtoB[ic] = 0;
1201 fprintf(stderr, "%d molecule%s",
1202 g->fluxfromAtoB[ic], g->fluxfromAtoB[ic] == 1 ? "" : "s");
1203 fprintf(stderr, "\n");
1207 /* Set pointers for checkpoint writing */
1208 swapstate->fluxleak_p = &s->fluxleak;
1209 for (int ig = eSwapFixedGrpNR; ig < s->ngrp; ig++)
1212 gs = &swapstate->ionType[ig - eSwapFixedGrpNR];
1214 for (int ic = 0; ic < eChanNR; ic++)
1216 gs->fluxfromAtoB_p[ic] = &g->fluxfromAtoB[ic];
1222 /*! \brief Outputs the initial structure to PDB file for debugging reasons.
1224 * Output the starting structure so that in case of multimeric channels
1225 * the user can check whether we have the correct PBC image for all atoms.
1226 * If this is not correct, the ion counts per channel will be very likely
1229 static void outputStartStructureIfWanted(gmx_mtop_t *mtop, rvec *x, int ePBC, const matrix box)
1231 char *env = getenv("GMX_COMPELDUMP");
1235 fprintf(stderr, "\n%s Found env.var. GMX_COMPELDUMP, will output CompEL starting structure made whole.\n"
1236 "%s In case of multimeric channels, please check whether they have the correct PBC representation.\n",
1239 write_sto_conf_mtop("CompELAssumedWholeConfiguration.pdb", *mtop->name, mtop, x, nullptr, ePBC, box);
1244 /*! \brief Initialize the swapstate structure, used for checkpoint writing.
1246 * The swapstate struct stores the information we need to make the channels
1247 * whole again after restarts from a checkpoint file. Here we do the following:
1248 * a) If we did not start from .cpt, we prepare the struct for proper .cpt writing,
1249 * b) if we did start from .cpt, we copy over the last whole structures from .cpt,
1250 * c) in any case, for subsequent checkpoint writing, we set the pointers in
1251 * swapstate to the x_old arrays, which contain the correct PBC representation of
1252 * multimeric channels at the last time step.
1254 static void init_swapstate(
1255 swaphistory_t *swapstate,
1259 const rvec *x, /* the initial positions */
1261 const t_inputrec *ir)
1263 rvec *x_pbc = nullptr; /* positions of the whole MD system with molecules made whole */
1267 /* We always need the last whole positions such that
1268 * in the next time step we can make the channels whole again in PBC */
1269 if (swapstate->bFromCpt)
1271 /* Copy the last whole positions of each channel from .cpt */
1272 g = &(s->group[eGrpSplit0]);
1273 for (size_t i = 0; i < g->atomset.numAtomsGlobal(); i++)
1275 copy_rvec(swapstate->xc_old_whole[eChan0][i], g->xc_old[i]);
1277 g = &(s->group[eGrpSplit1]);
1278 for (size_t i = 0; i < g->atomset.numAtomsGlobal(); i++)
1280 copy_rvec(swapstate->xc_old_whole[eChan1][i], g->xc_old[i]);
1285 swapstate->eSwapCoords = ir->eSwapCoords;
1287 /* Set the number of ion types and allocate memory for checkpointing */
1288 swapstate->nIonTypes = s->ngrp - eSwapFixedGrpNR;
1289 snew(swapstate->ionType, swapstate->nIonTypes);
1291 /* Store the total number of ions of each type in the swapstateIons
1292 * structure that is accessible during checkpoint writing */
1293 for (int ii = 0; ii < swapstate->nIonTypes; ii++)
1295 swapstateIons_t *gs = &swapstate->ionType[ii];
1296 gs->nMol = sc->grp[ii + eSwapFixedGrpNR].nat;
1299 /* Extract the initial split group positions. */
1301 /* Remove pbc, make molecule whole. */
1302 snew(x_pbc, mtop->natoms);
1303 copy_rvecn(x, x_pbc, 0, mtop->natoms);
1305 /* This can only make individual molecules whole, not multimers */
1306 do_pbc_mtop(ir->ePBC, box, mtop, x_pbc);
1308 /* Output the starting structure? */
1309 outputStartStructureIfWanted(mtop, x_pbc, ir->ePBC, box);
1311 /* If this is the first run (i.e. no checkpoint present) we assume
1312 * that the starting positions give us the correct PBC representation */
1313 for (int ig = eGrpSplit0; ig <= eGrpSplit1; ig++)
1315 g = &(s->group[ig]);
1316 for (size_t i = 0; i < g->atomset.numAtomsGlobal(); i++)
1318 copy_rvec(x_pbc[g->atomset.globalIndex()[i]], g->xc_old[i]);
1323 /* Prepare swapstate arrays for later checkpoint writing */
1324 swapstate->nat[eChan0] = s->group[eGrpSplit0].atomset.numAtomsGlobal();
1325 swapstate->nat[eChan1] = s->group[eGrpSplit1].atomset.numAtomsGlobal();
1328 /* For subsequent checkpoint writing, set the swapstate pointers to the xc_old
1329 * arrays that get updated at every swapping step */
1330 swapstate->xc_old_whole_p[eChan0] = &s->group[eGrpSplit0].xc_old;
1331 swapstate->xc_old_whole_p[eChan1] = &s->group[eGrpSplit1].xc_old;
1334 /*! \brief Determine the total charge imbalance resulting from the swap groups */
1335 static real getRequestedChargeImbalance(t_swap *s)
1340 real particle_charge;
1341 real particle_number[eCompNR];
1343 // s->deltaQ = ( (-1) * s->comp[eCompA][eIonNEG].nat_req + s->comp[eCompA][eIonPOS].nat_req )
1344 // - ( (-1) * s->comp[eCompB][eIonNEG].nat_req + s->comp[eCompB][eIonPOS].nat_req );
1346 for (ig = eSwapFixedGrpNR; ig < s->ngrp; ig++)
1350 particle_charge = g->q;
1351 particle_number[eCompA] = g->comp[eCompA].nMolReq;
1352 particle_number[eCompB] = g->comp[eCompB].nMolReq;
1354 DeltaQ += particle_charge * (particle_number[eCompA] - particle_number[eCompB]);
1361 /*! \brief Sorts anions and cations into two separate groups
1363 * This routine should be called for the 'anions' and 'cations' group,
1364 * of which the indices were lumped together in the older version of the code.
1366 static void copyIndicesToGroup(
1374 /* If explicit ion counts were requested in the .mdp file
1375 * (by setting positive values for the number of ions),
1376 * we can make an additional consistency check here */
1377 if ( (g->nmolReq[eCompA] < 0) && (g->nmolReq[eCompB] < 0) )
1379 if (g->nat != (g->nmolReq[eCompA] + g->nmolReq[eCompB]) )
1381 gmx_fatal_collective(FARGS, cr->mpi_comm_mysim, MASTER(cr),
1382 "%s Inconsistency while importing swap-related data from an old input file version.\n"
1383 "%s The requested ion counts in compartments A (%d) and B (%d)\n"
1384 "%s do not add up to the number of ions (%d) of this type for the group '%s'.\n",
1385 SwS, SwSEmpty, g->nmolReq[eCompA], g->nmolReq[eCompB], SwSEmpty, g->nat, g->molname);
1389 srenew(g->ind, g->nat);
1390 for (int i = 0; i < g->nat; i++)
1392 g->ind[i] = indIons[i];
1397 /*! \brief Converts old .tpr file CompEL contents to new data layout.
1399 * If we have read an old .tpr file (tpxv <= tpxv_CompElPolyatomicIonsAndMultipleIonTypes),
1400 * anions and cations are stored together in group #3. In the new
1401 * format we store each ion type in a separate group.
1402 * The 'classic' groups are:
1403 * #0 split group 0 - OK
1404 * #1 split group 1 - OK
1406 * #3 anions - contains also cations, needs to be converted
1407 * #4 cations - empty before conversion
1410 static void convertOldToNewGroupFormat(
1416 t_swapGroup *g = &sc->grp[3];
1418 /* Loop through the atom indices of group #3 (anions) and put all indices
1419 * that belong to cations into the cation group.
1423 int *indAnions = nullptr;
1424 int *indCations = nullptr;
1425 snew(indAnions, g->nat);
1426 snew(indCations, g->nat);
1429 for (int i = 0; i < g->nat; i++)
1431 const t_atom &atom = mtopGetAtomParameters(mtop, g->ind[i], &molb);
1434 // This is an anion, add it to the list of anions
1435 indAnions[nAnions++] = g->ind[i];
1439 // This is a cation, add it to the list of cations
1440 indCations[nCations++] = g->ind[i];
1446 fprintf(stdout, "%s Sorted %d ions into separate groups of %d anions and %d cations.\n",
1447 SwS, g->nat, nAnions, nCations);
1451 /* Now we have the correct lists of anions and cations.
1452 * Copy it to the right groups.
1454 copyIndicesToGroup(indAnions, nAnions, g, cr);
1456 copyIndicesToGroup(indCations, nCations, g, cr);
1462 /*! \brief Returns TRUE if we started from an old .tpr
1464 * Then we need to re-sort anions and cations into separate groups */
1465 static gmx_bool bConvertFromOldTpr(t_swapcoords *sc)
1467 // If the last group has no atoms it means we need to convert!
1468 return (sc->ngrp >= 5) && (0 == sc->grp[4].nat);
1472 t_swap *init_swapcoords(
1474 const t_inputrec *ir,
1477 const t_state *globalState,
1478 ObservablesHistory *oh,
1480 gmx::LocalAtomSetManager *atomSets,
1481 const gmx_output_env_t *oenv,
1482 const gmx::MdrunOptions &mdrunOptions,
1483 const gmx::StartingBehavior startingBehavior)
1486 swapstateIons_t *gs;
1487 swaphistory_t *swapstate = nullptr;
1489 if ( (PAR(cr)) && !DOMAINDECOMP(cr) )
1491 gmx_fatal(FARGS, "Position swapping is only implemented for domain decomposition!");
1495 auto s = new t_swap();
1497 if (mdrunOptions.rerun)
1501 gmx_fatal(FARGS, "%s This module does not support reruns in parallel\nPlease request a serial run with -nt 1 / -np 1\n", SwS);
1504 fprintf(stderr, "%s Rerun - using every available frame\n", SwS);
1506 sc->nAverage = 1; /* averaging makes no sense for reruns */
1509 if (MASTER(cr) && startingBehavior == gmx::StartingBehavior::NewSimulation)
1511 fprintf(fplog, "\nInitializing ion/water position exchanges\n");
1512 please_cite(fplog, "Kutzner2011b");
1515 switch (ir->eSwapCoords)
1531 const gmx_bool bVerbose = mdrunOptions.verbose;
1533 // For compatibility with old .tpr files
1534 if (bConvertFromOldTpr(sc) )
1536 convertOldToNewGroupFormat(sc, mtop, bVerbose && MASTER(cr), cr);
1539 /* Copy some data and pointers to the group structures for convenience */
1540 /* Number of atoms in the group */
1542 for (int i = 0; i < s->ngrp; i++)
1544 s->group.emplace_back(atomSets->add(gmx::ArrayRef<const int>( sc->grp[i].ind, sc->grp[i].ind+sc->grp[i].nat)));
1545 s->group[i].molname = sc->grp[i].molname;
1548 /* Check for overlapping atoms */
1549 check_swap_groups(s, mtop->natoms, bVerbose && MASTER(cr));
1551 /* Allocate space for the collective arrays for all groups */
1552 /* For the collective position array */
1553 for (int i = 0; i < s->ngrp; i++)
1556 snew(g->xc, g->atomset.numAtomsGlobal());
1558 /* For the split groups (the channels) we need some extra memory to
1559 * be able to make the molecules whole even if they span more than
1560 * half of the box size. */
1561 if ( (i == eGrpSplit0) || (i == eGrpSplit1) )
1563 snew(g->xc_shifts, g->atomset.numAtomsGlobal());
1564 snew(g->xc_eshifts, g->atomset.numAtomsGlobal());
1565 snew(g->xc_old, g->atomset.numAtomsGlobal());
1571 if (oh->swapHistory == nullptr)
1573 oh->swapHistory = std::make_unique<swaphistory_t>(swaphistory_t {});
1575 swapstate = oh->swapHistory.get();
1577 init_swapstate(swapstate, sc, s, mtop, globalState->x.rvec_array(), globalState->box, ir);
1580 /* After init_swapstate we have a set of (old) whole positions for our
1581 * channels. Now transfer that to all nodes */
1584 for (int ig = eGrpSplit0; ig <= eGrpSplit1; ig++)
1586 g = &(s->group[ig]);
1587 gmx_bcast((g->atomset.numAtomsGlobal())*sizeof((g->xc_old)[0]), g->xc_old, (cr));
1591 /* Make sure that all molecules in the solvent and ion groups contain the
1592 * same number of atoms each */
1593 for (int ig = eGrpSolvent; ig < s->ngrp; ig++)
1597 g = &(s->group[ig]);
1598 g->apm = get_group_apm_check(ig, s, MASTER(cr) && bVerbose, mtop);
1600 /* Since all molecules of a group are equal, we only need enough space
1601 * to determine properties of a single molecule at at time */
1602 snew(g->m, g->apm); /* For the center of mass */
1603 charge = 0; /* To determine the charge imbalance */
1605 for (int j = 0; j < g->apm; j++)
1607 const t_atom &atom = mtopGetAtomParameters(mtop, g->atomset.globalIndex()[j], &molb);
1611 /* Total charge of one molecule of this group: */
1616 /* Need mass-weighted center of split group? */
1617 for (int j = eGrpSplit0; j <= eGrpSplit1; j++)
1620 if (sc->massw_split[j])
1622 /* Save the split group masses if mass-weighting is requested */
1623 snew(g->m, g->atomset.numAtomsGlobal());
1625 for (size_t i = 0; i < g->atomset.numAtomsGlobal(); i++)
1627 g->m[i] = mtopGetAtomMass(mtop, g->atomset.globalIndex()[i], &molb);
1632 /* Make a t_pbc struct on all nodes so that the molecules
1633 * chosen for an exchange can be made whole. */
1636 bool restartWithAppending = (startingBehavior == gmx::StartingBehavior::RestartWithAppending);
1641 fprintf(stderr, "%s Opening output file %s%s\n", SwS, fn, restartWithAppending ? " for appending" : "");
1644 s->fpout = gmx_fio_fopen(fn, restartWithAppending ? "a" : "w" );
1646 if (!restartWithAppending)
1648 xvgr_header(s->fpout, "Molecule counts", "Time (ps)", "counts", exvggtXNY, oenv);
1650 for (int ig = 0; ig < s->ngrp; ig++)
1652 g = &(s->group[ig]);
1653 fprintf(s->fpout, "# %s group '%s' contains %d atom%s",
1654 ig < eSwapFixedGrpNR ? eSwapFixedGrp_names[ig] : "Ion",
1655 g->molname, static_cast<int>(g->atomset.numAtomsGlobal()), (g->atomset.numAtomsGlobal() > 1) ? "s" : "");
1656 if (!(eGrpSplit0 == ig || eGrpSplit1 == ig) )
1658 fprintf(s->fpout, " with %d atom%s in each molecule of charge %g",
1659 g->apm, (g->apm > 1) ? "s" : "", g->q);
1661 fprintf(s->fpout, ".\n");
1664 fprintf(s->fpout, "#\n# Initial positions of split groups:\n");
1667 for (int j = eGrpSplit0; j <= eGrpSplit1; j++)
1670 for (size_t i = 0; i < g->atomset.numAtomsGlobal(); i++)
1672 copy_rvec(globalState->x[sc->grp[j].ind[i]], g->xc[i]);
1674 /* xc has the correct PBC representation for the two channels, so we do
1675 * not need to correct for that */
1676 get_center(g->xc, g->m, g->atomset.numAtomsGlobal(), g->center);
1677 if (!restartWithAppending)
1679 fprintf(s->fpout, "# %s group %s-center %5f nm\n", eSwapFixedGrp_names[j],
1680 DimStr[s->swapdim], g->center[s->swapdim]);
1684 if (!restartWithAppending)
1686 if ( (0 != sc->bulkOffset[eCompA]) || (0 != sc->bulkOffset[eCompB]) )
1688 fprintf(s->fpout, "#\n");
1689 fprintf(s->fpout, "# You provided an offset for the position of the bulk layer(s).\n");
1690 fprintf(s->fpout, "# That means the layers to/from which ions and water molecules are swapped\n");
1691 fprintf(s->fpout, "# are not midway (= at 0.0) between the compartment-defining layers (at +/- 1.0).\n");
1692 fprintf(s->fpout, "# bulk-offsetA = %g\n", sc->bulkOffset[eCompA]);
1693 fprintf(s->fpout, "# bulk-offsetB = %g\n", sc->bulkOffset[eCompB]);
1696 fprintf(s->fpout, "#\n");
1697 fprintf(s->fpout, "# Split0 cylinder radius %f nm, up %f nm, down %f nm\n",
1698 sc->cyl0r, sc->cyl0u, sc->cyl0l);
1699 fprintf(s->fpout, "# Split1 cylinder radius %f nm, up %f nm, down %f nm\n",
1700 sc->cyl1r, sc->cyl1u, sc->cyl1l);
1702 fprintf(s->fpout, "#\n");
1703 if (!mdrunOptions.rerun)
1705 fprintf(s->fpout, "# Coupling constant (number of swap attempt steps to average over): %d (translates to %f ps).\n",
1706 sc->nAverage, sc->nAverage*sc->nstswap*ir->delta_t);
1707 fprintf(s->fpout, "# Threshold is %f\n", sc->threshold);
1708 fprintf(s->fpout, "#\n");
1709 fprintf(s->fpout, "# Remarks about which atoms passed which channel use global atoms numbers starting at one.\n");
1718 /* Allocate memory to remember the past particle counts for time averaging */
1719 for (int ig = eSwapFixedGrpNR; ig < s->ngrp; ig++)
1721 g = &(s->group[ig]);
1722 for (int ic = 0; ic < eCompNR; ic++)
1724 snew(g->comp[ic].nMolPast, sc->nAverage);
1728 /* Get the initial particle concentrations and let the other nodes know */
1731 if (startingBehavior != gmx::StartingBehavior::NewSimulation)
1733 get_initial_ioncounts_from_cpt(ir, s, swapstate, cr, bVerbose);
1737 fprintf(stderr, "%s Determining initial numbers of ions per compartment.\n", SwS);
1738 get_initial_ioncounts(ir, s, globalState->x.rvec_array(), globalState->box, cr, mdrunOptions.rerun);
1741 /* Prepare (further) checkpoint writes ... */
1742 if (startingBehavior != gmx::StartingBehavior::NewSimulation)
1744 /* Consistency check */
1745 if (swapstate->nAverage != sc->nAverage)
1747 gmx_fatal(FARGS, "%s Ion count averaging steps mismatch! checkpoint: %d, tpr: %d",
1748 SwS, swapstate->nAverage, sc->nAverage);
1753 swapstate->nAverage = sc->nAverage;
1755 fprintf(stderr, "%s Setting pointers for checkpoint writing\n", SwS);
1756 for (int ic = 0; ic < eCompNR; ic++)
1758 for (int ig = eSwapFixedGrpNR; ig < s->ngrp; ig++)
1761 gs = &swapstate->ionType[ig - eSwapFixedGrpNR];
1763 gs->nMolReq_p[ic] = &(g->comp[ic].nMolReq);
1764 gs->nMolPast_p[ic] = &(g->comp[ic].nMolPast[0]);
1765 gs->inflow_net_p[ic] = &(g->comp[ic].inflow_net);
1769 /* Determine the total charge imbalance */
1770 s->deltaQ = getRequestedChargeImbalance(s);
1774 fprintf(stderr, "%s Requested charge imbalance is Q(A) - Q(B) = %g e.\n", SwS, s->deltaQ);
1776 if (!restartWithAppending)
1778 fprintf(s->fpout, "# Requested charge imbalance is Q(A)-Q(B) = %g e.\n", s->deltaQ);
1784 bc_initial_concentrations(cr, ir->swap, s);
1787 /* Update the time-averaged number of molecules for all groups and compartments */
1788 for (int ig = eSwapFixedGrpNR; ig < sc->ngrp; ig++)
1791 for (int ic = 0; ic < eCompNR; ic++)
1793 update_time_window(&g->comp[ic], sc->nAverage, -1);
1797 /* Initialize arrays that keep track of through which channel the ions go */
1798 detect_flux_per_channel_init(s, swapstate, startingBehavior != gmx::StartingBehavior::NewSimulation);
1800 /* We need to print the legend if we open this file for the first time. */
1801 if (MASTER(cr) && !restartWithAppending)
1803 print_ionlist_legend(ir, s, oenv);
1809 void finish_swapcoords(t_swap *s)
1817 // Close the swap output file
1818 gmx_fio_fclose(s->fpout);
1822 /*! \brief Do we need to swap a molecule in any of the ion groups with a water molecule at this step?
1824 * From the requested and average molecule counts we determine whether a swap is needed
1825 * at this time step.
1827 static gmx_bool need_swap(t_swapcoords *sc,
1833 for (ig = eSwapFixedGrpNR; ig < sc->ngrp; ig++)
1837 for (ic = 0; ic < eCompNR; ic++)
1839 if (g->comp[ic].nMolReq - g->comp[ic].nMolAv >= sc->threshold)
1849 /*! \brief Return the index of an atom or molecule suitable for swapping.
1851 * Returns the index of an atom that is far off the compartment boundaries,
1852 * that is near to the bulk layer to/from which the swaps take place.
1853 * Other atoms of the molecule (if any) will directly follow the returned index.
1855 * \param[in] comp Structure containing compartment-specific data.
1856 * \param[in] molname Name of the molecule.
1858 * \returns Index of the first atom of the molecule chosen for a position exchange.
1860 static int get_index_of_distant_atom(
1861 t_compartment *comp,
1862 const char molname[])
1865 real d = GMX_REAL_MAX;
1868 /* comp->nat contains the original number of atoms in this compartment
1869 * prior to doing any swaps. Some of these atoms may already have been
1870 * swapped out, but then they are marked with a distance of GMX_REAL_MAX
1872 for (int iMol = 0; iMol < comp->nMolBefore; iMol++)
1874 if (comp->dist[iMol] < d)
1877 d = comp->dist[ibest];
1883 gmx_fatal(FARGS, "Could not get index of %s atom. Compartment contains %d %s molecules before swaps.",
1884 molname, comp->nMolBefore, molname);
1887 /* Set the distance of this index to infinity such that it won't get selected again in
1890 comp->dist[ibest] = GMX_REAL_MAX;
1892 return comp->ind[ibest];
1896 /*! \brief Swaps centers of mass and makes molecule whole if broken */
1897 static void translate_positions(
1905 rvec reference, dx, correctPBCimage;
1908 /* Use the first atom as the reference for PBC */
1909 copy_rvec(x[0], reference);
1911 for (i = 0; i < apm; i++)
1913 /* PBC distance between position and reference */
1914 pbc_dx(pbc, x[i], reference, dx);
1916 /* Add PBC distance to reference */
1917 rvec_add(reference, dx, correctPBCimage);
1919 /* Subtract old_com from correct image and add new_com */
1920 rvec_dec(correctPBCimage, old_com);
1921 rvec_inc(correctPBCimage, new_com);
1923 copy_rvec(correctPBCimage, x[i]);
1928 /*! \brief Write back the modified local positions from the collective array to the official positions. */
1929 static void apply_modified_positions(
1933 auto collectiveIndex = g->atomset.collectiveIndex().begin();
1934 for (const auto localIndex : g->atomset.localIndex())
1936 /* Copy the possibly modified position */
1937 copy_rvec(g->xc[*collectiveIndex], x[localIndex]);
1943 gmx_bool do_swapcoords(
1949 gmx_wallcycle *wcycle,
1956 int j, ic, ig, nswaps;
1957 int thisC, otherC; /* Index into this compartment and the other one */
1958 gmx_bool bSwap = FALSE;
1959 t_swapgrp *g, *gsol;
1961 rvec com_solvent, com_particle; /* solvent and swap molecule's center of mass */
1964 wallcycle_start(wcycle, ewcSWAP);
1968 set_pbc(s->pbc, ir->ePBC, box);
1970 /* Assemble the positions of the split groups, i.e. the channels.
1971 * Here we also pass a shifts array to communicate_group_positions(), so that it can make
1972 * the molecules whole even in cases where they span more than half of the box in
1974 for (ig = eGrpSplit0; ig <= eGrpSplit1; ig++)
1976 g = &(s->group[ig]);
1977 communicate_group_positions(cr, g->xc, g->xc_shifts, g->xc_eshifts, TRUE,
1978 x, g->atomset.numAtomsGlobal(), g->atomset.numAtomsLocal(), g->atomset.localIndex().data(), g->atomset.collectiveIndex().data(), g->xc_old, box);
1980 get_center(g->xc, g->m, g->atomset.numAtomsGlobal(), g->center); /* center of split groups == channels */
1983 /* Assemble the positions of the ions (ig = 3, 4, ...). These molecules should
1984 * be small and we can always make them whole with a simple distance check.
1985 * Therefore we pass NULL as third argument. */
1986 for (ig = eSwapFixedGrpNR; ig < s->ngrp; ig++)
1988 g = &(s->group[ig]);
1989 communicate_group_positions(cr, g->xc, nullptr, nullptr, FALSE,
1990 x, g->atomset.numAtomsGlobal(), g->atomset.numAtomsLocal(), g->atomset.localIndex().data(), g->atomset.collectiveIndex().data(), nullptr, nullptr);
1992 /* Determine how many ions of this type each compartment contains */
1993 sortMoleculesIntoCompartments(g, cr, sc, s, box, step, s->fpout, bRerun, FALSE);
1996 /* Output how many ions are in the compartments */
1999 print_ionlist(s, t, "");
2002 /* If we are doing a rerun, we are finished here, since we cannot perform
2009 /* Do we have to perform a swap? */
2010 bSwap = need_swap(sc, s);
2013 /* Since we here know that we have to perform ion/water position exchanges,
2014 * we now assemble the solvent positions */
2015 g = &(s->group[eGrpSolvent]);
2016 communicate_group_positions(cr, g->xc, nullptr, nullptr, FALSE,
2017 x, g->atomset.numAtomsGlobal(), g->atomset.numAtomsLocal(), g->atomset.localIndex().data(), g->atomset.collectiveIndex().data(), nullptr, nullptr);
2019 /* Determine how many molecules of solvent each compartment contains */
2020 sortMoleculesIntoCompartments(g, cr, sc, s, box, step, s->fpout, bRerun, TRUE);
2022 /* Save number of solvent molecules per compartment prior to any swaps */
2023 g->comp[eCompA].nMolBefore = g->comp[eCompA].nMol;
2024 g->comp[eCompB].nMolBefore = g->comp[eCompB].nMol;
2026 for (ig = eSwapFixedGrpNR; ig < s->ngrp; ig++)
2028 g = &(s->group[ig]);
2030 for (ic = 0; ic < eCompNR; ic++)
2032 /* Determine in which compartment ions are missing and where they are too many */
2033 g->vacancy[ic] = g->comp[ic].nMolReq - g->comp[ic].nMolAv;
2035 /* Save number of ions per compartment prior to swaps */
2036 g->comp[ic].nMolBefore = g->comp[ic].nMol;
2040 /* Now actually perform the particle exchanges, one swap group after another */
2041 gsol = &s->group[eGrpSolvent];
2042 for (ig = eSwapFixedGrpNR; ig < s->ngrp; ig++)
2046 for (thisC = 0; thisC < eCompNR; thisC++)
2048 /* Index to the other compartment */
2049 otherC = (thisC+1) % eCompNR;
2051 while (g->vacancy[thisC] >= sc->threshold)
2053 /* Swap in an ion */
2055 /* Get the xc-index of the first atom of a solvent molecule of this compartment */
2056 isol = get_index_of_distant_atom(&gsol->comp[thisC], gsol->molname);
2058 /* Get the xc-index of a particle from the other compartment */
2059 iion = get_index_of_distant_atom(&g->comp[otherC], g->molname);
2061 get_molecule_center(&gsol->xc[isol], gsol->apm, gsol->m, com_solvent, s->pbc);
2062 get_molecule_center(&g->xc[iion], g->apm, g->m, com_particle, s->pbc);
2064 /* Subtract solvent molecule's center of mass and add swap particle's center of mass */
2065 translate_positions(&gsol->xc[isol], gsol->apm, com_solvent, com_particle, s->pbc);
2066 /* Similarly for the swap particle, subtract com_particle and add com_solvent */
2067 translate_positions(&g->xc[iion], g->apm, com_particle, com_solvent, s->pbc);
2069 /* Keep track of the changes */
2070 g->vacancy[thisC ]--;
2071 g->vacancy[otherC]++;
2072 g->comp [thisC ].nMol++;
2073 g->comp [otherC].nMol--;
2074 g->comp [thisC ].inflow_net++;
2075 g->comp [otherC].inflow_net--;
2076 /* Correct the past time window to still get the right averages from now on */
2077 g->comp [thisC ].nMolAv++;
2078 g->comp [otherC].nMolAv--;
2079 for (j = 0; j < sc->nAverage; j++)
2081 g->comp[thisC ].nMolPast[j]++;
2082 g->comp[otherC].nMolPast[j]--;
2084 /* Clear ion history */
2087 int iMol = iion / g->apm;
2088 g->channel_label[iMol] = eChHistPassedNone;
2089 g->comp_from[iMol] = eDomainNotset;
2091 /* That was the swap */
2096 if (nswaps && bVerbose)
2098 fprintf(stderr, "%s Performed %d swap%s in step %" PRId64 " for iontype %s.\n",
2099 SwS, nswaps, nswaps > 1 ? "s" : "", step, g->molname);
2103 if (s->fpout != nullptr)
2105 print_ionlist(s, t, " # after swap");
2108 /* For the solvent and user-defined swap groups, each rank writes back its
2109 * (possibly modified) local positions to the official position array. */
2110 for (ig = eGrpSolvent; ig < s->ngrp; ig++)
2113 apply_modified_positions(g, x);
2116 } /* end of if(bSwap) */
2118 wallcycle_stop(wcycle, ewcSWAP);