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 106 : void ActionWithGrid::registerKeywords( Keywords& keys ) { 30 106 : vesselbase::ActionWithAveraging::registerKeywords( keys ); 31 212 : keys.add("compulsory","BANDWIDTH","the bandwidths for kernel density estimation"); 32 212 : 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 212 : keys.add("optional","CONCENTRATION","the concentration parameter for Von Mises-Fisher distributions"); 35 106 : } 36 : 37 70 : ActionWithGrid::ActionWithGrid( const ActionOptions& ao): 38 : Action(ao), 39 : ActionWithAveraging(ao), 40 70 : mygrid(NULL) { 41 70 : } 42 : 43 65 : std::unique_ptr<GridVessel> ActionWithGrid::createGrid( const std::string& type, const std::string& inputstr ) { 44 : // Start creating the input for the grid 45 65 : std::string vstring = inputstr; 46 130 : if( keywords.exists("KERNEL") ) { 47 : std::string vconc; 48 90 : parse("CONCENTRATION",vconc); 49 45 : if( vconc.length()>0 ) { 50 4 : vstring += " TYPE=fibonacci CONCENTRATION=" + vconc; 51 : } else { 52 : std::string kstring; 53 86 : parse("KERNEL",kstring); 54 43 : if( kstring=="DISCRETE" ) { 55 20 : vstring += " KERNEL=" + kstring; 56 : } else { 57 66 : vstring += " KERNEL=" + kstring + " " + getKeyword("BANDWIDTH"); 58 : } 59 : } 60 : } 61 130 : vesselbase::VesselOptions da("mygrid","",-1,vstring,this); 62 65 : Keywords keys; 63 65 : gridtools::AverageOnGrid::registerKeywords( keys ); 64 65 : vesselbase::VesselOptions dar( da, keys ); 65 65 : std::unique_ptr<GridVessel> grid; 66 65 : if( type=="histogram" ) { 67 37 : grid=Tools::make_unique<HistogramOnGrid>(dar); 68 28 : } else if( type=="average" ) { 69 8 : grid=Tools::make_unique<AverageOnGrid>(dar); 70 20 : } else if( type=="grid" ) { 71 40 : grid=Tools::make_unique<GridVessel>(dar); 72 : } else { 73 0 : plumed_merror("no way to create grid of type " + type ); 74 : } 75 : // cppcheck-suppress danglingLifetime 76 65 : mygrid=grid.get(); 77 65 : return grid; 78 65 : } 79 : 80 16 : void ActionWithGrid::turnOnDerivatives() { 81 16 : needsDerivatives(); 82 16 : ActionWithValue::turnOnDerivatives(); 83 16 : if( getStride()==1 ) { 84 8 : setStride(0); 85 8 : } else if( getStride()!=0 ) { 86 0 : error("conflicting instructions for grid - stride was set but must be evaluated on every step for derivatives - remove STRIDE keyword"); 87 : } 88 16 : if( clearstride>1 ) { 89 0 : error("conflicting instructions for grid - CLEAR was set but grid must be reset on every step for derivatives - remove CLEAR keyword" ); 90 : } 91 16 : if( weights.size()>0 ) { 92 0 : error("conflicting instructions for grid - LOGWEIGHTS was set but weights are not considered when derivatives of grid are evaluated - remove LOGWEIGHTS keyword"); 93 : } 94 16 : } 95 : 96 215 : void ActionWithGrid::calculate() { 97 : // Do nothing if derivatives are not required 98 215 : if( doNotCalculateDerivatives() ) { 99 : return; 100 : } 101 : // Clear on every step 102 40 : if( mygrid ) { 103 20 : clearAverage(); 104 : } 105 : // Should not be any reweighting so just set these accordingly 106 40 : lweight=0; 107 40 : cweight=1.0; 108 : // Prepare to do the averaging 109 40 : prepareForAveraging(); 110 : // Run all the tasks (if required 111 40 : if( useRunAllTasks ) { 112 20 : runAllTasks(); 113 : } 114 : // This the averaging if it is not done using task list 115 : else { 116 20 : performOperations( true ); 117 : } 118 : // Update the norm 119 40 : if( mygrid ) { 120 : mygrid->setNorm( cweight ); 121 : } 122 : // Finish the averaging 123 40 : finishAveraging(); 124 : // And reset for next step 125 40 : if( mygrid ) { 126 20 : mygrid->reset(); 127 : } 128 : } 129 : 130 57902 : void ActionWithGrid::runTask( const unsigned& current, MultiValue& myvals ) const { 131 : // Set the weight of this point 132 : myvals.setValue( 0, cweight ); 133 57902 : compute( current, myvals ); 134 57902 : } 135 : 136 : } 137 : }