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: 2025-03-25 09:33:27 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             : The LOAD action is only available on systems that allow dynamic loading.
      35             : This action allows you load new funcionality into PLUMED at runtime.
      36             : This new functionality can be in a .so file or a .cpp file. If the
      37             : new functionality is in a cpp file then the code is compiled and the
      38             : the resulting object is loaded.
      39             : 
      40             : Using the LOAD action is useful for making quick tests while developing your own
      41             : actions. Of course, once your implementation is ready you should
      42             : add it to the PLUMED source tree and recompile PLUMED.
      43             : 
      44             : One way to use the LOAD action is to directly load a cpp file as illustrated in the input below.
      45             : 
      46             : ```plumed
      47             : #SETTINGS INPUTFILES=regtest/basic/rt15/Distance2.cpp
      48             : # load the new definition
      49             : # this is a cpp file so it will be compiled
      50             : LOAD FILE=regtest/basic/rt15/Distance2.cpp
      51             : # compute standard distance
      52             : d: DISTANCE ATOMS=1,10
      53             : # compute modified distance
      54             : d2: DISTANCE2 ATOMS=1,10
      55             : # print them on a file
      56             : PRINT ARG=d,d2 FILE=compare-them
      57             : ```
      58             : 
      59             : When PLUMED reads the input above it first compiles the code in `Distance2.cpp`. The resulting object file
      60             : is then loaded into PLUMED. If you look at the cpp file that is input in the command above you can see that
      61             : a new action called `DISTANCE2` is defined within it.  We can thus use this new action in the input above.
      62             : 
      63             : Instead of compiling the code at runtime you can construct a shared object from the Distance2.cpp file and LOAD the shared library instead of
      64             : the cpp file.  To construct the shared object you would use the following command:
      65             : 
      66             : ````
      67             : > plumed mklib Distance2.cpp
      68             : ````
      69             : 
      70             : When you run this command a new file called `Distance2.so` (or `Distance2.dylib` on a mac) is created.
      71             : You can then load this shared object in PLUMED by using the following LOAD command
      72             : 
      73             : ````
      74             : LOAD FILE=Distance2.so
      75             : ````
      76             : 
      77             : The new functionality within the Distance2.cpp file can then be used in the remainder of the input in the same way it was
      78             : used in the previous example.
      79             : (Notice that the only reason for not using the usual PLUMED syntax highlightling in the example above is that we have no
      80             : file called Distance2.so.)
      81             : 
      82             : From PLUMED 2.10 onwards the LOAD action can be placed at any point of the input
      83             : file. The loaded functionality will then only affect commands that are placed after the LOAD action.
      84             : These features are illustrated in the input below:
      85             : 
      86             : ```plumed
      87             : #SETTINGS INPUTFILES=regtest/basic/rt15/Distance3.cpp
      88             : # compute standard distance
      89             : d: DISTANCE ATOMS=1,10
      90             : # load the new definition
      91             : LOAD FILE=regtest/basic/rt15/Distance3.cpp
      92             : # compute modified distance
      93             : d2: DISTANCE ATOMS=1,10
      94             : # print them on a file
      95             : PRINT ARG=d,d2 FILE=compare-them
      96             : ```
      97             : 
      98             : Notice that `Distance3.cpp` replaces DISTANCE2 in the command that was used in `Distance2.cpp`; namely:
      99             : 
     100             : ```c++
     101             : PLUMED_REGISTER_ACTION(Distance,"DISTANCE2")
     102             : ```
     103             : 
     104             : with DISTANCE. Consequently, when we load the `Distance3.cpp` file here we redefine the DISTANCE command.
     105             : The functions that compute `d` and `d2` in the above input are thus different.
     106             : 
     107             : A final point to note is that, starting with PLUMED 2.10, the LOAD action can be used in contexts where
     108             : multiple Plumed objects exist. A possible example is multithreading: loading an action
     109             : from a Plumed object used in one thread will not affect other threads.
     110             : Another example is if multiple Plumed objects are created in the C/C++ or Python interface.
     111             : If a LOAD command is used in one of these objects, the loaded action will not affect
     112             : the other objects.
     113             : 
     114             : > [!NOTE]
     115             : > The example inputs on this page appear as not working because you cannot use the dynamic loading on GitHub Actions. On a machine where this functionality is available these inputs should work.
     116             : 
     117             : */
     118             : //+ENDPLUMEDOC
     119             : 
     120             : class Load :
     121             :   public virtual ActionAnyorder {
     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          44 :   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             :   std::string f;
     138          43 :   parse("FILE",f);
     139          42 :   checkRead();
     140          42 :   plumed.load(f);
     141          42 : }
     142             : 
     143             : }
     144             : }
     145             : 

Generated by: LCOV version 1.16