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 : 39 : $$ 40 : p(s) = 41 : \frac 42 : {2^{1-\frac{k}{2}}} 43 : {\sigma \, \Gamma\left(\frac{k}{2}\right) } 44 : \, \left(\frac{s-a}{\sigma}\right)^{k-1} \, \exp\left(- \frac{1}{2} \left(\frac{s-a}{\sigma}\right)^2\right), 45 : $$ 46 : 47 : where $a$ is the minimum of the distribution that is defined on the interval $[a,\infty)$, 48 : the parameter $k$ (given as a positive integer larger than 1) determines how far 49 : the peak of the distribution is from the minimum (known as the "degrees of freedom"), 50 : and the parameter $\sigma>0$ determines the broadness of the distribution. 51 : 52 : The minimum $a$ is given using the MINIMUM keyword, the parameter $k$ is given 53 : using the KAPPA keyword, and the parameter $\sigma$ is given using the SIGMA keyword. 54 : 55 : This target distribution action is only defined for one dimension, for multiple dimensions 56 : it should be used in combination with the [TD_PRODUCT_DISTRIBUTION](TD_PRODUCT_DISTRIBUTION.md) action. 57 : 58 : 59 : ## Examples 60 : 61 : Chi distribution with $a=10.0$, $\sigma=2.0$, and $k=2$ 62 : 63 : ```plumed 64 : td: TD_CHI MINIMUM=10.0 SIGMA=2.0 KAPPA=2 65 : ``` 66 : 67 : The Chi distribution is only defined for one dimension so for multiple 68 : dimensions we have to use it in combination with the [TD_PRODUCT_DISTRIBUTION](TD_PRODUCT_DISTRIBUTION.md) action as shown in 69 : the following example where we have a uniform distribution for argument 1 and 70 : a Chi distribution for argument 1 71 : 72 : ```plumed 73 : td_uni: TD_UNIFORM 74 : 75 : td_chi: TD_CHI MINIMUM=-10.0 SIGMA=2.0 KAPPA=2 76 : 77 : td_pd: TD_PRODUCT_DISTRIBUTION DISTRIBUTIONS=td_uni,td_chi 78 : ``` 79 : 80 : */ 81 : //+ENDPLUMEDOC 82 : 83 : class TD_Chi: public TargetDistribution { 84 : std::vector<double> minima_; 85 : std::vector<double> sigma_; 86 : std::vector<double> kappa_; 87 : std::vector<double> normalization_; 88 : public: 89 : static void registerKeywords(Keywords&); 90 : explicit TD_Chi(const ActionOptions& ao); 91 : double getValue(const std::vector<double>&) const override; 92 : }; 93 : 94 : 95 : PLUMED_REGISTER_ACTION(TD_Chi,"TD_CHI") 96 : 97 : 98 11 : void TD_Chi::registerKeywords(Keywords& keys) { 99 11 : TargetDistribution::registerKeywords(keys); 100 11 : keys.add("compulsory","MINIMUM","The minimum of the chi distribution."); 101 11 : keys.add("compulsory","SIGMA","The sigma parameter of the chi distribution given as a positive number."); 102 11 : keys.add("compulsory","KAPPA","The k parameter of the chi distribution given as positive integer larger than 1."); 103 11 : keys.use("WELLTEMPERED_FACTOR"); 104 11 : keys.use("SHIFT_TO_ZERO"); 105 11 : keys.use("NORMALIZE"); 106 11 : } 107 : 108 : 109 9 : TD_Chi::TD_Chi(const ActionOptions& ao): 110 : PLUMED_VES_TARGETDISTRIBUTION_INIT(ao), 111 18 : minima_(0), 112 9 : sigma_(0), 113 9 : kappa_(0), 114 18 : normalization_(0) { 115 9 : parseVector("MINIMUM",minima_); 116 9 : parseVector("SIGMA",sigma_); 117 18 : for(unsigned int k=0; k<sigma_.size(); k++) { 118 9 : if(sigma_[k] < 0.0) { 119 0 : plumed_merror(getName()+": the value given in SIGMA should be positive."); 120 : } 121 : } 122 : 123 : 124 9 : std::vector<unsigned int> kappa_int(0); 125 18 : parseVector("KAPPA",kappa_int); 126 9 : if(kappa_int.size()==0) { 127 0 : plumed_merror(getName()+": some problem with KAPPA keyword, should given as positive integer larger than 1"); 128 : } 129 9 : kappa_.resize(kappa_int.size()); 130 18 : for(unsigned int k=0; k<kappa_int.size(); k++) { 131 9 : if(kappa_int[k] < 1) { 132 0 : plumed_merror(getName()+": KAPPA should be an integer 1 or higher"); 133 : } 134 9 : kappa_[k] = static_cast<double>(kappa_int[k]); 135 : } 136 : 137 9 : setDimension(minima_.size()); 138 9 : if(getDimension()>1) { 139 0 : plumed_merror(getName()+": only defined for one dimension, for multiple dimensions it should be used in combination with the TD_PRODUCT_DISTRIBUTION action."); 140 : } 141 9 : if(sigma_.size()!=getDimension()) { 142 0 : plumed_merror(getName()+": the SIGMA keyword does not match the given dimension in MINIMUM"); 143 : } 144 9 : if(kappa_.size()!=getDimension()) { 145 0 : plumed_merror(getName()+": the KAPPA keyword does not match the given dimension in MINIMUM"); 146 : } 147 : 148 9 : normalization_.resize(getDimension()); 149 18 : for(unsigned int k=0; k<getDimension(); k++) { 150 9 : normalization_[k] = pow(2.0,(1.0-0.5*kappa_[k]))/(tgamma(0.5*kappa_[k])*sigma_[k]); 151 : } 152 9 : checkRead(); 153 9 : } 154 : 155 : 156 1509 : double TD_Chi::getValue(const std::vector<double>& argument) const { 157 : double value = 1.0; 158 3018 : for(unsigned int k=0; k<argument.size(); k++) { 159 1509 : double arg=(argument[k]-minima_[k])/sigma_[k]; 160 1509 : if(arg<0.0) { 161 0 : plumed_merror(getName()+": the chi distribution is not defined for values less that ones given in MINIMUM"); 162 : } 163 1509 : value *= normalization_[k] * pow(arg,kappa_[k]-1.0) * exp(-0.5*arg*arg); 164 : } 165 1509 : return value; 166 : } 167 : 168 : 169 : } 170 : }