diff --git a/hw4-pybind/CMakeLists.txt b/hw4-pybind/CMakeLists.txt index 22285d6c..7dca00ac 100644 --- a/hw4-pybind/CMakeLists.txt +++ b/hw4-pybind/CMakeLists.txt @@ -1,117 +1,117 @@ cmake_minimum_required (VERSION 3.1) project (Particles) cmake_policy(VERSION 3.3) set(CMAKE_EXPORT_COMPILE_COMMANDS 1) set(CMAKE_CXX_STANDARD 14) set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${PROJECT_SOURCE_DIR}/cmake") ################################################################ # libpart ################################################################ add_library(part SHARED compute_boundary.cc compute_verlet_integration.cc particle.cc planet.cc compute_gravity.cc csv_reader.cc particles_factory_interface.cc planets_factory.cc compute_contact.cc compute_kinetic_energy.cc csv_writer.cc system.cc compute_energy.cc compute_potential_energy.cc ping_pong_ball.cc material_point.cc system_evolution.cc ping_pong_balls_factory.cc compute_interaction.cc compute_temperature.cc material_points_factory.cc ) ################################################################ # FFTW ################################################################ option(USE_FFTW "Use fftw library" ON) if(USE_FFTW) add_definitions(-DUSE_FFTW) set(FFTW_LIBRARY_PATH CACHE PATH /usr/local/include/) # "library where to search libfftw3" find_library(FFTW_LIBRARIES fftw3 ${FFTW_LIBRARY_PATH} /usr/lib/x86_64-linux-gnu/) set(FFTW_INCLUDE_PATH CACHE PATH /usr/local/include) # "path where to search fftw include files" find_path(FFTW_INCLUDES fftw3.h ${FFTW_INCLUDE_PATH} /usr/include/) include_directories(${FFTW_INCLUDES}) #find_package(FFTW) target_link_libraries(part ${FFTW_LIBRARIES}) endif(USE_FFTW) ################################################################ # Particles executable ################################################################ add_executable(particles main.cc) target_link_libraries(particles part) ################################################################ # Google test ################################################################ add_subdirectory(googletest) add_executable(test_kepler test_kepler.cc) target_link_libraries(test_kepler part gtest_main gtest pthread) if(USE_FFTW) add_executable(test_fft test_fft.cc) target_link_libraries(test_fft part gtest_main gtest ${FFTW_LIBRARIES} pthread) endif(USE_FFTW) ################################################################ # Doxygen ################################################################ # find_package(Doxygen) # if (DOXYGEN_FOUND) # # # to set other options, read: https://cmake.org/cmake/help/v3.9/module/FindDoxygen.html # doxygen_add_docs( # doxygen # ${PROJECT_SOURCE_DIR} # COMMENT "Generate html pages" # ) # add_custom_target(doc DEPENDS doxygen) # endif(DOXYGEN_FOUND) ################################################################ # Pybind ################################################################ -option (USE_PYTHON "Use pybind library" OFF) +option (USE_PYTHON "Use pybind library" ON) if (USE_PYTHON) #find_package(pybind11 REQUIRED) add_subdirectory(pybind11) add_library(pypart MODULE pypart.cc) target_link_libraries(pypart PRIVATE pybind11::module part) set_target_properties(pypart PROPERTIES PREFIX "${PYTHON_MODULE_PREFIX}" SUFFIX "${PYTHON_MODULE_EXTENSION}" ) file( COPY ${CMAKE_CURRENT_SOURCE_DIR}/main.py DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/ ) endif(USE_PYTHON) diff --git a/hw4-pybind/README.md b/hw4-pybind/README.md index 91f7933d..8cfe1f37 100644 --- a/hw4-pybind/README.md +++ b/hw4-pybind/README.md @@ -1,16 +1,23 @@ # Homework 4 +### Exercise 1: + +The role of function "createSimulation" with overloading is to do : +1. +2. +... + ### Exercise 4: Can run the example using the command `./particles 365 1 ../init.csv planet 1` from the `build` folder. From C++ the gravitational constant G must be changed accordingly. The trajectories can be viewed using Paraview, and we observe trajectories as expected. Except for Mercury. ### Exercise 5 through 7: See the file `python_functions.py`. Run the file from the `build` folder with `python3 ../python_functions.py`. Note that we assume 5 integers in the filenames (e.g. `step-00000.csv`), i.e., not 4 as in the trajectory folder. We can easily rename multiple files using the command `rename 's/step-0/step-00/g' *` in order to get same naming convention. We find that the scaling factor for Mercury's velocity is approximately 0.4. diff --git a/hw4-pybind/pypart.cc b/hw4-pybind/pypart.cc index f8daecde..1c821eb9 100644 --- a/hw4-pybind/pypart.cc +++ b/hw4-pybind/pypart.cc @@ -1,47 +1,64 @@ #include #include #include #include "material_points_factory.hh" #include "ping_pong_balls_factory.hh" #include "planets_factory.hh" #include "csv_writer.hh" #include "compute_temperature.hh" #include "compute_gravity.hh" #include "compute_verlet_integration.hh" #include "compute_interaction.hh" namespace py = pybind11; PYBIND11_MODULE(pypart, m) { m.doc() = "pybind pypart plugin"; // optional docstring // TODO: // The member functions of the various classes (used in main.py) must be exposed to pybind - // Exercise 1 - Question 1, 2, 3 - py::class_(m, "ParticlesFactoryInterface"); + py::class_(m, "ParticlesFactoryInterface") + //.def("createSimulation",&createSimulation); + .def("getInstance", &ParticlesFactoryInterface::getInstance); // DOES NOT COMPILE: - //.def("createSimulation", py::overload_cast(&ParticlesFactoryInterface::createSimulation), "create simulation without functor") - //.def("createSimulation", py::overload_cast(&ParticlesFactoryInterface::createSimulation), "create simulation with function"); - - py::class_(m, "MaterialPointsFactory" ); // inherits from ParticlesFactoryInterface - py::class_(m, "PingPongBallsFactory" ); // inherits from ParticlesFactoryInterface - py::class_(m, "PlanetsFactory" ); // inherits from ParticlesFactoryInterface + //.def(py::init(m, "MaterialPointsFactory" ) + .def("getInstance", &MaterialPointsFactory::getInstance); // inherits from ParticlesFactoryInterface + py::class_(m, "PingPongBallsFactory" ) // inherits from ParticlesFactoryInterface + .def("getInstance", &PingPongBallsFactory::getInstance); + py::class_(m, "PlanetsFactory" ) // inherits from ParticlesFactoryInterface + .def("getInstance", &PlanetsFactory::getInstance); - - // Exercise 2 - Question 1 + // Exercise 2 - Question 1, 2, 3 py::class_(m, "Compute" ); - py::class_(m, "CsvWriter" ); // inherits from Compute - py::class_(m, "ComputeTemperature" ); // inherits from Compute - py::class_(m, "ComputeGravity" ); // inherits from Compute - py::class_(m, "ComputeVerletIntegration"); // inherits from Compute - py::class_(m, "ComputeInteraction" ); // inherits from Compute + py::class_(m, "CsvWrite" ) // inherits from Compute + .def("write", &CsvWriter::write); + py::class_(m, "ComputeTemperature", py::dynamic_attr()) // inherits from Compute + .def(py::init<>()) + .def_property_readonly("conductivity", &ComputeTemperature::getConductivity) + .def_property_readonly("capacity", &ComputeTemperature::getCapacity) + .def_property_readonly("L", &ComputeTemperature::getL) + .def_property_readonly("deltat", &ComputeTemperature::getDeltat); + py::class_(m, "ComputeGravity") // inherits from Compute + .def("setG", &ComputeGravity::setG); + py::class_(m, "ComputeVerletIntegration") // inherits from Compute + .def("addInteraction", &ComputeVerletIntegration::addInteraction); + // Exercise 3 - Question 1 + py::class_(m, "system_evolution") // no heritage but referenced : strange ! system_evolution in main.py !? + .def("evolve", &SystemEvolution::evolve) + .def("addCompute", &SystemEvolution::addCompute) + .def("getSystem", &SystemEvolution::getSystem) + .def("setNSteps", &SystemEvolution::setNSteps) + .def("setDumpFreq", &SystemEvolution::setDumpFreq); +// ??? py::class_(m, "ComputeInteraction" ); // inherits from Compute // Should we use the inheritance class in pybind??? Like below? - //py::class_(m, "PingPongBallsFactory"); - + // py::class_(m, "PingPongBallsFactory"); }