Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 : Copyright (c) 2012-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 :
23 : #include "Function.h"
24 : #include "core/ActionRegister.h"
25 :
26 : namespace PLMD {
27 : namespace function {
28 :
29 : //+PLUMEDOC FUNCTION FUNCPATHMSD
30 : /*
31 : This function calculates path collective variables.
32 :
33 : This is the Path Collective Variables implementation described in the paper from the bibliography.
34 : This variable computes the progress along a given set of frames that is provided
35 : in input ("s" component) and the distance from them ("z" component).
36 : It is a function of mean squared displacement that are obtained by the joint use of mean squared displacement variables with the SQUARED flag
37 : (see below).
38 :
39 : ## Examples
40 :
41 : Here below is a case where you have defined three frames and you want to
42 : calculate the progress along the path and the distance from it in p1
43 :
44 : ```plumed
45 : #SETTINGS INPUTFILES=regtest/trajectories/path_msd/frame_1.dat,regtest/trajectories/path_msd/frame_21.dat,regtest/trajectories/path_msd/frame_42.dat
46 : t1: RMSD REFERENCE=regtest/trajectories/path_msd/frame_1.dat TYPE=OPTIMAL SQUARED
47 : t2: RMSD REFERENCE=regtest/trajectories/path_msd/frame_21.dat TYPE=OPTIMAL SQUARED
48 : t3: RMSD REFERENCE=regtest/trajectories/path_msd/frame_42.dat TYPE=OPTIMAL SQUARED
49 : p1: FUNCPATHMSD ARG=t1,t2,t3 LAMBDA=500.0
50 : PRINT ARG=t1,t2,t3,p1.s,p1.z STRIDE=1 FILE=colvar FMT=%8.4f
51 : ```
52 :
53 : You can see that the reference coordinates here are described in three separate pdb files that you view above.
54 :
55 : This second example shows how to define a PATH in [CONTACTMAP](CONTACTMAP.md) space:
56 :
57 : ```plumed
58 : CONTACTMAP ...
59 : ATOMS1=1,2 REFERENCE1=0.1
60 : ATOMS2=3,4 REFERENCE2=0.5
61 : ATOMS3=4,5 REFERENCE3=0.25
62 : ATOMS4=5,6 REFERENCE4=0.0
63 : SWITCH={RATIONAL R_0=1.5}
64 : LABEL=c1
65 : CMDIST
66 : ... CONTACTMAP
67 :
68 : CONTACTMAP ...
69 : ATOMS1=1,2 REFERENCE1=0.3
70 : ATOMS2=3,4 REFERENCE2=0.9
71 : ATOMS3=4,5 REFERENCE3=0.45
72 : ATOMS4=5,6 REFERENCE4=0.1
73 : SWITCH={RATIONAL R_0=1.5}
74 : LABEL=c2
75 : CMDIST
76 : ... CONTACTMAP
77 :
78 : CONTACTMAP ...
79 : ATOMS1=1,2 REFERENCE1=1.0
80 : ATOMS2=3,4 REFERENCE2=1.0
81 : ATOMS3=4,5 REFERENCE3=1.0
82 : ATOMS4=5,6 REFERENCE4=1.0
83 : SWITCH={RATIONAL R_0=1.5}
84 : LABEL=c3
85 : CMDIST
86 : ... CONTACTMAP
87 :
88 : p1: FUNCPATHMSD ARG=c1,c2,c3 LAMBDA=500.0
89 : PRINT ARG=c1,c2,c3,p1.s,p1.z STRIDE=1 FILE=colvar FMT=%8.4f
90 : ```
91 :
92 : This third example shows how to define a PATH in [PIV](PIV.md) space:
93 :
94 : ```plumed
95 : #SETTINGS INPUTFILES=regtest/piv/rt-piv-distance/Liq.pdb,regtest/piv/rt-piv-distance/Ice.pdb
96 : PIV ...
97 : LABEL=c1
98 : PRECISION=1000
99 : NLIST
100 : REF_FILE=regtest/piv/rt-piv-distance/Liq.pdb
101 : PIVATOMS=2
102 : ATOMTYPES=A,B
103 : ONLYDIRECT
104 : SFACTOR=1.0,0.2
105 : SORT=1,1
106 : SWITCH1={RATIONAL R_0=0.6 MM=12 NN=4}
107 : SWITCH2={RATIONAL R_0=0.5 MM=10 NN=5}
108 : NL_CUTOFF=1.2,1.2
109 : NL_STRIDE=10,10
110 : NL_SKIN=0.1,0.1
111 : ... PIV
112 : PIV ...
113 : LABEL=c2
114 : PRECISION=1000
115 : NLIST
116 : REF_FILE=regtest/piv/rt-piv-distance/Ice.pdb
117 : PIVATOMS=2
118 : ATOMTYPES=A,B
119 : ONLYDIRECT
120 : SFACTOR=1.0,0.2
121 : SORT=1,1
122 : SWITCH1={RATIONAL R_0=0.6 MM=12 NN=4}
123 : SWITCH2={RATIONAL R_0=0.4 MM=10 NN=5}
124 : NL_CUTOFF=1.2,1.2
125 : NL_STRIDE=10,10
126 : NL_SKIN=0.1,0.1
127 : ... PIV
128 :
129 : p1: FUNCPATHMSD ARG=c1,c2 LAMBDA=0.180338
130 : METAD ARG=p1.s,p1.z SIGMA=0.01,0.2 HEIGHT=0.8 PACE=500 LABEL=res
131 : PRINT ARG=c1,c2,p1.s,p1.z,res.bias STRIDE=500 FILE=colvar FMT=%15.6f
132 : ```
133 :
134 : */
135 : //+ENDPLUMEDOC
136 :
137 : class FuncPathMSD : public Function {
138 : double lambda;
139 : int neigh_size;
140 : double neigh_stride;
141 : std::vector< std::pair<Value *,double> > neighpair;
142 : std::map<Value *,double > indexmap; // use double to allow isomaps
143 : std::vector <Value*> allArguments;
144 : // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
145 : // this below is useful when one wants to sort a vector of double and have back the order
146 : // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
147 : // create a custom sorter
148 : typedef std::vector<double>::const_iterator myiter;
149 : struct ordering {
150 : bool operator ()(std::pair<unsigned, myiter> const& a, std::pair<unsigned, myiter> const& b) {
151 : return *(a.second) < *(b.second);
152 : }
153 : };
154 : // sorting utility
155 : std::vector<int> increasingOrder( std::vector<double> &v) {
156 : // make a pair
157 : std::vector< std::pair<unsigned, myiter> > order(v.size());
158 : unsigned n = 0;
159 : for (myiter it = v.begin(); it != v.end(); ++it, ++n) {
160 : order[n] = make_pair(n, it); // note: heere i do not put the values but the addresses that point to the value
161 : }
162 : // now sort according the second value
163 : std::sort(order.begin(), order.end(), ordering());
164 : std::vector<int> vv(v.size());
165 : n=0;
166 : for (const auto & it : order) {
167 : vv[n]=it.first;
168 : n++;
169 : }
170 : return vv;
171 : }
172 : // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
173 : // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
174 :
175 : struct pairordering {
176 : bool operator ()(std::pair<Value *, double> const& a, std::pair<Value*, double> const& b) {
177 437 : return (a).second > (b).second;
178 : }
179 : };
180 :
181 : public:
182 : explicit FuncPathMSD(const ActionOptions&);
183 : // active methods:
184 : void calculate() override;
185 : void prepare() override;
186 : static void registerKeywords(Keywords& keys);
187 : };
188 :
189 : PLUMED_REGISTER_ACTION(FuncPathMSD,"FUNCPATHMSD")
190 :
191 4 : void FuncPathMSD::registerKeywords(Keywords& keys) {
192 4 : Function::registerKeywords(keys);
193 4 : keys.add("compulsory","LAMBDA","the lambda parameter is needed for smoothing, is in the units of plumed");
194 4 : keys.add("optional","NEIGH_SIZE","size of the neighbor list");
195 4 : keys.add("optional","NEIGH_STRIDE","how often the neighbor list needs to be calculated in time units");
196 8 : keys.addOutputComponent("s","default","scalar","the position on the path");
197 8 : keys.addOutputComponent("z","default","scalar","the distance from the path");
198 4 : keys.addDOI("10.1063/1.2432340");
199 4 : }
200 2 : FuncPathMSD::FuncPathMSD(const ActionOptions&ao):
201 : Action(ao),
202 : Function(ao),
203 2 : neigh_size(-1),
204 2 : neigh_stride(-1.) {
205 :
206 2 : parse("LAMBDA",lambda);
207 2 : parse("NEIGH_SIZE",neigh_size);
208 2 : parse("NEIGH_STRIDE",neigh_stride);
209 2 : checkRead();
210 2 : log.printf(" lambda is %f\n",lambda);
211 : // list the action involved and check the type
212 2 : std::string myname=getPntrToArgument(0)->getPntrToAction()->getName();
213 2 : if(myname!="RMSD_SCALAR"&&myname!="CONTACTMAP"&&myname!="DISTANCE"&&myname!="PIV") {
214 0 : error("One or more of your arguments is not of RMSD/CONTACTMAP/DISTANCE/PIV type!!!");
215 : }
216 6 : for(unsigned i=1; i<getNumberOfArguments(); i++) {
217 : // for each value get the name and the label of the corresponding action
218 4 : if( getPntrToArgument(i)->getPntrToAction()->getName()!=myname ) {
219 0 : error("mismatch between the types of arguments");
220 : }
221 : }
222 2 : log.printf(" Consistency check completed! Your path cvs look good!\n");
223 : // do some neighbor printout
224 2 : if(neigh_stride>0. || neigh_size>0) {
225 1 : if(neigh_size>static_cast<int>(getNumberOfArguments())) {
226 0 : log.printf(" List size required ( %d ) is too large: resizing to the maximum number of arg required: %d \n",neigh_size,getNumberOfArguments());
227 0 : neigh_size=getNumberOfArguments();
228 : }
229 1 : log.printf(" Neighbor list enabled: \n");
230 1 : log.printf(" size : %d elements\n",neigh_size);
231 1 : log.printf(" stride : %f time \n",neigh_stride);
232 : } else {
233 1 : log.printf(" Neighbor list NOT enabled \n");
234 : }
235 :
236 2 : addComponentWithDerivatives("s");
237 2 : componentIsNotPeriodic("s");
238 2 : addComponentWithDerivatives("z");
239 2 : componentIsNotPeriodic("z");
240 :
241 : // now backup the arguments
242 8 : for(unsigned i=0; i<getNumberOfArguments(); i++) {
243 6 : allArguments.push_back(getPntrToArgument(i));
244 : }
245 : double i=1.;
246 8 : for(const auto & it : allArguments) {
247 6 : indexmap[it]=i;
248 6 : i+=1.;
249 : }
250 :
251 2 : }
252 : // calculator
253 1092 : void FuncPathMSD::calculate() {
254 : // log.printf("NOW CALCULATE! \n");
255 : double s_path=0.;
256 : double partition=0.;
257 1092 : if(neighpair.empty()) { // at first step, resize it
258 0 : neighpair.resize(allArguments.size());
259 0 : for(unsigned i=0; i<allArguments.size(); i++) {
260 0 : neighpair[i].first=allArguments[i];
261 : }
262 : }
263 :
264 1092 : Value* val_s_path=getPntrToComponent("s");
265 2184 : Value* val_z_path=getPntrToComponent("z");
266 :
267 3959 : for(auto & it : neighpair) {
268 2867 : it.second=std::exp(-lambda*(it.first->get()));
269 2867 : s_path+=(indexmap[it.first])*it.second;
270 2867 : partition+=it.second;
271 : }
272 1092 : s_path/=partition;
273 : val_s_path->set(s_path);
274 1092 : val_z_path->set(-(1./lambda)*std::log(partition));
275 : int n=0;
276 3959 : for(const auto & it : neighpair) {
277 2867 : double expval=it.second;
278 2867 : double tmp=lambda*expval*(s_path-(indexmap[it.first]))/partition;
279 : setDerivative(val_s_path,n,tmp);
280 2867 : setDerivative(val_z_path,n,expval/partition);
281 2867 : n++;
282 : }
283 :
284 : // log.printf("CALCULATION DONE! \n");
285 1092 : }
286 : ///
287 : /// this function updates the needed argument list
288 : ///
289 1092 : void FuncPathMSD::prepare() {
290 :
291 : // neighbor list: rank and activate the chain for the next step
292 :
293 : // neighbor list: if neigh_size<0 never sort and keep the full vector
294 : // neighbor list: if neigh_size>0
295 : // if the size is full -> sort the vector and decide the dependencies for next step
296 : // if the size is not full -> check if next step will need the full dependency otherwise keep this dependencies
297 :
298 : // here just resize the neighpair. The real resizing of reinit will be done by the prepare stage that will modify the list of arguments
299 1092 : if (neigh_size>0) {
300 546 : if(neighpair.size()==allArguments.size()) { // I just did the complete round: need to sort, shorten and give it a go
301 : // sort the values
302 137 : std::sort(neighpair.begin(),neighpair.end(),pairordering());
303 : // resize the effective list
304 137 : neighpair.resize(neigh_size);
305 137 : log.printf(" NEIGH LIST NOW INCLUDE INDEXES: ");
306 411 : for(int i=0; i<neigh_size; ++i) {
307 274 : log.printf(" %f ",indexmap[neighpair[i].first]);
308 : }
309 137 : log.printf(" \n");
310 : } else {
311 409 : if( int(getStep())%int(neigh_stride/getTimeStep())==0 ) {
312 137 : log.printf(" Time %f : recalculating full neighlist \n",getStep()*getTimeStep());
313 137 : neighpair.resize(allArguments.size());
314 548 : for(unsigned i=0; i<allArguments.size(); i++) {
315 411 : neighpair[i].first=allArguments[i];
316 : }
317 : }
318 : }
319 : } else {
320 546 : if( int(getStep())==0) {
321 1 : neighpair.resize(allArguments.size());
322 4 : for(unsigned i=0; i<allArguments.size(); i++) {
323 3 : neighpair[i].first=allArguments[i];
324 : }
325 : }
326 : }
327 : std::vector<Value*> argstocall;
328 : //log.printf("PREPARING \n");
329 : argstocall.clear();
330 1092 : if(!neighpair.empty()) {
331 3959 : for(const auto & it : neighpair) {
332 2867 : argstocall.push_back( it.first );
333 : // log.printf("CALLING %p %f ",(*it).first ,indexmap[(*it).first] );
334 : }
335 : } else {
336 0 : for(unsigned i=0; i<allArguments.size(); i++) {
337 0 : argstocall.push_back(allArguments[i]);
338 : }
339 : }
340 : // now the list of argument changes
341 1092 : requestArguments(argstocall);
342 : //now resize the derivatives as well
343 : //for each value in this action
344 3276 : for(int i=0; i< getNumberOfComponents(); i++) {
345 : //resize the derivative to the number the
346 2184 : getPntrToComponent(i)->clearDerivatives();
347 2184 : getPntrToComponent(i)->resizeDerivatives(getNumberOfArguments());
348 : }
349 : //log.printf("PREPARING DONE! \n");
350 1092 : }
351 :
352 : }
353 : }
354 :
355 :
|