-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create pyvcell_fvsolver Python module using pybind11 and vcell library
- Loading branch information
Showing
8 changed files
with
163 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,18 @@ | ||
project(example) | ||
project(pyvcell_fvsolver) | ||
set(CMAKE_CXX_STANDARD 17) | ||
set(CMAKE_CXX_STANDARD_REQUIRED ON) | ||
|
||
set(HEADER_FILES | ||
include/add.h | ||
include/SolverMain.h | ||
) | ||
|
||
set(SOURCE_FILES | ||
src/add.cpp | ||
src/example.cpp | ||
src/pyvcell_fvsolver.cpp | ||
src/SolverMain.cpp | ||
) | ||
|
||
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include) | ||
|
||
pybind11_add_module(example ${SOURCE_FILES} ${HEADER_FILES}) | ||
pybind11_add_module(pyvcell_fvsolver ${SOURCE_FILES} ${HEADER_FILES}) | ||
|
||
target_link_libraries(pyvcell_fvsolver PRIVATE vcell) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// | ||
// Created by Jim Schaff on 6/1/24. | ||
// | ||
|
||
#ifndef SOLVERMAIN_H | ||
#define SOLVERMAIN_H | ||
|
||
#include <string> | ||
|
||
std::string version(); | ||
int solve(const std::string& inputFilename, const std::string& outputDir); | ||
|
||
#endif //SOLVERMAIN_H |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
// | ||
// Created by Jim Schaff on 6/1/24. | ||
// | ||
#include "SolverMain.h" | ||
|
||
#include <iostream> | ||
#include <fstream> | ||
#include <string> | ||
#include <filesystem> | ||
using namespace std; | ||
|
||
#undef USE_MESSAGING | ||
|
||
#include <VCELL/FVSolver.h> | ||
#include <sys/stat.h> | ||
#include <VCELL/SimTool.h> | ||
#include <VCELL/SimulationMessaging.h> | ||
#include <VCELL/GitDescribe.h> | ||
#include <Exception.h> | ||
#include <vcellhybrid.h> | ||
|
||
|
||
|
||
void vcellExit(int returnCode, string& errorMsg) { | ||
if (SimulationMessaging::getInstVar() == 0) { | ||
if (returnCode != 0) { | ||
cerr << errorMsg << endl; | ||
} | ||
} else if (!SimulationMessaging::getInstVar()->isStopRequested()) { | ||
if (returnCode != 0) { | ||
SimulationMessaging::getInstVar()->setWorkerEvent(new WorkerEvent(JOB_FAILURE, errorMsg.c_str())); | ||
} | ||
} | ||
} | ||
|
||
std::string version() | ||
{ | ||
return "Finite Volume version " + std::string(g_GIT_DESCRIBE) + " with smoldyn version " + std::string(VERSION); | ||
} | ||
|
||
int solve(const std::string& inputFilename, const std::string& outputDir) { | ||
// Check if output directory exists, if not create it | ||
std::filesystem::path dirPath(outputDir); | ||
if (!std::filesystem::exists(dirPath)) { | ||
std::filesystem::create_directories(dirPath); | ||
} | ||
|
||
// Open the input file | ||
std::ifstream inputFile(inputFilename); | ||
if (!inputFile.is_open()) { | ||
throw std::runtime_error("Could not open input file: " + inputFilename); | ||
} | ||
|
||
vcellhybrid::setHybrid(); //get smoldyn library in correct state | ||
int returnCode = 0; | ||
string errorMsg = "Exception : "; | ||
|
||
bool bSimZip = true; | ||
int taskID = 0; | ||
|
||
SimulationMessaging::create(); | ||
|
||
FVSolver* fvSolver = nullptr; | ||
|
||
try { | ||
fvSolver = new FVSolver(inputFile, taskID, outputDir.c_str(), bSimZip); | ||
|
||
inputFile.close(); | ||
|
||
if(fvSolver->getNumVariables() == 0){ | ||
//sims with no reactions and no diffusing species cause exit logic to 'wait' forever | ||
//never sending a job failed or job finished message and never cleaning up threads | ||
throw invalid_argument("FiniteVolume error: Must have at least 1 variable or reaction to solve"); | ||
} | ||
fvSolver->solve(); | ||
|
||
} catch (const char *exStr){ | ||
errorMsg += exStr; | ||
returnCode = 1; | ||
} catch (string& exStr){ | ||
errorMsg += exStr; | ||
returnCode = 1; | ||
} catch (VCell::Exception& ex){ | ||
errorMsg += ex.getMessage(); | ||
returnCode = 1; | ||
} catch (std::exception & e) { | ||
errorMsg += e.what(); | ||
returnCode = 1; | ||
} catch (...){ | ||
errorMsg += "unknown error"; | ||
returnCode = 1; | ||
} | ||
|
||
if (inputFile.is_open()) { | ||
inputFile.close(); | ||
} | ||
vcellExit(returnCode, errorMsg); | ||
delete fvSolver; | ||
return returnCode; | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#include <../../extern/pybind11/include/pybind11/pybind11.h> | ||
|
||
#include "SolverMain.h" | ||
|
||
namespace py = pybind11; | ||
|
||
PYBIND11_MODULE(pyvcell_fvsolver, m) { | ||
m.doc() = "VCell FiniteVolume plugin"; // optional module docstring | ||
|
||
m.def("version", &version, "A function that returns the version of the FiniteVolume solver"); | ||
|
||
m.def("solve", &solve, "A function that invokes the FiniteVolume solver", py::arg("inputFilename"), py::arg("outputDir")); | ||
} |