diff --git a/AmberMDrun/CMakeLists.txt b/AmberMDrun/CMakeLists.txt index e19d6c2..7c2769b 100644 --- a/AmberMDrun/CMakeLists.txt +++ b/AmberMDrun/CMakeLists.txt @@ -31,6 +31,8 @@ add_subdirectory(extern/pybind11) aux_source_directory(src SOURCE) #add_library(ambermd SHARED ${SOURCE}) add_library(ambermd STATIC ${SOURCE}) +target_link_libraries(ambermd PUBLIC pybind11::module) +target_link_libraries(ambermd PUBLIC pybind11::embed) add_executable(amberMd main.cpp ) diff --git a/AmberMDrun/include/common.hpp b/AmberMDrun/include/common.hpp index 60394c0..eb3c830 100644 --- a/AmberMDrun/include/common.hpp +++ b/AmberMDrun/include/common.hpp @@ -28,4 +28,6 @@ inline void trim(std::string &s) s.erase(s.find_last_not_of(' ') + 1); } [[maybe_unused]] std::string executeCMD2(const std::vector& args, const bool inc_stderr = false); +void signalHandler(int signum); +void raiseHandler(int signum); #endif//AMBERMD_COMMON_HPP diff --git a/AmberMDrun/src/base.cpp b/AmberMDrun/src/base.cpp index 6d7076f..ebd470e 100644 --- a/AmberMDrun/src/base.cpp +++ b/AmberMDrun/src/base.cpp @@ -2,8 +2,10 @@ // Created by jack on 2022/9/19. // #include "base.hpp" +#include "common.hpp" #include "fmt/core.h" #include "fmt/os.h" +#include #include #define f fmt::format Base::Base(const std::string &name, SystemInfo systemInfo, const std::string &rst7, const std::string &refc, bool irest, @@ -27,6 +29,7 @@ void Base::writeInput() void Base::Run() { + signal(SIGINT, raiseHandler); if (this->done_ == true) this->done_ = false; writeInput(); charmmWater(); @@ -157,7 +160,7 @@ void Base::progress() while (!this->done_) { fswatcher_event_handler handler = {[&](fswatcher_event_handler *handler, fswatcher_event_type evtype, const char *src, const char *dst) -> bool { - if (src =="./" + this->name_ + ".out") + if (src == "./" + this->name_ + ".out") { (void) handler; (void) dst; diff --git a/AmberMDrun/src/common.cpp b/AmberMDrun/src/common.cpp index 812543b..4e25a7f 100644 --- a/AmberMDrun/src/common.cpp +++ b/AmberMDrun/src/common.cpp @@ -4,18 +4,21 @@ #include "common.hpp" #include "fmt/format.h" +#include "pybind11/pybind11.h" +#include #include #include #include +#include #include #include - - +namespace py = pybind11; std::vector executeCMD(const std::string &strCmd) { + + signal(SIGINT, signalHandler); char buf[1024] = {0}; FILE *pf = nullptr; - if ((pf = popen(strCmd.c_str(), "r")) == nullptr) { throw std::runtime_error(fmt::format("{} run failed!\n", strCmd)); @@ -44,8 +47,9 @@ std::vector executeCMD(const std::string &strCmd) return result; } + [[maybe_unused]] std::string executeCMD2(const std::vector &args, - const bool inc_stderr) + const bool inc_stderr) { int stdout_fds[2]; pipe(stdout_fds); @@ -125,4 +129,13 @@ std::vector executeCMD(const std::string &strCmd) } while (r == -1 && errno == EINTR); return out; +} +void signalHandler(int signum) +{ + fmt::print("Interrupt signal{} received.\n", signum); + exit(signum); +} +void raiseHandler(int signum) +{ + raise(signum); } \ No newline at end of file