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 :
23 : namespace PLMD {
24 : namespace opes {
25 :
26 49 : void ExpansionCVs::registerKeywords(Keywords& keys) {
27 49 : Action::registerKeywords(keys);
28 49 : ActionWithValue::registerKeywords(keys);
29 49 : ActionWithArguments::registerKeywords(keys);
30 49 : ActionWithValue::useCustomisableComponents(keys);
31 98 : keys.add("compulsory","TEMP","-1","temperature. If not specified tries to get it from MD engine");
32 49 : }
33 :
34 37 : ExpansionCVs::ExpansionCVs(const ActionOptions&ao)
35 : : Action(ao)
36 : , ActionWithValue(ao)
37 : , ActionWithArguments(ao)
38 37 : , isReady_(false)
39 37 : , totNumECVs_(0) {
40 : //set kbt_
41 37 : const double kB=getKBoltzmann();
42 37 : kbt_=getkBT();
43 37 : log.printf(" temperature = %g, beta = %g\n",kbt_/kB,1./kbt_);
44 :
45 : //set components
46 37 : plumed_massert( getNumberOfArguments()!=0, "you must specify the underlying CV");
47 90 : for(unsigned j=0; j<getNumberOfArguments(); j++) {
48 53 : std::string name_j=getPntrToArgument(j)->getName();
49 53 : ActionWithValue::addComponentWithDerivatives(name_j);
50 53 : getPntrToComponent(j)->resizeDerivatives(1);
51 53 : if(getPntrToArgument(j)->isPeriodic()) { //it should not be necessary, but why not
52 : std::string min,max;
53 17 : getPntrToArgument(j)->getDomain(min,max);
54 17 : getPntrToComponent(j)->setDomain(min,max);
55 : } else {
56 36 : getPntrToComponent(j)->setNotPeriodic();
57 : }
58 : }
59 37 : plumed_massert((int)getNumberOfArguments()==getNumberOfComponents(),"Expansion CVs have same number of arguments and components");
60 37 : }
61 :
62 0 : std::string ExpansionCVs::getOutputComponentDescription( const std::string& cname, const Keywords& keys ) const {
63 0 : return "the value of the argument named " + cname;
64 : }
65 :
66 1847 : void ExpansionCVs::calculate() {
67 1847 : std::vector<double> args(getNumberOfArguments());
68 4470 : for(unsigned j=0; j<getNumberOfArguments(); j++) {
69 2623 : args[j]=getArgument(j);
70 2623 : getPntrToComponent(j)->set(args[j]); //components are equal to arguments
71 2623 : getPntrToComponent(j)->addDerivative(0,1.); //the derivative of the identity is 1
72 : }
73 1847 : if(isReady_) {
74 1417 : calculateECVs(&args[0]);
75 : }
76 1847 : }
77 :
78 1847 : void ExpansionCVs::apply() {
79 4470 : for(unsigned j=0; j<getNumberOfArguments(); j++) {
80 2623 : std::vector<double> force_j(1);
81 2623 : if(getPntrToComponent(j)->applyForce(force_j)) { //a bias is applied?
82 2623 : getPntrToArgument(j)->addForce(force_j[0]); //just tell it to the CV!
83 : }
84 : }
85 1847 : }
86 :
87 26 : std::vector< std::vector<unsigned> > ExpansionCVs::getIndex_k() const {
88 26 : plumed_massert(isReady_ && totNumECVs_>0,"cannot access getIndex_k() of ECV before initialization");
89 26 : std::vector< std::vector<unsigned> > index_k(totNumECVs_,std::vector<unsigned>(getNumberOfArguments()));
90 869 : for(unsigned k=0; k<totNumECVs_; k++)
91 2391 : for(unsigned j=0; j<getNumberOfArguments(); j++) {
92 1548 : index_k[k][j]=k; //each CV gives rise to the same number of ECVs
93 : }
94 26 : return index_k;
95 0 : }
96 :
97 : //following methods are meant to be used only in case of linear expansions
98 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 {
99 24 : plumed_massert(!(lambda_min==lambda_max && lambda_steps>1),"cannot have multiple "+msg+"_STEPS if "+msg+"_MIN=="+msg+"_MAX");
100 24 : std::vector<double> lambda(lambda_steps);
101 24 : if(lambda_steps==1) {
102 0 : lambda[0]=(lambda_min+lambda_max)/2.;
103 0 : log.printf(" +++ WARNING +++ using one single %s as target = %g\n",msg.c_str(),lambda[0]);
104 : } else {
105 24 : if(geom_spacing) { //geometric spacing
106 : //this way lambda[k]/lambda[k+1] is constant
107 14 : lambda_min+=shift;
108 14 : lambda_max+=shift;
109 14 : plumed_massert(lambda_min>0,"cannot use GEOM_SPACING when %s_MIN is not greater than zero");
110 14 : plumed_massert(lambda_max>0,"cannot use GEOM_SPACING when %s_MAX is not greater than zero");
111 14 : const double log_lambda_min=std::log(lambda_min);
112 14 : const double log_lambda_max=std::log(lambda_max);
113 196 : for(unsigned k=0; k<lambda.size(); k++) {
114 182 : lambda[k]=std::exp(log_lambda_min+k*(log_lambda_max-log_lambda_min)/(lambda_steps-1))-shift;
115 : }
116 : } else //linear spacing
117 108 : for(unsigned k=0; k<lambda.size(); k++) {
118 98 : lambda[k]=lambda_min+k*(lambda_max-lambda_min)/(lambda_steps-1);
119 : }
120 : }
121 24 : return lambda;
122 : }
123 :
124 6 : unsigned ExpansionCVs::estimateNumSteps(const double left_side,const double right_side,const std::vector<double>& obs,const std::string& msg) const {
125 : //for linear expansions only, it uses effective sample size (Neff) to estimate the grid spacing
126 6 : if(left_side==0 && right_side==0) {
127 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());
128 0 : return 1;
129 : }
130 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
131 : //func: Neff/N-0.5 is a function between -0.5 and 0.5
132 109 : auto func=[](const double delta,const std::vector<double>& obs) {
133 : double sum_w=0;
134 : double sum_w2=0;
135 : //we could avoid recomputing safe_shift every time, but here speed is not a concern
136 218 : const double safe_shift=delta<0?*std::max_element(obs.begin(),obs.end()):*std::min_element(obs.begin(),obs.end());
137 899 : for(unsigned t=0; t<obs.size(); t++) {
138 790 : const double w=std::exp(-delta*(obs[t]-safe_shift)); //robust to overflow
139 790 : sum_w+=w;
140 790 : sum_w2+=w*w;
141 : }
142 109 : return sum_w*sum_w/sum_w2/obs.size()-0.5;
143 : };
144 : //here we find the root of func using the regula falsi (false position) method
145 : //but any method would be OK, not much precision is needed. src/tools/RootFindingBase.h looked complicated
146 : const double tolerance=1e-4; //seems to be a good default
147 : double a=0; //default is right side case
148 : double func_a=0.5;
149 : double b=side;
150 9 : double func_b=func(side,obs);
151 9 : if(func_b>=0) {
152 : return 0.0; //no zero is present!
153 : }
154 9 : if(b<0) { //left side case
155 : std::swap(a,b);
156 : std::swap(func_a,func_b);
157 : }
158 : double c=a;
159 : double func_c=func_a;
160 109 : while(std::abs(func_c)>tolerance) {
161 100 : if(func_a*func_c>0) {
162 : a=c;
163 : func_a=func_c;
164 : } else {
165 : b=c;
166 : func_b=func_c;
167 : }
168 100 : c=(a*func_b-b*func_a)/(func_b-func_a);
169 100 : func_c=func(c,obs); //func is evaluated only here, it might be expensive
170 : }
171 9 : return std::abs(c);
172 : };
173 :
174 : //estimation
175 : double left_HWHM=0;
176 6 : if(left_side!=0) {
177 4 : left_HWHM=get_neff_HWHM(left_side,obs);
178 : }
179 : double right_HWHM=0;
180 6 : if(right_side!=0) {
181 5 : right_HWHM=get_neff_HWHM(right_side,obs);
182 : }
183 6 : if(left_HWHM==0) {
184 2 : right_HWHM*=2;
185 2 : if(left_side==0) {
186 2 : log.printf(" --- %s_MIN is equal to %s\n",msg.c_str(),msg.c_str());
187 : } else {
188 0 : log.printf(" +++ WARNING +++ %s_MIN is very close to %s\n",msg.c_str(),msg.c_str());
189 : }
190 : }
191 6 : if(right_HWHM==0) {
192 1 : left_HWHM*=2;
193 1 : if(right_side==0) {
194 1 : log.printf(" --- %s_MAX is equal to %s\n",msg.c_str(),msg.c_str());
195 : } else {
196 0 : log.printf(" +++ WARNING +++ %s_MAX is very close to %s\n",msg.c_str(),msg.c_str());
197 : }
198 : }
199 6 : const double grid_spacing=left_HWHM+right_HWHM;
200 6 : log.printf(" estimated %s spacing = %g\n",msg.c_str(),grid_spacing);
201 6 : unsigned steps=std::ceil(std::abs(right_side-left_side)/grid_spacing);
202 6 : if(steps<2 || grid_spacing==0) {
203 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());
204 : steps=2;
205 : }
206 : return steps;
207 : }
208 :
209 : }
210 : }
|