LCOV - code coverage report
Current view: top level - core - ActionToGetData.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 39 47 83.0 %
Date: 2025-03-25 09:33:27 Functions: 6 7 85.7 %

          Line data    Source code
       1             : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       2             :    Copyright (c) 2017-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 "ActionToGetData.h"
      23             : #include "ActionRegister.h"
      24             : #include "PlumedMain.h"
      25             : 
      26             : //+PLUMEDOC ANALYSIS GET
      27             : /*
      28             : Get data from PLUMED for another code
      29             : 
      30             : The GET command takes in the label of a Value and transfers the contents of the Value to
      31             : to a void pointer that is accessible in the code that called PLUMED. As the calling code does not know the shape of the Value in advance,
      32             : we provide the functionality to get the data rank and shape. The following Python snippet illustrates how this works in practice:
      33             : 
      34             : ```python
      35             : import plumed
      36             : 
      37             : # Create a PLUMED object
      38             : p = plumed.Plumed()
      39             : # Setup PLUMED
      40             : num_atoms = 10
      41             : p.cmd("setNatoms",num_atoms)
      42             : p.cmd("setLogFile","test.log")
      43             : p.cmd("init")
      44             : # Tell PLUMED to calculate the distance between two atoms
      45             : p.cmd("readInputLine", "d1: DISTANCE ATOMS=1,2")
      46             : # Get the rank of the PLMD::Value that holds the distance
      47             : # This command sets up the GET object
      48             : rank = np.zeros( 1, dtype=np.int_ )
      49             : p.cmd("getDataRank d1", rank )
      50             : # Now get the shape of the PLMD::Value d1 that we are asking for in the GET object
      51             : shape = np.zeros( rank, dtype=np.int_ )
      52             : p.cmd("getDataShape d1", shape )
      53             : # And now set the void pointer that the data in PLMD::Value d1 should be
      54             : # transferred to so it can be accessed in our python script when asking PLMD to do a calculation
      55             : d1 = np.zeros( shape )
      56             : p.cmd("setMemoryForData d1", data )
      57             : 
      58             : # if we now transfer some atomic positions to plumed and call calc the variable d1 is set equal to the distance between atom 1 and atom 2.
      59             : ```
      60             : 
      61             : Notice that you can have as many GET actions as you need. The data is transferred from the PLMD::Value to the void pointer when the `calculate` method of GET is called.
      62             : Transferring variables is thus seamlessly integrated into the PLUMED calculation cycle.
      63             : 
      64             : You would only use the GET command if you were calling PLUMED from python or an MD code. The equivalent commands that you would use for this action in a conventional PLUMED input file as follows.
      65             : 
      66             : ```plumed
      67             : d: DISTANCE ATOMS=1,2
      68             : GET ARG=d
      69             : ```
      70             : 
      71             : */
      72             : //+ENDPLUMEDOC
      73             : 
      74             : namespace PLMD {
      75             : 
      76             : PLUMED_REGISTER_ACTION(ActionToGetData,"GET")
      77             : 
      78         117 : void ActionToGetData::registerKeywords(Keywords& keys) {
      79         117 :   Action::registerKeywords(keys);
      80         117 :   ActionPilot::registerKeywords(keys);
      81         117 :   ActionWithArguments::registerKeywords(keys);
      82         234 :   keys.addInputKeyword("optional","ARG","scalar/vector/matrix/grid","the label of the value that you would like to GET");
      83         117 :   keys.add("compulsory","STRIDE","1","the frequency with which the quantities of interest should be stored");
      84         117 :   keys.add("compulsory","TYPE","value","what do you want to collect for the value can be derivative/force");
      85         234 :   keys.setValueDescription("scalar/vector/matrix/grid","a copy of the data in the value specified by the ARG keyword");
      86         117 : }
      87             : 
      88         115 : ActionToGetData::ActionToGetData(const ActionOptions&ao):
      89             :   Action(ao),
      90             :   ActionPilot(ao),
      91             :   ActionWithArguments(ao),
      92         115 :   mydata(DataPassingObject::create(plumed.getRealPrecision())) {
      93             :   std::string type;
      94         230 :   parse("TYPE",type);
      95         115 :   if( type=="value" ) {
      96         115 :     gtype=val;
      97           0 :   } else if( type=="derivatives" ) {
      98           0 :     gtype=deriv;
      99           0 :   } else if( type=="forces" ) {
     100           0 :     gtype=force;
     101             :   } else {
     102           0 :     plumed_merror("cannot get " + type + " for value TYPE should be value/derivative/force");
     103             :   }
     104             : 
     105         115 :   if( gtype!=val ) {
     106           0 :     error("not implemented functionality to pass derviatives or forces to python.  Email gareth.tribello@gmail.com if you want this.");
     107             :   }
     108             : 
     109         115 :   if( getNumberOfArguments()!=1 ) {
     110           0 :     error("python interface works best when you ask for one argument at a time");
     111             :   }
     112         115 :   if( getPntrToArgument(0)->getNumberOfValues()==0 ) {
     113           0 :     error("cannot get data as shape of value " + getPntrToArgument(0)->getName() + " has not been set");
     114             :   }
     115         115 :   getPntrToArgument(0)->buildDataStore();
     116         115 :   data.resize( getPntrToArgument(0)->getNumberOfValues() );
     117         115 : }
     118             : 
     119         115 : void ActionToGetData::get_rank( const TypesafePtr & dims ) {
     120         115 :   if( getPntrToArgument(0)->getRank()==0 ) {
     121          98 :     dims.set(long(1));
     122          98 :     return;
     123             :   }
     124          17 :   dims.set(long(getPntrToArgument(0)->getRank()));
     125             : }
     126             : 
     127          51 : void ActionToGetData::get_shape( const TypesafePtr & dims ) {
     128          51 :   if( getPntrToArgument(0)->getRank()==0 ) {
     129          34 :     dims.set(long(1));
     130          34 :     return;
     131             :   }
     132          17 :   auto dims_=dims.get<long*>( { getPntrToArgument(0)->getRank() } );
     133          37 :   for(unsigned j=0; j<getPntrToArgument(0)->getRank(); ++j) {
     134          20 :     dims_[j] = getPntrToArgument(0)->getShape()[j];
     135             :   }
     136             : }
     137             : 
     138         115 : void ActionToGetData::set_memory( const TypesafePtr & val ) {
     139         115 :   mydata->setValuePointer(val,getPntrToArgument(0)->getShape(),false);
     140         115 : }
     141             : 
     142       12447 : void ActionToGetData::calculate() {
     143       12447 :   plumed_assert( gtype==val );
     144       12447 :   mydata->setData( getPntrToArgument(0) );
     145       12447 : }
     146             : 
     147             : }

Generated by: LCOV version 1.16