LCOV - code coverage report
Current view: top level - setup - Load.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 10 10 100.0 %
Date: 2024-10-18 13:59:31 Functions: 2 3 66.7 %

          Line data    Source code
       1             : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       2             :    Copyright (c) 2012-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 "core/ActionAnyorder.h"
      23             : #include "core/ActionRegister.h"
      24             : #include "core/PlumedMain.h"
      25             : #include "tools/Exception.h"
      26             : 
      27             : namespace PLMD {
      28             : namespace setup {
      29             : 
      30             : //+PLUMEDOC GENERIC LOAD
      31             : /*
      32             : Loads a library, possibly defining new actions.
      33             : 
      34             : It is available only
      35             : on systems allowing for dynamic loading. It can also be fed with a cpp file,
      36             : in which case the file is compiled first.
      37             : 
      38             : \par Examples
      39             : 
      40             : If you have a shared object named extensions.so and want to
      41             : use the functions implemented within it within PLUMED you can
      42             : load it with the following syntax
      43             : 
      44             : \plumedfile
      45             : LOAD FILE=extensions.so
      46             : \endplumedfile
      47             : 
      48             : As a more practical example, imagine that you want to make a
      49             : small change to one collective variable that is already implemented
      50             : in PLUMED, say \ref DISTANCE . Copy the file `src/colvar/Distance.cpp`
      51             : into your work directory, rename it as `Distance2.cpp`
      52             : and  edit it as you wish. It might be better
      53             : to also replace any occurrence of the string DISTANCE within the file
      54             : with DISTANCE2, so that both old and new implementation will be available
      55             : with different names. Then you can compile it into a shared object using
      56             : \verbatim
      57             : > plumed mklib Distance2.cpp
      58             : \endverbatim
      59             : This will generate a file `Distance2.so` (or `Distance2.dylib` on a mac)
      60             : that can be loaded.
      61             : Now you can use your new implementation with the following input
      62             : \plumedfile
      63             : # load the new library
      64             : LOAD FILE=Distance2.so
      65             : # compute standard distance
      66             : d: DISTANCE ATOMS=1,10
      67             : # compute modified distance
      68             : d2: DISTANCE2 ATOMS=1,10
      69             : # print them on a file
      70             : PRINT ARG=d,d2 FILE=compare-them
      71             : \endplumedfile
      72             : 
      73             : You can even skip the initial step and directly feed PLUMED
      74             : with the `Distance2.cpp` file: it will be compiled on the fly.
      75             : \plumedfile
      76             : # load the new definition
      77             : # this is a cpp file so it will be compiled
      78             : LOAD FILE=Distance2.cpp
      79             : # compute standard distance
      80             : d: DISTANCE ATOMS=1,10
      81             : # compute modified distance
      82             : d2: DISTANCE2 ATOMS=1,10
      83             : # print them on a file
      84             : PRINT ARG=d,d2 FILE=compare-them
      85             : \endplumedfile
      86             : 
      87             : This will allow to make quick tests while developing your own
      88             : variables. Of course, after your implementation is ready you might
      89             : want to add it to the PLUMED source tree and recompile
      90             : the whole PLUMED.
      91             : 
      92             : Starting with PLUMED 2.10, the LOAD action can be placed in any point of the input
      93             : file, and will only affect commands that are placed after the LOAD action.
      94             : In other words, you can create a file named `Distance.cpp` and that reimplement
      95             : the \ref DISTANCE action and use it like this:
      96             : 
      97             : \plumedfile
      98             : # compute standard distance
      99             : d: DISTANCE ATOMS=1,10
     100             : # load the new definition
     101             : LOAD FILE=Distance.so
     102             : # compute modified distance
     103             : d2: DISTANCE ATOMS=1,10
     104             : # print them on a file
     105             : PRINT ARG=d,d2 FILE=compare-them
     106             : \endplumedfile
     107             : 
     108             : In addition, starting with PLUMED 2.10 the LOAD action can be used in contexts where
     109             : multiple Plumed objects exist. A possible example is multithreading: loading an action
     110             : from a Plumed object used in one thread will not affect other threads.
     111             : Another example is if multiple Plumed objects are created in the C/C++ or Python interface.
     112             : If a LOAD command is used in one of these objects, the loaded action will not affect
     113             : the other objects.
     114             : 
     115             : 
     116             : */
     117             : //+ENDPLUMEDOC
     118             : 
     119             : class Load :
     120             :   public virtual ActionAnyorder
     121             : {
     122             : public:
     123             :   static void registerKeywords( Keywords& keys );
     124             :   explicit Load(const ActionOptions&ao);
     125             : };
     126             : 
     127             : PLUMED_REGISTER_ACTION(Load,"LOAD")
     128             : 
     129          44 : void Load::registerKeywords( Keywords& keys ) {
     130          44 :   ActionAnyorder::registerKeywords(keys);
     131          88 :   keys.add("compulsory","FILE","file to be loaded");
     132          44 : }
     133             : 
     134          42 : Load::Load(const ActionOptions&ao):
     135             :   Action(ao),
     136          42 :   ActionAnyorder(ao)
     137             : {
     138             :   std::string f;
     139          43 :   parse("FILE",f);
     140          42 :   checkRead();
     141          42 :   plumed.load(f);
     142          42 : }
     143             : 
     144             : }
     145             : }
     146             : 

Generated by: LCOV version 1.16