Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 : Copyright (c) 2015-2020 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 64 : void GridVessel::registerKeywords( Keywords& keys ) {
31 64 : AveragingVessel::registerKeywords( keys );
32 320 : keys.add("compulsory","TYPE","flat","how the grid points are being generated");
33 256 : keys.add("compulsory","COMPONENTS","the names of the components in the vector");
34 256 : keys.add("compulsory","COORDINATES","the names of the coordinates of the grid");
35 256 : keys.add("compulsory","PBC","is the grid periodic in each direction or not");
36 64 : }
37 :
38 65 : 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 715 : noderiv(false)
45 : {
46 130 : std::string geom; parse("TYPE",geom);
47 65 : if( geom=="flat" ) gtype=flat;
48 3 : else if( geom=="fibonacci" ) gtype=fibonacci;
49 0 : else plumed_merror( geom + " is invalid geometry type");
50 195 : std::vector<std::string> compnames; parseVector("COMPONENTS",compnames);
51 195 : std::vector<std::string> coordnames; parseVector("COORDINATES",coordnames);
52 65 : if( gtype==flat ) {
53 62 : dimension=coordnames.size();
54 62 : str_min.resize( dimension); str_max.resize( dimension ); stride.resize( dimension );
55 62 : 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 130 : unsigned n=0; nper=compnames.size()*( 1 + coordnames.size() );
62 65 : arg_names.resize( coordnames.size() + compnames.size()*( 1 + coordnames.size() ) );
63 427 : for(unsigned i=0; i<coordnames.size(); ++i) { arg_names[n] = coordnames[i]; n++; }
64 328 : for(unsigned i=0; i<compnames.size(); ++i) {
65 132 : arg_names[n]=compnames[i]; n++;
66 839 : for(unsigned j=0; j<coordnames.size(); ++j) { arg_names[n] = "d" + compnames[i] + "_" + coordnames[j]; n++; }
67 : }
68 65 : pbc.resize( dimension );
69 195 : std::vector<std::string> spbc( dimension ); parseVector("PBC",spbc);
70 263 : for(unsigned i=0; i<dimension; ++i) {
71 198 : if( spbc[i]=="F" ) pbc[i]=false;
72 28 : else if( spbc[i]=="T" ) pbc[i]=true;
73 0 : else plumed_error();
74 : }
75 65 : }
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 63 : for(unsigned i=0; i<dimension; ++i) tnames[i]=arg_names[i];
81 76 : 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 63 : 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 94 : 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 259 : plumed_assert( gtype==flat && (spacing.size()==dimension || binsin.size()==dimension) );
91 :
92 94 : npoints=1; bounds_set=true;
93 376 : for(unsigned i=0; i<dimension; ++i) {
94 141 : str_min[i]=smin[i]; str_max[i]=smax[i];
95 141 : Tools::convert( str_min[i], min[i] );
96 141 : Tools::convert( str_max[i], max[i] );
97 177 : if( spacing.size()==dimension && binsin.size()==dimension ) {
98 38 : if( spacing[i]==0 ) nbin[i] = binsin[i];
99 : else {
100 102 : 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 68 : if( nbin[i]!=binsin[i] ) plumed_merror("mismatch between input spacing and input number of bins");
103 : }
104 210 : } 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 564 : dx[i] = ( max[i] - min[i] ) / static_cast<double>( nbin[i] );
108 426 : if( !pbc[i] ) { max[i] +=dx[i]; nbin[i]+=1; }
109 141 : stride[i]=npoints;
110 141 : npoints*=nbin[i];
111 : }
112 94 : resize(); // Always resize after setting new bounds as grid size may have have changed
113 94 : }
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 63 : std::string GridVessel::description() {
160 63 : if( !bounds_set ) return "";
161 :
162 : std::string des;
163 49 : if( gtype==flat ) {
164 : des="grid of "; std::string num;
165 83 : for(unsigned i=0; i<dimension-1; ++i) {
166 36 : Tools::convert( nbin[i], num );
167 36 : des += num + " X ";
168 : }
169 94 : Tools::convert( nbin[dimension-1], num );
170 94 : des += num + " equally spaced points between (";
171 101 : for(unsigned i=0; i<dimension-1; ++i) des += str_min[i] + ",";
172 94 : Tools::convert( nbin[dimension-1], num );
173 141 : des += str_min[dimension-1] + ") and (";
174 101 : for(unsigned i=0; i<dimension-1; ++i) des += str_max[i] + ",";
175 141 : 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 207 : void GridVessel::resize() {
184 207 : plumed_massert( nper>0, "Number of datapoints at each grid point has not been set");
185 411 : if( getAction() ) resizeBuffer( getNumberOfBufferPoints()*nper + 1 + 2*getAction()->getNumberOfDerivatives() );
186 207 : setDataSize( npoints*nper ); forces.resize( npoints );
187 207 : if( active.size()!=npoints) active.resize( npoints, true );
188 207 : }
189 :
190 24333366 : 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 48666732 : unsigned index=indices[dimension-1];
194 115441402 : for(unsigned i=dimension-1; i>0; --i) {
195 136662054 : index=index*nbin[i-1]+indices[i-1];
196 : }
197 24333366 : return index;
198 : }
199 :
200 1060387 : 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 5331865 : for(unsigned i=0; i<dimension; ++i) {
203 10678695 : indices[i]=std::floor( (point[i] - min[i])/dx[i] );
204 2226771 : if( pbc[i] ) indices[i]=indices[i]%nbin[i];
205 4180446 : else if( indices[i]>nbin[i] ) {
206 0 : std::string pp, num; Tools::convert( point[0], pp );
207 0 : for(unsigned j=1; j<point.size(); ++j) { Tools::convert( point[j], num ); pp += ", " + num; }
208 0 : plumed_merror("point (" + pp + ") is outside grid range");
209 : }
210 : }
211 1060387 : }
212 :
213 530955 : unsigned GridVessel::getIndex( const std::vector<double>& point ) const {
214 : plumed_dbg_assert( gtype==flat && bounds_set && point.size()==dimension );
215 530955 : if( gtype==flat ) {
216 530955 : std::vector<unsigned> indices(dimension); getIndices( point, indices );
217 530955 : return getIndex( indices );
218 0 : } else if( gtype==fibonacci ) {
219 0 : return getFibonacciIndex( point );
220 : } else {
221 0 : plumed_error();
222 : }
223 : }
224 :
225 57 : unsigned GridVessel::getFibonacciIndex( const std::vector<double>& p ) const {
226 : plumed_dbg_assert( gtype==fibonacci );
227 : // Convert input point to coordinates on cylinder
228 114 : int k=2; double phi = atan2( p[2], p[0] ), sinthet2 = 1 - p[1]*p[1];
229 : // Calculate power to raise golden ratio
230 57 : if( sinthet2<epsilon ) { k = 2; }
231 : else {
232 57 : k = std::floor( std::log( npoints*pi*root5*sinthet2 ) / log_golden2 );
233 57 : if( k<2 ) k = 2;
234 : }
235 57 : double Fk = pow( golden, k ) / root5, F0 = std::round(Fk), F1 = std::round(Fk*golden);
236 57 : Matrix<double> B(2,2), invB(2,2); std::vector<double> thisp(3);
237 114 : B(0,0) = 2*pi*((F0+1)*igolden - std::floor((F0+1)*igolden)) - fib_increment;
238 114 : B(0,1) = 2*pi*((F1+1)*igolden - std::floor((F1+1)*igolden)) - fib_increment;
239 171 : B(1,0) = -2*F0/npoints; B(1,1) = -2*F1/npoints; Invert( B, invB );
240 228 : std::vector<double> vv(2), rc(2); vv[0]=-phi; vv[1] = p[1] - fib_shift;
241 285 : mult( invB, vv, rc ); std::vector<int> c(2); c[0]=std::floor(rc[0]); c[1]=std::floor(rc[1]);
242 : unsigned outind; double mindist = 10000000.;
243 513 : for(int s=0; s<4; ++s) {
244 684 : double ttt, costheta = B(1,0)*( c[0] + s%2 ) + B(1,1)*( c[1] + s/2 ) + fib_shift;
245 228 : if( costheta>1 ) ttt=1; else if( costheta<-1 ) ttt=-1; else ttt=costheta;
246 228 : costheta = 2*ttt - costheta;
247 228 : unsigned i = std::floor( 0.5*npoints*(1+costheta) ); getFibonacciCoordinates( i, thisp );
248 2280 : double dist=0; for(unsigned j=0; j<3; ++j) { double tmp=thisp[j]-p[j]; dist += tmp*tmp; }
249 228 : if( dist<mindist ) { outind = i; mindist = dist; }
250 : }
251 57 : return outind;
252 : }
253 :
254 112439944 : void GridVessel::convertIndexToIndices( const unsigned& index, const std::vector<unsigned>& nnbin, std::vector<unsigned>& indices ) const {
255 337319832 : plumed_dbg_assert( gtype==flat ); unsigned kk=index; indices[0]=index%nnbin[0];
256 329399310 : for(unsigned i=1; i<dimension-1; ++i) {
257 325439049 : kk=(kk-indices[i-1])/nnbin[i-1];
258 325439049 : indices[i]=kk%nnbin[i];
259 : }
260 112439944 : if(dimension>=2) { // I think this is wrong
261 449694548 : indices[dimension-1]=(kk-indices[dimension-2])/nnbin[dimension-2];
262 : }
263 112439944 : }
264 :
265 15891146 : void GridVessel::getIndices( const unsigned& index, std::vector<unsigned>& indices ) const {
266 15891146 : plumed_dbg_assert( gtype==flat ); convertIndexToIndices( index, nbin, indices );
267 15891146 : }
268 :
269 662780 : void GridVessel::getGridPointCoordinates( const unsigned& ipoint, std::vector<double>& x ) const {
270 662780 : std::vector<unsigned> tindices( dimension ); getGridPointCoordinates( ipoint, tindices, x );
271 662780 : }
272 :
273 12511473 : void GridVessel::getGridPointCoordinates( const unsigned& ipoint, std::vector<unsigned>& tindices, std::vector<double>& x ) const {
274 : plumed_dbg_assert( bounds_set && x.size()==dimension && tindices.size()==dimension && ipoint<npoints );
275 12511473 : if( gtype==flat ) {
276 12506356 : getFlatGridCoordinates( ipoint, tindices, x );
277 5117 : } else if( gtype==fibonacci ) {
278 5117 : getFibonacciCoordinates( ipoint, x );
279 : } else {
280 0 : plumed_error();
281 : }
282 12511473 : }
283 :
284 13035200 : void GridVessel::getFlatGridCoordinates( const unsigned& ipoint, std::vector<unsigned>& tindices, std::vector<double>& x ) const {
285 13035200 : plumed_dbg_assert( gtype==flat ); getIndices( ipoint, tindices );
286 202572905 : for(unsigned i=0; i<dimension; ++i) x[i] = min[i] + dx[i]*tindices[i];
287 13035200 : }
288 :
289 86817 : void GridVessel::getFibonacciCoordinates( const unsigned& ipoint, std::vector<double>& x ) const {
290 : plumed_dbg_assert( gtype==fibonacci );
291 260451 : x[1] = (ipoint*fib_offset) + fib_shift; double r = sqrt( 1 - x[1]*x[1] );
292 260451 : double phi = ipoint*fib_increment; x[0] = r*cos(phi); x[2] = r*sin(phi);
293 607719 : double norm=0; for(unsigned j=0; j<3; ++j) norm+=x[j]*x[j];
294 347268 : norm = sqrt(norm); for(unsigned j=0; j<3; ++j) x[j] = x[j] / norm;
295 86817 : }
296 :
297 528844 : void GridVessel::getSplineNeighbors( const unsigned& mybox, unsigned& nneighbors, std::vector<unsigned>& mysneigh ) const {
298 528844 : plumed_dbg_assert( gtype==flat ); unsigned nneigh=unsigned(pow(2.0,int(dimension)));
299 528844 : if( mysneigh.size()!=nneigh ) mysneigh.resize(nneigh);
300 :
301 528844 : unsigned inind; nneighbors = 0;
302 528844 : std::vector<unsigned> tmp_indices( dimension );
303 528844 : std::vector<unsigned> my_indices( dimension );
304 528844 : getIndices( mybox, my_indices );
305 4826348 : for(unsigned i=0; i<nneigh; ++i) {
306 : unsigned tmp=i; inind=0;
307 10878064 : for(unsigned j=0; j<dimension; ++j) {
308 8729312 : unsigned i0=tmp%2+my_indices[j]; tmp/=2;
309 8553152 : if(!pbc[j] && i0==nbin[j]) continue;
310 4500016 : if( pbc[j] && i0==nbin[j]) i0=0;
311 8647712 : tmp_indices[inind++]=i0;
312 : }
313 2148752 : if(inind==dimension ) {
314 2108152 : unsigned findex=getIndex( tmp_indices );
315 4216304 : mysneigh[nneighbors++]=findex;
316 4216304 : plumed_massert( active[findex], "inactive grid point required for splines");
317 : }
318 : }
319 528844 : }
320 :
321 6538251 : double GridVessel::getGridElement( const unsigned& ipoint, const unsigned& jelement ) const {
322 13076502 : plumed_assert( bounds_set && ipoint<npoints && jelement<nper && active[ipoint] );
323 13076502 : return getDataElement( nper*ipoint + jelement );
324 : }
325 :
326 154208 : void GridVessel::setGridElement( const unsigned& ipoint, const unsigned& jelement, const double& value ) {
327 : plumed_dbg_assert( bounds_set && ipoint<npoints && jelement<nper );
328 154208 : setDataElement( nper*ipoint + jelement, value );
329 154208 : }
330 :
331 24200 : void GridVessel::setValueAndDerivatives( const unsigned& ipoint, const unsigned& jelement, const double& value, const std::vector<double>& der ) {
332 : plumed_dbg_assert( !noderiv && jelement<getNumberOfComponents() && der.size()==nbin.size() );
333 145200 : setGridElement( ipoint, jelement, value ); for(unsigned i=0; i<der.size(); ++i) setGridElement( ipoint, jelement+1+i, der[i] );
334 24200 : }
335 :
336 5 : void GridVessel::addToGridElement( const unsigned& ipoint, const unsigned& jelement, const double& value ) {
337 : plumed_dbg_assert( bounds_set && ipoint<npoints && jelement<nper );
338 5 : addDataElement( nper*ipoint + jelement, value );
339 5 : }
340 :
341 12487 : void GridVessel::calculate( const unsigned& current, MultiValue& myvals, std::vector<double>& buffer, std::vector<unsigned>& der_list ) const {
342 : plumed_dbg_assert( myvals.getNumberOfValues()==(nper+1) );
343 139540 : for(unsigned i=0; i<nper; ++i) buffer[bufstart + nper*current + i] += myvals.get(i+1);
344 12487 : }
345 :
346 116 : void GridVessel::finish( const std::vector<double>& buffer ) {
347 116 : if( wasforced ) getFinalForces( buffer, finalForces );
348 96 : else AveragingVessel::finish( buffer );
349 116 : }
350 :
351 0 : double GridVessel::getGridElement( const std::vector<unsigned>& indices, const unsigned& jelement ) const {
352 0 : return getGridElement( getIndex( indices ), jelement );
353 : }
354 :
355 0 : void GridVessel::setGridElement( const std::vector<unsigned>& indices, const unsigned& jelement, const double& value ) {
356 0 : setGridElement( getIndex( indices ), jelement, value );
357 0 : }
358 :
359 197416 : std::vector<std::string> GridVessel::getMin() const {
360 197416 : plumed_dbg_assert( gtype==flat ); return str_min;
361 : }
362 :
363 197449 : std::vector<std::string> GridVessel::getMax() const {
364 197449 : plumed_dbg_assert( gtype==flat ); return str_max;
365 : }
366 :
367 198046 : std::vector<unsigned> GridVessel::getNbin() const {
368 : plumed_dbg_assert( gtype==flat && bounds_set );
369 198046 : std::vector<unsigned> ngrid( dimension );
370 982288 : for(unsigned i=0; i<dimension; ++i) {
371 1520296 : if( !pbc[i] ) ngrid[i]=nbin[i] - 1;
372 24094 : else ngrid[i]=nbin[i];
373 : }
374 198046 : return ngrid;
375 : }
376 :
377 21514 : void GridVessel::getNeighbors( const std::vector<double>& pp, const std::vector<unsigned>& nneigh,
378 : unsigned& num_neighbors, std::vector<unsigned>& neighbors ) const {
379 : plumed_dbg_assert( bounds_set );
380 :
381 21514 : if( gtype == flat ) {
382 : plumed_dbg_assert( nneigh.size()==dimension );
383 21457 : std::vector<unsigned> indices( dimension );
384 340872 : for(unsigned i=0; i<dimension; ++i) indices[i] = std::floor( (pp[i]-min[i])/dx[i] );
385 21457 : getNeighbors( indices, nneigh, num_neighbors, neighbors );
386 57 : } else if( gtype == fibonacci ) {
387 57 : unsigned find = getFibonacciIndex( pp );
388 114 : num_neighbors = 1 + fib_nlist[find].size();
389 57 : if( neighbors.size()<num_neighbors ) neighbors.resize( num_neighbors );
390 14262 : neighbors[0]=find; for(unsigned i=0; i<fib_nlist[find].size(); ++i) neighbors[1+i] = fib_nlist[find][i];
391 : } else {
392 0 : plumed_error();
393 : }
394 21514 : }
395 :
396 40878 : void GridVessel::getNeighbors( const std::vector<unsigned>& indices, const std::vector<unsigned>& nneigh,
397 : unsigned& num_neighbors, std::vector<unsigned>& neighbors ) const {
398 : plumed_dbg_assert( gtype==flat && bounds_set && nneigh.size()==dimension );
399 :
400 40878 : unsigned num_neigh=1; std::vector<unsigned> small_bin( dimension );
401 285170 : for(unsigned i=0; i<dimension; ++i) {
402 366438 : small_bin[i]=(2*nneigh[i]+1);
403 122146 : num_neigh *=small_bin[i];
404 : }
405 40878 : if( neighbors.size()!=num_neigh ) neighbors.resize( num_neigh );
406 :
407 40878 : num_neighbors=0;
408 40878 : std::vector<unsigned> s_indices(dimension), t_indices(dimension);
409 96589676 : for(unsigned index=0; index<num_neigh; ++index) {
410 : bool found=true;
411 96548798 : convertIndexToIndices( index, small_bin, s_indices );
412 675783666 : for(unsigned i=0; i<dimension; ++i) {
413 1158469736 : int i0=s_indices[i]-nneigh[i]+indices[i];
414 289617434 : if(!pbc[i] && i0<0) found=false;
415 382214494 : if(!pbc[i] && i0>=nbin[i]) found=false;
416 304672924 : if( pbc[i] && i0<0) i0=nbin[i]-(-i0)%nbin[i];
417 486637808 : if( pbc[i] && i0>=nbin[i]) i0%=nbin[i];
418 289617434 : t_indices[i]=static_cast<unsigned>(i0);
419 : }
420 96548798 : if( found ) {
421 42187606 : neighbors[num_neighbors]=getIndex( t_indices );
422 21093803 : num_neighbors++;
423 : }
424 : }
425 40878 : }
426 :
427 11 : void GridVessel::setCubeUnits( const double& units ) {
428 11 : plumed_dbg_assert( gtype==flat ); cube_units=units;
429 11 : }
430 :
431 8 : double GridVessel::getCubeUnits() const {
432 8 : plumed_dbg_assert( gtype==flat ); return cube_units;
433 : }
434 :
435 15 : std::string GridVessel::getInputString() const {
436 15 : std::string mstring="COORDINATES="+arg_names[0];
437 30 : for(unsigned i=1; i<dimension; ++i) mstring+="," + arg_names[i];
438 15 : if( gtype==flat ) {
439 : mstring += " TYPE=flat PBC=";
440 15 : if( pbc[0] ) mstring +="T";
441 : else mstring +="F";
442 25 : for(unsigned i=1; i<dimension; ++i) {
443 10 : if( pbc[i] ) mstring +=",T";
444 : else mstring +=",F";
445 : }
446 0 : } else if( gtype==fibonacci ) {
447 : mstring += " TYPE=fibonacci";
448 : }
449 15 : return mstring;
450 : }
451 :
452 528844 : double GridVessel::getValueAndDerivatives( const std::vector<double>& x, const unsigned& ind, std::vector<double>& der ) const {
453 : plumed_dbg_assert( gtype==flat && der.size()==dimension && !noderiv && ind<getNumberOfComponents() );
454 :
455 1057688 : double X,X2,X3,value=0; der.assign(der.size(),0.0);
456 528844 : std::vector<double> fd(dimension);
457 528844 : std::vector<double> C(dimension);
458 528844 : std::vector<double> D(dimension);
459 528844 : std::vector<double> dder(dimension);
460 :
461 528844 : std::vector<unsigned> nindices(dimension); unsigned n_neigh;
462 528844 : std::vector<unsigned> indices(dimension); getIndices( x, indices );
463 528844 : std::vector<unsigned> neigh; getSplineNeighbors( getIndex(indices), n_neigh, neigh );
464 528844 : std::vector<double> xfloor(dimension); getFlatGridCoordinates( getIndex(x), nindices, xfloor );
465 :
466 : // loop over neighbors
467 4745148 : for(unsigned int ipoint=0; ipoint<n_neigh; ++ipoint) {
468 4216304 : double grid=getGridElement(neigh[ipoint], ind*(1+dimension) );
469 14958520 : for(unsigned j=0; j<dimension; ++j) dder[j] = getGridElement( neigh[ipoint], ind*(1+dimension) + 1 + j );
470 :
471 2108152 : getIndices( neigh[ipoint], nindices );
472 : double ff=1.0;
473 :
474 10675064 : for(unsigned j=0; j<dimension; ++j) {
475 : int x0=1;
476 12850368 : if(nindices[j]==indices[j]) x0=0;
477 4283456 : double ddx=dx[j];
478 8566912 : X=fabs((x[j]-xfloor[j])/ddx-(double)x0);
479 4283456 : X2=X*X;
480 4283456 : X3=X2*X;
481 : double yy;
482 4283456 : if(fabs(grid)<0.0000001) yy=0.0;
483 4269609 : else yy=-dder[j]/grid;
484 8566912 : C[j]=(1.0-3.0*X2+2.0*X3) - (x0?-1.0:1.0)*yy*(X-2.0*X2+X3)*ddx;
485 8566912 : D[j]=( -6.0*X +6.0*X2) - (x0?-1.0:1.0)*yy*(1.0-4.0*X +3.0*X2)*ddx;
486 4283456 : D[j]*=(x0?-1.0:1.0)/ddx;
487 4283456 : ff*=C[j];
488 : }
489 10675064 : for(unsigned j=0; j<dimension; ++j) {
490 8566912 : fd[j]=D[j];
491 22024048 : for(unsigned i=0; i<dimension; ++i) if(i!=j) fd[j]*=C[i];
492 : }
493 2108152 : value+=grid*ff;
494 14958520 : for(unsigned j=0; j<dimension; ++j) der[j]+=grid*fd[j];
495 : }
496 528844 : return value;
497 : }
498 :
499 3 : void GridVessel::activateThesePoints( const std::vector<bool>& to_activate ) {
500 : plumed_dbg_assert( to_activate.size()==npoints );
501 59979 : for(unsigned i=0; i<npoints; ++i) active[i]=to_activate[i];
502 3 : }
503 :
504 20 : void GridVessel::setForce( const std::vector<double>& inforces ) {
505 : plumed_dbg_assert( inforces.size()==npoints );
506 5725 : wasforced=true; for(unsigned i=0; i<npoints; ++i) forces[i]=inforces[i];
507 20 : }
508 :
509 11870273 : bool GridVessel::wasForced() const {
510 11870273 : return wasforced;
511 : }
512 :
513 20 : bool GridVessel::applyForce( std::vector<double>& fforces ) {
514 : plumed_dbg_assert( fforces.size()==finalForces.size() );
515 20 : if( !wasforced ) return false;
516 11425 : for(unsigned i=0; i<finalForces.size(); ++i) fforces[i]=finalForces[i];
517 20 : wasforced=false; return true;
518 : }
519 :
520 : }
521 5517 : }
522 :
|