OpenRadioss 2025.1.11
OpenRadioss project
Loading...
Searching...
No Matches
cpp_python_funct.h
Go to the documentation of this file.
1//Copyright> OpenRadioss
2//Copyright> Copyright (C) 1986-2025 Altair Engineering Inc.
3//Copyright>
4//Copyright> This program is free software: you can redistribute it and/or modify
5//Copyright> it under the terms of the GNU Affero General Public License as published by
6//Copyright> the Free Software Foundation, either version 3 of the License, or
7//Copyright> (at your option) any later version.
8//Copyright>
9//Copyright> This program is distributed in the hope that it will be useful,
10//Copyright> but WITHOUT ANY WARRANTY; without even the implied warranty of
11//Copyright> MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12//Copyright> GNU Affero General Public License for more details.
13//Copyright>
14//Copyright> You should have received a copy of the GNU Affero General Public License
15//Copyright> along with this program. If not, see <https://www.gnu.org/licenses/>.
16//Copyright>
17//Copyright>
18//Copyright> Commercial Alternative: Altair Radioss Software
19//Copyright>
20//Copyright> As an alternative to this open-source version, Altair also offers Altair Radioss
21//Copyright> software under a commercial license. Contact Altair to discuss further if the
22//Copyright> commercial version may interest you: https://www.altair.com/radioss/.
23
24// the maximum length of a line of code of python function
25#include <iostream>
26#include <string>
27#include <regex>
28#include <array>
29#include <set>
30#include <array>
31#include <set>
32#include <utility>
33#include <vector>
34#include <limits>
35#include <cmath>
36
37
38#include <stdio.h>
39
40#if defined(_WIN64)
41#include <BaseTsd.h>
42typedef SSIZE_T My_ssize_t;
43#else
44#include <stddef.h>
45typedef ssize_t My_ssize_t;
46#endif
47
48#define max_line_length 500
49// the maximum number of lines of python function
50#define max_num_lines 1000
51#define max_code_length max_line_length *max_num_lines
52#define max_variable_length 100
53
54constexpr int sensor_result_size = 2;
55
56typedef void *PyObject;
57// Corrected typedefs for Python C API function pointers
58typedef PyObject *(*T_PyDict_GetItemString)(PyObject *, const char *);
59typedef int (*T_PyCallable_Check)(PyObject *);
60typedef PyObject *(*T_PyTuple_New)(My_ssize_t); // Use Py_ssize_t for size
61typedef PyObject *(*T_PyFloat_FromDouble)(double);
62typedef PyObject *(*T_PyObject_CallObject)(PyObject *, PyObject *);
63typedef void (*T_Py_Initialize)();
64typedef void (*T_Py_Finalize)();
65typedef PyObject *(*T_PyImport_AddModule)(const char *);
66typedef PyObject *(*T_PyModule_GetDict)(PyObject *);
67typedef int (*T_PyRun_SimpleString)(const char *);
68typedef int (*T_PyTuple_SetItem)(PyObject *, My_ssize_t, PyObject *); // Use Py_ssize_t for index
70typedef void (*T_Py_DecRef)(PyObject *);
71typedef void (*T_Py_IncRef)(PyObject *);
72typedef double (*T_PyFloat_AsDouble)(PyObject *);
73typedef int (*T_PyDict_SetItemString)(PyObject *, const char *, PyObject *);
74typedef void (*T_PyErr_Fetch)(PyObject **, PyObject **, PyObject **);
75typedef void (*T_PyErr_Display)(PyObject *, PyObject *, PyObject *);
76typedef PyObject *(*T_PyErr_Occurred)();
77typedef int (*T_Py_IsInitialized)();
78typedef PyObject *(*T_PyObject_Str)(PyObject *);
79typedef const char *(*T_PyUnicode_AsUTF8)(PyObject *);
80typedef PyObject *(*T_PyDict_New)();
81typedef PyObject *(*T_PyList_New)(My_ssize_t); // Use Py_ssize_t for size
82typedef void (*T_PyErr_Clear)();
83typedef PyObject *(*T_PyLong_FromLong)(long int);
84typedef PyObject *(*T_PyLong_FromVoidPtr)(void *);
85typedef PyObject *(*T_PyUnicode_FromString)(const char *); // Added this line to declare the function pointer
86
87
88// Python library handle
89#ifdef _WIN32
90template <typename T>
91void load_function(HMODULE handle, const std::string &func_name, T &func_ptr, bool &python_initialized)
92{
93 func_ptr = reinterpret_cast<T>(GetProcAddress(handle, func_name.c_str()));
94 if (func_ptr == nullptr)
95 {
96 // std::cout << "Could not load " << func_name << ": " << GetLastError() << std::endl;
97 python_initialized = false;
98 }
99}
100
101#else
102
103template <typename T>
104void load_function(void *h, const std::string &func_name, T &func_ptr, bool &python_initialized)
105{
106 func_ptr = reinterpret_cast<T>(dlsym(h, func_name.c_str()));
107 if (func_ptr == nullptr)
108 {
109 const char *dlsym_error = dlerror();
110 std::cout << "Could not load " << func_name << ": " << dlsym_error << std::endl;
111 python_initialized = false;
112 }
113}
114#endif
115
142T_PyLong_FromVoidPtr MyLong_FromVoidPtr; // Added this line to declare the function pointer
143T_PyUnicode_FromString MyUnicode_FromString; // Added this line to declare the function pointer
144
145constexpr std::array<const char*, 89> ELEMENT_KEYWORDS = {
146"ALPHA",
147"AMS",
148"BFRAC",
149"BULK",
150"COLOR",
151"DAM1",
152"DAM2",
153"DAM3",
154"DAMA",
155"DAMG",
156"DAMINI",
157"DENS",
158"DOMAIN",
159"DT",
160"EINT",
161"EINTM",
162"EINTV",
163"ENER",
164"ENTH",
165"ENTHM",
166"ENTHV",
167"EPSD",
168"EPSP",
169"FAIL",
170"FAILURE",
171"FILL",
172"FLAY",
173"FLDF",
174"FLDZ",
175"GROUP",
176"HC_DSSE_F",
177"HC_DSSE_Z",
178"HOURGLASS",
179"K",
180"M151DENS",
181"M151ENER",
182"M151PRES",
183"M151VFRAC",
184"MACH",
185"MASS",
186"MOMX",
187"MOMXY",
188"MOMXZ",
189"MOMY",
190"MOMYZ",
191"MOMZ",
192"NL_EPSD",
193"NL_EPSP",
194"NXTF",
195"OFF",
196"P",
197"PHI",
198"SCHLIEREN",
199"SIGEQ",
200"SIGX",
201"SIGXY",
202"SIGY",
203"SIGYZ",
204"SIGZ",
205"SIGZX",
206"SSP",
207"TDEL",
208"TDET",
209"TEMP",
210"THICK",
211"THIN",
212"TILLOTSON",
213"TSAIWU",
214"TVIS",
215"VDAM1",
216"VDAM2",
217"VDAM3",
218"VELX",
219"VELXY",
220"VELXZ",
221"VELY",
222"VELYZ",
223"VELZ",
224"VFRAC1",
225"VFRAC2",
226"VFRAC3",
227"VFRAC4",
228"VOLU",
229"VONM",
230"VORT",
231"VORTX",
232"VORTY",
233"VORTZ",
234"WPLA"
235};
236
237using KeywordPair = std::pair<int, const char*>;
238// Comparator for the set to order pairs by keyword and id
240 bool operator() (const KeywordPair& lhs, const KeywordPair& rhs) const {
241 if (lhs.second == rhs.second) {
242 return lhs.first < rhs.first;
243 }
244 return std::string(lhs.second) < std::string(rhs.second);
245 }
246};
247using KeywordPairs = std::set<std::pair<int, const char*>, ComparePairs>;
248
249// Get a pair of keyword, id from an index: is inefficient but called only at initialization
250KeywordPair get_keyword_pair(const KeywordPairs& keywordPairs, size_t n) {
251 if (n >= keywordPairs.size()) {
252 std::cout<<"ERROR: Out of range access in elemental value for Python function."<<std::endl;
253 std::cout<<"Index: "<<n<<" Size: "<<keywordPairs.size()<<std::endl;
254 }
255 auto it = std::next(keywordPairs.begin(), n);
256 return *it;
257}
258
259
260// Utility function to add a key-value pair to a dictionary with proper reference counting
261//static int add_to_dict(PyObject* dict, PyObject* key, PyObject* value) {
262// int result = MyDict_SetItem(dict, key, value);
263// My_DecRef(key); // PyDict_SetItem doesn't steal references
264// My_DecRef(value); // so we need to decrease them ourselves
265// return result;
266//}
267
268// Utility function to format a key with the array name
269//static PyObject* create_key(const char* array_name, const char* suffix) {
270// return MyUnicode_FromFormat("%s_%s", array_name, suffix);
271//}
272
273// Utility function to check if a Python object is a valid dictionary
274//static int check_context(PyObject* context) {
275// if (!context || !MyDict_Check(context)) {
276// std::cerr << "Error: Invalid context pointer" << std::endl;
277// return -1;
278// }
279// return 0;
280//}
281
282
283
void * handle
bool python_initialized
PyObject *(* T_PyUnicode_FromString)(const char *)
int(* T_PyRun_SimpleString)(const char *)
T_PyModule_GetDict MyModule_GetDict
PyObject *(* T_PyFloat_FromDouble)(double)
T_PyList_New MyList_New
PyObject *(* T_PyLong_FromLong)(long int)
T_PyErr_Display MyErr_Display
double(* T_PyFloat_AsDouble)(PyObject *)
PyObject *(* T_PyErr_Occurred)()
T_Py_DecRef My_DecRef
int(* T_PyTuple_SetItem)(PyObject *, My_ssize_t, PyObject *)
void(* T_Py_IncRef)(PyObject *)
T_PyTuple_SetItem MyTuple_SetItem
T_PyCallable_Check MyCallable_Check
ssize_t My_ssize_t
T_PyLong_FromLong MyLong_FromLong
T_PyDict_New MyDict_New
PyObject *(* T_PyDict_GetItemString)(PyObject *, const char *)
void * PyObject
T_PyLong_FromVoidPtr MyLong_FromVoidPtr
PyObject *(* T_PyModule_GetDict)(PyObject *)
void(* T_PyErr_Display)(PyObject *, PyObject *, PyObject *)
void(* T_Py_Initialize)()
int(* T_Py_IsInitialized)()
int(* T_PyCallable_Check)(PyObject *)
PyObject *(* T_PyList_New)(My_ssize_t)
constexpr std::array< const char *, 89 > ELEMENT_KEYWORDS
T_PyUnicode_FromString MyUnicode_FromString
T_PyUnicode_AsUTF8 MyUnicode_AsUTF8
T_PyDict_GetItemString MyDict_GetItemString
void(* T_PyErr_Fetch)(PyObject **, PyObject **, PyObject **)
PyObject *(* T_PyTuple_New)(My_ssize_t)
KeywordPair get_keyword_pair(const KeywordPairs &keywordPairs, size_t n)
T_PyErr_Clear MyErr_Clear
void(* T_Py_Finalize)()
std::pair< int, const char * > KeywordPair
int(* T_PyDict_SetItemString)(PyObject *, const char *, PyObject *)
T_PyErr_Occurred MyErr_Occurred
T_Py_IsInitialized My_IsInitialized
int(* T_PyList_SetItem)(PyObject *, My_ssize_t, PyObject *)
T_Py_Initialize My_Initialize
void(* T_Py_DecRef)(PyObject *)
PyObject *(* T_PyImport_AddModule)(const char *)
T_PyErr_Fetch MyErr_Fetch
T_Py_Finalize My_Finalize
T_Py_IncRef My_IncRef
PyObject *(* T_PyDict_New)()
T_PyFloat_AsDouble MyFloat_AsDouble
T_PyImport_AddModule MyImport_AddModule
void(* T_PyErr_Clear)()
constexpr int sensor_result_size
std::set< std::pair< int, const char * >, ComparePairs > KeywordPairs
PyObject *(* T_PyObject_Str)(PyObject *)
void load_function(void *h, const std::string &func_name, T &func_ptr, bool &python_initialized)
PyObject *(* T_PyLong_FromVoidPtr)(void *)
T_PyDict_SetItemString MyDict_SetItemString
T_PyTuple_New MyTuple_New
const char *(* T_PyUnicode_AsUTF8)(PyObject *)
T_PyRun_SimpleString MyRun_SimpleString
T_PyList_SetItem MyList_SetItem
PyObject *(* T_PyObject_CallObject)(PyObject *, PyObject *)
T_PyObject_CallObject MyObject_CallObject
T_PyObject_Str MyObject_Str
T_PyFloat_FromDouble MyFloat_FromDouble
n
bool operator()(const KeywordPair &lhs, const KeywordPair &rhs) const