From 4c8d346e2d49c4a7ce44404c478922a7302005f7 Mon Sep 17 00:00:00 2001
From: Giovanni Bussi <giovanni.bussi@gmail.com>
Date: Fri, 2 Dec 2011 15:30:05 +0100
Subject: [PATCH] Added tool to recover total bias

This is useful to compute acceptance.
I did it in the "dirty" way: if a value named "bias" exist inside any
action, it is accumulated into the total bias. I also changed the name
of values in Restraint and MovingRestraint so that the energy is now called
"bias" and non-capital letters are more consistent
---
 src/BiasMovingRestraint.cpp | 10 +++++-----
 src/BiasRestraint.cpp       | 10 +++++-----
 src/PlumedMain.cpp          | 22 ++++++++++++++++------
 src/PlumedMain.h            |  5 +++++
 4 files changed, 31 insertions(+), 16 deletions(-)

diff --git a/src/BiasMovingRestraint.cpp b/src/BiasMovingRestraint.cpp
index 84349cdb5..cc0b600ed 100644
--- a/src/BiasMovingRestraint.cpp
+++ b/src/BiasMovingRestraint.cpp
@@ -125,9 +125,9 @@ PLUMED_BIAS_INIT(ao)
     log.printf("\n");
   };
 
-  addValue("Energy");
-  addValue("Force2");
-  addValue("Work");
+  addValue("bias");
+  addValue("force2");
+  addValue("work");
 }
 
 
@@ -163,9 +163,9 @@ void BiasMovingRestraint::calculate(){
     totf2+=f*f;
   };
   Value* value;
-  value=getValue("Energy");
+  value=getValue("bias");
   setValue(value,ene);
-  value=getValue("Force2");
+  value=getValue("force2");
   setValue(value,totf2);
 }
 
diff --git a/src/BiasRestraint.cpp b/src/BiasRestraint.cpp
index cefda6fb7..a51e3155a 100644
--- a/src/BiasRestraint.cpp
+++ b/src/BiasRestraint.cpp
@@ -30,7 +30,7 @@ values, and it is printing the energy of the restraint
 DISTANCE ATOMS=3,5 LABEL=d1
 DISTANCE ATOMS=2,4 LABEL=d2
 RESTRAINT ARG=d1,d2 AT=1.0,1.5 KAPPA=150.0,150.0 LABEL=restraint
-PRINT ARG=restraint.Energy
+PRINT ARG=restraint.bias
 \endverbatim
 (See also \ref DISTANCE and \ref PRINT).
 
@@ -65,8 +65,8 @@ kappa(getNumberOfArguments(),0.0)
   for(unsigned i=0;i<kappa.size();i++) log.printf(" %f",kappa[i]);
   log.printf("\n");
 
-  addValue("Energy");
-  addValue("Force2");
+  addValue("bias");
+  addValue("force2");
 }
 
 
@@ -82,8 +82,8 @@ void BiasRestraint::calculate(){
     totf2+=f*f;
   };
   Value* value;
-  value=getValue("Energy"); setValue(value,ene);
-  value=getValue("Force2");  setValue(value,totf2);
+  value=getValue("bias"); setValue(value,ene);
+  value=getValue("force2");  setValue(value,totf2);
 }
 
 }
diff --git a/src/PlumedMain.cpp b/src/PlumedMain.cpp
index 1bf1ac2c9..af23e340c 100644
--- a/src/PlumedMain.cpp
+++ b/src/PlumedMain.cpp
@@ -79,6 +79,7 @@ PlumedMain::PlumedMain():
   active(false),
   atoms(*this),
   actionSet((*this)),
+  bias(0.0),
   novirial(false)
 {}
 
@@ -426,21 +427,26 @@ void PlumedMain::waitData(){
 
 void PlumedMain::justCalculate(){
 
+  bias=0.0;
+
 // calculate the active actions in order (assuming *backward* dependence)
   for(ActionSet::iterator p=actionSet.begin();p!=actionSet.end();++p){
+    ActionWithValue*av=dynamic_cast<ActionWithValue*>(*p);
+    ActionAtomistic*aa=dynamic_cast<ActionAtomistic*>(*p);
     {
-      ActionWithValue*a=dynamic_cast<ActionWithValue*>(*p);
-      if(a) a->clearInputForces();
-      if(a) a->clearDerivatives();
+      if(av) av->clearInputForces();
+      if(av) av->clearDerivatives();
     }
     {
-      ActionAtomistic*a=dynamic_cast<ActionAtomistic*>(*p);
-      if(a) a->clearOutputForces();
-      if(a) if(a->isActive()) a->retrieveAtoms();
+      if(aa) aa->clearOutputForces();
+      if(aa) if(aa->isActive()) aa->retrieveAtoms();
     }
     if((*p)->isActive()){
       if((*p)->checkNumericalDerivatives()) (*p)->calculateNumericalDerivatives();
       else (*p)->calculate();
+      if(av)for(int i=0;i<av->getNumberOfValues();++i){
+        if(av->getValue(i)->getName()=="bias") bias+=av->getValue(i)->get();
+      }
     }
   }
 }
@@ -490,6 +496,10 @@ void PlumedMain::load(std::vector<std::string> & words){
     log<<"Here is the new list of available actions\n";
     log<<actionRegister();
 }
+
+double PlumedMain::getBias() const{
+  return bias;
+}
 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 
 
diff --git a/src/PlumedMain.h b/src/PlumedMain.h
index d298b38b8..1ab8c4919 100644
--- a/src/PlumedMain.h
+++ b/src/PlumedMain.h
@@ -80,6 +80,9 @@ private:
 /// Suffix string for file opening, useful for multiple simulations in the same directory
   std::string suffix;
 
+/// The total bias (=total energy of the restraints)
+  double bias;
+
 public:
 /// Flag to switch off virial calculation (for debug)
   bool novirial;
@@ -131,6 +134,8 @@ public:
   const std::string & getSuffix()const;
 /// Set the suffix string
   void setSuffix(const std::string&);
+/// get the value of the bias
+  double getBias()const;
 };
 
 /////
-- 
GitLab