Skip to content
Snippets Groups Projects
Commit c465376e authored by Hkorb's avatar Hkorb
Browse files

refactor Python bindings

parent e37a3c8b
No related branches found
No related tags found
1 merge request!246Refactor python bindings
...@@ -6,27 +6,78 @@ endif() ...@@ -6,27 +6,78 @@ endif()
project(VirtualFluidsPython LANGUAGES ${PYFLUIDS_LANGUAGES}) project(VirtualFluidsPython LANGUAGES ${PYFLUIDS_LANGUAGES})
pybind11_add_module(python_bindings MODULE src/VirtualFluids.cpp) add_custom_target(python_bindings)
target_compile_definitions(python_bindings PUBLIC VF_DOUBLE_ACCURACY)
set_target_properties( python_bindings PROPERTIES set(PYFLUIDS_DIR ${SKBUILD_PLATLIB_DIR}/pyfluids)
LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/pythonbindings/pyfluids set(PYMUPRASER_DIR ${SKBUILD_PLATLIB_DIR}/pymuparser)
OUTPUT_NAME "bindings")
pybind11_add_module(basics_bindings MODULE src/basics/basics.cpp)
set_target_properties( basics_bindings PROPERTIES
LIBRARY_OUTPUT_DIRECTORY ${PYFLUIDS_DIR}
OUTPUT_NAME "basics")
target_link_libraries(basics_bindings PRIVATE basics)
target_include_directories(basics_bindings PRIVATE ${CMAKE_SOURCE_DIR}/src/)
target_include_directories(basics_bindings PRIVATE ${CMAKE_BINARY_DIR})
add_dependencies(python_bindings basics_bindings)
pybind11_add_module(logger_bindings MODULE src/logger.cpp)
set_target_properties( logger_bindings PROPERTIES
LIBRARY_OUTPUT_DIRECTORY ${PYFLUIDS_DIR}
OUTPUT_NAME "logger")
target_link_libraries(logger_bindings PRIVATE logger)
target_include_directories(logger_bindings PRIVATE ${CMAKE_SOURCE_DIR}/src/)
target_include_directories(logger_bindings PRIVATE ${CMAKE_BINARY_DIR})
add_dependencies(python_bindings logger_bindings)
pybind11_add_module(lbm_bindings MODULE src/lbm.cpp)
set_target_properties( lbm_bindings PROPERTIES
LIBRARY_OUTPUT_DIRECTORY ${PYFLUIDS_DIR}
OUTPUT_NAME "lbm")
target_link_libraries(lbm_bindings PRIVATE lbm)
target_include_directories(lbm_bindings PRIVATE ${CMAKE_SOURCE_DIR}/src/)
target_include_directories(lbm_bindings PRIVATE ${CMAKE_BINARY_DIR})
add_dependencies(python_bindings lbm_bindings)
target_link_libraries(python_bindings PRIVATE basics logger mpi lbm)
IF(BUILD_VF_GPU) IF(BUILD_VF_GPU)
set_source_files_properties(src/VirtualFluids.cpp PROPERTIES LANGUAGE CUDA) pybind11_add_module(gpu_bindings MODULE src/gpu/gpu.cpp)
set_target_properties( gpu_bindings PROPERTIES
LIBRARY_OUTPUT_DIRECTORY ${PYFLUIDS_DIR}
OUTPUT_NAME "gpu")
target_link_libraries(gpu_bindings PRIVATE basics)
set_source_files_properties(src/gpu/gpu.cpp PROPERTIES LANGUAGE CUDA)
target_include_directories(gpu_bindings PRIVATE ${VF_THIRD_DIR}/cuda_samples/)
target_include_directories(python_bindings PRIVATE ${VF_THIRD_DIR}/cuda_samples/) target_link_libraries(gpu_bindings PRIVATE GridGenerator VirtualFluids_GPU)
target_compile_definitions(python_bindings PRIVATE VF_GPU_PYTHONBINDINGS)
target_include_directories(gpu_bindings PRIVATE ${CMAKE_SOURCE_DIR}/src/)
target_include_directories(gpu_bindings PRIVATE ${CMAKE_BINARY_DIR})
add_dependencies(python_bindings gpu_bindings)
target_link_libraries(python_bindings PRIVATE GridGenerator VirtualFluids_GPU)
ENDIF() ENDIF()
IF(BUILD_VF_CPU) IF(BUILD_VF_CPU)
target_compile_definitions(python_bindings PRIVATE VF_METIS VF_MPI VF_CPU_PYTHONBINDINGS) pybind11_add_module(cpu_bindings MODULE src/cpu/cpu.cpp)
target_link_libraries(python_bindings PRIVATE simulationconfig VirtualFluidsCore muparser) set_target_properties( cpu_bindings PROPERTIES
LIBRARY_OUTPUT_DIRECTORY ${PYFLUIDS_DIR}
OUTPUT_NAME "cpu")
target_link_libraries(cpu_bindings PRIVATE simulationconfig VirtualFluidsCore muparser)
target_include_directories(cpu_bindings PRIVATE ${CMAKE_SOURCE_DIR}/src/)
target_include_directories(cpu_bindings PRIVATE ${CMAKE_BINARY_DIR})
target_compile_definitions(cpu_bindings PUBLIC VF_DOUBLE_ACCURACY) # TODO: remove this and always set it dynamically
target_compile_definitions(basics_bindings PUBLIC VF_DOUBLE_ACCURACY)
target_compile_definitions(logger_bindings PUBLIC VF_DOUBLE_ACCURACY)
target_compile_definitions(lbm_bindings PUBLIC VF_DOUBLE_ACCURACY)
target_compile_definitions(cpu_bindings PRIVATE VF_METIS VF_MPI)
add_dependencies(python_bindings cpu_bindings)
# include bindings for muparsers # include bindings for muparsers
pybind11_add_module(pymuparser MODULE src/muParser.cpp) pybind11_add_module(pymuparser MODULE src/muParser.cpp)
...@@ -40,7 +91,3 @@ IF(BUILD_VF_CPU) ...@@ -40,7 +91,3 @@ IF(BUILD_VF_CPU)
target_compile_definitions(pymuparser PRIVATE VF_METIS VF_MPI) target_compile_definitions(pymuparser PRIVATE VF_METIS VF_MPI)
target_link_libraries(pymuparser PRIVATE muparser) target_link_libraries(pymuparser PRIVATE muparser)
ENDIF() ENDIF()
target_include_directories(python_bindings PRIVATE ${CMAKE_SOURCE_DIR}/src/)
target_include_directories(python_bindings PRIVATE ${CMAKE_BINARY_DIR})
\ No newline at end of file
...@@ -37,12 +37,8 @@ namespace basics ...@@ -37,12 +37,8 @@ namespace basics
{ {
namespace py = pybind11; namespace py = pybind11;
py::module makeModule(py::module_ &parentModule) PYBIND11_MODULE(basics, m)
{ {
py::module basicsModule = parentModule.def_submodule("basics"); configuration::makeModule(m);
configuration::makeModule(basicsModule);
return basicsModule;
} }
} }
\ No newline at end of file
...@@ -38,18 +38,16 @@ ...@@ -38,18 +38,16 @@
#include "submodules/simulationparameters.cpp" #include "submodules/simulationparameters.cpp"
#include "submodules/writer.cpp" #include "submodules/writer.cpp"
namespace cpu namespace cpu_bindings
{ {
namespace py = pybind11; namespace py = pybind11;
py::module makeModule(py::module_ &parentModule) PYBIND11_MODULE(cpu, m)
{ {
py::module cpuModule = parentModule.def_submodule("cpu"); boundaryconditions::makeModule(m);
boundaryconditions::makeModule(cpuModule); simulation::makeModule(m);
simulation::makeModule(cpuModule); geometry::makeModule(m);
geometry::makeModule(cpuModule); kernel::makeModule(m);
kernel::makeModule(cpuModule); parameters::makeModule(m);
parameters::makeModule(cpuModule); writer::makeModule(m);
writer::makeModule(cpuModule);
return cpuModule;
} }
} }
\ No newline at end of file
...@@ -46,27 +46,25 @@ ...@@ -46,27 +46,25 @@
#include "submodules/actuator_farm.cpp" #include "submodules/actuator_farm.cpp"
#include "submodules/grid_scaling_factory.cpp" #include "submodules/grid_scaling_factory.cpp"
namespace gpu namespace gpu_bindings
{ {
namespace py = pybind11; namespace py = pybind11;
py::module makeModule(py::module_ &parentModule) PYBIND11_MODULE(gpu, m)
{ {
py::module gpuModule = parentModule.def_submodule("gpu"); simulation::makeModule(m);
simulation::makeModule(gpuModule); parameter::makeModule(m);
parameter::makeModule(gpuModule); pre_collision_interactor::makeModule(m);
pre_collision_interactor::makeModule(gpuModule); actuator_farm::makeModule(m);
actuator_farm::makeModule(gpuModule); boundary_conditions::makeModule(m);
boundary_conditions::makeModule(gpuModule); transient_bc_setter::makeModule(m);
transient_bc_setter::makeModule(gpuModule); communicator::makeModule(m);
communicator::makeModule(gpuModule); cuda_memory_manager::makeModule(m);
cuda_memory_manager::makeModule(gpuModule); probes::makeModule(m);
probes::makeModule(gpuModule); precursor_writer::makeModule(m);
precursor_writer::makeModule(gpuModule); grid_generator::makeModule(m);
grid_generator::makeModule(gpuModule); grid_provider::makeModule(m);
grid_provider::makeModule(gpuModule); turbulence_model::makeModule(m);
turbulence_model::makeModule(gpuModule); grid_scaling_factory::makeModule(m);
grid_scaling_factory::makeModule(gpuModule);
return gpuModule;
} }
} }
\ No newline at end of file
...@@ -32,14 +32,11 @@ ...@@ -32,14 +32,11 @@
//======================================================================================= //=======================================================================================
#include <pybind11/pybind11.h> #include <pybind11/pybind11.h>
namespace lbm namespace lbm_bindings
{ {
namespace py = pybind11; namespace py = pybind11;
py::module makeModule(py::module_ &parentModule) PYBIND11_MODULE(lbm, m)
{ {
py::module lbmModule = parentModule.def_submodule("lbm");
return lbmModule;
} }
} }
\ No newline at end of file
...@@ -33,25 +33,23 @@ ...@@ -33,25 +33,23 @@
#include <pybind11/pybind11.h> #include <pybind11/pybind11.h>
#include <logger/Logger.h> #include <logger/Logger.h>
namespace logging namespace logger_bindings
{ {
namespace py = pybind11; namespace py = pybind11;
py::module makeModule(py::module_ &parentModule) PYBIND11_MODULE(logger, m)
{ {
py::module loggerModule = parentModule.def_submodule("logger"); py::class_<vf::logging::Logger>(m, "Logger")
py::class_<vf::logging::Logger>(loggerModule, "Logger")
.def_static("initialize_logger", &vf::logging::Logger::initializeLogger) .def_static("initialize_logger", &vf::logging::Logger::initializeLogger)
.def_static("change_log_path", &vf::logging::Logger::changeLogPath, py::arg("path")); .def_static("change_log_path", &vf::logging::Logger::changeLogPath, py::arg("path"));
// use f-strings (f"text {float}") in python for compounded messages // use f-strings (f"text {float}") in python for compounded messages
loggerModule.def("vf_log_trace", [](std::string message){ VF_LOG_TRACE(message); }, py::arg("message")); m.def("vf_log_trace", [](std::string message){ VF_LOG_TRACE(message); }, py::arg("message"));
loggerModule.def("vf_log_debug", [](std::string message){ VF_LOG_DEBUG(message); }, py::arg("message")); m.def("vf_log_debug", [](std::string message){ VF_LOG_DEBUG(message); }, py::arg("message"));
loggerModule.def("vf_log_info", [](std::string message){ VF_LOG_INFO(message); }, py::arg("message")); m.def("vf_log_info", [](std::string message){ VF_LOG_INFO(message); }, py::arg("message"));
loggerModule.def("vf_log_warning", [](std::string message){ VF_LOG_WARNING(message); }, py::arg("message")); m.def("vf_log_warning", [](std::string message){ VF_LOG_WARNING(message); }, py::arg("message"));
loggerModule.def("vf_log_critical", [](std::string message){ VF_LOG_CRITICAL(message); }, py::arg("message")); m.def("vf_log_critical", [](std::string message){ VF_LOG_CRITICAL(message); }, py::arg("message"));
return loggerModule;
} }
} // namespace logging }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment