Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 : Copyright (c) 2020-2021 of Michele Invernizzi.
3 :
4 : This file is part of the OPES plumed module.
5 :
6 : The OPES plumed module is free software: you can redistribute it and/or modify
7 : it under the terms of the GNU Lesser General Public License as published by
8 : the Free Software Foundation, either version 3 of the License, or
9 : (at your option) any later version.
10 :
11 : The OPES plumed module is distributed in the hope that it will be useful,
12 : but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : GNU Lesser General Public License for more details.
15 :
16 : You should have received a copy of the GNU Lesser General Public License
17 : along with plumed. If not, see <http://www.gnu.org/licenses/>.
18 : +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
19 : #include "ExpansionCVs.h"
20 :
21 : #include "tools/OpenMP.h"
22 : #include "core/PlumedMain.h"
23 : #include "core/Atoms.h"
24 :
25 : namespace PLMD {
26 : namespace opes {
27 :
28 61 : void ExpansionCVs::registerKeywords(Keywords& keys) {
29 61 : Action::registerKeywords(keys);
30 61 : ActionWithValue::registerKeywords(keys);
31 61 : ActionWithArguments::registerKeywords(keys);
32 61 : ActionWithValue::useCustomisableComponents(keys);
33 122 : keys.add("compulsory","TEMP","-1","temperature. If not specified tries to get it from MD engine");
34 61 : }
35 :
36 37 : ExpansionCVs::ExpansionCVs(const ActionOptions&ao)
37 : : Action(ao)
38 : , ActionWithValue(ao)
39 : , ActionWithArguments(ao)
40 37 : , isReady_(false)
41 37 : , totNumECVs_(0) {
42 : //set kbt_
43 37 : const double kB=plumed.getAtoms().getKBoltzmann();
44 37 : kbt_=plumed.getAtoms().getKbT();
45 37 : double temp=-1;
46 37 : parse("TEMP",temp);
47 37 : if(temp>0) {
48 37 : if(kbt_>0 && std::abs(kbt_-kB*temp)>1e-4) {
49 0 : log.printf(" +++ WARNING +++ using TEMP=%g while MD engine uses %g\n",temp,kbt_/kB);
50 : }
51 37 : kbt_=kB*temp;
52 : }
53 37 : plumed_massert(kbt_>0,"your MD engine does not pass the temperature to plumed, you must specify it using TEMP");
54 37 : log.printf(" temperature = %g, beta = %g\n",kbt_/kB,1./kbt_);
55 :
56 : //set components
57 37 : plumed_massert( getNumberOfArguments()!=0, "you must specify the underlying CV");
58 90 : for(unsigned j=0; j<getNumberOfArguments(); j++) {
59 53 : std::string name_j=getPntrToArgument(j)->getName();
60 53 : ActionWithValue::addComponentWithDerivatives(name_j);
61 53 : getPntrToComponent(j)->resizeDerivatives(1);
62 53 : if(getPntrToArgument(j)->isPeriodic()) { //it should not be necessary, but why not
63 : std::string min,max;
64 17 : getPntrToArgument(j)->getDomain(min,max);
65 17 : getPntrToComponent(j)->setDomain(min,max);
66 : } else {
67 36 : getPntrToComponent(j)->setNotPeriodic();
68 : }
69 : }
70 37 : plumed_massert((int)getNumberOfArguments()==getNumberOfComponents(),"Expansion CVs have same number of arguments and components");
71 37 : }
72 :
73 1847 : void ExpansionCVs::calculate() {
74 1847 : std::vector<double> args(getNumberOfArguments());
75 4470 : for(unsigned j=0; j<getNumberOfArguments(); j++) {
76 2623 : args[j]=getArgument(j);
77 2623 : getPntrToComponent(j)->set(args[j]); //components are equal to arguments
78 2623 : getPntrToComponent(j)->addDerivative(0,1.); //the derivative of the identity is 1
79 : }
80 1847 : if(isReady_) {
81 1417 : calculateECVs(&args[0]);
82 : }
83 1847 : }
84 :
85 1847 : void ExpansionCVs::apply() {
86 4470 : for(unsigned j=0; j<getNumberOfArguments(); j++) {
87 2623 : std::vector<double> force_j(1);
88 2623 : if(getPntrToComponent(j)->applyForce(force_j)) { //a bias is applied?
89 2623 : getPntrToArgument(j)->addForce(force_j[0]); //just tell it to the CV!
90 : }
91 : }
92 1847 : }
93 :
94 26 : std::vector< std::vector<unsigned> > ExpansionCVs::getIndex_k() const {
95 26 : plumed_massert(isReady_ && totNumECVs_>0,"cannot access getIndex_k() of ECV before initialization");
96 26 : std::vector< std::vector<unsigned> > index_k(totNumECVs_,std::vector<unsigned>(getNumberOfArguments()));
97 869 : for(unsigned k=0; k<totNumECVs_; k++)
98 2391 : for(unsigned j=0; j<getNumberOfArguments(); j++) {
99 1548 : index_k[k][j]=k; //each CV gives rise to the same number of ECVs
100 : }
101 26 : return index_k;
102 0 : }
103 :
104 : //following methods are meant to be used only in case of linear expansions
105 24 : std::vector<double> ExpansionCVs::getSteps(double lambda_min,double lambda_max,const unsigned lambda_steps,const std::string& msg,const bool geom_spacing, const double shift) const {
106 24 : plumed_massert(!(lambda_min==lambda_max && lambda_steps>1),"cannot have multiple "+msg+"_STEPS if "+msg+"_MIN=="+msg+"_MAX");
107 24 : std::vector<double> lambda(lambda_steps);
108 24 : if(lambda_steps==1) {
109 0 : lambda[0]=(lambda_min+lambda_max)/2.;
110 0 : log.printf(" +++ WARNING +++ using one single %s as target = %g\n",msg.c_str(),lambda[0]);
111 : } else {
112 24 : if(geom_spacing) { //geometric spacing
113 : //this way lambda[k]/lambda[k+1] is constant
114 14 : lambda_min+=shift;
115 14 : lambda_max+=shift;
116 14 : plumed_massert(lambda_min>0,"cannot use GEOM_SPACING when %s_MIN is not greater than zero");
117 14 : plumed_massert(lambda_max>0,"cannot use GEOM_SPACING when %s_MAX is not greater than zero");
118 14 : const double log_lambda_min=std::log(lambda_min);
119 14 : const double log_lambda_max=std::log(lambda_max);
120 196 : for(unsigned k=0; k<lambda.size(); k++) {
121 182 : lambda[k]=std::exp(log_lambda_min+k*(log_lambda_max-log_lambda_min)/(lambda_steps-1))-shift;
122 : }
123 : } else //linear spacing
124 108 : for(unsigned k=0; k<lambda.size(); k++) {
125 98 : lambda[k]=lambda_min+k*(lambda_max-lambda_min)/(lambda_steps-1);
126 : }
127 : }
128 24 : return lambda;
129 : }
130 :
131 6 : unsigned ExpansionCVs::estimateNumSteps(const double left_side,const double right_side,const std::vector<double>& obs,const std::string& msg) const {
132 : //for linear expansions only, it uses effective sample size (Neff) to estimate the grid spacing
133 6 : if(left_side==0 && right_side==0) {
134 0 : log.printf(" +++ WARNING +++ %s_MIN and %s_MAX are equal to %s, using single step\n",msg.c_str(),msg.c_str(),msg.c_str());
135 0 : return 1;
136 : }
137 9 : auto get_neff_HWHM=[](const double side,const std::vector<double>& obs) { //HWHM = half width at half maximum. neff is in general not symmetric
138 : //func: Neff/N-0.5 is a function between -0.5 and 0.5
139 109 : auto func=[](const double delta,const std::vector<double>& obs) {
140 : double sum_w=0;
141 : double sum_w2=0;
142 : //we could avoid recomputing safe_shift every time, but here speed is not a concern
143 218 : const double safe_shift=delta<0?*std::max_element(obs.begin(),obs.end()):*std::min_element(obs.begin(),obs.end());
144 899 : for(unsigned t=0; t<obs.size(); t++) {
145 790 : const double w=std::exp(-delta*(obs[t]-safe_shift)); //robust to overflow
146 790 : sum_w+=w;
147 790 : sum_w2+=w*w;
148 : }
149 109 : return sum_w*sum_w/sum_w2/obs.size()-0.5;
150 : };
151 : //here we find the root of func using the regula falsi (false position) method
152 : //but any method would be OK, not much precision is needed. src/tools/RootFindingBase.h looked complicated
153 : const double tolerance=1e-4; //seems to be a good default
154 : double a=0; //default is right side case
155 : double func_a=0.5;
156 : double b=side;
157 9 : double func_b=func(side,obs);
158 9 : if(func_b>=0) {
159 : return 0.0; //no zero is present!
160 : }
161 9 : if(b<0) { //left side case
162 : std::swap(a,b);
163 : std::swap(func_a,func_b);
164 : }
165 : double c=a;
166 : double func_c=func_a;
167 109 : while(std::abs(func_c)>tolerance) {
168 100 : if(func_a*func_c>0) {
169 : a=c;
170 : func_a=func_c;
171 : } else {
172 : b=c;
173 : func_b=func_c;
174 : }
175 100 : c=(a*func_b-b*func_a)/(func_b-func_a);
176 100 : func_c=func(c,obs); //func is evaluated only here, it might be expensive
177 : }
178 9 : return std::abs(c);
179 : };
180 :
181 : //estimation
182 : double left_HWHM=0;
183 6 : if(left_side!=0) {
184 4 : left_HWHM=get_neff_HWHM(left_side,obs);
185 : }
186 : double right_HWHM=0;
187 6 : if(right_side!=0) {
188 5 : right_HWHM=get_neff_HWHM(right_side,obs);
189 : }
190 6 : if(left_HWHM==0) {
191 2 : right_HWHM*=2;
192 2 : if(left_side==0) {
193 2 : log.printf(" --- %s_MIN is equal to %s\n",msg.c_str(),msg.c_str());
194 : } else {
195 0 : log.printf(" +++ WARNING +++ %s_MIN is very close to %s\n",msg.c_str(),msg.c_str());
196 : }
197 : }
198 6 : if(right_HWHM==0) {
199 1 : left_HWHM*=2;
200 1 : if(right_side==0) {
201 1 : log.printf(" --- %s_MAX is equal to %s\n",msg.c_str(),msg.c_str());
202 : } else {
203 0 : log.printf(" +++ WARNING +++ %s_MAX is very close to %s\n",msg.c_str(),msg.c_str());
204 : }
205 : }
206 6 : const double grid_spacing=left_HWHM+right_HWHM;
207 6 : log.printf(" estimated %s spacing = %g\n",msg.c_str(),grid_spacing);
208 6 : unsigned steps=std::ceil(std::abs(right_side-left_side)/grid_spacing);
209 6 : if(steps<2 || grid_spacing==0) {
210 0 : log.printf(" +++ WARNING +++ %s range is very narrow, using %s_MIN and %s_MAX as only steps\n",msg.c_str(),msg.c_str(),msg.c_str());
211 : steps=2;
212 : }
213 : return steps;
214 : }
215 :
216 : }
217 : }
|