MFEM  v4.3.0
Finite element discretization library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
invariants.hpp
Go to the documentation of this file.
1 // Copyright (c) 2010-2021, Lawrence Livermore National Security, LLC. Produced
2 // at the Lawrence Livermore National Laboratory. All Rights reserved. See files
3 // LICENSE and NOTICE for details. LLNL-CODE-806117.
4 //
5 // This file is part of the MFEM library. For more information and source code
6 // availability visit https://mfem.org.
7 //
8 // MFEM is free software; you can redistribute it and/or modify it under the
9 // terms of the BSD-3 license. We welcome feedback and contributions, see file
10 // CONTRIBUTING.md for details.
11 
12 #ifndef MFEM_INVARIANTS_HPP
13 #define MFEM_INVARIANTS_HPP
14 
15 #include "../config/config.hpp"
16 #include "../general/error.hpp"
17 #include <cmath>
18 
19 namespace mfem
20 {
21 
22 // Matrix invariants and their derivatives for 2x2 and 3x3 matrices.
23 
24 /** @brief Auxiliary class used as the default for the second template parameter
25  in the classes InvariantsEvaluator2D and InvariantsEvaluator3D. */
26 template <typename scalar_t>
27 struct ScalarOps
28 {
29  static scalar_t sign(const scalar_t &a)
30  { return (a >= scalar_t(0)) ? scalar_t(1) : scalar_t(-1); }
31 
32  static scalar_t pow(const scalar_t &x, int m, int n)
33  { return std::pow(x, scalar_t(m)/n); }
34 };
35 
36 
37 /** @brief Auxiliary class for evaluating the 2x2 matrix invariants and their
38  first and second derivatives. */
39 /**
40  The type `scalar_t` must support the standard operations:
41 
42  =, +=, -=, +, -, *, /, unary -, int*scalar_t, int/scalar_t, scalar_t/int
43 
44  The type `scalar_ops` must define the static method:
45 
46  scalar_t sign(const scalar_t &);
47 */
48 template <typename scalar_t, typename scalar_ops = ScalarOps<scalar_t> >
50 {
51 protected:
52  // Transformation Jacobian
53  const scalar_t *J;
54 
55  // Invariants: I_1 = ||J||_F^2, \bar{I}_1 = I_1/det(J), \bar{I}_2 = det(J).
56  scalar_t I1, I1b, I2b;
57 
58  // Derivatives of I1, I1b, I2, and I2b using column-major storage.
59  scalar_t dI1[4], dI1b[4], dI2[4], dI2b[4];
60 
62  const scalar_t *D; // Always points to external data or is empty
63  scalar_t *DaJ, *DJt, *DXt, *DYt;
64 
65  enum EvalMasks
66  {
67  HAVE_I1 = 1,
68  HAVE_I1b = 2,
69  HAVE_I2b = 4,
70  HAVE_dI1 = 8,
71  HAVE_dI1b = 16,
72  HAVE_dI2 = 32,
73  HAVE_dI2b = 64,
74  HAVE_DaJ = 128, // D adj(J) = D dI2b^t
75  HAVE_DJt = 256 // D J^t
76  };
77 
78  // Bitwise OR of EvalMasks
80 
81  bool dont(int have_mask) const { return !(eval_state & have_mask); }
82 
83  void Eval_I1()
84  {
86  I1 = J[0]*J[0] + J[1]*J[1] + J[2]*J[2] + J[3]*J[3];
87  }
88  void Eval_I1b()
89  {
91  I1b = Get_I1()/Get_I2b();
92  }
93  void Eval_I2b()
94  {
96  const scalar_t det = J[0]*J[3] - J[1]*J[2];
97  I2b = det;
98  }
99  void Eval_dI1()
100  {
101  eval_state |= HAVE_dI1;
102  dI1[0] = 2*J[0]; dI1[2] = 2*J[2];
103  dI1[1] = 2*J[1]; dI1[3] = 2*J[3];
104  }
105  void Eval_dI1b()
106  {
108  // I1b = I1/I2b
109  // dI1b = (1/I2b)*dI1 - (I1/I2b^2)*dI2b = (2/I2b)*[J - (I1b/2)*dI2b]
110  const scalar_t c1 = 2/Get_I2b();
111  const scalar_t c2 = Get_I1b()/2;
112  Get_dI2b();
113  dI1b[0] = c1*(J[0] - c2*dI2b[0]);
114  dI1b[1] = c1*(J[1] - c2*dI2b[1]);
115  dI1b[2] = c1*(J[2] - c2*dI2b[2]);
116  dI1b[3] = c1*(J[3] - c2*dI2b[3]);
117  }
118  void Eval_dI2()
119  {
120  eval_state |= HAVE_dI2;
121  // I2 = I2b^2
122  // dI2 = 2*I2b*dI2b = 2*det(J)*adj(J)^T
123  const scalar_t c1 = 2*Get_I2b();
124  Get_dI2b();
125  dI2[0] = c1*dI2b[0];
126  dI2[1] = c1*dI2b[1];
127  dI2[2] = c1*dI2b[2];
128  dI2[3] = c1*dI2b[3];
129  }
130  void Eval_dI2b()
131  {
133  // I2b = det(J)
134  // dI2b = adj(J)^T
135  Get_I2b();
136  dI2b[0] = J[3];
137  dI2b[1] = -J[2];
138  dI2b[2] = -J[1];
139  dI2b[3] = J[0];
140  }
141  void Eval_DaJ() // D adj(J) = D dI2b^t
142  {
143  eval_state |= HAVE_DaJ;
144  Get_dI2b();
145  Eval_DZt(dI2b, &DaJ);
146  }
147  void Eval_DJt() // D J^t
148  {
149  eval_state |= HAVE_DJt;
150  Eval_DZt(J, &DJt);
151  }
152  void Eval_DZt(const scalar_t *Z, scalar_t **DZt_ptr)
153  {
154  MFEM_ASSERT(D != NULL, "");
155  const int nd = D_height;
156  scalar_t *DZt = *DZt_ptr;
157  if (DZt == NULL) { *DZt_ptr = DZt = new scalar_t[2*alloc_height]; }
158  for (int i = 0; i < nd; i++)
159  {
160  const int i0 = i+nd*0, i1 = i+nd*1;
161  DZt[i0] = D[i0]*Z[0] + D[i1]*Z[2];
162  DZt[i1] = D[i0]*Z[1] + D[i1]*Z[3];
163  }
164  }
165 
166 public:
167  /// The Jacobian should use column-major storage.
168  InvariantsEvaluator2D(const scalar_t *Jac = NULL)
169  : J(Jac), D_height(), alloc_height(), D(), DaJ(), DJt(), DXt(), DYt(),
170  eval_state(0) { }
171 
173  {
174  delete [] DYt;
175  delete [] DXt;
176  delete [] DJt;
177  delete [] DaJ;
178  }
179 
180  /// The Jacobian should use column-major storage.
181  void SetJacobian(const scalar_t *Jac) { J = Jac; eval_state = 0; }
182 
183  /// The @a Deriv matrix is `dof x 2`, using column-major storage.
184  void SetDerivativeMatrix(int height, const scalar_t *Deriv)
185  {
186  eval_state &= ~(HAVE_DaJ | HAVE_DJt);
187  if (alloc_height < height)
188  {
189  delete [] DYt; DYt = NULL;
190  delete [] DXt; DXt = NULL;
191  delete [] DJt; DJt = NULL;
192  delete [] DaJ; DaJ = NULL;
193  alloc_height = height;
194  }
195  D_height = height;
196  D = Deriv;
197  }
198 
199  scalar_t Get_I1() { if (dont(HAVE_I1 )) { Eval_I1(); } return I1; }
200  scalar_t Get_I1b() { if (dont(HAVE_I1b)) { Eval_I1b(); } return I1b; }
201  scalar_t Get_I2() { if (dont(HAVE_I2b)) { Eval_I2b(); } return I2b*I2b; }
202  scalar_t Get_I2b() { if (dont(HAVE_I2b)) { Eval_I2b(); } return I2b; }
203 
204  const scalar_t *Get_dI1()
205  {
206  if (dont(HAVE_dI1 )) { Eval_dI1(); } return dI1;
207  }
208  const scalar_t *Get_dI1b()
209  {
210  if (dont(HAVE_dI1b)) { Eval_dI1b(); } return dI1b;
211  }
212  const scalar_t *Get_dI2()
213  {
214  if (dont(HAVE_dI2)) { Eval_dI2(); } return dI2;
215  }
216  const scalar_t *Get_dI2b()
217  {
218  if (dont(HAVE_dI2b)) { Eval_dI2b(); } return dI2b;
219  }
220 
221  // Assemble operation for tensor X with components X_jslt:
222  // A(i+nd*j,k+nd*l) += (\sum_st w D_is X_jslt D_kt)
223  // 0 <= i,k < nd, 0 <= j,l,s,t < 2
224  // where nd is the height of D, i.e. the number of DOFs in one component.
225 
226  void Assemble_ddI1(scalar_t w, scalar_t *A)
227  {
228  // ddI1_jslt = 2 I_jslt = 2 δ_jl δ_st
229  // A(i+nd*j,k+nd*l) += (\sum_st 2 w D_is δ_jl δ_st D_kt)
230  // or
231  // A(i+nd*j,k+nd*l) += (2 w) (\sum_s D_is D_ks) δ_jl
232  // A(i+nd*j,k+nd*l) += (2 w) (D D^t)_ik δ_jl
233 
234  const int nd = D_height;
235  const int ah = 2*nd;
236  const scalar_t a = 2*w;
237  for (int i = 0; i < nd; i++)
238  {
239  const int i0 = i+nd*0, i1 = i+nd*1;
240  const scalar_t aDi[2] = { a*D[i0], a*D[i1] };
241  // k == i
242  const scalar_t aDDt_ii = aDi[0]*D[i0] + aDi[1]*D[i1];
243  A[i0+ah*i0] += aDDt_ii;
244  A[i1+ah*i1] += aDDt_ii;
245  // 0 <= k < i
246  for (int k = 0; k < i; k++)
247  {
248  const int k0 = k+nd*0, k1 = k+nd*1;
249  const scalar_t aDDt_ik = aDi[0]*D[k0] + aDi[1]*D[k1];
250  A[i0+ah*k0] += aDDt_ik;
251  A[k0+ah*i0] += aDDt_ik;
252  A[i1+ah*k1] += aDDt_ik;
253  A[k1+ah*i1] += aDDt_ik;
254  }
255  }
256  }
257  void Assemble_ddI1b(scalar_t w, scalar_t *A)
258  {
259  // ddI1b = X1 + X2 + X3, where
260  // X1_ijkl = (I1b/I2) [ (δ_ks δ_it + δ_kt δ_si) dI2b_tj dI2b_sl ]
261  // = (I1b/I2) [ dI2b_ij dI2b_kl + dI2b_kj dI2b_il ]
262  // X2_ijkl = (2/I2b) δ_ik δ_jl = (1/I2b) ddI1_ijkl
263  // X3_ijkl = -(2/I2) (δ_ks δ_it) (J_tj dI2b_sl + dI2b_tj J_sl)
264  // = -(2/I2) (J_ij dI2b_kl + dI2b_ij J_kl)
265  //
266  // A(i+nd*j,k+nd*l) += (\sum_st w D_is ddI1b_jslt D_kt)
267  // or
268  // A(i+nd*j,k+nd*l) +=
269  // w (I1b/I2) [(D dI2b^t)_ij (D dI2b^t)_kl +
270  // (D dI2b^t)_il (D dI2b^t)_kj]
271  // + w (2/I2b) δ_jl (D D^t)_ik
272  // - w (2/I2) [(D J^t)_ij (D dI2b^t)_kl + (D dI2b^t)_ij (D J^t)_kl]
273 
274  if (dont(HAVE_DaJ)) { Eval_DaJ(); }
275  if (dont(HAVE_DJt)) { Eval_DJt(); }
276  const int nd = D_height;
277  const int ah = 2*nd;
278  const scalar_t a = w*Get_I1b()/Get_I2();
279  const scalar_t b = 2*w/Get_I2b();
280  const scalar_t c = -2*w/Get_I2();
281  for (int i = 0; i < nd; i++)
282  {
283  const int i0 = i+nd*0, i1 = i+nd*1;
284  const scalar_t aDaJ_i[2] = { a*DaJ[i0], a*DaJ[i1] };
285  const scalar_t bD_i[2] = { b*D[i0], b*D[i1] };
286  const scalar_t cDJt_i[2] = { c*DJt[i0], c*DJt[i1] };
287  const scalar_t cDaJ_i[2] = { c*DaJ[i0], c*DaJ[i1] };
288  // k == i
289  {
290  // Symmetries: A2_ii_00 = A2_ii_11
291  const scalar_t A2_ii = bD_i[0]*D[i0] + bD_i[1]*D[i1];
292 
293  A[i0+ah*i0] += 2*(aDaJ_i[0] + cDJt_i[0])*DaJ[i0] + A2_ii;
294 
295  // Symmetries: A_ii_01 = A_ii_10
296  const scalar_t A_ii_01 =
297  (2*aDaJ_i[0] + cDJt_i[0])*DaJ[i1] + cDaJ_i[0]*DJt[i1];
298  A[i0+ah*i1] += A_ii_01;
299  A[i1+ah*i0] += A_ii_01;
300 
301  A[i1+ah*i1] += 2*(aDaJ_i[1] + cDJt_i[1])*DaJ[i1] + A2_ii;
302  }
303  // 0 <= k < i
304  for (int k = 0; k < i; k++)
305  {
306  const int k0 = k+nd*0, k1 = k+nd*1;
307  // Symmetries: A1_ik_01 = A1_ik_10 = A1_ki_01 = A1_ki_10
308  const scalar_t A1_ik_01 = aDaJ_i[0]*DaJ[k1] + aDaJ_i[1]*DaJ[k0];
309 
310  // Symmetries: A2_ik_00 = A2_ik_11 = A2_ki_00 = A2_ki_11
311  const scalar_t A2_ik = bD_i[0]*D[k0] + bD_i[1]*D[k1];
312 
313  const scalar_t A_ik_00 =
314  (2*aDaJ_i[0] + cDJt_i[0])*DaJ[k0] + A2_ik + cDaJ_i[0]*DJt[k0];
315  A[i0+ah*k0] += A_ik_00;
316  A[k0+ah*i0] += A_ik_00;
317 
318  const scalar_t A_ik_01 =
319  A1_ik_01 + cDJt_i[0]*DaJ[k1] + cDaJ_i[0]*DJt[k1];
320  A[i0+ah*k1] += A_ik_01;
321  A[k1+ah*i0] += A_ik_01;
322 
323  const scalar_t A_ik_10 =
324  A1_ik_01 + cDJt_i[1]*DaJ[k0] + cDaJ_i[1]*DJt[k0];
325  A[i1+ah*k0] += A_ik_10;
326  A[k0+ah*i1] += A_ik_10;
327 
328  const scalar_t A_ik_11 =
329  (2*aDaJ_i[1] + cDJt_i[1])*DaJ[k1] + A2_ik + cDaJ_i[1]*DJt[k1];
330  A[i1+ah*k1] += A_ik_11;
331  A[k1+ah*i1] += A_ik_11;
332  }
333  }
334  }
335  void Assemble_ddI2(scalar_t w, scalar_t *A)
336  {
337  // ddI2_ijkl = 2 (2 δ_ks δ_it - δ_kt δ_si) dI2b_tj dI2b_sl
338  // = 4 dI2b_ij dI2b_kl - 2 dI2b_kj dI2b_il
339  // = 2 dI2b_ij dI2b_kl + 2 (dI2b_ij dI2b_kl - dI2b_kj dI2b_il)
340  //
341  // A(i+nd*j,k+nd*l) += (\sum_st w D_is ddI2_jslt D_kt)
342  // or
343  // A(i+nd*j,k+nd*l) +=
344  // (\sum_st w D_is (4 dI2b_js dI2b_lt - 2 dI2b_ls dI2b_jt) D_kt)
345  // A(i+nd*j,k+nd*l) +=
346  // 2 w [2 (D dI2b^t)_ij (D dI2b^t)_kl - (D dI2b^t)_il (D dI2b^t)_kj]
347  //
348  // Note: the expression
349  // (D dI2b^t)_ij (D dI2b^t)_kl - (D dI2b^t)_il (D dI2b^t)_kj
350  // is the determinant of the 2x2 matrix formed by rows {i,k} and columns
351  // {j,l} from the matrix (D dI2b^t).
352 
353  if (dont(HAVE_DaJ)) { Eval_DaJ(); }
354  const int nd = D_height;
355  const int ah = 2*nd;
356  const scalar_t a = 2*w;
357  for (int i = 0; i < ah; i++)
358  {
359  const scalar_t avi = a*DaJ[i];
360  A[i+ah*i] += avi*DaJ[i];
361  for (int j = 0; j < i; j++)
362  {
363  const scalar_t aVVt_ij = avi*DaJ[j];
364  A[i+ah*j] += aVVt_ij;
365  A[j+ah*i] += aVVt_ij;
366  }
367  }
368  const int j = 1, l = 0;
369  for (int i = 0; i < nd; i++)
370  {
371  const int ij = i+nd*j, il = i+nd*l;
372  const scalar_t aDaJ_ij = a*DaJ[ij], aDaJ_il = a*DaJ[il];
373  for (int k = 0; k < i; k++)
374  {
375  const int kj = k+nd*j, kl = k+nd*l;
376  const scalar_t A_ijkl = aDaJ_ij*DaJ[kl] - aDaJ_il*DaJ[kj];
377  A[ij+ah*kl] += A_ijkl;
378  A[kl+ah*ij] += A_ijkl;
379  A[kj+ah*il] -= A_ijkl;
380  A[il+ah*kj] -= A_ijkl;
381  }
382  }
383  }
384  void Assemble_ddI2b(scalar_t w, scalar_t *A)
385  {
386  // ddI2b_ijkl = (1/I2b) (δ_ks δ_it - δ_kt δ_si) dI2b_tj dI2b_sl
387  // [j -> u], [l -> v], [i -> j], [k -> l]
388  // ddI2b_julv = (1/I2b) (δ_ls δ_jt - δ_lt δ_sj) dI2b_tu dI2b_sv
389  //
390  // A(i+nd*j,k+nd*l) += (\sum_st w D_is ddI2b_jslt D_kt)
391  // or
392  // A(i+nd*j,k+nd*l) += (\sum_uv w D_iu ddI2b_julv D_kv)
393  // A(i+nd*j,k+nd*l) +=
394  // (\sum_uvst (w/I2b)
395  // D_iu (δ_ls δ_jt - δ_lt δ_sj) dI2b_tu dI2b_sv D_kv)
396  // A(i+nd*j,k+nd*l) +=
397  // (\sum_st (w/I2b)
398  // (D dI2b^t)_it (δ_ls δ_jt - δ_lt δ_sj) (D dI2b^t)_ks)
399  // A(i+nd*j,k+nd*l) += (w/I2b)
400  // [ (D dI2b^t)_ij (D dI2b^t)_kl - (D dI2b^t)_il (D dI2b^t)_kj ]
401 
402  if (dont(HAVE_DaJ)) { Eval_DaJ(); }
403  const int nd = D_height;
404  const int ah = 2*nd;
405  const int j = 1, l = 0;
406  const scalar_t a = w/Get_I2b();
407  for (int i = 0; i < nd; i++)
408  {
409  const int ij = i+nd*j, il = i+nd*l;
410  const scalar_t aDaJ_ij = a*DaJ[ij], aDaJ_il = a*DaJ[il];
411  for (int k = 0; k < i; k++)
412  {
413  const int kj = k+nd*j, kl = k+nd*l;
414  const scalar_t A_ijkl = aDaJ_ij*DaJ[kl] - aDaJ_il*DaJ[kj];
415  A[ij+ah*kl] += A_ijkl;
416  A[kl+ah*ij] += A_ijkl;
417  A[kj+ah*il] -= A_ijkl;
418  A[il+ah*kj] -= A_ijkl;
419  }
420  }
421  }
422  // Assemble the contribution from the term: T_ijkl = X_ij Y_kl + Y_ij X_kl,
423  // where X and Y are pointers to 2x2 matrices stored in column-major layout.
424  //
425  // The contribution to the matrix A is given by:
426  // A(i+nd*j,k+nd*l) += \sum_st w D_is T_jslt D_kt
427  // or
428  // A(i+nd*j,k+nd*l) += \sum_st w D_is (X_js Y_lt + Y_js X_lt) D_kt
429  // or
430  // A(i+nd*j,k+nd*l) +=
431  // \sum_st w [ (D X^t)_ij (D Y^t)_kl + (D Y^t)_ij (D X^t)_kl ]
432  void Assemble_TProd(scalar_t w, const scalar_t *X, const scalar_t *Y,
433  scalar_t *A)
434  {
435  Eval_DZt(X, &DXt);
436  Eval_DZt(Y, &DYt);
437  const int nd = D_height;
438  const int ah = 2*nd;
439 
440  for (int i = 0; i < ah; i++)
441  {
442  const scalar_t axi = w*DXt[i], ayi = w*DYt[i];
443  A[i+ah*i] += 2*axi*DYt[i];
444  for (int j = 0; j < i; j++)
445  {
446  const scalar_t A_ij = axi*DYt[j] + ayi*DXt[j];
447  A[i+ah*j] += A_ij;
448  A[j+ah*i] += A_ij;
449  }
450  }
451  }
452 
453  // Assemble the contribution from the term: T_ijkl = X_ij X_kl, where X is a
454  // pointer to a 2x2 matrix stored in column-major layout.
455  //
456  // The contribution to the matrix A is given by:
457  // A(i+nd*j,k+nd*l) += \sum_st w D_is X_js X_lt D_kt
458  // or
459  // A(i+nd*j,k+nd*l) += \sum_st w [ (D X^t)_ij (D X^t)_kl ]
460  void Assemble_TProd(scalar_t w, const scalar_t *X, scalar_t *A)
461  {
462  Eval_DZt(X, &DXt);
463  const int nd = D_height;
464  const int ah = 2*nd;
465 
466  for (int i = 0; i < ah; i++)
467  {
468  const scalar_t axi = w*DXt[i];
469  A[i+ah*i] += axi*DXt[i];
470  for (int j = 0; j < i; j++)
471  {
472  const scalar_t A_ij = axi*DXt[j];
473  A[i+ah*j] += A_ij;
474  A[j+ah*i] += A_ij;
475  }
476  }
477  }
478 };
479 
480 
481 /** @brief Auxiliary class for evaluating the 3x3 matrix invariants and their
482  first and second derivatives. */
483 /**
484  The type `scalar_t` must support the standard operations:
485 
486  =, +=, -=, +, -, *, /, unary -, int*scalar_t, int/scalar_t, scalar_t/int
487 
488  The type `scalar_ops` must define the static methods:
489 
490  scalar_t sign(const scalar_t &);
491  scalar_t pow(const scalar_t &x, int a, int b); // x^(a/b)
492 */
493 template <typename scalar_t, typename scalar_ops = ScalarOps<scalar_t> >
495 {
496 protected:
497  // Transformation Jacobian
498  const scalar_t *J;
499 
500  // Invariants:
501  // I_1 = ||J||_F^2, \bar{I}_1 = det(J)^{-2/3}*I_1,
502  // I_2 = (1/2)*(||J||_F^4-||J J^t||_F^2) = (1/2)*(I_1^2-||J J^t||_F^2),
503  // \bar{I}_2 = det(J)^{-4/3}*I_2,
504  // I_3 = det(J)^2, \bar{I}_3 = det(J).
505  scalar_t I1, I1b, I2, I2b, I3b;
506  scalar_t I3b_p; // I3b^{-2/3}
507 
508  // Derivatives of I1, I1b, I2, I2b, I3, and I3b using column-major storage.
509  scalar_t dI1[9], dI1b[9], dI2[9], dI2b[9], dI3[9], dI3b[9];
510  scalar_t B[6]; // B = J J^t (diagonal entries first, then off-diagonal)
511 
513  const scalar_t *D; // Always points to external data or is empty
514  scalar_t *DaJ, *DJt, *DdI2t, *DXt, *DYt;
515 
517  {
518  HAVE_I1 = 1,
519  HAVE_I1b = 2,
521  HAVE_I2 = 8,
522  HAVE_I2b = 16,
523  HAVE_I3b = 1<<5,
524  HAVE_I3b_p = 1<<6,
525  HAVE_dI1 = 1<<7,
526  HAVE_dI1b = 1<<8,
527  HAVE_dI2 = 1<<9,
528  HAVE_dI2b = 1<<10,
529  HAVE_dI3 = 1<<11,
530  HAVE_dI3b = 1<<12,
531  HAVE_DaJ = 1<<13, // D adj(J) = D dI3b^t
532  HAVE_DJt = 1<<14, // D J^t
533  HAVE_DdI2t = 1<<15 // D dI2^t
534  };
535 
536  // Bitwise OR of EvalMasks
538 
539  bool dont(int have_mask) const { return !(eval_state & have_mask); }
540 
541  void Eval_I1()
542  {
543  eval_state |= HAVE_I1;
544  B[0] = J[0]*J[0] + J[3]*J[3] + J[6]*J[6];
545  B[1] = J[1]*J[1] + J[4]*J[4] + J[7]*J[7];
546  B[2] = J[2]*J[2] + J[5]*J[5] + J[8]*J[8];
547  I1 = B[0] + B[1] + B[2];
548  }
549  void Eval_I1b() // det(J)^{-2/3}*I_1 = I_1/I_3^{1/3}
550  {
551  eval_state |= HAVE_I1b;
552  I1b = Get_I1()*Get_I3b_p();
553  }
554  void Eval_B_offd()
555  {
557  // B = J J^t
558  // B[3]=B(0,1), B[4]=B(0,2), B[5]=B(1,2)
559  B[3] = J[0]*J[1] + J[3]*J[4] + J[6]*J[7]; // B(0,1)
560  B[4] = J[0]*J[2] + J[3]*J[5] + J[6]*J[8]; // B(0,2)
561  B[5] = J[1]*J[2] + J[4]*J[5] + J[7]*J[8]; // B(1,2)
562  }
563  void Eval_I2()
564  {
565  eval_state |= HAVE_I2;
566  Get_I1();
567  if (dont(HAVE_B_offd)) { Eval_B_offd(); }
568  const scalar_t BF2 = B[0]*B[0] + B[1]*B[1] + B[2]*B[2] +
569  2*(B[3]*B[3] + B[4]*B[4] + B[5]*B[5]);
570  I2 = (I1*I1 - BF2)/2;
571  }
572  void Eval_I2b() // I2b = I2*I3b^{-4/3}
573  {
574  eval_state |= HAVE_I2b;
575  Get_I3b_p();
576  I2b = Get_I2()*I3b_p*I3b_p;
577  }
578  void Eval_I3b() // det(J)
579  {
580  eval_state |= HAVE_I3b;
581  I3b = J[0]*(J[4]*J[8] - J[7]*J[5]) - J[1]*(J[3]*J[8] - J[5]*J[6]) +
582  J[2]*(J[3]*J[7] - J[4]*J[6]);
583  }
584  scalar_t Get_I3b_p() // I3b^{-2/3}
585  {
586  if (dont(HAVE_I3b_p))
587  {
589  const scalar_t i3b = Get_I3b();
590  I3b_p = scalar_ops::pow(i3b, -2, 3);
591  }
592  return I3b_p;
593  }
594  void Eval_dI1()
595  {
596  eval_state |= HAVE_dI1;
597  for (int i = 0; i < 9; i++)
598  {
599  dI1[i] = 2*J[i];
600  }
601  }
602  void Eval_dI1b()
603  {
605  // I1b = I3b^{-2/3}*I1
606  // dI1b = 2*I3b^{-2/3}*(J - (1/3)*I1/I3b*dI3b)
607  const scalar_t c1 = 2*Get_I3b_p();
608  const scalar_t c2 = Get_I1()/(3*I3b);
609  Get_dI3b();
610  for (int i = 0; i < 9; i++)
611  {
612  dI1b[i] = c1*(J[i] - c2*dI3b[i]);
613  }
614  }
615  void Eval_dI2()
616  {
617  eval_state |= HAVE_dI2;
618  // dI2 = 2 I_1 J - 2 J J^t J = 2 (I_1 I - B) J
619  Get_I1();
620  if (dont(HAVE_B_offd)) { Eval_B_offd(); }
621  // B[0]=B(0,0), B[1]=B(1,1), B[2]=B(2,2)
622  // B[3]=B(0,1), B[4]=B(0,2), B[5]=B(1,2)
623  const scalar_t C[6] =
624  {
625  2*(I1 - B[0]), 2*(I1 - B[1]), 2*(I1 - B[2]),
626  -2*B[3], -2*B[4], -2*B[5]
627  };
628  // | C[0] C[3] C[4] | | J[0] J[3] J[6] |
629  // dI2 = | C[3] C[1] C[5] | | J[1] J[4] J[7] |
630  // | C[4] C[5] C[2] | | J[2] J[5] J[8] |
631  dI2[0] = C[0]*J[0] + C[3]*J[1] + C[4]*J[2];
632  dI2[1] = C[3]*J[0] + C[1]*J[1] + C[5]*J[2];
633  dI2[2] = C[4]*J[0] + C[5]*J[1] + C[2]*J[2];
634 
635  dI2[3] = C[0]*J[3] + C[3]*J[4] + C[4]*J[5];
636  dI2[4] = C[3]*J[3] + C[1]*J[4] + C[5]*J[5];
637  dI2[5] = C[4]*J[3] + C[5]*J[4] + C[2]*J[5];
638 
639  dI2[6] = C[0]*J[6] + C[3]*J[7] + C[4]*J[8];
640  dI2[7] = C[3]*J[6] + C[1]*J[7] + C[5]*J[8];
641  dI2[8] = C[4]*J[6] + C[5]*J[7] + C[2]*J[8];
642  }
643  void Eval_dI2b()
644  {
646  // I2b = det(J)^{-4/3}*I2 = I3b^{-4/3}*I2
647  // dI2b = (-4/3)*I3b^{-7/3}*I2*dI3b + I3b^{-4/3}*dI2
648  // = I3b^{-4/3} * [ dI2 - (4/3)*I2/I3b*dI3b ]
649  Get_I3b_p();
650  const scalar_t c1 = I3b_p*I3b_p;
651  const scalar_t c2 = (4*Get_I2()/I3b)/3;
652  Get_dI2();
653  Get_dI3b();
654  for (int i = 0; i < 9; i++)
655  {
656  dI2b[i] = c1*(dI2[i] - c2*dI3b[i]);
657  }
658  }
659  void Eval_dI3()
660  {
661  eval_state |= HAVE_dI3;
662  // I3 = I3b^2
663  // dI3 = 2*I3b*dI3b = 2*det(J)*adj(J)^T
664  const scalar_t c1 = 2*Get_I3b();
665  Get_dI3b();
666  for (int i = 0; i < 9; i++)
667  {
668  dI3[i] = c1*dI3b[i];
669  }
670  }
671  void Eval_dI3b()
672  {
674  // I3b = det(J)
675  // dI3b = adj(J)^T
676  dI3b[0] = J[4]*J[8] - J[5]*J[7]; // 0 3 6
677  dI3b[1] = J[5]*J[6] - J[3]*J[8]; // 1 4 7
678  dI3b[2] = J[3]*J[7] - J[4]*J[6]; // 2 5 8
679  dI3b[3] = J[2]*J[7] - J[1]*J[8];
680  dI3b[4] = J[0]*J[8] - J[2]*J[6];
681  dI3b[5] = J[1]*J[6] - J[0]*J[7];
682  dI3b[6] = J[1]*J[5] - J[2]*J[4];
683  dI3b[7] = J[2]*J[3] - J[0]*J[5];
684  dI3b[8] = J[0]*J[4] - J[1]*J[3];
685  }
686  void Eval_DZt(const scalar_t *Z, scalar_t **DZt_ptr)
687  {
688  MFEM_ASSERT(D != NULL, "");
689  const int nd = D_height;
690  scalar_t *DZt = *DZt_ptr;
691  if (DZt == NULL) { *DZt_ptr = DZt = new scalar_t[3*alloc_height]; }
692  for (int i = 0; i < nd; i++)
693  {
694  const int i0 = i+nd*0, i1 = i+nd*1, i2 = i+nd*2;
695  DZt[i0] = D[i0]*Z[0] + D[i1]*Z[3] + D[i2]*Z[6];
696  DZt[i1] = D[i0]*Z[1] + D[i1]*Z[4] + D[i2]*Z[7];
697  DZt[i2] = D[i0]*Z[2] + D[i1]*Z[5] + D[i2]*Z[8];
698  }
699  }
700  void Eval_DaJ() // DaJ = D adj(J) = D dI3b^t
701  {
702  eval_state |= HAVE_DaJ;
703  Get_dI3b();
704  Eval_DZt(dI3b, &DaJ);
705  }
706  void Eval_DJt() // DJt = D J^t
707  {
708  eval_state |= HAVE_DJt;
709  Eval_DZt(J, &DJt);
710  }
711  void Eval_DdI2t() // DdI2t = D dI2^t
712  {
714  Get_dI2();
715  Eval_DZt(dI2, &DdI2t);
716  }
717 
718 public:
719  /// The Jacobian should use column-major storage.
720  InvariantsEvaluator3D(const scalar_t *Jac = NULL)
721  : J(Jac), D_height(), alloc_height(),
722  D(), DaJ(), DJt(), DdI2t(), DXt(), DYt(), eval_state(0) { }
723 
725  {
726  delete [] DYt;
727  delete [] DXt;
728  delete [] DdI2t;
729  delete [] DJt;
730  delete [] DaJ;
731  }
732 
733  /// The Jacobian should use column-major storage.
734  void SetJacobian(const scalar_t *Jac) { J = Jac; eval_state = 0; }
735 
736  /// The @a Deriv matrix is `dof x 3`, using column-major storage.
737  void SetDerivativeMatrix(int height, const scalar_t *Deriv)
738  {
740  if (alloc_height < height)
741  {
742  delete [] DYt; DYt = NULL;
743  delete [] DXt; DXt = NULL;
744  delete [] DdI2t; DdI2t = NULL;
745  delete [] DJt; DJt = NULL;
746  delete [] DaJ; DaJ = NULL;
747  alloc_height = height;
748  }
749  D_height = height;
750  D = Deriv;
751  }
752 
753  scalar_t Get_I1() { if (dont(HAVE_I1 )) { Eval_I1(); } return I1; }
754  scalar_t Get_I1b() { if (dont(HAVE_I1b)) { Eval_I1b(); } return I1b; }
755  scalar_t Get_I2() { if (dont(HAVE_I2 )) { Eval_I2(); } return I2; }
756  scalar_t Get_I2b() { if (dont(HAVE_I2b)) { Eval_I2b(); } return I2b; }
757  scalar_t Get_I3() { if (dont(HAVE_I3b)) { Eval_I3b(); } return I3b*I3b; }
758  scalar_t Get_I3b() { if (dont(HAVE_I3b)) { Eval_I3b(); } return I3b; }
759 
760  const scalar_t *Get_dI1()
761  {
762  if (dont(HAVE_dI1 )) { Eval_dI1(); } return dI1;
763  }
764  const scalar_t *Get_dI1b()
765  {
766  if (dont(HAVE_dI1b)) { Eval_dI1b(); } return dI1b;
767  }
768  const scalar_t *Get_dI2()
769  {
770  if (dont(HAVE_dI2)) { Eval_dI2(); } return dI2;
771  }
772  const scalar_t *Get_dI2b()
773  {
774  if (dont(HAVE_dI2b)) { Eval_dI2b(); } return dI2b;
775  }
776  const scalar_t *Get_dI3()
777  {
778  if (dont(HAVE_dI3)) { Eval_dI3(); } return dI3;
779  }
780  const scalar_t *Get_dI3b()
781  {
782  if (dont(HAVE_dI3b)) { Eval_dI3b(); } return dI3b;
783  }
784 
785  // Assemble operation for tensor X with components X_jslt:
786  // A(i+nd*j,k+nd*l) += (\sum_st w D_is X_jslt D_kt)
787  // 0 <= i,k < nd, 0 <= j,l,s,t < 3
788  // where nd is the height of D, i.e. the number of DOFs in one component.
789 
790  void Assemble_ddI1(scalar_t w, scalar_t *A)
791  {
792  // ddI1_jslt = 2 I_jslt = 2 δ_jl δ_st
793  // A(i+nd*j,k+nd*l) += (\sum_st 2 w D_is δ_jl δ_st D_kt)
794  // or
795  // A(i+nd*j,k+nd*l) += (2 w) (\sum_s D_is D_ks) δ_jl
796  // A(i+nd*j,k+nd*l) += (2 w) (D D^t)_ik δ_jl
797 
798  const int nd = D_height;
799  const int ah = 3*nd;
800  const scalar_t a = 2*w;
801  for (int i = 0; i < nd; i++)
802  {
803  const int i0 = i+nd*0, i1 = i+nd*1, i2 = i+nd*2;
804  const scalar_t aDi[3] = { a*D[i0], a*D[i1], a*D[i2] };
805  // k == i
806  const scalar_t aDDt_ii = aDi[0]*D[i0] + aDi[1]*D[i1] + aDi[2]*D[i2];
807  A[i0+ah*i0] += aDDt_ii;
808  A[i1+ah*i1] += aDDt_ii;
809  A[i2+ah*i2] += aDDt_ii;
810  // 0 <= k < i
811  for (int k = 0; k < i; k++)
812  {
813  const int k0 = k+nd*0, k1 = k+nd*1, k2 = k+nd*2;
814  const scalar_t aDDt_ik = aDi[0]*D[k0] + aDi[1]*D[k1] + aDi[2]*D[k2];
815  A[i0+ah*k0] += aDDt_ik;
816  A[k0+ah*i0] += aDDt_ik;
817  A[i1+ah*k1] += aDDt_ik;
818  A[k1+ah*i1] += aDDt_ik;
819  A[i2+ah*k2] += aDDt_ik;
820  A[k2+ah*i2] += aDDt_ik;
821  }
822  }
823  }
824  void Assemble_ddI1b(scalar_t w, scalar_t *A)
825  {
826  // Similar to InvariantsEvaluator2D::Assemble_ddI1b():
827  //
828  // ddI1b = X1 + X2 + X3, where
829  // X1_ijkl = (2/3*I1b/I3) [ (2/3 δ_ks δ_it + δ_kt δ_si) dI3b_tj dI3b_sl ]
830  // = (2/3*I1b/I3) [ 2/3 dI3b_ij dI3b_kl + dI3b_kj dI3b_il ]
831  // X2_ijkl = (2*I3b^{-2/3}) δ_ik δ_jl = (I3b^{-2/3}) ddI1_ijkl
832  // X3_ijkl = -(4/3*I3b^{-5/3}) (δ_ks δ_it) (J_tj dI3b_sl + dI3b_tj J_sl)
833  // = -(4/3*I3b^{-5/3}) (J_ij dI3b_kl + dI3b_ij J_kl)
834  //
835  // A(i+nd*j,k+nd*l) += (\sum_st w D_is ddI1b_jslt D_kt)
836  // or
837  // A(i+nd*j,k+nd*l) +=
838  // w (2/3*I1b/I3) [ 2/3 DaJ_ij DaJ_kl + DaJ_il DaJ_kj ]
839  // + w (2*I3b^{-2/3}) (D D^t)_ik δ_jl
840  // - w (4/3*I3b^{-5/3}) [ DJt_ij DaJ_kl + DaJ_ij DJt_kl ]
841 
842  if (dont(HAVE_DaJ)) { Eval_DaJ(); }
843  if (dont(HAVE_DJt)) { Eval_DJt(); }
844  const int nd = D_height;
845  const int ah = 3*nd;
846  const scalar_t r23 = scalar_t(2)/3;
847  const scalar_t r53 = scalar_t(5)/3;
848  const scalar_t a = r23*w*Get_I1b()/Get_I3();
849  const scalar_t b = 2*w*Get_I3b_p();
850  const scalar_t c = -r23*b/I3b;
851  for (int i = 0; i < nd; i++)
852  {
853  // A1a_ik_jl = 2/3 a DaJ_ij DaJ_kl, A1b_ik_jl = a DaJ_il DaJ_kj
854  // Symmetries: A1a_ik_jl = A1a_ki_lj = 2/3 A1b_ik_lj = 2/3 A1b_ki_jl
855  // A1_ik_jl = A1_ki_lj = A1b_ik_jl + 2/3 A1b_ik_lj
856  // A1_ik_lj = A1_ki_jl = 2/3 A1b_ik_jl + A1b_ik_lj
857  // k == i:
858  // A1_ii_jl = A1_ii_lj = (5/3) a DaJ_ij DaJ_il
859  // l == j:
860  // A1_ik_jj = A1_ki_jj = (5/3) a DaJ_ij DaJ_kj
861  // k == i && l == j:
862  // A1_ii_jj = (5/3) a DaJ_ij^2
863 
864  // A2_ik_jl = b (D D^t)_ik δ_jl
865  // Symmetries:
866 
867  // A3_ik_jl = c [ DJt_ij DaJ_kl + DaJ_ij DJt_kl ]
868  // Symmetries:
869  // A3_ik_jl = A3_ki_lj = c [ DJt_ij DaJ_kl + DaJ_ij DJt_kl ]
870  // A3_ik_lj = A3_ki_jl = c [ DJt_il DaJ_kj + DaJ_il DJt_kj ]
871  // k == i:
872  // A3_ii_jl = A3_ii_lj = c [ DJt_ij DaJ_il + DaJ_ij DJt_il ]
873  // l == j:
874  // A3_ik_jj = A3_ki_jj = c [ DJt_ij DaJ_kj + DaJ_ij DJt_kj ]
875  // k == i && l == j:
876  // A3_ii_jj = 2 c DJt_ij DaJ_ij
877 
878  const int i0 = i+nd*0, i1 = i+nd*1, i2 = i+nd*2;
879  const scalar_t aDaJ_i[3] = { a*DaJ[i0], a*DaJ[i1], a*DaJ[i2] };
880  const scalar_t bD_i[3] = { b*D[i0], b*D[i1], b*D[i2] };
881  const scalar_t cDJt_i[3] = { c*DJt[i0], c*DJt[i1], c*DJt[i2] };
882  const scalar_t cDaJ_i[3] = { c*DaJ[i0], c*DaJ[i1], c*DaJ[i2] };
883  // k == i
884  {
885  // Symmetries: A2_ii_00 = A2_ii_11 = A2_ii_22
886  const scalar_t A2_ii = bD_i[0]*D[i0]+bD_i[1]*D[i1]+bD_i[2]*D[i2];
887  A[i0+ah*i0] += (r53*aDaJ_i[0] + 2*cDJt_i[0])*DaJ[i0] + A2_ii;
888  A[i1+ah*i1] += (r53*aDaJ_i[1] + 2*cDJt_i[1])*DaJ[i1] + A2_ii;
889  A[i2+ah*i2] += (r53*aDaJ_i[2] + 2*cDJt_i[2])*DaJ[i2] + A2_ii;
890 
891  // Symmetries: A_ii_jl = A_ii_lj
892  for (int j = 1; j < 3; j++)
893  {
894  const int ij = i+nd*j;
895  for (int l = 0; l < j; l++)
896  {
897  const int il = i+nd*l;
898  const scalar_t A_ii_jl =
899  (r53*aDaJ_i[j] + cDJt_i[j])*DaJ[il] + cDaJ_i[j]*DJt[il];
900  A[ij+ah*il] += A_ii_jl;
901  A[il+ah*ij] += A_ii_jl;
902  }
903  }
904  }
905  // 0 <= k < i
906  for (int k = 0; k < i; k++)
907  {
908  const int k0 = k+nd*0, k1 = k+nd*1, k2 = k+nd*2;
909  // Symmetries: A2_ik_jj = A2_ki_ll
910  const scalar_t A2_ik = bD_i[0]*D[k0]+bD_i[1]*D[k1]+bD_i[2]*D[k2];
911 
912  // l == j
913  for (int j = 0; j < 3; j++)
914  {
915  const int ij = i+nd*j, kj = k+nd*j;
916  const scalar_t A_ik_jj = (r53*aDaJ_i[j] + cDJt_i[j])*DaJ[kj] +
917  cDaJ_i[j]*DJt[kj] + A2_ik;
918  A[ij+ah*kj] += A_ik_jj;
919  A[kj+ah*ij] += A_ik_jj;
920  }
921 
922  // 0 <= l < j
923  for (int j = 1; j < 3; j++)
924  {
925  const int ij = i+nd*j, kj = k+nd*j;
926  for (int l = 0; l < j; l++)
927  {
928  const int il = i+nd*l, kl = k+nd*l;
929  // A1b_ik_jl = a DaJ_il DaJ_kj
930  const scalar_t A1b_ik_jl = aDaJ_i[l]*DaJ[kj];
931  const scalar_t A1b_ik_lj = aDaJ_i[j]*DaJ[kl];
932  // A1_ik_jl = A1_ki_lj = A1b_ik_jl + 2/3 A1b_ik_lj
933  // A1_ik_lj = A1_ki_jl = 2/3 A1b_ik_jl + A1b_ik_lj
934  // A3_ik_jl = c [ DJt_ij DaJ_kl + DaJ_ij DJt_kl ]
935  const scalar_t A_ik_jl = A1b_ik_jl + r23*A1b_ik_lj +
936  cDJt_i[j]*DaJ[kl]+cDaJ_i[j]*DJt[kl];
937  A[ij+ah*kl] += A_ik_jl;
938  A[kl+ah*ij] += A_ik_jl;
939  const scalar_t A_ik_lj = r23*A1b_ik_jl + A1b_ik_lj +
940  cDJt_i[l]*DaJ[kj]+cDaJ_i[l]*DJt[kj];
941  A[il+ah*kj] += A_ik_lj;
942  A[kj+ah*il] += A_ik_lj;
943  }
944  }
945  }
946  }
947  }
948  void Assemble_ddI2(scalar_t w, scalar_t *A)
949  {
950  // dI2 = 2 I_1 J - 2 J J^t J = 2 (I_1 I - B) J
951  //
952  // ddI2 = X1 + X2 + X3
953  // X1_ijkl = (2 I_1) δ_ik δ_jl
954  // X2_ijkl = 2 ( 2 δ_ku δ_iv - δ_ik δ_uv - δ_kv δ_iu ) J_vj J_ul
955  // X3_ijkl = -2 (J J^t)_ik δ_jl = -2 B_ik δ_jl
956  //
957  // Apply: j->s, i->j, l->t, k->l
958  // X2_jslt = 2 ( δ_lu δ_jv - δ_jl δ_uv +
959  // δ_lu δ_jv - δ_lv δ_ju ) J_vs J_ut
960  //
961  // A(i+nd*j,k+nd*l) += (\sum_st w D_is ddI2_jslt D_kt)
962  //
963  // \sum_st w D_is X1_jslt D_kt =
964  // \sum_st w D_is [ (2 I_1) δ_jl δ_st ] D_kt =
965  // (2 w I_1) D_is δ_jl D_ks = (2 w I_1) (D D^t)_ik δ_jl
966  //
967  // \sum_st w D_is X2_jslt D_kt =
968  // \sum_stuv w D_is [ 2 ( δ_lu δ_jv - δ_jl δ_uv +
969  // δ_lu δ_jv - δ_lv δ_ju ) J_vs J_ut ] D_kt =
970  // \sum_uv 2 w [ δ_lu δ_jv - δ_jl δ_uv +
971  // δ_lu δ_jv - δ_lv δ_ju ] (D J^t)_iv (D J^t)_ku =
972  // 2 w ( DJt_ij DJt_kl - δ_jl (DJt DJt^t)_ik ) +
973  // 2 w ( DJt_ij DJt_kl - DJt_il DJt_kj )
974  //
975  // \sum_st w D_is X3_jslt D_kt = \sum_st w D_is [ -2 B_jl δ_st ] D_kt =
976  // -2 w (D D^t)_ik B_jl
977  //
978  // A(i+nd*j,k+nd*l) +=
979  // (2 w I_1) (D D^t)_ik δ_jl - 2 w (D D^t)_ik B_jl +
980  // 2 w DJt_ij DJt_kl - 2 w (DJt DJt^t)_ik δ_jl +
981  // 2 w ( DJt_ij DJt_kl - DJt_il DJt_kj )
982  //
983  // The last term is a determinant: rows {i,k} and columns {j,l} of DJt:
984  // | DJt_ij DJt_il |
985  // | DJt_kj DJt_kl | = DJt_ij DJt_kl - DJt_il DJt_kj
986 
987  if (dont(HAVE_DJt)) { Eval_DJt(); }
988  Get_I1(); // evaluates I1 and the diagonal of B
989  if (dont(HAVE_B_offd)) { Eval_B_offd(); }
990  const int nd = D_height;
991  const int ah = 3*nd;
992  const scalar_t a = 2*w;
993  for (int i = 0; i < ah; i++)
994  {
995  const scalar_t avi = a*DJt[i];
996  A[i+ah*i] += avi*DJt[i];
997  for (int j = 0; j < i; j++)
998  {
999  const scalar_t aVVt_ij = avi*DJt[j];
1000  A[i+ah*j] += aVVt_ij;
1001  A[j+ah*i] += aVVt_ij;
1002  }
1003  }
1004 
1005  for (int i = 0; i < nd; i++)
1006  {
1007  const int i0 = i+nd*0, i1 = i+nd*1, i2 = i+nd*2;
1008  const scalar_t aD_i[3] = { a*D[i0], a*D[i1], a*D[i2] };
1009  const scalar_t aDJt_i[3] = { a*DJt[i0], a*DJt[i1], a*DJt[i2] };
1010  // k == i
1011  {
1012  const scalar_t aDDt_ii =
1013  aD_i[0]*D[i0] + aD_i[1]*D[i1] + aD_i[2]*D[i2];
1014  const scalar_t Z1_ii =
1015  I1*aDDt_ii - (aDJt_i[0]*DJt[i0] + aDJt_i[1]*DJt[i1] +
1016  aDJt_i[2]*DJt[i2]);
1017  // l == j
1018  for (int j = 0; j < 3; j++)
1019  {
1020  const int ij = i+nd*j;
1021  A[ij+ah*ij] += Z1_ii - aDDt_ii*B[j];
1022  }
1023  // l != j
1024  const scalar_t Z2_ii_01 = aDDt_ii*B[3];
1025  const scalar_t Z2_ii_02 = aDDt_ii*B[4];
1026  const scalar_t Z2_ii_12 = aDDt_ii*B[5];
1027  A[i0+ah*i1] -= Z2_ii_01;
1028  A[i1+ah*i0] -= Z2_ii_01;
1029  A[i0+ah*i2] -= Z2_ii_02;
1030  A[i2+ah*i0] -= Z2_ii_02;
1031  A[i1+ah*i2] -= Z2_ii_12;
1032  A[i2+ah*i1] -= Z2_ii_12;
1033  }
1034  // 0 <= k < i
1035  for (int k = 0; k < i; k++)
1036  {
1037  const int k0 = k+nd*0, k1 = k+nd*1, k2 = k+nd*2;
1038  const scalar_t aDDt_ik =
1039  aD_i[0]*D[k0] + aD_i[1]*D[k1] + aD_i[2]*D[k2];
1040  const scalar_t Z1_ik =
1041  I1*aDDt_ik - (aDJt_i[0]*DJt[k0] + aDJt_i[1]*DJt[k1] +
1042  aDJt_i[2]*DJt[k2]);
1043  // l == j
1044  for (int j = 0; j < 3; j++)
1045  {
1046  const int ij = i+nd*j, kj = k+nd*j;
1047  const scalar_t Z2_ik_jj = Z1_ik - aDDt_ik*B[j];
1048  A[ij+ah*kj] += Z2_ik_jj;
1049  A[kj+ah*ij] += Z2_ik_jj;
1050  }
1051  // l != j
1052  {
1053  const scalar_t Z2_ik_01 = aDDt_ik*B[3];
1054  A[i0+ah*k1] -= Z2_ik_01;
1055  A[i1+ah*k0] -= Z2_ik_01;
1056  A[k0+ah*i1] -= Z2_ik_01;
1057  A[k1+ah*i0] -= Z2_ik_01;
1058  const scalar_t Z2_ik_02 = aDDt_ik*B[4];
1059  A[i0+ah*k2] -= Z2_ik_02;
1060  A[i2+ah*k0] -= Z2_ik_02;
1061  A[k0+ah*i2] -= Z2_ik_02;
1062  A[k2+ah*i0] -= Z2_ik_02;
1063  const scalar_t Z2_ik_12 = aDDt_ik*B[5];
1064  A[i1+ah*k2] -= Z2_ik_12;
1065  A[i2+ah*k1] -= Z2_ik_12;
1066  A[k1+ah*i2] -= Z2_ik_12;
1067  A[k2+ah*i1] -= Z2_ik_12;
1068  }
1069  // 0 <= l < j
1070  for (int j = 1; j < 3; j++)
1071  {
1072  const int ij = i+nd*j, kj = k+nd*j;
1073  for (int l = 0; l < j; l++)
1074  {
1075  const int il = i+nd*l, kl = k+nd*l;
1076  const scalar_t Z3_ik_jl =
1077  aDJt_i[j]*DJt[kl] - aDJt_i[l]*DJt[kj];
1078  A[ij+ah*kl] += Z3_ik_jl;
1079  A[kl+ah*ij] += Z3_ik_jl;
1080  A[il+ah*kj] -= Z3_ik_jl;
1081  A[kj+ah*il] -= Z3_ik_jl;
1082  }
1083  }
1084  }
1085  }
1086  }
1087  void Assemble_ddI2b(scalar_t w, scalar_t *A)
1088  {
1089  // dI2b = (-4/3)*I3b^{-7/3}*I2*dI3b + I3b^{-4/3}*dI2
1090  // = I3b^{-4/3} * [ dI2 - (4/3)*I2/I3b*dI3b ]
1091  //
1092  // ddI2b = X1 + X2 + X3
1093  // X1_ijkl = 16/9 det(J)^{-10/3} I2 dI3b_ij dI3b_kl +
1094  // 4/3 det(J)^{-10/3} I2 dI3b_il dI3b_kj
1095  // X2_ijkl = -4/3 det(J)^{-7/3} (dI2_ij dI3b_kl + dI2_kl dI3b_ij)
1096  // X3_ijkl = det(J)^{-4/3} ddI2_ijkl
1097  //
1098  // Apply: j->s, i->j, l->t, k->l
1099  // X1_jslt = 16/9 det(J)^{-10/3} I2 dI3b_js dI3b_lt +
1100  // 4/3 det(J)^{-10/3} I2 dI3b_jt dI3b_ls
1101  // X2_jslt = -4/3 det(J)^{-7/3} (dI2_js dI3b_lt + dI2_lt dI3b_js)
1102  //
1103  // A(i+nd*j,k+nd*l) += (\sum_st w D_is ddI2b_jslt D_kt)
1104  //
1105  // (\sum_st w D_is X1_jslt D_kt) =
1106  // 16/9 w det(J)^{-10/3} I2 DaJ_ij DaJ_kl +
1107  // 4/3 w det(J)^{-10/3} I2 DaJ_il DaJ_kj
1108  //
1109  // (\sum_st w D_is X1_jslt D_kt) =
1110  // -4/3 w det(J)^{-7/3} D_is (dI2_js dI3b_lt + dI2_lt dI3b_js) D_kt =
1111  // -4/3 w det(J)^{-7/3} [ (D dI2^t)_ij DaJ_kl + DaJ_ij (D dI2^t)_kl ]
1112  //
1113  // A(i+nd*j,k+nd*l) +=
1114  // 16/9 w det(J)^{-10/3} I2 DaJ_ij DaJ_kl +
1115  // 4/3 w det(J)^{-10/3} I2 DaJ_il DaJ_kj -
1116  // 4/3 w det(J)^{-7/3} [ DdI2t_ij DaJ_kl + DaJ_ij DdI2t_kl ] +
1117  // w det(J)^{-4/3} D_is D_kt ddI2_jslt
1118 
1119  Get_I3b_p(); // = det(J)^{-2/3}, evaluates I3b
1120  if (dont(HAVE_DaJ)) { Eval_DaJ(); }
1121  if (dont(HAVE_DdI2t)) { Eval_DdI2t(); }
1122  const int nd = D_height;
1123  const int ah = 3*nd;
1124  const scalar_t a = w*I3b_p*I3b_p;
1125  const scalar_t b = (-4*a)/(3*I3b);
1126  const scalar_t c = -b*Get_I2()/I3b;
1127  const scalar_t d = (4*c)/3;
1128 
1129  for (int i = 0; i < ah; i++)
1130  {
1131  const scalar_t dvi = d*DaJ[i];
1132  A[i+ah*i] += dvi*DaJ[i];
1133  for (int j = 0; j < i; j++)
1134  {
1135  const scalar_t dVVt_ij = dvi*DaJ[j];
1136  A[i+ah*j] += dVVt_ij;
1137  A[j+ah*i] += dVVt_ij;
1138  }
1139  }
1140  Assemble_ddI2(a, A);
1141  for (int i = 0; i < nd; i++)
1142  {
1143  const int i0 = i+nd*0, i1 = i+nd*1, i2 = i+nd*2;
1144  const scalar_t cDaJ_i[3] = { c*DaJ[i0], c*DaJ[i1], c*DaJ[i2] };
1145  const scalar_t bDaJ_i[3] = { b*DaJ[i0], b*DaJ[i1], b*DaJ[i2] };
1146  const scalar_t bDdI2t_i[3] = { b*DdI2t[i0], b*DdI2t[i1], b*DdI2t[i2] };
1147  // k == i
1148  {
1149  // l == j
1150  for (int j = 0; j < 3; j++)
1151  {
1152  const int ij = i+nd*j;
1153  A[ij+ah*ij] += (cDaJ_i[j] + 2*bDdI2t_i[j])*DaJ[ij];
1154  }
1155  // 0 <= l < j
1156  for (int j = 1; j < 3; j++)
1157  {
1158  const int ij = i+nd*j;
1159  for (int l = 0; l < j; l++)
1160  {
1161  const int il = i+nd*l;
1162  const scalar_t Z_ii_jl =
1163  (cDaJ_i[l] + bDdI2t_i[l])*DaJ[ij] + bDdI2t_i[j]*DaJ[il];
1164  A[ij+ah*il] += Z_ii_jl;
1165  A[il+ah*ij] += Z_ii_jl;
1166  }
1167  }
1168  }
1169  // 0 <= k < i
1170  for (int k = 0; k < i; k++)
1171  {
1172  // l == j
1173  for (int j = 0; j < 3; j++)
1174  {
1175  const int ij = i+nd*j, kj = k+nd*j;
1176  const scalar_t Z_ik_jj =
1177  (cDaJ_i[j] + bDdI2t_i[j])*DaJ[kj] + bDaJ_i[j]*DdI2t[kj];
1178  A[ij+ah*kj] += Z_ik_jj;
1179  A[kj+ah*ij] += Z_ik_jj;
1180  }
1181  // 0 <= l < j
1182  for (int j = 1; j < 3; j++)
1183  {
1184  const int ij = i+nd*j, kj = k+nd*j;
1185  for (int l = 0; l < j; l++)
1186  {
1187  const int il = i+nd*l, kl = k+nd*l;
1188  const scalar_t Z_ik_jl = cDaJ_i[l]*DaJ[kj] +
1189  bDdI2t_i[j]*DaJ[kl] +
1190  bDaJ_i[j]*DdI2t[kl];
1191  A[ij+ah*kl] += Z_ik_jl;
1192  A[kl+ah*ij] += Z_ik_jl;
1193  const scalar_t Z_ik_lj = cDaJ_i[j]*DaJ[kl] +
1194  bDdI2t_i[l]*DaJ[kj] +
1195  bDaJ_i[l]*DdI2t[kj];
1196  A[il+ah*kj] += Z_ik_lj;
1197  A[kj+ah*il] += Z_ik_lj;
1198  }
1199  }
1200  }
1201  }
1202  }
1203  void Assemble_ddI3(scalar_t w, scalar_t *A)
1204  {
1205  // Similar to InvariantsEvaluator2D::Assemble_ddI2():
1206  //
1207  // A(i+nd*j,k+nd*l) += 2 w [ 2 DaJ_ij DaJ_kl - DaJ_il DaJ_kj ]
1208  //
1209  // Note: the expression ( DaJ_ij DaJ_kl - DaJ_il DaJ_kj ) is the
1210  // determinant of the 2x2 matrix formed by rows {i,k} and columns {j,l}
1211  // from the matrix DaJ = D dI3b^t.
1212 
1213  if (dont(HAVE_DaJ)) { Eval_DaJ(); }
1214  const int nd = D_height;
1215  const int ah = 3*nd;
1216  const scalar_t a = 2*w;
1217 
1218  for (int i = 0; i < ah; i++)
1219  {
1220  const scalar_t avi = a*DaJ[i];
1221  A[i+ah*i] += avi*DaJ[i];
1222  for (int j = 0; j < i; j++)
1223  {
1224  const scalar_t aVVt_ij = avi*DaJ[j];
1225  A[i+ah*j] += aVVt_ij;
1226  A[j+ah*i] += aVVt_ij;
1227  }
1228  }
1229  for (int j = 1; j < 3; j++)
1230  {
1231  for (int l = 0; l < j; l++)
1232  {
1233  for (int i = 0; i < nd; i++)
1234  {
1235  const int ij = i+nd*j, il = i+nd*l;
1236  const scalar_t aDaJ_ij = a*DaJ[ij], aDaJ_il = a*DaJ[il];
1237  for (int k = 0; k < i; k++)
1238  {
1239  const int kj = k+nd*j, kl = k+nd*l;
1240  const scalar_t A_ijkl = aDaJ_ij*DaJ[kl] - aDaJ_il*DaJ[kj];
1241  A[ij+ah*kl] += A_ijkl;
1242  A[kl+ah*ij] += A_ijkl;
1243  A[kj+ah*il] -= A_ijkl;
1244  A[il+ah*kj] -= A_ijkl;
1245  }
1246  }
1247  }
1248  }
1249  }
1250  void Assemble_ddI3b(scalar_t w, scalar_t *A)
1251  {
1252  // Similar to InvariantsEvaluator2D::Assemble_ddI2b():
1253  //
1254  // A(i+nd*j,k+nd*l) += (w/I3b) [ DaJ_ij DaJ_kl - DaJ_il DaJ_kj ]
1255  //
1256  // | DaJ_ij DaJ_il | = determinant of rows {i,k}, columns {j,l} from DaJ
1257  // | DaJ_kj DaJ_kl |
1258 
1259  if (dont(HAVE_DaJ)) { Eval_DaJ(); }
1260  const int nd = D_height;
1261  const int ah = 3*nd;
1262  const scalar_t a = w/Get_I3b();
1263  for (int j = 1; j < 3; j++)
1264  {
1265  for (int l = 0; l < j; l++)
1266  {
1267  for (int i = 0; i < nd; i++)
1268  {
1269  const int ij = i+nd*j, il = i+nd*l;
1270  const scalar_t aDaJ_ij = a*DaJ[ij], aDaJ_il = a*DaJ[il];
1271  for (int k = 0; k < i; k++)
1272  {
1273  const int kj = k+nd*j, kl = k+nd*l;
1274  const scalar_t A_ijkl = aDaJ_ij*DaJ[kl] - aDaJ_il*DaJ[kj];
1275  A[ij+ah*kl] += A_ijkl;
1276  A[kl+ah*ij] += A_ijkl;
1277  A[kj+ah*il] -= A_ijkl;
1278  A[il+ah*kj] -= A_ijkl;
1279  }
1280  }
1281  }
1282  }
1283  }
1284  // Assemble the contribution from the term: T_ijkl = X_ij Y_kl + Y_ij X_kl,
1285  // where X and Y are pointers to 3x3 matrices stored in column-major layout.
1286  //
1287  // The contribution to the matrix A is given by:
1288  // A(i+nd*j,k+nd*l) += \sum_st w D_is T_jslt D_kt
1289  // or
1290  // A(i+nd*j,k+nd*l) += \sum_st w D_is (X_js Y_lt + Y_js X_lt) D_kt
1291  // or
1292  // A(i+nd*j,k+nd*l) +=
1293  // \sum_st w [ (D X^t)_ij (D Y^t)_kl + (D Y^t)_ij (D X^t)_kl ]
1294  void Assemble_TProd(scalar_t w, const scalar_t *X, const scalar_t *Y,
1295  scalar_t *A)
1296  {
1297  Eval_DZt(X, &DXt);
1298  Eval_DZt(Y, &DYt);
1299  const int nd = D_height;
1300  const int ah = 3*nd;
1301 
1302  for (int i = 0; i < ah; i++)
1303  {
1304  const scalar_t axi = w*DXt[i], ayi = w*DYt[i];
1305  A[i+ah*i] += 2*axi*DYt[i];
1306  for (int j = 0; j < i; j++)
1307  {
1308  const scalar_t A_ij = axi*DYt[j] + ayi*DXt[j];
1309  A[i+ah*j] += A_ij;
1310  A[j+ah*i] += A_ij;
1311  }
1312  }
1313  }
1314  // Assemble the contribution from the term: T_ijkl = X_ij X_kl, where X is a
1315  // pointer to a 3x3 matrix stored in column-major layout.
1316  //
1317  // The contribution to the matrix A is given by:
1318  // A(i+nd*j,k+nd*l) += \sum_st w D_is X_js X_lt D_kt
1319  // or
1320  // A(i+nd*j,k+nd*l) += \sum_st w [ (D X^t)_ij (D X^t)_kl ]
1321  void Assemble_TProd(scalar_t w, const scalar_t *X, scalar_t *A)
1322  {
1323  Eval_DZt(X, &DXt);
1324  const int nd = D_height;
1325  const int ah = 3*nd;
1326 
1327  for (int i = 0; i < ah; i++)
1328  {
1329  const scalar_t axi = w*DXt[i];
1330  A[i+ah*i] += axi*DXt[i];
1331  for (int j = 0; j < i; j++)
1332  {
1333  const scalar_t A_ij = axi*DXt[j];
1334  A[i+ah*j] += A_ij;
1335  A[j+ah*i] += A_ij;
1336  }
1337  }
1338  }
1339 };
1340 
1341 }
1342 
1343 #endif
bool dont(int have_mask) const
Definition: invariants.hpp:539
void Assemble_ddI3(scalar_t w, scalar_t *A)
void Assemble_ddI2b(scalar_t w, scalar_t *A)
Definition: invariants.hpp:384
InvariantsEvaluator2D(const scalar_t *Jac=NULL)
The Jacobian should use column-major storage.
Definition: invariants.hpp:168
static scalar_t pow(const scalar_t &x, int m, int n)
Definition: invariants.hpp:32
void Assemble_ddI2b(scalar_t w, scalar_t *A)
void SetDerivativeMatrix(int height, const scalar_t *Deriv)
The Deriv matrix is dof x 2, using column-major storage.
Definition: invariants.hpp:184
const scalar_t * Get_dI3b()
Definition: invariants.hpp:780
static scalar_t sign(const scalar_t &a)
Definition: invariants.hpp:29
void Eval_DZt(const scalar_t *Z, scalar_t **DZt_ptr)
Definition: invariants.hpp:686
bool dont(int have_mask) const
Definition: invariants.hpp:81
void Assemble_TProd(scalar_t w, const scalar_t *X, const scalar_t *Y, scalar_t *A)
Definition: invariants.hpp:432
void Assemble_ddI1(scalar_t w, scalar_t *A)
Definition: invariants.hpp:226
Auxiliary class for evaluating the 2x2 matrix invariants and their first and second derivatives...
Definition: invariants.hpp:49
void Assemble_ddI2(scalar_t w, scalar_t *A)
Definition: invariants.hpp:335
void Eval_DZt(const scalar_t *Z, scalar_t **DZt_ptr)
Definition: invariants.hpp:152
void SetJacobian(const scalar_t *Jac)
The Jacobian should use column-major storage.
Definition: invariants.hpp:181
void Assemble_ddI3b(scalar_t w, scalar_t *A)
Auxiliary class used as the default for the second template parameter in the classes InvariantsEvalua...
Definition: invariants.hpp:27
double b
Definition: lissajous.cpp:42
void Assemble_TProd(scalar_t w, const scalar_t *X, scalar_t *A)
Definition: invariants.hpp:460
const scalar_t * Get_dI2b()
Definition: invariants.hpp:216
void SetDerivativeMatrix(int height, const scalar_t *Deriv)
The Deriv matrix is dof x 3, using column-major storage.
Definition: invariants.hpp:737
void Assemble_TProd(scalar_t w, const scalar_t *X, scalar_t *A)
void Assemble_ddI1b(scalar_t w, scalar_t *A)
Definition: invariants.hpp:257
const scalar_t * Get_dI3()
Definition: invariants.hpp:776
void Assemble_ddI2(scalar_t w, scalar_t *A)
Definition: invariants.hpp:948
const scalar_t * Get_dI1()
Definition: invariants.hpp:760
const scalar_t * Get_dI1()
Definition: invariants.hpp:204
const scalar_t * Get_dI1b()
Definition: invariants.hpp:208
const scalar_t * Get_dI1b()
Definition: invariants.hpp:764
double a
Definition: lissajous.cpp:41
const scalar_t * Get_dI2b()
Definition: invariants.hpp:772
void Assemble_TProd(scalar_t w, const scalar_t *X, const scalar_t *Y, scalar_t *A)
const scalar_t * Get_dI2()
Definition: invariants.hpp:768
void Assemble_ddI1(scalar_t w, scalar_t *A)
Definition: invariants.hpp:790
Auxiliary class for evaluating the 3x3 matrix invariants and their first and second derivatives...
Definition: invariants.hpp:494
const scalar_t * Get_dI2()
Definition: invariants.hpp:212
void Assemble_ddI1b(scalar_t w, scalar_t *A)
Definition: invariants.hpp:824
InvariantsEvaluator3D(const scalar_t *Jac=NULL)
The Jacobian should use column-major storage.
Definition: invariants.hpp:720
void SetJacobian(const scalar_t *Jac)
The Jacobian should use column-major storage.
Definition: invariants.hpp:734