Update instructions in containers.rst
[gromacs.git] / src / gromacs / mimic / communicator.cpp
blob12dcf874d3f976913149edf461766bd8eb2ca813
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2018,2019,2020, 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.
35 #include "gmxpre.h"
37 #include "communicator.h"
39 #include "config.h"
41 #include <unordered_set>
43 #include "gromacs/math/units.h"
44 #include "gromacs/utility/fatalerror.h"
46 #if GMX_MIMIC
47 # include <DataTypes.h>
48 # include <MessageApi.h>
49 #endif
51 // When not built in a configuration with QMMM support, much of this
52 // code is unreachable by design. Tell clang not to warn about it.
53 #pragma GCC diagnostic push
54 #pragma GCC diagnostic ignored "-Wmissing-noreturn"
56 #if !GMX_MIMIC
57 //! \brief Definitions to stub the ones defined in DataTypes.h
58 constexpr int TYPE_INT = 0, TYPE_DOUBLE = 0;
60 /*! \brief Stub communication library function to call in case if
61 * GROMACS is compiled without MiMiC. Calling causes GROMACS to exit!
63 static void MCL_init_client(const char*) // NOLINT(readability-named-parameter)
65 GMX_RELEASE_ASSERT(
66 GMX_MIMIC,
67 "GROMACS is compiled without MiMiC support! Please, recompile with -DGMX_MIMIC=ON");
70 /*! \brief Stub communication library function to call in case if
71 * GROMACS is compiled without MiMiC. Calling causes GROMACS to exit!
73 static void MCL_send(void*, int, int, int) // NOLINT(readability-named-parameter)
75 GMX_RELEASE_ASSERT(
76 GMX_MIMIC,
77 "GROMACS is compiled without MiMiC support! Please, recompile with -DGMX_MIMIC=ON");
80 /*! \brief Stub communication library function to call in case if
81 * GROMACS is compiled without MiMiC. Calling causes GROMACS to exit!
83 static void MCL_receive(void*, int, int, int) // NOLINT(readability-named-parameter)
85 GMX_RELEASE_ASSERT(
86 GMX_MIMIC,
87 "GROMACS is compiled without MiMiC support! Please, recompile with -DGMX_MIMIC=ON");
90 /*! \brief Stub communication library function to call in case if
91 * GROMACS is compiled without MiMiC. Calling causes GROMACS to exit!
93 static void MCL_destroy()
95 GMX_RELEASE_ASSERT(
96 GMX_MIMIC,
97 "GROMACS is compiled without MiMiC support! Please, recompile with -DGMX_MIMIC=ON");
99 #endif
101 void gmx::MimicCommunicator::init()
103 char path[GMX_PATH_MAX];
104 gmx_getcwd(path, GMX_PATH_MAX);
105 MCL_init_client(path);
108 void gmx::MimicCommunicator::sendInitData(gmx_mtop_t* mtop, PaddedHostVector<gmx::RVec> coords)
110 MCL_send(&mtop->natoms, 1, TYPE_INT, 0);
111 MCL_send(&mtop->atomtypes.nr, 1, TYPE_INT, 0);
113 std::vector<int> atomTypes;
114 std::vector<int> nAtomsMol;
115 std::vector<int> idOrder;
116 std::vector<double> charges;
117 std::vector<double> masses(mtop->atomtypes.nr, -1);
118 std::vector<int> elements(mtop->atomtypes.nr, -1);
119 std::vector<int> bonds;
120 std::vector<double> bondLengths;
121 std::unordered_set<int> existingTypes;
123 atomTypes.reserve(static_cast<size_t>(mtop->natoms));
124 charges.reserve(static_cast<size_t>(mtop->natoms));
126 int offset = 0;
127 for (const gmx_molblock_t& molblock : mtop->molblock)
129 gmx_moltype_t* type = &mtop->moltype[molblock.type];
130 for (int mol = 0; mol < molblock.nmol; ++mol)
132 int nconstr = type->ilist[F_CONSTR].size() / 3;
133 int nconstrc = type->ilist[F_CONSTRNC].size() / 3;
134 int nsettle = type->ilist[F_SETTLE].size() / 4;
136 for (int ncon = 0; ncon < nconstr + nconstrc; ++ncon)
138 int contype = type->ilist[F_CONSTR].iatoms[0];
139 int at1 = type->ilist[F_CONSTR].iatoms[1];
140 int at2 = type->ilist[F_CONSTR].iatoms[2];
141 bonds.push_back(offset + at1 + 1);
142 bonds.push_back(offset + at2 + 1);
143 bondLengths.push_back(static_cast<double>(mtop->ffparams.iparams[contype].constr.dA)
144 / BOHR2NM);
147 for (int ncon = 0; ncon < nsettle; ++ncon)
149 t_iatom ox;
150 t_iatom h1;
151 t_iatom h2;
153 int contype = type->ilist[F_SETTLE].iatoms[0];
155 ox = type->ilist[F_SETTLE].iatoms[1];
156 h1 = type->ilist[F_SETTLE].iatoms[2];
157 h2 = type->ilist[F_SETTLE].iatoms[3];
159 bonds.push_back(offset + ox + 1);
160 bonds.push_back(offset + h1 + 1);
162 bonds.push_back(offset + ox + 1);
163 bonds.push_back(offset + h2 + 1);
165 bonds.push_back(offset + h1 + 1);
166 bonds.push_back(offset + h2 + 1);
167 bondLengths.push_back(static_cast<double>(mtop->ffparams.iparams[contype].constr.dA)
168 / BOHR2NM);
169 bondLengths.push_back(static_cast<double>(mtop->ffparams.iparams[contype].constr.dA)
170 / BOHR2NM);
171 bondLengths.push_back(static_cast<double>(mtop->ffparams.iparams[contype].constr.dB)
172 / BOHR2NM);
175 nAtomsMol.push_back(type->atoms.nr);
176 for (int at = 0; at < type->atoms.nr; ++at)
178 int atomtype = type->atoms.atom[at].type;
179 auto charge = static_cast<double>(type->atoms.atom[at].q);
180 idOrder.push_back(offset + 1);
181 offset++;
182 atomTypes.push_back(atomtype + 1);
183 charges.push_back(charge);
184 if (existingTypes.insert(atomtype).second)
186 masses[atomtype] = type->atoms.atom[at].m;
187 elements[atomtype] = type->atoms.atom[at].atomnumber;
192 // sending atom types
193 MCL_send(&*atomTypes.begin(), mtop->natoms, TYPE_INT, 0);
195 int max_multipole_order = 0;
196 // sending multipole orders
197 MCL_send(&max_multipole_order, 1, TYPE_INT, 0);
199 int nMolecules = nAtomsMol.size();
200 // sending molecule number
201 MCL_send(&nMolecules, 1, TYPE_INT, 0);
203 // sending number of atoms per molecules
204 MCL_send(&*nAtomsMol.begin(), nAtomsMol.size(), TYPE_INT, 0);
206 int nBonds = bonds.size() / 2;
207 // sending number of bond constraints
208 MCL_send(&nBonds, 1, TYPE_INT, 0);
210 // sending number of angle constraints
211 MCL_send(&max_multipole_order, 1, TYPE_INT, 0);
213 if (nBonds > 0)
215 // sending bonded atoms indices
216 MCL_send(&*bonds.begin(), bonds.size(), TYPE_INT, 0);
218 // sending bond lengths
219 MCL_send(&*bondLengths.begin(), bondLengths.size(), TYPE_DOUBLE, 0);
222 // sending array of atomic charges
223 MCL_send(&*charges.begin(), mtop->natoms, TYPE_DOUBLE, 0);
225 // sending array of atomic masses
226 MCL_send(&*masses.begin(), mtop->atomtypes.nr, TYPE_DOUBLE, 0);
228 // sending ids of atoms per molecule
229 MCL_send(&*idOrder.begin(), idOrder.size(), TYPE_INT, 0);
231 // sending list of elements
232 MCL_send(&*elements.begin(), mtop->atomtypes.nr, TYPE_INT, 0);
234 std::vector<double> convertedCoords;
235 for (auto& coord : coords)
237 convertedCoords.push_back(static_cast<double>(coord[0]) / BOHR2NM);
238 convertedCoords.push_back(static_cast<double>(coord[1]) / BOHR2NM);
239 convertedCoords.push_back(static_cast<double>(coord[2]) / BOHR2NM);
242 // sending array of coordinates
243 MCL_send(&*convertedCoords.begin(), 3 * mtop->natoms, TYPE_DOUBLE, 0);
246 int64_t gmx::MimicCommunicator::getStepNumber()
248 int steps;
249 MCL_receive(&steps, 1, TYPE_INT, 0);
250 return steps;
253 void gmx::MimicCommunicator::getCoords(PaddedHostVector<RVec>* x, const int natoms)
255 std::vector<double> coords(natoms * 3);
256 MCL_receive(&*coords.begin(), 3 * natoms, TYPE_DOUBLE, 0);
257 for (int j = 0; j < natoms; ++j)
259 (*x)[j][0] = static_cast<real>(coords[j * 3] * BOHR2NM);
260 (*x)[j][1] = static_cast<real>(coords[j * 3 + 1] * BOHR2NM);
261 (*x)[j][2] = static_cast<real>(coords[j * 3 + 2] * BOHR2NM);
265 void gmx::MimicCommunicator::sendEnergies(real energy)
267 double convertedEnergy = energy / (HARTREE2KJ * AVOGADRO);
268 MCL_send(&convertedEnergy, 1, TYPE_DOUBLE, 0);
271 void gmx::MimicCommunicator::sendForces(gmx::ArrayRef<gmx::RVec> forces, int natoms)
273 std::vector<double> convertedForce;
274 for (int j = 0; j < natoms; ++j)
276 convertedForce.push_back(static_cast<real>(forces[j][0]) / HARTREE_BOHR2MD);
277 convertedForce.push_back(static_cast<real>(forces[j][1]) / HARTREE_BOHR2MD);
278 convertedForce.push_back(static_cast<real>(forces[j][2]) / HARTREE_BOHR2MD);
280 MCL_send(&*convertedForce.begin(), convertedForce.size(), TYPE_DOUBLE, 0);
283 void gmx::MimicCommunicator::finalize()
285 MCL_destroy();
288 #pragma GCC diagnostic pop