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 48 : void PathProjectionCalculator::registerKeywords(Keywords& keys) { 33 96 : keys.addInputKeyword("compulsory","ARG","matrix","the labels of the matrix that contains the vectors of displacements from each frame in the path"); 34 48 : keys.add("compulsory","METRIC","the method to use for computing the displacement vectors between the reference frames"); 35 48 : 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 96 : keys.addInputKeyword("compulsory","REFERENCE","vector/matrix","labels for actions that contain reference coordinates for each point on the path (one matrix, or vectors)"); 37 48 : } 38 : 39 19 : PathProjectionCalculator::PathProjectionCalculator( Action* act ): 40 19 : mypath_obj(NULL) { 41 19 : ActionWithArguments* aarg=dynamic_cast<ActionWithArguments*>( act ); 42 19 : if( aarg ) { 43 13 : mypath_obj = aarg->getPntrToArgument(0); 44 : // Check that we have only one argument as input 45 13 : 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 19 : if( mypath_obj ) 52 13 : 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 19 : std::vector<std::string> reference_data; 57 19 : act->parseVector("REFERENCE", reference_data); 58 19 : std::vector<colvar::RMSDVector*> allrmsd = act->plumed.getActionSet().select<colvar::RMSDVector*>(); 59 19 : ActionWithArguments::interpretArgumentList( reference_data, act->plumed.getActionSet(), act, refargs ); 60 44 : for(unsigned i=0; i<refargs.size(); ++i ) { 61 25 : Action* thisact = refargs[i]->getPntrToAction(); 62 29 : for(unsigned j=0; j<allrmsd.size(); ++j) { 63 17 : if( allrmsd[j]->checkForDependency(thisact) ) { 64 13 : rmsd_objects.push_back( allrmsd[j] ); 65 : break; 66 : } 67 : } 68 25 : if( !refargs[i]->isConstant() ) { 69 0 : act->error("input" + refargs[i]->getName() + " is not constant"); 70 : } 71 25 : 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 25 : 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 19 : int s=sizeof(double); 80 19 : metric.cmd("setRealPrecision",&s); 81 19 : metric.cmd("setMDEngine","plumed"); 82 19 : int nat=0; 83 19 : metric.cmd("setNatoms",&nat); 84 19 : metric.cmd("setNoVirial"); 85 19 : unsigned nargs=refargs.size(); 86 19 : if( refargs[0]->getRank()==2 ) { 87 14 : nargs = refargs[0]->getShape()[1]; 88 : } 89 : std::string str_nargs; 90 19 : Tools::convert( nargs, str_nargs ); 91 19 : std::string period_str=" PERIODIC=NO"; 92 19 : 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 19 : metric.readInputLine("arg1: PUT UNIT=number SHAPE=" + str_nargs + period_str, true); 98 19 : metric.readInputLine("arg2: PUT UNIT=number SHAPE=" + str_nargs + period_str, true); 99 19 : double tstep=1.0; 100 19 : metric.cmd("setTimestep",&tstep); 101 : std::string inp; 102 38 : act->parse("METRIC",inp); 103 : inp += " ARG=arg2,arg1"; 104 : const char* cinp=inp.c_str(); 105 19 : std::vector<std::string> input=Tools::getWords(inp); 106 19 : if( input.size()==1 && !actionRegister().check(input[0]) ) { 107 0 : metric.cmd("setPlumedDat",cinp); 108 0 : metric.cmd("init"); 109 : } else { 110 19 : metric.cmd("init"); 111 19 : metric.cmd("readInputLine",cinp); 112 : } 113 : // Now setup stuff to retrieve the final displacement 114 19 : unsigned aind = metric.getActionSet().size()-1; 115 : while( true ) { 116 38 : const ActionShortcut* as=dynamic_cast<const ActionShortcut*>( metric.getActionSet()[aind].get() ); 117 38 : if( !as ) { 118 : break ; 119 : } 120 : // aind is unsigned: check before decrementing, otherwise it wraps around 121 19 : plumed_assert( aind>0 ); 122 19 : --aind; 123 19 : } 124 19 : ActionWithValue* fav = dynamic_cast<ActionWithValue*>( metric.getActionSet()[aind].get() ); 125 19 : if( !fav ) { 126 0 : act->error("final value should calculate relevant value that you want as reference"); 127 : } 128 19 : std::string name = (fav->copyOutput(0))->getName(); 129 19 : if( fav->getNumberOfComponents()>1 ) { 130 : std::string comp; 131 14 : act->parse("METRIC_COMPONENT",comp); 132 28 : name = fav->getLabel() + "." + comp; 133 : } 134 : long rank; 135 38 : metric.cmd("getDataRank " + name, &rank ); 136 19 : if( rank==0 ) { 137 0 : rank=1; 138 : } 139 19 : std::vector<long> ishape( rank ); 140 38 : metric.cmd("getDataShape " + name, &ishape[0] ); 141 : unsigned nvals=1; 142 38 : for(unsigned i=0; i<ishape.size(); ++i) { 143 19 : nvals *= ishape[i]; 144 : } 145 19 : data.resize( nvals ); 146 38 : metric.cmd("setMemoryForData " + name, &data[0] ); 147 38 : } 148 : 149 2258 : unsigned PathProjectionCalculator::getNumberOfFrames() const { 150 2258 : return refargs[0]->getShape()[0]; 151 : } 152 : 153 26471 : void PathProjectionCalculator::computeVectorBetweenFrames( const unsigned& ifrom, const unsigned& ito ) { 154 26471 : int step = 1; 155 26471 : metric.cmd("setStep",&step); 156 26471 : std::vector<double> valdata1( data.size() ), valdata2( data.size() ); 157 26471 : getReferenceConfiguration( ito, valdata2 ); 158 26471 : getReferenceConfiguration( ifrom, valdata1 ); 159 26471 : metric.cmd("setValue arg1", &valdata1[0] ); 160 26471 : metric.cmd("setValue arg2", &valdata2[0] ); 161 26471 : metric.cmd("calc"); 162 26471 : } 163 : 164 26471 : void PathProjectionCalculator::getDisplaceVector( const unsigned& ifrom, const unsigned& ito, std::vector<double>& displace ) { 165 26471 : if( displace.size()!=data.size() ) { 166 4401 : displace.resize( data.size() ); 167 : } 168 26471 : computeVectorBetweenFrames( ifrom, ito ); 169 993783 : for(unsigned i=0; i<data.size(); ++i) { 170 967312 : displace[i] = data[i]; 171 : } 172 26471 : } 173 : 174 61342 : void PathProjectionCalculator::getReferenceConfiguration( const unsigned& iframe, std::vector<double>& refpos ) const { 175 61342 : if( refpos.size()!=data.size() ) { 176 4 : refpos.resize( data.size() ); 177 : } 178 61342 : if( refargs[0]->getRank()==2 ) { 179 2305440 : for(unsigned i=0; i<refpos.size(); ++i) { 180 2247804 : refpos[i] = refargs[0]->get( iframe*refpos.size() + i ); 181 : } 182 : } else { 183 11178 : for(unsigned i=0; i<refpos.size(); ++i) { 184 7472 : refpos[i] = refargs[i]->get(iframe); 185 : } 186 : } 187 61342 : } 188 : 189 8400 : void PathProjectionCalculator::setReferenceConfiguration( const unsigned& iframe, std::vector<double>& refpos ) { 190 : plumed_dbg_assert( refpos.size()==data.size() ); 191 8400 : if( refargs[0]->getRank()==2 ) { 192 328480 : for(unsigned i=0; i<refpos.size(); ++i) { 193 320268 : refargs[0]->set( iframe*refpos.size() + i, refpos[i] ); 194 : } 195 : } else { 196 572 : for(unsigned i=0; i<refpos.size(); ++i) { 197 384 : refargs[i]->set( iframe, refpos[i] ); 198 : } 199 : } 200 8400 : } 201 : 202 108 : void PathProjectionCalculator::updateDepedentRMSDObjects() { 203 212 : for(unsigned i=0; i<rmsd_objects.size(); ++i) { 204 104 : rmsd_objects[i]->setReferenceConfigurations(); 205 : } 206 108 : } 207 : 208 : } 209 : } 210 :