Commit 06e3a67f authored by Georg Martin Reinke's avatar Georg Martin Reinke
Browse files

playing around with embedding python


Former-commit-id: 97be4bb4
parent 9642130f
......@@ -8,13 +8,15 @@ else()
find_package(Eigen3 REQUIRED)
endif()
find_package(PythonLibs)
if(WIN32)
set(ARABICA_XML_BACKEND USE_MSXML)
else()
set(LIBS "-lrt -lvillas-ext")
endif()
include_directories(${EIGEN3_INCLUDE_DIR})
include_directories(${EIGEN3_INCLUDE_DIR} ${PYTHON_INCLUDE_DIRS})
add_subdirectory(CIM-XML-Parser)
# static library target for everything but main (otherwise, CMake compiles all
......@@ -28,7 +30,7 @@ target_link_libraries(DPsim CIMParser)
# actual executable
add_executable(DPSolver ${MAIN_FULLPATH})
target_link_libraries(DPSolver DPsim CIMParser ${LIBS})
target_link_libraries(DPSolver DPsim CIMParser pthread ${PYTHON_LIBRARIES} ${LIBS})
# tests
file(GLOB TESTS Tests/*.cpp)
......
#include <Python.h>
#include <iostream>
#include <string>
#include <pthread.h>
#include "CIMReader.h"
#include "Simulation.h"
#include "ShmemInterface.h"
......@@ -31,6 +33,73 @@ bool parseInt(const char *s, int *i) {
return (end != s && !*end);
}
std::vector<BaseComponent*> components;
//
static pthread_t simThread;
struct SimContext {
Simulation *sim;
Logger *log;
pthread_mutex_t S, M;
std::atomic_int stop;
};
static SimContext globalCtx;
static void* simThreadFunction(void* arg) {
bool running = true;
SimContext* ctx = (SimContext*) arg;
pthread_mutex_lock(&ctx->S);
while (running) {
running = ctx->sim->step(*ctx->log);
ctx->sim->increaseByTimeStep();
if (ctx->stop) {
pthread_mutex_unlock(&ctx->S);
pthread_mutex_lock(&ctx->M);
pthread_mutex_unlock(&ctx->M);
pthread_mutex_lock(&ctx->S);
}
}
pthread_mutex_unlock(&ctx->S);
return nullptr;
}
static PyObject*
pythonStart(PyObject *self, PyObject *args) {
globalCtx.log = new Logger();
globalCtx.sim = new Simulation(components, 2*PI*50, 1e-3, 1000, *globalCtx.log);
globalCtx.stop = 0;
pthread_mutex_init(&globalCtx.M, nullptr);
pthread_mutex_init(&globalCtx.S, nullptr);
pthread_create(&simThread, nullptr, simThreadFunction, &globalCtx);
//pthread_join(simThread, nullptr);
return Py_None;
}
static PyObject*
pythonWait(PyObject *self, PyObject *args) {
pthread_join(simThread, nullptr);
return Py_None;
}
static PyMethodDef pythonMethods[] = {
{"start", pythonStart, METH_VARARGS, "Start the simulation."},
{"wait", pythonWait, METH_VARARGS, "Wait for the simulation to finish."},
{NULL, NULL, 0, NULL}
};
static PyModuleDef dpsimModule = {
PyModuleDef_HEAD_INIT, "dpsim", NULL, -1, pythonMethods,
NULL, NULL, NULL, NULL
};
static PyObject*
PyInit_dpsim(void) {
return PyModule_Create(&dpsimModule);
}
// TODO: that many platform-dependent ifdefs inside main are kind of ugly
int cimMain(int argc, const char* argv[]) {
bool rt = false;
......@@ -134,7 +203,7 @@ int cimMain(int argc, const char* argv[]) {
}
}
reader.parseFiles();
std::vector<BaseComponent*> components = reader.getComponents();
components = reader.getComponents();
#ifdef __linux__
// TODO: this is a simple, pretty much fixed setup. Make this more flexible / configurable
......@@ -194,6 +263,21 @@ int cimMain(int argc, const char* argv[]) {
return 0;
}
int pythonMain(int argc, const char* argv[]) {
PyImport_AppendInittab("dpsim", &PyInit_dpsim);
Py_Initialize();
components.push_back(new VoltSourceRes("V_in", 1, 0, 10, 1, 1));
components.push_back(new LinearResistor("R_load", 1, 0, 100));
while (std::cin.good() && Py_IsInitialized()) {
std::cout << "> ";
std::string line;
std::getline(std::cin, line);
PyRun_SimpleString(line.c_str());
}
}
int main(int argc, const char* argv[]) {
return cimMain(argc, argv);
return pythonMain(argc, argv);
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment