diff --git a/HW4/py_wrap.cc b/HW4/py_wrap.cc index aa24d19..2e16d20 100644 --- a/HW4/py_wrap.cc +++ b/HW4/py_wrap.cc @@ -1,112 +1,111 @@ #include #include #include "material_points_factory.hh" #include "ping_pong_balls_factory.hh" #include "particles_factory_interface.hh" #include "planets_factory.hh" #include "compute_temperature.hh" #include "csv_writer.hh" #include "system.hh" // All the classes which are to be used in python are included here, together with the needed external library. +// instead of just including this header at the end of each file! namespace py = pybind11; -//Start of the pybind module: +//Start of the py_wrap.cc module: +PYBIND11_MODULE(pypart,m){ -PYBIND11_MODULE(pypart,m) { + // Generate Documentation m.doc()="This file is to wrap the C++ library over a Python API for the particles project."; -// - + // Wrapping ParticlesFactoryInterface with Reference Return (C++ will handle the Garbage Collection) py::class_(m, "ParticlesFactoryInterface") + .def("getInstance", &ParticlesFactoryInterface::getInstance, - py::return_value_policy::reference); + py::return_value_policy::reference) + ; + // Wrapping the PingPongBallsFactory (Reference Return) py::class_(m, "PingPongBallsFactory") + .def("getInstance", &PingPongBallsFactory::getInstance, - py::return_value_policy::reference) + py::return_value_policy::reference) + .def("createSimulation", &PingPongBallsFactory::createSimulation, - py::return_value_policy::reference); + py::return_value_policy::reference) + ; + // Wrapping the PlanetsFactory interface (Reference Return) py::class_(m, "PlanetsFactory") + .def("getInstance", &PlanetsFactory::getInstance, py::return_value_policy::reference) + .def("createSimulation", &PlanetsFactory::createSimulation, - py::return_value_policy::reference); - + py::return_value_policy::reference) + ; + // Wrapping the MaterialPointsFactory (Reference Return) py::class_(m, "MaterialPointsFactory") + .def("getInstance", &MaterialPointsFactory::getInstance, - py::return_value_policy::reference) + py::return_value_policy::reference) + .def("createSimulation", - (SystemEvolution & (MaterialPointsFactory::*)(const std::string &, Real)) - &MaterialPointsFactory::createSimulation, - py::return_value_policy::reference) + (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); - -// In this case py::overload_cast operator has been used. The reason is that otherwise compiler can not know which method to use and thus produces the error. -// In Python method overloading does not exist in the same way like in c++ (there is no overloading resolution by the compiler obviously), thus we use py::overload_cast. + (&MaterialPointsFactory::createSimulation), + py::return_value_policy::reference) + ; + // In this case py::overload_cast operator has been used. The reason is that otherwise compiler can not know which method to use and thus produces the error. + // In Python method overloading does not exist in the same way like in c++ (there is no overloading resolution by the compiler obviously), thus we use py::overload_cast. + // Wrapping SystemEvoution class py::class_(m, "SystemEvolution") + .def("setNSteps", &SystemEvolution::setNSteps) + .def("setDumpFreq", &SystemEvolution::setDumpFreq) + .def("evolve", &SystemEvolution::evolve) + .def("getSystem", &SystemEvolution::getSystem, - py::return_value_policy::reference); + py::return_value_policy::reference) + ; + // Wrapping the system writer (CSV files) py::class_(m, "CsvWriter") - .def(py::init()) - .def("write", &CsvWriter::write); - - py::class_(m, "System"); - -// To read protected classes variables in Python, it is necessary to use "getsmt" methods, instead of reading them directly. -/* - py::class_(m, "ComputeTemperature") - .def("compute", &ComputeTemperature::compute) - .def_property("deltat", &ComputeTemperature::getDeltat); - .def_property("density", &ComputeTemperature::getDensity) - .def_property("L", &ComputeTemperature::getL) - .def_property("capacity", &ComputeTemperature::getCapacity) - .def_property("conductivity", &ComputeTemperature::getConductivity) -*/ - - - - - - - - - - - - - - - - - - - - - - - + .def(py::init()) + .def("write", &CsvWriter::write) + ; + py::class_(m, "System"); + // Wrapping ComputeTemperature class + // To read protected classes variables in Python, it is necessary to use "getsmt" methods, instead of reading them directly. + py::class_(m, "ComputeTemperature") +// .def("compute", &ComputeTemperature::compute) + ; // delete this semicolon for debugging only +/* + .def_property("deltat", &ComputeTemperature::getDeltat) + .def_property("density", &ComputeTemperature::getDensity) + .def_property("L", &ComputeTemperature::getL) + .def_property("capacity", &ComputeTemperature::getCapacity) + .def_property("conductivity", &ComputeTemperature::getConductivity) + ; + */ } diff --git a/HW4/pypart.cc b/HW4/pypart.cc deleted file mode 100644 index c49003d..0000000 --- a/HW4/pypart.cc +++ /dev/null @@ -1,113 +0,0 @@ -#include -#include -#include "material_points_factory.hh" -#include "ping_pong_balls_factory.hh" -#include "particles_factory_interface.hh" -#include "planets_factory.hh" -#include "compute_temperature.hh" -#include "csv_writer.hh" -#include "system.hh" - -// All the classes which are to be used in python are included here, together with the needed external library. - -namespace py = pybind11; - -//Start of the pybind module: - -PYBIND11_MODULE(pypart,m) { - m.doc()="This file is to wrap the C++ library over a Python API for the particles project."; - -// - - py::class_(m, "ParticlesFactoryInterface") - .def("getInstance", - &ParticlesFactoryInterface::getInstance, - py::return_value_policy::reference); - - py::class_(m, "PingPongBallsFactory") - .def("getInstance", &PingPongBallsFactory::getInstance, - py::return_value_policy::reference) - .def("createSimulation", - &PingPongBallsFactory::createSimulation, - py::return_value_policy::reference); - - py::class_(m, "PlanetsFactory") - .def("getInstance", &PlanetsFactory::getInstance, - py::return_value_policy::reference) - .def("createSimulation", - &PlanetsFactory::createSimulation, - py::return_value_policy::reference); - - - py::class_(m, "MaterialPointsFactory") - .def("getInstance", &MaterialPointsFactory::getInstance, - 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); - -// In this case py::overload_cast operator has been used. The reason is that otherwise compiler can not know which method to use and thus produces the error. -// In Python method overloading does not exist in the same way like in c++ (there is no overloading resolution by the compiler obviously), thus we use py::overload_cast. - - py::class_(m, "SystemEvolution") - .def("setNSteps", &SystemEvolution::setNSteps) - .def("setDumpFreq", &SystemEvolution::setDumpFreq) - .def("evolve", &SystemEvolution::evolve) - .def("getSystem", &SystemEvolution::getSystem, - py::return_value_policy::reference); - - py::class_(m, "CsvWriter") - .def(py::init()) - .def("write", &CsvWriter::write); - - py::class_(m, "System"); - -// To read protected classes variables in Python, it is necessary to use "getsmt" methods, instead of reading them directly. - -/* - py::class_(m, "ComputeTemperature") - .def("compute", &ComputeTemperature::compute) - .def_property("deltat", &ComputeTemperature::getDeltat); - .def_property("density", &ComputeTemperature::getDensity) - .def_property("L", &ComputeTemperature::getL) - .def_property("capacity", &ComputeTemperature::getCapacity) - .def_property("conductivity", &ComputeTemperature::getConductivity) - - -*/ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -}