Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 : Copyright (c) 2015-2023 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 "HistogramOnGrid.h"
23 : #include "tools/KernelFunctions.h"
24 :
25 : namespace PLMD {
26 : namespace gridtools {
27 :
28 65 : void HistogramOnGrid::registerKeywords( Keywords& keys ) {
29 65 : GridVessel::registerKeywords( keys );
30 130 : keys.add("compulsory","KERNEL","the type of kernel to use");
31 130 : keys.add("compulsory","BANDWIDTH","the bandwidths");
32 130 : keys.add("compulsory","CONCENTRATION","the concentration parameter for Von Mises-Fisher distributions");
33 65 : }
34 :
35 45 : HistogramOnGrid::HistogramOnGrid( const vesselbase::VesselOptions& da ):
36 : GridVessel(da),
37 45 : neigh_tot(0),
38 45 : addOneKernelAtATime(false),
39 45 : bandwidths(dimension),
40 45 : discrete(false) {
41 90 : if( getType()=="flat" ) {
42 86 : parse("KERNEL",kerneltype);
43 86 : if( kerneltype=="discrete" || kerneltype=="DISCRETE" ) {
44 10 : discrete=true;
45 10 : setNoDerivatives();
46 : } else {
47 66 : parseVector("BANDWIDTH",bandwidths);
48 : }
49 : } else {
50 2 : parse("CONCENTRATION",von_misses_concentration);
51 2 : von_misses_norm = von_misses_concentration / ( 4*pi*sinh( von_misses_concentration ) );
52 : }
53 45 : }
54 :
55 3 : double HistogramOnGrid::getFibonacciCutoff() const {
56 3 : return std::log( epsilon / von_misses_norm ) / von_misses_concentration;
57 : }
58 :
59 19 : bool HistogramOnGrid::noDiscreteKernels() const {
60 19 : return !discrete;
61 : }
62 :
63 50 : void HistogramOnGrid::setBounds( const std::vector<std::string>& smin, const std::vector<std::string>& smax,
64 : const std::vector<unsigned>& nbins, const std::vector<double>& spacing ) {
65 50 : GridVessel::setBounds( smin, smax, nbins, spacing );
66 50 : if( !discrete ) {
67 40 : std::vector<double> point(dimension,0);
68 40 : KernelFunctions kernel( point, bandwidths, kerneltype, "DIAGONAL", 1.0 );
69 40 : neigh_tot=1;
70 40 : nneigh=kernel.getSupport( dx );
71 40 : std::vector<double> support( kernel.getContinuousSupport() );
72 103 : for(unsigned i=0; i<dimension; ++i) {
73 63 : if( pbc[i] && 2*support[i]>getGridExtent(i) ) {
74 0 : error("bandwidth is too large for periodic grid");
75 : }
76 63 : neigh_tot *= (2*nneigh[i]+1);
77 : }
78 : }
79 50 : }
80 :
81 23625 : std::unique_ptr<KernelFunctions> HistogramOnGrid::getKernelAndNeighbors( std::vector<double>& point, unsigned& num_neigh, std::vector<unsigned>& neighbors ) const {
82 23625 : if( discrete ) {
83 4222 : plumed_assert( getType()=="flat" );
84 2111 : num_neigh=1;
85 4222 : for(unsigned i=0; i<dimension; ++i) {
86 2111 : point[i] += 0.5*dx[i];
87 : }
88 2111 : neighbors[0] = getIndex( point );
89 : return NULL;
90 43028 : } else if( getType()=="flat" ) {
91 21457 : auto kernel=Tools::make_unique<KernelFunctions>( point, bandwidths, kerneltype, "DIAGONAL", 1.0 );
92 : // GB: Now values is destroyed when exiting this function.
93 : // I think before there was a leak
94 21457 : std::vector<std::unique_ptr<Value>> values=getVectorOfValues();
95 21457 : kernel->normalize( Tools::unique2raw(values) );
96 42914 : getNeighbors( kernel->getCenter(), nneigh, num_neigh, neighbors );
97 : return kernel;
98 21571 : } else if( getType()=="fibonacci" ) {
99 57 : getNeighbors( point, nneigh, num_neigh, neighbors );
100 : return NULL;
101 : } else {
102 0 : plumed_error();
103 : }
104 : return NULL;
105 : }
106 :
107 57812 : std::vector<std::unique_ptr<Value>> HistogramOnGrid::getVectorOfValues() const {
108 : std::vector<std::unique_ptr<Value>> vv;
109 225230 : for(unsigned i=0; i<dimension; ++i) {
110 167418 : vv.emplace_back(Tools::make_unique<Value>());
111 167418 : if( pbc[i] ) {
112 102964 : vv[i]->setDomain( str_min[i], str_max[i] );
113 : } else {
114 64454 : vv[i]->setNotPeriodic();
115 : }
116 : }
117 57812 : return vv;
118 0 : }
119 :
120 38461 : void HistogramOnGrid::calculate( const unsigned& current, MultiValue& myvals, std::vector<double>& buffer, std::vector<unsigned>& der_list ) const {
121 38461 : if( addOneKernelAtATime ) {
122 : plumed_dbg_assert( myvals.getNumberOfValues()==2 && !wasforced );
123 14908 : std::vector<double> der( dimension );
124 54492 : for(unsigned i=0; i<dimension; ++i) {
125 39584 : der[i]=myvals.getDerivative( 1, i );
126 : }
127 14908 : accumulate( getAction()->getPositionInCurrentTaskList(current), myvals.get(0), myvals.get(1), der, buffer );
128 : } else {
129 : plumed_dbg_assert( myvals.getNumberOfValues()==dimension+2 );
130 23553 : std::vector<double> point( dimension );
131 23553 : double weight=myvals.get(0)*myvals.get( 1+dimension );
132 89610 : for(unsigned i=0; i<dimension; ++i) {
133 66057 : point[i]=myvals.get( 1+i );
134 : }
135 : // Get the kernel
136 : unsigned num_neigh;
137 23553 : std::vector<unsigned> neighbors(1);
138 23553 : std::vector<double> der( dimension );
139 23553 : std::unique_ptr<KernelFunctions> kernel=getKernelAndNeighbors( point, num_neigh, neighbors );
140 :
141 27879 : if( !kernel && getType()=="flat" ) {
142 : plumed_dbg_assert( num_neigh==1 );
143 2106 : der.resize(0);
144 2106 : accumulate( neighbors[0], weight, 1.0, der, buffer );
145 : } else {
146 : double totwforce=0.0;
147 21447 : std::vector<double> intforce( 2*dimension, 0.0 );
148 21447 : std::vector<std::unique_ptr<Value>> vv( getVectorOfValues() );
149 :
150 : double newval;
151 21447 : std::vector<unsigned> tindices( dimension );
152 21447 : std::vector<double> xx( dimension );
153 20582512 : for(unsigned i=0; i<num_neigh; ++i) {
154 20561065 : unsigned ineigh=neighbors[i];
155 20561065 : if( inactive( ineigh ) ) {
156 8712372 : continue ;
157 : }
158 11848693 : getGridPointCoordinates( ineigh, tindices, xx );
159 11848693 : if( kernel ) {
160 47354528 : for(unsigned j=0; j<dimension; ++j) {
161 35510608 : vv[j]->set(xx[j]);
162 : }
163 11843920 : newval = kernel->evaluate( Tools::unique2raw(vv), der, true );
164 : } else {
165 : // Evalulate dot product
166 : double dot=0;
167 19092 : for(unsigned j=0; j<dimension; ++j) {
168 14319 : dot+=xx[j]*point[j];
169 14319 : der[j]=xx[j];
170 : }
171 : // Von misses distribution for concentration parameter
172 4773 : newval = von_misses_norm*exp( von_misses_concentration*dot );
173 : // And final derivatives
174 19092 : for(unsigned j=0; j<dimension; ++j) {
175 14319 : der[j] *= von_misses_concentration*newval;
176 : }
177 : }
178 11848693 : accumulate( ineigh, weight, newval, der, buffer );
179 11848693 : if( wasForced() ) {
180 5744 : accumulateForce( ineigh, weight, der, intforce );
181 5744 : totwforce += myvals.get( 1+dimension )*newval*forces[ineigh];
182 : }
183 : }
184 21447 : if( wasForced() ) {
185 : // Minus sign for kernel here as we are taking derivative with respect to position of center of
186 : // kernel NOT derivative wrt to grid point
187 : double pref = 1;
188 110 : if( kernel ) {
189 : pref = -1;
190 : }
191 110 : unsigned nder = getAction()->getNumberOfDerivatives();
192 110 : unsigned gridbuf = getNumberOfBufferPoints()*getNumberOfQuantities();
193 300 : for(unsigned j=0; j<dimension; ++j) {
194 33940 : for(unsigned k=0; k<myvals.getNumberActive(); ++k) {
195 : unsigned kder=myvals.getActiveIndex(k);
196 33750 : buffer[ bufstart + gridbuf + kder ] += pref*intforce[j]*myvals.getDerivative( j+1, kder );
197 : }
198 : }
199 : // Accumulate the sum of all the weights
200 110 : buffer[ bufstart + gridbuf + nder ] += myvals.get(0);
201 : // Add the derivatives of the weights into the force -- this is separate loop as weights of all parts are considered together
202 32060 : for(unsigned k=0; k<myvals.getNumberActive(); ++k) {
203 : unsigned kder=myvals.getActiveIndex(k);
204 31950 : buffer[ bufstart + gridbuf + kder ] += totwforce*myvals.getDerivative( 0, kder );
205 31950 : buffer[ bufstart + gridbuf + nder + 1 + kder ] += myvals.getDerivative( 0, kder );
206 : }
207 : }
208 21447 : }
209 23553 : }
210 38461 : }
211 :
212 40596 : void HistogramOnGrid::accumulate( const unsigned& ipoint, const double& weight, const double& dens, const std::vector<double>& der, std::vector<double>& buffer ) const {
213 40596 : buffer[bufstart+nper*ipoint] += weight*dens;
214 40596 : if( der.size()>0 )
215 128076 : for(unsigned j=0; j<dimension; ++j) {
216 89586 : buffer[bufstart+nper*ipoint + 1 + j] += weight*der[j];
217 : }
218 40596 : }
219 :
220 5744 : void HistogramOnGrid::accumulateForce( const unsigned& ipoint, const double& weight, const std::vector<double>& der, std::vector<double>& intforce ) const {
221 18414 : for(unsigned j=0; j<der.size(); ++j) {
222 12670 : intforce[j] += forces[ipoint]*weight*der[j];
223 : }
224 5744 : }
225 :
226 20 : void HistogramOnGrid::getFinalForces( const std::vector<double>& buffer, std::vector<double>& finalForces ) {
227 20 : if( finalForces.size()!=getAction()->getNumberOfDerivatives() ) {
228 4 : finalForces.resize( getAction()->getNumberOfDerivatives() );
229 : }
230 : // And the final force
231 20 : unsigned nder = getAction()->getNumberOfDerivatives();
232 : // Derivatives due to normalization
233 20 : unsigned gridbuf = getNumberOfBufferPoints()*getNumberOfQuantities();
234 3815 : for(unsigned i=0; i<finalForces.size(); ++i) {
235 3795 : finalForces[i] = buffer[ bufstart + gridbuf + i ];
236 : }
237 : // Derivatives due to normalization
238 20 : if( !noAverage() ) {
239 15 : unsigned wderstart = bufstart + gridbuf + nder;
240 : double pref=0;
241 3620 : for(unsigned ipoint=0; ipoint<getNumberOfPoints(); ++ipoint) {
242 3605 : pref += forces[ipoint]*buffer[ bufstart + ipoint*nper ] / buffer[wderstart];
243 : }
244 3570 : for(unsigned j=0; j<finalForces.size(); ++j) {
245 3555 : finalForces[j] -= pref*buffer[ wderstart + 1 + j ];
246 : }
247 : }
248 20 : }
249 :
250 156 : void HistogramOnGrid::finish( const std::vector<double>& buffer ) {
251 156 : if( addOneKernelAtATime ) {
252 14975 : for(unsigned i=0; i<getAction()->getCurrentNumberOfActiveTasks(); ++i) {
253 69400 : for(unsigned j=0; j<nper; ++j) {
254 54492 : addDataElement( nper*getAction()->getActiveTask(i)+j, buffer[bufstart+i*nper+j] );
255 : }
256 : }
257 : } else {
258 89 : GridVessel::finish( buffer );
259 : }
260 156 : }
261 :
262 : }
263 : }
|