Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 : Copyright (c) 2015-2017 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 "AdjacencyMatrixBase.h" 23 : #include "core/ActionRegister.h" 24 : #include "tools/Matrix.h" 25 : 26 : //+PLUMEDOC MATRIX DISTANCE_MATRIX 27 : /* 28 : Calculate a matrix of distances between atoms. 29 : 30 : To calculate the matrix of distances between every distinct pair of atoms in a single group you can use the following command: 31 : 32 : ```plumed 33 : d1: DISTANCE_MATRIX GROUP=1-7 34 : ``` 35 : 36 : If you would like to calculate the matrix of distances between the atoms in two different groups of atoms you can use the following command: 37 : 38 : ```plumed 39 : d2: DISTANCE_MATRIX GROUPA=1-7 GROUPB=8-20 40 : ``` 41 : 42 : Once you have calculated your distance matrix in this way you can do many of the operations that were discussed for [CONTACT_MATRIX](CONTACT_MATRIX.md) with the output. 43 : For example, you can use the COMPONENTS flag to calcuate the $x$, $y$ and $z$ components of the vectors connecting the atoms in your two groups by using 44 : an input like that shown below: 45 : 46 : ```plumed 47 : d1: DISTANCE_MATRIX GROUP=1-7 COMPONENTS 48 : ``` 49 : 50 : ## Optimisation details 51 : 52 : If for some reaon, you only want to calculate the distances if they are less than a certain CUTOFF you can add the cutoff keyword as follows: 53 : 54 : ```plumed 55 : d3: DISTANCE_MATRIX GROUP=1-7 CUTOFF=1.0 56 : ``` 57 : 58 : This command will only store those distances that are less than 1 nm. Notice, however, that the cutoff implemented here is __not__ a continuous function. 59 : You should thus be careful when commands such as this one above to ensure that any quantities that are forced have continuous derivatives. If you use 60 : the CUTOFF keyword, however, many of the features that are used to optimise [CONTACT_MATRIX](CONTACT_MATRIX.md) are used for this action. 61 : 62 : */ 63 : //+ENDPLUMEDOC 64 : 65 : 66 : namespace PLMD { 67 : namespace adjmat { 68 : 69 : class DistanceMatrix : public AdjacencyMatrixBase { 70 : private: 71 : double cutoff; 72 : public: 73 : /// Create manual 74 : static void registerKeywords( Keywords& keys ); 75 : /// Constructor 76 : explicit DistanceMatrix(const ActionOptions&); 77 : /// This does nothing 78 : double calculateWeight( const Vector& pos1, const Vector& pos2, const unsigned& natoms, MultiValue& myvals ) const ; 79 : }; 80 : 81 : PLUMED_REGISTER_ACTION(DistanceMatrix,"DISTANCE_MATRIX") 82 : 83 178 : void DistanceMatrix::registerKeywords( Keywords& keys ) { 84 178 : AdjacencyMatrixBase::registerKeywords( keys ); 85 178 : keys.add("compulsory","CUTOFF","-1","ignore distances that have a value larger than this cutoff"); 86 178 : } 87 : 88 92 : DistanceMatrix::DistanceMatrix( const ActionOptions& ao ): 89 : Action(ao), 90 92 : AdjacencyMatrixBase(ao) { 91 : // And set the link cell cutoff 92 92 : log.printf(" weight is distance between atoms \n"); 93 92 : parse("CUTOFF",cutoff); 94 92 : if( cutoff<0 ) { 95 5 : setLinkCellCutoff( true, std::numeric_limits<double>::max() ); 96 : } else { 97 87 : log.printf(" ignoring distances that are larger than %f \n", cutoff); 98 87 : setLinkCellCutoff( true, cutoff ); 99 : } 100 92 : } 101 : 102 5857010 : double DistanceMatrix::calculateWeight( const Vector& pos1, const Vector& pos2, const unsigned& natoms, MultiValue& myvals ) const { 103 5857010 : Vector distance = pos2; 104 5857010 : double mod = distance.modulo(); 105 5857010 : if( cutoff<0 || mod<cutoff ) { 106 5157580 : double invd = 1.0/mod; 107 5157580 : addAtomDerivatives( 0, (-invd)*distance, myvals ); 108 5157580 : addAtomDerivatives( 1, (+invd)*distance, myvals ); 109 5157580 : addBoxDerivatives( (-invd)*Tensor(distance,distance), myvals ); 110 : } 111 5857010 : return mod; 112 : } 113 : 114 : } 115 : } 116 :