38c83ffa2a4766126dee574bcf2b49a8ce03b72f
[alexxy/gromacs.git] / src / gromacs / mdlib / tests / settletestdata.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2014,2015,2016,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.
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 /*! \internal \file
36  * \brief Defines the class that accumulates SETTLE test data.
37  *
38  * \author Mark Abraham  <mark.j.abraham@gmail.com>
39  * \author Artem Zhmurov <zhmurov@gmail.com>
40  *
41  * \ingroup module_mdlib
42  */
43 #include "gmxpre.h"
44
45 #include "settletestdata.h"
46
47 #include <algorithm>
48 #include <unordered_map>
49 #include <vector>
50
51 #include <gtest/gtest.h>
52
53 #include "gromacs/gpu_utils/gpu_utils.h"
54 #include "gromacs/math/paddedvector.h"
55 #include "gromacs/math/vec.h"
56 #include "gromacs/math/vectypes.h"
57 #include "gromacs/mdlib/settle.h"
58 #include "gromacs/mdtypes/mdatom.h"
59 #include "gromacs/pbcutil/pbc.h"
60 #include "gromacs/topology/idef.h"
61 #include "gromacs/topology/ifunc.h"
62 #include "gromacs/topology/topology.h"
63 #include "gromacs/utility/smalloc.h"
64 #include "gromacs/utility/stringutil.h"
65 #include "gromacs/utility/unique_cptr.h"
66
67 #include "gromacs/mdlib/tests/watersystem.h"
68 #include "testutils/testasserts.h"
69
70 namespace gmx
71 {
72 namespace test
73 {
74
75 SettleTestData::SettleTestData(int numSettles) :
76     x_(c_waterPositions.size()),
77     xPrime_(c_waterPositions.size()),
78     v_(c_waterPositions.size())
79 {
80     // Initialize coordinates and velocities from the constant set of coordinates
81     std::copy(c_waterPositions.begin(), c_waterPositions.end(), x_.begin());
82     std::copy(c_waterPositions.begin(), c_waterPositions.end(), xPrime_.begin());
83
84     // Perturb the atom positions, to appear like an
85     // "update," and where there is definitely constraining
86     // work to do.
87     for (int i = 0; i < xPrime_.size()*DIM; ++i)
88     {
89         if (i % 4 == 0)
90         {
91             xPrime_[i / DIM][i % DIM] += 0.01;
92         }
93         else if (i % 4 == 1)
94         {
95             xPrime_[i / DIM][i % DIM] -= 0.01;
96         }
97         else if (i % 4 == 2)
98         {
99             xPrime_[i / DIM][i % DIM] += 0.02;
100         }
101         else if (i % 4 == 3)
102         {
103             xPrime_[i / DIM][i % DIM] -= 0.02;
104         }
105         v_[i / DIM][i % DIM] = 0.0;
106     }
107
108     // Set up the topology.
109     const int settleType = 0;
110     mtop_.moltype.resize(1);
111     mtop_.molblock.resize(1);
112     mtop_.molblock[0].type = 0;
113     std::vector<int> &iatoms = mtop_.moltype[0].ilist[F_SETTLE].iatoms;
114     for (int i = 0; i < numSettles; ++i)
115     {
116         iatoms.push_back(settleType);
117         iatoms.push_back(i*atomsPerSettle_ + 0);
118         iatoms.push_back(i*atomsPerSettle_ + 1);
119         iatoms.push_back(i*atomsPerSettle_ + 2);
120     }
121
122     // Set up the SETTLE parameters.
123     t_iparams         iparams;
124     iparams.settle.doh = dOH_;
125     iparams.settle.dhh = dHH_;
126     mtop_.ffparams.iparams.push_back(iparams);
127
128     // Set up the masses.
129     mtop_.moltype[0].atoms.atom = static_cast<t_atom*>(calloc(numSettles*atomsPerSettle_, sizeof(t_atom)));
130     mdatoms_.homenr             = numSettles * atomsPerSettle_;
131     mdatoms_.massT              = static_cast<real*>(calloc(mdatoms_.homenr, sizeof(real)));
132     mdatoms_.invmass            = static_cast<real*>(calloc(mdatoms_.homenr, sizeof(real)));
133     for (int i = 0; i < numSettles; ++i)
134     {
135         mdatoms_.massT[i*atomsPerSettle_ + 0] = oxygenMass_;
136         mdatoms_.massT[i*atomsPerSettle_ + 1] = hydrogenMass_;
137         mdatoms_.massT[i*atomsPerSettle_ + 2] = hydrogenMass_;
138
139         mdatoms_.invmass[i*atomsPerSettle_ + 0] = 1.0/oxygenMass_;
140         mdatoms_.invmass[i*atomsPerSettle_ + 1] = 1.0/hydrogenMass_;
141         mdatoms_.invmass[i*atomsPerSettle_ + 2] = 1.0/hydrogenMass_;
142
143         mtop_.moltype[0].atoms.atom[i*atomsPerSettle_ + 0].m = oxygenMass_;
144         mtop_.moltype[0].atoms.atom[i*atomsPerSettle_ + 1].m = hydrogenMass_;
145         mtop_.moltype[0].atoms.atom[i*atomsPerSettle_ + 2].m = hydrogenMass_;
146     }
147
148     // Reshape some data so it can be directly used by the SETTLE constraints
149     ilist_             = { mtop_.moltype[0].ilist[F_SETTLE].size(), 0, mtop_.moltype[0].ilist[F_SETTLE].iatoms.data(), 0 };
150     idef_.il[F_SETTLE] = ilist_;
151 }
152
153 SettleTestData::~SettleTestData()
154 {
155     free(mdatoms_.massT);
156     free(mdatoms_.invmass);
157 }
158
159 }  // namespace test
160 }  // namespace gmx