LCOV - code coverage report
Current view: top level - vesselbase - StoreDataVessel.h (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 29 31 93.5 %
Date: 2026-03-30 13:16:06 Functions: 6 7 85.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             : #ifndef __PLUMED_vesselbase_StoreDataVessel_h
      23             : #define __PLUMED_vesselbase_StoreDataVessel_h
      24             : 
      25             : #include <string>
      26             : #include <cstring>
      27             : #include <vector>
      28             : #include <cstddef>
      29             : #include "Vessel.h"
      30             : 
      31             : namespace PLMD {
      32             : namespace vesselbase {
      33             : 
      34             : /**
      35             : \ingroup TOOLBOX
      36             : Objects that inherit from FunctionVessel can be used (in tandem with PLMD::vesselbase::ActionWithVessel)
      37             : to store values and derivatives for a set of scalars or vectors that are calculated by a
      38             : PLMD::vesselbase::ActionWithVessel.  Functions of these stored quantities can then be calculated in a
      39             : second step.
      40             : */
      41             : 
      42             : class StoreDataVessel : public Vessel {
      43             :   friend class Moments;
      44             : private:
      45             : /// Do the quantities being stored in here need derivatives
      46             :   bool hasderiv;
      47             : /// What is the maximum number of vectors we are going to
      48             : /// have to store when using lowmem option
      49             :   unsigned max_lowmem_stash;
      50             : /// The size of the vector we are computing
      51             :   std::size_t vecsize;
      52             : /// The amount of data per vector element
      53             :   std::size_t nspace;
      54             : /// The currently active values
      55             : //  std::vector<unsigned> active_val;
      56             : /// The active derivative elements
      57             :   std::vector<unsigned> active_der;
      58             : /// The buffer
      59             :   std::vector<double> local_buffer;
      60             : /// The actions that are going to use the stored data
      61             :   std::vector<ActionWithVessel*> userActions;
      62             : /// We create a vector of tempory MultiValues here so as to avoid
      63             : /// lots of vector resizing
      64             :   unsigned tmp_index;
      65             :   std::vector<MultiValue> my_tmp_vals;
      66             : protected:
      67             : /// Is the weight differentiable
      68             :   bool weightHasDerivatives();
      69             : /// Are we using low mem option
      70             :   bool usingLowMem();
      71             : /// Finish the setup of the storage object by setting how much
      72             : /// data has to be stored
      73             :   void completeSetup( const unsigned&, const unsigned& );
      74             : /// Return value of nspace
      75             :   unsigned getNumberOfDerivativeSpacesPerComponent() const ;
      76             : /// Retrieve the values from the underlying ActionWithVessel
      77             :   void storeValues( const unsigned&, MultiValue&, std::vector<double>& ) const ;
      78             : /// This stores the data we get from the calculation
      79             :   void storeDerivatives( const unsigned&, MultiValue& myvals, std::vector<double>&, std::vector<unsigned>& ) const ;
      80             : /// Get the ibuf'th local derivative value
      81             :   double getLocalDerivative( const unsigned& ibuf );
      82             : /// Set the ibuf'th local derivative value
      83             :   void setLocalDerivative( const unsigned& ibuf, const double& val );
      84             : public:
      85             :   static void registerKeywords( Keywords& keys );
      86             :   explicit StoreDataVessel( const VesselOptions& );
      87             : /// Get the number of values that have been stored
      88             :   virtual unsigned getNumberOfStoredValues() const ;
      89             : /// Get the index to store a particular index inside
      90             :   unsigned getStoreIndex( const unsigned& ) const ;
      91             : /// Get the true index of a quantity from the index it is stored in
      92             :   unsigned getTrueIndex( const unsigned& ) const ;
      93             : /// Recalculate one of the base quantities
      94             :   void recalculateStoredQuantity( const unsigned& myelm, MultiValue& myvals );
      95             : /// Set a hard cutoff on the weight of an element
      96             :   void setHardCutoffOnWeight( const double& mytol );
      97             : /// Add an action that uses this data
      98             :   void addActionThatUses( ActionWithVessel* actionThatUses );
      99             : /// Return the number of components in the vector
     100             :   unsigned getNumberOfComponents() const {
     101        2094 :     return vecsize;
     102             :   }
     103             : /// Get the values of all the components in the vector
     104             :   void retrieveSequentialValue( const unsigned& myelem, const bool& normed, std::vector<double>& values ) const ;
     105             :   void retrieveValueWithIndex( const unsigned& myelem, const bool& normed, std::vector<double>& values ) const ;
     106             :   double retrieveWeightWithIndex( const unsigned& myelem ) const ;
     107             : /// Get the derivatives for one of the components in the vector
     108             :   void retrieveDerivatives( const unsigned& myelem, const bool& normed, MultiValue& myvals );
     109             : /// Do all resizing of data
     110             :   void resize() override;
     111             : ///
     112         128 :   std::string description() override {
     113         128 :     return "";
     114             :   }
     115             : /// Get the number of derivatives for the ith value
     116             :   unsigned getNumberOfDerivatives( const unsigned& );
     117             : /// Get the size of the derivative list
     118             :   unsigned getSizeOfDerivativeList() const ;
     119             : /// This stores the data when not using lowmem
     120             :   void calculate( const unsigned& current, MultiValue& myvals, std::vector<double>& buffer, std::vector<unsigned>& der_index ) const override;
     121             : /// Final step in gathering data
     122             :   void finish( const std::vector<double>& buffer ) override;
     123             : /// Is a particular stored value active at the present time
     124             :   bool storedValueIsActive( const unsigned& iatom ) const ;
     125             : /// Set the active values
     126             :   void setActiveValsAndDerivatives( const std::vector<unsigned>& der_index );
     127             : /// Activate indexes (this is used at end of chain rule)
     128           0 :   virtual void activateIndices( ActionWithVessel* ) {}
     129             : /// Forces on vectors should always be applied elsewhere
     130         958 :   bool applyForce(std::vector<double>&) override {
     131         958 :     return false;
     132             :   }
     133             : ///  Get the number of data users
     134             :   unsigned getNumberOfDataUsers() const ;
     135             : /// Get one of the ith data user
     136             :   ActionWithVessel* getDataUser( const unsigned& );
     137             : /// Set the number of tempory multivalues we need
     138             :   void resizeTemporyMultiValues( const unsigned& nvals );
     139             : /// Return a tempory multi value - we do this so as to avoid vector resizing
     140             :   MultiValue& getTemporyMultiValue( const unsigned& ind );
     141             : };
     142             : 
     143             : inline
     144             : bool StoreDataVessel::weightHasDerivatives() {
     145             :   return getAction()->weightHasDerivatives;
     146             : }
     147             : 
     148             : inline
     149             : bool StoreDataVessel::usingLowMem() {
     150             :   return getAction()->lowmem;
     151             : }
     152             : 
     153             : inline
     154             : unsigned StoreDataVessel::getNumberOfDerivativeSpacesPerComponent() const {
     155             :   return nspace;
     156             : }
     157             : 
     158             : inline
     159       65536 : bool StoreDataVessel::storedValueIsActive( const unsigned& iatom ) const {
     160       65536 :   if( !getAction()->taskIsCurrentlyActive( iatom ) ) {
     161             :     return false;
     162             :   }
     163       53771 :   unsigned jatom = getStoreIndex( iatom );
     164             :   plumed_dbg_assert( jatom<getNumberOfStoredValues() );
     165       53771 :   return local_buffer[jatom*vecsize*nspace]>epsilon;
     166             : }
     167             : 
     168             : inline
     169             : unsigned StoreDataVessel::getSizeOfDerivativeList() const {
     170             :   return active_der.size();
     171             : }
     172             : 
     173             : inline
     174      632197 : unsigned StoreDataVessel::getNumberOfStoredValues() const {
     175      632197 :   return getAction()->nactive_tasks;
     176             : }
     177             : 
     178             : inline
     179     1558477 : unsigned StoreDataVessel::getStoreIndex( const unsigned& ind ) const {
     180     1558477 :   if( getAction()->nactive_tasks==getAction()->getFullNumberOfTasks() ) {
     181     1494318 :     return ind;
     182             :   }
     183             : 
     184             :   // Binary search for required element - faster scaling than sequential search
     185       64159 :   unsigned l=0, r=getAction()->nactive_tasks-1;
     186      696300 :   for(unsigned i=0; i<getAction()->nactive_tasks; ++i) {
     187      696300 :     plumed_assert( l<=r );
     188      696300 :     unsigned m = std::floor( (l + r)/2 );
     189      696300 :     if( ind==getAction()->indexOfTaskInFullList[m] ) {
     190             :       return m;
     191      632141 :     } else if( getAction()->indexOfTaskInFullList[m]<ind ) {
     192      330041 :       l=m+1;
     193      302100 :     } else if( getAction()->indexOfTaskInFullList[m]>ind ) {
     194      302100 :       r=m-1;
     195             :     }
     196             :   }
     197           0 :   plumed_merror("requested task is not active");
     198             : }
     199             : 
     200             : inline
     201             : unsigned StoreDataVessel::getTrueIndex( const unsigned& ind ) const {
     202         300 :   return getAction()->indexOfTaskInFullList[ind];
     203             : }
     204             : 
     205             : inline
     206      103064 : void StoreDataVessel::recalculateStoredQuantity( const unsigned& myelem, MultiValue& myvals ) {
     207      103064 :   getAction()->performTask( myelem, getAction()->getTaskCode(myelem), myvals );
     208      103064 : }
     209             : 
     210             : inline
     211             : unsigned StoreDataVessel::getNumberOfDataUsers() const {
     212         225 :   return userActions.size();
     213             : }
     214             : 
     215             : inline
     216             : ActionWithVessel* StoreDataVessel::getDataUser( const unsigned& idata ) {
     217             :   plumed_dbg_assert( idata<userActions.size() );
     218          49 :   return userActions[idata];
     219             : }
     220             : 
     221             : }
     222             : }
     223             : #endif
     224             : 
     225             : 

Generated by: LCOV version 1.16