LCOV - code coverage report
Current view: top level - tools - LinkCells.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 69 69 100.0 %
Date: 2018-12-19 07:49:13 Functions: 9 9 100.0 %

          Line data    Source code
       1             : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       2             :    Copyright (c) 2014-2018 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 "LinkCells.h"
      23             : #include "Communicator.h"
      24             : #include "Tools.h"
      25             : 
      26             : namespace PLMD {
      27             : 
      28         530 : LinkCells::LinkCells( Communicator& cc ) :
      29             :   comm(cc),
      30             :   cutoffwasset(false),
      31             :   link_cutoff(0.0),
      32             :   ncells(3),
      33         530 :   nstride(3)
      34             : {
      35         530 : }
      36             : 
      37         294 : void LinkCells::setCutoff( const double& lcut ) {
      38         294 :   cutoffwasset=true; link_cutoff=lcut;
      39         294 : }
      40             : 
      41           4 : double LinkCells::getCutoff() const {
      42           4 :   plumed_assert( cutoffwasset ); return link_cutoff;
      43             : }
      44             : 
      45        1520 : void LinkCells::buildCellLists( const std::vector<Vector>& pos, const std::vector<unsigned>& indices, const Pbc& pbc ) {
      46        1520 :   plumed_assert( cutoffwasset && pos.size()==indices.size() );
      47             : 
      48             :   // Must be able to check that pbcs are not nonsensical in some way?? -- GAT
      49             : 
      50             :   // Setup the pbc object by copying it from action
      51        1520 :   mypbc.setBox( pbc.getBox() );
      52             : 
      53             :   // Setup the lists
      54        1520 :   if( pos.size()!=allcells.size() ) {
      55         385 :     allcells.resize( pos.size() ); lcell_lists.resize( pos.size() );
      56             :   }
      57             : 
      58             :   {
      59             : // This is the reciprocal lattice
      60             : // notice that reciprocal.getRow(0) is a vector that is orthogonal to b and c
      61             : // This allows to use linked cells in non orthorhomic boxes
      62        1520 :     Tensor reciprocal(transpose(mypbc.getInvBox()));
      63        1520 :     ncells[0] = std::floor( 1.0/ reciprocal.getRow(0).modulo() / link_cutoff );
      64        1520 :     if( ncells[0]==0 ) ncells[0]=1;
      65        1520 :     ncells[1] = std::floor( 1.0/ reciprocal.getRow(1).modulo() / link_cutoff );
      66        1520 :     if( ncells[1]==0 ) ncells[1]=1;
      67        1520 :     ncells[2] = std::floor( 1.0/ reciprocal.getRow(2).modulo() / link_cutoff );
      68        1520 :     if( ncells[2]==0 ) ncells[2]=1;
      69             :   }
      70             :   // Setup the strides
      71        1520 :   nstride[0]=1; nstride[1]=ncells[0]; nstride[2]=ncells[0]*ncells[1];
      72             : 
      73             :   // Setup the storage for link cells
      74        1520 :   unsigned ncellstot=ncells[0]*ncells[1]*ncells[2];
      75        1520 :   if( lcell_tots.size()!=ncellstot ) {
      76         374 :     lcell_tots.resize( ncellstot ); lcell_starts.resize( ncellstot );
      77             :   }
      78             :   // Clear nlcells
      79        1520 :   for(unsigned i=0; i<ncellstot; ++i) lcell_tots[i]=0;
      80             :   // Clear allcells
      81        1520 :   allcells.assign( allcells.size(), 0 );
      82             : 
      83             :   // Find out what cell everyone is in
      84        1520 :   unsigned rank=comm.Get_rank(), size=comm.Get_size();
      85      220131 :   for(unsigned i=rank; i<pos.size(); i+=size) {
      86      218611 :     allcells[i]=findCell( pos[i] );
      87      218611 :     lcell_tots[allcells[i]]++;
      88             :   }
      89             :   // And gather all this information on every node
      90        1520 :   comm.Sum( allcells ); comm.Sum( lcell_tots );
      91             : 
      92             :   // Now prepare the link cell lists
      93        1520 :   unsigned tot=0;
      94        1520 :   for(unsigned i=0; i<lcell_tots.size(); ++i) { lcell_starts[i]=tot; tot+=lcell_tots[i]; lcell_tots[i]=0; }
      95        1520 :   plumed_assert( tot==pos.size() );
      96             : 
      97             :   // And setup the link cells properly
      98      227394 :   for(unsigned j=0; j<pos.size(); ++j) {
      99      225874 :     unsigned myind = lcell_starts[ allcells[j] ] + lcell_tots[ allcells[j] ];
     100      225874 :     lcell_lists[ myind ] = indices[j];
     101      225874 :     lcell_tots[allcells[j]]++;
     102             :   }
     103        1520 : }
     104             : 
     105             : #define LINKC_MIN(n) ((n<2)? 0 : -1)
     106             : #define LINKC_MAX(n) ((n<3)? 1 : 2)
     107             : #define LINKC_PBC(n,num) ((n<0)? num-1 : n%num )
     108             : 
     109      205779 : void LinkCells::retrieveNeighboringAtoms( const Vector& pos, unsigned& natomsper, std::vector<unsigned>& atoms ) const {
     110      205779 :   plumed_assert( natomsper==1 || natomsper==2 );  // This is really a bug. If you are trying to reuse this ask GAT for help
     111      205779 :   std::vector<unsigned> celn( findMyCell( pos ) );
     112             : 
     113      555566 :   for(int nx=LINKC_MIN(ncells[0]); nx<LINKC_MAX(ncells[0]); ++nx) {
     114      349733 :     int xval = celn[0] + nx;
     115      349742 :     xval=LINKC_PBC(xval,ncells[0])*nstride[0];
     116     1127496 :     for(int ny=LINKC_MIN(ncells[1]); ny<LINKC_MAX(ncells[1]); ++ny) {
     117      777815 :       int yval = celn[1] + ny;
     118      777829 :       yval=LINKC_PBC(yval,ncells[1])*nstride[1];
     119     2829061 :       for(int nz=LINKC_MIN(ncells[2]); nz<LINKC_MAX(ncells[2]); ++nz) {
     120     2051739 :         int zval = celn[2] + nz;
     121     2051855 :         zval=LINKC_PBC(zval,ncells[2])*nstride[2];
     122             : 
     123     2051677 :         unsigned mybox=xval+yval+zval;
     124    15997870 :         for(unsigned k=0; k<lcell_tots[mybox]; ++k) {
     125    13969732 :           unsigned myatom = lcell_lists[lcell_starts[mybox]+k];
     126    13971573 :           if( myatom!=atoms[0] ) { // Ideally would provide an option to not do this
     127    13826416 :             atoms[natomsper]=myatom;
     128    13821400 :             natomsper++;
     129             :           }
     130             :         }
     131             :       }
     132             :     }
     133      205793 :   }
     134      205826 : }
     135             : 
     136      424367 : std::vector<unsigned> LinkCells::findMyCell( const Vector& pos ) const {
     137      424367 :   Vector fpos=mypbc.realToScaled( pos ); std::vector<unsigned> celn(3);
     138     1697576 :   for(unsigned j=0; j<3; ++j) {
     139     1273128 :     celn[j] = std::floor( ( Tools::pbc(fpos[j]) + 0.5 ) * ncells[j] );
     140     1273230 :     plumed_assert( celn[j]>=0 && celn[j]<ncells[j] ); // Check that atom is in box
     141             :   }
     142      424448 :   return celn;
     143             : }
     144             : 
     145      218611 : unsigned LinkCells::findCell( const Vector& pos ) const {
     146      218611 :   std::vector<unsigned> celn( findMyCell(pos ) );
     147      218611 :   return celn[0]*nstride[0] + celn[1]*nstride[1] + celn[2]*nstride[2];
     148             : }
     149             : 
     150             : 
     151        2523 : }

Generated by: LCOV version 1.13