Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 : Copyright (c) 2014-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 "MultiValue.h"
23 :
24 : namespace PLMD {
25 :
26 109789 : MultiValue::MultiValue( const unsigned& nvals, const unsigned& nder ):
27 : values(nvals),
28 109789 : nderivatives(nder),
29 109789 : derivatives(nvals*nder),
30 : tmpval(0),
31 : tmpder(nder),
32 439156 : atLeastOneSet(false)
33 : {
34 109789 : std::vector<unsigned> myind( nder );
35 18728057 : for(unsigned i=0; i<nder; ++i) myind[i]=i;
36 109789 : hasDerivatives.createIndexListFromVector( myind );
37 109789 : }
38 :
39 41822 : void MultiValue::resize( const unsigned& nvals, const unsigned& nder ) {
40 41822 : values.resize(nvals); nderivatives=nder; derivatives.resize( nvals*nder );
41 41822 : tmpder.resize( nder ); hasDerivatives.clear(); std::vector<unsigned> myind( nder );
42 21589196 : for(unsigned i=0; i<nder; ++i) myind[i]=i;
43 41822 : hasDerivatives.createIndexListFromVector( myind );
44 41822 : atLeastOneSet=false;
45 41822 : }
46 :
47 2103213 : void MultiValue::clearAll() {
48 2103213 : if( atLeastOneSet && !hasDerivatives.updateComplete() ) hasDerivatives.updateActiveMembers();
49 12540836 : for(unsigned i=0; i<values.size(); ++i) clear(i);
50 2103213 : clearTemporyDerivatives(); hasDerivatives.deactivateAll(); atLeastOneSet=false;
51 2103213 : }
52 :
53 8334410 : void MultiValue::clear( const unsigned& ival ) {
54 16668820 : values[ival]=0;
55 8334410 : unsigned base=ival*nderivatives, ndert=hasDerivatives.getNumberActive();
56 925456631 : for(unsigned i=0; i<ndert; ++i) derivatives[ base+hasDerivatives[i] ]=0.;
57 8334410 : }
58 :
59 2103213 : void MultiValue::clearTemporyDerivatives() {
60 2103213 : unsigned ndert=hasDerivatives.getNumberActive(); tmpval=0.;
61 245475543 : for(unsigned i=0; i<ndert; ++i) tmpder[ hasDerivatives[i] ]=0.;
62 2103213 : }
63 :
64 523625 : 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 523625 : if( !hasDerivatives.updateComplete() ) hasDerivatives.updateActiveMembers();
67 :
68 : plumed_dbg_assert( off<stride );
69 523625 : unsigned base=nderivatives*ival, ndert=hasDerivatives.getNumberActive();
70 523625 : unsigned start=bufstart+stride*(nderivatives+1)*iout + stride;
71 60283107 : for(unsigned i=0; i<ndert; ++i) {
72 : unsigned jder=hasDerivatives[i];
73 89639223 : buffer[start+jder*stride] += df*derivatives[base+jder];
74 : }
75 523625 : }
76 :
77 57565 : void MultiValue::copyValues( MultiValue& outvals ) const {
78 : plumed_dbg_assert( values.size()<=outvals.getNumberOfValues() );
79 492110 : for(unsigned i=0; i<values.size(); ++i) outvals.setValue( i, values[i] );
80 :
81 57565 : }
82 :
83 50656 : void MultiValue::copyDerivatives( MultiValue& outvals ) {
84 : plumed_dbg_assert( values.size()<=outvals.getNumberOfValues() && nderivatives<=outvals.getNumberOfDerivatives() );
85 50656 : if( !hasDerivatives.updateComplete() ) hasDerivatives.updateActiveMembers();
86 :
87 50656 : outvals.atLeastOneSet=true; unsigned ndert=hasDerivatives.getNumberActive();
88 7482514 : for(unsigned j=0; j<ndert; ++j) {
89 3715929 : unsigned jder=hasDerivatives[j]; outvals.hasDerivatives.activate(jder);
90 : }
91 :
92 : unsigned base=0, obase=0;
93 436838 : for(unsigned i=0; i<values.size(); ++i) {
94 16548350 : for(unsigned j=0; j<ndert; ++j) {
95 : unsigned jder=hasDerivatives[j];
96 24654762 : outvals.derivatives[obase+jder] += derivatives[base+jder];
97 : }
98 111842 : obase+=outvals.nderivatives; base+=nderivatives;
99 : }
100 50656 : }
101 :
102 1869862 : void MultiValue::quotientRule( const unsigned& nder, const unsigned& oder ) {
103 : plumed_dbg_assert( nder<values.size() && oder<values.size() );
104 1869862 : if( !hasDerivatives.updateComplete() ) hasDerivatives.updateActiveMembers();
105 :
106 1869862 : unsigned ndert=hasDerivatives.getNumberActive(); double wpref;
107 1869862 : unsigned obase=oder*nderivatives, nbase=nder*nderivatives;
108 :
109 1869862 : if( fabs(tmpval)>epsilon ) { wpref=1.0/tmpval; }
110 : else { wpref=1.0; }
111 :
112 3739724 : double pref = values[nder]*wpref*wpref;
113 357925798 : for(unsigned j=0; j<ndert; ++j) {
114 : unsigned jder=hasDerivatives[j];
115 712111872 : derivatives[obase+jder] = wpref*derivatives[nbase+jder] - pref*tmpder[jder];
116 : }
117 5609586 : values[oder] = wpref*values[nder];
118 1869862 : }
119 :
120 4839 : }
|