-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpyVUMAT.cpp
More file actions
415 lines (339 loc) · 12.2 KB
/
pyVUMAT.cpp
File metadata and controls
415 lines (339 loc) · 12.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/* Overwrite function name depending on requirements of the FEA code */
//#define PYVUMAT_FUNC_NAME VUMAT20
#include <stdio.h>
#include <stdlib.h>
#include <fenv.h>
#define PY_SSIZE_T_CLEAN
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
#include <Python.h>
#include <numpy/arrayobject.h>
/* Determine if compilation is in Abaqus and set function name */
#if defined(ABQ_LINUX) || defined(ABQ_WIN86_64) || defined(ABQ_WIN86_32)
#define IN_ABAQUS
#include <omi_for_c.h>
#elif defined(PYVUMAT_FUNC_NAME)
#define FOR_NAME(lower_case_name,upper_case_name) PYVUMAT_FUNC_NAME
#else
#define FOR_NAME(lower_case_name,upper_case_name) lower_case_name
#endif /* In Abaqus */
/* Define flag for memory layout of NumPy arrays */
#if defined(C_ORDERING)
#define MEM_FLAG NPY_ARRAY_CARRAY_RO
#else
#define MEM_FLAG NPY_ARRAY_FARRAY_RO
#endif
/* Create a 1D numpy array */
PyObject *
create1dNpArray(npy_intp dim, const double *data) {
npy_intp dims[1] = {dim};
return PyArray_New(&PyArray_Type,
1,dims,
NPY_DOUBLE, NULL,
(void *)data,
0, MEM_FLAG,
NULL);
}
/* Create a 2D numpy array */
PyObject *
create2dNpArray(npy_intp dim1, npy_intp dim2, const double *data) {
npy_intp dims[2];
#if defined(C_ORDERING)
dims[0] = dim2;
dims[1] = dim1;
#else
dims[0] = dim1;
dims[1] = dim2;
#endif
return PyArray_New(&PyArray_Type,
2,dims,
NPY_DOUBLE, NULL,
(void *)data,
0, MEM_FLAG,
NULL);
}
/*
* VUMAT function called by FEA codes.
*/
extern "C"
void
FOR_NAME(vumat,VUMAT)(
/* Read only */
const int *blockInfo, const int *ndirPtr, const int *nshrPtr,
const int *nstatevPtr, const int *nfieldvPtr, const int *npropsPtr,
const int *lanneal, const double *stepTime, const double *totalTime,
const double *dt, const char *cmname, const double *coordMp,
const double *charLength, const double *props, const double *density,
const double *strainInc, const double *relSpinInc, const double *tempOld,
const double *stretchOld, const double *defgradOld, const double *fieldOld,
const double *stressOld, const double *stateOld, const double *enerInternOld,
const double *enerInelasOld, const double *tempNew, const double *stretchNew,
const double *defgradNew, const double *fieldNew,
/* Write only */
double *stressNew, double *stateNew, double *enerInternNew,
double *enerInelasNew )
{
static FILE *_logFile;
static int _isInitialized = 0;
static PyObject *_evaluateMethod;
static PyThreadState *_threadState;
/*
* Perform initialization on the first step
*/
if (!_isInitialized) {
/* Create logging file */
_logFile = fopen("pyvumat.log","a");
/* Get the INI configuration file */
char *configFileName = getenv("PYVUMAT_CONF_FILE");
/* Write warning that configure file was not found */
if ( configFileName == NULL ) {
fprintf(_logFile, "Warning: Could not find the INI conf file.\n");
fprintf(_logFile,
"Make sure PYVUMAT_CONF_FILE environment variable is set.");
}
/* Initializes the Python interpreter */
Py_Initialize();
/*
* _import_array() causes floating point exception on some systems.
* Temporarily disable floating point exceptions to get through
* NumPy initialization
*/
fenv_t orig_feenv;
feholdexcept(&orig_feenv);
_import_array();
fesetenv(&orig_feenv);
/* Load the driver module */
PyObject *pModuleName = PyUnicode_FromString("pyvumat.driver");
PyObject *pModule = PyImport_Import(pModuleName);
if (pModule == NULL) PyErr_Print();
Py_XDECREF(pModuleName);
/* Load the module's dict */
PyObject *pDict = PyModule_GetDict(pModule);
if (pDict == NULL) PyErr_Print();
Py_XDECREF(pModule);
/* Build the name of a callable class */
PyObject *pClass = PyDict_GetItemString(pDict, "Driver");
if (pClass == NULL || !PyCallable_Check(pClass)) PyErr_Print();
Py_XDECREF(pDict);
/* Creates an instance of the driver class */
PyObject *pClassArg = NULL;
if (configFileName != NULL) {
pClassArg = Py_BuildValue("(s)",
configFileName);
}
PyObject *pyDriver = PyObject_CallObject(pClass, pClassArg);
if (pyDriver == NULL) PyErr_Print();
Py_XDECREF(pClass);
Py_XDECREF(pClassArg);
/* Create the evaluate() method object */
_evaluateMethod = PyObject_GetAttrString(pyDriver,"evaluate");
if (_evaluateMethod == NULL) PyErr_Print();
Py_XDECREF(pyDriver);
_threadState = PyEval_SaveThread();
_isInitialized = 1;
} /* End initialization */
/* Get lock to python interpreter */
PyGILState_STATE gState;
gState = PyGILState_Ensure();
/* Get array sizes */
const int nblock = blockInfo[0];
const int ndir = ndirPtr[0];
const int nshr = nshrPtr[0];
const int nprops = npropsPtr[0];
const int nfieldv = nfieldvPtr[0];
const int nstatev = nstatevPtr[0];
/* Create an empty tuple to pass as the argument */
PyObject *emptyArg = Py_BuildValue("()");
/* Create the keyword dict containing all inputs */
PyObject *keywords = PyDict_New();
/*
* Add scalar arguments
*/
PyObject *lanneal_py = Py_BuildValue("i",*lanneal);
PyDict_SetItemString(keywords,"lanneal",lanneal_py);
PyObject *stepTime_py = Py_BuildValue("d",*stepTime);
PyDict_SetItemString(keywords,"stepTime",stepTime_py);
PyObject *totalTime_py = Py_BuildValue("d",*totalTime);
PyDict_SetItemString(keywords,"totalTime",totalTime_py);
PyObject *dt_py = Py_BuildValue("d",*dt);
PyDict_SetItemString(keywords,"dt",dt_py);
PyObject *nblock_py = Py_BuildValue("i",nblock);
PyDict_SetItemString(keywords,"nblock",nblock_py);
PyObject *ndir_py = Py_BuildValue("i",ndir);
PyDict_SetItemString(keywords,"ndir",ndir_py);
PyObject *nshr_py = Py_BuildValue("i",nshr);
PyDict_SetItemString(keywords,"nshr",nshr_py);
PyObject *nprops_py = Py_BuildValue("i",nprops);
PyDict_SetItemString(keywords,"nprops",nprops_py);
PyObject *nfieldv_py = Py_BuildValue("i",nfieldv);
PyDict_SetItemString(keywords,"nfieldv",nfieldv_py);
PyObject *nstatev_py = Py_BuildValue("i",nstatev);
PyDict_SetItemString(keywords,"nstatev",nstatev_py);
/*
* Convert the arrays to python arrays
*/
/* CoordMp */
/* Fixme: Is dimension 3, ndir, or something else? */
PyObject *coordMpArray = create2dNpArray(nblock,ndir,coordMp);
PyDict_SetItemString(keywords,"coordMp",coordMpArray);
/* CharLength */
PyObject *charLengthArray = create1dNpArray(nblock,charLength);
PyDict_SetItemString(keywords,"charLength",charLengthArray);
/* Props */
PyObject *propsArray = create1dNpArray(nprops,props);
PyDict_SetItemString(keywords,"props",propsArray);
/* Density */
PyObject *densityArray = create1dNpArray(nblock,density);
PyDict_SetItemString(keywords,"density",densityArray);
/* StrainInc */
PyObject *strainIncArray = create2dNpArray(nblock,ndir+nshr,
strainInc);
PyDict_SetItemString(keywords,"strainInc",strainIncArray);
/* RelSpinInc */
PyObject *relSpinIncArray = create2dNpArray(nblock,nshr,
relSpinInc);
PyDict_SetItemString(keywords,"relSpinInc",relSpinIncArray);
/* TempOld */
PyObject *tempOldArray = create1dNpArray(nblock,tempOld);
PyDict_SetItemString(keywords,"tempOld",tempOldArray);
/* StretchOld */
PyObject *stretchOldArray= create2dNpArray(nblock,ndir+nshr,
stretchOld);
PyDict_SetItemString(keywords,"stretchOld",stretchOldArray);
/* DefGradOld */
PyObject *defgradOldArray = create2dNpArray(nblock,
ndir+nshr+nshr,
defgradOld);
PyDict_SetItemString(keywords,"defgradOld",defgradOldArray);
/* FieldOld */
PyObject *fieldOldArray = create2dNpArray(nblock,nfieldv,
fieldOld);
PyDict_SetItemString(keywords,"fieldOld",fieldOldArray);
/* StressOld */
PyObject *stressOldArray = create2dNpArray(nblock,ndir+nshr,
stressOld);
PyDict_SetItemString(keywords,"stressOld",stressOldArray);
/* StateOld */
PyObject *stateOldArray = create2dNpArray(nblock,nstatev,
stateOld);
PyDict_SetItemString(keywords,"stateOld",stateOldArray);
/* enerInternOld */
PyObject *enerInternOldArray = create1dNpArray(nblock,enerInternOld);
PyDict_SetItemString(keywords,"enerInternOld",enerInternOldArray);
/* enerInelasOld */
PyObject *enerInelasOldArray = create1dNpArray(nblock,enerInelasOld);
PyDict_SetItemString(keywords,"enerInelasOld",enerInelasOldArray);
/* tempNew */
PyObject *tempNewArray = create1dNpArray(nblock,tempNew);
PyDict_SetItemString(keywords,"tempNew",tempNewArray);
/* StretchNew */
PyObject *stretchNewArray = create2dNpArray(nblock,ndir+nshr,
stretchNew);
PyDict_SetItemString(keywords,"stretchNew",stretchNewArray);
/* DefGradNew */
PyObject *defgradNewArray = create2dNpArray(nblock,
ndir+nshr+nshr,
defgradNew);
PyDict_SetItemString(keywords,"defgradNew",defgradNewArray);
/* FieldNew */
PyObject *fieldNewArray = create2dNpArray(nblock,nfieldv,
fieldNew);
PyDict_SetItemString(keywords,"fieldNew",fieldNewArray);
/* Evaluate the pyVUMAT model for the inputs */
PyObject *retVal = PyObject_Call(_evaluateMethod,emptyArg,keywords);
if (retVal == NULL) PyErr_Print();
/*
* Extract the results
*/
PyArrayObject *stressNewArray;
PyArrayObject *stateNewArray;
PyArrayObject *enerInternNewArray;
PyArrayObject *enerInelasNewArray;
int parseSuccess = PyArg_ParseTuple(retVal,"OOOO",
&stressNewArray,
&stateNewArray,
&enerInternNewArray,
&enerInelasNewArray);
if (!parseSuccess) PyErr_Print();
/*
* Store the results to return to FEA code
*/
/* StressNew */
unsigned int numOutDims = PyArray_NDIM(stressNewArray);
npy_intp *outDims = PyArray_DIMS(stressNewArray);
assert(numOutDims == 2);
#if defined(C_ORDERING)
assert(outDims[0] == ndir+nshr);
assert(outDims[1] == nblock);
#else
assert(outDims[0] == nblock);
assert(outDims[1] == ndir+nshr);
#endif
double *tmpStressNew = (double *)PyArray_DATA(stressNewArray);
memcpy(stressNew,tmpStressNew,nblock*(ndir+nshr)*sizeof(double));
/* StateNew */
numOutDims = PyArray_NDIM(stateNewArray);
outDims = PyArray_DIMS(stateNewArray);
assert(numOutDims == 2);
#if defined(C_ORDERING)
assert(outDims[0] == nstatev);
assert(outDims[1] == nblock);
#else
assert(outDims[0] == nblock);
assert(outDims[1] == nstatev);
#endif
double *tmpStateNew = (double *)PyArray_DATA(stateNewArray);
memcpy(stateNew,tmpStateNew,nblock*nstatev*sizeof(double));
/* EnerInternNew */
numOutDims = PyArray_NDIM(enerInternNewArray);
outDims = PyArray_DIMS(enerInternNewArray);
assert(numOutDims == 1);
assert(outDims[0] == nblock);
double *tmpEnerInternNew = (double *)PyArray_DATA(enerInternNewArray);
memcpy(enerInternNew,tmpEnerInternNew,nblock*sizeof(double));
/* EnerInelasNew */
numOutDims = PyArray_NDIM(enerInelasNewArray);
outDims = PyArray_DIMS(enerInelasNewArray);
assert(numOutDims == 1);
assert(outDims[0] == nblock);
double *tmpEnerInelasNew = (double *)PyArray_DATA(enerInelasNewArray);
memcpy(enerInelasNew,tmpEnerInelasNew,nblock*sizeof(double));
Py_XDECREF(lanneal_py);
Py_XDECREF(stepTime_py);
Py_XDECREF(totalTime_py);
Py_XDECREF(dt_py);
Py_XDECREF(nblock_py);
Py_XDECREF(ndir_py);
Py_XDECREF(nshr_py);
Py_XDECREF(nprops_py);
Py_XDECREF(nfieldv_py);
Py_XDECREF(nstatev_py);
Py_XDECREF(coordMpArray);
Py_XDECREF(charLengthArray);
Py_XDECREF(propsArray);
Py_XDECREF(densityArray);
Py_XDECREF(strainIncArray);
Py_XDECREF(relSpinIncArray);
Py_XDECREF(tempOldArray);
Py_XDECREF(stretchOldArray);
Py_XDECREF(defgradOldArray);
Py_XDECREF(fieldOldArray);
Py_XDECREF(stressOldArray);
Py_XDECREF(stateOldArray);
Py_XDECREF(enerInternOldArray);
Py_XDECREF(enerInelasOldArray);
Py_XDECREF(tempNewArray);
Py_XDECREF(stretchNewArray);
Py_XDECREF(defgradNewArray);
Py_XDECREF(fieldNewArray);
Py_XDECREF(retVal);
Py_XDECREF(emptyArg);
Py_XDECREF(keywords);
/* Release GIL and return */
PyGILState_Release(gState);
/*
* Uncomment this line if you are printing to the
* log file and need to see the output in real time
*/
/* fflush(_logFile); */
return;
}