All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Plumed.h
Go to the documentation of this file.
1 /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2  Copyright (c) 2013 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-code.org for more information.
6 
7  This file is part of plumed, version 2.0.
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 #ifndef __PLUMED_wrapper_Plumed_h
23 #define __PLUMED_wrapper_Plumed_h
24 
25 /**
26 \page ReferencePlumedH Reference for interfacing MD codes with PLUMED
27 
28  Plumed.h and Plumed.c contain the external plumed interface, which is used to
29  integrate it with MD engines. This interface is very general, and is expected
30  not to change across plumed versions. Plumed.c also implements a dummy version
31  of the interface, so as to allow a code to be fully linked even if the plumed
32  library is not available yet. These files could be directly included in the official
33  host MD distribution. In this manner, it will be sufficient to link the plumed
34  library at link time (on all systems) or directly at runtime (on system where
35  dynamic loading is enabled) to include plumed features.
36 
37  Why is Plumed.c written in C and not C++? The reason is that the resulting Plumed.o
38  needs to be linked with the host MD code immediately (whereas the rest of plumed
39  could be linked a posteriori). Imagine the MD code is written in FORTRAN: when we
40  link the Plumed.o file we would like not to need any C++ library linked. In this
41  manner, we do not need to know which C++ compiler will be used to compile plumed.
42  The C++ library is only linked to the "rest" of plumed, which actually use it.
43  Anyway, Plumed.c is written in such a manner to allow its compilation also in C++
44  (C++ is a bit stricter than C; compatibility is checked when PlumedStatic.cpp,
45  which basically includes Plumed.c, is compiled with the C++ compiler). This will
46  allow e.g. MD codes written in C++ to just incorporate Plumed.c (maybe renamed into
47  Plumed.cpp), without the need of configuring a plain C compiler.
48 
49  Plumed interface can be used from C, C++ and FORTRAN. Everything concerning plumed
50  is hidden inside a single object type, which is described in C by a structure
51  (struct \ref plumed), in C++ by a class (PLMD::Plumed) and in FORTRAN by a
52  fixed-length string (CHARACTER(LEN=32)). Obviously C++ can use both struct
53  and class interfaces, but the first should be preferred. The reference interface
54  is the C one, whereas FORTRAN and C++ interfaces are implemented as wrappers
55  around it.
56 
57  In the C++ interface, all the routines are implemented as methods of PLMD::Plumed.
58  In the C and FORTRAN interfaces, all the routines are named plumed_*, to
59  avoid potential name clashes. Notice that the entire plumed library
60  is implemented in C++, and it is hidden inside the PLMD namespace.
61 
62  Handlers to the plumed object can be converted among different representations,
63  to allow inter-operability among languages. In C, there are tools to convert
64  to/from FORTRAN, whereas in C++ there are tools to convert to/from FORTRAN and C.
65 
66  These handlers only contain a pointer to the real structure, so that
67  when a plumed object is brought from one language to another,
68  it brings a reference to the same environment.
69 
70  Moreover, to simplify life in all cases where a single Plumed object is
71  required for the entire simulation (which covers most of the practical
72  applications with conventional MD codes) it is possible to take advantage
73  of a global interface, which is implicitly referring to a unique global instance.
74  The global object should still be initialized and finalized properly.
75 
76  The basic method to send a message to plumed is
77 \verbatim
78  (C) plumed_cmd
79  (C++) PLMD::Plumed::cmd
80  (FORTRAN) PLUMED_F_CMD
81 \endverbatim
82 
83  To initialize a plumed object, use:
84 \verbatim
85  (C) plumed_create
86  (C++) (constructor of PLMD::Plumed)
87  (FORTRAN) PLUMED_F_CREATE
88 \endverbatim
89 
90  To finalize it, use
91 \verbatim
92  (C) plumed_finalize
93  (C++) (destructor of PLMD::Plumed)
94  (FORTRAN) PLUMED_F_FINALIZE
95 \endverbatim
96 
97  To access to the global-object, use
98 \verbatim
99  (C) plumed_gcreate, plumed_gfinalize, plumed_gcmd
100  (C++) PLMD::Plumed::gcreate, PLMD::Plumed::gfinalize, PLMD::Plumed::gcmd
101  (FORTRAN) PLUMED_F_GCREATE, PLUMED_F_GFINALIZE, PLUMED_F_GCMD
102 \endverbatim
103 
104  To check if the global object has been initialized, use
105 \verbatim
106  (C) plumed_ginitialized
107  (C++) PLMD::Plumed::ginitialized
108  (FORTRAN) PLUMED_F_GINITIALIZED
109 \endverbatim
110 
111  To check if plumed library is available (this is useful for runtime linking), use
112 \verbatim
113  (C) plumed_installed
114  (C++) PLMD::Plumed::installed
115  (FORTRAN) PLUMED_F_INSTALLED
116 \endverbatim
117 
118  To convert handlers use
119 \verbatim
120  (C) plumed_c2f (C to FORTRAN)
121  (C) plumed_f2c (FORTRAN to C)
122  (C++) Plumed(plumed) constructor (C to C++)
123  (C++) operator plumed() cast (C++ to C)
124  (C++) Plumed(char*) constructor (FORTRAN to C++)
125  (C++) toFortran(char*) (C++ to FORTRAN)
126 \endverbatim
127 
128 \verbatim
129  FORTRAN interface
130  SUBROUTINE PLUMED_F_INSTALLED(i)
131  INTEGER, INTENT(OUT) :: i
132  SUBROUTINE PLUMED_F_GINITIALIZED(i)
133  INTEGER, INTENT(OUT) :: i
134  SUBROUTINE PLUMED_F_GCREATE()
135  SUBROUTINE PLUMED_F_GCMD(key,val)
136  CHARACTER(LEN=*), INTENT(IN) :: key
137  UNSPECIFIED_TYPE, INTENT(INOUT) :: val(*)
138  SUBROUTINE PLUMED_F_GFINALIZE()
139  SUBROUTINE PLUMED_F_GLOBAL(p)
140  CHARACTER(LEN=32), INTENT(OUT) :: p
141  SUBROUTINE PLUMED_F_CREATE(p)
142  CHARACTER(LEN=32), INTENT(OUT) :: p
143  SUBROUTINE PLUMED_F_CMD(p,key,val)
144  CHARACTER(LEN=32), INTENT(IN) :: p
145  CHARACTER(LEN=*), INTENT(IN) :: key
146  UNSPECIFIED_TYPE, INTENT(INOUT) :: val(*)
147  SUBROUTINE PLUMED_F_FINALIZE(p)
148  CHARACTER(LEN=32), INTENT(IN) :: p
149 \endverbatim
150 
151  The main routine is "cmd", which accepts two arguments:
152  key is a string containing the name of the command
153  val is the argument. it is declared const so as to use allow passing const objects, but in practice plumed
154  is going to modify val in several cases (using a const_cast).
155  In some cases val can be omitted: just pass a NULL pointer (in C++, val is optional and can be omitted).
156  The set of possible keys is the real API of the plumed library, and will be expanded with time.
157  New commands will be added, but backward compatibility will be retained as long as possible.
158 
159  To pass plumed a callback function use the following syntax (not available in FORTRAN yet)
160 \verbatim
161  plumed_function_holder ff;
162  ff.p=your_function;
163  plumed_cmd(plumed,"xxxx",&ff);
164 \endverbatim
165  (this is passing the your_function() function to the "xxxx" command)
166 */
167 
168 #ifdef __cplusplus
169  extern "C" {
170 #endif
171 
172 /* Generic function pointer */
173 typedef void (*plumed_function_pointer)(void);
174 
175 /**
176  \brief Holder for function pointer.
177 
178  To pass plumed a callback function use the following syntax:
179 \verbatim
180  plumed_function_holder ff;
181  ff.p=your_function;
182  plumed_cmd(plumed,"xxxx",&ff);
183 \endverbatim
184  (this is going to pass the your_function() function to the "xxxx" command)
185 */
186 
187 typedef struct {
190 
191 /**
192  \brief Main plumed object
193 
194  This is an object containing a Plumed instance, which should be used in
195  the MD engine. It should first be initialized with plumed_create(),
196  then it communicates with the MD engine using plumed_cmd(). Finally,
197  before the termination, it should be deallocated with plumed_finalize().
198  Its interface is very simple and general, and is expected
199  not to change across plumed versions. See \ref ReferencePlumedH.
200 */
201 typedef struct {
202 /**
203  \private
204  \brief Void pointer holding the real PlumedMain structure
205 */
206  void*p;
207 } plumed;
208 
209 /** \relates plumed
210  \brief Constructor
211 
212  \return The constructed plumed object
213 */
214 plumed plumed_create(void);
215 
216 /** \relates plumed
217  \brief Tells p to execute a command
218 
219  \param p The plumed object on which command is acting
220  \param key The name of the command to be executed
221  \param val The argument. It is declared as const to allow calls like plumed_cmd(p,"A","B"),
222  but for some choice of key it can change the content
223 */
224 void plumed_cmd(plumed p,const char*key,const void*val);
225 
226 /** \relates plumed
227  \brief Destructor
228 
229  \param p The plumed object to be deallocated
230 */
231 void plumed_finalize(plumed p);
232 
233 /** \relates plumed
234  \brief Check if plumed is installed (for runtime binding)
235 
236  \return 1 if plumed is installed, to 0 otherwise
237 */
238 int plumed_installed(void);
239 
240 /** \relates plumed
241  \brief Retrieves an handler to the global structure.
242 */
243 plumed plumed_global(void);
244 
245 /** \relates plumed
246  \brief Check if the global interface has been initialized
247 
248  \return 1 if plumed has been initialized, 0 otherwise
249 */
250 int plumed_ginitialized(void);
251 
252 /* global C interface, working on a global object */
253 
254 /** \relates plumed
255  \brief Constructor for the global interface.
256 
257  \note Equivalent to plumed_create(), but initialize a static global plumed object
258 */
259 void plumed_gcreate(void);
260 
261 /** \relates plumed
262  \brief Tells to the global interface to execute a command.
263 
264  \param key The name of the command to be executed
265  \param val The argument. It is declared as const to allow calls like plumed_gcmd("A","B"),
266  but for some choice of key it can change the content
267 
268  \note Equivalent to plumed_cmd(), but skipping the plumed argument
269 */
270 void plumed_gcmd(const char* key,const void* val);
271 
272 /** \relates plumed
273  \brief Destructor for the global interface.
274 
275  \note Equivalent to plumed_finalize(), but skipping the plumed argument
276 */
277 void plumed_gfinalize(void);
278 
279 /* routines to convert char handler from/to plumed objects */
280 
281 /** \related plumed
282  \brief Converts a C handler to a FORTRAN handler
283 
284  \param p The C handler
285  \param c The FORTRAN handler (a char[32])
286 */
287 void plumed_c2f(plumed p,char* c);
288 
289 /** \related plumed
290  \brief Converts a FORTRAN handler to a C handler
291  \param c The FORTRAN handler (a char[32])
292  \return The C handler
293 */
294 plumed plumed_f2c(const char* c);
295 
296 #ifdef __cplusplus
297  }
298 #endif
299 
300 #ifdef __cplusplus
301 
302 /* this is to include the NULL pointer */
303 #include <cstdlib>
304 
305 /* C++ interface is hidden in PLMD namespace (same as plumed library) */
306 namespace PLMD {
307 
308 /**
309  C++ wrapper for \ref plumed.
310 
311  This class provides a C++ interface to PLUMED.
312 */
313 
314 class Plumed{
316 /**
317  keeps track if the object was created from scratch using
318  the defaults destructor (cloned=false) or if it was imported
319  from C or FORTRAN (cloned-true). In the latter case, the
320  plumed_finalize() method is not called when destructing the object,
321  since it is expected to be finalized in the C/FORTRAN code
322 */
323  bool cloned;
324 public:
325 /**
326  Check if plumed is installed (for runtime binding)
327  \return true if plumed is installed, false otherwise
328 */
329  static bool installed();
330 /**
331  Check if global-plumed has been initialized
332  \return true if global plumed object (see global()) is initialized (i.e. if gcreate() has been
333  called), false otherwise.
334 */
335  static bool ginitialized();
336 /**
337  Initialize global-plumed
338 */
339  static void gcreate();
340 /**
341  Send a command to global-plumed
342  \param key The name of the command to be executed
343  \param val The argument. It is declared as const to allow calls like gcmd("A","B"),
344  but for some choice of key it can change the content
345 */
346  static void gcmd(const char* key,const void* val);
347 /**
348  Finalize global-plumed
349 */
350  static void gfinalize();
351 /**
352  Returns the Plumed global object
353  \return The Plumed global object
354 */
355  static Plumed global();
356 /**
357  Constructor
358 */
359  Plumed();
360 /**
361  Clone a Plumed object from a FORTRAN char* handler
362  \param c The FORTRAN handler (a char[32]).
363 
364  \attention The Plumed object created in this manner
365  will not finalize the corresponding plumed structure.
366  It is expected that the FORTRAN code calls plumed_c_finalize for it
367 */
368  Plumed(const char*c);
369 /**
370  Clone a Plumed object from a C plumed structure
371  \param p The C plumed structure.
372 
373  \attention The Plumed object created in this manner
374  will not finalize the corresponding plumed structure.
375  It is expected that the C code calls plumed_finalize for it
376 */
377  Plumed(plumed p);
378 private:
379 /** Copy constructor is disabled (private and unimplemented)
380  The problem here is that after copying it will not be clear who is
381  going to finalize the corresponding plumed structure.
382 */
383  Plumed(const Plumed&);
384 /** Assignment operator is disabled (private and unimplemented)
385  The problem here is that after copying it will not be clear who is
386  going to finalize the corresponding plumed structure.
387 */
388  Plumed&operator=(const Plumed&);
389 public:
390 /**
391  Retrieve the C plumed structure for this object
392 */
393  operator plumed()const;
394 /**
395  Retrieve a FORTRAN handler for this object
396  \param c The FORTRAN handler (a char[32]).
397 */
398  void toFortran(char*c)const;
399 /**
400  Send a command to this plumed object
401  \param key The name of the command to be executed
402  \param val The argument. It is declared as const to allow calls like p.cmd("A","B"),
403  but for some choice of key it can change the content
404 */
405  void cmd(const char*key,const void*val=NULL);
406 /**
407  Destructor
408 
409  Destructor is virtual so as to allow correct inheritance from Plumed object.
410  To avoid linking problems with g++, I specify "inline" also here (in principle
411  it should be enough to specify it down in the definition of the function, but
412  for some reason that I do not understand g++ does not inline it properly in that
413  case and complains when Plumed.h is included but Plumed.o is not linked. Anyway, the
414  way it is done here seems to work properly).
415 */
416  inline virtual ~Plumed();
417 };
418 
419 /* All methods are inlined so as to avoid the compilation of an extra c++ file */
420 
421 inline
423  return plumed_installed();
424 }
425 
426 inline
428  main(plumed_create()),
429  cloned(false)
430 {}
431 
432 inline
433 Plumed::Plumed(const char*c):
434  main(plumed_f2c(c)),
435  cloned(true)
436 {}
437 
438 inline
440  main(p),
441  cloned(true)
442 {}
443 
444 inline
445 Plumed::operator plumed()const{
446  return main;
447 }
448 
449 inline
450 void Plumed::toFortran(char*c)const{
451  plumed_c2f(main,c);
452 }
453 
454 inline
455 void Plumed::cmd(const char*key,const void*val){
456  plumed_cmd(main,key,val);
457 }
458 
459 inline
462 }
463 
464 inline
466  return plumed_ginitialized();
467 }
468 
469 inline
471  plumed_gcreate();
472 }
473 
474 inline
475 void Plumed::gcmd(const char* key,const void* val){
476  plumed_gcmd(key,val);
477 }
478 
479 inline
482 }
483 
484 inline
486  return plumed_global();
487 }
488 
489 }
490 
491 #endif
492 
493 
494 #endif
plumed plumed_global(void)
Retrieves an handler to the global structure.
Definition: Plumed.c:172
void cmd(const char *key, const void *val=NULL)
Send a command to this plumed object.
Definition: Plumed.h:455
void plumed_cmd(plumed p, const char *key, const void *val)
Definition: Plumed.c:155
plumed plumed_f2c(const char *c)
Definition: Plumed.c:217
static void gcreate()
Initialize global-plumed.
Definition: Plumed.h:470
Plumed & operator=(const Plumed &)
Assignment operator is disabled (private and unimplemented) The problem here is that after copying it...
int plumed_ginitialized(void)
Check if the global interface has been initialized.
Definition: Plumed.c:190
int plumed_ginitialized(void)
Definition: Plumed.c:190
plumed plumed_f2c(const char *c)
Converts a FORTRAN handler to a C handler.
Definition: Plumed.c:217
int main(int argc, char **argv)
This main uses only the interface published in Plumed.h.
Definition: main.cpp:38
static void gcmd(const char *key, const void *val)
Send a command to global-plumed.
Definition: Plumed.h:475
int plumed_installed(void)
Check if plumed is installed (for runtime binding)
Definition: Plumed.c:163
int plumed_installed(void)
Definition: Plumed.c:163
virtual ~Plumed()
Destructor.
Definition: Plumed.h:460
void(* plumed_function_pointer)(void)
Definition: Plumed.h:173
Holder for function pointer.
Definition: PlumedMain.h:41
void plumed_finalize(plumed p)
Definition: Plumed.c:159
static bool ginitialized()
Check if global-plumed has been initialized.
Definition: Plumed.h:465
plumed main
Definition: Plumed.h:315
void * p
Void pointer holding the real PlumedMain structure.
Definition: Plumed.h:206
bool cloned
keeps track if the object was created from scratch using the defaults destructor (cloned=false) or if...
Definition: Plumed.h:323
void plumed_gfinalize(void)
Definition: Plumed.c:185
static Plumed global()
Returns the Plumed global object.
Definition: Plumed.h:485
void toFortran(char *c) const
Retrieve a FORTRAN handler for this object.
Definition: Plumed.h:450
void plumed_cmd(plumed p, const char *key, const void *val)
Tells p to execute a command.
Definition: Plumed.c:155
C++ wrapper for plumed.
Definition: Plumed.h:314
static bool installed()
Check if plumed is installed (for runtime binding)
Definition: Plumed.h:422
void plumed_gcmd(const char *key, const void *val)
Definition: Plumed.c:181
Plumed()
Constructor.
Definition: Plumed.h:427
void plumed_gcreate(void)
Definition: Plumed.c:176
void plumed_c2f(plumed p, char *c)
Converts a C handler to a FORTRAN handler.
Definition: Plumed.c:195
void plumed_c2f(plumed p, char *c)
Definition: Plumed.c:195
plumed plumed_create(void)
Definition: Plumed.c:149
static void gfinalize()
Finalize global-plumed.
Definition: Plumed.h:480
Main plumed object.
Definition: Plumed.h:201
plumed plumed_create(void)
Constructor.
Definition: Plumed.c:149
void plumed_gcmd(const char *key, const void *val)
Tells to the global interface to execute a command.
Definition: Plumed.c:181
void plumed_gfinalize(void)
Destructor for the global interface.
Definition: Plumed.c:185
void plumed_finalize(plumed p)
Destructor.
Definition: Plumed.c:159
void plumed_gcreate(void)
Constructor for the global interface.
Definition: Plumed.c:176
plumed plumed_global(void)
Definition: Plumed.c:172