Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 : Copyright (c) 2014-2019 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 "core/PlumedMain.h"
25 : #include "core/Atoms.h"
26 :
27 : using namespace std;
28 :
29 : namespace PLMD {
30 : namespace function {
31 :
32 : //+PLUMEDOC FUNCTION ENSEMBLE
33 : /*
34 : Calculates the replica averaging of a collective variable over multiple replicas.
35 :
36 : Each collective variable is averaged separately and stored in a component labelled <em>label</em>.cvlabel.
37 :
38 : \par Examples
39 :
40 : The following input tells plumed to calculate the distance between atoms 3 and 5
41 : and the average it over the available replicas.
42 : \plumedfile
43 : dist: DISTANCE ATOMS=3,5
44 : ens: ENSEMBLE ARG=dist
45 : PRINT ARG=dist,ens.dist
46 : \endplumedfile
47 :
48 : */
49 : //+ENDPLUMEDOC
50 :
51 :
52 54 : class Ensemble :
53 : public Function
54 : {
55 : unsigned ens_dim;
56 : unsigned my_repl;
57 : unsigned narg;
58 : bool master;
59 : bool do_reweight;
60 : bool do_moments;
61 : bool do_central;
62 : bool do_powers;
63 : double kbt;
64 : double moment;
65 : double power;
66 : public:
67 : explicit Ensemble(const ActionOptions&);
68 : void calculate();
69 : static void registerKeywords(Keywords& keys);
70 : };
71 :
72 :
73 6479 : PLUMED_REGISTER_ACTION(Ensemble,"ENSEMBLE")
74 :
75 28 : void Ensemble::registerKeywords(Keywords& keys) {
76 28 : Function::registerKeywords(keys);
77 56 : keys.use("ARG");
78 84 : keys.addFlag("REWEIGHT",false,"simple REWEIGHT using the latest ARG as energy");
79 84 : keys.addFlag("CENTRAL",false,"calculate a central moment instead of a standard moment");
80 112 : keys.add("optional","TEMP","the system temperature - this is only needed if you are reweighting");
81 112 : keys.add("optional","MOMENT","the moment you want to calculate in alternative to the mean or the variance");
82 112 : keys.add("optional","POWER","the power of the mean (and moment)");
83 28 : ActionWithValue::useCustomisableComponents(keys);
84 28 : }
85 :
86 27 : Ensemble::Ensemble(const ActionOptions&ao):
87 : Action(ao),
88 : Function(ao),
89 : do_reweight(false),
90 : do_moments(false),
91 : do_central(false),
92 : do_powers(false),
93 : kbt(-1.0),
94 : moment(0),
95 27 : power(0)
96 : {
97 54 : parseFlag("REWEIGHT", do_reweight);
98 27 : double temp=0.0;
99 54 : parse("TEMP",temp);
100 27 : if(do_reweight) {
101 24 : if(temp>0.0) kbt=plumed.getAtoms().getKBoltzmann()*temp;
102 0 : else kbt=plumed.getAtoms().getKbT();
103 12 : if(kbt==0.0) error("Unless the MD engine passes the temperature to plumed, with REWEIGHT you must specify TEMP");
104 : }
105 :
106 54 : parse("MOMENT",moment);
107 27 : if(moment==1) error("MOMENT can be any number but for 0 and 1");
108 27 : if(moment!=0) do_moments=true;
109 54 : parseFlag("CENTRAL", do_central);
110 27 : if(!do_moments&&do_central) error("To calculate a CENTRAL moment you need to define for which MOMENT");
111 :
112 54 : parse("POWER",power);
113 27 : if(power==1) error("POWER can be any number but for 0 and 1");
114 27 : if(power!=0) do_powers=true;
115 :
116 27 : checkRead();
117 :
118 27 : master = (comm.Get_rank()==0);
119 27 : ens_dim=0;
120 27 : my_repl=0;
121 27 : if(master) {
122 17 : ens_dim=multi_sim_comm.Get_size();
123 17 : my_repl=multi_sim_comm.Get_rank();
124 : }
125 27 : comm.Bcast(ens_dim,0);
126 27 : comm.Bcast(my_repl,0);
127 27 : if(ens_dim<2) log.printf("WARNING: ENSEMBLE with one replica is not doing any averaging!\n");
128 :
129 : // prepare output components, the number depending on reweighing or not
130 27 : narg = getNumberOfArguments();
131 27 : if(do_reweight) narg--;
132 :
133 : // these are the averages
134 6061 : for(unsigned i=0; i<narg; i++) {
135 : std::string s=getPntrToArgument(i)->getName();
136 3017 : addComponentWithDerivatives(s);
137 3017 : getPntrToComponent(i)->setNotPeriodic();
138 : }
139 : // these are the moments
140 27 : if(do_moments) {
141 0 : for(unsigned i=0; i<narg; i++) {
142 0 : std::string s=getPntrToArgument(i)->getName()+"_m";
143 0 : addComponentWithDerivatives(s);
144 0 : getPntrToComponent(i+narg)->setNotPeriodic();
145 : }
146 : }
147 :
148 27 : log.printf(" averaging over %u replicas.\n", ens_dim);
149 27 : if(do_reweight) log.printf(" doing simple REWEIGHT using the latest ARGUMENT as energy.\n");
150 27 : if(do_moments&&!do_central) log.printf(" calculating also the %lf standard moment\n", moment);
151 27 : if(do_moments&&do_central) log.printf(" calculating also the %lf central moment\n", moment);
152 27 : if(do_powers) log.printf(" calculating the %lf power of the mean (and moment)\n", power);
153 27 : }
154 :
155 125 : void Ensemble::calculate() {
156 : double norm = 0.0;
157 125 : double fact = 0.0;
158 :
159 : // calculate the weights either from BIAS
160 125 : if(do_reweight) {
161 : vector<double> bias;
162 0 : bias.resize(ens_dim);
163 0 : if(master) {
164 0 : bias[my_repl] = getArgument(narg);
165 0 : if(ens_dim>1) multi_sim_comm.Sum(&bias[0], ens_dim);
166 : }
167 0 : comm.Sum(&bias[0], ens_dim);
168 0 : const double maxbias = *(std::max_element(bias.begin(), bias.end()));
169 0 : for(unsigned i=0; i<ens_dim; ++i) {
170 0 : bias[i] = exp((bias[i]-maxbias)/kbt);
171 0 : norm += bias[i];
172 : }
173 0 : fact = bias[my_repl]/norm;
174 : // or arithmetic ones
175 : } else {
176 125 : norm = static_cast<double>(ens_dim);
177 125 : fact = 1.0/norm;
178 : }
179 :
180 125 : const double fact_kbt = fact/kbt;
181 :
182 125 : vector<double> mean(narg);
183 125 : vector<double> dmean(narg,fact);
184 : // calculate the mean
185 125 : if(master) {
186 6120 : for(unsigned i=0; i<narg; ++i) mean[i] = fact*getArgument(i);
187 197 : if(ens_dim>1) multi_sim_comm.Sum(&mean[0], narg);
188 : }
189 250 : comm.Sum(&mean[0], narg);
190 :
191 : vector<double> v_moment, dv_moment;
192 : // calculate other moments
193 125 : if(do_moments) {
194 0 : v_moment.resize(narg);
195 0 : dv_moment.resize(narg);
196 : // standard moment
197 0 : if(!do_central) {
198 0 : if(master) {
199 0 : for(unsigned i=0; i<narg; ++i) {
200 0 : const double tmp = fact*pow(getArgument(i),moment-1);
201 0 : v_moment[i] = tmp*getArgument(i);
202 0 : dv_moment[i] = moment*tmp;
203 : }
204 0 : if(ens_dim>1) multi_sim_comm.Sum(&v_moment[0], narg);
205 : } else {
206 0 : for(unsigned i=0; i<narg; ++i) {
207 0 : const double tmp = fact*pow(getArgument(i),moment-1);
208 0 : dv_moment[i] = moment*tmp;
209 : }
210 : }
211 : // central moment
212 : } else {
213 0 : if(master) {
214 0 : for(unsigned i=0; i<narg; ++i) {
215 0 : const double tmp = pow(getArgument(i)-mean[i],moment-1);
216 0 : v_moment[i] = fact*tmp*(getArgument(i)-mean[i]);
217 0 : dv_moment[i] = moment*tmp*(fact-fact/norm);
218 : }
219 0 : if(ens_dim>1) multi_sim_comm.Sum(&v_moment[0], narg);
220 : } else {
221 0 : for(unsigned i=0; i<narg; ++i) {
222 0 : const double tmp = pow(getArgument(i)-mean[i],moment-1);
223 0 : dv_moment[i] = moment*tmp*(fact-fact/norm);
224 : }
225 : }
226 : }
227 0 : comm.Sum(&v_moment[0], narg);
228 : }
229 :
230 : // calculate powers of moments
231 125 : if(do_powers) {
232 120 : for(unsigned i=0; i<narg; ++i) {
233 96 : const double tmp1 = pow(mean[i],power-1);
234 48 : mean[i] *= tmp1;
235 48 : dmean[i] *= power*tmp1;
236 48 : if(do_moments) {
237 0 : const double tmp2 = pow(v_moment[i],power-1);
238 0 : v_moment[i] *= tmp2;
239 0 : dv_moment[i] *= power*tmp2;
240 : }
241 : }
242 : }
243 :
244 : // set components
245 6591 : for(unsigned i=0; i<narg; ++i) {
246 : // set mean
247 3233 : Value* v=getPntrToComponent(i);
248 6466 : v->set(mean[i]);
249 3233 : setDerivative(v, i, dmean[i]);
250 3233 : if(do_reweight) {
251 0 : const double w_tmp = fact_kbt*(getArgument(i) - mean[i]);
252 0 : setDerivative(v, narg, w_tmp);
253 : }
254 3233 : if(do_moments) {
255 : // set moments
256 0 : Value* u=getPntrToComponent(i+narg);
257 0 : u->set(v_moment[i]);
258 0 : setDerivative(u, i, dv_moment[i]);
259 0 : if(do_reweight) {
260 0 : const double w_tmp = fact_kbt*(pow(getArgument(i),moment) - v_moment[i]);
261 0 : setDerivative(u, narg, w_tmp);
262 : }
263 : }
264 : }
265 125 : }
266 :
267 : }
268 4839 : }
|