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 26 : keys.setValueDescription("scalar/vector","the normalized euclidean distances between the input vectors"); 55 39 : keys.needsAction("DISPLACEMENT"); keys.needsAction("CUSTOM"); keys.needsAction("OUTER_PRODUCT"); 56 39 : keys.needsAction("TRANSPOSE"); keys.needsAction("MATRIX_PRODUCT_DIAGONAL"); keys.needsAction("ONES"); 57 13 : } 58 : 59 8 : NormalizedEuclideanDistance::NormalizedEuclideanDistance( const ActionOptions& ao): 60 : Action(ao), 61 8 : ActionShortcut(ao) 62 : { 63 24 : std::string arg1, arg2, metstr; parse("ARG1",arg1); parse("ARG2",arg2); parse("METRIC",metstr); 64 : // Vectors are in rows here 65 16 : readInputLine( getShortcutLabel() + "_diff: DISPLACEMENT ARG1=" + arg1 + " ARG2=" + arg2 ); 66 : // Vectors are in columns here 67 16 : readInputLine( getShortcutLabel() + "_diffT: TRANSPOSE ARG=" + getShortcutLabel() + "_diff"); 68 : // Get the action that computes the differences 69 16 : ActionWithValue* av = plumed.getActionSet().selectWithLabel<ActionWithValue*>( getShortcutLabel() + "_diffT"); plumed_assert( av ); 70 : // If this is a matrix we need create a matrix to multiply by 71 8 : if( av->copyOutput(0)->getRank()==2 ) { 72 : // Create some ones 73 4 : std::string nones; Tools::convert( av->copyOutput(0)->getShape()[1], nones ); 74 8 : readInputLine( getShortcutLabel() + "_ones: ONES SIZE=" + nones); 75 : // Now do some multiplication to create a matrix that can be multiplied by our "inverse variance" vector 76 4 : if( av->copyOutput(0)->getShape()[0]==1 ) { 77 4 : readInputLine( getShortcutLabel() + "_" + metstr + "T: CUSTOM ARG=" + metstr + "," + getShortcutLabel() + "_ones FUNC=x*y PERIODIC=NO"); 78 4 : readInputLine( getShortcutLabel() + "_" + metstr + ": TRANSPOSE ARG=" + getShortcutLabel() + "_" + metstr + "T"); 79 4 : } else readInputLine( getShortcutLabel() + "_" + metstr + ": OUTER_PRODUCT ARG=" + metstr + "," + getShortcutLabel() + "_ones"); 80 8 : metstr = getShortcutLabel() + "_" + metstr; 81 : } 82 : // Now do the multiplication 83 16 : readInputLine( getShortcutLabel() + "_sdiff: CUSTOM ARG=" + metstr + "," + getShortcutLabel() +"_diffT FUNC=x*y PERIODIC=NO"); 84 16 : bool squared; parseFlag("SQUARED",squared); std::string olab = getShortcutLabel(); if( !squared ) olab += "_2"; 85 16 : readInputLine( olab + ": MATRIX_PRODUCT_DIAGONAL ARG=" + getShortcutLabel() +"_diff," + getShortcutLabel() + "_sdiff"); 86 13 : if( !squared ) readInputLine( getShortcutLabel() + ": CUSTOM ARG=" + getShortcutLabel() + "_2 FUNC=sqrt(x) PERIODIC=NO"); 87 8 : } 88 : 89 : } 90 : }