Page MenuHomec4science

py_wrap.cc
No OneTemporary

File Metadata

Created
Wed, May 1, 20:34

py_wrap.cc

#include <iostream>
#include<memory>
#include <pybind11/pybind11.h>
#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)
py::class_<ParticlesFactoryInterface>(m, "ParticlesFactoryInterface")
.def("getInstance",
&ParticlesFactoryInterface::getInstance,
py::return_value_policy::reference)
.def("createSimulation",
&ParticlesFactoryInterface::createSimulation)
;
// Wrapping the PingPongBallsFactory (Reference Return)
py::class_<PingPongBallsFactory, ParticlesFactoryInterface>(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_<PlanetsFactory, ParticlesFactoryInterface>(m, "PlanetsFactory")
.def("getInstance", &PlanetsFactory::getInstance,
py::return_value_policy::reference)
.def("createSimulation",
&PlanetsFactory::createSimulation)
;
// Wrapping the MaterialPointsFactory (Reference Return)
py::class_<MaterialPointsFactory, ParticlesFactoryInterface>(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<const std::string&, Real, py::function>
(&MaterialPointsFactory::createSimulation<py::function>),
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_<SystemEvolution>(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_<CsvWriter>(m, "CsvWriter")
.def(py::init<const std::string&>())
.def("write", &CsvWriter::write)
;
py::class_<System>(m, "System");
py::class_<Compute, std::shared_ptr<Compute>>(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_<ComputeTemperature, Compute, std::shared_ptr<ComputeTemperature>>(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)
;
}

Event Timeline