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 24 : void PathProjectionCalculator::registerKeywords(Keywords& keys) { 33 48 : 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 48 : keys.addInputKeyword("compulsory","REFERENCE","vector","labels for actions that contain reference coordinates for each point on the path"); 37 24 : } 38 : 39 10 : PathProjectionCalculator::PathProjectionCalculator( Action* act ): 40 10 : mypath_obj(NULL) 41 : { 42 10 : ActionWithArguments* aarg=dynamic_cast<ActionWithArguments*>( act ); 43 10 : if( aarg ) { 44 6 : mypath_obj = aarg->getPntrToArgument(0); 45 : // Check that we have only one argument as input 46 6 : if( aarg->getNumberOfArguments()!=1 ) act->error("should only have one argument to this function"); 47 : } 48 : // Ensure that values are stored in base calculation and that PLUMED doesn't try to calculate this in the stream 49 10 : if( mypath_obj ) mypath_obj->buildDataStore(); 50 : // Check that the input is a matrix 51 10 : if( mypath_obj ) if( mypath_obj->getRank()!=2 ) act->error("the input to this action should be a matrix"); 52 : // Get the labels for the reference points 53 10 : std::vector<std::string> reference_data; act->parseVector("REFERENCE", reference_data); 54 10 : std::vector<colvar::RMSDVector*> allrmsd = act->plumed.getActionSet().select<colvar::RMSDVector*>(); 55 10 : ActionWithArguments::interpretArgumentList( reference_data, act->plumed.getActionSet(), act, refargs ); 56 26 : for(unsigned i=0; i<refargs.size(); ++i ) { 57 16 : Action* thisact = refargs[i]->getPntrToAction(); 58 16 : for(unsigned j=0; j<allrmsd.size(); ++j) { 59 4 : if( allrmsd[j]->checkForDependency(thisact) ) { rmsd_objects.push_back( allrmsd[j] ); break; } 60 : } 61 16 : if( !refargs[i]->isConstant() ) act->error("input" + refargs[i]->getName() + " is not constant"); 62 16 : if( refargs[i]->getRank()==2 && reference_data.size()!=1 ) act->error("should only be one matrix in input to path projection object"); 63 16 : if( refargs[i]->getRank()>0 && refargs[i]->getShape()[0]!=refargs[0]->getShape()[0] ) act->error("mismatch in number of reference frames in input to reference_data"); 64 : } 65 : // Create a plumed main object to compute distances between reference configurations 66 10 : int s=sizeof(double); 67 10 : metric.cmd("setRealPrecision",&s); 68 10 : metric.cmd("setMDEngine","plumed"); 69 10 : int nat=0; metric.cmd("setNatoms",&nat); metric.cmd("setNoVirial"); 70 10 : unsigned nargs=refargs.size(); if( refargs[0]->getRank()==2 ) nargs = refargs[0]->getShape()[1]; 71 10 : std::string str_nargs; Tools::convert( nargs, str_nargs ); std::string period_str=" PERIODIC=NO"; 72 11 : if( mypath_obj && mypath_obj->isPeriodic() ) { std::string min, max; mypath_obj->getDomain( min, max ); period_str=" PERIODIC=" + min + "," + max; } 73 20 : metric.readInputLine("arg1: PUT UNIT=number SHAPE=" + str_nargs + period_str, true); 74 20 : metric.readInputLine("arg2: PUT UNIT=number SHAPE=" + str_nargs + period_str, true); 75 10 : double tstep=1.0; metric.cmd("setTimestep",&tstep); 76 20 : std::string inp; act->parse("METRIC",inp); inp += " ARG=arg2,arg1"; const char* cinp=inp.c_str(); 77 10 : std::vector<std::string> input=Tools::getWords(inp); 78 10 : if( input.size()==1 && !actionRegister().check(input[0]) ) { 79 0 : metric.cmd("setPlumedDat",cinp); metric.cmd("init"); 80 : } else { 81 10 : metric.cmd("init"); metric.cmd("readInputLine",cinp); 82 : } 83 : // Now setup stuff to retrieve the final displacement 84 10 : unsigned aind = metric.getActionSet().size()-1; 85 : while( true ) { 86 15 : const ActionShortcut* as=dynamic_cast<const ActionShortcut*>( metric.getActionSet()[aind].get() ); 87 15 : if( !as ) break ; aind = aind - 1; plumed_assert( aind>=0 ); 88 5 : } 89 10 : ActionWithValue* fav = dynamic_cast<ActionWithValue*>( metric.getActionSet()[aind].get() ); 90 10 : if( !fav ) act->error("final value should calculate relevant value that you want as reference"); 91 10 : std::string name = (fav->copyOutput(0))->getName(); 92 10 : if( fav->getNumberOfComponents()>1 ) { 93 15 : std::string comp; act->parse("METRIC_COMPONENT",comp); name = fav->getLabel() + "." + comp; 94 : } 95 20 : long rank; metric.cmd("getDataRank " + name, &rank ); 96 10 : if( rank==0 ) rank=1; 97 20 : std::vector<long> ishape( rank ); metric.cmd("getDataShape " + name, &ishape[0] ); 98 20 : unsigned nvals=1; for(unsigned i=0; i<ishape.size(); ++i) nvals *= ishape[i]; 99 30 : data.resize( nvals ); metric.cmd("setMemoryForData " + name, &data[0] ); 100 20 : } 101 : 102 1114 : unsigned PathProjectionCalculator::getNumberOfFrames() const { 103 1114 : return refargs[0]->getShape()[0]; 104 : } 105 : 106 11728 : void PathProjectionCalculator::computeVectorBetweenFrames( const unsigned& ifrom, const unsigned& ito ) { 107 11728 : int step = 1; metric.cmd("setStep",&step); 108 11728 : std::vector<double> valdata1( data.size() ), valdata2( data.size() ); 109 11728 : getReferenceConfiguration( ito, valdata2 ); getReferenceConfiguration( ifrom, valdata1 ); 110 11728 : metric.cmd("setValue arg1", &valdata1[0] ); 111 11728 : metric.cmd("setValue arg2", &valdata2[0] ); 112 11728 : metric.cmd("calc"); 113 11728 : } 114 : 115 11728 : void PathProjectionCalculator::getDisplaceVector( const unsigned& ifrom, const unsigned& ito, std::vector<double>& displace ) { 116 11728 : if( displace.size()!=data.size() ) displace.resize( data.size() ); 117 404063 : computeVectorBetweenFrames( ifrom, ito ); for(unsigned i=0; i<data.size(); ++i) displace[i] = data[i]; 118 11728 : } 119 : 120 27778 : void PathProjectionCalculator::getReferenceConfiguration( const unsigned& iframe, std::vector<double>& refpos ) const { 121 27778 : if( refpos.size()!=data.size() ) refpos.resize( data.size() ); 122 27778 : if( refargs[0]->getRank()==2 ) { 123 962880 : for(unsigned i=0; i<refpos.size(); ++i) refpos[i] = refargs[0]->get( iframe*refpos.size() + i ); 124 : } else { 125 11178 : for(unsigned i=0; i<refpos.size(); ++i) refpos[i] = refargs[i]->get(iframe); 126 : } 127 27778 : } 128 : 129 4322 : void PathProjectionCalculator::setReferenceConfiguration( const unsigned& iframe, std::vector<double>& refpos ) { 130 : plumed_dbg_assert( refpos.size()==data.size() ); 131 4322 : if( refargs[0]->getRank()==2 ) { 132 165360 : for(unsigned i=0; i<refpos.size(); ++i) refargs[0]->set( iframe*refpos.size() + i, refpos[i] ); 133 : } else { 134 572 : for(unsigned i=0; i<refpos.size(); ++i) refargs[i]->set( iframe, refpos[i] ); 135 : } 136 4322 : } 137 : 138 27 : void PathProjectionCalculator::updateDepedentRMSDObjects() { 139 50 : for(unsigned i=0; i<rmsd_objects.size(); ++i) rmsd_objects[i]->setReferenceConfigurations(); 140 27 : } 141 : 142 : } 143 : } 144 :