LCOV - code coverage report
Current view: top level - analysis - LandmarkSelectionBase.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 41 41 100.0 %
Date: 2024-10-11 08:09:47 Functions: 5 6 83.3 %

          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 "LandmarkSelectionBase.h"
      23             : 
      24             : namespace PLMD {
      25             : namespace analysis {
      26             : 
      27          15 : void LandmarkSelectionBase::registerKeywords( Keywords& keys ) {
      28          15 :   AnalysisBase::registerKeywords( keys );
      29          30 :   keys.add("compulsory","NLANDMARKS","the number of landmarks that you would like to select");
      30          30 :   keys.addFlag("NOVORONOI",false,"do not do a Voronoi analysis of the data to determine weights of final points");
      31          30 :   keys.addFlag("IGNORE_WEIGHTS",false,"ignore the weights in the underlying analysis object");
      32          15 : }
      33             : 
      34          10 : LandmarkSelectionBase::LandmarkSelectionBase( const ActionOptions& ao ):
      35             :   Action(ao),
      36             :   AnalysisBase(ao),
      37          10 :   nlandmarks(0)
      38             : {
      39          29 :   if( keywords.exists("NLANDMARKS") ) parse("NLANDMARKS",nlandmarks);
      40          10 :   log.printf("  selecting %u landmark points \n",nlandmarks);
      41          10 :   lweights.resize( nlandmarks );
      42             : 
      43          10 :   parseFlag("NOVORONOI",novoronoi);
      44          18 :   if( !novoronoi && !dissimilaritiesWereSet() ) error("cannot calculate voronoi weights without dissimilarity mesaure");
      45             : 
      46          10 :   if( !novoronoi ) log.printf("  ascribing weights to landmarks using voronoi analysis\n");
      47           2 :   else log.printf("  ascribing weights of original points to landmark\n");
      48          10 : }
      49             : 
      50         291 : void LandmarkSelectionBase::selectFrame( const unsigned& iframe ) {
      51         291 :   landmark_indices.push_back( iframe );
      52         291 : }
      53             : 
      54          12 : void LandmarkSelectionBase::performAnalysis() {
      55          12 :   landmark_indices.resize(0); selectLandmarks();
      56             :   plumed_dbg_assert( nlandmarks==getNumberOfDataPoints() );
      57          12 :   if( lweights.size()!=nlandmarks ) lweights.resize( nlandmarks );
      58             : 
      59          12 :   if( !novoronoi ) {
      60           8 :     lweights.assign(lweights.size(),0.0);
      61           8 :     std::vector<unsigned> tmpass( my_input_data->getNumberOfDataPoints() );
      62           8 :     voronoiAnalysis( landmark_indices, lweights, tmpass );
      63             :   } else {
      64          18 :     for(unsigned i=0; i<nlandmarks; ++i) lweights[i]=my_input_data->getWeight( landmark_indices[i] );
      65             :   }
      66          12 : }
      67             : 
      68           8 : void LandmarkSelectionBase::voronoiAnalysis( const std::vector<unsigned>& myindices, std::vector<double>& lweights, std::vector<unsigned>& assignments ) const {
      69             :   plumed_dbg_assert( myindices.size()==lweights.size() && assignments.size()==my_input_data->getNumberOfDataPoints() );
      70           8 :   lweights.assign( lweights.size(), 0 );
      71           8 :   unsigned rank=comm.Get_rank(), size=comm.Get_size();
      72         567 :   for(unsigned i=rank; i<my_input_data->getNumberOfDataPoints(); i+=size) {
      73         559 :     assignments[i]=0;
      74         559 :     double mindist=my_input_data->getDissimilarity( i, myindices[0] );
      75      125239 :     for(unsigned j=1; j<nlandmarks; ++j) {
      76      124680 :       double dist=my_input_data->getDissimilarity( i, myindices[j] );
      77      124680 :       if( dist<mindist ) { mindist=dist; assignments[i]=j; }
      78             :     }
      79         559 :     lweights[ assignments[i] ] += my_input_data->getWeight(i);
      80             :   }
      81           8 :   comm.Sum( &lweights[0], lweights.size() );
      82           8 :   comm.Sum( &assignments[0], assignments.size() );
      83           8 : }
      84             : 
      85             : }
      86             : }

Generated by: LCOV version 1.15