Merge branch release-2021
[alexxy/gromacs.git] / api / nblib / samples / argon-forces-integration.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 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 /*! \internal \file
36  * \brief
37  * This tests that sample code can run
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  */
44
45 #include <cstdio>
46
47 #include "gromacs/utility/arrayref.h"
48
49 // The entire nblib public API can be included with a single header or individual components
50 // can be included via their respective headers.
51 #include "nblib/nblib.h"
52
53 // Main function to write the MD program.
54 int main(); // Keep the compiler happy
55
56 int main()
57 {
58     // Create an argon particle with a name and a mass.
59     nblib::ParticleType argonAtom(nblib::ParticleTypeName("Ar"), nblib::Mass(39.94800));
60     // Create an argon molecule.
61     nblib::Molecule argonMolecule(nblib::MoleculeName("AR"));
62     // Add the argon particle to a molecule. The names are for bookkeeping and need not match.
63     argonMolecule.addParticle(nblib::ParticleName("Argon"), argonAtom);
64     // Define Lennard-Jones params for argon (parameters from gromos43A1).
65     nblib::C6  ArC6{ 0.0062647225 };  // C6 parameter
66     nblib::C12 ArC12{ 9.847044e-06 }; // C12 parameter
67     // Holder for non-bonded interactions.
68     nblib::ParticleTypesInteractions interactions;
69     // Add non-bonded interactions for argon.
70     interactions.add(argonAtom.name(), ArC6, ArC12);
71     // The TopologyBuilder builds the Topology!
72     nblib::TopologyBuilder topologyBuilder;
73     // Number of Argon particles (molecules) in the system.
74     int numParticles = 12;
75     // Add the requested number of argon molecules to a topology.
76     topologyBuilder.addMolecule(argonMolecule, numParticles);
77     // Add the argon interactions to the topology.
78     topologyBuilder.addParticleTypesInteractions(interactions);
79     // Build the topology.
80     nblib::Topology topology = topologyBuilder.buildTopology();
81     // The system needs a bounding box. Only cubic and rectangular boxes are supported.
82     nblib::Box box(6.05449);
83     // User defined coordinates.
84     std::vector<nblib::Vec3> coordinates = {
85         { 0.794, 1.439, 0.610 }, { 1.397, 0.673, 1.916 }, { 0.659, 1.080, 0.573 },
86         { 1.105, 0.090, 3.431 }, { 1.741, 1.291, 3.432 }, { 1.936, 1.441, 5.873 },
87         { 0.960, 2.246, 1.659 }, { 0.382, 3.023, 2.793 }, { 0.053, 4.857, 4.242 },
88         { 2.655, 5.057, 2.211 }, { 4.114, 0.737, 0.614 }, { 5.977, 5.104, 5.217 },
89     };
90     // User defined velocities.
91     std::vector<nblib::Vec3> velocities = {
92         { 0.0055, -0.1400, 0.2127 },   { 0.0930, -0.0160, -0.0086 }, { 0.1678, 0.2476, -0.0660 },
93         { 0.1591, -0.0934, -0.0835 },  { -0.0317, 0.0573, 0.1453 },  { 0.0597, 0.0013, -0.0462 },
94         { 0.0484, -0.0357, 0.0168 },   { 0.0530, 0.0295, -0.2694 },  { -0.0550, -0.0896, 0.0494 },
95         { -0.0799, -0.2534, -0.0079 }, { 0.0436, -0.1557, 0.1849 },  { -0.0214, 0.0446, 0.0758 },
96     };
97     // Force buffer initialization for each particle.
98     std::vector<nblib::Vec3> forces = {
99         { 0.0000, 0.0000, 0.0000 }, { 0.0000, 0.0000, 0.0000 }, { 0.0000, 0.0000, 0.0000 },
100         { 0.0000, 0.0000, 0.0000 }, { 0.0000, 0.0000, 0.0000 }, { 0.0000, 0.0000, 0.0000 },
101         { 0.0000, 0.0000, 0.0000 }, { 0.0000, 0.0000, 0.0000 }, { 0.0000, 0.0000, 0.0000 },
102         { 0.0000, 0.0000, 0.0000 }, { 0.0000, 0.0000, 0.0000 }, { 0.0000, 0.0000, 0.0000 },
103     };
104     // A simulation state contains all the molecular information about the system.
105     nblib::SimulationState simState(coordinates, velocities, forces, box, topology);
106     // Kernel options are flags needed for force calculation.
107     nblib::NBKernelOptions options = nblib::NBKernelOptions();
108     // Use a simple cutoff rule for Coulomb
109     options.coulombType = nblib::CoulombType::Cutoff;
110     // Some performance flags can be set a run time
111     options.nbnxmSimd = nblib::SimdKernels::SimdNo;
112     // The force calculator contains all the data needed to compute forces.
113     nblib::ForceCalculator forceCalculator(simState, options);
114     // Integration requires masses, positions, and forces
115     nblib::LeapFrog integrator(simState.topology(), simState.box());
116     // Print some diagnostic info
117     printf("initial forces on particle 0: x %4f y %4f z %4f\n", forces[0][0], forces[0][1], forces[0][2]);
118     // The forces are computed for the user
119     gmx::ArrayRef<nblib::Vec3> userForces(simState.forces());
120     forceCalculator.compute(simState.coordinates(), userForces);
121     // Print some diagnostic info
122     printf("  final forces on particle 0: x %4f y %4f z %4f\n",
123            userForces[0][0],
124            userForces[0][1],
125            userForces[0][2]);
126     // User may modify forces stored in simState.forces() if needed
127     // Print some diagnostic info
128     printf("initial position of particle 0: x %4f y %4f z %4f\n",
129            simState.coordinates()[0][0],
130            simState.coordinates()[0][1],
131            simState.coordinates()[0][2]);
132     // Integrate with a time step of 1 fs
133     integrator.integrate(1.0, simState.coordinates(), simState.velocities(), simState.forces());
134     // Print some diagnostic info
135
136     printf("  final position of particle 0: x %4f y %4f z %4f\n",
137            simState.coordinates()[0][0],
138            simState.coordinates()[0][1],
139            simState.coordinates()[0][2]);
140     return 0;
141 }