LCOV - code coverage report
Current view: top level - analysis - LandmarkSelectionBase.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 47 48 97.9 %
Date: 2026-03-30 13:16:06 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          30 : void LandmarkSelectionBase::registerKeywords( Keywords& keys ) {
      28          30 :   AnalysisBase::registerKeywords( keys );
      29          60 :   keys.add("compulsory","NLANDMARKS","the number of landmarks that you would like to select");
      30          60 :   keys.addFlag("NOVORONOI",false,"do not do a Voronoi analysis of the data to determine weights of final points");
      31          60 :   keys.addFlag("IGNORE_WEIGHTS",false,"ignore the weights in the underlying analysis object");
      32          30 : }
      33             : 
      34          10 : LandmarkSelectionBase::LandmarkSelectionBase( const ActionOptions& ao ):
      35             :   Action(ao),
      36             :   AnalysisBase(ao),
      37          10 :   nlandmarks(0) {
      38          20 :   if( keywords.exists("NLANDMARKS") ) {
      39          18 :     parse("NLANDMARKS",nlandmarks);
      40             :   }
      41          10 :   log.printf("  selecting %u landmark points \n",nlandmarks);
      42          10 :   lweights.resize( nlandmarks );
      43             : 
      44          10 :   parseFlag("NOVORONOI",novoronoi);
      45          18 :   if( !novoronoi && !dissimilaritiesWereSet() ) {
      46           0 :     error("cannot calculate voronoi weights without dissimilarity mesaure");
      47             :   }
      48             : 
      49          10 :   if( !novoronoi ) {
      50           8 :     log.printf("  ascribing weights to landmarks using voronoi analysis\n");
      51             :   } else {
      52           2 :     log.printf("  ascribing weights of original points to landmark\n");
      53             :   }
      54          10 : }
      55             : 
      56         291 : void LandmarkSelectionBase::selectFrame( const unsigned& iframe ) {
      57         291 :   landmark_indices.push_back( iframe );
      58         291 : }
      59             : 
      60          12 : void LandmarkSelectionBase::performAnalysis() {
      61          12 :   landmark_indices.resize(0);
      62          12 :   selectLandmarks();
      63             :   plumed_dbg_assert( nlandmarks==getNumberOfDataPoints() );
      64          12 :   if( lweights.size()!=nlandmarks ) {
      65           1 :     lweights.resize( nlandmarks );
      66             :   }
      67             : 
      68          12 :   if( !novoronoi ) {
      69           8 :     lweights.assign(lweights.size(),0.0);
      70           8 :     std::vector<unsigned> tmpass( my_input_data->getNumberOfDataPoints() );
      71           8 :     voronoiAnalysis( landmark_indices, lweights, tmpass );
      72             :   } else {
      73          18 :     for(unsigned i=0; i<nlandmarks; ++i) {
      74          14 :       lweights[i]=my_input_data->getWeight( landmark_indices[i] );
      75             :     }
      76             :   }
      77          12 : }
      78             : 
      79           8 : void LandmarkSelectionBase::voronoiAnalysis( const std::vector<unsigned>& myindices, std::vector<double>& lweights, std::vector<unsigned>& assignments ) const {
      80             :   plumed_dbg_assert( myindices.size()==lweights.size() && assignments.size()==my_input_data->getNumberOfDataPoints() );
      81           8 :   lweights.assign( lweights.size(), 0 );
      82           8 :   unsigned rank=comm.Get_rank(), size=comm.Get_size();
      83         567 :   for(unsigned i=rank; i<my_input_data->getNumberOfDataPoints(); i+=size) {
      84         559 :     assignments[i]=0;
      85         559 :     double mindist=my_input_data->getDissimilarity( i, myindices[0] );
      86      125239 :     for(unsigned j=1; j<nlandmarks; ++j) {
      87      124680 :       double dist=my_input_data->getDissimilarity( i, myindices[j] );
      88      124680 :       if( dist<mindist ) {
      89             :         mindist=dist;
      90        2590 :         assignments[i]=j;
      91             :       }
      92             :     }
      93         559 :     lweights[ assignments[i] ] += my_input_data->getWeight(i);
      94             :   }
      95           8 :   comm.Sum( &lweights[0], lweights.size() );
      96           8 :   comm.Sum( &assignments[0], assignments.size() );
      97           8 : }
      98             : 
      99             : }
     100             : }

Generated by: LCOV version 1.16