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 "BasisFunctions.h"
24 : #include "VesTools.h"
25 :
26 : #include "core/ActionRegister.h"
27 : #include "core/ActionSet.h"
28 : #include "core/PlumedMain.h"
29 :
30 :
31 : namespace PLMD {
32 : namespace ves {
33 :
34 : //+PLUMEDOC VES_BASISF BF_COMBINED
35 : /*
36 : Combining other basis functions types
37 :
38 : \par Examples
39 :
40 : Here we define both Fourier cosine and sine expansions of order 10,
41 : each with 11 basis functions, which are combined. This results
42 : in a total number of 21 basis functions as only the constant from
43 : is bf_cos is used.
44 : \plumedfile
45 : bf_cos: BF_COSINE MINIMUM=-pi MAXIMUM=+pi ORDER=10
46 : bf_sin: BF_SINE MINIMUM=-pi MAXIMUM=+pi ORDER=10
47 : bf_comb: BF_COMBINED BASIS_FUNCTIONS=bf_cos,bf_sin
48 : \endplumedfile
49 : In principle this is the same as using BF_FOURIER with
50 : ORDER=10 but with different ordering of the basis functions.
51 : Note that the order used in BASIS_FUNCTIONS matters for the ordering
52 : of the basis functions, using BASIS_FUNCTIONS=bf_sin,bf_cos would
53 : results in a different order of the basis functions.
54 : This should be kept in mind when restarting from previous
55 : coefficients.
56 :
57 :
58 :
59 :
60 :
61 :
62 : */
63 : //+ENDPLUMEDOC
64 :
65 : class BF_Combined : public BasisFunctions {
66 : std::vector<BasisFunctions*> basisf_pntrs_;
67 : void setupLabels() override;
68 : void setupUniformIntegrals() override;
69 : // void getBFandValueIndices(const unsigned int, unsigned int&, unsigned int&) const;
70 : public:
71 : static void registerKeywords(Keywords&);
72 : explicit BF_Combined(const ActionOptions&);
73 : void getAllValues(const double, double&, bool&, std::vector<double>&, std::vector<double>&) const override;
74 : };
75 :
76 :
77 : PLUMED_REGISTER_ACTION(BF_Combined,"BF_COMBINED")
78 :
79 :
80 4 : void BF_Combined::registerKeywords(Keywords& keys) {
81 4 : BasisFunctions::registerKeywords(keys);
82 4 : keys.remove("ORDER");
83 4 : keys.remove("MAXIMUM");
84 4 : keys.remove("MINIMUM");
85 4 : keys.remove("NUMERICAL_INTEGRALS");
86 8 : keys.remove("NGRID_POINTS");
87 4 : keys.add("compulsory","BASIS_FUNCTIONS","Labels of the basis functions that should be combined. Note that the order used matters for the ordering of the basis functions. This needs to be kept in mind when restarting from previous coefficients.");
88 4 : }
89 :
90 :
91 2 : BF_Combined::BF_Combined(const ActionOptions&ao):
92 : PLUMED_VES_BASISFUNCTIONS_INIT(ao),
93 2 : basisf_pntrs_(0) {
94 : std::vector<std::string> basisf_labels;
95 2 : parseVector("BASIS_FUNCTIONS",basisf_labels);
96 4 : addKeywordToList("BASIS_FUNCTIONS",basisf_labels);
97 2 : if(basisf_labels.size()==1) {
98 0 : plumed_merror("using only one basis function in BF_COMBINED does not make sense");
99 : }
100 2 : std::string error_msg = "";
101 4 : basisf_pntrs_ = VesTools::getPointersFromLabels<BasisFunctions*>(basisf_labels,plumed.getActionSet(),error_msg);
102 2 : if(error_msg.size()>0) {
103 0 : plumed_merror("Error in keyword BASIS_FUNCTIONS of "+getName()+": "+error_msg);
104 : }
105 :
106 : unsigned int nbasisf_total_ = 1;
107 : bool periodic = true;
108 7 : for(unsigned int i=0; i<basisf_pntrs_.size(); i++) {
109 5 : nbasisf_total_ += basisf_pntrs_[i]->getNumberOfBasisFunctions() - 1;
110 15 : if(basisf_pntrs_[i]->intervalMinStr()!=basisf_pntrs_[0]->intervalMinStr() || basisf_pntrs_[i]->intervalMaxStr()!=basisf_pntrs_[0]->intervalMaxStr()) {
111 0 : plumed_merror("all the basis functions to be combined should have same MINIMUM and MAXIMUM");
112 : }
113 5 : if(!basisf_pntrs_[i]->arePeriodic()) {
114 : periodic=false;
115 : }
116 : }
117 2 : setOrder(nbasisf_total_-1);
118 2 : setNumberOfBasisFunctions(nbasisf_total_);
119 4 : setInterval(basisf_pntrs_[0]->intervalMinStr(),basisf_pntrs_[0]->intervalMaxStr());
120 4 : setIntrinsicInterval("-1.0","+1.0");
121 2 : if(periodic) {
122 : setPeriodic();
123 : } else {
124 : setNonPeriodic();
125 : }
126 : setIntervalBounded();
127 2 : setType("combined");
128 2 : setDescription("Combined");
129 2 : setupBF();
130 2 : checkRead();
131 2 : }
132 :
133 :
134 : // void BF_Combined::getBFandValueIndices(const unsigned int n, unsigned int& bf_index, unsigned int& value_index) const {
135 : // bf_index = 0; value_index = 0;
136 : // if(n==0){
137 : // bf_index = 0;
138 : // value_index = 0;
139 : // return;
140 : // }
141 : // else{
142 : // unsigned int r=1;
143 : // for(unsigned int i=0; i<basisf_pntrs_.size(); i++){
144 : // for(unsigned int l=1; l<basisf_pntrs_[i]->numberOfBasisFunctions(); l++){
145 : // if(r==n){
146 : // bf_index = i;
147 : // value_index = l;
148 : // return;
149 : // }
150 : // r++;
151 : // }
152 : // }
153 : // }
154 : // }
155 :
156 :
157 3616 : void BF_Combined::getAllValues(const double arg, double& argT, bool& inside_range, std::vector<double>& values, std::vector<double>& derivs) const {
158 : // first BF, constant, argT, and inside_range taken from here
159 : unsigned int r=0;
160 3616 : std::vector<double> values_tmp(basisf_pntrs_[0]->numberOfBasisFunctions(),0.0);
161 3616 : std::vector<double> derivs_tmp(basisf_pntrs_[0]->numberOfBasisFunctions(),0.0);
162 3616 : basisf_pntrs_[0]->getAllValues(arg,argT,inside_range,values_tmp,derivs_tmp);
163 43392 : for(unsigned int l=0; l<basisf_pntrs_[0]->numberOfBasisFunctions(); l++) {
164 39776 : values[r] = values_tmp[l];
165 39776 : derivs[r] = derivs_tmp[l];
166 39776 : r++;
167 : }
168 : // other BF
169 9043 : for(unsigned int i=1; i<basisf_pntrs_.size(); i++) {
170 5427 : values_tmp.assign(basisf_pntrs_[i]->numberOfBasisFunctions(),0.0);
171 5427 : derivs_tmp.assign(basisf_pntrs_[i]->numberOfBasisFunctions(),0.0);
172 : double dummy_dbl;
173 5427 : bool dummy_bool=true;
174 5427 : basisf_pntrs_[i]->getAllValues(arg,dummy_dbl,dummy_bool,values_tmp,derivs_tmp);
175 59697 : for(unsigned int l=1; l<basisf_pntrs_[i]->numberOfBasisFunctions(); l++) {
176 54270 : values[r] = values_tmp[l];
177 54270 : derivs[r] = derivs_tmp[l];
178 54270 : r++;
179 : }
180 : }
181 3616 : }
182 :
183 :
184 2 : void BF_Combined::setupLabels() {
185 2 : setLabel(0,basisf_pntrs_[0]->getBasisFunctionLabel(0));
186 : unsigned int r=1;
187 7 : for(unsigned int i=0; i<basisf_pntrs_.size(); i++) {
188 55 : for(unsigned int l=1; l<basisf_pntrs_[i]->numberOfBasisFunctions(); l++) {
189 50 : setLabel(r,basisf_pntrs_[i]->getBasisFunctionLabel(l));
190 50 : r++;
191 : }
192 : }
193 2 : }
194 :
195 :
196 2 : void BF_Combined::setupUniformIntegrals() {
197 2 : setUniformIntegral(0,basisf_pntrs_[0]->getUniformIntegrals()[0]);
198 : unsigned int r=1;
199 7 : for(unsigned int i=0; i<basisf_pntrs_.size(); i++) {
200 5 : std::vector<double> uniform_tmp = basisf_pntrs_[i]->getUniformIntegrals();
201 55 : for(unsigned int l=1; l<basisf_pntrs_[i]->numberOfBasisFunctions(); l++) {
202 50 : setUniformIntegral(r,uniform_tmp[l]);
203 50 : r++;
204 : }
205 : }
206 2 : }
207 :
208 :
209 : }
210 : }
|