LCOV - code coverage report
Current view: top level - refdist - NormalizedEuclideanDistance.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 39 39 100.0 %
Date: 2025-11-25 13:55:50 Functions: 2 3 66.7 %

          Line data    Source code
       1             : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       2             :    Copyright (c) 2016-2018 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             : #include "core/ActionWithValue.h"
      25             : #include "core/PlumedMain.h"
      26             : #include "core/ActionSet.h"
      27             : 
      28             : //+PLUMEDOC FUNCTION NORMALIZED_EUCLIDEAN_DISTANCE
      29             : /*
      30             : Calculate the normalised euclidean distance between two points in CV space
      31             : 
      32             : \par Examples
      33             : 
      34             : */
      35             : //+ENDPLUMEDOC
      36             : 
      37             : namespace PLMD {
      38             : namespace refdist {
      39             : 
      40             : class NormalizedEuclideanDistance : public ActionShortcut {
      41             : public:
      42             :   static void registerKeywords( Keywords& keys );
      43             :   explicit NormalizedEuclideanDistance(const ActionOptions&ao);
      44             : };
      45             : 
      46             : PLUMED_REGISTER_ACTION(NormalizedEuclideanDistance,"NORMALIZED_EUCLIDEAN_DISTANCE")
      47             : 
      48          13 : void NormalizedEuclideanDistance::registerKeywords( Keywords& keys ) {
      49          13 :   ActionShortcut::registerKeywords(keys);
      50          26 :   keys.add("compulsory","ARG1","The poin that we are calculating the distance from");
      51          26 :   keys.add("compulsory","ARG2","The point that we are calculating the distance to");
      52          26 :   keys.add("compulsory","METRIC","The inverse covariance matrix that should be used when calculating the distance");
      53          26 :   keys.addFlag("SQUARED",false,"The squared distance should be calculated");
      54          13 :   keys.setValueDescription("the normalized euclidean distances between the input vectors");
      55          13 :   keys.needsAction("DISPLACEMENT");
      56          13 :   keys.needsAction("CUSTOM");
      57          13 :   keys.needsAction("OUTER_PRODUCT");
      58          13 :   keys.needsAction("TRANSPOSE");
      59          13 :   keys.needsAction("MATRIX_PRODUCT_DIAGONAL");
      60          13 :   keys.needsAction("ONES");
      61          13 : }
      62             : 
      63           8 : NormalizedEuclideanDistance::NormalizedEuclideanDistance( const ActionOptions& ao):
      64             :   Action(ao),
      65           8 :   ActionShortcut(ao) {
      66             :   std::string arg1, arg2, metstr;
      67           8 :   parse("ARG1",arg1);
      68           8 :   parse("ARG2",arg2);
      69           8 :   parse("METRIC",metstr);
      70             :   // Vectors are in rows here
      71          16 :   readInputLine( getShortcutLabel() + "_diff: DISPLACEMENT ARG1=" + arg1 + " ARG2=" + arg2 );
      72             :   // Vectors are in columns here
      73          16 :   readInputLine( getShortcutLabel() + "_diffT: TRANSPOSE ARG=" + getShortcutLabel() + "_diff");
      74             :   // Get the action that computes the differences
      75           8 :   ActionWithValue* av = plumed.getActionSet().selectWithLabel<ActionWithValue*>( getShortcutLabel() + "_diffT");
      76           8 :   plumed_assert( av );
      77             :   // If this is a matrix we need create a matrix to multiply by
      78           8 :   if( av->copyOutput(0)->getRank()==2 ) {
      79             :     // Create some ones
      80             :     std::string nones;
      81           4 :     Tools::convert( av->copyOutput(0)->getShape()[1], nones );
      82           8 :     readInputLine( getShortcutLabel() + "_ones: ONES SIZE=" + nones);
      83             :     // Now do some multiplication to create a matrix that can be multiplied by our "inverse variance" vector
      84           4 :     if( av->copyOutput(0)->getShape()[0]==1 ) {
      85           4 :       readInputLine( getShortcutLabel() + "_" + metstr + "T: CUSTOM ARG=" + metstr + "," + getShortcutLabel() + "_ones FUNC=x*y PERIODIC=NO");
      86           4 :       readInputLine( getShortcutLabel() + "_" + metstr + ": TRANSPOSE ARG=" + getShortcutLabel() + "_" + metstr + "T");
      87             :     } else {
      88           4 :       readInputLine( getShortcutLabel() + "_" + metstr + ": OUTER_PRODUCT ARG=" + metstr + "," + getShortcutLabel() + "_ones");
      89             :     }
      90           8 :     metstr = getShortcutLabel() + "_" + metstr;
      91             :   }
      92             :   // Now do the multiplication
      93          16 :   readInputLine( getShortcutLabel() + "_sdiff: CUSTOM ARG=" + metstr + "," + getShortcutLabel() +"_diffT FUNC=x*y PERIODIC=NO");
      94             :   bool squared;
      95           8 :   parseFlag("SQUARED",squared);
      96           8 :   std::string olab = getShortcutLabel();
      97           8 :   if( !squared ) {
      98             :     olab += "_2";
      99             :   }
     100          16 :   readInputLine( olab + ": MATRIX_PRODUCT_DIAGONAL ARG=" + getShortcutLabel() +"_diff," + getShortcutLabel() + "_sdiff");
     101           8 :   if( !squared ) {
     102          10 :     readInputLine( getShortcutLabel() + ": CUSTOM ARG=" + getShortcutLabel() + "_2 FUNC=sqrt(x) PERIODIC=NO");
     103             :   }
     104           8 : }
     105             : 
     106             : }
     107             : }

Generated by: LCOV version 1.16