LCOV - code coverage report
Current view: top level - gridtools - GridVessel.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 319 338 94.4 %
Date: 2019-08-13 10:39:37 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          43 : void GridVessel::registerKeywords( Keywords& keys ) {
      31          43 :   AveragingVessel::registerKeywords( keys );
      32          43 :   keys.add("compulsory","TYPE","flat","how the grid points are being generated");
      33          43 :   keys.add("compulsory","COMPONENTS","the names of the components in the vector");
      34          43 :   keys.add("compulsory","COORDINATES","the names of the coordinates of the grid");
      35          43 :   keys.add("compulsory","PBC","is the grid periodic in each direction or not");
      36          43 : }
      37             : 
      38          43 : 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          43 :   wasforced(false)
      45             : {
      46          43 :   std::string geom; parse("TYPE",geom);
      47          43 :   if( geom=="flat" ) gtype=flat;
      48           3 :   else if( geom=="fibonacci" ) gtype=fibonacci;
      49           0 :   else plumed_merror( geom + " is invalid geometry type");
      50          86 :   std::vector<std::string> compnames; parseVector("COMPONENTS",compnames);
      51          86 :   std::vector<std::string> coordnames; parseVector("COORDINATES",coordnames);
      52          43 :   if( gtype==flat ) {
      53          40 :     dimension=coordnames.size();
      54          40 :     str_min.resize( dimension);  str_max.resize( dimension ); stride.resize( dimension );
      55          40 :     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          43 :   unsigned n=0; nper=compnames.size()*( 1 + coordnames.size() );
      62          43 :   arg_names.resize( coordnames.size() + compnames.size()*( 1 + coordnames.size() ) );
      63          43 :   for(unsigned i=0; i<coordnames.size(); ++i) { arg_names[n] = coordnames[i]; n++; }
      64          86 :   for(unsigned i=0; i<compnames.size(); ++i) {
      65          43 :     arg_names[n]=compnames[i]; n++;
      66          43 :     for(unsigned j=0; j<coordnames.size(); ++j) { arg_names[n] = "d" + compnames[i] + "_" + coordnames[j]; n++; }
      67             :   }
      68          43 :   pbc.resize( dimension );
      69          86 :   std::vector<std::string> spbc( dimension ); parseVector("PBC",spbc);
      70         114 :   for(unsigned i=0; i<dimension; ++i) {
      71          71 :     if( spbc[i]=="F" ) pbc[i]=false;
      72          22 :     else if( spbc[i]=="T" ) pbc[i]=true;
      73           0 :     else plumed_error();
      74          43 :   }
      75          43 : }
      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           7 :   for(unsigned i=0; i<dimension; ++i) tnames[i]=arg_names[i];
      81           7 :   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           7 :   for(unsigned i=0; i<dimension; ++i) arg_names[i]=tnames[i];
      84          14 :   for(unsigned i=0; i<nper; ++i) arg_names[dimension+i]=cnames[i];
      85           7 : }
      86             : 
      87          64 : 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          64 :   plumed_assert( gtype==flat && (spacing.size()==dimension || binsin.size()==dimension) );
      91             : 
      92          64 :   npoints=1; bounds_set=true;
      93         168 :   for(unsigned i=0; i<dimension; ++i) {
      94         104 :     str_min[i]=smin[i]; str_max[i]=smax[i];
      95         104 :     Tools::convert( str_min[i], min[i] );
      96         104 :     Tools::convert( str_max[i], max[i] );
      97         104 :     if( spacing.size()==dimension && binsin.size()==dimension ) {
      98          27 :       double range = max[i] - min[i]; unsigned spc = std::floor( range / spacing[i]);
      99             :       // This check ensures that nbins is set correctly if spacing is set the same as the number of bins
     100          27 :       if( fabs( binsin[i]*spacing[i]-range )>epsilon ) spc += 1;
     101          27 :       if( spc>binsin[i] ) nbin[i]=spc; else nbin[i]=binsin[i];
     102          77 :     } else if( binsin.size()==dimension ) nbin[i]=binsin[i];
     103           0 :     else if( spacing.size()==dimension ) nbin[i] = std::floor(( max[i] - min[i] ) / spacing[i]) + 1;
     104           0 :     else plumed_error();
     105         104 :     dx[i] = ( max[i] - min[i] ) / static_cast<double>( nbin[i] );
     106         104 :     if( !pbc[i] ) { max[i] +=dx[i]; nbin[i]+=1; }
     107         104 :     stride[i]=npoints;
     108         104 :     npoints*=nbin[i];
     109             :   }
     110          64 :   resize();  // Always resize after setting new bounds as grid size may have have changed
     111          64 : }
     112             : 
     113           3 : void GridVessel::setupFibonacciGrid( const unsigned& np ) {
     114           3 :   bounds_set=true; root5 = sqrt(5);
     115           3 :   npoints = np; golden = ( 1 + sqrt(5) ) / 2.0; igolden = golden - 1;
     116           3 :   fib_increment = 2*pi*igolden; log_golden2 = std::log( golden*golden );
     117           3 :   fib_offset = 2 / static_cast<double>( npoints );
     118           3 :   fib_shift = fib_offset/2 - 1;
     119           3 :   resize();
     120             : 
     121           6 :   std::vector<double> icoord( dimension ), jcoord( dimension );
     122             :   // Find minimum distance between each pair of points
     123           6 :   std::vector<double> mindists( npoints );
     124         347 :   for(unsigned i=0; i<npoints; ++i) {
     125         344 :     getFibonacciCoordinates( i, icoord ); mindists[i] = 0;
     126       41080 :     for(unsigned j=0; j<npoints; ++j) {
     127       40736 :       if( i==j ) continue ; // Points are not neighbors to themselves
     128       40392 :       getFibonacciCoordinates( j, jcoord );
     129             :       // Calculate the dot product
     130       40392 :       double dot=0; for(unsigned k=0; k<dimension; ++k) dot += icoord[k]*jcoord[k];
     131       40392 :       if( dot>mindists[i] ) mindists[i]=dot;
     132             :     }
     133             :   }
     134             :   // And now take minimum of dot products
     135           3 :   double min=mindists[0];
     136         344 :   for(unsigned i=1; i<npoints; ++i) {
     137         341 :     if( mindists[i]<min ) min=mindists[i];
     138             :   }
     139             :   double final_cutoff;
     140           3 :   if( getFibonacciCutoff()<-1 ) final_cutoff=-1;
     141           2 :   else final_cutoff = cos( acos( getFibonacciCutoff() ) + acos( min ) );
     142             : 
     143             :   // And now construct the neighbor list
     144           3 :   fib_nlist.resize( npoints );
     145         347 :   for(unsigned i=0; i<npoints; ++i) {
     146         344 :     getFibonacciCoordinates( i, icoord );
     147       41080 :     for(unsigned j=0; j<npoints; ++j) {
     148       40736 :       if( i==j ) continue ; // Points are not neighbors to themselves
     149       40392 :       getFibonacciCoordinates( j, jcoord );
     150             :       // Calculate the dot product
     151       40392 :       double dot=0; for(unsigned k=0; k<dimension; ++k) dot += icoord[k]*jcoord[k];
     152       40392 :       if( dot>final_cutoff ) { fib_nlist[i].push_back(j); }
     153             :     }
     154           3 :   }
     155           3 : }
     156             : 
     157          43 : std::string GridVessel::description() {
     158          43 :   if( !bounds_set ) return "";
     159             : 
     160          30 :   std::string des;
     161          30 :   if( gtype==flat ) {
     162          28 :     des="grid of "; std::string num;
     163          43 :     for(unsigned i=0; i<dimension-1; ++i) {
     164          15 :       Tools::convert( nbin[i], num );
     165          15 :       des += num + " X ";
     166             :     }
     167          28 :     Tools::convert( nbin[dimension-1], num );
     168          28 :     des += num + " equally spaced points between (";
     169          28 :     for(unsigned i=0; i<dimension-1; ++i) des += str_min[i] + ",";
     170          28 :     Tools::convert( nbin[dimension-1], num );
     171          28 :     des += str_min[dimension-1] + ") and (";
     172          28 :     for(unsigned i=0; i<dimension-1; ++i) des += str_max[i] + ",";
     173          28 :     des += str_max[dimension-1] + ")";
     174           2 :   } else if( gtype==fibonacci ) {
     175           2 :     std::string num; Tools::convert( npoints, num );
     176           2 :     des += "fibonacci grid of " + num + " points on spherical surface";
     177             :   }
     178          30 :   return des;
     179             : }
     180             : 
     181         155 : void GridVessel::resize() {
     182         155 :   plumed_massert( nper>0, "Number of datapoints at each grid point has not been set");
     183         155 :   resizeBuffer( getNumberOfBufferPoints()*nper + 1 + 2*getAction()->getNumberOfDerivatives() );
     184         155 :   setDataSize( npoints*nper ); forces.resize( npoints );
     185         155 :   if( active.size()!=npoints) active.resize( npoints, true );
     186         155 : }
     187             : 
     188    21240426 : unsigned GridVessel::getIndex( const std::vector<unsigned>& indices ) const {
     189             :   plumed_dbg_assert( gtype==flat && bounds_set && indices.size()==dimension );
     190             :   // indices are flattended using a column-major order
     191    21240426 :   unsigned index=indices[dimension-1];
     192    63699056 :   for(unsigned i=dimension-1; i>0; --i) {
     193    42458019 :     index=index*nbin[i-1]+indices[i-1];
     194             :   }
     195    21241037 :   return index;
     196             : }
     197             : 
     198       17871 : void GridVessel::getIndices( const std::vector<double>& point, std::vector<unsigned>& indices ) const {
     199             :   plumed_dbg_assert( gtype==flat && bounds_set && point.size()==dimension && indices.size()==dimension );
     200       70714 :   for(unsigned i=0; i<dimension; ++i) {
     201       52829 :     indices[i]=std::floor( (point[i] - min[i])/dx[i] );
     202       52842 :     if( pbc[i] ) indices[i]=indices[i]%nbin[i];
     203        7321 :     else if( indices[i]>nbin[i] ) plumed_merror("point is outside grid range");
     204             :   }
     205       17885 : }
     206             : 
     207        8650 : unsigned GridVessel::getIndex( const std::vector<double>& point ) const {
     208             :   plumed_dbg_assert( gtype==flat && bounds_set && point.size()==dimension );
     209        8650 :   if( gtype==flat ) {
     210        8650 :     std::vector<unsigned> indices(dimension); getIndices( point, indices );
     211        8651 :     return getIndex( indices );
     212           0 :   } else if( gtype==fibonacci ) {
     213           0 :     return getFibonacciIndex( point );
     214             :   } else {
     215           0 :     plumed_error();
     216             :   }
     217             : }
     218             : 
     219          55 : unsigned GridVessel::getFibonacciIndex( const std::vector<double>& p ) const {
     220             :   plumed_dbg_assert( gtype==fibonacci );
     221             :   // Convert input point to coordinates on cylinder
     222          55 :   int k=2; double phi = atan2( p[2], p[0] ), sinthet2 = 1 - p[1]*p[1];
     223             :   // Calculate power to raise golden ratio
     224          56 :   if( sinthet2<epsilon ) { k = 2; }
     225             :   else {
     226          56 :     k = std::floor( std::log( npoints*pi*root5*sinthet2 ) / log_golden2 );
     227          56 :     if( k<2 ) k = 2;
     228             :   }
     229          56 :   double Fk = pow( golden, k ) / root5, F0 = std::round(Fk), F1 = std::round(Fk*golden);
     230         113 :   Matrix<double> B(2,2), invB(2,2); std::vector<double> thisp(3);
     231          56 :   B(0,0) = 2*pi*((F0+1)*igolden - std::floor((F0+1)*igolden)) - fib_increment;
     232          56 :   B(0,1) = 2*pi*((F1+1)*igolden - std::floor((F1+1)*igolden)) - fib_increment;
     233          56 :   B(1,0) = -2*F0/npoints; B(1,1) = -2*F1/npoints; Invert( B, invB );
     234         113 :   std::vector<double> vv(2), rc(2); vv[0]=-phi; vv[1] = p[1] - fib_shift;
     235         114 :   mult( invB, vv, rc ); std::vector<int> c(2); c[0]=std::floor(rc[0]); c[1]=std::floor(rc[1]);
     236          53 :   unsigned outind; double mindist = 10000000.;
     237         281 :   for(int s=0; s<4; ++s) {
     238         224 :     double ttt, costheta = B(1,0)*( c[0] + s%2 ) + B(1,1)*( c[1] + s/2 ) + fib_shift;
     239         225 :     if( costheta>1 ) ttt=1; else if( costheta<-1 ) ttt=-1; else ttt=costheta;
     240         225 :     costheta = 2*ttt - costheta;
     241         225 :     unsigned i = std::floor( 0.5*npoints*(1+costheta) ); getFibonacciCoordinates( i, thisp );
     242         228 :     double dist=0; for(unsigned j=0; j<3; ++j) { double tmp=thisp[j]-p[j]; dist += tmp*tmp; }
     243         228 :     if( dist<mindist ) { outind = i; mindist = dist; }
     244             :   }
     245         114 :   return outind;
     246             : }
     247             : 
     248   108593189 : void GridVessel::convertIndexToIndices( const unsigned& index, const std::vector<unsigned>& nnbin, std::vector<unsigned>& indices ) const {
     249   108593189 :   plumed_dbg_assert( gtype==flat ); unsigned kk=index; indices[0]=index%nnbin[0];
     250   216871541 :   for(unsigned i=1; i<dimension-1; ++i) {
     251   108430262 :     kk=(kk-indices[i-1])/nnbin[i-1];
     252   108131579 :     indices[i]=kk%nnbin[i];
     253             :   }
     254   108441279 :   if(dimension>=2) { // I think this is wrong
     255   108557734 :     indices[dimension-1]=(kk-indices[dimension-2])/nnbin[dimension-2];
     256             :   }
     257   108013279 : }
     258             : 
     259    12147431 : void GridVessel::getIndices( const unsigned& index, std::vector<unsigned>& indices ) const {
     260    12147431 :   plumed_dbg_assert( gtype==flat ); convertIndexToIndices( index, nbin, indices );
     261    12146524 : }
     262             : 
     263       98562 : void GridVessel::getGridPointCoordinates( const unsigned& ipoint, std::vector<double>& x ) const {
     264       98562 :   std::vector<unsigned> tindices( dimension ); getGridPointCoordinates( ipoint, tindices, x );
     265       98568 : }
     266             : 
     267    11945828 : void GridVessel::getGridPointCoordinates( const unsigned& ipoint, std::vector<unsigned>& tindices, std::vector<double>& x ) const {
     268             :   plumed_dbg_assert( bounds_set && x.size()==dimension && tindices.size()==dimension && ipoint<npoints );
     269    11945828 :   if( gtype==flat ) {
     270    11940717 :     getFlatGridCoordinates( ipoint, tindices, x );
     271        5111 :   } else if( gtype==fibonacci ) {
     272        5111 :     getFibonacciCoordinates( ipoint, x );
     273             :   } else {
     274           0 :     plumed_error();
     275             :   }
     276    11945208 : }
     277             : 
     278    11949346 : void GridVessel::getFlatGridCoordinates( const unsigned& ipoint, std::vector<unsigned>& tindices, std::vector<double>& x ) const {
     279    11949346 :   plumed_dbg_assert( gtype==flat ); getIndices( ipoint, tindices );
     280    11946811 :   for(unsigned i=0; i<dimension; ++i) x[i] = min[i] + dx[i]*tindices[i];
     281    11948513 : }
     282             : 
     283       86805 : void GridVessel::getFibonacciCoordinates( const unsigned& ipoint, std::vector<double>& x ) const {
     284             :   plumed_dbg_assert( gtype==fibonacci );
     285       86805 :   x[1] = (ipoint*fib_offset) + fib_shift; double r = sqrt( 1 - x[1]*x[1] );
     286       86812 :   double phi = ipoint*fib_increment; x[0] = r*cos(phi); x[2] = r*sin(phi);
     287       86775 :   double norm=0; for(unsigned j=0; j<3; ++j) norm+=x[j]*x[j];
     288       86804 :   norm = sqrt(norm); for(unsigned j=0; j<3; ++j) x[j] = x[j] / norm;
     289       86805 : }
     290             : 
     291        8644 : void GridVessel::getSplineNeighbors( const unsigned& mybox, std::vector<unsigned>& mysneigh ) const {
     292        8644 :   plumed_dbg_assert( gtype==flat ); mysneigh.resize( static_cast<unsigned>(pow(2.,dimension)) );
     293             : 
     294        8646 :   std::vector<unsigned> tmp_indices( dimension );
     295       17292 :   std::vector<unsigned> my_indices( dimension );
     296        8646 :   getIndices( mybox, my_indices );
     297       76612 :   for(unsigned i=0; i<mysneigh.size(); ++i) {
     298       67959 :     unsigned tmp=i;
     299      270925 :     for(unsigned j=0; j<dimension; ++j) {
     300      202963 :       unsigned i0=tmp%2+my_indices[j]; tmp/=2;
     301      203015 :       if(!pbc[j] && i0==nbin[j]) getAction()->error("Extrapolating function on grid");
     302      202973 :       if( pbc[j] && i0==nbin[j]) i0=0;
     303      203029 :       tmp_indices[j]=i0;
     304             :     }
     305       67962 :     mysneigh[i]=getIndex( tmp_indices );
     306       67954 :     plumed_massert( active[mysneigh[i]], "inactive grid point required for splines");
     307        8646 :   }
     308        8646 : }
     309             : 
     310      295157 : double GridVessel::getGridElement( const unsigned& ipoint, const unsigned& jelement ) const {
     311      295157 :   plumed_assert( bounds_set && ipoint<npoints && jelement<nper && active[ipoint] );
     312      295137 :   return getDataElement( nper*ipoint + jelement  );
     313             : }
     314             : 
     315           0 : void GridVessel::setGridElement( const unsigned& ipoint, const unsigned& jelement, const double& value ) {
     316             :   plumed_dbg_assert( bounds_set && ipoint<npoints && jelement<nper );
     317           0 :   setDataElement( nper*ipoint + jelement, value );
     318           0 : }
     319             : 
     320           5 : void GridVessel::addToGridElement( const unsigned& ipoint, const unsigned& jelement, const double& value ) {
     321             :   plumed_dbg_assert( bounds_set && ipoint<npoints && jelement<nper );
     322           5 :   addDataElement( nper*ipoint + jelement, value );
     323           5 : }
     324             : 
     325        9536 : void GridVessel::calculate( const unsigned& current, MultiValue& myvals, std::vector<double>& buffer, std::vector<unsigned>& der_list ) const {
     326             :   plumed_dbg_assert( myvals.getNumberOfValues()==(nper+1) );
     327        9536 :   for(unsigned i=0; i<nper; ++i) buffer[bufstart + nper*current + i] += myvals.get(i+1);
     328        9578 : }
     329             : 
     330          97 : void GridVessel::finish( const std::vector<double>& buffer ) {
     331          97 :   if( wasforced ) getFinalForces( buffer, finalForces );
     332          77 :   else AveragingVessel::finish( buffer );
     333          97 : }
     334             : 
     335           0 : double GridVessel::getGridElement( const std::vector<unsigned>& indices, const unsigned& jelement ) const {
     336           0 :   return getGridElement( getIndex( indices ), jelement );
     337             : }
     338             : 
     339           0 : void GridVessel::setGridElement( const std::vector<unsigned>& indices, const unsigned& jelement, const double& value ) {
     340           0 :   setGridElement( getIndex( indices ), jelement, value );
     341           0 : }
     342             : 
     343      166443 : std::vector<std::string> GridVessel::getMin() const {
     344      166443 :   plumed_dbg_assert( gtype==flat ); return str_min;
     345             : }
     346             : 
     347      166476 : std::vector<std::string> GridVessel::getMax() const {
     348      166476 :   plumed_dbg_assert( gtype==flat ); return str_max;
     349             : }
     350             : 
     351      167061 : std::vector<unsigned> GridVessel::getNbin() const {
     352             :   plumed_dbg_assert( gtype==flat && bounds_set );
     353      167061 :   std::vector<unsigned> ngrid( dimension );
     354      497786 :   for(unsigned i=0; i<dimension; ++i) {
     355      330723 :     if( !pbc[i] ) ngrid[i]=nbin[i] - 1;
     356        3888 :     else ngrid[i]=nbin[i];
     357             :   }
     358      167063 :   return ngrid;
     359             : }
     360             : 
     361       21415 : void GridVessel::getNeighbors( const std::vector<double>& pp, const std::vector<unsigned>& nneigh,
     362             :                                unsigned& num_neighbors, std::vector<unsigned>& neighbors ) const {
     363             :   plumed_dbg_assert( bounds_set );
     364             : 
     365       21415 :   if( gtype == flat ) {
     366             :     plumed_dbg_assert( nneigh.size()==dimension );
     367       21361 :     std::vector<unsigned> indices( dimension );
     368       21387 :     for(unsigned i=0; i<dimension; ++i) indices[i] = std::floor( (pp[i]-min[i])/dx[i] );
     369       21369 :     getNeighbors( indices, nneigh, num_neighbors, neighbors );
     370          54 :   } else if( gtype == fibonacci ) {
     371          54 :     unsigned find = getFibonacciIndex( pp );
     372          56 :     num_neighbors = 1 + fib_nlist[find].size();
     373          57 :     if( neighbors.size()<num_neighbors ) neighbors.resize( num_neighbors );
     374          56 :     neighbors[0]=find; for(unsigned i=0; i<fib_nlist[find].size(); ++i) neighbors[1+i] = fib_nlist[find][i];
     375             :   } else {
     376           0 :     plumed_error();
     377             :   }
     378       21428 : }
     379             : 
     380       40784 : void GridVessel::getNeighbors( const std::vector<unsigned>& indices, const std::vector<unsigned>& nneigh,
     381             :                                unsigned& num_neighbors, std::vector<unsigned>& neighbors ) const {
     382             :   plumed_dbg_assert( gtype==flat && bounds_set && nneigh.size()==dimension );
     383             : 
     384       40784 :   unsigned num_neigh=1; std::vector<unsigned> small_bin( dimension );
     385      162773 :   for(unsigned i=0; i<dimension; ++i) {
     386      121979 :     small_bin[i]=(2*nneigh[i]+1);
     387      121985 :     num_neigh *=small_bin[i];
     388             :   }
     389       40794 :   if( neighbors.size()!=num_neigh ) neighbors.resize( num_neigh );
     390             : 
     391       40790 :   num_neighbors=0;
     392       81583 :   std::vector<unsigned> s_indices(dimension), t_indices(dimension);
     393    96530856 :   for(unsigned index=0; index<num_neigh; ++index) {
     394    96490063 :     bool found=true;
     395    96490063 :     convertIndexToIndices( index, small_bin, s_indices );
     396   385160918 :     for(unsigned i=0; i<dimension; ++i) {
     397   288714028 :       int i0=s_indices[i]-nneigh[i]+indices[i];
     398   289365148 :       if(!pbc[i] && i0<0)        found=false;
     399   289365167 :       if(!pbc[i] && i0>=nbin[i]) found=false;
     400   289196995 :       if( pbc[i] && i0<0)        i0=nbin[i]-(-i0)%nbin[i];
     401   288973654 :       if( pbc[i] && i0>=nbin[i]) i0%=nbin[i];
     402   288953906 :       t_indices[i]=static_cast<unsigned>(i0);
     403             :     }
     404    96446890 :     if( found ) {
     405    21083567 :       neighbors[num_neighbors]=getIndex( t_indices );
     406    21084883 :       num_neighbors++;
     407             :     }
     408       40793 :   }
     409       40793 : }
     410             : 
     411          11 : void GridVessel::setCubeUnits( const double& units ) {
     412          11 :   plumed_dbg_assert( gtype==flat ); cube_units=units;
     413          11 : }
     414             : 
     415           8 : double GridVessel::getCubeUnits() const {
     416           8 :   plumed_dbg_assert( gtype==flat ); return cube_units;
     417             : }
     418             : 
     419           7 : std::string GridVessel::getInputString() const {
     420           7 :   std::string mstring="COORDINATES="+arg_names[0];
     421           7 :   for(unsigned i=1; i<dimension; ++i) mstring+="," + arg_names[i];
     422           7 :   if( gtype==flat ) {
     423           7 :     mstring += " TYPE=flat PBC=";
     424           7 :     if( pbc[0] ) mstring +="T";
     425           5 :     else mstring +="F";
     426          11 :     for(unsigned i=1; i<dimension; ++i) {
     427           4 :       if( pbc[i] ) mstring +=",T";
     428           4 :       else mstring +=",F";
     429             :     }
     430           0 :   } else if( gtype==fibonacci ) {
     431           0 :     mstring += " TYPE=fibonacci";
     432             :   }
     433           7 :   return mstring;
     434             : }
     435             : 
     436        8643 : double GridVessel::getValueAndDerivatives( const std::vector<double>& x, const unsigned& ind, std::vector<double>& der ) const {
     437             :   plumed_dbg_assert( gtype==flat && der.size()==dimension && !noderiv && ind<getNumberOfComponents() );
     438             : 
     439        8643 :   double X,X2,X3,value=0; der.assign(der.size(),0.0);
     440        8645 :   std::vector<double> fd(dimension);
     441       17292 :   std::vector<double> C(dimension);
     442       17291 :   std::vector<double> D(dimension);
     443       17291 :   std::vector<double> dder(dimension);
     444             : 
     445       17292 :   std::vector<unsigned> nindices(dimension);
     446       17292 :   std::vector<unsigned> indices(dimension); getIndices( x, indices );
     447       17291 :   std::vector<unsigned> neigh; getSplineNeighbors( getIndex(indices), neigh );
     448       17291 :   std::vector<double> xfloor(dimension); getFlatGridCoordinates( getIndex(x), nindices, xfloor );
     449             : 
     450             : // loop over neighbors
     451       76606 :   for(unsigned int ipoint=0; ipoint<neigh.size(); ++ipoint) {
     452       67949 :     double grid=getGridElement(neigh[ipoint], ind*(1+dimension) );
     453       67994 :     for(unsigned j=0; j<dimension; ++j) dder[j] = getGridElement( neigh[ipoint], ind*(1+dimension) + 1 + j );
     454             : 
     455       67959 :     getIndices( neigh[ipoint], nindices );
     456       67873 :     double ff=1.0;
     457             : 
     458      270889 :     for(unsigned j=0; j<dimension; ++j) {
     459      202875 :       int x0=1;
     460      202875 :       if(nindices[j]==indices[j]) x0=0;
     461      203043 :       double ddx=dx[j];
     462      202965 :       X=fabs((x[j]-xfloor[j])/ddx-(double)x0);
     463      202755 :       X2=X*X;
     464      202755 :       X3=X2*X;
     465             :       double yy;
     466      202755 :       if(fabs(grid)<0.0000001) yy=0.0;
     467      188938 :       else yy=-dder[j]/grid;
     468      203024 :       C[j]=(1.0-3.0*X2+2.0*X3) - (x0?-1.0:1.0)*yy*(X-2.0*X2+X3)*ddx;
     469      203079 :       D[j]=( -6.0*X +6.0*X2) - (x0?-1.0:1.0)*yy*(1.0-4.0*X +3.0*X2)*ddx;
     470      203054 :       D[j]*=(x0?-1.0:1.0)/ddx;
     471      203046 :       ff*=C[j];
     472             :     }
     473      271028 :     for(unsigned j=0; j<dimension; ++j) {
     474      203038 :       fd[j]=D[j];
     475      203024 :       for(unsigned i=0; i<dimension; ++i) if(i!=j) fd[j]*=C[i];
     476             :     }
     477       67990 :     value+=grid*ff;
     478       67990 :     for(unsigned j=0; j<dimension; ++j) der[j]+=grid*fd[j];
     479             :   }
     480       17291 :   return value;
     481             : }
     482             : 
     483           3 : void GridVessel::activateThesePoints( const std::vector<bool>& to_activate ) {
     484             :   plumed_dbg_assert( to_activate.size()==npoints );
     485           3 :   for(unsigned i=0; i<npoints; ++i) active[i]=to_activate[i];
     486           3 : }
     487             : 
     488          20 : void GridVessel::setForce( const std::vector<double>& inforces ) {
     489             :   plumed_dbg_assert( inforces.size()==npoints );
     490          20 :   wasforced=true; for(unsigned i=0; i<npoints; ++i) forces[i]=inforces[i];
     491          20 : }
     492             : 
     493    11868312 : bool GridVessel::wasForced() const {
     494    11868312 :   return wasforced;
     495             : }
     496             : 
     497          20 : bool GridVessel::applyForce( std::vector<double>& fforces ) {
     498             :   plumed_dbg_assert( fforces.size()==finalForces.size() );
     499          20 :   if( !wasforced ) return false;
     500          20 :   for(unsigned i=0; i<finalForces.size(); ++i) fforces[i]=finalForces[i];
     501          20 :   wasforced=false; return true;
     502             : }
     503             : 
     504             : }
     505        4821 : }
     506             : 

Generated by: LCOV version 1.13