Skip to content

Commit 1082b9f

Browse files
committed
wip
1 parent 0d11d0a commit 1082b9f

93 files changed

Lines changed: 10159 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
add_subdirectory(src)
2+
add_subdirectory(mgis)
3+
if(MGIS_HAVE_TFEL)
4+
add_subdirectory(tests)
5+
endif(MGIS_HAVE_TFEL)
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*!
2+
* \file bindings/python/include/MGIS/Python/NumPySupport.hxx
3+
* \brief
4+
* \author Thomas Helfer
5+
* \date 07/11/2018
6+
* \copyright (C) Copyright Thomas Helfer 2018.
7+
* Use, modification and distribution are subject
8+
* to one of the following licences:
9+
* - GNU Lesser General Public License (LGPL), Version 3.0. (See accompanying
10+
* file LGPL-3.0.txt)
11+
* - CECILL-C, Version 1.0 (See accompanying files
12+
* CeCILL-C_V1-en.txt and CeCILL-C_V1-fr.txt).
13+
*/
14+
15+
#ifndef LIB_MGIS_PYTHON_NUMPYSUPPORT_HXX
16+
#define LIB_MGIS_PYTHON_NUMPYSUPPORT_HXX
17+
18+
#include <span>
19+
#include <vector>
20+
#include <variant>
21+
#include <boost/python/object.hpp>
22+
#include "MGIS/Config.hxx"
23+
24+
namespace mgis::python {
25+
26+
//! \brief initialize NumPy
27+
void initializeNumPy();
28+
29+
/*!
30+
* \brief create a 1D-ndarray object from a vector.
31+
* The ndarray does not own the data, the lifetime of which is handled by
32+
* the vector.
33+
* \param[in] v: values
34+
*/
35+
boost::python::object wrapInNumPyArray(std::span<double>&);
36+
/*!
37+
* \brief create a 1D-ndarray object from a vector.
38+
* The ndarray does not own the data, the lifetime of which is handled by
39+
* the vector.
40+
* \param[in] v: values
41+
*/
42+
boost::python::object wrapInNumPyArray(
43+
std::variant<std::span<double>, std::vector<double>>&);
44+
/*!
45+
* \brief create a 1D-ndarray object from a vector.
46+
* The ndarray does not own the data, the lifetime of which is handled by
47+
* the vector.
48+
* \param[in] v: vector holding the values
49+
*/
50+
boost::python::object wrapInNumPyArray(std::vector<double>&);
51+
/*!
52+
* \brief create a 2D-ndarray object from a vector.
53+
* The ndarray does not own the data, the lifetime of which is handled by
54+
* the vector.
55+
* \param[in] v: values
56+
* \param[in] nc: number of columns
57+
*/
58+
boost::python::object wrapInNumPyArray(std::span<double>&,
59+
const mgis::size_type);
60+
/*!
61+
* \brief create a 2D-ndarray object from a vector.
62+
* The ndarray does not own the data, the lifetime of which is handled by
63+
* the vector.
64+
* \param[in] v: values
65+
* \param[in] nc: number of columns
66+
*/
67+
boost::python::object wrapInNumPyArray(
68+
std::variant<std::span<double>, std::vector<double>>&,
69+
const mgis::size_type);
70+
/*!
71+
* \brief create a 2D-ndarray object from a vector.
72+
* The ndarray does not own the data, the lifetime of which is handled by
73+
* the vector.
74+
* \param[in] v: vector holding the values
75+
* \param[in] nc: number of columns
76+
*/
77+
boost::python::object wrapInNumPyArray(std::vector<double>&,
78+
const mgis::size_type);
79+
/*!
80+
* \brief create a 3D-ndarray object from a vector.
81+
* The ndarray does not own the data, the lifetime of which is handled by
82+
* the vector.
83+
* \param[in] v: values
84+
* \param[in] nl: number of line
85+
* \param[in] nc: number of columns
86+
*/
87+
boost::python::object wrapInNumPyArray(std::span<double>&,
88+
const mgis::size_type,
89+
const mgis::size_type);
90+
/*!
91+
* \brief create a 3D-ndarray object from a vector.
92+
* The ndarray does not own the data, the lifetime of which is handled by
93+
* the vector.
94+
* \param[in] v: values
95+
* \param[in] nl: number of line
96+
* \param[in] nc: number of columns
97+
*/
98+
boost::python::object wrapInNumPyArray(
99+
std::variant<std::span<double>, std::vector<double>>&,
100+
const mgis::size_type,
101+
const mgis::size_type);
102+
/*!
103+
* \brief create a 3D-ndarray object from a vector.
104+
* The ndarray does not own the data, the lifetime of which is handled by
105+
* the vector.
106+
* \param[in] v: vector holding the values
107+
* \param[in] nl: number of line
108+
* \param[in] nc: number of columns
109+
*/
110+
boost::python::object wrapInNumPyArray(std::vector<double>&,
111+
const mgis::size_type,
112+
const mgis::size_type);
113+
114+
std::span<mgis::real> mgis_convert_to_span(const boost::python::object&);
115+
116+
} // end of namespace mgis::python
117+
118+
#endif /* LIB_MGIS_PYTHON_NUMPYSUPPORT_HXX */
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*!
2+
* \file bindings/python/include/MGIS/Python/PairConverter.hxx
3+
* \brief
4+
* \author Thomas Helfer
5+
* \brief 02/03/2020
6+
*/
7+
8+
#ifndef LIB_MGIS_PYTHON_PAIRCONVERTER_HXX
9+
#define LIB_MGIS_PYTHON_PAIRCONVERTER_HXX
10+
11+
#include <utility>
12+
#include <boost/python.hpp>
13+
#include <boost/python/stl_iterator.hpp>
14+
15+
namespace mgis {
16+
17+
namespace python {
18+
19+
/*!
20+
* convert pair converter to python tuple
21+
*/
22+
template <typename T1, typename T2>
23+
struct pair_to_python_tuple {
24+
static PyObject* convert(const std::pair<T1, T2>& pair) {
25+
using namespace boost::python;
26+
return incref(make_tuple(pair.first, pair.second).ptr());
27+
}
28+
};
29+
30+
template <typename T1, typename T2>
31+
struct pair_from_python_tuple {
32+
pair_from_python_tuple() {
33+
using boost::python::type_id;
34+
using namespace boost::python::converter;
35+
registry::push_back(&convertible, &construct,
36+
type_id<std::pair<T1, T2>>());
37+
}
38+
39+
static void* convertible(PyObject* ptr) {
40+
using namespace boost::python;
41+
if (!PyTuple_Check(ptr)) {
42+
return nullptr;
43+
}
44+
if (PyTuple_Size(ptr) != 2) {
45+
return nullptr;
46+
}
47+
handle<> h(borrowed(ptr));
48+
tuple l(h);
49+
stl_input_iterator<object> p(l);
50+
extract<T1> e1(*p);
51+
if (!e1.check()) {
52+
return nullptr;
53+
}
54+
++p;
55+
extract<T2> e2(*p);
56+
if (!e2.check()) {
57+
return nullptr;
58+
}
59+
return ptr;
60+
}
61+
62+
static void construct(
63+
PyObject* ptr,
64+
boost::python::converter::rvalue_from_python_stage1_data* data) {
65+
using namespace boost::python;
66+
using namespace boost::python::converter;
67+
using py_storage = rvalue_from_python_storage<std::pair<T1, T2>>;
68+
using std::pair;
69+
assert(PyTuple_Check(ptr));
70+
handle<> h(borrowed(ptr));
71+
tuple l(h);
72+
stl_input_iterator<object> p(l);
73+
// Grab pointer to memory into which to construct the new pair<T1,T2>
74+
void* storage = reinterpret_cast<py_storage*>(data)->storage.bytes;
75+
// in-place construct the new pair<T1,T2> using the character data
76+
// extraced from the python object
77+
new (storage) pair<T1, T2>();
78+
auto& v = *(static_cast<pair<T1, T2>*>(storage));
79+
extract<T1> e1(*p);
80+
assert(e1.check());
81+
++p;
82+
extract<T2> e2(*p);
83+
assert(e2.check());
84+
v.first = e1;
85+
v.second = e2;
86+
// Stash the memory chunk pointer for later use by boost.python
87+
data->convertible = storage;
88+
}
89+
};
90+
91+
template <typename T1, typename T2>
92+
static void initializePairConverter() {
93+
using namespace boost::python;
94+
using std::pair;
95+
// register the to-python converter
96+
to_python_converter<pair<T1, T2>, pair_to_python_tuple<T1, T2>>();
97+
// register the from-python converter
98+
pair_from_python_tuple<T1, T2>();
99+
}
100+
101+
} // end of namespace python
102+
103+
} // end of namespace mgis
104+
105+
#endif /* LIB_MGIS_PYTHON_PAIRCONVERTER_HXX */
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/*!
2+
* \file VectorConverter.hxx
3+
* \brief
4+
* \author Thomas Helfer
5+
* \date 06/11/2018
6+
* \copyright (C) Copyright Thomas Helfer 2018.
7+
* Use, modification and distribution are subject
8+
* to one of the following licences:
9+
* - GNU Lesser General Public License (LGPL), Version 3.0. (See accompanying
10+
* file LGPL-3.0.txt)
11+
* - CECILL-C, Version 1.0 (See accompanying files
12+
* CeCILL-C_V1-en.txt and CeCILL-C_V1-fr.txt).
13+
*/
14+
15+
#ifndef LIB_MGIS_PYTHON_VECTORCONVERTER_HXX
16+
#define LIB_MGIS_PYTHON_VECTORCONVERTER_HXX
17+
18+
#include <iostream>
19+
#include <vector>
20+
21+
#ifdef _MSC_VER
22+
#ifdef and
23+
#undef and
24+
#endif
25+
#ifdef xor
26+
#undef xor
27+
#endif
28+
#ifdef or
29+
#undef or
30+
#endif
31+
#endif /* _MSC_VER */
32+
33+
#include <boost/python.hpp>
34+
#include <boost/python/stl_iterator.hpp>
35+
36+
namespace mgis {
37+
38+
namespace python {
39+
40+
template <typename T>
41+
boost::python::list convert_vector_to_list(const std::vector<T>& v) {
42+
boost::python::list l;
43+
for (const auto& e : v) {
44+
l.append(e);
45+
}
46+
return l;
47+
} // end of convert
48+
49+
/*!
50+
* convert vector to python list
51+
*/
52+
template <typename T>
53+
struct vector_to_python_list {
54+
static PyObject* convert(const T& o) {
55+
boost::python::list l;
56+
for (const auto& v : o) {
57+
l.append(v);
58+
}
59+
return boost::python::incref(l.ptr());
60+
}
61+
};
62+
63+
template <typename T>
64+
struct vector_from_python_list {
65+
vector_from_python_list() {
66+
using boost::python::type_id;
67+
using namespace boost::python::converter;
68+
registry::push_back(&convertible, &construct, type_id<T>());
69+
}
70+
71+
static void* convertible(PyObject* ptr) {
72+
using namespace boost::python;
73+
if (!PyList_Check(ptr)) {
74+
return nullptr;
75+
}
76+
handle<> h(borrowed(ptr));
77+
list l(h);
78+
stl_input_iterator<object> p(l);
79+
stl_input_iterator<object> pe;
80+
while (p != pe) {
81+
extract<typename T::value_type> e(*p);
82+
if (!e.check()) {
83+
return nullptr;
84+
}
85+
++p;
86+
}
87+
return ptr;
88+
}
89+
90+
static void construct(
91+
PyObject* ptr,
92+
boost::python::converter::rvalue_from_python_stage1_data* data) {
93+
using namespace boost::python;
94+
using namespace boost::python::converter;
95+
typedef rvalue_from_python_storage<T> py_storage;
96+
assert(PyList_Check(ptr));
97+
handle<> h(borrowed(ptr));
98+
list l(h);
99+
stl_input_iterator<object> p(l);
100+
stl_input_iterator<object> pe;
101+
// Grab pointer to memory into which to construct the new T
102+
void* storage = reinterpret_cast<py_storage*>(data)->storage.bytes;
103+
// in-place construct the new T using the character data
104+
// extraced from the python object
105+
new (storage) T();
106+
T& v = *(static_cast<T*>(storage));
107+
while (p != pe) {
108+
extract<typename T::value_type> e(*p);
109+
assert(e.check());
110+
v.push_back(e());
111+
++p;
112+
}
113+
// Stash the memory chunk pointer for later use by boost.python
114+
data->convertible = storage;
115+
}
116+
};
117+
118+
template <typename T>
119+
static void initializeVectorConverter() {
120+
using namespace boost::python;
121+
// register the to-python converter
122+
to_python_converter<T, vector_to_python_list<T>>();
123+
// register the from-python converter
124+
vector_from_python_list<T>();
125+
}
126+
127+
} // end of namespace python
128+
129+
} // end of namespace mgis
130+
131+
#endif /* LIB_MGIS_PYTHON_VECTORCONVERTER_HXX */
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
add_subdirectory(fenics)
2+
3+
install(FILES __init__.py
4+
DESTINATION ${MGIS_PYTHON_MODULES_INSTALL_DIRECTORY}
5+
COMPONENT python_bindings)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from ._mgis import *
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function(mgis_fenics_python_script file)
2+
install(FILES ${file}
3+
DESTINATION ${MGIS_PYTHON_MODULES_INSTALL_DIRECTORY}/fenics
4+
COMPONENT python_bindings)
5+
endfunction(mgis_fenics_python_script)
6+
7+
mgis_fenics_python_script(__init__.py)
8+
mgis_fenics_python_script(gradient_flux.py)
9+
mgis_fenics_python_script(utils.py)
10+
mgis_fenics_python_script(nonlinear_material.py)
11+
mgis_fenics_python_script(nonlinear_problem.py)

0 commit comments

Comments
 (0)