Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 : Copyright (c) 2016-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 "ActionWithGrid.h" 23 : #include "core/PlumedMain.h" 24 : #include "core/ActionSet.h" 25 : 26 : namespace PLMD { 27 : namespace gridtools { 28 : 29 79 : void ActionWithGrid::registerKeywords( Keywords& keys ) { 30 79 : vesselbase::ActionWithAveraging::registerKeywords( keys ); 31 158 : keys.add("compulsory","BANDWIDTH","the bandwidths for kernel density estimation"); 32 158 : keys.add("compulsory","KERNEL","gaussian","the kernel function you are using. More details on the kernels available " 33 : "in plumed plumed can be found in \\ref kernelfunctions."); 34 158 : keys.add("optional","CONCENTRATION","the concentration parameter for Von Mises-Fisher distributions"); 35 79 : } 36 : 37 70 : ActionWithGrid::ActionWithGrid( const ActionOptions& ao): 38 : Action(ao), 39 : ActionWithAveraging(ao), 40 70 : mygrid(NULL) 41 : { 42 70 : } 43 : 44 65 : std::unique_ptr<GridVessel> ActionWithGrid::createGrid( const std::string& type, const std::string& inputstr ) { 45 : // Start creating the input for the grid 46 65 : std::string vstring = inputstr; 47 130 : if( keywords.exists("KERNEL") ) { 48 90 : std::string vconc; parse("CONCENTRATION",vconc); 49 45 : if( vconc.length()>0 ) { 50 4 : vstring += " TYPE=fibonacci CONCENTRATION=" + vconc; 51 : } else { 52 86 : std::string kstring; parse("KERNEL",kstring); 53 53 : if( kstring=="DISCRETE" ) vstring += " KERNEL=" + kstring; 54 78 : else vstring += " KERNEL=" + kstring + " " + getKeyword("BANDWIDTH"); 55 : } 56 : } 57 130 : vesselbase::VesselOptions da("mygrid","",-1,vstring,this); 58 65 : Keywords keys; gridtools::AverageOnGrid::registerKeywords( keys ); 59 65 : vesselbase::VesselOptions dar( da, keys ); 60 65 : std::unique_ptr<GridVessel> grid; 61 65 : if( type=="histogram" ) { 62 37 : grid=Tools::make_unique<HistogramOnGrid>(dar); 63 28 : } else if( type=="average" ) { 64 8 : grid=Tools::make_unique<AverageOnGrid>(dar); 65 20 : } else if( type=="grid" ) { 66 40 : grid=Tools::make_unique<GridVessel>(dar); 67 : } else { 68 0 : plumed_merror("no way to create grid of type " + type ); 69 : } 70 65 : mygrid=grid.get(); 71 65 : return grid; 72 65 : } 73 : 74 16 : void ActionWithGrid::turnOnDerivatives() { 75 16 : needsDerivatives(); ActionWithValue::turnOnDerivatives(); 76 16 : if( getStride()==1 ) setStride(0); 77 8 : else if( getStride()!=0 ) error("conflicting instructions for grid - stride was set but must be evaluated on every step for derivatives - remove STRIDE keyword"); 78 16 : if( clearstride>1 ) error("conflicting instructions for grid - CLEAR was set but grid must be reset on every step for derivatives - remove CLEAR keyword" ); 79 16 : if( weights.size()>0 ) error("conflicting instructions for grid - LOGWEIGHTS was set but weights are not considered when derivatives of grid are evaluated - remove LOGWEIGHTS keyword"); 80 16 : } 81 : 82 215 : void ActionWithGrid::calculate() { 83 : // Do nothing if derivatives are not required 84 215 : if( doNotCalculateDerivatives() ) return; 85 : // Clear on every step 86 40 : if( mygrid ) clearAverage(); 87 : // Should not be any reweighting so just set these accordingly 88 40 : lweight=0; cweight=1.0; 89 : // Prepare to do the averaging 90 40 : prepareForAveraging(); 91 : // Run all the tasks (if required 92 40 : if( useRunAllTasks ) runAllTasks(); 93 : // This the averaging if it is not done using task list 94 20 : else performOperations( true ); 95 : // Update the norm 96 40 : if( mygrid ) mygrid->setNorm( cweight ); 97 : // Finish the averaging 98 40 : finishAveraging(); 99 : // And reset for next step 100 40 : if( mygrid ) mygrid->reset(); 101 : } 102 : 103 57902 : void ActionWithGrid::runTask( const unsigned& current, MultiValue& myvals ) const { 104 : // Set the weight of this point 105 57902 : myvals.setValue( 0, cweight ); compute( current, myvals ); 106 57902 : } 107 : 108 : } 109 : }