MFEM  v4.5.2
Finite element discretization library
tmop_pa_p2.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 "../../general/forall.hpp"
15 #include "../../linalg/kernels.hpp"
16 #include "../../linalg/dinvariants.hpp"
17 
18 namespace mfem
19 {
20 
21 using Args = kernels::InvariantsEvaluator2D::Buffers;
22 
23 static MFEM_HOST_DEVICE inline
24 void EvalP_001(const double *Jpt, double *P)
25 {
26  double dI1[4];
27  kernels::InvariantsEvaluator2D ie(Args().J(Jpt).dI1(dI1));
28  kernels::Set(2,2, 1.0, ie.Get_dI1(), P);
29 }
30 
31 static MFEM_HOST_DEVICE inline
32 void EvalP_002(const double *Jpt, double *P)
33 {
34  double dI1b[4], dI2b[4];
35  kernels::InvariantsEvaluator2D ie(Args().J(Jpt).dI1b(dI1b).dI2b(dI2b));
36  kernels::Set(2,2, 1./2., ie.Get_dI1b(), P);
37 }
38 
39 static MFEM_HOST_DEVICE inline
40 void EvalP_007(const double *Jpt, double *P)
41 {
42  double dI1[4], dI2[4], dI2b[4];
43  kernels::InvariantsEvaluator2D ie(Args().J(Jpt).dI1(dI1)
44  .dI2(dI2).dI2b(dI2b));
45  const double I2 = ie.Get_I2();
46  kernels::Add(2,2, 1.0 + 1.0 / I2, ie.Get_dI1(),
47  -ie.Get_I1() / (I2*I2), ie.Get_dI2(), P);
48 }
49 
50 static MFEM_HOST_DEVICE inline
51 void EvalP_077(const double *Jpt, double *P)
52 {
53  double dI2[4], dI2b[4];
54  kernels::InvariantsEvaluator2D ie(Args().
55  J(Jpt).
56  dI2(dI2).dI2b(dI2b));
57  const double I2 = ie.Get_I2();
58  kernels::Set(2,2, 0.5 * (1.0 - 1.0 / (I2 * I2)), ie.Get_dI2(), P);
59 }
60 
61 static MFEM_HOST_DEVICE inline
62 void EvalP_080(const double *Jpt, double gamma, double *P)
63 {
64  // p_80 = (1-gamma) p_2 + gamma p_77.
65 
66  double dI1b[4], dI2[4], dI2b[4];
67  kernels::InvariantsEvaluator2D ie(Args().J(Jpt).
68  dI1b(dI1b).dI2(dI2).dI2b(dI2b));
69 
70  kernels::Set(2,2, (1.0 - gamma) * 1./2., ie.Get_dI1b(), P);
71 
72  const double I2 = ie.Get_I2();
73  kernels::Add(2,2, gamma * 0.5 * (1.0 - 1.0 / (I2 * I2)), ie.Get_dI2(), P);
74 }
75 
76 MFEM_REGISTER_TMOP_KERNELS(void, AddMultPA_Kernel_2D,
77  const double metric_normal,
78  const double metric_param,
79  const int mid,
80  const int NE,
81  const DenseTensor &j_,
82  const Array<double> &w_,
83  const Array<double> &b_,
84  const Array<double> &g_,
85  const Vector &x_,
86  Vector &y_,
87  const int d1d,
88  const int q1d)
89 {
90  MFEM_VERIFY(mid == 1 || mid == 2 || mid == 7 || mid == 77 || mid == 80,
91  "Metric not yet implemented!");
92 
93  constexpr int DIM = 2;
94  constexpr int NBZ = 1;
95 
96  const int D1D = T_D1D ? T_D1D : d1d;
97  const int Q1D = T_Q1D ? T_Q1D : q1d;
98 
99  const auto J = Reshape(j_.Read(), DIM, DIM, Q1D, Q1D, NE);
100  const auto W = Reshape(w_.Read(), Q1D, Q1D);
101  const auto b = Reshape(b_.Read(), Q1D, D1D);
102  const auto g = Reshape(g_.Read(), Q1D, D1D);
103  auto X = Reshape(x_.Read(), D1D, D1D, DIM, NE);
104  auto Y = Reshape(y_.ReadWrite(), D1D, D1D, DIM, NE);
105 
106  MFEM_FORALL_2D(e, NE, Q1D, Q1D, NBZ,
107  {
108  constexpr int NBZ = 1;
109  constexpr int MQ1 = T_Q1D ? T_Q1D : T_MAX;
110  constexpr int MD1 = T_D1D ? T_D1D : T_MAX;
111  const int D1D = T_D1D ? T_D1D : d1d;
112  const int Q1D = T_Q1D ? T_Q1D : q1d;
113 
114  MFEM_SHARED double BG[2][MQ1*MD1];
115  MFEM_SHARED double XY[2][NBZ][MD1*MD1];
116  MFEM_SHARED double DQ[4][NBZ][MD1*MQ1];
117  MFEM_SHARED double QQ[4][NBZ][MQ1*MQ1];
118 
119  kernels::internal::LoadX<MD1,NBZ>(e,D1D,X,XY);
120  kernels::internal::LoadBG<MD1,MQ1>(D1D,Q1D,b,g,BG);
121 
122  kernels::internal::GradX<MD1,MQ1,NBZ>(D1D,Q1D,BG,XY,DQ);
123  kernels::internal::GradY<MD1,MQ1,NBZ>(D1D,Q1D,BG,DQ,QQ);
124 
125  MFEM_FOREACH_THREAD(qy,y,Q1D)
126  {
127  MFEM_FOREACH_THREAD(qx,x,Q1D)
128  {
129  const double *Jtr = &J(0,0,qx,qy,e);
130  const double detJtr = kernels::Det<2>(Jtr);
131  const double weight = metric_normal * W(qx,qy) * detJtr;
132 
133  // Jrt = Jtr^{-1}
134  double Jrt[4];
135  kernels::CalcInverse<2>(Jtr, Jrt);
136 
137  // Jpr = X{^T}.DSh
138  double Jpr[4];
139  kernels::internal::PullGrad<MQ1,NBZ>(Q1D,qx,qy,QQ,Jpr);
140 
141  // Jpt = X{^T}.DS = (X{^T}.DSh).Jrt = Jpr.Jrt
142  double Jpt[4];
143  kernels::Mult(2,2,2, Jpr, Jrt, Jpt);
144 
145  // metric->EvalP(Jpt, P);
146  double P[4];
147  if (mid == 1) { EvalP_001(Jpt, P); }
148  if (mid == 2) { EvalP_002(Jpt, P); }
149  if (mid == 7) { EvalP_007(Jpt, P); }
150  if (mid == 77) { EvalP_077(Jpt, P); }
151  if (mid == 80) { EvalP_080(Jpt, metric_param, P); }
152  for (int i = 0; i < 4; i++) { P[i] *= weight; }
153 
154  // PMatO += DS . P^t += DSh . (Jrt . P^t)
155  double A[4];
156  kernels::MultABt(2,2,2, Jrt, P, A);
157  kernels::internal::PushGrad<MQ1,NBZ>(Q1D,qx,qy,A,QQ);
158  }
159  }
160  MFEM_SYNC_THREAD;
161  kernels::internal::LoadBGt<MD1,MQ1>(D1D,Q1D,b,g,BG);
162  kernels::internal::GradYt<MD1,MQ1,NBZ>(D1D,Q1D,BG,QQ,DQ);
163  kernels::internal::GradXt<MD1,MQ1,NBZ>(D1D,Q1D,BG,DQ,Y,e);
164  });
165 }
166 
168 {
169  const int N = PA.ne;
170  const int M = metric->Id();
171  const int D1D = PA.maps->ndof;
172  const int Q1D = PA.maps->nqpt;
173  const int id = (D1D << 4 ) | Q1D;
174  const DenseTensor &J = PA.Jtr;
175  const Array<double> &W = PA.ir->GetWeights();
176  const Array<double> &B = PA.maps->B;
177  const Array<double> &G = PA.maps->G;
178  const double mn = metric_normal;
179 
180  double mp = 0.0;
181  if (auto m = dynamic_cast<TMOP_Metric_080 *>(metric)) { mp = m->GetGamma(); }
182 
183  MFEM_LAUNCH_TMOP_KERNEL(AddMultPA_Kernel_2D,id,mn,mp,M,N,J,W,B,G,X,Y);
184 }
185 
186 } // namespace mfem
const T * Read(bool on_dev=true) const
Shortcut for mfem::Read(a.GetMemory(), a.Size(), on_dev).
Definition: array.hpp:307
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:1645
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
virtual const double * Read(bool on_dev=true) const
Shortcut for mfem::Read(vec.GetMemory(), vec.Size(), on_dev).
Definition: vector.hpp:448
constexpr int DIM
const double * Read(bool on_dev=true) const
Shortcut for mfem::Read( GetMemory(), TotalSize(), on_dev).
Definition: densemat.hpp:1112
virtual int Id() const
Return the metric ID.
Definition: tmop.hpp:78
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
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:464
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
Vector data type.
Definition: vector.hpp:60
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
void AddMultPA_2D(const Vector &, Vector &) const
Definition: tmop_pa_p2.cpp:167
Rank 3 tensor (array of matrices)
Definition: densemat.hpp:978