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

Generated by: LCOV version 1.16