MFEM  v4.5.1
Finite element discretization library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
tmop_pa_p3.cpp
Go to the documentation of this file.
1 // Copyright (c) 2010-2022, 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_321 = dI1 + (1/I3)*dI2 - (2*I2/I3b^3)*dI3b
63 static MFEM_HOST_DEVICE inline
64 void EvalP_321(const double *J, double *P)
65 {
66  double B[9];
67  double dI1[9], dI2[9], dI3b[9];
68  kernels::InvariantsEvaluator3D ie(Args().J(J).B(B)
69  .dI1(dI1).dI2(dI2).dI3b(dI3b));
70  double sign_detJ;
71  const double I3 = ie.Get_I3();
72  const double alpha = 1.0/I3;
73  const double beta = -2.*ie.Get_I2()/(I3*ie.Get_I3b(sign_detJ));
74  kernels::Add(3,3, alpha, ie.Get_dI2(), beta, ie.Get_dI3b(sign_detJ), P);
75  kernels::Add(3,3, ie.Get_dI1(), P);
76 }
77 
78 // P_332 = (1-gamma) P_302 + gamma P_315.
79 static MFEM_HOST_DEVICE inline
80 void EvalP_332(const double *J, double gamma, double *P)
81 {
82  double B[9];
83  double dI1b[9], dI2[9], dI2b[9], dI3b[9];
84  kernels::InvariantsEvaluator3D ie(Args()
85  .J(J).B(B)
86  .dI1b(dI1b)
87  .dI2(dI2).dI2b(dI2b)
88  .dI3b(dI3b));
89  const double alpha = (1.0 - gamma) * ie.Get_I1b()/9.;
90  const double beta = (1.0 - gamma) * ie.Get_I2b()/9.;
91  kernels::Add(3,3, alpha, ie.Get_dI2b(), beta, ie.Get_dI1b(), P);
92 
93  double sign_detJ;
94  const double I3b = ie.Get_I3b(sign_detJ);
95  kernels::Add(3,3, gamma * 2.0 * (I3b - 1.0), ie.Get_dI3b(sign_detJ), P);
96 }
97 
98 MFEM_REGISTER_TMOP_KERNELS(void, AddMultPA_Kernel_3D,
99  const double metric_normal,
100  double metric_param,
101  const int mid,
102  const int NE,
103  const DenseTensor &j_,
104  const Array<double> &w_,
105  const Array<double> &b_,
106  const Array<double> &g_,
107  const Vector &x_,
108  Vector &y_,
109  const int d1d,
110  const int q1d)
111 {
112  MFEM_VERIFY(mid == 302 || mid == 303 || mid == 315 ||
113  mid == 321 || mid == 332, "3D metric not yet implemented!");
114 
115  constexpr int DIM = 3;
116  const int D1D = T_D1D ? T_D1D : d1d;
117  const int Q1D = T_Q1D ? T_Q1D : q1d;
118 
119  const auto J = Reshape(j_.Read(), DIM, DIM, Q1D, Q1D, Q1D, NE);
120  const auto W = Reshape(w_.Read(), Q1D, Q1D, Q1D);
121  const auto b = Reshape(b_.Read(), Q1D, D1D);
122  const auto g = Reshape(g_.Read(), Q1D, D1D);
123  const auto X = Reshape(x_.Read(), D1D, D1D, D1D, DIM, NE);
124  auto Y = Reshape(y_.ReadWrite(), D1D, D1D, D1D, DIM, NE);
125 
126  MFEM_FORALL_3D(e, NE, Q1D, Q1D, Q1D,
127  {
128  const int D1D = T_D1D ? T_D1D : d1d;
129  const int Q1D = T_Q1D ? T_Q1D : q1d;
130  constexpr int MQ1 = T_Q1D ? T_Q1D : T_MAX;
131  constexpr int MD1 = T_D1D ? T_D1D : T_MAX;
132 
133  MFEM_SHARED double s_BG[2][MQ1*MD1];
134  MFEM_SHARED double s_DDD[3][MD1*MD1*MD1];
135  MFEM_SHARED double s_DDQ[9][MD1*MD1*MQ1];
136  MFEM_SHARED double s_DQQ[9][MD1*MQ1*MQ1];
137  MFEM_SHARED double s_QQQ[9][MQ1*MQ1*MQ1];
138 
139  kernels::internal::LoadX<MD1>(e,D1D,X,s_DDD);
140  kernels::internal::LoadBG<MD1,MQ1>(D1D,Q1D,b,g,s_BG);
141 
142  kernels::internal::GradX<MD1,MQ1>(D1D,Q1D,s_BG,s_DDD,s_DDQ);
143  kernels::internal::GradY<MD1,MQ1>(D1D,Q1D,s_BG,s_DDQ,s_DQQ);
144  kernels::internal::GradZ<MD1,MQ1>(D1D,Q1D,s_BG,s_DQQ,s_QQQ);
145 
146  MFEM_FOREACH_THREAD(qz,z,Q1D)
147  {
148  MFEM_FOREACH_THREAD(qy,y,Q1D)
149  {
150  MFEM_FOREACH_THREAD(qx,x,Q1D)
151  {
152  const double *Jtr = &J(0,0,qx,qy,qz,e);
153  const double detJtr = kernels::Det<3>(Jtr);
154  const double weight = metric_normal * W(qx,qy,qz) * detJtr;
155 
156  // Jrt = Jtr^{-1}
157  double Jrt[9];
158  kernels::CalcInverse<3>(Jtr, Jrt);
159 
160  // Jpr = X^T.DSh
161  double Jpr[9];
162  kernels::internal::PullGrad<MQ1>(Q1D,qx,qy,qz,s_QQQ,Jpr);
163 
164  // Jpt = X^T.DS = (X^T.DSh).Jrt = Jpr.Jrt
165  double Jpt[9];
166  kernels::Mult(3,3,3, Jpr, Jrt, Jpt);
167 
168  // metric->EvalP(Jpt, P);
169  double P[9];
170  if (mid == 302) { EvalP_302(Jpt, P); }
171  if (mid == 303) { EvalP_303(Jpt, P); }
172  if (mid == 315) { EvalP_315(Jpt, P); }
173  if (mid == 321) { EvalP_321(Jpt, P); }
174  if (mid == 332) { EvalP_332(Jpt, metric_param, P); }
175  for (int i = 0; i < 9; i++) { P[i] *= weight; }
176 
177  // Y += DS . P^t += DSh . (Jrt . P^t)
178  double A[9];
179  kernels::MultABt(3,3,3, Jrt, P, A);
180  kernels::internal::PushGrad<MQ1>(Q1D,qx,qy,qz,A,s_QQQ);
181  }
182  }
183  }
184  MFEM_SYNC_THREAD;
185  kernels::internal::LoadBGt<MD1,MQ1>(D1D,Q1D,b,g,s_BG);
186  kernels::internal::GradZt<MD1,MQ1>(D1D,Q1D,s_BG,s_QQQ,s_DQQ);
187  kernels::internal::GradYt<MD1,MQ1>(D1D,Q1D,s_BG,s_DQQ,s_DDQ);
188  kernels::internal::GradXt<MD1,MQ1>(D1D,Q1D,s_BG,s_DDQ,Y,e);
189  });
190 }
191 
193 {
194  const int N = PA.ne;
195  const int M = metric->Id();
196  const int D1D = PA.maps->ndof;
197  const int Q1D = PA.maps->nqpt;
198  const int id = (D1D << 4 ) | Q1D;
199  const DenseTensor &J = PA.Jtr;
200  const Array<double> &W = PA.ir->GetWeights();
201  const Array<double> &B = PA.maps->B;
202  const Array<double> &G = PA.maps->G;
203  const double mn = metric_normal;
204 
205  double mp = 0.0;
206  if (auto m = dynamic_cast<TMOP_Metric_332 *>(metric)) { mp = m->GetGamma(); }
207 
208  MFEM_LAUNCH_TMOP_KERNEL(AddMultPA_Kernel_3D,id,mn,mp,M,N,J,W,B,G,X,Y);
209 }
210 
211 } // namespace mfem
virtual int Id() const
Return the metric ID.
Definition: tmop.hpp:78
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:1572
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
const double * Read(bool on_dev=true) const
Shortcut for mfem::Read( GetMemory(), TotalSize(), on_dev).
Definition: densemat.hpp:1087
constexpr int DIM
double b
Definition: lissajous.cpp:42
MFEM_REGISTER_TMOP_KERNELS(void, DatcSize, const int NE, const int ncomp, const int sizeidx, const DenseMatrix &w_, const Array< double > &b_, const Vector &x_, DenseTensor &j_, const int d1d, const int q1d)
Definition: tmop_pa_da3.cpp:20
const T * Read(bool on_dev=true) const
Shortcut for mfem::Read(a.GetMemory(), a.Size(), on_dev).
Definition: array.hpp:304
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
void AddMultPA_3D(const Vector &, Vector &) const
Definition: tmop_pa_p3.cpp:192
virtual double * ReadWrite(bool on_dev=true)
Shortcut for mfem::ReadWrite(vec.GetMemory(), vec.Size(), on_dev).
Definition: vector.hpp:465
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:60
Rank 3 tensor (array of matrices)
Definition: densemat.hpp:953
virtual const double * Read(bool on_dev=true) const
Shortcut for mfem::Read(vec.GetMemory(), vec.Size(), on_dev).
Definition: vector.hpp:449
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