Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 : Copyright (c) 2011-2023 The plumed team 3 : (see the PEOPLE file at the root of the distribution for a list of names) 4 : 5 : See http://www.plumed.org for more information. 6 : 7 : This file is part of plumed, version 2. 8 : 9 : plumed is free software: you can redistribute it and/or modify 10 : it under the terms of the GNU Lesser General Public License as published by 11 : the Free Software Foundation, either version 3 of the License, or 12 : (at your option) any later version. 13 : 14 : plumed 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 17 : GNU Lesser General Public License for more details. 18 : 19 : You should have received a copy of the GNU Lesser General Public License 20 : along with plumed. If not, see <http://www.gnu.org/licenses/>. 21 : +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */ 22 : #include "core/ActionToPutData.h" 23 : #include "core/DomainDecomposition.h" 24 : #include "core/PlumedMain.h" 25 : #include "core/ActionSet.h" 26 : #include "core/ActionRegister.h" 27 : 28 : namespace PLMD { 29 : namespace colvar { 30 : 31 : //+PLUMEDOC COLVAR ENERGY 32 : /* 33 : Calculate the total potential energy of the simulation box. 34 : 35 : The potential energy can be biased e.g. with umbrella sampling \cite bart-karp98jpcb or with well-tempered metadynamics \cite Bonomi:2009p17935. 36 : 37 : Notice that this CV could be unavailable with some MD code. When 38 : it is available, and when also replica exchange is available, 39 : metadynamics applied to ENERGY can be used to decrease the 40 : number of required replicas. 41 : 42 : \warning Some MD engines communicate to PLUMED the energy without the long tail corrections, 43 : leading to a small mismatch. If this is the case, use the energy from the MD engine for 44 : reweighting. See \issue{567}. 45 : 46 : \bug Acceptance for replica exchange when \ref ENERGY is biased 47 : is computed correctly only if all the replicas have the same 48 : potential energy function. This is for instance not true when 49 : using GROMACS with lambda replica exchange or with plumed-hrex branch. 50 : 51 : \par Examples 52 : 53 : The following input instructs plumed to print the energy of the system 54 : \plumedfile 55 : ene: ENERGY 56 : PRINT ARG=ene 57 : \endplumedfile 58 : 59 : */ 60 : //+ENDPLUMEDOC 61 : 62 : 63 : class Energy : public ActionToPutData { 64 : private: 65 : /// This is used to sum the data 66 : DomainDecomposition* interface; 67 : /// This is the list of forces that must be scaled 68 : std::vector<ActionToPutData*> forces_to_scale; 69 : public: 70 : explicit Energy(const ActionOptions&); 71 : // active methods: 72 : static void registerKeywords( Keywords& keys ); 73 : void wait() override; 74 : void apply() override; 75 : }; 76 : 77 : 78 : PLUMED_REGISTER_ACTION(Energy,"ENERGY") 79 : 80 40 : Energy::Energy(const ActionOptions&ao): 81 : Action(ao), 82 : ActionToPutData(ao), 83 40 : interface(NULL) { 84 40 : plumed.setEnergyValue( getLabel() ); 85 : std::vector<unsigned> shape; 86 40 : addValue( shape ); 87 40 : setNotPeriodic(); 88 80 : setUnit( "energy", "default" ); 89 40 : ActionToPutData* px=plumed.getActionSet().selectWithLabel< ActionToPutData*>("posx"); 90 40 : plumed_assert(px); 91 40 : forces_to_scale.push_back(px); 92 40 : addDependency( px ); 93 40 : ActionToPutData* py=plumed.getActionSet().selectWithLabel< ActionToPutData*>("posy"); 94 40 : plumed_assert(py); 95 40 : forces_to_scale.push_back(py); 96 40 : addDependency( py ); 97 40 : ActionToPutData* pz=plumed.getActionSet().selectWithLabel< ActionToPutData*>("posz"); 98 40 : plumed_assert(pz); 99 40 : forces_to_scale.push_back(pz); 100 40 : addDependency( pz ); 101 40 : ActionToPutData* bx=plumed.getActionSet().selectWithLabel< ActionToPutData*>("Box"); 102 40 : plumed_assert(bx); 103 40 : forces_to_scale.push_back(bx); 104 40 : addDependency( bx ); 105 40 : log<<" Bibliography "; 106 80 : log<<plumed.cite("Bartels and Karplus, J. Phys. Chem. B 102, 865 (1998)"); 107 80 : log<<plumed.cite("Bonomi and Parrinello, J. Comp. Chem. 30, 1615 (2009)"); 108 40 : log<<"\n"; 109 40 : } 110 : 111 44 : void Energy::registerKeywords( Keywords& keys ) { 112 44 : Action::registerKeywords( keys ); 113 44 : } 114 : 115 3989 : void Energy::wait() { 116 3989 : if( !interface ) { 117 40 : std::vector<DomainDecomposition*> allput=plumed.getActionSet().select<DomainDecomposition*>(); 118 40 : if( allput.size()>1 ) { 119 0 : warning("found more than one interface so don't know how to sum energy"); 120 : } 121 40 : interface = allput[0]; 122 : } 123 3989 : ActionToPutData::wait(); 124 3989 : if( interface ) { 125 3989 : interface->sumOverDomains( copyOutput(0) ); 126 : } 127 3989 : } 128 : 129 3989 : void Energy::apply() { 130 3989 : if( getPntrToValue()->forcesWereAdded() ) { 131 250 : for(unsigned i=0; i<forces_to_scale.size(); ++i) { 132 200 : forces_to_scale[i]->rescaleForces( 1.- getPntrToValue()->getForce(0)); 133 : } 134 : } 135 3989 : } 136 : 137 : } 138 : } 139 : 140 : 141 :