02abe3e20f000eaf00a6574fbad9377a712324a4
[alexxy/gromacs.git] / api / nblib / tests / testsystems.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 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.
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
37  * This implements nblib test systems
38  *
39  * \author Victor Holanda <victor.holanda@cscs.ch>
40  * \author Joe Jordan <ejjordan@kth.se>
41  * \author Prashanth Kanduri <kanduri@cscs.ch>
42  * \author Sebastian Keller <keller@cscs.ch>
43  * \author Artem Zhmurov <zhmurov@gmail.com>
44  */
45 #include "nblib/tests/testsystems.h"
46 #include "nblib/exception.h"
47
48 namespace nblib
49 {
50
51 //! User class to hold ParticleTypes
52 //! Note: this is not part of NBLIB, users should write their own
53 class ParticleLibrary
54 {
55 public:
56     ParticleLibrary()
57     {
58         ParticleType Ow(ParticleTypeName("Ow"), Mass(15.99940));
59         ParticleType H(ParticleTypeName("H"), Mass(1.008));
60         ParticleType OMet(ParticleTypeName("OMet"), Mass(15.999));
61         ParticleType CMet(ParticleTypeName("CMet"), Mass(15.035));
62         ParticleType Ar(ParticleTypeName("Ar"), Mass(39.94800));
63
64         particles_.insert(std::make_pair(Ow.name(), Ow));
65         particles_.insert(std::make_pair(H.name(), H));
66         particles_.insert(std::make_pair(OMet.name(), OMet));
67         particles_.insert(std::make_pair(CMet.name(), CMet));
68         particles_.insert(std::make_pair(Ar.name(), Ar));
69
70         c6_[Ow.name()]   = 0.0026173456;
71         c6_[H.name()]    = 0;
72         c6_[OMet.name()] = 0.0022619536;
73         c6_[CMet.name()] = 0.0088755241;
74         c6_[Ar.name()]   = 0.0062647225;
75
76         c12_[Ow.name()]   = 2.634129e-06;
77         c12_[H.name()]    = 0;
78         c12_[OMet.name()] = 1.505529e-06;
79         c12_[CMet.name()] = 2.0852922e-05;
80         c12_[Ar.name()]   = 9.847044e-06;
81     }
82
83     //! Get particle type using the string identifier
84     [[nodiscard]] ParticleType type(const std::string& particleTypeName) const
85     {
86         return particles_.at(ParticleTypeName(particleTypeName));
87     }
88
89     //! Get C6 parameter of a given particle
90     [[nodiscard]] C6 c6(const ParticleName& particleName) const
91     {
92         return c6_.at(ParticleTypeName(particleName.value()));
93     }
94
95     //! Get C12 parameter of a given particle
96     [[nodiscard]] C12 c12(const ParticleName& particleName) const
97     {
98         return c12_.at(ParticleTypeName(particleName.value()));
99     }
100
101 private:
102     std::map<ParticleTypeName, ParticleType> particles_;
103     std::map<ParticleTypeName, C6>           c6_;
104     std::map<ParticleTypeName, C12>          c12_;
105 };
106
107 std::unordered_map<std::string, Charge> Charges{ { "Ow", Charge(-0.82) },
108                                                  { "Hw", Charge(+0.41) },
109                                                  { "OMet", Charge(-0.574) },
110                                                  { "CMet", Charge(+0.176) },
111                                                  { "HMet", Charge(+0.398) } };
112
113 WaterMoleculeBuilder::WaterMoleculeBuilder() : water_(MoleculeName("SOL"))
114 {
115     ParticleLibrary plib;
116
117     //! Add the particles
118     water_.addParticle(ParticleName("Oxygen"), Charges.at("Ow"), plib.type("Ow"));
119     water_.addParticle(ParticleName("H1"), Charges.at("Hw"), plib.type("H"));
120     water_.addParticle(ParticleName("H2"), Charges.at("Hw"), plib.type("H"));
121 }
122
123 Molecule WaterMoleculeBuilder::waterMolecule()
124 {
125     addExclusionsFromNames();
126     return water_;
127 }
128
129 Molecule WaterMoleculeBuilder::waterMoleculeWithoutExclusions()
130 {
131     return water_;
132 }
133
134 void WaterMoleculeBuilder::addExclusionsFromNames()
135 {
136     water_.addExclusion("H1", "Oxygen");
137     water_.addExclusion("H2", "Oxygen");
138     water_.addExclusion("H1", "H2");
139 }
140
141 MethanolMoleculeBuilder::MethanolMoleculeBuilder() : methanol_(MoleculeName("MeOH"))
142 {
143     ParticleLibrary library;
144
145     //! Add the particles
146     methanol_.addParticle(ParticleName("Me1"), Charges.at("CMet"), library.type("CMet"));
147     methanol_.addParticle(ParticleName("O2"), Charges.at("OMet"), library.type("OMet"));
148     methanol_.addParticle(ParticleName("H3"), Charges.at("HMet"), library.type("H"));
149
150     // Add the exclusions
151     methanol_.addExclusion("Me1", "O2");
152     methanol_.addExclusion("Me1", "H3");
153     methanol_.addExclusion("H3", "O2");
154 }
155
156 Molecule MethanolMoleculeBuilder::methanolMolecule()
157 {
158     return methanol_;
159 }
160
161
162 Topology WaterTopologyBuilder::buildTopology(int numMolecules)
163 {
164     ParticleLibrary library;
165
166     ParticleTypesInteractions interactions;
167     std::vector<std::string>  typeNames = { "Ow", "H" };
168     for (const auto& name : typeNames)
169     {
170         interactions.add(ParticleTypeName(name), library.c6(ParticleName(name)),
171                          library.c12(ParticleName(name)));
172     }
173
174     // Add some molecules to the topology
175     TopologyBuilder topologyBuilder;
176     topologyBuilder.addMolecule(water(), numMolecules);
177
178     // Add non-bonded interaction information
179     topologyBuilder.addParticleTypesInteractions(interactions);
180
181     Topology topology = topologyBuilder.buildTopology();
182     return topology;
183 }
184
185 Molecule WaterTopologyBuilder::water()
186 {
187     return waterMolecule_.waterMolecule();
188 }
189
190 Topology SpcMethanolTopologyBuilder::buildTopology(int numWater, int numMethanol)
191 {
192     ParticleLibrary library;
193
194     ParticleTypesInteractions interactions;
195     std::vector<std::string>  typeNames = { "Ow", "H", "OMet", "CMet" };
196     for (const auto& name : typeNames)
197     {
198         interactions.add(ParticleTypeName(name), library.c6(ParticleName(name)),
199                          library.c12(ParticleName(name)));
200     }
201
202     // Add some molecules to the topology
203     TopologyBuilder topologyBuilder;
204     topologyBuilder.addMolecule(methanol(), numMethanol);
205     topologyBuilder.addMolecule(water(), numWater);
206
207     // Add non-bonded interaction information
208     topologyBuilder.addParticleTypesInteractions(interactions);
209
210     Topology topology = topologyBuilder.buildTopology();
211     return topology;
212 }
213
214 Molecule SpcMethanolTopologyBuilder::methanol()
215 {
216     return methanolMolecule_.methanolMolecule();
217 }
218
219 Molecule SpcMethanolTopologyBuilder::water()
220 {
221     return waterMolecule_.waterMolecule();
222 }
223
224 ArgonTopologyBuilder::ArgonTopologyBuilder(const int& numParticles)
225 {
226     ParticleLibrary library;
227
228     ParticleTypesInteractions nbinteractions;
229     nbinteractions.add(ParticleTypeName("Ar"), library.c6(ParticleName("Ar")),
230                        library.c12(ParticleName("Ar")));
231
232     Molecule argonMolecule(MoleculeName("AR"));
233     argonMolecule.addParticle(ParticleName("AR"), library.type("Ar"));
234
235     topologyBuilder_.addMolecule(argonMolecule, numParticles);
236     topologyBuilder_.addParticleTypesInteractions((nbinteractions));
237 }
238
239 Topology ArgonTopologyBuilder::argonTopology()
240 {
241     return topologyBuilder_.buildTopology();
242 }
243
244
245 } // namespace nblib