v0.16.3
Loading...
Searching...
No Matches
UmatInterface.cpp
Go to the documentation of this file.
1/** \file UmatInterface.cpp
2 * \brief UmatInterface
3 *
4 * UmatInterface
5 *
6 */
7
8#include <MoFEM.hpp>
9using namespace MoFEM;
10#include "UmatInterface.hpp"
11
12namespace UmatOps {
13
14inline int tensorIndex(const int ii, const int jj) { return 3 * ii + jj; }
15
18 mat.resize(3, 3, false);
19 mat.clear();
21}
22
25 mat.resize(9, 9, false);
26 mat.clear();
28}
29
30}; // namespace UmatOps
31// MatUmat can do the conversions depending of the strain measure used.
32
33using namespace UmatOps;
34
35template <int MODEL_TYPE>
37 : nstatv(nstatv), nprops(nprops), props(nprops, 0.0), statev(nstatv, 0.0) {}
38
40 if (handle) {
41 dlclose(handle);
42 handle = nullptr;
43 umat = nullptr;
44 }
45}
46
47template <int MODEL_TYPE>
49MoFEM::UmatInterface<MODEL_TYPE>::initialise(const std::string &library_path,
50 const std::string &material_name) {
52
53 if (handle) {
54 dlclose(handle);
55 handle = nullptr;
56 umat = nullptr;
57 }
58
59 std::memset(filename, 0, sizeof(filename));
60 std::strncpy(filename, library_path.c_str(), sizeof(filename) - 1);
61
62 std::memset(cmname, ' ', sizeof(cmname));
63 const auto material_name_size = material_name.size() < sizeof(cmname)
64 ? material_name.size()
65 : sizeof(cmname);
66 std::memcpy(cmname, material_name.data(), material_name_size);
67
68 handle = dlopen(filename, RTLD_NOW | RTLD_LOCAL);
69 if (!handle) {
70 SETERRQ(PETSC_COMM_SELF, MOFEM_OPERATION_UNSUCCESSFUL,
71 "Failed to open UMAT library %s: %s", filename, dlerror());
72 }
73
74 umat = (umat_f)dlsym(handle, "umat_");
75 if (!umat) {
76 SETERRQ(PETSC_COMM_SELF, MOFEM_OPERATION_UNSUCCESSFUL,
77 "Failed to resolve symbol umat_ in %s: %s", filename, dlerror());
78 }
79
81}
82
83template <int MODEL_TYPE>
86
87 if (!umat) {
88 SETERRQ(PETSC_COMM_SELF, MOFEM_OPERATION_UNSUCCESSFUL,
89 "UMAT function pointer is not initialised");
90 }
91
92 umat(stress.data(), statev.data(), ddsdde.data(), &sse, &spd, &scd, &rpl,
93 ddsddt, drplde, &drpldt, stran.data(), dstran.data(), time, &dtime,
94 &temp, &dtemp, predef.data(), dpred.data(), cmname, &ndi, &nshr, &ntens,
95 &nstatv, props.data(), &nprops, coords, drot.data(), &pnewdt, &celent,
96 dfgrd0.data(), dfgrd1.data(), &noel, &npt, &layer, &kspt, &kstep, &kinc,
97 sizeof(cmname));
98
100}
101
102template <int MODEL_TYPE>
106
107 dfgrd0.fill(0);
108 for (int dd = 0; dd != 3; ++dd)
109 dfgrd0[dd + 3 * dd] = 1.0;
110
112}
113
114template <int MODEL_TYPE>
118
119 drot.fill(0);
120 for (int dd = 0; dd != 3; ++dd)
121 drot[dd + 3 * dd] = 1.0;
122
124}
125
126template <int MODEL_TYPE>
128 const MatrixDouble &grad, bool use_deformation_gradient) {
130
131 dfgrd1.fill(0);
132 for (int dd = 0; dd != 3; ++dd)
133 dfgrd1[dd + 3 * dd] = 1.0;
134
135 for (int row = 0; row != DIM; ++row) {
136 for (int col = 0; col != DIM; ++col) {
137 const double grad_value = grad(row, col);
138 dfgrd1[row + 3 * col] = use_deformation_gradient
139 ? grad_value
140 : grad_value + (row == col ? 1.0 : 0.0);
141 }
142 }
143
145}
146
147template <int MODEL_TYPE>
150 double time_increment) {
152
153 // Abaqus passes both TIME entries at the start of the increment. TIME(1)
154 // is step time and TIME(2) is total time; the TS clock is total time here.
155 time[0] = current_time - time_increment;
156 time[1] = time[0];
157 dtime = time_increment;
158
160}
161
162template <int MODEL_TYPE>
165 int increment_number) {
167
168 kstep = step_number;
169 kinc = increment_number;
170
172}
173
174template <int MODEL_TYPE>
178
179 auto get_small_strain_voigt = [](const std::array<double, 9> &dfgrd,
180 auto &strain) {
181 std::array<double, 9> grad = dfgrd;
182 for (int dd = 0; dd != DIM; ++dd)
183 grad[dd + 3 * dd] -= 1.0;
184
185 if constexpr (MODEL_TYPE == MatOps::MODEL_3D) {
186 strain[0] = grad[0];
187 strain[1] = grad[4];
188 strain[2] = grad[8];
189 strain[3] = grad[1] + grad[3];
190 strain[4] = grad[2] + grad[6];
191 strain[5] = grad[5] + grad[7];
192 } else if constexpr (MODEL_TYPE == MatOps::MODEL_2D_PLANE_STRAIN) {
193 strain[0] = grad[0];
194 strain[1] = grad[4];
195 strain[2] = 0.0;
196 strain[3] = grad[1] + grad[3];
197 } else if constexpr (MODEL_TYPE == MatOps::MODEL_2D_PLANE_STRESS) {
198 strain[0] = grad[0];
199 strain[1] = grad[4];
200 strain[2] = grad[1] + grad[3];
201 } else if constexpr (MODEL_TYPE == MatOps::MODEL_AXISYMMETRIC) {
202 strain[0] = grad[0];
203 strain[1] = grad[4];
204 strain[2] = grad[8];
205 strain[3] = grad[1] + grad[3];
206 } else {
207 SETERRQ(PETSC_COMM_SELF, MOFEM_NOT_IMPLEMENTED,
208 "UMAT strain increment is not implemented for this model type");
209 }
210 };
211
212 std::array<double, NTENS> reference_strain{};
213 std::array<double, NTENS> current_strain{};
214 get_small_strain_voigt(dfgrd0, reference_strain);
215 get_small_strain_voigt(dfgrd1, current_strain);
216
217 for (int ii = 0; ii != NTENS; ++ii) {
218 stran[ii] = reference_strain[ii];
219 dstran[ii] = current_strain[ii] - reference_strain[ii];
220 }
221
223}
224
225template <int MODEL_TYPE>
227 int n_statev, const std::vector<double> &state_var_values) {
229
231}
232
233template <int MODEL_TYPE>
235 MatrixDouble &F) const {
238 for (int row = 0; row != DIM; ++row)
239 for (int col = 0; col != DIM; ++col)
240 F(row, col) = dfgrd1[row + 3 * col];
242}
243
244template <int MODEL_TYPE>
246 MatrixDouble &sigma) const {
248 CHKERR resizeTensor2(sigma);
249 if constexpr (MODEL_TYPE == MatOps::MODEL_3D) {
250 sigma(0, 0) = stress[0];
251 sigma(1, 1) = stress[1];
252 sigma(2, 2) = stress[2];
253 sigma(0, 1) = sigma(1, 0) = stress[3];
254 sigma(0, 2) = sigma(2, 0) = stress[4];
255 sigma(1, 2) = sigma(2, 1) = stress[5];
256 } else if constexpr (MODEL_TYPE == MatOps::MODEL_2D_PLANE_STRAIN ||
257 MODEL_TYPE == MatOps::MODEL_AXISYMMETRIC) {
258 sigma(0, 0) = stress[0];
259 sigma(1, 1) = stress[1];
260 sigma(2, 2) = stress[2];
261 sigma(0, 1) = sigma(1, 0) = stress[3];
262 } else if constexpr (MODEL_TYPE == MatOps::MODEL_2D_PLANE_STRESS) {
263 sigma(0, 0) = stress[0];
264 sigma(1, 1) = stress[1];
265 sigma(0, 1) = sigma(1, 0) = stress[2];
266 } else {
267 SETERRQ(PETSC_COMM_SELF, MOFEM_NOT_IMPLEMENTED,
268 "UMAT stress conversion is not implemented for this model type");
269 }
271}
272
273template <int MODEL_TYPE>
275 MatrixDouble &cauchy_tangent) const {
277 CHKERR resizeTensor4(cauchy_tangent);
278 if constexpr (MODEL_TYPE == MatOps::MODEL_3D) {
279 static const std::array<std::pair<int, int>, 6> pairs = {
280 std::pair<int, int>{0, 0}, {1, 1}, {2, 2}, {0, 1}, {0, 2}, {1, 2}};
281 for (int row = 0; row != 6; ++row) {
282 const auto [i, j] = pairs[row];
283 for (int col = 0; col != 6; ++col) {
284 const auto [k, l] = pairs[col];
285 const double value = ddsdde[row + 6 * col];
286 cauchy_tangent(tensorIndex(i, j), tensorIndex(k, l)) = value;
287 cauchy_tangent(tensorIndex(j, i), tensorIndex(k, l)) = value;
288 cauchy_tangent(tensorIndex(i, j), tensorIndex(l, k)) = value;
289 cauchy_tangent(tensorIndex(j, i), tensorIndex(l, k)) = value;
290 }
291 }
292 } else if constexpr (MODEL_TYPE == MatOps::MODEL_2D_PLANE_STRAIN ||
293 MODEL_TYPE == MatOps::MODEL_AXISYMMETRIC) {
294 static const std::array<std::pair<int, int>, 4> pairs = {
295 std::pair<int, int>{0, 0}, {1, 1}, {2, 2}, {0, 1}};
296 for (int row = 0; row != 4; ++row) {
297 const auto [i, j] = pairs[row];
298 for (int col = 0; col != 4; ++col) {
299 const auto [k, l] = pairs[col];
300 const double value = ddsdde[row + 4 * col];
301 cauchy_tangent(tensorIndex(i, j), tensorIndex(k, l)) = value;
302 cauchy_tangent(tensorIndex(j, i), tensorIndex(k, l)) = value;
303 cauchy_tangent(tensorIndex(i, j), tensorIndex(l, k)) = value;
304 cauchy_tangent(tensorIndex(j, i), tensorIndex(l, k)) = value;
305 }
306 }
307 } else if constexpr (MODEL_TYPE == MatOps::MODEL_2D_PLANE_STRESS) {
308 static const std::array<std::pair<int, int>, 3> pairs = {
309 std::pair<int, int>{0, 0}, {1, 1}, {0, 1}};
310 for (int row = 0; row != 3; ++row) {
311 const auto [i, j] = pairs[row];
312 for (int col = 0; col != 3; ++col) {
313 const auto [k, l] = pairs[col];
314 const double value = ddsdde[row + 3 * col];
315 cauchy_tangent(tensorIndex(i, j), tensorIndex(k, l)) = value;
316 cauchy_tangent(tensorIndex(j, i), tensorIndex(k, l)) = value;
317 cauchy_tangent(tensorIndex(i, j), tensorIndex(l, k)) = value;
318 cauchy_tangent(tensorIndex(j, i), tensorIndex(l, k)) = value;
319 }
320 }
321 } else {
322 SETERRQ(PETSC_COMM_SELF, MOFEM_NOT_IMPLEMENTED,
323 "UMAT tangent conversion is not implemented for this model type");
324 }
326}
327
EntityHandle handle
std::vector< FieldBitNumber > row
Definition Schur.cpp:2488
std::vector< FieldBitNumber > col
Definition Schur.cpp:2488
UmatInterface.
void(*)(double *stress, double *statev, double *ddsdde, double *sse, double *spd, double *scd, double *rpl, double *ddsddt, double *drplde, double *drpldt, double *stran, double *dstran, double *time, double *dtime, double *temp, double *dtemp, double *predef, double *dpred, char *cmname, int *ndi, int *nshr, int *ntens, int *nstatv, double *props, int *nprops, double *coords, double *drot, double *pnewdt, double *celent, double *dfgrd0, double *dfgrd1, int *noel, int *npt, int *layer, int *kspt, int *kstep, int *kinc, std::size_t cmname_len) umat_f
#define MoFEMFunctionReturnHot(a)
Last executable line of each PETSc function used for error handling. Replaces return()
#define MoFEMFunctionBegin
First executable line of each MoFEM function, used for error handling. Final line of MoFEM functions ...
@ MOFEM_OPERATION_UNSUCCESSFUL
Definition definitions.h:34
@ MOFEM_NOT_IMPLEMENTED
Definition definitions.h:32
#define MoFEMFunctionReturn(a)
Last executable line of each PETSc function used for error handling. Replaces return()
#define CHKERR
Inline error check.
#define MoFEMFunctionBeginHot
First executable line of each MoFEM function, used for error handling. Final line of MoFEM functions ...
@ F
FTensor::Index< 'i', SPACE_DIM > i
FTensor::Index< 'l', 3 > l
FTensor::Index< 'j', 3 > j
FTensor::Index< 'k', 3 > k
@ MODEL_AXISYMMETRIC
Definition MatOps.hpp:237
@ MODEL_3D
Definition MatOps.hpp:234
@ MODEL_2D_PLANE_STRESS
Definition MatOps.hpp:236
@ MODEL_2D_PLANE_STRAIN
Definition MatOps.hpp:235
PetscErrorCode MoFEMErrorCode
MoFEM/PETSc error code.
implementation of Data Operators for Forces and Sources
Definition Common.hpp:10
MoFEMErrorCode resizeTensor2(MatrixDouble &mat)
MoFEMErrorCode resizeTensor4(MatrixDouble &mat)
int tensorIndex(const int ii, const int jj)
void temp(int x, int y=10)
Definition simple.cpp:4
MoFEMErrorCode setStepData(int step_number, int increment_number)
MoFEMErrorCode setTimeData(double current_time, double time_increment)
MoFEMErrorCode getCauchyTangentTensor(MatrixDouble &cauchy_tangent) const
MoFEMErrorCode setIdentityRotationIncrement()
MoFEMErrorCode setStrainIncrementFromDeformationGradient()
MoFEMErrorCode initialise(const std::string &library_path=DEFAULT_LIBRARY_PATH, const std::string &material_name="UMAT")
MoFEMErrorCode callUmat()
UmatInterface(int nstatv=0, int nprops=2)
MoFEMErrorCode storeStateVar(int n_state_var, const std::vector< double > &state_var_values)
MoFEMErrorCode setDeformationGradient(const MatrixDouble &grad, bool use_deformation_gradient)
MoFEMErrorCode setIdentityDeformationGradient()
MoFEMErrorCode getCauchyStressTensor(MatrixDouble &sigma) const
MoFEMErrorCode getDeformationGradient(MatrixDouble &F) const
subroutine umat(stress, statev, ddsdde, sse, spd, scd, rpl, ddsddt, drplde, drpldt, stran, dstran, time, dtime, temp, dtemp, predef, dpred, cmname, ndi, nshr, ntens, nstatv, props, nprops, coords, drot, pnewdt, celent, dfgrd0, dfgrd1, noel, npt, layer, kspt, kstep, kinc)