Restore gmxapi._gmxapi.add_mdmodule() Python functionality.
[alexxy/gromacs.git] / python_packaging / src / gmxapi / pycontext.cpp
index 5b9878efbb3d6d7ea8064cfbf3d6750a43d6cf60..a2640301d5f5e4e362e2db84fe573fee9c9e25e7 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * This file is part of the GROMACS molecular simulation package.
  *
- * Copyright (c) 2019,2020, by the GROMACS development team, led by
+ * Copyright (c) 2019,2020,2021, by the GROMACS development team, led by
  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
  * and including many others, as listed in the AUTHORS file in the
  * top-level source directory and at http://www.gromacs.org.
  */
 #include "pycontext.h"
 
+#include "gmxapi/exceptions.h"
 #include "gmxapi/gmxapi.h"
 #include "gmxapi/md.h"
+#include "gmxapi/session.h"
+#include "gmxapi/status.h"
 
 
 namespace py = pybind11;
@@ -58,7 +61,23 @@ void PyContext::setMDArgs(const MDArgs& mdArgs)
 std::shared_ptr<gmxapi::Session> PyContext::launch(const gmxapi::Workflow& work)
 {
     assert(context_);
-    return context_->launch(work);
+    std::shared_ptr<gmxapi::Session> session = nullptr;
+
+    // TODO: gmxapi::Workflow, gmxapi::MDWorkSpec, and gmxapi::MDModule need sensible consolidation.
+    session = gmxapi::launchSession(context_.get(), work);
+    if (!session)
+    {
+        throw gmxapi::ProtocolError("Context::launch() expected to produce non-null session.");
+    }
+
+    for (auto&& module : workNodes_->getModules())
+    {
+        // TODO: This should be the job of the launching code that produces the Session.
+        // Configure the restraints in a restraint manager made available to the session launcher.
+        auto status = gmxapi::addSessionRestraint(session.get(), module);
+    }
+
+    return session;
 }
 
 std::shared_ptr<gmxapi::MDWorkSpec> PyContext::getSpec() const
@@ -81,7 +100,7 @@ PyContext::PyContext() :
     assert(workNodes_);
 }
 
-void PyContext::addMDModule(pybind11::object force_object)
+void PyContext::addMDModule(const pybind11::object& force_object) const
 {
     // If force_object has a bind method, give it a PyCapsule with a pointer
     // to our C++ object.