Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 : Copyright (c) 2013-2023 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 "VectorMultiColvar.h" 23 : #include "multicolvar/BridgedMultiColvarFunction.h" 24 : 25 : namespace PLMD { 26 : namespace crystallization { 27 : 28 32 : void VectorMultiColvar::registerKeywords( Keywords& keys ) { 29 32 : MultiColvarBase::registerKeywords( keys ); 30 32 : keys.setComponentsIntroduction("When the label of this action is used as the input for a second you are not referring to a scalar quantity as you are in " 31 : "regular collective variables. The label is used to reference the full set of vectors calculated by " 32 : "the action. This is usual when using \\ref multicolvarfunction. Generally when doing this the previously calculated " 33 : "multicolvar will be referenced using the DATA keyword rather than ARG.\n\n" 34 : "This Action can be used to calculate the following scalar quantities directly. These quantities are calculated by " 35 : "employing the keywords listed below. " 36 : "These quantities can then be referenced elsewhere in the input file by using this Action's label " 37 : "followed by a dot and the name of the quantity. All of them can be calculated multiple times " 38 : "with different parameters. In this case the quantities calculated can be referenced elsewhere in the " 39 : "input by using the name of the quantity followed by a numerical identifier " 40 : "e.g. <em>label</em>.lessthan-1, <em>label</em>.lessthan-2 etc. When doing this and, for clarity we have " 41 : "made it so that the user can set the label for the components. As such by using the LABEL keyword in the description of the keyword " 42 : "input you can customize the component name. In addition, you can calculate all of these scalar functions for " 43 : "one particular component of the calculated vector by making use of the COMPONENT keyword. The first component is used to " 44 : "refer to the norm of the vector. The individual components can then be referenced using the numbers 2, 3, and so on. So " 45 : "as an example MEAN1={COMPONENT=1} calculates the average vector norm. MEAN2={COMPONENT=2} by contrast calculates the mean " 46 : "for all of the first components of the vectors."); 47 32 : } 48 : 49 26 : VectorMultiColvar::VectorMultiColvar(const ActionOptions& ao): 50 : Action(ao), 51 : MultiColvarBase(ao), 52 26 : store_director(true) 53 : { 54 : setLowMemOption(true); 55 26 : } 56 : 57 26 : void VectorMultiColvar::setVectorDimensionality( const unsigned& ncomp ) { 58 26 : ncomponents = ncomp; resizeFunctions(); // This resize needs to be here to ensure buffers are set to correct size in base 59 26 : } 60 : 61 0 : void VectorMultiColvar::doNotCalculateDirector() { 62 0 : store_director=false; // Need a sanity check in here so that you don't use the same instance of Q4 to calculate vectors and directors 63 0 : } 64 : 65 100463 : double VectorMultiColvar::compute( const unsigned& taskIndex, multicolvar::AtomValuePack& myatoms ) const { 66 : // Now calculate the vector 67 100463 : calculateVector( myatoms ); 68 : // Sort out the active derivatives 69 100463 : updateActiveAtoms( myatoms ); 70 : 71 : // Now calculate the norm of the vector (this is what we return here) 72 : double norm=0; 73 1994705 : for(unsigned i=0; i<ncomponents; ++i) norm += myatoms.getValue(2+i)*myatoms.getValue(2+i); 74 100463 : norm=sqrt(norm); 75 : 76 100463 : if( !doNotCalculateDerivatives() ) { 77 74901 : double inorm = 1.0 / norm; std::vector<double> dervec( ncomponents ); 78 1498191 : for(unsigned i=0; i<ncomponents; ++i) dervec[i] = inorm*myatoms.getValue(2+i); 79 : 80 : MultiValue& myvals=myatoms.getUnderlyingMultiValue(); 81 7788750 : for(unsigned j=0; j<myvals.getNumberActive(); ++j) { 82 7713849 : unsigned jder=myvals.getActiveIndex(j); 83 183127731 : for(unsigned i=0; i<ncomponents; ++i) myvals.addDerivative( 1, jder, dervec[i]*myvals.getDerivative( 2+i, jder ) ); 84 : } 85 : } 86 : 87 100463 : return norm; 88 : } 89 : 90 64156 : void VectorMultiColvar::normalizeVector( std::vector<double>& vals ) const { 91 : double inorm = 1.0; 92 64156 : if( vals[1]>epsilon ) inorm = 1.0 / vals[1]; 93 1632096 : for(unsigned i=2; i<vals.size(); ++i) vals[i] = inorm*vals[i]; 94 64156 : } 95 : 96 29195 : void VectorMultiColvar::normalizeVectorDerivatives( MultiValue& myvals ) const { 97 29195 : double v = myvals.get(1), weight = 1.0 / v, wdf = 1.0 / ( v*v*v ); 98 3570659 : for(unsigned j=0; j<myvals.getNumberActive(); ++j) { 99 3541464 : double comp2=0.0; unsigned jder=myvals.getActiveIndex(j); 100 85824660 : for(unsigned jcomp=2; jcomp<myvals.getNumberOfValues(); ++jcomp) comp2 += myvals.get(jcomp)*myvals.getDerivative( jcomp, jder ); 101 85824660 : for(unsigned jcomp=2; jcomp<myvals.getNumberOfValues(); ++jcomp) { 102 82283196 : myvals.setDerivative( jcomp, jder, weight*myvals.getDerivative( jcomp, jder ) - wdf*comp2*myvals.get(jcomp) ); 103 : } 104 : } 105 29195 : } 106 : 107 0 : void VectorMultiColvar::addForcesOnAtoms( const std::vector<double>& inforces ) { 108 : plumed_dbg_assert( inforces.size()==getNumberOfDerivatives() ); 109 0 : std::vector<double> oldforces( getNumberOfDerivatives() ); 110 0 : getForcesFromVessels( oldforces ); 111 0 : for(unsigned i=0; i<getNumberOfDerivatives(); ++i) oldforces[i]+=inforces[i]; 112 0 : setForcesOnAtoms( oldforces ); 113 0 : } 114 : 115 : } 116 : }