Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 : Copyright (c) 2016,2017 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 "PathProjectionCalculator.h" 23 : #include "core/ActionWithValue.h" 24 : #include "core/ActionWithArguments.h" 25 : #include "core/ActionRegister.h" 26 : #include "core/ActionSet.h" 27 : #include "core/PlumedMain.h" 28 : 29 : namespace PLMD { 30 : namespace mapping { 31 : 32 26 : void PathProjectionCalculator::registerKeywords(Keywords& keys) { 33 52 : keys.addInputKeyword("compulsory","ARG","matrix","the labels of the matrix that contains the vectors of displacements from each frame in the path"); 34 26 : keys.add("compulsory","METRIC","the method to use for computing the displacement vectors between the reference frames"); 35 26 : keys.add("compulsory","METRIC_COMPONENT","if the final action in your metric contains multiple components this keyword is used to specify the component that should be used"); 36 52 : keys.addInputKeyword("compulsory","REFERENCE","vector","labels for actions that contain reference coordinates for each point on the path"); 37 26 : } 38 : 39 11 : PathProjectionCalculator::PathProjectionCalculator( Action* act ): 40 11 : mypath_obj(NULL) { 41 11 : ActionWithArguments* aarg=dynamic_cast<ActionWithArguments*>( act ); 42 11 : if( aarg ) { 43 7 : mypath_obj = aarg->getPntrToArgument(0); 44 : // Check that we have only one argument as input 45 7 : if( aarg->getNumberOfArguments()!=1 ) { 46 0 : act->error("should only have one argument to this function"); 47 : } 48 : } 49 : // Ensure that values are stored in base calculation and that PLUMED doesn't try to calculate this in the stream 50 : // Check that the input is a matrix 51 11 : if( mypath_obj ) 52 7 : if( mypath_obj->getRank()!=2 ) { 53 0 : act->error("the input to this action should be a matrix"); 54 : } 55 : // Get the labels for the reference points 56 : std::vector<std::string> reference_data; 57 11 : act->parseVector("REFERENCE", reference_data); 58 11 : std::vector<colvar::RMSDVector*> allrmsd = act->plumed.getActionSet().select<colvar::RMSDVector*>(); 59 11 : ActionWithArguments::interpretArgumentList( reference_data, act->plumed.getActionSet(), act, refargs ); 60 28 : for(unsigned i=0; i<refargs.size(); ++i ) { 61 17 : Action* thisact = refargs[i]->getPntrToAction(); 62 17 : for(unsigned j=0; j<allrmsd.size(); ++j) { 63 5 : if( allrmsd[j]->checkForDependency(thisact) ) { 64 5 : rmsd_objects.push_back( allrmsd[j] ); 65 : break; 66 : } 67 : } 68 17 : if( !refargs[i]->isConstant() ) { 69 0 : act->error("input" + refargs[i]->getName() + " is not constant"); 70 : } 71 17 : if( refargs[i]->getRank()==2 && reference_data.size()!=1 ) { 72 0 : act->error("should only be one matrix in input to path projection object"); 73 : } 74 17 : if( refargs[i]->getRank()>0 && refargs[i]->getShape()[0]!=refargs[0]->getShape()[0] ) { 75 0 : act->error("mismatch in number of reference frames in input to reference_data"); 76 : } 77 : } 78 : // Create a plumed main object to compute distances between reference configurations 79 11 : int s=sizeof(double); 80 11 : metric.cmd("setRealPrecision",&s); 81 11 : metric.cmd("setMDEngine","plumed"); 82 11 : int nat=0; 83 11 : metric.cmd("setNatoms",&nat); 84 11 : metric.cmd("setNoVirial"); 85 11 : unsigned nargs=refargs.size(); 86 11 : if( refargs[0]->getRank()==2 ) { 87 6 : nargs = refargs[0]->getShape()[1]; 88 : } 89 : std::string str_nargs; 90 11 : Tools::convert( nargs, str_nargs ); 91 11 : std::string period_str=" PERIODIC=NO"; 92 11 : if( mypath_obj && mypath_obj->isPeriodic() ) { 93 : std::string min, max; 94 0 : mypath_obj->getDomain( min, max ); 95 0 : period_str=" PERIODIC=" + min + "," + max; 96 : } 97 22 : metric.readInputLine("arg1: PUT UNIT=number SHAPE=" + str_nargs + period_str, true); 98 22 : metric.readInputLine("arg2: PUT UNIT=number SHAPE=" + str_nargs + period_str, true); 99 11 : double tstep=1.0; 100 11 : metric.cmd("setTimestep",&tstep); 101 : std::string inp; 102 22 : act->parse("METRIC",inp); 103 : inp += " ARG=arg2,arg1"; 104 : const char* cinp=inp.c_str(); 105 11 : std::vector<std::string> input=Tools::getWords(inp); 106 11 : if( input.size()==1 && !actionRegister().check(input[0]) ) { 107 0 : metric.cmd("setPlumedDat",cinp); 108 0 : metric.cmd("init"); 109 : } else { 110 11 : metric.cmd("init"); 111 11 : metric.cmd("readInputLine",cinp); 112 : } 113 : // Now setup stuff to retrieve the final displacement 114 11 : unsigned aind = metric.getActionSet().size()-1; 115 : while( true ) { 116 22 : const ActionShortcut* as=dynamic_cast<const ActionShortcut*>( metric.getActionSet()[aind].get() ); 117 22 : if( !as ) { 118 : break ; 119 : } 120 11 : aind = aind - 1; 121 : plumed_assert( aind>=0 ); 122 11 : } 123 11 : ActionWithValue* fav = dynamic_cast<ActionWithValue*>( metric.getActionSet()[aind].get() ); 124 11 : if( !fav ) { 125 0 : act->error("final value should calculate relevant value that you want as reference"); 126 : } 127 11 : std::string name = (fav->copyOutput(0))->getName(); 128 11 : if( fav->getNumberOfComponents()>1 ) { 129 : std::string comp; 130 6 : act->parse("METRIC_COMPONENT",comp); 131 12 : name = fav->getLabel() + "." + comp; 132 : } 133 : long rank; 134 22 : metric.cmd("getDataRank " + name, &rank ); 135 11 : if( rank==0 ) { 136 0 : rank=1; 137 : } 138 11 : std::vector<long> ishape( rank ); 139 22 : metric.cmd("getDataShape " + name, &ishape[0] ); 140 : unsigned nvals=1; 141 22 : for(unsigned i=0; i<ishape.size(); ++i) { 142 11 : nvals *= ishape[i]; 143 : } 144 11 : data.resize( nvals ); 145 22 : metric.cmd("setMemoryForData " + name, &data[0] ); 146 22 : } 147 : 148 1114 : unsigned PathProjectionCalculator::getNumberOfFrames() const { 149 1114 : return refargs[0]->getShape()[0]; 150 : } 151 : 152 12820 : void PathProjectionCalculator::computeVectorBetweenFrames( const unsigned& ifrom, const unsigned& ito ) { 153 12820 : int step = 1; 154 12820 : metric.cmd("setStep",&step); 155 12820 : std::vector<double> valdata1( data.size() ), valdata2( data.size() ); 156 12820 : getReferenceConfiguration( ito, valdata2 ); 157 12820 : getReferenceConfiguration( ifrom, valdata1 ); 158 12820 : metric.cmd("setValue arg1", &valdata1[0] ); 159 12820 : metric.cmd("setValue arg2", &valdata2[0] ); 160 12820 : metric.cmd("calc"); 161 12820 : } 162 : 163 12820 : void PathProjectionCalculator::getDisplaceVector( const unsigned& ifrom, const unsigned& ito, std::vector<double>& displace ) { 164 12820 : if( displace.size()!=data.size() ) { 165 2215 : displace.resize( data.size() ); 166 : } 167 12820 : computeVectorBetweenFrames( ifrom, ito ); 168 447743 : for(unsigned i=0; i<data.size(); ++i) { 169 434923 : displace[i] = data[i]; 170 : } 171 12820 : } 172 : 173 29962 : void PathProjectionCalculator::getReferenceConfiguration( const unsigned& iframe, std::vector<double>& refpos ) const { 174 29962 : if( refpos.size()!=data.size() ) { 175 2 : refpos.resize( data.size() ); 176 : } 177 29962 : if( refargs[0]->getRank()==2 ) { 178 1050240 : for(unsigned i=0; i<refpos.size(); ++i) { 179 1023984 : refpos[i] = refargs[0]->get( iframe*refpos.size() + i ); 180 : } 181 : } else { 182 11178 : for(unsigned i=0; i<refpos.size(); ++i) { 183 7472 : refpos[i] = refargs[i]->get(iframe); 184 : } 185 : } 186 29962 : } 187 : 188 4322 : void PathProjectionCalculator::setReferenceConfiguration( const unsigned& iframe, std::vector<double>& refpos ) { 189 : plumed_dbg_assert( refpos.size()==data.size() ); 190 4322 : if( refargs[0]->getRank()==2 ) { 191 165360 : for(unsigned i=0; i<refpos.size(); ++i) { 192 161226 : refargs[0]->set( iframe*refpos.size() + i, refpos[i] ); 193 : } 194 : } else { 195 572 : for(unsigned i=0; i<refpos.size(); ++i) { 196 384 : refargs[i]->set( iframe, refpos[i] ); 197 : } 198 : } 199 4322 : } 200 : 201 27 : void PathProjectionCalculator::updateDepedentRMSDObjects() { 202 50 : for(unsigned i=0; i<rmsd_objects.size(); ++i) { 203 23 : rmsd_objects[i]->setReferenceConfigurations(); 204 : } 205 27 : } 206 : 207 : } 208 : } 209 :