Use constexpr for physical constants and move them into gmx namespace
[alexxy/gromacs.git] / src / gromacs / mimic / communicator.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2018,2019,2020,2021, 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.
8  *
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.
13  *
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.
18  *
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.
23  *
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.
31  *
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.
34  */
35 #include "gmxpre.h"
36
37 #include "communicator.h"
38
39 #include "config.h"
40
41 #include <unordered_set>
42
43 #include "gromacs/math/units.h"
44 #include "gromacs/utility/fatalerror.h"
45
46 #if GMX_MIMIC
47 #    include <DataTypes.h>
48 #    include <MessageApi.h>
49 #endif
50
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"
55
56 #if !GMX_MIMIC
57 //! \brief Definitions to stub the ones defined in DataTypes.h
58 constexpr int TYPE_INT = 0, TYPE_DOUBLE = 0;
59
60 /*! \brief Stub communication library function to call in case if
61  * GROMACS is compiled without MiMiC. Calling causes GROMACS to exit!
62  */
63 static void MCL_init_client(const char*) // NOLINT(readability-named-parameter)
64 {
65     GMX_RELEASE_ASSERT(
66             GMX_MIMIC,
67             "GROMACS is compiled without MiMiC support! Please, recompile with -DGMX_MIMIC=ON");
68 }
69
70 /*! \brief Stub communication library function to call in case if
71  * GROMACS is compiled without MiMiC. Calling causes GROMACS to exit!
72  */
73 static void MCL_send(void*, int, int, int) // NOLINT(readability-named-parameter)
74 {
75     GMX_RELEASE_ASSERT(
76             GMX_MIMIC,
77             "GROMACS is compiled without MiMiC support! Please, recompile with -DGMX_MIMIC=ON");
78 }
79
80 /*! \brief Stub communication library function to call in case if
81  * GROMACS is compiled without MiMiC. Calling causes GROMACS to exit!
82  */
83 static void MCL_receive(void*, int, int, int) // NOLINT(readability-named-parameter)
84 {
85     GMX_RELEASE_ASSERT(
86             GMX_MIMIC,
87             "GROMACS is compiled without MiMiC support! Please, recompile with -DGMX_MIMIC=ON");
88 }
89
90 /*! \brief Stub communication library function to call in case if
91  * GROMACS is compiled without MiMiC. Calling causes GROMACS to exit!
92  */
93 static void MCL_destroy()
94 {
95     GMX_RELEASE_ASSERT(
96             GMX_MIMIC,
97             "GROMACS is compiled without MiMiC support! Please, recompile with -DGMX_MIMIC=ON");
98 }
99 #endif
100
101 void gmx::MimicCommunicator::init()
102 {
103     char path[GMX_PATH_MAX];
104     gmx_getcwd(path, GMX_PATH_MAX);
105     MCL_init_client(path);
106 }
107
108 void gmx::MimicCommunicator::sendInitData(gmx_mtop_t* mtop, PaddedHostVector<gmx::RVec> coords)
109 {
110     MCL_send(&mtop->natoms, 1, TYPE_INT, 0);
111     MCL_send(&mtop->atomtypes.nr, 1, TYPE_INT, 0);
112
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;
122
123     atomTypes.reserve(static_cast<size_t>(mtop->natoms));
124     charges.reserve(static_cast<size_t>(mtop->natoms));
125
126     int offset = 0;
127     for (const gmx_molblock_t& molblock : mtop->molblock)
128     {
129         gmx_moltype_t* type = &mtop->moltype[molblock.type];
130         for (int mol = 0; mol < molblock.nmol; ++mol)
131         {
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;
135
136             for (int ncon = 0; ncon < nconstr + nconstrc; ++ncon)
137             {
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                                       / gmx::c_bohr2Nm);
145             }
146
147             for (int ncon = 0; ncon < nsettle; ++ncon)
148             {
149                 t_iatom ox;
150                 t_iatom h1;
151                 t_iatom h2;
152
153                 int contype = type->ilist[F_SETTLE].iatoms[0];
154
155                 ox = type->ilist[F_SETTLE].iatoms[1];
156                 h1 = type->ilist[F_SETTLE].iatoms[2];
157                 h2 = type->ilist[F_SETTLE].iatoms[3];
158
159                 bonds.push_back(offset + ox + 1);
160                 bonds.push_back(offset + h1 + 1);
161
162                 bonds.push_back(offset + ox + 1);
163                 bonds.push_back(offset + h2 + 1);
164
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                                       / gmx::c_bohr2Nm);
169                 bondLengths.push_back(static_cast<double>(mtop->ffparams.iparams[contype].constr.dA)
170                                       / gmx::c_bohr2Nm);
171                 bondLengths.push_back(static_cast<double>(mtop->ffparams.iparams[contype].constr.dB)
172                                       / gmx::c_bohr2Nm);
173             }
174
175             nAtomsMol.push_back(type->atoms.nr);
176             for (int at = 0; at < type->atoms.nr; ++at)
177             {
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)
185                 {
186                     masses[atomtype]   = type->atoms.atom[at].m;
187                     elements[atomtype] = type->atoms.atom[at].atomnumber;
188                 }
189             }
190         }
191     }
192     // sending atom types
193     MCL_send(&*atomTypes.begin(), mtop->natoms, TYPE_INT, 0);
194
195     int max_multipole_order = 0;
196     // sending multipole orders
197     MCL_send(&max_multipole_order, 1, TYPE_INT, 0);
198
199     int nMolecules = nAtomsMol.size();
200     // sending molecule number
201     MCL_send(&nMolecules, 1, TYPE_INT, 0);
202
203     // sending number of atoms per molecules
204     MCL_send(&*nAtomsMol.begin(), nAtomsMol.size(), TYPE_INT, 0);
205
206     int nBonds = bonds.size() / 2;
207     // sending number of bond constraints
208     MCL_send(&nBonds, 1, TYPE_INT, 0);
209
210     // sending number of angle constraints
211     MCL_send(&max_multipole_order, 1, TYPE_INT, 0);
212
213     if (nBonds > 0)
214     {
215         // sending bonded atoms indices
216         MCL_send(&*bonds.begin(), bonds.size(), TYPE_INT, 0);
217
218         // sending bond lengths
219         MCL_send(&*bondLengths.begin(), bondLengths.size(), TYPE_DOUBLE, 0);
220     }
221
222     // sending array of atomic charges
223     MCL_send(&*charges.begin(), mtop->natoms, TYPE_DOUBLE, 0);
224
225     // sending array of atomic masses
226     MCL_send(&*masses.begin(), mtop->atomtypes.nr, TYPE_DOUBLE, 0);
227
228     // sending ids of atoms per molecule
229     MCL_send(&*idOrder.begin(), idOrder.size(), TYPE_INT, 0);
230
231     // sending list of elements
232     MCL_send(&*elements.begin(), mtop->atomtypes.nr, TYPE_INT, 0);
233
234     std::vector<double> convertedCoords;
235     for (auto& coord : coords)
236     {
237         convertedCoords.push_back(static_cast<double>(coord[0]) / gmx::c_bohr2Nm);
238         convertedCoords.push_back(static_cast<double>(coord[1]) / gmx::c_bohr2Nm);
239         convertedCoords.push_back(static_cast<double>(coord[2]) / gmx::c_bohr2Nm);
240     }
241
242     // sending array of coordinates
243     MCL_send(&*convertedCoords.begin(), 3 * mtop->natoms, TYPE_DOUBLE, 0);
244 }
245
246 int64_t gmx::MimicCommunicator::getStepNumber()
247 {
248     int steps;
249     MCL_receive(&steps, 1, TYPE_INT, 0);
250     return steps;
251 }
252
253 void gmx::MimicCommunicator::getCoords(PaddedHostVector<RVec>* x, const int natoms)
254 {
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)
258     {
259         (*x)[j][0] = static_cast<real>(coords[j * 3] * gmx::c_bohr2Nm);
260         (*x)[j][1] = static_cast<real>(coords[j * 3 + 1] * gmx::c_bohr2Nm);
261         (*x)[j][2] = static_cast<real>(coords[j * 3 + 2] * gmx::c_bohr2Nm);
262     }
263 }
264
265 void gmx::MimicCommunicator::sendEnergies(real energy)
266 {
267     double convertedEnergy = energy / (gmx::c_hartree2Kj * gmx::c_avogadro);
268     MCL_send(&convertedEnergy, 1, TYPE_DOUBLE, 0);
269 }
270
271 void gmx::MimicCommunicator::sendForces(gmx::ArrayRef<gmx::RVec> forces, int natoms)
272 {
273     std::vector<double> convertedForce;
274     for (int j = 0; j < natoms; ++j)
275     {
276         convertedForce.push_back(static_cast<real>(forces[j][0]) / gmx::c_hartreeBohr2Md);
277         convertedForce.push_back(static_cast<real>(forces[j][1]) / gmx::c_hartreeBohr2Md);
278         convertedForce.push_back(static_cast<real>(forces[j][2]) / gmx::c_hartreeBohr2Md);
279     }
280     MCL_send(&*convertedForce.begin(), convertedForce.size(), TYPE_DOUBLE, 0);
281 }
282
283 void gmx::MimicCommunicator::finalize()
284 {
285     MCL_destroy();
286 }
287
288 #pragma GCC diagnostic pop