LCOV - code coverage report
Current view: top level - core - ActionToGetData.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 38 46 82.6 %
Date: 2025-12-04 11:19:34 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 STRIDE=1 TYPE=value
      69             : ```
      70             : 
      71             : !!! warning "TYPE not fully implemented"
      72             : 
      73             :     At the moment you can only use GET to extract the values. The TYPE keyword was added so that we could support passing of the forces
      74             :     on a value or the derivatives of the value.  Currently, we do not support these options. If you are interested in using this feature
      75             :     please let us know as we could implement this functionality here relatively easily.
      76             : 
      77             : */
      78             : //+ENDPLUMEDOC
      79             : 
      80             : namespace PLMD {
      81             : 
      82             : PLUMED_REGISTER_ACTION(ActionToGetData,"GET")
      83             : 
      84         118 : void ActionToGetData::registerKeywords(Keywords& keys) {
      85         118 :   Action::registerKeywords(keys);
      86         118 :   ActionPilot::registerKeywords(keys);
      87         118 :   ActionWithArguments::registerKeywords(keys);
      88         236 :   keys.addInputKeyword("optional","ARG","scalar/vector/matrix/grid","the label of the value that you would like to GET");
      89         118 :   keys.add("compulsory","STRIDE","1","the frequency with which the quantities of interest should be stored");
      90         118 :   keys.add("compulsory","TYPE","value","what do you want to collect for the value can be derivative/force");
      91         236 :   keys.setValueDescription("scalar/vector/matrix/grid","a copy of the data in the value specified by the ARG keyword");
      92         118 : }
      93             : 
      94         116 : ActionToGetData::ActionToGetData(const ActionOptions&ao):
      95             :   Action(ao),
      96             :   ActionPilot(ao),
      97             :   ActionWithArguments(ao),
      98         116 :   mydata(DataPassingObject::create(plumed.getRealPrecision())) {
      99             :   std::string type;
     100         232 :   parse("TYPE",type);
     101         116 :   if( type=="value" ) {
     102         116 :     gtype=dataType::val;
     103           0 :   } else if( type=="derivatives" ) {
     104           0 :     gtype=dataType::deriv;
     105           0 :   } else if( type=="forces" ) {
     106           0 :     gtype=dataType::force;
     107             :   } else {
     108           0 :     plumed_merror("cannot get " + type + " for value TYPE should be value/derivative/force");
     109             :   }
     110             : 
     111         116 :   if( gtype!=dataType::val ) {
     112           0 :     error("not implemented functionality to pass derviatives or forces to python.  Email gareth.tribello@gmail.com if you want this.");
     113             :   }
     114             : 
     115         116 :   if( getNumberOfArguments()!=1 ) {
     116           0 :     error("python interface works best when you ask for one argument at a time");
     117             :   }
     118         116 :   if( getPntrToArgument(0)->getNumberOfValues()==0 ) {
     119           0 :     error("cannot get data as shape of value " + getPntrToArgument(0)->getName() + " has not been set");
     120             :   }
     121         116 :   data.resize( getPntrToArgument(0)->getNumberOfValues() );
     122         116 : }
     123             : 
     124         116 : void ActionToGetData::get_rank( const TypesafePtr & dims ) {
     125         116 :   if( getPntrToArgument(0)->getRank()==0 ) {
     126          98 :     dims.set(long(1));
     127          98 :     return;
     128             :   }
     129          18 :   dims.set(long(getPntrToArgument(0)->getRank()));
     130             : }
     131             : 
     132          52 : void ActionToGetData::get_shape( const TypesafePtr & dims ) {
     133          52 :   if( getPntrToArgument(0)->getRank()==0 ) {
     134          34 :     dims.set(long(1));
     135          34 :     return;
     136             :   }
     137          18 :   auto dims_=dims.get<long*>( { getPntrToArgument(0)->getRank() } );
     138          39 :   for(unsigned j=0; j<getPntrToArgument(0)->getRank(); ++j) {
     139          21 :     dims_[j] = getPntrToArgument(0)->getShape()[j];
     140             :   }
     141             : }
     142             : 
     143         116 : void ActionToGetData::set_memory( const TypesafePtr & val ) {
     144         116 :   mydata->setValuePointer(val,getPntrToArgument(0)->getShape(),false);
     145         116 : }
     146             : 
     147       13539 : void ActionToGetData::calculate() {
     148       13539 :   plumed_assert( gtype==dataType::val );
     149       13539 :   mydata->setData( getPntrToArgument(0) );
     150       13539 : }
     151             : 
     152             : }

Generated by: LCOV version 1.16