Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 : Copyright (c) 2016-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 "Function.h" 23 : #include "ActionRegister.h" 24 : #include "tools/OpenMP.h" 25 : 26 : namespace PLMD { 27 : namespace function { 28 : 29 : //+PLUMEDOC FUNCTION LOCALENSEMBLE 30 : /* 31 : Calculates the average over multiple arguments. 32 : 33 : If more than one collective variable is given for each argument then they 34 : are averaged separately. The average is stored in a component labelled <em>label</em>.cvlabel. 35 : 36 : \par Examples 37 : 38 : The following input tells plumed to calculate the chemical shifts for four 39 : different proteins in the same simulation box then average them, calculated 40 : the sum of the squared deviation with respect to the experimental values and 41 : applies a linear restraint. 42 : \plumedfile 43 : MOLINFO STRUCTURE=data/template.pdb 44 : 45 : chaina: GROUP ATOMS=1-1640 46 : chainb: GROUP ATOMS=1641-3280 47 : chainc: GROUP ATOMS=3281-4920 48 : chaind: GROUP ATOMS=4921-6560 49 : 50 : WHOLEMOLECULES ENTITY0=chaina ENTITY1=chainb ENTITY2=chainc ENTITY3=chaind 51 : 52 : csa: CS2BACKBONE ATOMS=chaina NRES=100 DATA=data/ TEMPLATE=chaina.pdb NOPBC 53 : csb: CS2BACKBONE ATOMS=chainb NRES=100 DATA=data/ TEMPLATE=chainb.pdb NOPBC 54 : csc: CS2BACKBONE ATOMS=chainc NRES=100 DATA=data/ TEMPLATE=chainc.pdb NOPBC 55 : csd: CS2BACKBONE ATOMS=chaind NRES=100 DATA=data/ TEMPLATE=chaind.pdb NOPBC 56 : 57 : ensca: LOCALENSEMBLE NUM=4 ARG1=(csa\.ca_.*) ARG2=(csb\.ca_.*) ARG3=(csc\.ca_.*) ARG4=(csd\.ca_.*) 58 : enscb: LOCALENSEMBLE NUM=4 ARG1=(csa\.cb_.*) ARG2=(csb\.cb_.*) ARG3=(csc\.cb_.*) ARG4=(csd\.cb_.*) 59 : ensco: LOCALENSEMBLE NUM=4 ARG1=(csa\.co_.*) ARG2=(csb\.co_.*) ARG3=(csc\.co_.*) ARG4=(csd\.co_.*) 60 : enshn: LOCALENSEMBLE NUM=4 ARG1=(csa\.hn_.*) ARG2=(csb\.hn_.*) ARG3=(csc\.hn_.*) ARG4=(csd\.hn_.*) 61 : ensnh: LOCALENSEMBLE NUM=4 ARG1=(csa\.nh_.*) ARG2=(csb\.nh_.*) ARG3=(csc\.nh_.*) ARG4=(csd\.nh_.*) 62 : 63 : stca: STATS ARG=(ensca\.csa\.ca_.*) PARARG=(csa\.expca_.*) SQDEVSUM 64 : stcb: STATS ARG=(enscb\.csa\.cb_.*) PARARG=(csa\.expcb_.*) SQDEVSUM 65 : stco: STATS ARG=(ensco\.csa\.co_.*) PARARG=(csa\.expco_.*) SQDEVSUM 66 : sthn: STATS ARG=(enshn\.csa\.hn_.*) PARARG=(csa\.exphn_.*) SQDEVSUM 67 : stnh: STATS ARG=(ensnh\.csa\.nh_.*) PARARG=(csa\.expnh_.*) SQDEVSUM 68 : 69 : res: RESTRAINT ARG=stca.*,stcb.*,stco.*,sthn.*,stnh.* AT=0.,0.,0.,0.,0. KAPPA=0.,0.,0.,0.,0 SLOPE=16.,16.,12.,24.,0.5 70 : \endplumedfile 71 : 72 : */ 73 : //+ENDPLUMEDOC 74 : 75 : 76 : class LocalEnsemble : 77 : public Function 78 : { 79 : unsigned ens_dim; 80 : unsigned narg; 81 : public: 82 : explicit LocalEnsemble(const ActionOptions&); 83 : void calculate() override; 84 : static void registerKeywords(Keywords& keys); 85 : }; 86 : 87 : 88 10423 : PLUMED_REGISTER_ACTION(LocalEnsemble,"LOCALENSEMBLE") 89 : 90 3 : void LocalEnsemble::registerKeywords(Keywords& keys) { 91 3 : Function::registerKeywords(keys); 92 3 : keys.use("ARG"); 93 6 : keys.add("compulsory","NUM","the number of local replicas"); 94 3 : useCustomisableComponents(keys); 95 3 : } 96 : 97 2 : LocalEnsemble::LocalEnsemble(const ActionOptions&ao): 98 : Action(ao), 99 : Function(ao), 100 2 : ens_dim(0) 101 : { 102 2 : parse("NUM",ens_dim); 103 2 : if(ens_dim==0) error("NUM should be greater or equal to 1"); 104 : 105 : std::vector<Value*> arg; 106 : int oldsize=-1; 107 8 : for(unsigned i=1; i<=ens_dim; ++i ) { 108 : std::vector<Value*> larg; 109 12 : if(!parseArgumentList("ARG",i,larg)) break; 110 18 : for(unsigned j=0; j<larg.size(); j++) arg.push_back(larg[j]); 111 6 : if(oldsize!=-1&&oldsize!=static_cast<int>(larg.size())) error("In LOCALENSEMBLE you should have the same number of arguments for each ARG keyword"); 112 6 : oldsize = larg.size(); 113 6 : if(!larg.empty()) { 114 6 : log.printf(" with arguments %u: ", i); 115 18 : for(unsigned j=0; j<larg.size(); j++) log.printf(" %s",larg[j]->getName().c_str()); 116 6 : log.printf("\n"); 117 : } 118 : } 119 2 : requestArguments(arg); 120 2 : narg = arg.size()/ens_dim; 121 : 122 : // these are the averages 123 6 : for(unsigned i=0; i<narg; i++) { 124 4 : std::string s=getPntrToArgument(i)->getName(); 125 4 : addComponentWithDerivatives(s); 126 4 : getPntrToComponent(i)->setNotPeriodic(); 127 : } 128 : 129 2 : log.printf(" averaging over %u replicas.\n", ens_dim); 130 2 : } 131 : 132 40 : void LocalEnsemble::calculate() 133 : { 134 40 : const double fact = 1.0/static_cast<double>(ens_dim); 135 40 : #pragma omp parallel for num_threads(OpenMP::getNumThreads()) 136 : for(unsigned i=0; i<narg; ++i) { 137 : double mean = 0.; 138 : Value* v=getPntrToComponent(i); 139 : for(unsigned j=0; j<ens_dim; ++j) { 140 : const unsigned index = j*narg+i; 141 : setDerivative(v, index, fact); 142 : mean += fact*getArgument(index); 143 : } 144 : v->set(mean); 145 : } 146 40 : } 147 : 148 : } 149 : } 150 : 151 :