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