Allow more granularity for public interfaces in build tree.
[gromacs.git] / api / gmxapi / include / gmxapi / md.h
blob97d7bb97d30cbef1d82364eb527a26ef9b0e545e
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2018,2019,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.
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.
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.
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.
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.
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.
35 #ifndef GMXAPI_MD_H
36 #define GMXAPI_MD_H
37 /*! \file
38 * \brief Declare base classes and API for MD simulation engines.
40 * This header allows interaction with gmxapi for basic MD simulation functionality in client code
41 * without additional dependencies.
43 * Helper functions, standard concrete classes, and implementation interfaces are in gmxapi/md/
44 * \ingroup gmxapi_md
46 /*! \dir md
47 * \brief Additional declarations for API implementation and extension.
49 * This directory contains headers that require gromacsfwd.h and that declare
50 * objects that have stronger dependencies on GROMACS to fully define or extend.
51 * \ingroup gmxapi_md
54 /*! \defgroup gmxapi_md Molecular Dynamics
55 * \brief API access to Molecular Mechanics and Molecular Dynamics calculation in GROMACS
57 * At a minimum, the client code must specify the input source for a MD simulation. Helper functions
58 * allow setting up inputs from a standard GROMACS run input `.tpr` file. A System object serves as
59 * a container for a molecular system and associated computational work.
61 * \ingroup gmxapi
63 #include <memory>
64 #include <string>
65 #include <vector>
67 namespace gmxapi
70 class MDModule;
72 /*! \addtogroup gmxapi_md
74 # Extending MD with custom code.
76 Classes deriving from MDModule can interact with GROMACS at a low level to
77 extend the native GROMACS functionality. It is an evolving API that will
78 allow increased flexibility with further development. Near term functionality
79 includes the ability to attach custom code apply restraint forces during
80 simulation.
82 Below, we show code that extends the GROMACS library, code that interfaces with this
83 API, and client code that calls GROMACS with the custom code.
84 Sample MD plugin code is available at https://github.com/kassonlab/sample_restraint
86 \todo Move this to an example source code file that we can compile and test.
88 ## Example
90 In client code, extend the GROMACS library by implementing a new restraint
91 potential (see library documentation).
93 The gmxapi protocol to register a gmxapi::MDModule with a gmxapi::Session
94 by passing an gmx::IRestraintPotential to a gmx::MdRunner is described in the
95 gmxapi::Session docs. To exercise it, we need to call gmxapi::Session::setRestraint(),
96 passing a std::shared_ptr<gmxapi::MDModule> argument.
98 class NullRestraint : public gmx::IRestraintPotential
100 public:
101 gmx::PotentialPointData evaluate(gmx::Vector r1,
102 gmx::Vector r2,
103 double t) override
105 return {};
109 Use gmxapi::MDModule to define an API object class that we can pass around.
111 class SimpleApiModule : public gmxapi::MDModule
113 public:
114 const char *name() override
116 return "NullApiModule";
119 // Implement the MDModule protocol.
120 std::shared_ptr<gmx::IRestraintPotential> getRestraint() override
122 auto restraint = std::make_shared<NullRestraint>();
123 return restraint;
127 C++ client code to run an MD simulation with the custom restraint.
129 bool mysim() {
130 auto system = gmxapi::fromTprFile(filename);
131 std::shared_ptr<gmxapi::Context> context = gmxapi::defaultContext();
132 auto runner = system->runner();
134 auto session = runner->initialize(context);
136 auto module = std::make_shared<SimpleApiModule>();
137 session->setRestraint(module);
139 gmxapi::Status status;
140 status = session->run();
141 return status.success();
147 * \brief Container for Molecular Dynamics simulation setup.
149 * Client code provides the specification for MD work through an object of this type and registers
150 * the object in the computing context when an execution session is launched. The contents of the
151 * MDWorkSpec are used to pass appropriate parameters to the MD runner.
153 * \ingroup gmxapi_md
155 class MDWorkSpec
157 public:
158 MDWorkSpec();
159 ~MDWorkSpec();
162 * \brief Grant shared ownership of a modular MD computation object
164 * \param module instance that can produce a IRestraintPotential at runtime.
166 void addModule(std::shared_ptr<gmxapi::MDModule> module);
169 * \brief Get a handle to the stored list of modules
171 * Future versions of MDWorkSpec will not directly hold and grant access to module instances.
172 * \return reference that is only valid for the life of this object.
174 std::vector<std::shared_ptr<gmxapi::MDModule>>& getModules();
176 private:
177 //! \cond internal
178 //! \brief Private implementation class
179 class Impl;
180 //! \brief Opaque pointer to implementation object.
181 std::unique_ptr<Impl> impl_;
182 //! \endcond
185 } // end namespace gmxapi
187 #endif // header guard