MFEM  v4.6.0
Finite element discretization library
tmop_pa_p3.cpp
Go to the documentation of this file.
1 // Copyright (c) 2010-2023, 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 #include "../tmop.hpp"
13 #include "tmop_pa.hpp"
14 #include "../linearform.hpp"
15 #include "../../general/forall.hpp"
16 #include "../../linalg/kernels.hpp"
17 #include "../../linalg/dinvariants.hpp"
18 
19 namespace mfem
20 {
21 
22 using Args = kernels::InvariantsEvaluator3D::Buffers;
23 
24 // P_302 = (I1b/9)*dI2b + (I2b/9)*dI1b
25 static MFEM_HOST_DEVICE inline
26 void EvalP_302(const double *J, double *P)
27 {
28  double B[9];
29  double dI1b[9], dI2[9], dI2b[9], dI3b[9];
30  kernels::InvariantsEvaluator3D ie(Args()
31  .J(J).B(B)
32  .dI1b(dI1b)
33  .dI2(dI2).dI2b(dI2b)
34  .dI3b(dI3b));
35  const double alpha = ie.Get_I1b()/9.;
36  const double beta = ie.Get_I2b()/9.;
37  kernels::Add(3,3, alpha, ie.Get_dI2b(), beta, ie.Get_dI1b(), P);
38 }
39 
40 // P_303 = dI1b/3
41 static MFEM_HOST_DEVICE inline
42 void EvalP_303(const double *J, double *P)
43 {
44  double B[9];
45  double dI1b[9], dI3b[9];
46  kernels::InvariantsEvaluator3D ie(Args().J(J).B(B).dI1b(dI1b).dI3b(dI3b));
47  kernels::Set(3,3, 1./3., ie.Get_dI1b(), P);
48 }
49 
50 // P_315 = 2*(I3b - 1)*dI3b
51 static MFEM_HOST_DEVICE inline
52 void EvalP_315(const double *J, double *P)
53 {
54  double dI3b[9];
55  kernels::InvariantsEvaluator3D ie(Args().J(J).dI3b(dI3b));
56 
57  double sign_detJ;
58  const double I3b = ie.Get_I3b(sign_detJ);
59  kernels::Set(3,3, 2.0 * (I3b - 1.0), ie.Get_dI3b(sign_detJ), P);
60 }
61 
62 // P_318 = (I3b - 1/I3b^3)*dI3b.
63 // Uses the I3b form, as dI3 and ddI3 were not implemented at the time.
64 static MFEM_HOST_DEVICE inline
65 void EvalP_318(const double *J, double *P)
66 {
67  double dI3b[9];
68  kernels::InvariantsEvaluator3D ie(Args().J(J).dI3b(dI3b));
69 
70  double sign_detJ;
71  const double I3b = ie.Get_I3b(sign_detJ);
72  kernels::Set(3,3, I3b - 1.0/(I3b * I3b * I3b), ie.Get_dI3b(sign_detJ), P);
73 }
74 
75 // P_321 = dI1 + (1/I3)*dI2 - (2*I2/I3b^3)*dI3b
76 static MFEM_HOST_DEVICE inline
77 void EvalP_321(const double *J, double *P)
78 {
79  double B[9];
80  double dI1[9], dI2[9], dI3b[9];
81  kernels::InvariantsEvaluator3D ie(Args().J(J).B(B)
82  .dI1(dI1).dI2(dI2).dI3b(dI3b));
83  double sign_detJ;
84  const double I3 = ie.Get_I3();
85  const double alpha = 1.0/I3;
86  const double beta = -2.*ie.Get_I2()/(I3*ie.Get_I3b(sign_detJ));
87  kernels::Add(3,3, alpha, ie.Get_dI2(), beta, ie.Get_dI3b(sign_detJ), P);
88  kernels::Add(3,3, ie.Get_dI1(), P);
89 }
90 
91 // P_332 = w0 P_302 + w1 P_315.
92 static MFEM_HOST_DEVICE inline
93 void EvalP_332(const double *J, const double *w, double *P)
94 {
95  double B[9];
96  double dI1b[9], dI2[9], dI2b[9], dI3b[9];
97  kernels::InvariantsEvaluator3D ie(Args()
98  .J(J).B(B)
99  .dI1b(dI1b)
100  .dI2(dI2).dI2b(dI2b)
101  .dI3b(dI3b));
102  const double alpha = w[0] * ie.Get_I1b()/9.;
103  const double beta = w[0]* ie.Get_I2b()/9.;
104  kernels::Add(3,3, alpha, ie.Get_dI2b(), beta, ie.Get_dI1b(), P);
105 
106  double sign_detJ;
107  const double I3b = ie.Get_I3b(sign_detJ);
108  kernels::Add(3,3, w[1] * 2.0 * (I3b - 1.0), ie.Get_dI3b(sign_detJ), P);
109 }
110 
111 // P_338 = w0 P_302 + w1 P_318.
112 static MFEM_HOST_DEVICE inline
113 void EvalP_338(const double *J, const double *w, double *P)
114 {
115  double B[9];
116  double dI1b[9], dI2[9], dI2b[9], dI3b[9];
117  kernels::InvariantsEvaluator3D ie(Args()
118  .J(J).B(B)
119  .dI1b(dI1b)
120  .dI2(dI2).dI2b(dI2b)
121  .dI3b(dI3b));
122  const double alpha = w[0] * ie.Get_I1b()/9.;
123  const double beta = w[0]* ie.Get_I2b()/9.;
124  kernels::Add(3,3, alpha, ie.Get_dI2b(), beta, ie.Get_dI1b(), P);
125 
126  double sign_detJ;
127  const double I3b = ie.Get_I3b(sign_detJ);
128  kernels::Add(3,3, w[1] * (I3b - 1.0/(I3b * I3b * I3b)),
129  ie.Get_dI3b(sign_detJ), P);
130 }
131 
132 MFEM_REGISTER_TMOP_KERNELS(void, AddMultPA_Kernel_3D,
133  const double metric_normal,
134  const Array<double> &metric_param,
135  const int mid,
136  const int NE,
137  const DenseTensor &j_,
138  const Array<double> &w_,
139  const Array<double> &b_,
140  const Array<double> &g_,
141  const Vector &x_,
142  Vector &y_,
143  const int d1d,
144  const int q1d)
145 {
146  MFEM_VERIFY(mid == 302 || mid == 303 || mid == 315 || mid == 318 ||
147  mid == 321 || mid == 332 || mid == 338,
148  "3D metric not yet implemented!");
149 
150  constexpr int DIM = 3;
151  const int D1D = T_D1D ? T_D1D : d1d;
152  const int Q1D = T_Q1D ? T_Q1D : q1d;
153 
154  const auto J = Reshape(j_.Read(), DIM, DIM, Q1D, Q1D, Q1D, NE);
155  const auto W = Reshape(w_.Read(), Q1D, Q1D, Q1D);
156  const auto b = Reshape(b_.Read(), Q1D, D1D);
157  const auto g = Reshape(g_.Read(), Q1D, D1D);
158  const auto X = Reshape(x_.Read(), D1D, D1D, D1D, DIM, NE);
159  auto Y = Reshape(y_.ReadWrite(), D1D, D1D, D1D, DIM, NE);
160 
161  const double *metric_data = metric_param.Read();
162 
163  mfem::forall_3D(NE, Q1D, Q1D, Q1D, [=] MFEM_HOST_DEVICE (int e)
164  {
165  const int D1D = T_D1D ? T_D1D : d1d;
166  const int Q1D = T_Q1D ? T_Q1D : q1d;
167  constexpr int MQ1 = T_Q1D ? T_Q1D : T_MAX;
168  constexpr int MD1 = T_D1D ? T_D1D : T_MAX;
169 
170  MFEM_SHARED double s_BG[2][MQ1*MD1];
171  MFEM_SHARED double s_DDD[3][MD1*MD1*MD1];
172  MFEM_SHARED double s_DDQ[9][MD1*MD1*MQ1];
173  MFEM_SHARED double s_DQQ[9][MD1*MQ1*MQ1];
174  MFEM_SHARED double s_QQQ[9][MQ1*MQ1*MQ1];
175 
176  kernels::internal::LoadX<MD1>(e,D1D,X,s_DDD);
177  kernels::internal::LoadBG<MD1,MQ1>(D1D,Q1D,b,g,s_BG);
178 
179  kernels::internal::GradX<MD1,MQ1>(D1D,Q1D,s_BG,s_DDD,s_DDQ);
180  kernels::internal::GradY<MD1,MQ1>(D1D,Q1D,s_BG,s_DDQ,s_DQQ);
181  kernels::internal::GradZ<MD1,MQ1>(D1D,Q1D,s_BG,s_DQQ,s_QQQ);
182 
183  MFEM_FOREACH_THREAD(qz,z,Q1D)
184  {
185  MFEM_FOREACH_THREAD(qy,y,Q1D)
186  {
187  MFEM_FOREACH_THREAD(qx,x,Q1D)
188  {
189  const double *Jtr = &J(0,0,qx,qy,qz,e);
190  const double detJtr = kernels::Det<3>(Jtr);
191  const double weight = metric_normal * W(qx,qy,qz) * detJtr;
192 
193  // Jrt = Jtr^{-1}
194  double Jrt[9];
195  kernels::CalcInverse<3>(Jtr, Jrt);
196 
197  // Jpr = X^T.DSh
198  double Jpr[9];
199  kernels::internal::PullGrad<MQ1>(Q1D,qx,qy,qz,s_QQQ,Jpr);
200 
201  // Jpt = X^T.DS = (X^T.DSh).Jrt = Jpr.Jrt
202  double Jpt[9];
203  kernels::Mult(3,3,3, Jpr, Jrt, Jpt);
204 
205  // metric->EvalP(Jpt, P);
206  double P[9];
207  if (mid == 302) { EvalP_302(Jpt, P); }
208  if (mid == 303) { EvalP_303(Jpt, P); }
209  if (mid == 315) { EvalP_315(Jpt, P); }
210  if (mid == 318) { EvalP_318(Jpt, P); }
211  if (mid == 321) { EvalP_321(Jpt, P); }
212  if (mid == 332) { EvalP_332(Jpt, metric_data, P); }
213  if (mid == 338) { EvalP_338(Jpt, metric_data, P); }
214  for (int i = 0; i < 9; i++) { P[i] *= weight; }
215 
216  // Y += DS . P^t += DSh . (Jrt . P^t)
217  double A[9];
218  kernels::MultABt(3,3,3, Jrt, P, A);
219  kernels::internal::PushGrad<MQ1>(Q1D,qx,qy,qz,A,s_QQQ);
220  }
221  }
222  }
223  MFEM_SYNC_THREAD;
224  kernels::internal::LoadBGt<MD1,MQ1>(D1D,Q1D,b,g,s_BG);
225  kernels::internal::GradZt<MD1,MQ1>(D1D,Q1D,s_BG,s_QQQ,s_DQQ);
226  kernels::internal::GradYt<MD1,MQ1>(D1D,Q1D,s_BG,s_DQQ,s_DDQ);
227  kernels::internal::GradXt<MD1,MQ1>(D1D,Q1D,s_BG,s_DDQ,Y,e);
228  });
229 }
230 
232 {
233  const int N = PA.ne;
234  const int M = metric->Id();
235  const int D1D = PA.maps->ndof;
236  const int Q1D = PA.maps->nqpt;
237  const int id = (D1D << 4 ) | Q1D;
238  const DenseTensor &J = PA.Jtr;
239  const Array<double> &W = PA.ir->GetWeights();
240  const Array<double> &B = PA.maps->B;
241  const Array<double> &G = PA.maps->G;
242  const double mn = metric_normal;
243 
244  Array<double> mp;
245  if (auto m = dynamic_cast<TMOP_Combo_QualityMetric *>(metric))
246  {
247  m->GetWeights(mp);
248  }
249 
250  MFEM_LAUNCH_TMOP_KERNEL(AddMultPA_Kernel_3D,id,mn,mp,M,N,J,W,B,G,X,Y);
251 }
252 
253 } // namespace mfem
const T * Read(bool on_dev=true) const
Shortcut for mfem::Read(a.GetMemory(), a.Size(), on_dev).
Definition: array.hpp:307
void forall_3D(int N, int X, int Y, int Z, lambda &&body)
Definition: forall.hpp:763
MFEM_HOST_DEVICE void MultABt(const int Aheight, const int Awidth, const int Bheight, const TA *Adata, const TB *Bdata, TC *ABtdata)
Multiply a matrix of size Aheight x Awidth and data Adata with the transpose of a matrix of size Bhei...
Definition: kernels.hpp:363
struct mfem::TMOP_Integrator::@23 PA
TMOP_QualityMetric * metric
Definition: tmop.hpp:1740
MFEM_HOST_DEVICE void Add(const int height, const int width, const TALPHA alpha, const TA *Adata, const TB *Bdata, TC *Cdata)
Compute C = A + alpha*B, where the matrices A, B and C are of size height x width with data Adata...
Definition: kernels.hpp:266
void AddMultPA_3D(const Vector &, Vector &) const
Definition: tmop_pa_p3.cpp:231
virtual const double * Read(bool on_dev=true) const
Shortcut for mfem::Read(vec.GetMemory(), vec.Size(), on_dev).
Definition: vector.hpp:453
constexpr int DIM
Vector beta
const double * Read(bool on_dev=true) const
Shortcut for mfem::Read( GetMemory(), TotalSize(), on_dev).
Definition: densemat.hpp:1230
MFEM_REGISTER_TMOP_KERNELS(void, DatcSize, const int NE, const int ncomp, const int sizeidx, const double input_min_size, const DenseMatrix &w_, const Array< double > &b_, const Vector &x_, const Vector &nc_reduce, DenseTensor &j_, const int d1d, const int q1d)
Definition: tmop_pa_da3.cpp:20
virtual int Id() const
Return the metric ID.
Definition: tmop.hpp:78
double b
Definition: lissajous.cpp:42
MFEM_HOST_DEVICE void Mult(const int height, const int width, const TA *data, const TX *x, TY *y)
Matrix vector multiplication: y = A x, where the matrix A is of size height x width with given data...
Definition: kernels.hpp:163
virtual double * ReadWrite(bool on_dev=true)
Shortcut for mfem::ReadWrite(vec.GetMemory(), vec.Size(), on_dev).
Definition: vector.hpp:469
kernels::InvariantsEvaluator2D::Buffers Args
Definition: tmop_pa_h2s.cpp:21
MFEM_HOST_DEVICE void Set(const int height, const int width, const double alpha, const TA *Adata, TB *Bdata)
Compute B = alpha*A, where the matrices A and B are of size height x width with data Adata and Bdata...
Definition: kernels.hpp:326
const double alpha
Definition: ex15.cpp:369
Vector data type.
Definition: vector.hpp:58
MFEM_HOST_DEVICE DeviceTensor< sizeof...(Dims), T > Reshape(T *ptr, Dims... dims)
Wrap a pointer as a DeviceTensor with automatically deduced template parameters.
Definition: dtensor.hpp:131
Rank 3 tensor (array of matrices)
Definition: densemat.hpp:1096