Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 : Copyright (c) 2016-2021 The VES code team 3 : (see the PEOPLE-VES file at the root of this folder for a list of names) 4 : 5 : See http://www.ves-code.org for more information. 6 : 7 : This file is part of VES code module. 8 : 9 : The VES code module 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 : The VES code module 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 the VES code module. If not, see <http://www.gnu.org/licenses/>. 21 : +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */ 22 : 23 : #include "TargetDistribution.h" 24 : 25 : #include "core/ActionRegister.h" 26 : 27 : 28 : namespace PLMD { 29 : namespace ves { 30 : 31 : //+PLUMEDOC VES_TARGETDIST TD_CHI 32 : /* 33 : Chi distribution (static). 34 : 35 : Employ a target distribution given by a 36 : [chi distribution](https://en.wikipedia.org/wiki/Chi_distribution) 37 : that is defined as 38 : \f[ 39 : p(s) = 40 : \frac 41 : {2^{1-\frac{k}{2}}} 42 : {\sigma \, \Gamma\left(\frac{k}{2}\right) } 43 : \, \left(\frac{s-a}{\sigma}\right)^{k-1} \, \exp\left(- \frac{1}{2} \left(\frac{s-a}{\sigma}\right)^2\right), 44 : \f] 45 : where \f$a\f$ is the minimum of the distribution that is defined on the interval \f$[a,\infty)\f$, 46 : the parameter \f$k\f$ (given as a positive integer larger than 1) determines how far 47 : the peak of the distribution is from the minimum (known as the "degrees of freedom"), 48 : and the parameter \f$\sigma>0\f$ determines the broadness of the distribution. 49 : 50 : The minimum \f$a\f$ is given using the MINIMUM keyword, the parameter \f$k\f$ is given 51 : using the KAPPA keyword, and the parameter \f$\sigma\f$ is given using the SIGMA keyword. 52 : 53 : This target distribution action is only defined for one dimension, for multiple dimensions 54 : it should be used in combination with the \ref TD_PRODUCT_DISTRIBUTION action. 55 : 56 : 57 : \par Examples 58 : 59 : Chi distribution with \f$a=10.0\f$, \f$\sigma=2.0\f$, and \f$k=2\f$ 60 : \plumedfile 61 : td: TD_CHI MINIMUM=10.0 SIGMA=2.0 KAPPA=2 62 : \endplumedfile 63 : 64 : The Chi distribution is only defined for one dimension so for multiple 65 : dimensions we have to use it in combination with the \ref TD_PRODUCT_DISTRIBUTION action as shown in 66 : the following example where we have a uniform distribution for argument 1 and 67 : a Chi distribution for argument 1 68 : \plumedfile 69 : td_uni: TD_UNIFORM 70 : 71 : td_chi: TD_CHI MINIMUM=-10.0 SIGMA=2.0 KAPPA=2 72 : 73 : td_pd: TD_PRODUCT_DISTRIBUTION DISTRIBUTIONS=td_uni,td_chi 74 : \endplumedfile 75 : 76 : */ 77 : //+ENDPLUMEDOC 78 : 79 : class TD_Chi: public TargetDistribution { 80 : std::vector<double> minima_; 81 : std::vector<double> sigma_; 82 : std::vector<double> kappa_; 83 : std::vector<double> normalization_; 84 : public: 85 : static void registerKeywords(Keywords&); 86 : explicit TD_Chi(const ActionOptions& ao); 87 : double getValue(const std::vector<double>&) const override; 88 : }; 89 : 90 : 91 10428 : PLUMED_REGISTER_ACTION(TD_Chi,"TD_CHI") 92 : 93 : 94 10 : void TD_Chi::registerKeywords(Keywords& keys) { 95 10 : TargetDistribution::registerKeywords(keys); 96 20 : keys.add("compulsory","MINIMUM","The minimum of the chi distribution."); 97 20 : keys.add("compulsory","SIGMA","The \\f$\\sigma\\f$ parameter of the chi distribution given as a positive number."); 98 20 : keys.add("compulsory","KAPPA","The \\f$k\\f$ parameter of the chi distribution given as positive integer larger than 1."); 99 10 : keys.use("WELLTEMPERED_FACTOR"); 100 10 : keys.use("SHIFT_TO_ZERO"); 101 10 : keys.use("NORMALIZE"); 102 10 : } 103 : 104 : 105 9 : TD_Chi::TD_Chi(const ActionOptions& ao): 106 : PLUMED_VES_TARGETDISTRIBUTION_INIT(ao), 107 18 : minima_(0), 108 9 : sigma_(0), 109 9 : kappa_(0), 110 18 : normalization_(0) 111 : { 112 9 : parseVector("MINIMUM",minima_); 113 9 : parseVector("SIGMA",sigma_); 114 18 : for(unsigned int k=0; k<sigma_.size(); k++) { 115 9 : if(sigma_[k] < 0.0) {plumed_merror(getName()+": the value given in SIGMA should be positive.");} 116 : } 117 : 118 : 119 9 : std::vector<unsigned int> kappa_int(0); 120 18 : parseVector("KAPPA",kappa_int); 121 9 : if(kappa_int.size()==0) {plumed_merror(getName()+": some problem with KAPPA keyword, should given as positive integer larger than 1");} 122 9 : kappa_.resize(kappa_int.size()); 123 18 : for(unsigned int k=0; k<kappa_int.size(); k++) { 124 9 : if(kappa_int[k] < 1) {plumed_merror(getName()+": KAPPA should be an integer 1 or higher");} 125 9 : kappa_[k] = static_cast<double>(kappa_int[k]); 126 : } 127 : 128 9 : setDimension(minima_.size()); 129 9 : if(getDimension()>1) {plumed_merror(getName()+": only defined for one dimension, for multiple dimensions it should be used in combination with the TD_PRODUCT_DISTRIBUTION action.");} 130 9 : if(sigma_.size()!=getDimension()) {plumed_merror(getName()+": the SIGMA keyword does not match the given dimension in MINIMUM");} 131 9 : if(kappa_.size()!=getDimension()) {plumed_merror(getName()+": the KAPPA keyword does not match the given dimension in MINIMUM");} 132 : 133 9 : normalization_.resize(getDimension()); 134 18 : for(unsigned int k=0; k<getDimension(); k++) { 135 9 : normalization_[k] = pow(2.0,(1.0-0.5*kappa_[k]))/(tgamma(0.5*kappa_[k])*sigma_[k]); 136 : } 137 9 : checkRead(); 138 9 : } 139 : 140 : 141 1509 : double TD_Chi::getValue(const std::vector<double>& argument) const { 142 : double value = 1.0; 143 3018 : for(unsigned int k=0; k<argument.size(); k++) { 144 1509 : double arg=(argument[k]-minima_[k])/sigma_[k]; 145 1509 : if(arg<0.0) {plumed_merror(getName()+": the chi distribution is not defined for values less that ones given in MINIMUM");} 146 1509 : value *= normalization_[k] * pow(arg,kappa_[k]-1.0) * exp(-0.5*arg*arg); 147 : } 148 1509 : return value; 149 : } 150 : 151 : 152 : } 153 : }