MFEM  v4.5.1
Finite element discretization library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
tmop_pa_w2.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::InvariantsEvaluator2D::Buffers;
23 
24 static MFEM_HOST_DEVICE inline
25 double EvalW_001(const double *Jpt)
26 {
27  kernels::InvariantsEvaluator2D ie(Args().J(Jpt));
28  return ie.Get_I1();
29 }
30 
31 static MFEM_HOST_DEVICE inline
32 double EvalW_002(const double *Jpt)
33 {
34  kernels::InvariantsEvaluator2D ie(Args().J(Jpt));
35  return 0.5 * ie.Get_I1b() - 1.0;
36 }
37 
38 static MFEM_HOST_DEVICE inline
39 double EvalW_007(const double *Jpt)
40 {
41  kernels::InvariantsEvaluator2D ie(Args().J(Jpt));
42  return ie.Get_I1() * (1.0 + 1.0/ie.Get_I2()) - 4.0;
43 }
44 
45 static MFEM_HOST_DEVICE inline
46 double EvalW_077(const double *Jpt)
47 {
48  kernels::InvariantsEvaluator2D ie(Args().J(Jpt));
49  const double I2b = ie.Get_I2b();
50  return 0.5*(I2b*I2b + 1./(I2b*I2b) - 2.);
51 }
52 
53 static MFEM_HOST_DEVICE inline
54 double EvalW_080(const double *Jpt, double gamma)
55 {
56  return (1.0 - gamma) * EvalW_002(Jpt) + gamma * EvalW_077(Jpt);
57 }
58 
59 MFEM_REGISTER_TMOP_KERNELS(double, EnergyPA_2D,
60  const double metric_normal,
61  const double metric_param,
62  const int mid,
63  const int NE,
64  const DenseTensor &j_,
65  const Array<double> &w_,
66  const Array<double> &b_,
67  const Array<double> &g_,
68  const Vector &x_,
69  const Vector &ones,
70  Vector &energy,
71  const int d1d,
72  const int q1d)
73 {
74  MFEM_VERIFY(mid == 1 || mid == 2 || mid == 7 || mid == 77 || mid == 80,
75  "2D metric not yet implemented!");
76 
77  constexpr int DIM = 2;
78  constexpr int NBZ = 1;
79 
80  const int D1D = T_D1D ? T_D1D : d1d;
81  const int Q1D = T_Q1D ? T_Q1D : q1d;
82 
83  const auto J = Reshape(j_.Read(), DIM, DIM, Q1D, Q1D, NE);
84  const auto b = Reshape(b_.Read(), Q1D, D1D);
85  const auto g = Reshape(g_.Read(), Q1D, D1D);
86  const auto W = Reshape(w_.Read(), Q1D, Q1D);
87  const auto X = Reshape(x_.Read(), D1D, D1D, DIM, NE);
88 
89  auto E = Reshape(energy.Write(), Q1D, Q1D, NE);
90 
91  MFEM_FORALL_2D(e, NE, Q1D, Q1D, NBZ,
92  {
93  constexpr int NBZ = 1;
94  constexpr int MQ1 = T_Q1D ? T_Q1D : T_MAX;
95  constexpr int MD1 = T_D1D ? T_D1D : T_MAX;
96  const int D1D = T_D1D ? T_D1D : d1d;
97  const int Q1D = T_Q1D ? T_Q1D : q1d;
98 
99  MFEM_SHARED double BG[2][MQ1*MD1];
100  MFEM_SHARED double XY[2][NBZ][MD1*MD1];
101  MFEM_SHARED double DQ[4][NBZ][MD1*MQ1];
102  MFEM_SHARED double QQ[4][NBZ][MQ1*MQ1];
103 
104  kernels::internal::LoadX<MD1,NBZ>(e,D1D,X,XY);
105  kernels::internal::LoadBG<MD1,MQ1>(D1D,Q1D,b,g,BG);
106 
107  kernels::internal::GradX<MD1,MQ1,NBZ>(D1D,Q1D,BG,XY,DQ);
108  kernels::internal::GradY<MD1,MQ1,NBZ>(D1D,Q1D,BG,DQ,QQ);
109 
110  MFEM_FOREACH_THREAD(qy,y,Q1D)
111  {
112  MFEM_FOREACH_THREAD(qx,x,Q1D)
113  {
114  const double *Jtr = &J(0,0,qx,qy,e);
115  const double detJtr = kernels::Det<2>(Jtr);
116  const double weight = metric_normal * W(qx,qy) * detJtr;
117 
118  // Jrt = Jtr^{-1}
119  double Jrt[4];
120  kernels::CalcInverse<2>(Jtr, Jrt);
121 
122  // Jpr = X^t.DSh
123  double Jpr[4];
124  kernels::internal::PullGrad<MQ1,NBZ>(Q1D,qx,qy,QQ,Jpr);
125 
126  // Jpt = X^T.DS = (X^T.DSh).Jrt = Jpr.Jrt
127  double Jpt[4];
128  kernels::Mult(2,2,2,Jpr,Jrt,Jpt);
129 
130  // metric->EvalW(Jpt);
131  const double EvalW =
132  mid == 1 ? EvalW_001(Jpt) :
133  mid == 2 ? EvalW_002(Jpt) :
134  mid == 7 ? EvalW_007(Jpt) :
135  mid == 77 ? EvalW_077(Jpt) :
136  mid == 80 ? EvalW_080(Jpt, metric_param) : 0.0;
137 
138  E(qx,qy,e) = weight * EvalW;
139  }
140  }
141  });
142  return energy * ones;
143 }
144 
146 {
147  const int N = PA.ne;
148  const int M = metric->Id();
149  const int D1D = PA.maps->ndof;
150  const int Q1D = PA.maps->nqpt;
151  const int id = (D1D << 4 ) | Q1D;
152  const double mn = metric_normal;
153  const DenseTensor &J = PA.Jtr;
154  const Array<double> &W = PA.ir->GetWeights();
155  const Array<double> &B = PA.maps->B;
156  const Array<double> &G = PA.maps->G;
157  const Vector &O = PA.O;
158  Vector &E = PA.E;
159 
160  double mp = 0.0;
161  if (auto m = dynamic_cast<TMOP_Metric_080 *>(metric)) { mp = m->GetGamma(); }
162 
163  MFEM_LAUNCH_TMOP_KERNEL(EnergyPA_2D,id,mn,mp,M,N,J,W,B,G,X,O,E);
164 }
165 
166 } // namespace mfem
virtual int Id() const
Return the metric ID.
Definition: tmop.hpp:78
struct mfem::TMOP_Integrator::@23 PA
TMOP_QualityMetric * metric
Definition: tmop.hpp:1572
const double * Read(bool on_dev=true) const
Shortcut for mfem::Read( GetMemory(), TotalSize(), on_dev).
Definition: densemat.hpp:1087
constexpr int DIM
virtual double * Write(bool on_dev=true)
Shortcut for mfem::Write(vec.GetMemory(), vec.Size(), on_dev).
Definition: vector.hpp:457
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
double GetLocalStateEnergyPA_2D(const Vector &) const
Definition: tmop_pa_w2.cpp:145
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
kernels::InvariantsEvaluator2D::Buffers Args
Definition: tmop_pa_h2s.cpp:21
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