12#ifndef ARCANE_STD_INTERNAL_KDI_H
13#define ARCANE_STD_INTERNAL_KDI_H
17#define Py_LIMITED_API 0x03100000
18#define PY_SSIZE_T_CLEAN
19#define NPY_NO_DEPRECATED_API NPY_1_23_API_VERSION
22#include <numpy/arrayobject.h>
31 ARCANE_FATAL("KDI ASSERT"); \
33#define KTRACE(trace, msg) \
35 std::cout << msg << std::endl; \
45 const PyObject* m_pInstanceChunk;
50 KDIChunk(
const PyObject* _pInstanceChunk,
bool _trace =
false)
51 : m_pInstanceChunk(_pInstanceChunk)
54 KTRACE(m_trace,
"KDIChunk::KDIChunk IN/OUT");
59 KTRACE(m_trace,
"KDIChunk::~KDIChunk IN");
61 Py_DECREF(m_pInstanceChunk);
62 KTRACE(m_trace,
"KDIChunk::~KDIChunk OUT");
67 PyObject* _simple_call(
const std::string _method, PyObject* _pArgs)
69 KTRACE(m_trace,
"KDIChunk:_simple_call IN");
70 PyObject* pValue = PyUnicode_FromString(
"pykdi");
72 PyObject* pModule = PyImport_Import(pValue);
74 PyObject* pClass = PyObject_GetAttrString(pModule,
"KDIAgreementStepPartChunk");
76 PyObject* pMethod = PyObject_GetAttrString(pClass, _method.c_str());
78 KASSERT(PyCallable_Check(pMethod));
80 PyObject* pResult = PyObject_CallObject(pMethod, _pArgs);
85 KTRACE(m_trace,
"KDIChunk:_simple_call OUT");
93 KTRACE(m_trace,
"KDIChunk:dump IN");
94 KASSERT(m_pInstanceChunk);
95 PyObject* pArgs = Py_BuildValue(
"(O)", m_pInstanceChunk);
96 _simple_call(
"dump", pArgs);
98 KTRACE(m_trace,
"KDIChunk:dump OUT");
101 void set(
const std::string _absname, PyArrayObject* _array)
103 KTRACE(m_trace,
"KDIChunk:set IN");
104 KASSERT(m_pInstanceChunk);
105 PyObject* pArgs = Py_BuildValue(
"(O, z, O)", m_pInstanceChunk, _absname.c_str(), _array);
106 _simple_call(
"set", pArgs);
108 KTRACE(m_trace,
"KDIChunk:set OUT");
111 void saveVTKHDF(
const std::string _absfilename)
114 KTRACE(m_trace,
"KDIChunk:saveVTKHDF (...) IN");
116 KASSERT(m_pInstanceChunk);
117 PyObject* pArgs = Py_BuildValue(
"(O, z)", m_pInstanceChunk, _absfilename.c_str());
119 _simple_call(
"saveVTKHDF", pArgs);
121 KTRACE(m_trace,
"KDIChunk:saveVTKHDF OUT");
124 void saveVTKHDFCompute(
const std::string _absfilename)
126 KTRACE(m_trace,
"KDI KDIChunk saveVTKHDFCompute " << _absfilename);
129 KTRACE(m_trace,
"KDIChunk:saveVTKHDFCompute (...) IN");
131 KASSERT(m_pInstanceChunk);
132 PyObject* pArgs = Py_BuildValue(
"(O, z)", m_pInstanceChunk, _absfilename.c_str());
134 _simple_call(
"saveVTKHDFCompute", pArgs);
136 KTRACE(m_trace,
"KDI KDIChunk saveVTKHDFCompute");
147 const PyObject* m_pInstanceBase;
152 KDIBase(
const PyObject* _pInstanceBase,
const bool _trace)
153 : m_pInstanceBase(_pInstanceBase)
156 KTRACE(m_trace,
"KDIBase::KDIBase IN/OUT");
161 KTRACE(m_trace,
"KDIBase::~KDIBase IN");
163 Py_DECREF(m_pInstanceBase);
164 KTRACE(m_trace,
"KDIBase::~KDIBase OUT");
169 PyObject* _simple_call(
const std::string _method, PyObject* _pArgs)
171 KTRACE(m_trace,
"KDIBase:_simple_call IN");
172 PyObject* pValue = PyUnicode_FromString(
"pykdi");
174 PyObject* pModule = PyImport_Import(pValue);
176 PyObject* pClass = PyObject_GetAttrString(pModule,
"KDIAgreementStepPartBase");
178 PyObject* pMethod = PyObject_GetAttrString(pClass, _method.c_str());
180 KASSERT(PyCallable_Check(pMethod));
182 PyObject* pResult = PyObject_CallObject(pMethod, _pArgs);
187 KTRACE(m_trace,
"KDIBase:_simple_call OUT");
195 KTRACE(m_trace,
"KDIBase:dump IN");
196 KASSERT(m_pInstanceBase);
197 PyObject* pArgs = Py_BuildValue(
"(O)", m_pInstanceBase);
198 _simple_call(
"dump", pArgs);
200 KTRACE(m_trace,
"KDIBase:dump OUT");
203 void update(
const std::string& _typename,
const std::string& _absname)
205 KTRACE(m_trace,
"KDIBase:update IN");
206 KASSERT(m_pInstanceBase);
207 PyObject* pArgs = Py_BuildValue(
"(O, z, z)", m_pInstanceBase, _typename.c_str(), _absname.c_str());
209 _simple_call(
"update", pArgs);
211 KTRACE(m_trace,
"KDIBase:update OUT");
214 const std::string update_fields(
const std::string& _nameParentMesh,
const std::string& _nameField)
216 KTRACE(m_trace,
"KDIBase:update_fields IN");
217 KASSERT(m_pInstanceBase);
218 PyObject* pArgs = Py_BuildValue(
"(O, z, z)", m_pInstanceBase, _nameParentMesh.c_str(), _nameField.c_str());
220 _simple_call(
"update_fields", pArgs);
222 KTRACE(m_trace,
"KDIBase:update_fields OUT");
224 return _nameParentMesh +
"/" + _nameField;
227 const std::string update_sub(
const std::string& _nameParentMesh,
const std::string& _nameSubMesh)
229 KTRACE(m_trace,
"KDIBase:update_sub IN");
230 KASSERT(m_pInstanceBase);
231 KASSERT(_nameParentMesh[0] ==
'/');
232 KASSERT(_nameSubMesh[0] ==
'/');
233 PyObject* pArgs = Py_BuildValue(
"(O, z, z)", m_pInstanceBase, _nameParentMesh.c_str(), _nameSubMesh.c_str());
235 _simple_call(
"update_sub", pArgs);
237 KTRACE(m_trace,
"KDIBase:update_sub OUT");
239 return _nameParentMesh +
"/submeshes" + _nameSubMesh;
246 KTRACE(m_trace,
"KDIBase:_chunk IN");
247 PyObject* pResult = _simple_call(
"chunk", pArgs);
250 KTRACE(m_trace,
"KDIBase:_chunk OUT");
258 KTRACE(m_trace,
"KDIBase:chunk IN");
259 KASSERT(m_pInstanceBase);
260 PyObject* pArgs = Py_BuildValue(
"(O)", m_pInstanceBase);
263 KTRACE(m_trace,
"KDIBase:chunk OUT (" << chunk <<
")");
269 KTRACE(m_trace,
"KDIBase:chunk (" << _vstep <<
") IN");
270 KASSERT(m_pInstanceBase);
271 PyObject* pArgs = Py_BuildValue(
"(O, f)", m_pInstanceBase, _vstep);
274 KTRACE(m_trace,
"KDIBase:chunk OUT (" << chunk <<
")");
278 KDIChunk* chunk(
double _vstep,
int _ipart)
280 KTRACE(m_trace,
"KDIBase:chunk (" << _vstep <<
") IN");
281 KASSERT(m_pInstanceBase);
282 PyObject* pArgs = Py_BuildValue(
"(O, f, i)", m_pInstanceBase, _vstep, _ipart);
285 KTRACE(m_trace,
"KDIBase:chunk OUT (" << chunk <<
")");
294createBase(
unsigned int _nb_parts,
bool _trace =
false)
296 KTRACE(_trace,
"KDI C++ createBase (" << _nb_parts <<
") IN");
297 PyObject* pValue = PyUnicode_FromString(
"pykdi");
299 KTRACE(_trace,
"KDI FromString pValue=" << pValue);
300 KTRACE(_trace,
"KDI Getenv PYTHONPATH=" << getenv(
"PYTHONPATH"));
301 KTRACE(_trace,
"KDI Getenv KDI_DICTIONARY_PATH=" << getenv(
"KDI_DICTIONARY_PATH"));
306 PyObject* pModule = PyImport_Import(pValue);
307 KTRACE(_trace,
"KDI Import ?");
308 KTRACE(_trace,
"KDI pModule=" << pModule);
310 KTRACE(_trace,
"KDI Import");
311 PyObject* pClass = PyObject_GetAttrString(pModule,
"KDIAgreementStepPartBase");
313 KTRACE(_trace,
"KDI GetAttrString");
314 KASSERT(PyCallable_Check(pClass));
315 KTRACE(_trace,
"KDI Check");
316 PyObject* pArgs = Py_BuildValue(
"(i)", (
int)_nb_parts);
318 KTRACE(_trace,
"KDI BuildValue");
319 PyObject* pResult = PyObject_CallObject(pClass, pArgs);
321 KTRACE(_trace,
"KDI CallObject");
327 KTRACE(_trace,
"KDI C++ createBase OUT");
335loadVTKHDF(
const std::string _absfilename,
bool _trace =
false)
337 KTRACE(_trace,
"loadBase () IN");
338 PyObject* pValue = PyUnicode_FromString(
"pykdi");
340 PyObject* pModule = PyImport_Import(pValue);
342 PyObject* pClass = PyObject_GetAttrString(pModule,
"KDIAgreementStepPartBase");
344 KASSERT(PyCallable_Check(pClass));
345 PyObject* pArgs = Py_BuildValue(
"(z)", _absfilename.c_str());
347 PyObject* pResult = PyObject_CallObject(pClass, pArgs);
354 KTRACE(_trace,
"loadVTKHDF OUT");
373static PyArrayObject* vector_to_nparray(
const std::vector<std::vector<T>>& vec,
int type_num = NPY_FLOAT64)
380 if (!vec[0].empty()) {
382 size_t nRows = vec.size();
383 size_t nCols = vec[0].size();
384 npy_intp dims[2] = { nRows, nCols };
385 PyArrayObject* vec_array = (PyArrayObject*)PyArray_SimpleNew(2, dims, type_num);
387 T* vec_array_pointer = (T*)PyArray_DATA(vec_array);
390 for (
size_t iRow = 0; iRow < vec.size(); ++iRow) {
392 if (vec[iRow].size() != nCols) {
393 Py_DECREF(vec_array);
394 throw(std::string(
"Can not convert vector<vector<T>> to np.array, since c++ matrix shape is not uniform."));
397 copy(vec[iRow].begin(), vec[iRow].end(), vec_array_pointer + iRow * nCols);
405 npy_intp dims[2] = { vec.size(), 0 };
406 return (PyArrayObject*)PyArray_ZEROS(2, dims, NPY_FLOAT64, 0);
412 npy_intp dims[2] = { 0, 0 };
413 return (PyArrayObject*)PyArray_ZEROS(2, dims, NPY_FLOAT64, 0);
433static PyArrayObject* vector_to_nparray(
const std::vector<T>& vec,
int type_num = NPY_FLOAT64,
int comp_num = 1)
439 PyArrayObject* vec_array =
nullptr;
442 npy_intp dims[]{ vec.size() };
443 vec_array = (PyArrayObject*)PyArray_SimpleNew(nd, dims, type_num);
447 npy_intp dims[]{ int(vec.size() / comp_num), comp_num };
448 vec_array = (PyArrayObject*)PyArray_SimpleNew(nd, dims, type_num);
450 T* vec_array_pointer = (T*)PyArray_DATA(vec_array);
451 KTRACE(trace, vec_array_pointer);
453 copy(vec.begin(), vec.end(), vec_array_pointer);
459 npy_intp dims[1] = { 0 };
460 return (PyArrayObject*)PyArray_ZEROS(1, dims, NPY_FLOAT64, 0);
char * copy(char *to, const char *from)
Copie from dans to.