Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 : Copyright (c) 2016-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 "ActionWithAveraging.h"
23 : #include "core/PlumedMain.h"
24 : #include "core/ActionSet.h"
25 :
26 : namespace PLMD {
27 : namespace vesselbase {
28 :
29 69 : void ActionWithAveraging::registerKeywords( Keywords& keys ) {
30 69 : Action::registerKeywords( keys ); ActionPilot::registerKeywords( keys ); ActionAtomistic::registerKeywords( keys );
31 69 : ActionWithArguments::registerKeywords( keys ); ActionWithValue::registerKeywords( keys ); ActionWithVessel::registerKeywords( keys );
32 345 : keys.add("compulsory","STRIDE","1","the frequency with which the data should be collected and added to the quantity being averaged");
33 345 : keys.add("compulsory","CLEAR","0","the frequency with which to clear all the accumulated data. The default value "
34 : "of 0 implies that all the data will be used and that the grid will never be cleared");
35 276 : keys.add("optional","LOGWEIGHTS","list of actions that calculates log weights that should be used to weight configurations when calculating averages");
36 345 : keys.add("compulsory","NORMALIZATION","true","This controls how the data is normalized it can be set equal to true, false or ndata. The differences between these options are explained in the manual page for \\ref HISTOGRAM");
37 138 : keys.remove("NUMERICAL_DERIVATIVES");
38 69 : }
39 :
40 57 : ActionWithAveraging::ActionWithAveraging( const ActionOptions& ao ):
41 : Action(ao),
42 : ActionPilot(ao),
43 : ActionAtomistic(ao),
44 : ActionWithArguments(ao),
45 : ActionWithValue(ao),
46 : ActionWithVessel(ao),
47 : myaverage(NULL),
48 : useRunAllTasks(false),
49 : clearstride(0),
50 114 : lweight(0),cweight(0)
51 : {
52 114 : if( keywords.exists("CLEAR") ) {
53 100 : parse("CLEAR",clearstride);
54 50 : if( clearstride>0 ) {
55 12 : if( clearstride%getStride()!=0 ) error("CLEAR parameter must be a multiple of STRIDE");
56 12 : log.printf(" clearing grid every %u steps \n",clearstride);
57 : }
58 : }
59 114 : if( keywords.exists("LOGWEIGHTS") ) {
60 150 : std::vector<std::string> wwstr; parseVector("LOGWEIGHTS",wwstr);
61 50 : if( wwstr.size()>0 ) log.printf(" reweighting using weights from ");
62 50 : std::vector<Value*> arg( getArguments() );
63 118 : for(unsigned i=0; i<wwstr.size(); ++i) {
64 12 : ActionWithValue* val = plumed.getActionSet().selectWithLabel<ActionWithValue*>(wwstr[i]);
65 6 : if( !val ) error("could not find value named");
66 18 : weights.push_back( val->copyOutput(val->getLabel()) );
67 18 : arg.push_back( val->copyOutput(val->getLabel()) );
68 12 : log.printf("%s ",wwstr[i].c_str() );
69 : }
70 50 : if( wwstr.size()>0 ) log.printf("\n");
71 44 : else log.printf(" weights are all equal to one\n");
72 50 : requestArguments( arg );
73 : }
74 114 : if( keywords.exists("NORMALIZATION") ) {
75 100 : std::string normstr; parse("NORMALIZATION",normstr);
76 50 : if( normstr=="true" ) normalization=t;
77 26 : else if( normstr=="false" ) normalization=f;
78 17 : else if( normstr=="ndata" ) normalization=ndata;
79 0 : else error("invalid instruction for NORMALIZATION flag should be true, false, or ndata");
80 : }
81 57 : }
82 :
83 49 : void ActionWithAveraging::setAveragingAction( AveragingVessel* av_vessel, const bool& usetasks ) {
84 49 : myaverage=av_vessel; addVessel( myaverage );
85 49 : useRunAllTasks=usetasks; resizeFunctions();
86 49 : }
87 :
88 1520 : void ActionWithAveraging::lockRequests() {
89 : ActionAtomistic::lockRequests();
90 : ActionWithArguments::lockRequests();
91 1520 : }
92 :
93 1520 : void ActionWithAveraging::unlockRequests() {
94 : ActionAtomistic::unlockRequests();
95 : ActionWithArguments::unlockRequests();
96 1520 : }
97 :
98 0 : void ActionWithAveraging::calculateNumericalDerivatives(PLMD::ActionWithValue*) {
99 0 : error("not possible to compute numerical derivatives for this action");
100 0 : }
101 :
102 1523 : void ActionWithAveraging::update() {
103 1523 : if( (clearstride!=1 && getStep()==0) || !onStep() ) return;
104 : // Clear if it is time to reset
105 1444 : if( myaverage ) {
106 151 : if( myaverage->wasreset() ) clearAverage();
107 : }
108 : // Calculate the weight for all reweighting
109 1444 : if ( weights.size()>0 ) {
110 828 : double sum=0; for(unsigned i=0; i<weights.size(); ++i) sum+=weights[i]->get();
111 207 : lweight=sum; cweight = exp( sum );
112 : } else {
113 1237 : lweight=0; cweight=1.0;
114 : }
115 : // Prepare to do the averaging
116 1444 : prepareForAveraging();
117 : // Run all the tasks (if required
118 1444 : if( useRunAllTasks ) runAllTasks();
119 : // This the averaging if it is not done using task list
120 1320 : else performOperations( true );
121 : // Update the norm
122 1444 : double normt = cweight; if( normalization==ndata ) normt = 1;
123 1595 : if( myaverage ) myaverage->setNorm( normt + myaverage->getNorm() );
124 : // Finish the averaging
125 1444 : finishAveraging();
126 : // By resetting here we are ensuring that the grid will be cleared at the start of the next step
127 1444 : if( myaverage ) {
128 151 : if( getStride()==0 || (clearstride>0 && getStep()%clearstride==0) ) myaverage->reset();
129 : }
130 : }
131 :
132 83 : void ActionWithAveraging::clearAverage() { plumed_assert( myaverage->wasreset() ); myaverage->clear(); }
133 :
134 0 : void ActionWithAveraging::performOperations( const bool& from_update ) { plumed_error(); }
135 :
136 :
137 : }
138 4839 : }
|