Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 : Copyright (c) 2016-2018 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 6 : class BF_Combined : public BasisFunctions {
66 : std::vector<BasisFunctions*> basisf_pntrs_;
67 : virtual void setupLabels();
68 : virtual void setupUniformIntegrals();
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;
74 : };
75 :
76 :
77 7836 : PLUMED_REGISTER_ACTION(BF_Combined,"BF_COMBINED")
78 :
79 :
80 3 : void BF_Combined::registerKeywords(Keywords& keys) {
81 3 : BasisFunctions::registerKeywords(keys);
82 6 : keys.remove("ORDER");
83 6 : keys.remove("MAXIMUM");
84 6 : keys.remove("MINIMUM");
85 6 : keys.remove("NUMERICAL_INTEGRALS");
86 6 : keys.remove("NGRID_POINTS");
87 12 : 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 3 : }
89 :
90 :
91 2 : BF_Combined::BF_Combined(const ActionOptions&ao):
92 : PLUMED_VES_BASISFUNCTIONS_INIT(ao),
93 2 : basisf_pntrs_(0)
94 : {
95 : std::vector<std::string> basisf_labels;
96 6 : parseVector("BASIS_FUNCTIONS",basisf_labels); 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 6 : basisf_pntrs_ = VesTools::getPointersFromLabels<BasisFunctions*>(basisf_labels,plumed.getActionSet(),error_msg);
102 2 : if(error_msg.size()>0) {plumed_merror("Error in keyword BASIS_FUNCTIONS of "+getName()+": "+error_msg);}
103 :
104 : unsigned int nbasisf_total_ = 1;
105 : bool periodic = true;
106 12 : for(unsigned int i=0; i<basisf_pntrs_.size(); i++) {
107 10 : nbasisf_total_ += basisf_pntrs_[i]->getNumberOfBasisFunctions() - 1;
108 50 : if(basisf_pntrs_[i]->intervalMinStr()!=basisf_pntrs_[0]->intervalMinStr() || basisf_pntrs_[i]->intervalMaxStr()!=basisf_pntrs_[0]->intervalMaxStr()) {
109 0 : plumed_merror("all the basis functions to be combined should have same MINIMUM and MAXIMUM");
110 : }
111 10 : if(!basisf_pntrs_[i]->arePeriodic()) {periodic=false;}
112 : }
113 2 : setOrder(nbasisf_total_-1);
114 2 : setNumberOfBasisFunctions(nbasisf_total_);
115 10 : setInterval(basisf_pntrs_[0]->intervalMinStr(),basisf_pntrs_[0]->intervalMaxStr());
116 6 : setIntrinsicInterval("-1.0","+1.0");
117 2 : if(periodic) {setPeriodic();}
118 : else {setNonPeriodic();}
119 : setIntervalBounded();
120 4 : setType("combined");
121 4 : setDescription("Combined");
122 2 : setupBF();
123 4 : checkRead();
124 2 : }
125 :
126 :
127 : // void BF_Combined::getBFandValueIndices(const unsigned int n, unsigned int& bf_index, unsigned int& value_index) const {
128 : // bf_index = 0; value_index = 0;
129 : // if(n==0){
130 : // bf_index = 0;
131 : // value_index = 0;
132 : // return;
133 : // }
134 : // else{
135 : // unsigned int r=1;
136 : // for(unsigned int i=0; i<basisf_pntrs_.size(); i++){
137 : // for(unsigned int l=1; l<basisf_pntrs_[i]->numberOfBasisFunctions(); l++){
138 : // if(r==n){
139 : // bf_index = i;
140 : // value_index = l;
141 : // return;
142 : // }
143 : // r++;
144 : // }
145 : // }
146 : // }
147 : // }
148 :
149 :
150 731839 : void BF_Combined::getAllValues(const double arg, double& argT, bool& inside_range, std::vector<double>& values, std::vector<double>& derivs) const {
151 : // first BF, constant, argT, and inside_range taken from here
152 : unsigned int r=0;
153 2195517 : std::vector<double> values_tmp(basisf_pntrs_[0]->numberOfBasisFunctions(),0.0);
154 2195517 : std::vector<double> derivs_tmp(basisf_pntrs_[0]->numberOfBasisFunctions(),0.0);
155 731839 : basisf_pntrs_[0]->getAllValues(arg,argT,inside_range,values_tmp,derivs_tmp);
156 16832297 : for(unsigned int l=0; l<basisf_pntrs_[0]->numberOfBasisFunctions(); l++) {
157 24150687 : values[r] = values_tmp[l];
158 8050229 : derivs[r] = derivs_tmp[l];
159 8050229 : r++;
160 : }
161 : // other BF
162 3193123 : for(unsigned int i=1; i<basisf_pntrs_.size(); i++) {
163 4922568 : values_tmp.assign(basisf_pntrs_[i]->numberOfBasisFunctions(),0.0);
164 4922568 : derivs_tmp.assign(basisf_pntrs_[i]->numberOfBasisFunctions(),0.0);
165 1230642 : double dummy_dbl; bool dummy_bool=true;
166 1230642 : basisf_pntrs_[i]->getAllValues(arg,dummy_dbl,dummy_bool,values_tmp,derivs_tmp);
167 39380544 : for(unsigned int l=1; l<basisf_pntrs_[i]->numberOfBasisFunctions(); l++) {
168 36919260 : values[r] = values_tmp[l];
169 12306420 : derivs[r] = derivs_tmp[l];
170 12306420 : r++;
171 : }
172 : }
173 731839 : }
174 :
175 :
176 2 : void BF_Combined::setupLabels() {
177 4 : setLabel(0,basisf_pntrs_[0]->getBasisFunctionLabel(0));
178 : unsigned int r=1;
179 14 : for(unsigned int i=0; i<basisf_pntrs_.size(); i++) {
180 160 : for(unsigned int l=1; l<basisf_pntrs_[i]->numberOfBasisFunctions(); l++) {
181 50 : setLabel(r,basisf_pntrs_[i]->getBasisFunctionLabel(l));
182 50 : r++;
183 : }
184 : }
185 2 : }
186 :
187 :
188 2 : void BF_Combined::setupUniformIntegrals() {
189 6 : setUniformIntegral(0,basisf_pntrs_[0]->getUniformIntegrals()[0]);
190 : unsigned int r=1;
191 14 : for(unsigned int i=0; i<basisf_pntrs_.size(); i++) {
192 5 : std::vector<double> uniform_tmp = basisf_pntrs_[i]->getUniformIntegrals();
193 160 : for(unsigned int l=1; l<basisf_pntrs_[i]->numberOfBasisFunctions(); l++) {
194 100 : setUniformIntegral(r,uniform_tmp[l]);
195 50 : r++;
196 : }
197 : }
198 2 : }
199 :
200 :
201 : }
202 5874 : }
|