From a67412c25d038da5817d1695722ed43dcb71c01a Mon Sep 17 00:00:00 2001
From: Giovanni Bussi <giovanni.bussi@gmail.com>
Date: Wed, 13 Jul 2011 11:38:42 +0200
Subject: [PATCH] Added preparation step for Actions

New virtual method for Actions which is called at steps when action is
active just before atoms sharing. This is the right place where Actions
can change their atom requirements. E.g., you can do

class ColvarSomething{
...
void prepare();
}

void ColvarSomething::prepare(){
// take here your decision and eventually change the requested atoms
}
---
 src/Action.cpp     |  5 +++++
 src/Action.h       |  9 ++++++++-
 src/PlumedMain.cpp | 10 +++++++---
 3 files changed, 20 insertions(+), 4 deletions(-)

diff --git a/src/Action.cpp b/src/Action.cpp
index 2ac3b5bdf..3da7ff840 100644
--- a/src/Action.cpp
+++ b/src/Action.cpp
@@ -109,6 +109,11 @@ void Action::calculateNumericalDerivatives(){
   assert(0);
 }
 
+void Action::prepare(){
+  return;
+}
+
+
 
 
 
diff --git a/src/Action.h b/src/Action.h
index b432074e8..0af1fdf83 100644
--- a/src/Action.h
+++ b/src/Action.h
@@ -107,6 +107,13 @@ public:
 
   PlumedCommunicator& comm;
 
+/// Prepare an Action for calculation
+/// This can be used by Action if they need some special preparation
+/// before calculation. Typical case is for collective variables
+/// which would like to change their list of requested atoms.
+/// By default (if not overridden) does nothing.
+  virtual void prepare();
+
 /// Calculate an Action.
 /// This method is called one or more times per step.
 /// The set of all Actions is calculated in forward order.
@@ -114,7 +121,7 @@ public:
 
 /// Apply an Action.
 /// This method is called one time per step.
-/// The set of all Actions is applied in backword order.
+/// The set of all Actions is applied in backward order.
   virtual void apply()=0;
 
 /// Tell to the Action to flush open files
diff --git a/src/PlumedMain.cpp b/src/PlumedMain.cpp
index 5df10ce54..cffdd3864 100644
--- a/src/PlumedMain.cpp
+++ b/src/PlumedMain.cpp
@@ -373,8 +373,6 @@ void PlumedMain::prepareDependencies(){
 
   atoms.setCollectEnergy(false);
 
-// activate all the actions which are on step
-// activation is recursive and enables also the dependencies
   for(ActionSet::iterator p=actionSet.begin();p!=actionSet.end();p++){
     (*p)->deactivate();
     if(Colvar *c=dynamic_cast<Colvar*>(*p)) {
@@ -382,13 +380,19 @@ void PlumedMain::prepareDependencies(){
     }
   }
 
+// activate all the actions which are on step
+// activation is recursive and enables also the dependencies
   for(unsigned i=0;i<pilots.size();++i){
     if(pilots[i]->onStep()){
       pilots[i]->activate();
       active=true;
      }
   };
-}
+
+// allow actions to update their request list before atoms are shared
+  for(ActionSet::iterator p=actionSet.begin();p!=actionSet.end();p++)
+    if((*p)->isActive()) (*p)->prepare();
+  }
 
 void PlumedMain::shareData(){
   if(active)atoms.share();
-- 
GitLab