LCOV - code coverage report
Current view: top level - gridtools - GridVessel.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 304 322 94.4 %
Date: 2020-11-18 11:20:57 Functions: 37 40 92.5 %

          Line data    Source code
       1             : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       2             :    Copyright (c) 2015-2019 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 "GridVessel.h"
      23             : #include "vesselbase/ActionWithVessel.h"
      24             : #include "tools/Random.h"
      25             : #include "tools/Tools.h"
      26             : 
      27             : namespace PLMD {
      28             : namespace gridtools {
      29             : 
      30          47 : void GridVessel::registerKeywords( Keywords& keys ) {
      31          47 :   AveragingVessel::registerKeywords( keys );
      32         235 :   keys.add("compulsory","TYPE","flat","how the grid points are being generated");
      33         188 :   keys.add("compulsory","COMPONENTS","the names of the components in the vector");
      34         188 :   keys.add("compulsory","COORDINATES","the names of the coordinates of the grid");
      35         188 :   keys.add("compulsory","PBC","is the grid periodic in each direction or not");
      36          47 : }
      37             : 
      38          47 : GridVessel::GridVessel( const vesselbase::VesselOptions& da ):
      39             :   AveragingVessel(da),
      40             :   bounds_set(false),
      41             :   cube_units(1.0),
      42             :   noderiv(false),
      43             :   npoints(0),
      44         517 :   wasforced(false)
      45             : {
      46          94 :   std::string geom; parse("TYPE",geom);
      47          47 :   if( geom=="flat" ) gtype=flat;
      48           3 :   else if( geom=="fibonacci" ) gtype=fibonacci;
      49           0 :   else plumed_merror( geom + " is invalid geometry type");
      50         141 :   std::vector<std::string> compnames; parseVector("COMPONENTS",compnames);
      51         141 :   std::vector<std::string> coordnames; parseVector("COORDINATES",coordnames);
      52          47 :   if( gtype==flat ) {
      53          44 :     dimension=coordnames.size();
      54          44 :     str_min.resize( dimension);  str_max.resize( dimension ); stride.resize( dimension );
      55          44 :     max.resize( dimension ); dx.resize( dimension ); nbin.resize( dimension ); min.resize( dimension );
      56           3 :   } else if( gtype==fibonacci ) {
      57           3 :     if( coordnames.size()!=3 ) error("cannot generate fibonacci grid points on surface of sphere if not 3 input coordinates");
      58           3 :     dimension=3;
      59             :   }
      60             : 
      61          94 :   unsigned n=0; nper=compnames.size()*( 1 + coordnames.size() );
      62          47 :   arg_names.resize( coordnames.size() + compnames.size()*( 1 + coordnames.size() ) );
      63         325 :   for(unsigned i=0; i<coordnames.size(); ++i) { arg_names[n] = coordnames[i]; n++; }
      64         235 :   for(unsigned i=0; i<compnames.size(); ++i) {
      65          94 :     arg_names[n]=compnames[i]; n++;
      66         633 :     for(unsigned j=0; j<coordnames.size(); ++j) { arg_names[n] = "d" + compnames[i] + "_" + coordnames[j]; n++; }
      67             :   }
      68          47 :   pbc.resize( dimension );
      69         141 :   std::vector<std::string> spbc( dimension ); parseVector("PBC",spbc);
      70         201 :   for(unsigned i=0; i<dimension; ++i) {
      71         154 :     if( spbc[i]=="F" ) pbc[i]=false;
      72          28 :     else if( spbc[i]=="T" ) pbc[i]=true;
      73           0 :     else plumed_error();
      74             :   }
      75          47 : }
      76             : 
      77           7 : void GridVessel::setNoDerivatives() {
      78           7 :   nper = ( nper/(1+dimension) ); noderiv=true;
      79          14 :   std::vector<std::string> tnames( dimension ), cnames(nper);
      80          27 :   for(unsigned i=0; i<dimension; ++i) tnames[i]=arg_names[i];
      81          28 :   unsigned k=dimension; for(unsigned i=0; i<nper; ++i) { cnames[i]=arg_names[k]; k+=(1+dimension); }
      82           7 :   arg_names.resize( dimension + nper );
      83          27 :   for(unsigned i=0; i<dimension; ++i) arg_names[i]=tnames[i];
      84          28 :   for(unsigned i=0; i<nper; ++i) arg_names[dimension+i]=cnames[i];
      85           7 : }
      86             : 
      87          70 : void GridVessel::setBounds( const std::vector<std::string>& smin, const std::vector<std::string>& smax,
      88             :                             const std::vector<unsigned>& binsin, const std::vector<double>& spacing ) {
      89             :   plumed_dbg_assert( smin.size()==dimension && smax.size()==dimension );
      90         193 :   plumed_assert( gtype==flat && (spacing.size()==dimension || binsin.size()==dimension) );
      91             : 
      92          70 :   npoints=1; bounds_set=true;
      93         296 :   for(unsigned i=0; i<dimension; ++i) {
      94         113 :     str_min[i]=smin[i]; str_max[i]=smax[i];
      95         113 :     Tools::convert( str_min[i], min[i] );
      96         113 :     Tools::convert( str_max[i], max[i] );
      97         143 :     if( spacing.size()==dimension && binsin.size()==dimension ) {
      98          32 :       if( spacing[i]==0 ) nbin[i] = binsin[i];
      99             :       else {
     100          84 :         double range = max[i] - min[i]; nbin[i] = std::round( range / spacing[i]);
     101             :         // This check ensures that nbins is set correctly if spacing is set the same as the number of bins
     102          56 :         if( nbin[i]!=binsin[i] ) plumed_merror("mismatch between input spacing and input number of bins");
     103             :       }
     104         166 :     } else if( binsin.size()==dimension ) nbin[i]=binsin[i];
     105           0 :     else if( spacing.size()==dimension ) nbin[i] = std::floor(( max[i] - min[i] ) / spacing[i]) + 1;
     106           0 :     else plumed_error();
     107         452 :     dx[i] = ( max[i] - min[i] ) / static_cast<double>( nbin[i] );
     108         314 :     if( !pbc[i] ) { max[i] +=dx[i]; nbin[i]+=1; }
     109         113 :     stride[i]=npoints;
     110         113 :     npoints*=nbin[i];
     111             :   }
     112          70 :   resize();  // Always resize after setting new bounds as grid size may have have changed
     113          70 : }
     114             : 
     115           3 : void GridVessel::setupFibonacciGrid( const unsigned& np ) {
     116           3 :   bounds_set=true; root5 = sqrt(5);
     117           3 :   npoints = np; golden = ( 1 + sqrt(5) ) / 2.0; igolden = golden - 1;
     118           3 :   fib_increment = 2*pi*igolden; log_golden2 = std::log( golden*golden );
     119           3 :   fib_offset = 2 / static_cast<double>( npoints );
     120           3 :   fib_shift = fib_offset/2 - 1;
     121           3 :   resize();
     122             : 
     123           3 :   std::vector<double> icoord( dimension ), jcoord( dimension );
     124             :   // Find minimum distance between each pair of points
     125           3 :   std::vector<double> mindists( npoints );
     126         347 :   for(unsigned i=0; i<npoints; ++i) {
     127         688 :     getFibonacciCoordinates( i, icoord ); mindists[i] = 0;
     128       41080 :     for(unsigned j=0; j<npoints; ++j) {
     129       40736 :       if( i==j ) continue ; // Points are not neighbors to themselves
     130       40392 :       getFibonacciCoordinates( j, jcoord );
     131             :       // Calculate the dot product
     132      403920 :       double dot=0; for(unsigned k=0; k<dimension; ++k) dot += icoord[k]*jcoord[k];
     133       80784 :       if( dot>mindists[i] ) mindists[i]=dot;
     134             :     }
     135             :   }
     136             :   // And now take minimum of dot products
     137           3 :   double min=mindists[0];
     138         685 :   for(unsigned i=1; i<npoints; ++i) {
     139         682 :     if( mindists[i]<min ) min=mindists[i];
     140             :   }
     141             :   double final_cutoff;
     142           3 :   if( getFibonacciCutoff()<-1 ) final_cutoff=-1;
     143           2 :   else final_cutoff = cos( acos( getFibonacciCutoff() ) + acos( min ) );
     144             : 
     145             :   // And now construct the neighbor list
     146           3 :   fib_nlist.resize( npoints );
     147         347 :   for(unsigned i=0; i<npoints; ++i) {
     148         344 :     getFibonacciCoordinates( i, icoord );
     149       41080 :     for(unsigned j=0; j<npoints; ++j) {
     150       40736 :       if( i==j ) continue ; // Points are not neighbors to themselves
     151       40392 :       getFibonacciCoordinates( j, jcoord );
     152             :       // Calculate the dot product
     153      403920 :       double dot=0; for(unsigned k=0; k<dimension; ++k) dot += icoord[k]*jcoord[k];
     154       63278 :       if( dot>final_cutoff ) { fib_nlist[i].push_back(j); }
     155             :     }
     156             :   }
     157           3 : }
     158             : 
     159          47 : std::string GridVessel::description() {
     160          47 :   if( !bounds_set ) return "";
     161             : 
     162             :   std::string des;
     163          34 :   if( gtype==flat ) {
     164             :     des="grid of "; std::string num;
     165          66 :     for(unsigned i=0; i<dimension-1; ++i) {
     166          34 :       Tools::convert( nbin[i], num );
     167          34 :       des += num + " X ";
     168             :     }
     169          64 :     Tools::convert( nbin[dimension-1], num );
     170          64 :     des += num + " equally spaced points between (";
     171          83 :     for(unsigned i=0; i<dimension-1; ++i) des += str_min[i] + ",";
     172          64 :     Tools::convert( nbin[dimension-1], num );
     173          96 :     des += str_min[dimension-1] + ") and (";
     174          83 :     for(unsigned i=0; i<dimension-1; ++i) des += str_max[i] + ",";
     175          96 :     des += str_max[dimension-1] + ")";
     176           2 :   } else if( gtype==fibonacci ) {
     177           2 :     std::string num; Tools::convert( npoints, num );
     178           6 :     des += "fibonacci grid of " + num + " points on spherical surface";
     179             :   }
     180             :   return des;
     181             : }
     182             : 
     183         165 : void GridVessel::resize() {
     184         165 :   plumed_massert( nper>0, "Number of datapoints at each grid point has not been set");
     185         330 :   resizeBuffer( getNumberOfBufferPoints()*nper + 1 + 2*getAction()->getNumberOfDerivatives() );
     186         165 :   setDataSize( npoints*nper ); forces.resize( npoints );
     187         165 :   if( active.size()!=npoints) active.resize( npoints, true );
     188         165 : }
     189             : 
     190    21249266 : unsigned GridVessel::getIndex( const std::vector<unsigned>& indices ) const {
     191             :   plumed_dbg_assert( gtype==flat && bounds_set && indices.size()==dimension );
     192             :   // indices are flattended using a column-major order
     193    42498532 :   unsigned index=indices[dimension-1];
     194   106193892 :   for(unsigned i=dimension-1; i>0; --i) {
     195   127416939 :     index=index*nbin[i-1]+indices[i-1];
     196             :   }
     197    21249266 :   return index;
     198             : }
     199             : 
     200       17885 : void GridVessel::getIndices( const std::vector<double>& point, std::vector<unsigned>& indices ) const {
     201             :   plumed_dbg_assert( gtype==flat && bounds_set && point.size()==dimension && indices.size()==dimension );
     202      123575 :   for(unsigned i=0; i<dimension; ++i) {
     203      264225 :     indices[i]=std::floor( (point[i] - min[i])/dx[i] );
     204      143897 :     if( pbc[i] ) indices[i]=indices[i]%nbin[i];
     205       14638 :     else if( indices[i]>nbin[i] ) plumed_merror("point is outside grid range");
     206             :   }
     207       17885 : }
     208             : 
     209        8651 : unsigned GridVessel::getIndex( const std::vector<double>& point ) const {
     210             :   plumed_dbg_assert( gtype==flat && bounds_set && point.size()==dimension );
     211        8651 :   if( gtype==flat ) {
     212        8651 :     std::vector<unsigned> indices(dimension); getIndices( point, indices );
     213        8651 :     return getIndex( indices );
     214           0 :   } else if( gtype==fibonacci ) {
     215           0 :     return getFibonacciIndex( point );
     216             :   } else {
     217           0 :     plumed_error();
     218             :   }
     219             : }
     220             : 
     221          57 : unsigned GridVessel::getFibonacciIndex( const std::vector<double>& p ) const {
     222             :   plumed_dbg_assert( gtype==fibonacci );
     223             :   // Convert input point to coordinates on cylinder
     224         114 :   int k=2; double phi = atan2( p[2], p[0] ), sinthet2 = 1 - p[1]*p[1];
     225             :   // Calculate power to raise golden ratio
     226          57 :   if( sinthet2<epsilon ) { k = 2; }
     227             :   else {
     228          57 :     k = std::floor( std::log( npoints*pi*root5*sinthet2 ) / log_golden2 );
     229          57 :     if( k<2 ) k = 2;
     230             :   }
     231          57 :   double Fk = pow( golden, k ) / root5, F0 = std::round(Fk), F1 = std::round(Fk*golden);
     232          57 :   Matrix<double> B(2,2), invB(2,2); std::vector<double> thisp(3);
     233         114 :   B(0,0) = 2*pi*((F0+1)*igolden - std::floor((F0+1)*igolden)) - fib_increment;
     234         114 :   B(0,1) = 2*pi*((F1+1)*igolden - std::floor((F1+1)*igolden)) - fib_increment;
     235         171 :   B(1,0) = -2*F0/npoints; B(1,1) = -2*F1/npoints; Invert( B, invB );
     236         228 :   std::vector<double> vv(2), rc(2); vv[0]=-phi; vv[1] = p[1] - fib_shift;
     237         285 :   mult( invB, vv, rc ); std::vector<int> c(2); c[0]=std::floor(rc[0]); c[1]=std::floor(rc[1]);
     238             :   unsigned outind; double mindist = 10000000.;
     239         513 :   for(int s=0; s<4; ++s) {
     240         684 :     double ttt, costheta = B(1,0)*( c[0] + s%2 ) + B(1,1)*( c[1] + s/2 ) + fib_shift;
     241         228 :     if( costheta>1 ) ttt=1; else if( costheta<-1 ) ttt=-1; else ttt=costheta;
     242         228 :     costheta = 2*ttt - costheta;
     243         228 :     unsigned i = std::floor( 0.5*npoints*(1+costheta) ); getFibonacciCoordinates( i, thisp );
     244        2280 :     double dist=0; for(unsigned j=0; j<3; ++j) { double tmp=thisp[j]-p[j]; dist += tmp*tmp; }
     245         228 :     if( dist<mindist ) { outind = i; mindist = dist; }
     246             :   }
     247          57 :   return outind;
     248             : }
     249             : 
     250   108707058 : void GridVessel::convertIndexToIndices( const unsigned& index, const std::vector<unsigned>& nnbin, std::vector<unsigned>& indices ) const {
     251   326121174 :   plumed_dbg_assert( gtype==flat ); unsigned kk=index; indices[0]=index%nnbin[0];
     252   325666464 :   for(unsigned i=1; i<dimension-1; ++i) {
     253   325439109 :     kk=(kk-indices[i-1])/nnbin[i-1];
     254   325439109 :     indices[i]=kk%nnbin[i];
     255             :   }
     256   108707058 :   if(dimension>=2) { // I think this is wrong
     257   434768160 :     indices[dimension-1]=(kk-indices[dimension-2])/nnbin[dimension-2];
     258             :   }
     259   108707058 : }
     260             : 
     261    12161812 : void GridVessel::getIndices( const unsigned& index, std::vector<unsigned>& indices ) const {
     262    12161812 :   plumed_dbg_assert( gtype==flat ); convertIndexToIndices( index, nbin, indices );
     263    12161812 : }
     264             : 
     265      107408 : void GridVessel::getGridPointCoordinates( const unsigned& ipoint, std::vector<double>& x ) const {
     266      107408 :   std::vector<unsigned> tindices( dimension ); getGridPointCoordinates( ipoint, tindices, x );
     267      107408 : }
     268             : 
     269    11954882 : void GridVessel::getGridPointCoordinates( const unsigned& ipoint, std::vector<unsigned>& tindices, std::vector<double>& x ) const {
     270             :   plumed_dbg_assert( bounds_set && x.size()==dimension && tindices.size()==dimension && ipoint<npoints );
     271    11954882 :   if( gtype==flat ) {
     272    11949765 :     getFlatGridCoordinates( ipoint, tindices, x );
     273        5117 :   } else if( gtype==fibonacci ) {
     274        5117 :     getFibonacciCoordinates( ipoint, x );
     275             :   } else {
     276           0 :     plumed_error();
     277             :   }
     278    11954882 : }
     279             : 
     280    11958411 : void GridVessel::getFlatGridCoordinates( const unsigned& ipoint, std::vector<unsigned>& tindices, std::vector<double>& x ) const {
     281    11958411 :   plumed_dbg_assert( gtype==flat ); getIndices( ipoint, tindices );
     282   190731351 :   for(unsigned i=0; i<dimension; ++i) x[i] = min[i] + dx[i]*tindices[i];
     283    11958411 : }
     284             : 
     285       86817 : void GridVessel::getFibonacciCoordinates( const unsigned& ipoint, std::vector<double>& x ) const {
     286             :   plumed_dbg_assert( gtype==fibonacci );
     287      260451 :   x[1] = (ipoint*fib_offset) + fib_shift; double r = sqrt( 1 - x[1]*x[1] );
     288      260451 :   double phi = ipoint*fib_increment; x[0] = r*cos(phi); x[2] = r*sin(phi);
     289      607719 :   double norm=0; for(unsigned j=0; j<3; ++j) norm+=x[j]*x[j];
     290      347268 :   norm = sqrt(norm); for(unsigned j=0; j<3; ++j) x[j] = x[j] / norm;
     291       86817 : }
     292             : 
     293        8646 : void GridVessel::getSplineNeighbors( const unsigned& mybox, std::vector<unsigned>& mysneigh ) const {
     294        8646 :   plumed_dbg_assert( gtype==flat ); mysneigh.resize( static_cast<unsigned>(pow(2.,dimension)) );
     295             : 
     296        8646 :   std::vector<unsigned> tmp_indices( dimension );
     297        8646 :   std::vector<unsigned> my_indices( dimension );
     298        8646 :   getIndices( mybox, my_indices );
     299      221196 :   for(unsigned i=0; i<mysneigh.size(); ++i) {
     300             :     unsigned tmp=i;
     301      474176 :     for(unsigned j=0; j<dimension; ++j) {
     302      406208 :       unsigned i0=tmp%2+my_indices[j]; tmp/=2;
     303      230008 :       if(!pbc[j] && i0==nbin[j]) getAction()->error("Extrapolating function on grid");
     304      379304 :       if( pbc[j] && i0==nbin[j]) i0=0;
     305      203104 :       tmp_indices[j]=i0;
     306             :     }
     307       67968 :     mysneigh[i]=getIndex( tmp_indices );
     308      135936 :     plumed_massert( active[mysneigh[i]], "inactive grid point required for splines");
     309             :   }
     310        8646 : }
     311             : 
     312      314678 : double GridVessel::getGridElement( const unsigned& ipoint, const unsigned& jelement ) const {
     313      629356 :   plumed_assert( bounds_set && ipoint<npoints && jelement<nper && active[ipoint] );
     314      629356 :   return getDataElement( nper*ipoint + jelement  );
     315             : }
     316             : 
     317           0 : void GridVessel::setGridElement( const unsigned& ipoint, const unsigned& jelement, const double& value ) {
     318             :   plumed_dbg_assert( bounds_set && ipoint<npoints && jelement<nper );
     319           0 :   setDataElement( nper*ipoint + jelement, value );
     320           0 : }
     321             : 
     322           5 : void GridVessel::addToGridElement( const unsigned& ipoint, const unsigned& jelement, const double& value ) {
     323             :   plumed_dbg_assert( bounds_set && ipoint<npoints && jelement<nper );
     324           5 :   addDataElement( nper*ipoint + jelement, value );
     325           5 : }
     326             : 
     327       12181 : void GridVessel::calculate( const unsigned& current, MultiValue& myvals, std::vector<double>& buffer, std::vector<unsigned>& der_list ) const {
     328             :   plumed_dbg_assert( myvals.getNumberOfValues()==(nper+1) );
     329      138316 :   for(unsigned i=0; i<nper; ++i) buffer[bufstart + nper*current + i] += myvals.get(i+1);
     330       12181 : }
     331             : 
     332          99 : void GridVessel::finish( const std::vector<double>& buffer ) {
     333          99 :   if( wasforced ) getFinalForces( buffer, finalForces );
     334          79 :   else AveragingVessel::finish( buffer );
     335          99 : }
     336             : 
     337           0 : double GridVessel::getGridElement( const std::vector<unsigned>& indices, const unsigned& jelement ) const {
     338           0 :   return getGridElement( getIndex( indices ), jelement );
     339             : }
     340             : 
     341           0 : void GridVessel::setGridElement( const std::vector<unsigned>& indices, const unsigned& jelement, const double& value ) {
     342           0 :   setGridElement( getIndex( indices ), jelement, value );
     343           0 : }
     344             : 
     345      176647 : std::vector<std::string> GridVessel::getMin() const {
     346      176647 :   plumed_dbg_assert( gtype==flat ); return str_min;
     347             : }
     348             : 
     349      176680 : std::vector<std::string> GridVessel::getMax() const {
     350      176680 :   plumed_dbg_assert( gtype==flat ); return str_max;
     351             : }
     352             : 
     353      177267 : std::vector<unsigned> GridVessel::getNbin() const {
     354             :   plumed_dbg_assert( gtype==flat && bounds_set );
     355      177267 :   std::vector<unsigned> ngrid( dimension );
     356      879125 :   for(unsigned i=0; i<dimension; ++i) {
     357     1355528 :     if( !pbc[i] ) ngrid[i]=nbin[i] - 1;
     358       24094 :     else ngrid[i]=nbin[i];
     359             :   }
     360      177267 :   return ngrid;
     361             : }
     362             : 
     363       21470 : void GridVessel::getNeighbors( const std::vector<double>& pp, const std::vector<unsigned>& nneigh,
     364             :                                unsigned& num_neighbors, std::vector<unsigned>& neighbors ) const {
     365             :   plumed_dbg_assert( bounds_set );
     366             : 
     367       21470 :   if( gtype == flat ) {
     368             :     plumed_dbg_assert( nneigh.size()==dimension );
     369       21413 :     std::vector<unsigned> indices( dimension );
     370      340408 :     for(unsigned i=0; i<dimension; ++i) indices[i] = std::floor( (pp[i]-min[i])/dx[i] );
     371       21413 :     getNeighbors( indices, nneigh, num_neighbors, neighbors );
     372          57 :   } else if( gtype == fibonacci ) {
     373          57 :     unsigned find = getFibonacciIndex( pp );
     374         114 :     num_neighbors = 1 + fib_nlist[find].size();
     375          57 :     if( neighbors.size()<num_neighbors ) neighbors.resize( num_neighbors );
     376       14262 :     neighbors[0]=find; for(unsigned i=0; i<fib_nlist[find].size(); ++i) neighbors[1+i] = fib_nlist[find][i];
     377             :   } else {
     378           0 :     plumed_error();
     379             :   }
     380       21470 : }
     381             : 
     382       40834 : void GridVessel::getNeighbors( const std::vector<unsigned>& indices, const std::vector<unsigned>& nneigh,
     383             :                                unsigned& num_neighbors, std::vector<unsigned>& neighbors ) const {
     384             :   plumed_dbg_assert( gtype==flat && bounds_set && nneigh.size()==dimension );
     385             : 
     386       40834 :   unsigned num_neigh=1; std::vector<unsigned> small_bin( dimension );
     387      284958 :   for(unsigned i=0; i<dimension; ++i) {
     388      366186 :     small_bin[i]=(2*nneigh[i]+1);
     389      122062 :     num_neigh *=small_bin[i];
     390             :   }
     391       40834 :   if( neighbors.size()!=num_neigh ) neighbors.resize( num_neigh );
     392             : 
     393       40834 :   num_neighbors=0;
     394       40834 :   std::vector<unsigned> s_indices(dimension), t_indices(dimension);
     395    96586080 :   for(unsigned index=0; index<num_neigh; ++index) {
     396             :     bool found=true;
     397    96545246 :     convertIndexToIndices( index, small_bin, s_indices );
     398   675766530 :     for(unsigned i=0; i<dimension; ++i) {
     399  1158442568 :       int i0=s_indices[i]-nneigh[i]+indices[i];
     400   289610642 :       if(!pbc[i] && i0<0)        found=false;
     401   382200910 :       if(!pbc[i] && i0>=nbin[i]) found=false;
     402   304666132 :       if( pbc[i] && i0<0)        i0=nbin[i]-(-i0)%nbin[i];
     403   486631016 :       if( pbc[i] && i0>=nbin[i]) i0%=nbin[i];
     404   289610642 :       t_indices[i]=static_cast<unsigned>(i0);
     405             :     }
     406    96545246 :     if( found ) {
     407    42184778 :       neighbors[num_neighbors]=getIndex( t_indices );
     408    21092389 :       num_neighbors++;
     409             :     }
     410             :   }
     411       40834 : }
     412             : 
     413          11 : void GridVessel::setCubeUnits( const double& units ) {
     414          11 :   plumed_dbg_assert( gtype==flat ); cube_units=units;
     415          11 : }
     416             : 
     417           8 : double GridVessel::getCubeUnits() const {
     418           8 :   plumed_dbg_assert( gtype==flat ); return cube_units;
     419             : }
     420             : 
     421           9 : std::string GridVessel::getInputString() const {
     422           9 :   std::string mstring="COORDINATES="+arg_names[0];
     423          24 :   for(unsigned i=1; i<dimension; ++i) mstring+="," + arg_names[i];
     424           9 :   if( gtype==flat ) {
     425             :     mstring += " TYPE=flat PBC=";
     426           9 :     if( pbc[0] ) mstring +="T";
     427             :     else mstring +="F";
     428          19 :     for(unsigned i=1; i<dimension; ++i) {
     429          10 :       if( pbc[i] ) mstring +=",T";
     430             :       else mstring +=",F";
     431             :     }
     432           0 :   } else if( gtype==fibonacci ) {
     433             :     mstring += " TYPE=fibonacci";
     434             :   }
     435           9 :   return mstring;
     436             : }
     437             : 
     438        8646 : double GridVessel::getValueAndDerivatives( const std::vector<double>& x, const unsigned& ind, std::vector<double>& der ) const {
     439             :   plumed_dbg_assert( gtype==flat && der.size()==dimension && !noderiv && ind<getNumberOfComponents() );
     440             : 
     441       17292 :   double X,X2,X3,value=0; der.assign(der.size(),0.0);
     442        8646 :   std::vector<double> fd(dimension);
     443        8646 :   std::vector<double> C(dimension);
     444        8646 :   std::vector<double> D(dimension);
     445        8646 :   std::vector<double> dder(dimension);
     446             : 
     447        8646 :   std::vector<unsigned> nindices(dimension);
     448        8646 :   std::vector<unsigned> indices(dimension); getIndices( x, indices );
     449        8646 :   std::vector<unsigned> neigh; getSplineNeighbors( getIndex(indices), neigh );
     450        8646 :   std::vector<double> xfloor(dimension); getFlatGridCoordinates( getIndex(x), nindices, xfloor );
     451             : 
     452             : // loop over neighbors
     453      221196 :   for(unsigned int ipoint=0; ipoint<neigh.size(); ++ipoint) {
     454      135936 :     double grid=getGridElement(neigh[ipoint], ind*(1+dimension) );
     455      677280 :     for(unsigned j=0; j<dimension; ++j) dder[j] = getGridElement( neigh[ipoint], ind*(1+dimension) + 1 + j );
     456             : 
     457       67968 :     getIndices( neigh[ipoint], nindices );
     458             :     double ff=1.0;
     459             : 
     460      474176 :     for(unsigned j=0; j<dimension; ++j) {
     461             :       int x0=1;
     462      609312 :       if(nindices[j]==indices[j]) x0=0;
     463      203104 :       double ddx=dx[j];
     464      406208 :       X=fabs((x[j]-xfloor[j])/ddx-(double)x0);
     465      203104 :       X2=X*X;
     466      203104 :       X3=X2*X;
     467             :       double yy;
     468      203104 :       if(fabs(grid)<0.0000001) yy=0.0;
     469      189257 :       else yy=-dder[j]/grid;
     470      406208 :       C[j]=(1.0-3.0*X2+2.0*X3) - (x0?-1.0:1.0)*yy*(X-2.0*X2+X3)*ddx;
     471      406208 :       D[j]=( -6.0*X +6.0*X2) - (x0?-1.0:1.0)*yy*(1.0-4.0*X +3.0*X2)*ddx;
     472      203104 :       D[j]*=(x0?-1.0:1.0)/ddx;
     473      203104 :       ff*=C[j];
     474             :     }
     475      474176 :     for(unsigned j=0; j<dimension; ++j) {
     476      406208 :       fd[j]=D[j];
     477     1622432 :       for(unsigned i=0; i<dimension; ++i) if(i!=j) fd[j]*=C[i];
     478             :     }
     479       67968 :     value+=grid*ff;
     480      677280 :     for(unsigned j=0; j<dimension; ++j) der[j]+=grid*fd[j];
     481             :   }
     482        8646 :   return value;
     483             : }
     484             : 
     485           3 : void GridVessel::activateThesePoints( const std::vector<bool>& to_activate ) {
     486             :   plumed_dbg_assert( to_activate.size()==npoints );
     487       59979 :   for(unsigned i=0; i<npoints; ++i) active[i]=to_activate[i];
     488           3 : }
     489             : 
     490          20 : void GridVessel::setForce( const std::vector<double>& inforces ) {
     491             :   plumed_dbg_assert( inforces.size()==npoints );
     492        5725 :   wasforced=true; for(unsigned i=0; i<npoints; ++i) forces[i]=inforces[i];
     493          20 : }
     494             : 
     495    11869004 : bool GridVessel::wasForced() const {
     496    11869004 :   return wasforced;
     497             : }
     498             : 
     499          20 : bool GridVessel::applyForce( std::vector<double>& fforces ) {
     500             :   plumed_dbg_assert( fforces.size()==finalForces.size() );
     501          20 :   if( !wasforced ) return false;
     502       11425 :   for(unsigned i=0; i<finalForces.size(); ++i) fforces[i]=finalForces[i];
     503          20 :   wasforced=false; return true;
     504             : }
     505             : 
     506             : }
     507        4839 : }
     508             : 

Generated by: LCOV version 1.13