LCOV - code coverage report
Current view: top level - gridtools - GridVessel.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 314 332 94.6 %
Date: 2019-08-13 10:15:31 Functions: 39 41 95.1 %

          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          57 : void GridVessel::registerKeywords( Keywords& keys ) {
      31          57 :   AveragingVessel::registerKeywords( keys );
      32         285 :   keys.add("compulsory","TYPE","flat","how the grid points are being generated");
      33         228 :   keys.add("compulsory","COMPONENTS","the names of the components in the vector");
      34         228 :   keys.add("compulsory","COORDINATES","the names of the coordinates of the grid");
      35         228 :   keys.add("compulsory","PBC","is the grid periodic in each direction or not");
      36          57 : }
      37             : 
      38          58 : GridVessel::GridVessel( const vesselbase::VesselOptions& da ):
      39             :   AveragingVessel(da),
      40             :   bounds_set(false),
      41             :   npoints(0),
      42             :   cube_units(1.0),
      43             :   wasforced(false),
      44         232 :   noderiv(false)
      45             : {
      46         116 :   std::string geom; parse("TYPE",geom);
      47          58 :   if( geom=="flat" ) gtype=flat;
      48           3 :   else if( geom=="fibonacci" ) gtype=fibonacci;
      49           0 :   else plumed_merror( geom + " is invalid geometry type");
      50         174 :   std::vector<std::string> compnames; parseVector("COMPONENTS",compnames);
      51         174 :   std::vector<std::string> coordnames; parseVector("COORDINATES",coordnames);
      52          58 :   if( gtype==flat ) {
      53          55 :     dimension=coordnames.size();
      54          55 :     str_min.resize( dimension);  str_max.resize( dimension ); stride.resize( dimension );
      55          55 :     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         116 :   unsigned n=0; nper=compnames.size()*( 1 + coordnames.size() );
      62          58 :   arg_names.resize( coordnames.size() + compnames.size()*( 1 + coordnames.size() ) );
      63         322 :   for(unsigned i=0; i<coordnames.size(); ++i) { arg_names[n] = coordnames[i]; n++; }
      64         174 :   for(unsigned i=0; i<compnames.size(); ++i) {
      65         116 :     arg_names[n]=compnames[i]; n++;
      66         644 :     for(unsigned j=0; j<coordnames.size(); ++j) { arg_names[n] = "d" + compnames[i] + "_" + coordnames[j]; n++; }
      67             :   }
      68          58 :   pbc.resize( dimension );
      69         174 :   std::vector<std::string> spbc( dimension ); parseVector("PBC",spbc);
      70         146 :   for(unsigned i=0; i<dimension; ++i) {
      71         176 :     if( spbc[i]=="F" ) pbc[i]=false;
      72          22 :     else if( spbc[i]=="T" ) pbc[i]=true;
      73           0 :     else plumed_error();
      74             :   }
      75          58 : }
      76             : 
      77          19 : void GridVessel::setNoDerivatives() {
      78          19 :   nper = ( nper/(1+dimension) ); noderiv=true;
      79          38 :   std::vector<std::string> tnames( dimension ), cnames(nper);
      80          41 :   for(unsigned i=0; i<dimension; ++i) tnames[i]=arg_names[i];
      81          57 :   unsigned k=dimension; for(unsigned i=0; i<nper; ++i) { cnames[i]=arg_names[k]; k+=(1+dimension); }
      82          19 :   arg_names.resize( dimension + nper );
      83          44 :   for(unsigned i=0; i<dimension; ++i) arg_names[i]=tnames[i];
      84          76 :   for(unsigned i=0; i<nper; ++i) arg_names[dimension+i]=cnames[i];
      85          19 : }
      86             : 
      87          85 : 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         234 :   plumed_assert( gtype==flat && (spacing.size()==dimension || binsin.size()==dimension) );
      91             : 
      92          85 :   npoints=1; bounds_set=true;
      93         212 :   for(unsigned i=0; i<dimension; ++i) {
      94         127 :     str_min[i]=smin[i]; str_max[i]=smax[i];
      95         127 :     Tools::convert( str_min[i], min[i] );
      96         127 :     Tools::convert( str_max[i], max[i] );
      97         160 :     if( spacing.size()==dimension && binsin.size()==dimension ) {
      98          99 :       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          33 :       if( fabs( binsin[i]*spacing[i]-range )>epsilon ) spc += 1;
     101          66 :       if( spc>binsin[i] ) nbin[i]=spc; else nbin[i]=binsin[i];
     102         188 :     } 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         381 :     dx[i] = ( max[i] - min[i] ) / static_cast<double>( nbin[i] );
     106         397 :     if( !pbc[i] ) { max[i] +=dx[i]; nbin[i]+=1; }
     107         127 :     stride[i]=npoints;
     108         127 :     npoints*=nbin[i];
     109             :   }
     110          85 :   resize();  // Always resize after setting new bounds as grid size may have have changed
     111          85 : }
     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           3 :   std::vector<double> icoord( dimension ), jcoord( dimension );
     122             :   // Find minimum distance between each pair of points
     123           3 :   std::vector<double> mindists( npoints );
     124         347 :   for(unsigned i=0; i<npoints; ++i) {
     125         688 :     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      363528 :       double dot=0; for(unsigned k=0; k<dimension; ++k) dot += icoord[k]*jcoord[k];
     131       80784 :       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         682 :     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      363528 :       double dot=0; for(unsigned k=0; k<dimension; ++k) dot += icoord[k]*jcoord[k];
     152       63278 :       if( dot>final_cutoff ) { fib_nlist[i].push_back(j); }
     153             :     }
     154             :   }
     155           3 : }
     156             : 
     157          56 : std::string GridVessel::description() {
     158          56 :   if( !bounds_set ) return "";
     159             : 
     160             :   std::string des;
     161          43 :   if( gtype==flat ) {
     162             :     des="grid of "; std::string num;
     163          56 :     for(unsigned i=0; i<dimension-1; ++i) {
     164          30 :       Tools::convert( nbin[i], num );
     165          30 :       des += num + " X ";
     166             :     }
     167          82 :     Tools::convert( nbin[dimension-1], num );
     168          82 :     des += num + " equally spaced points between (";
     169          71 :     for(unsigned i=0; i<dimension-1; ++i) des += str_min[i] + ",";
     170          82 :     Tools::convert( nbin[dimension-1], num );
     171         123 :     des += str_min[dimension-1] + ") and (";
     172          71 :     for(unsigned i=0; i<dimension-1; ++i) des += str_max[i] + ",";
     173         123 :     des += str_max[dimension-1] + ")";
     174           2 :   } else if( gtype==fibonacci ) {
     175           2 :     std::string num; Tools::convert( npoints, num );
     176           6 :     des += "fibonacci grid of " + num + " points on spherical surface";
     177             :   }
     178             :   return des;
     179             : }
     180             : 
     181         190 : void GridVessel::resize() {
     182         190 :   plumed_massert( nper>0, "Number of datapoints at each grid point has not been set");
     183         377 :   if( getAction() ) resizeBuffer( getNumberOfBufferPoints()*nper + 1 + 2*getAction()->getNumberOfDerivatives() );
     184         190 :   setDataSize( npoints*nper ); forces.resize( npoints );
     185         190 :   if( active.size()!=npoints) active.resize( npoints, true );
     186         190 : }
     187             : 
     188    24327913 : 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    48655826 :   unsigned index=indices[dimension-1];
     192    94202109 :   for(unsigned i=dimension-1; i>0; --i) {
     193   136638849 :     index=index*nbin[i-1]+indices[i-1];
     194             :   }
     195    24327913 :   return index;
     196             : }
     197             : 
     198     1060340 : 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     3196033 :   for(unsigned i=0; i<dimension; ++i) {
     201     8542616 :     indices[i]=std::floor( (point[i] - min[i])/dx[i] );
     202     2226634 :     if( pbc[i] ) indices[i]=indices[i]%nbin[i];
     203     4180328 :     else if( indices[i]>nbin[i] ) {
     204           0 :       std::string pp, num; Tools::convert( point[0], pp );
     205           0 :       for(unsigned j=1; j<point.size(); ++j) { Tools::convert( point[j], num ); pp += ", " + num; }
     206           0 :       plumed_merror("point (" + pp + ")  is outside grid range");
     207             :     }
     208             :   }
     209     1060379 : }
     210             : 
     211      530952 : unsigned GridVessel::getIndex( const std::vector<double>& point ) const {
     212             :   plumed_dbg_assert( gtype==flat && bounds_set && point.size()==dimension );
     213      530952 :   if( gtype==flat ) {
     214      530952 :     std::vector<unsigned> indices(dimension); getIndices( point, indices );
     215      530954 :     return getIndex( indices );
     216           0 :   } else if( gtype==fibonacci ) {
     217           0 :     return getFibonacciIndex( point );
     218             :   } else {
     219           0 :     plumed_error();
     220             :   }
     221             : }
     222             : 
     223          55 : unsigned GridVessel::getFibonacciIndex( const std::vector<double>& p ) const {
     224             :   plumed_dbg_assert( gtype==fibonacci );
     225             :   // Convert input point to coordinates on cylinder
     226         110 :   int k=2; double phi = atan2( p[2], p[0] ), sinthet2 = 1 - p[1]*p[1];
     227             :   // Calculate power to raise golden ratio
     228          55 :   if( sinthet2<epsilon ) { k = 2; }
     229             :   else {
     230          57 :     k = std::floor( std::log( npoints*pi*root5*sinthet2 ) / log_golden2 );
     231          57 :     if( k<2 ) k = 2;
     232             :   }
     233          55 :   double Fk = pow( golden, k ) / root5, F0 = std::round(Fk), F1 = std::round(Fk*golden);
     234          55 :   Matrix<double> B(2,2), invB(2,2); std::vector<double> thisp(3);
     235          57 :   B(0,0) = 2*pi*((F0+1)*igolden - std::floor((F0+1)*igolden)) - fib_increment;
     236          57 :   B(0,1) = 2*pi*((F1+1)*igolden - std::floor((F1+1)*igolden)) - fib_increment;
     237         114 :   B(1,0) = -2*F0/npoints; B(1,1) = -2*F1/npoints; Invert( B, invB );
     238         171 :   std::vector<double> vv(2), rc(2); vv[0]=-phi; vv[1] = p[1] - fib_shift;
     239         167 :   mult( invB, vv, rc ); std::vector<int> c(2); c[0]=std::floor(rc[0]); c[1]=std::floor(rc[1]);
     240             :   unsigned outind; double mindist = 10000000.;
     241         282 :   for(int s=0; s<4; ++s) {
     242         675 :     double ttt, costheta = B(1,0)*( c[0] + s%2 ) + B(1,1)*( c[1] + s/2 ) + fib_shift;
     243         225 :     if( costheta>1 ) ttt=1; else if( costheta<-1 ) ttt=-1; else ttt=costheta;
     244         225 :     costheta = 2*ttt - costheta;
     245         225 :     unsigned i = std::floor( 0.5*npoints*(1+costheta) ); getFibonacciCoordinates( i, thisp );
     246        2046 :     double dist=0; for(unsigned j=0; j<3; ++j) { double tmp=thisp[j]-p[j]; dist += tmp*tmp; }
     247         227 :     if( dist<mindist ) { outind = i; mindist = dist; }
     248             :   }
     249          57 :   return outind;
     250             : }
     251             : 
     252   111784483 : void GridVessel::convertIndexToIndices( const unsigned& index, const std::vector<unsigned>& nnbin, std::vector<unsigned>& indices ) const {
     253   223568966 :   plumed_dbg_assert( gtype==flat ); unsigned kk=index; indices[0]=index%nnbin[0];
     254   220035646 :   for(unsigned i=1; i<dimension-1; ++i) {
     255   324753489 :     kk=(kk-indices[i-1])/nnbin[i-1];
     256   216502326 :     indices[i]=kk%nnbin[i];
     257             :   }
     258   111784483 :   if(dimension>=2) { // I think this is wrong
     259   448733336 :     indices[dimension-1]=(kk-indices[dimension-2])/nnbin[dimension-2];
     260             :   }
     261   111784483 : }
     262             : 
     263    15771998 : void GridVessel::getIndices( const unsigned& index, std::vector<unsigned>& indices ) const {
     264    15771998 :   plumed_dbg_assert( gtype==flat ); convertIndexToIndices( index, nbin, indices );
     265    15773046 : }
     266             : 
     267      643341 : void GridVessel::getGridPointCoordinates( const unsigned& ipoint, std::vector<double>& x ) const {
     268      643341 :   std::vector<unsigned> tindices( dimension ); getGridPointCoordinates( ipoint, tindices, x );
     269      643342 : }
     270             : 
     271    12489825 : void GridVessel::getGridPointCoordinates( const unsigned& ipoint, std::vector<unsigned>& tindices, std::vector<double>& x ) const {
     272             :   plumed_dbg_assert( bounds_set && x.size()==dimension && tindices.size()==dimension && ipoint<npoints );
     273    12489825 :   if( gtype==flat ) {
     274    12484711 :     getFlatGridCoordinates( ipoint, tindices, x );
     275        5114 :   } else if( gtype==fibonacci ) {
     276        5114 :     getFibonacciCoordinates( ipoint, x );
     277             :   } else {
     278           0 :     plumed_error();
     279             :   }
     280    12490870 : }
     281             : 
     282    13013526 : void GridVessel::getFlatGridCoordinates( const unsigned& ipoint, std::vector<unsigned>& tindices, std::vector<double>& x ) const {
     283    13013526 :   plumed_dbg_assert( gtype==flat ); getIndices( ipoint, tindices );
     284   126614802 :   for(unsigned i=0; i<dimension; ++i) x[i] = min[i] + dx[i]*tindices[i];
     285    13014603 : }
     286             : 
     287       86814 : void GridVessel::getFibonacciCoordinates( const unsigned& ipoint, std::vector<double>& x ) const {
     288             :   plumed_dbg_assert( gtype==fibonacci );
     289      173628 :   x[1] = (ipoint*fib_offset) + fib_shift; double r = sqrt( 1 - x[1]*x[1] );
     290      260442 :   double phi = ipoint*fib_increment; x[0] = r*cos(phi); x[2] = r*sin(phi);
     291      347237 :   double norm=0; for(unsigned j=0; j<3; ++j) norm+=x[j]*x[j];
     292      347240 :   norm = sqrt(norm); for(unsigned j=0; j<3; ++j) x[j] = x[j] / norm;
     293       86814 : }
     294             : 
     295      528841 : void GridVessel::getSplineNeighbors( const unsigned& mybox, unsigned& nneighbors, std::vector<unsigned>& mysneigh ) const {
     296      528841 :   plumed_dbg_assert( gtype==flat ); unsigned nneigh=unsigned(pow(2.0,int(dimension)));
     297      528841 :   if( mysneigh.size()!=nneigh ) mysneigh.resize(nneigh);
     298             : 
     299      528844 :   unsigned inind; nneighbors = 0;
     300      528844 :   std::vector<unsigned> tmp_indices( dimension );
     301      528844 :   std::vector<unsigned> my_indices( dimension );
     302      528842 :   getIndices( mybox, my_indices );
     303     2148735 :   for(unsigned i=0; i<nneigh; ++i) {
     304             :     unsigned tmp=i; inind=0;
     305     4363227 :     for(unsigned j=0; j<dimension; ++j) {
     306     8726454 :       unsigned i0=tmp%2+my_indices[j]; tmp/=2;
     307     8551690 :       if(!pbc[j] && i0==nbin[j]) continue;
     308     4498449 :       if( pbc[j] && i0==nbin[j]) i0=0;
     309     8645992 :       tmp_indices[inind++]=i0;
     310             :     }
     311     2148574 :     if(inind==dimension ) {
     312     2107968 :       unsigned findex=getIndex( tmp_indices );
     313     4216258 :       mysneigh[nneighbors++]=findex;
     314     4216258 :       plumed_massert( active[findex], "inactive grid point required for splines");
     315             :     }
     316             :   }
     317      528843 : }
     318             : 
     319     6415984 : double GridVessel::getGridElement( const unsigned& ipoint, const unsigned& jelement ) const {
     320    12831302 :   plumed_assert( bounds_set && ipoint<npoints && jelement<nper && active[ipoint] );
     321    12831968 :   return getDataElement( nper*ipoint + jelement  );
     322             : }
     323             : 
     324       72600 : void GridVessel::setGridElement( const unsigned& ipoint, const unsigned& jelement, const double& value ) {
     325             :   plumed_dbg_assert( bounds_set && ipoint<npoints && jelement<nper );
     326       72600 :   setDataElement( nper*ipoint + jelement, value );
     327       72600 : }
     328             : 
     329       24200 : void GridVessel::setValueAndDerivatives( const unsigned& ipoint, const unsigned& jelement, const double& value, const std::vector<double>& der ) {
     330             :   plumed_dbg_assert( !noderiv && jelement<getNumberOfComponents() && der.size()==nbin.size() );
     331      145200 :   setGridElement( ipoint, jelement, value ); for(unsigned i=0; i<der.size(); ++i) setGridElement( ipoint, jelement+1+i, der[i] );
     332       24200 : }
     333             : 
     334           5 : void GridVessel::addToGridElement( const unsigned& ipoint, const unsigned& jelement, const double& value ) {
     335             :   plumed_dbg_assert( bounds_set && ipoint<npoints && jelement<nper );
     336           5 :   addDataElement( nper*ipoint + jelement, value );
     337           5 : }
     338             : 
     339        9883 : void GridVessel::calculate( const unsigned& current, MultiValue& myvals, std::vector<double>& buffer, std::vector<unsigned>& der_list ) const {
     340             :   plumed_dbg_assert( myvals.getNumberOfValues()==(nper+1) );
     341       79133 :   for(unsigned i=0; i<nper; ++i) buffer[bufstart + nper*current + i] += myvals.get(i+1);
     342        9883 : }
     343             : 
     344         110 : void GridVessel::finish( const std::vector<double>& buffer ) {
     345         110 :   if( wasforced ) getFinalForces( buffer, finalForces );
     346          90 :   else AveragingVessel::finish( buffer );
     347         110 : }
     348             : 
     349           0 : double GridVessel::getGridElement( const std::vector<unsigned>& indices, const unsigned& jelement ) const {
     350           0 :   return getGridElement( getIndex( indices ), jelement );
     351             : }
     352             : 
     353           0 : void GridVessel::setGridElement( const std::vector<unsigned>& indices, const unsigned& jelement, const double& value ) {
     354           0 :   setGridElement( getIndex( indices ), jelement, value );
     355           0 : }
     356             : 
     357      166607 : std::vector<std::string> GridVessel::getMin() const {
     358      166607 :   plumed_dbg_assert( gtype==flat ); return str_min;
     359             : }
     360             : 
     361      166640 : std::vector<std::string> GridVessel::getMax() const {
     362      166640 :   plumed_dbg_assert( gtype==flat ); return str_max;
     363             : }
     364             : 
     365      167225 : std::vector<unsigned> GridVessel::getNbin() const {
     366             :   plumed_dbg_assert( gtype==flat && bounds_set );
     367      167225 :   std::vector<unsigned> ngrid( dimension );
     368      498114 :   for(unsigned i=0; i<dimension; ++i) {
     369      988773 :     if( !pbc[i] ) ngrid[i]=nbin[i] - 1;
     370        3888 :     else ngrid[i]=nbin[i];
     371             :   }
     372      167227 :   return ngrid;
     373             : }
     374             : 
     375       21422 : void GridVessel::getNeighbors( const std::vector<double>& pp, const std::vector<unsigned>& nneigh,
     376             :                                unsigned& num_neighbors, std::vector<unsigned>& neighbors ) const {
     377             :   plumed_dbg_assert( bounds_set );
     378             : 
     379       21422 :   if( gtype == flat ) {
     380             :     plumed_dbg_assert( nneigh.size()==dimension );
     381       21367 :     std::vector<unsigned> indices( dimension );
     382      212567 :     for(unsigned i=0; i<dimension; ++i) indices[i] = std::floor( (pp[i]-min[i])/dx[i] );
     383       21371 :     getNeighbors( indices, nneigh, num_neighbors, neighbors );
     384          55 :   } else if( gtype == fibonacci ) {
     385          55 :     unsigned find = getFibonacciIndex( pp );
     386         112 :     num_neighbors = 1 + fib_nlist[find].size();
     387          56 :     if( neighbors.size()<num_neighbors ) neighbors.resize( num_neighbors );
     388        9126 :     neighbors[0]=find; for(unsigned i=0; i<fib_nlist[find].size(); ++i) neighbors[1+i] = fib_nlist[find][i];
     389             :   } else {
     390           0 :     plumed_error();
     391             :   }
     392       21432 : }
     393             : 
     394       40787 : void GridVessel::getNeighbors( const std::vector<unsigned>& indices, const std::vector<unsigned>& nneigh,
     395             :                                unsigned& num_neighbors, std::vector<unsigned>& neighbors ) const {
     396             :   plumed_dbg_assert( gtype==flat && bounds_set && nneigh.size()==dimension );
     397             : 
     398       40787 :   unsigned num_neigh=1; std::vector<unsigned> small_bin( dimension );
     399      162796 :   for(unsigned i=0; i<dimension; ++i) {
     400      244000 :     small_bin[i]=(2*nneigh[i]+1);
     401      122000 :     num_neigh *=small_bin[i];
     402             :   }
     403       40796 :   if( neighbors.size()!=num_neigh ) neighbors.resize( num_neigh );
     404             : 
     405       40794 :   num_neighbors=0;
     406       40794 :   std::vector<unsigned> s_indices(dimension), t_indices(dimension);
     407    96573715 :   for(unsigned index=0; index<num_neigh; ++index) {
     408             :     bool found=true;
     409    96532920 :     convertIndexToIndices( index, small_bin, s_indices );
     410   289338469 :     for(unsigned i=0; i<dimension; ++i) {
     411  1157353876 :       int i0=s_indices[i]-nneigh[i]+indices[i];
     412   289338469 :       if(!pbc[i] && i0<0)        found=false;
     413   381924522 :       if(!pbc[i] && i0>=nbin[i]) found=false;
     414   304391474 :       if( pbc[i] && i0<0)        i0=nbin[i]-(-i0)%nbin[i];
     415   502021018 :       if( pbc[i] && i0>=nbin[i]) i0%=nbin[i];
     416   289338469 :       t_indices[i]=static_cast<unsigned>(i0);
     417             :     }
     418    96533057 :     if( found ) {
     419    42176736 :       neighbors[num_neighbors]=getIndex( t_indices );
     420    21088386 :       num_neighbors++;
     421             :     }
     422             :   }
     423       40787 : }
     424             : 
     425          11 : void GridVessel::setCubeUnits( const double& units ) {
     426          11 :   plumed_dbg_assert( gtype==flat ); cube_units=units;
     427          11 : }
     428             : 
     429           8 : double GridVessel::getCubeUnits() const {
     430           8 :   plumed_dbg_assert( gtype==flat ); return cube_units;
     431             : }
     432             : 
     433          13 : std::string GridVessel::getInputString() const {
     434          13 :   std::string mstring="COORDINATES="+arg_names[0];
     435          21 :   for(unsigned i=1; i<dimension; ++i) mstring+="," + arg_names[i];
     436          13 :   if( gtype==flat ) {
     437             :     mstring += " TYPE=flat PBC=";
     438          13 :     if( pbc[0] ) mstring +="T";
     439             :     else mstring +="F";
     440           4 :     for(unsigned i=1; i<dimension; ++i) {
     441           8 :       if( pbc[i] ) mstring +=",T";
     442             :       else mstring +=",F";
     443             :     }
     444           0 :   } else if( gtype==fibonacci ) {
     445             :     mstring += " TYPE=fibonacci";
     446             :   }
     447          13 :   return mstring;
     448             : }
     449             : 
     450      528839 : double GridVessel::getValueAndDerivatives( const std::vector<double>& x, const unsigned& ind, std::vector<double>& der ) const {
     451             :   plumed_dbg_assert( gtype==flat && der.size()==dimension && !noderiv && ind<getNumberOfComponents() );
     452             : 
     453     1057676 :   double X,X2,X3,value=0; der.assign(der.size(),0.0);
     454      528837 :   std::vector<double> fd(dimension);
     455      528844 :   std::vector<double> C(dimension);
     456      528844 :   std::vector<double> D(dimension);
     457      528844 :   std::vector<double> dder(dimension);
     458             : 
     459      528844 :   std::vector<unsigned> nindices(dimension); unsigned n_neigh;
     460      528843 :   std::vector<unsigned> indices(dimension); getIndices( x, indices );
     461      528841 :   std::vector<unsigned> neigh; getSplineNeighbors( getIndex(indices), n_neigh, neigh );
     462      528843 :   std::vector<double> xfloor(dimension); getFlatGridCoordinates( getIndex(x), nindices, xfloor );
     463             : 
     464             : // loop over neighbors
     465     2636937 :   for(unsigned int ipoint=0; ipoint<n_neigh; ++ipoint) {
     466     4216186 :     double grid=getGridElement(neigh[ipoint], ind*(1+dimension) );
     467    10674993 :     for(unsigned j=0; j<dimension; ++j) dder[j] = getGridElement( neigh[ipoint], ind*(1+dimension) + 1 + j );
     468             : 
     469     2108125 :     getIndices( neigh[ipoint], nindices );
     470             :     double ff=1.0;
     471             : 
     472     4283266 :     for(unsigned j=0; j<dimension; ++j) {
     473             :       int x0=1;
     474    12849798 :       if(nindices[j]==indices[j]) x0=0;
     475     4283266 :       double ddx=dx[j];
     476     8566532 :       X=fabs((x[j]-xfloor[j])/ddx-(double)x0);
     477     4283266 :       X2=X*X;
     478     4283266 :       X3=X2*X;
     479             :       double yy;
     480     4283266 :       if(fabs(grid)<0.0000001) yy=0.0;
     481     4269437 :       else yy=-dder[j]/grid;
     482     4283266 :       C[j]=(1.0-3.0*X2+2.0*X3) - (x0?-1.0:1.0)*yy*(X-2.0*X2+X3)*ddx;
     483     4283266 :       D[j]=( -6.0*X +6.0*X2) - (x0?-1.0:1.0)*yy*(1.0-4.0*X +3.0*X2)*ddx;
     484     4283266 :       D[j]*=(x0?-1.0:1.0)/ddx;
     485     4283266 :       ff*=C[j];
     486             :     }
     487     4283417 :     for(unsigned j=0; j<dimension; ++j) {
     488     8566834 :       fd[j]=D[j];
     489    13254701 :       for(unsigned i=0; i<dimension; ++i) if(i!=j) fd[j]*=C[i];
     490             :     }
     491     2108144 :     value+=grid*ff;
     492    10675008 :     for(unsigned j=0; j<dimension; ++j) der[j]+=grid*fd[j];
     493             :   }
     494      528844 :   return value;
     495             : }
     496             : 
     497           3 : void GridVessel::activateThesePoints( const std::vector<bool>& to_activate ) {
     498             :   plumed_dbg_assert( to_activate.size()==npoints );
     499       29991 :   for(unsigned i=0; i<npoints; ++i) active[i]=to_activate[i];
     500           3 : }
     501             : 
     502          20 : void GridVessel::setForce( const std::vector<double>& inforces ) {
     503             :   plumed_dbg_assert( inforces.size()==npoints );
     504        5725 :   wasforced=true; for(unsigned i=0; i<npoints; ++i) forces[i]=inforces[i];
     505          20 : }
     506             : 
     507    11868333 : bool GridVessel::wasForced() const {
     508    11868333 :   return wasforced;
     509             : }
     510             : 
     511          20 : bool GridVessel::applyForce( std::vector<double>& fforces ) {
     512             :   plumed_dbg_assert( fforces.size()==finalForces.size() );
     513          20 :   if( !wasforced ) return false;
     514        7610 :   for(unsigned i=0; i<finalForces.size(); ++i) fforces[i]=finalForces[i];
     515          20 :   wasforced=false; return true;
     516             : }
     517             : 
     518             : }
     519        5874 : }
     520             : 

Generated by: LCOV version 1.14