aboutsummaryrefslogtreecommitdiff
path: root/Source/Python
diff options
context:
space:
mode:
Diffstat (limited to 'Source/Python')
-rw-r--r--Source/Python/WarpXWrappers.H16
-rw-r--r--Source/Python/WarpXWrappers.cpp66
-rw-r--r--Source/Python/WarpX_py.H36
-rw-r--r--Source/Python/WarpX_py.cpp28
4 files changed, 49 insertions, 97 deletions
diff --git a/Source/Python/WarpXWrappers.H b/Source/Python/WarpXWrappers.H
index 42066b672..8d2ee4364 100644
--- a/Source/Python/WarpXWrappers.H
+++ b/Source/Python/WarpXWrappers.H
@@ -48,19 +48,9 @@ extern "C" {
typedef void(*WARPX_CALLBACK_PY_FUNC_0)();
- void warpx_set_callback_py_afterinit (WARPX_CALLBACK_PY_FUNC_0);
- void warpx_set_callback_py_beforeEsolve (WARPX_CALLBACK_PY_FUNC_0);
- void warpx_set_callback_py_poissonsolver (WARPX_CALLBACK_PY_FUNC_0);
- void warpx_set_callback_py_afterEsolve (WARPX_CALLBACK_PY_FUNC_0);
- void warpx_set_callback_py_beforedeposition (WARPX_CALLBACK_PY_FUNC_0);
- void warpx_set_callback_py_afterdeposition (WARPX_CALLBACK_PY_FUNC_0);
- void warpx_set_callback_py_particlescraper (WARPX_CALLBACK_PY_FUNC_0);
- void warpx_set_callback_py_particleloader (WARPX_CALLBACK_PY_FUNC_0);
- void warpx_set_callback_py_beforestep (WARPX_CALLBACK_PY_FUNC_0);
- void warpx_set_callback_py_afterstep (WARPX_CALLBACK_PY_FUNC_0);
- void warpx_set_callback_py_afterrestart (WARPX_CALLBACK_PY_FUNC_0);
- void warpx_set_callback_py_particleinjection (WARPX_CALLBACK_PY_FUNC_0);
- void warpx_set_callback_py_appliedfields (WARPX_CALLBACK_PY_FUNC_0);
+ void warpx_set_callback_py (const char* char_callback_name,
+ WARPX_CALLBACK_PY_FUNC_0 callback);
+ void warpx_clear_callback_py (const char* char_callback_name);
void warpx_evolve (int numsteps); // -1 means the inputs parameter will be used.
diff --git a/Source/Python/WarpXWrappers.cpp b/Source/Python/WarpXWrappers.cpp
index d4589ec8a..7350bbbb6 100644
--- a/Source/Python/WarpXWrappers.cpp
+++ b/Source/Python/WarpXWrappers.cpp
@@ -164,14 +164,8 @@ namespace
{
WarpX& warpx = WarpX::GetInstance();
warpx.InitData();
- if (warpx_py_afterinit) {
- WARPX_PROFILE("warpx_py_afterinit");
- warpx_py_afterinit();
- }
- if (warpx_py_particleloader) {
- WARPX_PROFILE("warpx_py_particleloader");
- warpx_py_particleloader();
- }
+ ExecutePythonCallback("afterinit");
+ ExecutePythonCallback("particleloader");
}
void warpx_finalize ()
@@ -179,57 +173,17 @@ namespace
WarpX::ResetInstance();
}
- void warpx_set_callback_py_afterinit (WARPX_CALLBACK_PY_FUNC_0 callback)
- {
- warpx_py_afterinit = callback;
- }
- void warpx_set_callback_py_beforeEsolve (WARPX_CALLBACK_PY_FUNC_0 callback)
+ void warpx_set_callback_py (
+ const char* char_callback_name, WARPX_CALLBACK_PY_FUNC_0 callback)
{
- warpx_py_beforeEsolve = callback;
+ const std::string callback_name(char_callback_name);
+ warpx_callback_py_map[callback_name] = callback;
}
- void warpx_set_callback_py_poissonsolver (WARPX_CALLBACK_PY_FUNC_0 callback)
- {
- warpx_py_poissonsolver = callback;
- }
- void warpx_set_callback_py_afterEsolve (WARPX_CALLBACK_PY_FUNC_0 callback)
- {
- warpx_py_afterEsolve = callback;
- }
- void warpx_set_callback_py_beforedeposition (WARPX_CALLBACK_PY_FUNC_0 callback)
- {
- warpx_py_beforedeposition = callback;
- }
- void warpx_set_callback_py_afterdeposition (WARPX_CALLBACK_PY_FUNC_0 callback)
- {
- warpx_py_afterdeposition = callback;
- }
- void warpx_set_callback_py_particlescraper (WARPX_CALLBACK_PY_FUNC_0 callback)
- {
- warpx_py_particlescraper = callback;
- }
- void warpx_set_callback_py_particleloader (WARPX_CALLBACK_PY_FUNC_0 callback)
- {
- warpx_py_particleloader = callback;
- }
- void warpx_set_callback_py_beforestep (WARPX_CALLBACK_PY_FUNC_0 callback)
- {
- warpx_py_beforestep = callback;
- }
- void warpx_set_callback_py_afterstep (WARPX_CALLBACK_PY_FUNC_0 callback)
- {
- warpx_py_afterstep = callback;
- }
- void warpx_set_callback_py_afterrestart (WARPX_CALLBACK_PY_FUNC_0 callback)
- {
- warpx_py_afterrestart = callback;
- }
- void warpx_set_callback_py_particleinjection (WARPX_CALLBACK_PY_FUNC_0 callback)
- {
- warpx_py_particleinjection = callback;
- }
- void warpx_set_callback_py_appliedfields (WARPX_CALLBACK_PY_FUNC_0 callback)
+
+ void warpx_clear_callback_py (const char* char_callback_name)
{
- warpx_py_appliedfields = callback;
+ const std::string callback_name(char_callback_name);
+ warpx_callback_py_map.erase(callback_name);
}
void warpx_evolve (int numsteps)
diff --git a/Source/Python/WarpX_py.H b/Source/Python/WarpX_py.H
index 42697fbac..efc248591 100644
--- a/Source/Python/WarpX_py.H
+++ b/Source/Python/WarpX_py.H
@@ -9,24 +9,30 @@
#define WARPX_PY_H_
#include "WarpXWrappers.H"
+#include "Utils/WarpXProfilerWrapper.H"
+#include <map>
+#include <string>
-extern "C" {
+/**
+ * Declare global map to hold python callback functions.
+ *
+ * The keys of the map describe at what point in the simulation the python
+ * functions will be called. Currently supported keys (callback points) are
+ * afterinit, beforeEsolve, poissonsolver, afterEsolve, beforedeposition,
+ * afterdeposition, particlescraper, particleloader, beforestep, afterstep,
+ * afterrestart, particleinjection and appliedfields.
+*/
+extern std::map< std::string, WARPX_CALLBACK_PY_FUNC_0 > warpx_callback_py_map;
- extern WARPX_CALLBACK_PY_FUNC_0 warpx_py_afterinit;
- extern WARPX_CALLBACK_PY_FUNC_0 warpx_py_beforeEsolve;
- extern WARPX_CALLBACK_PY_FUNC_0 warpx_py_poissonsolver;
- extern WARPX_CALLBACK_PY_FUNC_0 warpx_py_afterEsolve;
- extern WARPX_CALLBACK_PY_FUNC_0 warpx_py_beforedeposition;
- extern WARPX_CALLBACK_PY_FUNC_0 warpx_py_afterdeposition;
- extern WARPX_CALLBACK_PY_FUNC_0 warpx_py_particlescraper;
- extern WARPX_CALLBACK_PY_FUNC_0 warpx_py_particleloader;
- extern WARPX_CALLBACK_PY_FUNC_0 warpx_py_beforestep;
- extern WARPX_CALLBACK_PY_FUNC_0 warpx_py_afterstep;
- extern WARPX_CALLBACK_PY_FUNC_0 warpx_py_afterrestart;
- extern WARPX_CALLBACK_PY_FUNC_0 warpx_py_particleinjection;
- extern WARPX_CALLBACK_PY_FUNC_0 warpx_py_appliedfields;
+/**
+ * \brief Function to check if the given name is a key in warpx_callback_py_map
+ */
+bool IsPythonCallBackInstalled ( std::string name );
-}
+/**
+ * \brief Function to look for and execute Python callbacks
+ */
+void ExecutePythonCallback ( std::string name );
#endif
diff --git a/Source/Python/WarpX_py.cpp b/Source/Python/WarpX_py.cpp
index 1d69ea8f3..7e0e491f8 100644
--- a/Source/Python/WarpX_py.cpp
+++ b/Source/Python/WarpX_py.cpp
@@ -7,16 +7,18 @@
*/
#include "WarpX_py.H"
-WARPX_CALLBACK_PY_FUNC_0 warpx_py_afterinit = nullptr;
-WARPX_CALLBACK_PY_FUNC_0 warpx_py_beforeEsolve = nullptr;
-WARPX_CALLBACK_PY_FUNC_0 warpx_py_poissonsolver = nullptr;
-WARPX_CALLBACK_PY_FUNC_0 warpx_py_afterEsolve = nullptr;
-WARPX_CALLBACK_PY_FUNC_0 warpx_py_beforedeposition = nullptr;
-WARPX_CALLBACK_PY_FUNC_0 warpx_py_afterdeposition = nullptr;
-WARPX_CALLBACK_PY_FUNC_0 warpx_py_particlescraper = nullptr;
-WARPX_CALLBACK_PY_FUNC_0 warpx_py_particleloader = nullptr;
-WARPX_CALLBACK_PY_FUNC_0 warpx_py_beforestep = nullptr;
-WARPX_CALLBACK_PY_FUNC_0 warpx_py_afterstep = nullptr;
-WARPX_CALLBACK_PY_FUNC_0 warpx_py_afterrestart = nullptr;
-WARPX_CALLBACK_PY_FUNC_0 warpx_py_particleinjection = nullptr;
-WARPX_CALLBACK_PY_FUNC_0 warpx_py_appliedfields = nullptr;
+std::map< std::string, WARPX_CALLBACK_PY_FUNC_0 > warpx_callback_py_map;
+
+bool IsPythonCallBackInstalled ( std::string name )
+{
+ return (warpx_callback_py_map.count(name) == 1u);
+}
+
+// Execute Python callbacks of the type given by the input string
+void ExecutePythonCallback ( std::string name )
+{
+ if ( IsPythonCallBackInstalled(name) ) {
+ WARPX_PROFILE("warpx_py_"+name);
+ warpx_callback_py_map[name]();
+ }
+}