diff --git a/hw4-pybind/README.md b/hw4-pybind/README.md index 8cfe1f37..ca62769b 100644 --- a/hw4-pybind/README.md +++ b/hw4-pybind/README.md @@ -1,23 +1,23 @@ # Homework 4 ### Exercise 1: -The role of function "createSimulation" with overloading is to do : -1. -2. -... +1.2. : The role of function overloading "createSimulation" to associate one method or another one depending of the type or number of arguments that takes the method. + +### Exercise 2: +2.2. : To ensure that references are correctly managed to python bindings we used shared pointers : std::shared_ptr<>. Using this, the memory of these pointers will be DEALLOCATED when the python's reference goes to 0! ### 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 1d821a6a..3604e006 100644 --- a/hw4-pybind/pypart.cc +++ b/hw4-pybind/pypart.cc @@ -1,85 +1,78 @@ #include #include #include #include "material_points_factory.hh" #include "ping_pong_balls_factory.hh" #include "planets_factory.hh" #include "compute.hh" #include "compute_temperature.hh" #include "compute_gravity.hh" #include "compute_verlet_integration.hh" #include "compute_interaction.hh" #include "csv_writer.hh" #include "system.hh" #include "system_evolution.hh" namespace py = pybind11; PYBIND11_MODULE(pypart, m) { m.doc() = "pybind pypart plugin"; // optional docstring - // The member functions of the various classes (used in main.py) are exposed to pybind + // Bindings to pybind of the member functions of the various classes used in main.py. // Exercise 1 - Question 1, 2, 3 py::class_(m, "ParticlesFactoryInterface") .def("getInstance", &ParticlesFactoryInterface::getInstance, py::return_value_policy::reference) .def("createSimulation",(SystemEvolution & (ParticlesFactoryInterface::*)(const std::string &, Real)) &ParticlesFactoryInterface::createSimulation, py::return_value_policy::reference) - .def("createSimulation",py::overload_cast(&ParticlesFactoryInterface::createSimulation), - py::return_value_policy::reference) - //.def("createSimulation", &ParticlesFactoryInterface::createSimulation,py::return_value_policy::reference) - //.def("createSimulation",py::overload_cast(&ParticlesFactoryInterface::createSimulation),py::return_value_policy::reference) + .def("createSimulation",(SystemEvolution & (ParticlesFactoryInterface::*)(const std::string &, Real,py::function))&ParticlesFactoryInterface::createSimulation, py::return_value_policy::reference) + // COMMENT : We tried also with OVERLOAD but DID NOT WORK ! + //.def("createSimulation",py::overload_cast(&ParticlesFactoryInterface::createSimulation), + // py::return_value_policy::reference) .def_property_readonly("system_evolution", &ParticlesFactoryInterface::getSystemEvolution); py::class_(m, "MaterialPointsFactory") .def("getInstance", &MaterialPointsFactory::getInstance, py::return_value_policy::reference) - .def("createSimulation",py::overload_cast(&ParticlesFactoryInterface::createSimulation), - py::return_value_policy::reference); - //.def("createSimulation",(SystemEvolution & (MaterialPointsFactory::*)(const std::string &, Real))&MaterialPointsFactory::createSimulation, py::return_value_policy::reference); - //.def("createSimulation",py::overload_cast(&MaterialPointsFactory::createSimulation), py::return_value_policy::reference); + .def("createSimulation",(SystemEvolution & (MaterialPointsFactory::*)(const std::string &, Real))&MaterialPointsFactory::createSimulation, py::return_value_policy::reference) + .def("createSimulation",(SystemEvolution & (MaterialPointsFactory::*)(const std::string &, Real,py::function))&MaterialPointsFactory::createSimulation, py::return_value_policy::reference); py::class_(m, "PingPongBallsFactory") - .def("getInstance", &PingPongBallsFactory::getInstance, py::return_value_policy::reference); - //.def("createSimulation",py::overload_cast(&ParticlesFactoryInterface::createSimulation), - // py::return_value_policy::reference); - //.def("createSimulation", &PingPongBallsFactory::createSimulation,py::return_value_policy::reference); + .def("getInstance", &PingPongBallsFactory::getInstance, py::return_value_policy::reference) + .def("createSimulation",(SystemEvolution & (PingPongBallsFactory::*)(const std::string &, Real))&PingPongBallsFactory::createSimulation, py::return_value_policy::reference) + .def("createSimulation",(SystemEvolution & (PingPongBallsFactory::*)(const std::string &, Real,py::function))&PingPongBallsFactory::createSimulation, py::return_value_policy::reference); py::class_(m, "PlanetsFactory") - .def("getInstance", &PlanetsFactory::getInstance, py::return_value_policy::reference); - //.def("createSimulation",py::overload_cast(&ParticlesFactoryInterface::createSimulation), - // py::return_value_policy::reference); - //.def("createSimulation", &PlanetsFactory::createSimulation,py::return_value_policy::reference); + .def("getInstance", &PlanetsFactory::getInstance, py::return_value_policy::reference) + .def("createSimulation",(SystemEvolution & (PlanetsFactory::*)(const std::string &, Real))&PlanetsFactory::createSimulation, py::return_value_policy::reference) + .def("createSimulation",(SystemEvolution & (PlanetsFactory::*)(const std::string &, Real,py::function))&PlanetsFactory::createSimulation, py::return_value_policy::reference); // Exercise 2 - Question 1, 2, 3 - py::class_>(m, "Compute"); //To try: for memory conflict problem maybe: std::shared_ptr :NO + py::class_>(m, "Compute"); //To handle memory conflict problem. py::class_>(m, "ComputeInteraction"); py::class_>(m, "ComputeTemperature") // (tried: py::dynamic_attr()) .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") .def(py::init<>()) .def("setG", &ComputeGravity::setG); py::class_>(m, "ComputeVerletIntegration") .def(py::init()) .def("addInteraction", &ComputeVerletIntegration::addInteraction); // Exercise 3 - Question 1 py::class_(m, "CsvWriter") .def(py::init()) .def("write", &CsvWriter::write); - py::class_(m, "SystemEvolution") // no heritage but referenced : strange ! system_evolution in main.py !? + py::class_(m, "SystemEvolution") // no heritage but linkage .def("evolve", &SystemEvolution::evolve) .def("addCompute", &SystemEvolution::addCompute) .def("getSystem", &SystemEvolution::getSystem) .def("setNSteps", &SystemEvolution::setNSteps) .def("setDumpFreq", &SystemEvolution::setDumpFreq); py::class_(m, "System"); // not necessary but added for clarity }