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.
add_mapper_to_python.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: Philipp Bucher, Jordi Cotela
11 //
12 // See Master-Thesis P.Bucher
13 // "Development and Implementation of a Parallel
14 // Framework for Non-Matching Grid Mapping"
15 
16 #pragma once
17 
18 // System includes
19 
20 // External includes
21 
22 // Project includes
23 #include "includes/define_python.h"
24 #include "mappers/mapper.h"
26 #include "mappers/mapper_flags.h"
27 
28 namespace Kratos::Python
29 {
30 namespace {
31 
32 // Wrapper functions for taking a default argument for the flags
33 template<class TSparseSpace, class TDenseSpace>
34 inline void UpdateInterfaceWithoutArgs(Mapper<TSparseSpace, TDenseSpace>& dummy)
35 {
36  Kratos::Flags dummy_flags = Kratos::Flags();
37  double dummy_search_radius = -1.0f;
38  dummy.UpdateInterface(dummy_flags, dummy_search_radius);
39 }
40 
41 template<class TSparseSpace, class TDenseSpace>
42 inline void UpdateInterfaceWithOptions(Mapper<TSparseSpace, TDenseSpace>& dummy, Kratos::Flags options)
43 {
44  double dummy_search_radius = -1.0f;
45  dummy.UpdateInterface(options, dummy_search_radius);
46 }
47 
48 template<class TSparseSpace, class TDenseSpace>
49 inline void UpdateInterfaceWithSearchRadius(Mapper<TSparseSpace, TDenseSpace>& dummy, double search_radius)
50 {
51  Kratos::Flags dummy_flags = Kratos::Flags();
52  dummy.UpdateInterface(dummy_flags, search_radius);
53 }
54 
55 
56 template<class TSparseSpace, class TDenseSpace>
57 inline void MapWithoutOptionsScalar(Mapper<TSparseSpace, TDenseSpace>& dummy,
58  const Variable<double>& origin_variable,
59  const Variable<double>& destination_variable)
60 {
61  Kratos::Flags dummy_flags = Kratos::Flags();
62  dummy.Map(origin_variable, destination_variable, dummy_flags);
63 }
64 
65 template<class TSparseSpace, class TDenseSpace>
66 inline void MapWithoutOptionsVector(Mapper<TSparseSpace, TDenseSpace>& dummy,
67  const Variable< array_1d<double, 3> >& origin_variable,
68  const Variable< array_1d<double, 3> >& destination_variable)
69 {
70  Kratos::Flags dummy_flags = Kratos::Flags();
71  dummy.Map(origin_variable, destination_variable, dummy_flags);
72 }
73 
74 template<class TSparseSpace, class TDenseSpace>
75 inline void InverseMapWithoutOptionsScalar(Mapper<TSparseSpace, TDenseSpace>& dummy,
76  const Variable<double>& origin_variable,
77  const Variable<double>& destination_variable)
78 {
79  Kratos::Flags dummy_flags = Kratos::Flags();
80  dummy.InverseMap(origin_variable, destination_variable, dummy_flags);
81 }
82 
83 template<class TSparseSpace, class TDenseSpace>
84 inline void InverseMapWithoutOptionsVector(Mapper<TSparseSpace, TDenseSpace>& dummy,
85  const Variable< array_1d<double, 3> >& origin_variable,
86  const Variable< array_1d<double, 3> >& destination_variable)
87 {
88  Kratos::Flags dummy_flags = Kratos::Flags();
89  dummy.InverseMap(origin_variable, destination_variable, dummy_flags);
90 }
91 
92 
93 template<class TSparseSpace, class TDenseSpace>
94 inline void MapWithOptionsScalar(Mapper<TSparseSpace, TDenseSpace>& dummy,
95  const Variable<double>& origin_variable,
96  const Variable<double>& destination_variable,
97  Kratos::Flags MappingOptions)
98 {
99  dummy.Map(origin_variable, destination_variable, MappingOptions);
100 }
101 
102 template<class TSparseSpace, class TDenseSpace>
103 inline void MapWithOptionsVector(Mapper<TSparseSpace, TDenseSpace>& dummy,
104  const Variable< array_1d<double, 3> >& origin_variable,
105  const Variable< array_1d<double, 3> >& destination_variable,
106  Kratos::Flags MappingOptions)
107 {
108  dummy.Map(origin_variable, destination_variable, MappingOptions);
109 }
110 
111 template<class TSparseSpace, class TDenseSpace>
112 inline void InverseMapWithOptionsScalar(Mapper<TSparseSpace, TDenseSpace>& dummy,
113  const Variable<double>& origin_variable,
114  const Variable<double>& destination_variable,
115  Kratos::Flags MappingOptions)
116 {
117  dummy.InverseMap(origin_variable, destination_variable, MappingOptions);
118 }
119 
120 template<class TSparseSpace, class TDenseSpace>
121 inline void InverseMapWithOptionsVector(Mapper<TSparseSpace, TDenseSpace>& dummy,
122  const Variable< array_1d<double, 3> >& origin_variable,
123  const Variable< array_1d<double, 3> >& destination_variable,
124  Kratos::Flags MappingOptions)
125 {
126  dummy.InverseMap(origin_variable, destination_variable, MappingOptions);
127 }
128 
129 
130 template<class TSparseSpace, class TDenseSpace>
131 void ExposeMapperToPython(pybind11::module& m)
132 {
133  namespace py = pybind11;
134  // Exposing the base class of the Mappers to Python, but without constructor
135  const std::string mapper_name = TSparseSpace::IsDistributed() ? "MPIMapper" : "Mapper";
136  typedef Mapper<TSparseSpace, TDenseSpace> MapperType;
137  const auto mapper
138  = py::class_< MapperType, typename MapperType::Pointer >(m, mapper_name.c_str())
139  .def("UpdateInterface", UpdateInterfaceWithoutArgs<TSparseSpace, TDenseSpace>)
140  .def("UpdateInterface", UpdateInterfaceWithOptions<TSparseSpace, TDenseSpace>)
141  .def("UpdateInterface", UpdateInterfaceWithSearchRadius<TSparseSpace, TDenseSpace>)
142  .def("UpdateInterface", &MapperType::UpdateInterface) // with options & search-radius
143 
144  .def("Map", MapWithoutOptionsScalar<TSparseSpace, TDenseSpace>)
145  .def("Map", MapWithoutOptionsVector<TSparseSpace, TDenseSpace>)
146  .def("Map", MapWithOptionsScalar<TSparseSpace, TDenseSpace>)
147  .def("Map", MapWithOptionsVector<TSparseSpace, TDenseSpace>)
148 
149  .def("InverseMap", InverseMapWithoutOptionsScalar<TSparseSpace, TDenseSpace>)
150  .def("InverseMap", InverseMapWithoutOptionsVector<TSparseSpace, TDenseSpace>)
151  .def("InverseMap", InverseMapWithOptionsScalar<TSparseSpace, TDenseSpace>)
152  .def("InverseMap", InverseMapWithOptionsVector<TSparseSpace, TDenseSpace>)
153 
154  .def("GetMappingMatrix", &MapperType::GetMappingMatrix, py::return_value_policy::reference_internal)
155  .def("GetInterfaceModelPartOrigin", &MapperType::GetInterfaceModelPartOrigin, py::return_value_policy::reference_internal)
156  .def("GetInterfaceModelPartDestination", &MapperType::GetInterfaceModelPartDestination, py::return_value_policy::reference_internal)
157 
158  .def("AreMeshesConforming", &MapperType::AreMeshesConforming)
159 
160  .def("__str__", PrintObject<MapperType>)
161  ;
162 
163  // Adding the flags that can be used for mapping
164  mapper.attr("SWAP_SIGN") = MapperFlags::SWAP_SIGN;
165  mapper.attr("ADD_VALUES") = MapperFlags::ADD_VALUES;
166  mapper.attr("REMESHED") = MapperFlags::REMESHED;
167  mapper.attr("USE_TRANSPOSE") = MapperFlags::USE_TRANSPOSE;
168  mapper.attr("TO_NON_HISTORICAL") = MapperFlags::TO_NON_HISTORICAL;
169  mapper.attr("FROM_NON_HISTORICAL") = MapperFlags::FROM_NON_HISTORICAL;
170 }
171 
172 } // anonymous namespace
173 
174 template<class TSparseSpace, class TDenseSpace>
176 {
177  ExposeMapperToPython<TSparseSpace, TDenseSpace>(m);
178 
179  // Exposing the MapperFactory
180  const std::string mapper_factory_name = TSparseSpace::IsDistributed() ? "MPIMapperFactory" : "MapperFactory";
181  typedef MapperFactory<TSparseSpace, TDenseSpace> MapperFactoryType;
182  pybind11::class_<MapperFactoryType, typename MapperFactoryType::Pointer>(m, mapper_factory_name.c_str())
183  .def_static("CreateMapper", &MapperFactoryType::CreateMapper)
184  .def_static("HasMapper", &MapperFactoryType::HasMapper)
185  .def_static("GetRegisteredMapperNames", &MapperFactoryType::GetRegisteredMapperNames)
186  ;
187 }
188 
190 
191 } // namespace Kratos::Python.
Definition: flags.h:58
Python Interface of the MappingApplication.
Definition: mapper_factory.h:45
static void module(const array_1d< double, 3 > &Vector, double &distance)
Definition: GeometryFunctions.h:127
Definition: add_distributed_sparse_matrices_to_python.cpp:28
void AddMappingToPython(pybind11::module &m)
Definition: add_mapper_to_python.h:175
void AddMapperToPython(pybind11::module &m)
Definition: add_mapper_to_python.cpp:22
def CreateMapper(model_part_origin, model_part_destination, mapper_settings)
Definition: python_mapper_factory.py:34
int m
Definition: run_marine_rain_substepping.py:8
dummy
Definition: script.py:194