Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 : Copyright (c) 2017-2019 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 "core/ActionRegister.h" 23 : #include "core/ActionShortcut.h" 24 : 25 : //+PLUMEDOC REWEIGHTING COVARIANCE_MATRIX 26 : /* 27 : Calculate a covariance matix 28 : 29 : This shortcut takes multiple vectors in input as well as a vector of weights. A 30 : [covariance matrix](https://en.wikipedia.org/wiki/Covariance_matrix) is then computed from this input 31 : data. The example below shows how this action can be used to calculate a gyration tensor that describes 32 : the shape for a cluster of atoms. 33 : 34 : ```plumed 35 : # Calculate the geometric center for 100 atoms 36 : com: CENTER ATOMS=1-100 37 : # Calculate the vector connecting each of the 100 atoms to the geometric center 38 : d: DISTANCES ATOMS=1-100 ORIGIN=com COMPONENTS 39 : # Now compute the covariance matrix 40 : ones: ONES SIZE=100 41 : covar: COVARIANCE_MATRIX ARG=d.x,d.y,d.z WEIGHTS=ones 42 : ``` 43 : 44 : In the above case the elements of the gyration tensor are computed as follows: 45 : 46 : $$ 47 : G_{\alpha\beta} = \frac{\sum_i w_i d_{i,\alpha} d_{i,\beta} }{\sum_i w_i} 48 : $$ 49 : 50 : where $\alpha$ and $\beta$ can each be $x$, $y$ or $z$ and $w_i$ is a set of weights that in the above input are all set equal to one. 51 : 52 : If you would like to compute: 53 : 54 : $$ 55 : G_{\alpha\beta} = \sum_i w_i d_{i,\alpha} d_{i,\beta} 56 : $$ 57 : 58 : instead you use the `UNORMALIZED` flag as shown below: 59 : 60 : ```plumed 61 : # Calculate the geometric center for 100 atoms 62 : com: CENTER ATOMS=1-100 63 : # Calculate the vector connecting each of the 100 atoms to the geometric center 64 : d: DISTANCES ATOMS=1-100 ORIGIN=com COMPONENTS 65 : # Now compute the covariance matrix 66 : ones: ONES SIZE=100 67 : covar: COVARIANCE_MATRIX ARG=d.x,d.y,d.z WEIGHTS=ones UNORMALIZED 68 : ``` 69 : 70 : */ 71 : //+ENDPLUMEDOC 72 : 73 : namespace PLMD { 74 : namespace matrixtools { 75 : 76 : class CovarianceMatrix : public ActionShortcut { 77 : public: 78 : static void registerKeywords(Keywords&); 79 : explicit CovarianceMatrix(const ActionOptions&ao); 80 : }; 81 : 82 : PLUMED_REGISTER_ACTION(CovarianceMatrix,"COVARIANCE_MATRIX") 83 : 84 10 : void CovarianceMatrix::registerKeywords(Keywords& keys ) { 85 10 : ActionShortcut::registerKeywords( keys ); 86 10 : keys.add("numbered","ARG","the vectors of data from which we are calculating the covariance"); 87 10 : keys.add("compulsory","WEIGHTS","this keyword takes the label of an action that calculates a vector of values. The elements of this vector " 88 : "are used as weights for the input data points."); 89 10 : keys.addFlag("UNORMALIZED",false,"do not divide by the sum of the weights"); 90 20 : keys.setValueDescription("matrix","the covariance matrix"); 91 10 : keys.needsAction("SUM"); 92 10 : keys.needsAction("CUSTOM"); 93 10 : keys.needsAction("VSTACK"); 94 10 : keys.needsAction("TRANSPOSE"); 95 10 : keys.needsAction("ONES"); 96 10 : keys.needsAction("OUTER_PRODUCT"); 97 10 : keys.needsAction("MATRIX_PRODUCT"); 98 10 : } 99 : 100 4 : CovarianceMatrix::CovarianceMatrix(const ActionOptions&ao): 101 : Action(ao), 102 4 : ActionShortcut(ao) { 103 : std::vector<std::string> args; 104 8 : parseVector("ARG",args); 105 4 : unsigned nargs=args.size(); 106 4 : std::string argstr="ARG=" + args[0]; 107 12 : for(unsigned i=1; i<args.size(); ++i) { 108 16 : argstr += "," + args[i]; 109 : } 110 : 111 : bool unorm; 112 8 : parseFlag("UNORMALIZED",unorm); 113 : std::string wstr; 114 4 : parse("WEIGHTS",wstr); 115 4 : if( !unorm ) { 116 : // Normalize the weights 117 8 : readInputLine( getShortcutLabel() + "_wsum: SUM ARG=" + wstr + " PERIODIC=NO"); 118 8 : readInputLine( getShortcutLabel() + "_weights: CUSTOM ARG=" + wstr + "," + getShortcutLabel() + "_wsum FUNC=x/y PERIODIC=NO"); 119 8 : wstr = getShortcutLabel() + "_weights"; 120 : } 121 : // Make a stack of all the data 122 8 : readInputLine( getShortcutLabel() + "_stack: VSTACK " + argstr ); 123 : // And calculate the covariance matrix by first transposing the stack 124 8 : readInputLine( getShortcutLabel() + "_stackT: TRANSPOSE ARG=" + getShortcutLabel() + "_stack"); 125 : // Create a matrix that holds all the weights 126 : std::string str_nargs; 127 4 : Tools::convert( nargs, str_nargs ); 128 8 : readInputLine( getShortcutLabel() + "_ones: ONES SIZE=" + str_nargs ); 129 : // Now create a matrix that holds all the weights 130 8 : readInputLine( getShortcutLabel() + "_matweights: OUTER_PRODUCT ARG=" + getShortcutLabel() + "_ones," + wstr ); 131 : // And multiply the weights by the transpose to get the weighted transpose 132 8 : readInputLine( getShortcutLabel() + "_wT: CUSTOM ARG=" + getShortcutLabel() + "_matweights," + getShortcutLabel() + "_stackT FUNC=x*y PERIODIC=NO"); 133 : // And now calculate the covariance by doing a suitable matrix product 134 8 : readInputLine( getShortcutLabel() + ": MATRIX_PRODUCT ARG=" + getShortcutLabel() + "_wT," + getShortcutLabel() + "_stack"); 135 4 : } 136 : 137 : } 138 : }