1 /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2    Copyright (c) 2017-2021 The plumed team
3    (see the PEOPLE file at the root of the distribution for a list of names)
4 
5    See http://www.plumed.org for more information.
6 
7    This file is part of plumed, version 2.
8 
9    plumed 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    plumed 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 plumed.  If not, see <http://www.gnu.org/licenses/>.
21 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
22 #include "MultiColvarBase.h"
23 #include "AtomValuePack.h"
24 #include "core/ActionRegister.h"
25 
26 #include <string>
27 #include <cmath>
28 
29 //+PLUMEDOC MCOLVARF MCOLV_PRODUCT
30 /*
31 Calculate a product of multiple multicolvars
32 
33 \par Examples
34 
35 */
36 //+ENDPLUMEDOC
37 
38 namespace PLMD {
39 namespace multicolvar {
40 
41 class MultiColvarProduct : public MultiColvarBase {
42 private:
43 public:
44   static void registerKeywords( Keywords& keys );
45   explicit MultiColvarProduct(const ActionOptions&);
46 /// Actually do the calculation
47   double compute( const unsigned& tindex, AtomValuePack& myatoms ) const override;
48 /// Is the variable periodic
isPeriodic()49   bool isPeriodic() override { return false; }
50 };
51 
52 PLUMED_REGISTER_ACTION(MultiColvarProduct,"MCOLV_PRODUCT")
53 
registerKeywords(Keywords & keys)54 void MultiColvarProduct::registerKeywords( Keywords& keys ) {
55   MultiColvarBase::registerKeywords( keys );
56   keys.add("compulsory","DATA","the multicolvars you are calculating the product of");
57   keys.use("MEAN"); keys.use("MORE_THAN"); keys.use("SUM"); keys.use("LESS_THAN"); keys.use("HISTOGRAM");
58   keys.use("MIN"); keys.use("MAX"); keys.use("LOWEST"); keys.use("HIGHEST"); keys.use("ALT_MIN"); keys.use("BETWEEN"); keys.use("MOMENTS");
59 }
60 
MultiColvarProduct(const ActionOptions & ao)61 MultiColvarProduct::MultiColvarProduct(const ActionOptions& ao):
62   Action(ao),
63   MultiColvarBase(ao)
64 {
65   buildSets();
66   for(unsigned i=0; i<getNumberOfBaseMultiColvars(); ++i) {
67     if( mybasemulticolvars[i]->weightWithDerivatives() ) error("cannot take product of multicolvars with weights");
68   }
69 }
70 
compute(const unsigned & tindex,AtomValuePack & myatoms) const71 double MultiColvarProduct::compute( const unsigned& tindex, AtomValuePack& myatoms ) const {
72   double dot=1; std::vector<double> tval(2);
73   for(unsigned i=0; i<getNumberOfBaseMultiColvars(); ++i) {
74     getInputData( i, false, myatoms, tval );
75     dot *= tval[1];
76   }
77   if( !doNotCalculateDerivatives() ) {
78     std::vector<double> cc(2);
79     for(unsigned i=0; i<getNumberOfBaseMultiColvars(); ++i) {
80       getInputData( i, false, myatoms, cc ); cc[1] = dot / cc[1];
81       MultiValue& myder=getInputDerivatives( i, false, myatoms );
82       splitInputDerivatives( 1, 1, 2, i, cc, myder, myatoms );
83     }
84   }
85   return dot;
86 }
87 
88 }
89 }
90