diff --git a/HW4/py_wrap.cc b/HW4/py_wrap.cc index 271d3b8..da59609 100644 --- a/HW4/py_wrap.cc +++ b/HW4/py_wrap.cc @@ -1,143 +1,143 @@ #include #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" #include "my_types.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 py_wrap.cc module: 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) + // Wrapping ParticlesFactoryInterface with Reference Return (C++ will handle the Garbage Collection). However, we don't need the Reference Return in all cases. Primary case when required is if we're dealing with singletons. py::class_(m, "ParticlesFactoryInterface") .def("getInstance", &ParticlesFactoryInterface::getInstance, py::return_value_policy::reference) .def("createSimulation", &ParticlesFactoryInterface::createSimulation) ; // Wrapping the PingPongBallsFactory (Reference Return) py::class_(m, "PingPongBallsFactory") .def("getInstance", &PingPongBallsFactory::getInstance, py::return_value_policy::reference) .def("createSimulation", &PingPongBallsFactory::createSimulation, 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) ; // Wrapping the MaterialPointsFactory (Reference Return) 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. // Here, py::overload_cast only requires the parameter types to be specified. The return type and class are deduced .def_property_readonly("system_evolution", &MaterialPointsFactory::getSystemEvolution); ; // Wrapping SystemEvoution class py::class_(m, "SystemEvolution") .def("setNSteps", &SystemEvolution::setNSteps) .def("setDumpFreq", &SystemEvolution::setDumpFreq) .def("evolve", &SystemEvolution::evolve) .def("addCompute", &SystemEvolution::addCompute) .def("getSystem", &SystemEvolution::getSystem, 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"); py::class_>(m, "Compute"); // 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") // Define Getters first and then Setters, otherwise doesn't work! .def(py::init()) .def("compute", &ComputeTemperature::compute) .def_property("deltat", &ComputeTemperature::getDeltat, &ComputeTemperature::setDeltat) .def_property("density", &ComputeTemperature::getDensity, &ComputeTemperature::setDensity) .def_property("L", &ComputeTemperature::getL, &ComputeTemperature::setL) .def_property("capacity", &ComputeTemperature::getCapacity, &ComputeTemperature::setCapacity) .def_property("conductivity", &ComputeTemperature::getConductivity, &ComputeTemperature::setConductivity) ; }