Merge branch release-2021 into master
[alexxy/gromacs.git] / api / gmxapi / cpp / system.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2018,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
36 #include "gmxapi/system.h"
37
38 #include <array>
39 #include <memory>
40
41 #include "gromacs/utility.h"
42 #include "gromacs/mdrun/runner.h"
43
44 #include "gmxapi/context.h"
45 #include "gmxapi/md.h"
46 #include "gmxapi/session.h"
47 #include "gmxapi/status.h"
48
49 #include "system_impl.h"
50 #include "workflow.h"
51
52 namespace gmxapi
53 {
54
55 //! \cond
56 System::Impl::~Impl() = default;
57
58 System::Impl::Impl(System::Impl&&) noexcept = default;
59
60 System::Impl& System::Impl::operator=(System::Impl&& source) noexcept
61 {
62     if (this != &source)
63     {
64         workflow_.swap(source.workflow_);
65     }
66     return *this;
67 }
68 //! \endcond
69
70 std::shared_ptr<Session> System::launch(const std::shared_ptr<Context>& context)
71 {
72     return impl_->launch(context);
73 }
74
75 //! \cond
76 System::System(std::unique_ptr<Impl> implementation) : impl_{ std::move(implementation) }
77 {
78     GMX_ASSERT(impl_, "Constructor requires valid implementation object.");
79 }
80 System::Impl* System::get() const
81 {
82     return impl_.get();
83 }
84
85 System::~System() = default;
86 //! \endcond
87
88 System::System(System&&) noexcept = default;
89
90 System& System::operator=(System&&) noexcept = default;
91
92 System fromTprFile(const std::string& filename)
93 {
94     // TODO Confirm the file is readable and parseable and note unique
95     // identifying information for when the work spec is used in a different
96     // environment.
97
98     // Create a new Workflow instance.
99     // TODO error handling
100     auto workflow = Workflow::create(filename);
101
102     // This may produce errors or throw exceptions in the future, but as of
103     // 0.0.3 only memory allocation errors are possible, and we do not have a
104     // plan for how to recover from them.
105     auto systemImpl = std::make_unique<System::Impl>(std::move(workflow));
106     GMX_ASSERT(systemImpl, "Could not create a valid implementation object.");
107     auto system = System(std::move(systemImpl));
108
109     // TODO Separate TPR information into appropriate abstractions.
110     // The TPR file has enough information for us to
111     //  1. choose an MD engine
112     //  2. Get structure information
113     //  3. Get topology information
114     //  4. Get a lot of simulation and runtime parameters, but not all.
115     // It does not have enough information on its own to determine much about the
116     // necessary computation environment. That comes from environment
117     // introspection and user runtime options.
118
119     return system;
120 }
121
122 std::shared_ptr<Workflow> getWork(const System::Impl& system)
123 {
124     return system.workflow_;
125 }
126
127 System::Impl::Impl(std::unique_ptr<gmxapi::Workflow> workflow) noexcept :
128     workflow_(std::move(workflow)), spec_(std::make_shared<MDWorkSpec>())
129 {
130     GMX_ASSERT(workflow_, "Class invariant implies non-null workflow_ member");
131     GMX_ASSERT(spec_, "Class invariant implies non-null work specification member.");
132 }
133
134 std::shared_ptr<Session> System::Impl::launch(const std::shared_ptr<Context>& context)
135 {
136     std::shared_ptr<Session> session = nullptr;
137     if (context != nullptr)
138     {
139         // TODO: gmxapi::Workflow, gmxapi::MDWorkSpec, and gmxapi::MDModule need sensible consolidation.
140         session = context->launch(*workflow_);
141         GMX_ASSERT(session, "Context::launch() expected to produce non-null session.");
142
143         for (auto&& module : spec_->getModules())
144         {
145             // TODO: This should be the job of the launching code that produces the Session.
146             // Configure the restraints in a restraint manager made available to the session launcher.
147             addSessionRestraint(session.get(), module);
148         }
149     }
150     else
151     {
152         // we should log the error and return nullptr, but we have nowhere to set
153         // a status object, by the described behavior.
154     }
155
156     return session;
157 }
158
159 } // end namespace gmxapi