KratosMultiphysics
KRATOS Multiphysics (Kratos) is a framework for building parallel, multi-disciplinary simulation software, aiming at modularity, extensibility, and high performance. Kratos is written in C++, and counts with an extensive Python interface.
containers_interface.h
Go to the documentation of this file.
1 // | / |
2 // ' / __| _` | __| _ \ __|
3 // . \ | ( | | ( |\__ `
4 // _|\_\_| \__,_|\__|\___/ ____/
5 // Multi-Physics
6 //
7 // License: BSD License
8 // Kratos default license: kratos/license.txt
9 //
10 // Main authors: Riccardo Rossi
11 //
12 
13 #pragma once
14 
15 // System includes
16 #include <pybind11/pybind11.h>
17 #include <pybind11/stl.h>
18 
19 // External includes
20 
21 // Project includes
22 #include "includes/define_python.h"
23 
24 namespace Kratos::Python
25 {
26 namespace py = pybind11;
27 
28 template< class TContainerType >
30 {
31  public:
32 
34  virtual ~ContainerInterface(){};
35 
36  void CreateInterface(pybind11::module& m, std::string ContainerName)
37  {
38  py::class_<TContainerType, typename TContainerType::Pointer >(m,ContainerName.c_str())
39  .def(py::init<>())
40  .def("__len__", [](TContainerType& self){return self.size();} )
41  .def("__setitem__", [](TContainerType& self, unsigned int i, typename TContainerType::value_type& value){self[i] = value;} )
42  .def("__getitem__", [](TContainerType& self, unsigned int i){return self(i);} )
43  .def("__iter__", [](TContainerType& self){return py::make_iterator(self.begin(), self.end());}, py::keep_alive<0,1>())
44  .def("append", [](TContainerType& self, typename TContainerType::value_type& value){self.push_back(value);} )
45  ;
46  }
47 };
48 
49 template< class TContainerType >
51 {
52  public:
53 
55  virtual ~MapInterface(){};
56 
57  void CreateInterface(pybind11::module& m, std::string ContainerName)
58  {
59  py::class_<TContainerType, typename TContainerType::Pointer >(m,ContainerName.c_str())
60  .def(py::init<>())
61  .def("__len__", [](TContainerType& self){return self.size();} )
62  .def("__contains__", [](TContainerType& self, const typename TContainerType::value_type& value){return (self.find(value.Id()) != self.end());} )
63  .def("__contains__", [](TContainerType& self, unsigned int i){return (self.find(i) != self.end());} )
64  .def("__setitem__", [](TContainerType& self, unsigned int i, typename TContainerType::value_type& value){self[i] = value;} )
65  .def("__getitem__", [](TContainerType& self, unsigned int i){return self(i);} )
66  .def("__iter__", [](TContainerType& self){return py::make_iterator(self.begin(), self.end());}, py::keep_alive<0,1>())
67  .def("clear", [](TContainerType& self){self.clear();} )
68  ;
69  }
70 };
71 
72 template< class TContainerType >
74 {
75  public:
76 
79 
80  void CreateInterface(pybind11::module& m, std::string ContainerName)
81  {
82  py::class_<TContainerType, typename TContainerType::Pointer >(m,ContainerName.c_str())
83  .def(py::init<>())
84  .def("__len__", [](TContainerType& self){return self.size();} )
85  .def("__setitem__", [](TContainerType& self, unsigned int i, typename TContainerType::value_type& value){self[i] = value;} )
86  .def("__getitem__", [](TContainerType& self, unsigned int i){return self(i);} )
87  .def("__iter__", [](TContainerType& self){return py::make_iterator(self.begin(), self.end());}, py::keep_alive<0,1>())
88  .def("append", [](TContainerType& self, typename TContainerType::value_type::Pointer pValue) {self.push_back(pValue); })
89  ;
90  }
91 };
92 
93 template< class TContainerType >
95 {
96 public:
97 
100 
101  void CreateInterface(pybind11::module& m, std::string ContainerName)
102  {
103  py::class_<TContainerType, typename TContainerType::Pointer >(m,ContainerName.c_str())
104  .def(py::init<>())
105  .def("__len__", [](TContainerType& self){return self.size();} )
106  .def("__contains__", [](TContainerType& self, const typename TContainerType::value_type& value){return (self.find(value.Id()) != self.end());} )
107  .def("__contains__", [](TContainerType& self, unsigned int i){return (self.find(i) != self.end());} )
108  .def("__setitem__", [](TContainerType& self, typename TContainerType::value_type& value){self[value.Id()] = value;} )
109  .def("__setitem__", [](TContainerType& self, typename TContainerType::pointer& pvalue){self(pvalue->Id()) = pvalue;} )
110  .def("__getitem__", [](TContainerType& self, unsigned int i){return self(i);} )
111  .def("__iter__", [](TContainerType& self){return py::make_iterator(self.ptr_begin(), self.ptr_end());}, py::keep_alive<0,1>()) //TODO: decide if here we should use ptr_iterators or iterators
112  .def("append", [](TContainerType& self, typename TContainerType::pointer value){self.push_back(value);} )
113  .def("clear", [](TContainerType& self){self.clear();} )
114  ;
115  }
116 };
117 
118 } // namespace Kratos::Python.
119 
120 
Definition: containers_interface.h:30
virtual ~ContainerInterface()
Definition: containers_interface.h:34
void CreateInterface(pybind11::module &m, std::string ContainerName)
Definition: containers_interface.h:36
ContainerInterface()
Definition: containers_interface.h:33
Definition: containers_interface.h:51
virtual ~MapInterface()
Definition: containers_interface.h:55
MapInterface()
Definition: containers_interface.h:54
void CreateInterface(pybind11::module &m, std::string ContainerName)
Definition: containers_interface.h:57
Definition: containers_interface.h:74
virtual ~PointerVectorPythonInterface()
Definition: containers_interface.h:78
PointerVectorPythonInterface()
Definition: containers_interface.h:77
void CreateInterface(pybind11::module &m, std::string ContainerName)
Definition: containers_interface.h:80
Definition: containers_interface.h:95
virtual ~PointerVectorSetPythonInterface()
Definition: containers_interface.h:99
PointerVectorSetPythonInterface()
Definition: containers_interface.h:98
void CreateInterface(pybind11::module &m, std::string ContainerName)
Definition: containers_interface.h:101
end
Definition: DEM_benchmarks.py:180
static void module(const array_1d< double, 3 > &Vector, double &distance)
Definition: GeometryFunctions.h:127
Definition: add_distributed_sparse_matrices_to_python.cpp:28
int m
Definition: run_marine_rain_substepping.py:8
integer i
Definition: TensorModule.f:17