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/PlumedMain.h" 24 : #include "core/ActionSet.h" 25 : #include "core/ActionShortcut.h" 26 : #include "core/ActionWithValue.h" 27 : 28 : //+PLUMEDOC FUNCTION MAHALANOBIS_DISTANCE 29 : /* 30 : Calculate the mahalanobis 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 MahalanobisDistance : public ActionShortcut { 41 : public: 42 : static void registerKeywords( Keywords& keys ); 43 : explicit MahalanobisDistance(const ActionOptions&ao); 44 : }; 45 : 46 : PLUMED_REGISTER_ACTION(MahalanobisDistance,"MAHALANOBIS_DISTANCE") 47 : 48 21 : void MahalanobisDistance::registerKeywords( Keywords& keys ) { 49 21 : ActionShortcut::registerKeywords(keys); 50 42 : keys.add("compulsory","ARG1","The point that we are calculating the distance from"); 51 42 : keys.add("compulsory","ARG2","The point that we are calculating the distance to"); 52 42 : keys.add("compulsory","METRIC","The inverse covariance matrix that should be used when calculating the distance"); 53 42 : keys.addFlag("SQUARED",false,"The squared distance should be calculated"); 54 42 : keys.addFlag("VON_MISSES",false,"Compute the mahalanobis distance in a way that is more sympathetic to the periodic boundary conditions"); 55 42 : keys.setValueDescription("scalar/vector","the Mahalanobis distances between the input vectors"); 56 63 : keys.needsAction("DISPLACEMENT"); keys.needsAction("CUSTOM"); keys.needsAction("OUTER_PRODUCT"); 57 63 : keys.needsAction("TRANSPOSE"); keys.needsAction("MATRIX_PRODUCT_DIAGONAL"); keys.needsAction("CONSTANT"); 58 63 : keys.needsAction("MATRIX_VECTOR_PRODUCT"); keys.needsAction("MATRIX_PRODUCT"); keys.needsAction("COMBINE"); 59 21 : } 60 : 61 12 : MahalanobisDistance::MahalanobisDistance( const ActionOptions& ao): 62 : Action(ao), 63 12 : ActionShortcut(ao) 64 : { 65 36 : std::string arg1, arg2, metstr; parse("ARG1",arg1); parse("ARG2",arg2); parse("METRIC",metstr); 66 : // Check on input metric 67 12 : ActionWithValue* mav=plumed.getActionSet().selectWithLabel<ActionWithValue*>( metstr ); 68 12 : if( !mav ) error("could not find action named " + metstr + " to use for metric"); 69 12 : if( mav->copyOutput(0)->getRank()!=2 ) error("metric has incorrect rank"); 70 : 71 24 : readInputLine( getShortcutLabel() + "_diff: DISPLACEMENT ARG1=" + arg1 + " ARG2=" + arg2 ); 72 24 : readInputLine( getShortcutLabel() + "_diffT: TRANSPOSE ARG=" + getShortcutLabel() + "_diff"); 73 24 : bool von_miss, squared; parseFlag("VON_MISSES",von_miss); parseFlag("SQUARED",squared); 74 12 : if( von_miss ) { 75 7 : unsigned nrows = mav->copyOutput(0)->getShape()[0]; 76 7 : if( mav->copyOutput(0)->getShape()[1]!=nrows ) error("metric is not symmetric"); 77 : // Create a matrix that can be used to compute the off diagonal elements 78 : std::string valstr, nrstr; 79 7 : Tools::convert( mav->copyOutput(0)->get(0), valstr ); Tools::convert( nrows, nrstr ); 80 7 : std::string diagmet = getShortcutLabel() + "_diagmet: CONSTANT VALUES=" + valstr; 81 14 : std::string offdiagmet = getShortcutLabel() + "_offdiagmet: CONSTANT NROWS=" + nrstr + " NCOLS=" + nrstr + " VALUES=0"; 82 21 : for(unsigned i=0; i<nrows; ++i) { 83 42 : for(unsigned j=0; j<nrows; ++j) { 84 28 : Tools::convert( mav->copyOutput(0)->get(i*nrows+j), valstr ); 85 42 : if( i==j && i>0 ) { offdiagmet += ",0"; diagmet += "," + valstr; } 86 35 : else if( i!=j ) { offdiagmet += "," + valstr; } 87 : } 88 : } 89 7 : readInputLine( diagmet ); readInputLine( offdiagmet ); 90 : // Compute distances scaled by periods 91 14 : ActionWithValue* av=plumed.getActionSet().selectWithLabel<ActionWithValue*>( getShortcutLabel() + "_diff" ); plumed_assert( av ); 92 7 : if( !av->copyOutput(0)->isPeriodic() ) error("VON_MISSES only works with periodic variables"); 93 7 : std::string min, max; av->copyOutput(0)->getDomain(min,max); 94 14 : readInputLine( getShortcutLabel() + "_scaled: CUSTOM ARG=" + getShortcutLabel() + "_diffT FUNC=2*pi*x/(" + max +"-" + min + ") PERIODIC=NO"); 95 : // We start calculating off-diagonal elements by computing the sines of the scaled differences (this is a column vector) 96 14 : readInputLine( getShortcutLabel() + "_sinediffT: CUSTOM ARG=" + getShortcutLabel() + "_scaled FUNC=sin(x) PERIODIC=NO"); 97 : // Transpose sines to get a row vector 98 14 : readInputLine( getShortcutLabel() + "_sinediff: TRANSPOSE ARG=" + getShortcutLabel() + "_sinediffT"); 99 : // Compute the off diagonal elements 100 14 : readInputLine( getShortcutLabel() + "_matvec: MATRIX_PRODUCT ARG=" + getShortcutLabel() + "_offdiagmet," + getShortcutLabel() +"_sinediffT"); 101 14 : readInputLine( getShortcutLabel() + "_offdiag: MATRIX_PRODUCT_DIAGONAL ARG=" + getShortcutLabel() + "_sinediff," + getShortcutLabel() +"_matvec"); 102 : // Sort out the metric for the diagonal elements 103 7 : std::string metstr2 = getShortcutLabel() + "_diagmet"; 104 : // If this is a matrix we need create a matrix to multiply by 105 7 : if( av->copyOutput(0)->getShape()[0]>1 ) { 106 : // Create some ones 107 21 : std::string ones=" VALUES=1"; for(unsigned i=1; i<av->copyOutput(0)->getShape()[0]; ++i ) ones += ",1"; 108 14 : readInputLine( getShortcutLabel() + "_ones: CONSTANT " + ones ); 109 : // Now do some multiplication to create a matrix that can be multiplied by our "inverse variance" vector 110 14 : readInputLine( getShortcutLabel() + "_" + metstr + ": OUTER_PRODUCT ARG=" + metstr2 + "," + getShortcutLabel() + "_ones"); 111 14 : metstr2 = getShortcutLabel() + "_" + metstr; 112 : } 113 : // Compute the diagonal elements 114 14 : readInputLine( getShortcutLabel() + "_prod: CUSTOM ARG=" + getShortcutLabel() + "_scaled," + metstr2 + " FUNC=2*(1-cos(x))*y PERIODIC=NO"); 115 7 : std::string ncstr; Tools::convert( nrows, ncstr ); Tools::convert( av->copyOutput(0)->getShape()[0], nrstr ); 116 42 : std::string ones=" VALUES=1"; for(unsigned i=1; i<av->copyOutput(0)->getNumberOfValues(); ++i) ones += ",1"; 117 14 : readInputLine( getShortcutLabel() + "_matones: CONSTANT NROWS=" + nrstr + " NCOLS=" + ncstr + ones ); 118 14 : readInputLine( getShortcutLabel() + "_diag: MATRIX_PRODUCT_DIAGONAL ARG=" + getShortcutLabel() + "_matones," + getShortcutLabel() + "_prod"); 119 : // Sum everything 120 7 : if( !squared ) readInputLine( getShortcutLabel() + "_2: COMBINE ARG=" + getShortcutLabel() + "_offdiag," + getShortcutLabel() + "_diag PERIODIC=NO"); 121 14 : else readInputLine( getShortcutLabel() + ": COMBINE ARG=" + getShortcutLabel() + "_offdiag," + getShortcutLabel() + "_diag PERIODIC=NO"); 122 : } else { 123 10 : ActionWithValue* av=plumed.getActionSet().selectWithLabel<ActionWithValue*>( getShortcutLabel() + "_diffT" ); plumed_assert( av && av->getNumberOfComponents()==1 ); 124 9 : if( (av->copyOutput(0))->getRank()==1 ) readInputLine( getShortcutLabel() + "_matvec: MATRIX_VECTOR_PRODUCT ARG=" + metstr + "," + getShortcutLabel() +"_diffT"); 125 2 : else readInputLine( getShortcutLabel() + "_matvec: MATRIX_PRODUCT ARG=" + metstr + "," + getShortcutLabel() +"_diffT"); 126 5 : std::string olab = getShortcutLabel(); if( !squared ) olab += "_2"; 127 10 : readInputLine( olab + ": MATRIX_PRODUCT_DIAGONAL ARG=" + getShortcutLabel() + "_diff," + getShortcutLabel() +"_matvec"); 128 : } 129 17 : if( !squared ) readInputLine( getShortcutLabel() + ": CUSTOM ARG=" + getShortcutLabel() + "_2 FUNC=sqrt(x) PERIODIC=NO"); 130 12 : } 131 : 132 : } 133 : }