LCOV - code coverage report
Current view: top level - crystallization - VectorMultiColvar.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 42 52 80.8 %
Date: 2026-03-30 13:16:06 Functions: 6 9 66.7 %

          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          50 : void VectorMultiColvar::registerKeywords( Keywords& keys ) {
      29          50 :   MultiColvarBase::registerKeywords( keys );
      30          50 :   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          50 : }
      48             : 
      49          26 : VectorMultiColvar::VectorMultiColvar(const ActionOptions& ao):
      50             :   Action(ao),
      51             :   MultiColvarBase(ao),
      52          26 :   store_director(true) {
      53             :   setLowMemOption(true);
      54          26 : }
      55             : 
      56          26 : void VectorMultiColvar::setVectorDimensionality( const unsigned& ncomp ) {
      57          26 :   ncomponents = ncomp;
      58          26 :   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      101849 : double VectorMultiColvar::compute( const unsigned& taskIndex, multicolvar::AtomValuePack& myatoms ) const {
      66             :   // Now calculate the vector
      67      101849 :   calculateVector( myatoms );
      68             :   // Sort out the active derivatives
      69      101849 :   updateActiveAtoms( myatoms );
      70             : 
      71             :   // Now calculate the norm of the vector (this is what we return here)
      72             :   double norm=0;
      73     2032127 :   for(unsigned i=0; i<ncomponents; ++i) {
      74     1930278 :     norm += myatoms.getValue(2+i)*myatoms.getValue(2+i);
      75             :   }
      76      101849 :   norm=sqrt(norm);
      77             : 
      78      101849 :   if( !doNotCalculateDerivatives() ) {
      79       76287 :     double inorm = 1.0 / norm;
      80       76287 :     std::vector<double> dervec( ncomponents );
      81     1535613 :     for(unsigned i=0; i<ncomponents; ++i) {
      82     1459326 :       dervec[i] = inorm*myatoms.getValue(2+i);
      83             :     }
      84             : 
      85             :     MultiValue& myvals=myatoms.getUnderlyingMultiValue();
      86     7895952 :     for(unsigned j=0; j<myvals.getNumberActive(); ++j) {
      87     7819665 :       unsigned jder=myvals.getActiveIndex(j);
      88   185984763 :       for(unsigned i=0; i<ncomponents; ++i) {
      89   178165098 :         myvals.addDerivative( 1, jder, dervec[i]*myvals.getDerivative( 2+i, jder ) );
      90             :       }
      91             :     }
      92             :   }
      93             : 
      94      101849 :   return norm;
      95             : }
      96             : 
      97       64156 : void VectorMultiColvar::normalizeVector( std::vector<double>& vals ) const {
      98             :   double inorm = 1.0;
      99       64156 :   if( vals[1]>epsilon ) {
     100       64073 :     inorm = 1.0 / vals[1];
     101             :   }
     102     1632096 :   for(unsigned i=2; i<vals.size(); ++i) {
     103     1567940 :     vals[i] = inorm*vals[i];
     104             :   }
     105       64156 : }
     106             : 
     107       29195 : void VectorMultiColvar::normalizeVectorDerivatives( MultiValue& myvals ) const {
     108       29195 :   double v = myvals.get(1), weight = 1.0 / v,  wdf = 1.0 / ( v*v*v );
     109     3570659 :   for(unsigned j=0; j<myvals.getNumberActive(); ++j) {
     110             :     double comp2=0.0;
     111     3541464 :     unsigned jder=myvals.getActiveIndex(j);
     112    85824660 :     for(unsigned jcomp=2; jcomp<myvals.getNumberOfValues(); ++jcomp) {
     113    82283196 :       comp2 += myvals.get(jcomp)*myvals.getDerivative( jcomp, jder );
     114             :     }
     115    85824660 :     for(unsigned jcomp=2; jcomp<myvals.getNumberOfValues(); ++jcomp) {
     116    82283196 :       myvals.setDerivative( jcomp, jder, weight*myvals.getDerivative( jcomp, jder ) - wdf*comp2*myvals.get(jcomp) );
     117             :     }
     118             :   }
     119       29195 : }
     120             : 
     121           0 : void VectorMultiColvar::addForcesOnAtoms( const std::vector<double>& inforces ) {
     122             :   plumed_dbg_assert( inforces.size()==getNumberOfDerivatives() );
     123           0 :   std::vector<double> oldforces( getNumberOfDerivatives() );
     124           0 :   getForcesFromVessels( oldforces );
     125           0 :   for(unsigned i=0; i<getNumberOfDerivatives(); ++i) {
     126           0 :     oldforces[i]+=inforces[i];
     127             :   }
     128           0 :   setForcesOnAtoms( oldforces );
     129           0 : }
     130             : 
     131             : }
     132             : }

Generated by: LCOV version 1.16