LCOV - code coverage report
Current view: top level - matrixtools - CovarianceMatrix.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 35 35 100.0 %
Date: 2025-04-08 21:11:17 Functions: 2 3 66.7 %

          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             : */
      45             : //+ENDPLUMEDOC
      46             : 
      47             : namespace PLMD {
      48             : namespace matrixtools {
      49             : 
      50             : class CovarianceMatrix : public ActionShortcut {
      51             : public:
      52             :   static void registerKeywords(Keywords&);
      53             :   explicit CovarianceMatrix(const ActionOptions&ao);
      54             : };
      55             : 
      56             : PLUMED_REGISTER_ACTION(CovarianceMatrix,"COVARIANCE_MATRIX")
      57             : 
      58          10 : void CovarianceMatrix::registerKeywords(Keywords& keys ) {
      59          10 :   ActionShortcut::registerKeywords( keys );
      60          10 :   keys.add("numbered","ARG","the vectors of data from which we are calculating the covariance");
      61          10 :   keys.add("compulsory","WEIGHTS","this keyword takes the label of an action that calculates a vector of values.  The elements of this vector "
      62             :            "are used as weights for the input data points.");
      63          10 :   keys.addFlag("UNORMALIZED",false,"do not divide by the sum of the weights");
      64          20 :   keys.setValueDescription("matrix","the covariance matrix");
      65          10 :   keys.needsAction("SUM");
      66          10 :   keys.needsAction("CUSTOM");
      67          10 :   keys.needsAction("VSTACK");
      68          10 :   keys.needsAction("TRANSPOSE");
      69          10 :   keys.needsAction("ONES");
      70          10 :   keys.needsAction("OUTER_PRODUCT");
      71          10 :   keys.needsAction("MATRIX_PRODUCT");
      72          10 : }
      73             : 
      74           4 : CovarianceMatrix::CovarianceMatrix(const ActionOptions&ao):
      75             :   Action(ao),
      76           4 :   ActionShortcut(ao) {
      77             :   std::vector<std::string> args;
      78           8 :   parseVector("ARG",args);
      79           4 :   unsigned nargs=args.size();
      80           4 :   std::string argstr="ARG=" + args[0];
      81          12 :   for(unsigned i=1; i<args.size(); ++i) {
      82          16 :     argstr += "," + args[i];
      83             :   }
      84             : 
      85             :   bool unorm;
      86           8 :   parseFlag("UNORMALIZED",unorm);
      87             :   std::string wstr;
      88           4 :   parse("WEIGHTS",wstr);
      89           4 :   if( !unorm ) {
      90             :     // Normalize the weights
      91           8 :     readInputLine( getShortcutLabel() + "_wsum: SUM ARG=" + wstr + " PERIODIC=NO");
      92           8 :     readInputLine( getShortcutLabel() + "_weights: CUSTOM ARG=" + wstr + "," + getShortcutLabel() + "_wsum FUNC=x/y PERIODIC=NO");
      93           8 :     wstr = getShortcutLabel() + "_weights";
      94             :   }
      95             :   // Make a stack of all the data
      96           8 :   readInputLine( getShortcutLabel() + "_stack: VSTACK " + argstr );
      97             :   // And calculate the covariance matrix by first transposing the stack
      98           8 :   readInputLine( getShortcutLabel() + "_stackT: TRANSPOSE ARG=" + getShortcutLabel() + "_stack");
      99             :   // Create a matrix that holds all the weights
     100             :   std::string str_nargs;
     101           4 :   Tools::convert( nargs, str_nargs );
     102           8 :   readInputLine( getShortcutLabel() + "_ones: ONES SIZE=" + str_nargs );
     103             :   // Now create a matrix that holds all the weights
     104           8 :   readInputLine( getShortcutLabel() + "_matweights: OUTER_PRODUCT ARG=" + getShortcutLabel() + "_ones," + wstr );
     105             :   // And multiply the weights by the transpose to get the weighted transpose
     106           8 :   readInputLine( getShortcutLabel() + "_wT: CUSTOM ARG=" + getShortcutLabel() + "_matweights," + getShortcutLabel() + "_stackT FUNC=x*y PERIODIC=NO");
     107             :   // And now calculate the covariance by doing a suitable matrix product
     108           8 :   readInputLine( getShortcutLabel() + ": MATRIX_PRODUCT ARG=" + getShortcutLabel() + "_wT," + getShortcutLabel() + "_stack");
     109           4 : }
     110             : 
     111             : }
     112             : }

Generated by: LCOV version 1.16