Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 : Copyright (c) 2014-2018 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 "MultiValue.h"
23 :
24 : namespace PLMD {
25 :
26 109870 : MultiValue::MultiValue( const unsigned& nvals, const unsigned& nder ):
27 : values(nvals),
28 : nderivatives(nder),
29 110897 : derivatives(nvals*nder),
30 : tmpval(0),
31 : tmpder(nder),
32 220767 : atLeastOneSet(false)
33 : {
34 110587 : std::vector<unsigned> myind( nder );
35 208380 : for(unsigned i=0; i<nder; ++i) myind[i]=i;
36 110825 : hasDerivatives.createIndexListFromVector( myind );
37 110890 : }
38 :
39 39837 : void MultiValue::resize( const unsigned& nvals, const unsigned& nder ) {
40 39837 : values.resize(nvals); nderivatives=nder; derivatives.resize( nvals*nder );
41 39930 : tmpder.resize( nder ); hasDerivatives.clear(); std::vector<unsigned> myind( nder );
42 43732 : for(unsigned i=0; i<nder; ++i) myind[i]=i;
43 39929 : hasDerivatives.createIndexListFromVector( myind );
44 39926 : atLeastOneSet=false;
45 39931 : }
46 :
47 1839612 : void MultiValue::clearAll() {
48 1839612 : if( atLeastOneSet && !hasDerivatives.updateComplete() ) hasDerivatives.updateActiveMembers();
49 1840502 : for(unsigned i=0; i<values.size(); ++i) clear(i);
50 1841180 : clearTemporyDerivatives(); hasDerivatives.deactivateAll(); atLeastOneSet=false;
51 1840508 : }
52 :
53 6979187 : void MultiValue::clear( const unsigned& ival ) {
54 6979187 : values[ival]=0;
55 6981226 : unsigned base=ival*nderivatives, ndert=hasDerivatives.getNumberActive();
56 7066529 : for(unsigned i=0; i<ndert; ++i) derivatives[ base+hasDerivatives[i] ]=0.;
57 6981321 : }
58 :
59 1840988 : void MultiValue::clearTemporyDerivatives() {
60 1840988 : unsigned ndert=hasDerivatives.getNumberActive(); tmpval=0.;
61 1866117 : for(unsigned i=0; i<ndert; ++i) tmpder[ hasDerivatives[i] ]=0.;
62 1841083 : }
63 :
64 509577 : void MultiValue::chainRule( const unsigned& ival, const unsigned& iout, const unsigned& stride, const unsigned& off,
65 : const double& df, const unsigned& bufstart, std::vector<double>& buffer ) {
66 509577 : if( !hasDerivatives.updateComplete() ) hasDerivatives.updateActiveMembers();
67 :
68 : plumed_dbg_assert( off<stride );
69 509971 : unsigned base=nderivatives*ival, ndert=hasDerivatives.getNumberActive();
70 645803 : unsigned start=bufstart+stride*(nderivatives+1)*iout + stride;
71 29702184 : for(unsigned i=0; i<ndert; ++i) {
72 29192108 : unsigned jder=hasDerivatives[i];
73 29265701 : buffer[start+jder*stride] += df*derivatives[base+jder];
74 : }
75 510076 : }
76 :
77 57167 : void MultiValue::copyValues( MultiValue& outvals ) const {
78 : plumed_dbg_assert( values.size()<=outvals.getNumberOfValues() );
79 57167 : for(unsigned i=0; i<values.size(); ++i) outvals.setValue( i, values[i] );
80 :
81 57186 : }
82 :
83 50271 : void MultiValue::copyDerivatives( MultiValue& outvals ) {
84 : plumed_dbg_assert( values.size()<=outvals.getNumberOfValues() && nderivatives<=outvals.getNumberOfDerivatives() );
85 50271 : if( !hasDerivatives.updateComplete() ) hasDerivatives.updateActiveMembers();
86 :
87 50277 : outvals.atLeastOneSet=true; unsigned ndert=hasDerivatives.getNumberActive();
88 3726677 : for(unsigned j=0; j<ndert; ++j) {
89 3676404 : unsigned jder=hasDerivatives[j]; outvals.hasDerivatives.activate(jder);
90 : }
91 :
92 50273 : unsigned base=0, obase=0;
93 151574 : for(unsigned i=0; i<values.size(); ++i) {
94 7578365 : for(unsigned j=0; j<ndert; ++j) {
95 7477064 : unsigned jder=hasDerivatives[j];
96 7501723 : outvals.derivatives[obase+jder] += derivatives[base+jder];
97 : }
98 101301 : obase+=outvals.nderivatives; base+=nderivatives;
99 : }
100 50274 : }
101 :
102 1627865 : void MultiValue::quotientRule( const unsigned& nder, const unsigned& oder ) {
103 : plumed_dbg_assert( nder<values.size() && oder<values.size() );
104 1627865 : if( !hasDerivatives.updateComplete() ) hasDerivatives.updateActiveMembers();
105 :
106 1627878 : unsigned ndert=hasDerivatives.getNumberActive(); double wpref;
107 1627876 : unsigned obase=oder*nderivatives, nbase=nder*nderivatives;
108 :
109 1627876 : if( fabs(tmpval)>epsilon ) { wpref=1.0/tmpval; }
110 0 : else { wpref=1.0; }
111 :
112 1627876 : double pref = values[nder]*wpref*wpref;
113 168815304 : for(unsigned j=0; j<ndert; ++j) {
114 167187428 : unsigned jder=hasDerivatives[j];
115 167189187 : derivatives[obase+jder] = wpref*derivatives[nbase+jder] - pref*tmpder[jder];
116 : }
117 1627876 : values[oder] = wpref*values[nder];
118 1627874 : }
119 :
120 2523 : }
|