diff --git a/src/PlumedMain.cpp b/src/PlumedMain.cpp
index 82f96058e764e32c401f8e73b3d09126360492b8..8facfdf86afb5b2f8387bf25beee1f9d919bd4f9 100644
--- a/src/PlumedMain.cpp
+++ b/src/PlumedMain.cpp
@@ -62,7 +62,9 @@ PlumedMain::PlumedMain():
   bias(0.0),
   exchangePatterns(*new(ExchangePatterns)),
   novirial(false),
-  restart(false)
+  restart(false),
+  stopFlag(NULL),
+  stopNow(false)
 {
   log.link(comm);
   log.setLinePrefix("PLUMED: ");
@@ -271,6 +273,10 @@ void PlumedMain::cmd(const std::string & word,void*val){
        CHECK_NOTINIT(initialized,word);
        CHECK_NULL(val,word);
        log.open(static_cast<char*>(val),"w");
+  } else if(word=="setStopFlag"){
+       CHECK_NOTINIT(initialized,word);
+       CHECK_NULL(val,word);
+       stopFlag=static_cast<int*>(val);
   } else if(word=="getExchangesFlag"){
        CHECK_INIT(initialized,word);
        CHECK_NULL(val,word);
@@ -535,6 +541,11 @@ void PlumedMain::justApply(){
   for(ActionSet::iterator p=actionSet.begin();p!=actionSet.end();++p){
     if((*p)->isActive()) (*p)->update();
   }
+// Check that no action has told the calculation to stop
+  if(stopNow){
+     if(stopFlag) (*stopFlag)=1;
+     else plumed_merror("your md code cannot handle plumed stop events - add a call to plumed.comm(stopFlag,stopCondition)");
+  }  
   stopwatch.stop("5 Applying (backward loop)");
 }
 
@@ -608,6 +619,9 @@ void PlumedMain::eraseFile(PlumedFileBase&f){
   files.erase(&f);
 }
 
+void PlumedMain::stop(){ 
+  stopNow=true;
+} 
 
 
 
diff --git a/src/PlumedMain.h b/src/PlumedMain.h
index 1e087127a2c632c07da950233d4df15edcba7f07..48788c930dbd94c25c8f2d400fc0864f18de8c61 100644
--- a/src/PlumedMain.h
+++ b/src/PlumedMain.h
@@ -129,6 +129,10 @@ private:
   std::set<PlumedFileBase*> files;
   typedef std::set<PlumedFileBase*>::iterator files_iterator;
 
+/// Stuff to make plumed stop the MD code cleanly
+  int* stopFlag;
+  bool stopNow;
+
 public:
 /// Flag to switch off virial calculation (for debug)
   bool novirial;
@@ -237,6 +241,8 @@ public:
   void fflush();
 /// Check if restarting
   bool getRestart()const;
+/// Stop the calculation cleanly (both the MD code and plumed)
+  void stop();
 };
 
 /////