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