MFEM  v4.6.0
Finite element discretization library
tmop_pa_w2.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::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 // mu_56 = 0.5*(I2b + 1/I2b) - 1.
46 static MFEM_HOST_DEVICE inline
47 double EvalW_056(const double *Jpt)
48 {
49  kernels::InvariantsEvaluator2D ie(Args().J(Jpt));
50  const double I2b = ie.Get_I2b();
51  return 0.5*(I2b + 1.0/I2b) - 1.0;
52 }
53 
54 static MFEM_HOST_DEVICE inline
55 double EvalW_077(const double *Jpt)
56 {
57  kernels::InvariantsEvaluator2D ie(Args().J(Jpt));
58  const double I2b = ie.Get_I2b();
59  return 0.5*(I2b*I2b + 1./(I2b*I2b) - 2.);
60 }
61 
62 static MFEM_HOST_DEVICE inline
63 double EvalW_080(const double *Jpt, const double *w)
64 {
65  return w[0] * EvalW_002(Jpt) + w[1] * EvalW_077(Jpt);
66 }
67 
68 static MFEM_HOST_DEVICE inline
69 double EvalW_094(const double *Jpt, const double *w)
70 {
71  return w[0] * EvalW_002(Jpt) + w[1] * EvalW_056(Jpt);
72 }
73 
74 MFEM_REGISTER_TMOP_KERNELS(double, EnergyPA_2D,
75  const double metric_normal,
76  const Array<double> &metric_param,
77  const int mid,
78  const int NE,
79  const DenseTensor &j_,
80  const Array<double> &w_,
81  const Array<double> &b_,
82  const Array<double> &g_,
83  const Vector &x_,
84  const Vector &ones,
85  Vector &energy,
86  const int d1d,
87  const int q1d)
88 {
89  MFEM_VERIFY(mid == 1 || mid == 2 || mid == 7 || mid == 77
90  || mid == 80 || mid == 94,
91  "2D 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 b = Reshape(b_.Read(), Q1D, D1D);
101  const auto g = Reshape(g_.Read(), Q1D, D1D);
102  const auto W = Reshape(w_.Read(), Q1D, Q1D);
103  const auto X = Reshape(x_.Read(), D1D, D1D, DIM, NE);
104 
105  auto E = Reshape(energy.Write(), Q1D, Q1D, NE);
106 
107  const double *metric_data = metric_param.Read();
108 
109  mfem::forall_2D_batch(NE, Q1D, Q1D, NBZ, [=] MFEM_HOST_DEVICE (int e)
110  {
111  constexpr int NBZ = 1;
112  constexpr int MQ1 = T_Q1D ? T_Q1D : T_MAX;
113  constexpr int MD1 = T_D1D ? T_D1D : T_MAX;
114  const int D1D = T_D1D ? T_D1D : d1d;
115  const int Q1D = T_Q1D ? T_Q1D : q1d;
116 
117  MFEM_SHARED double BG[2][MQ1*MD1];
118  MFEM_SHARED double XY[2][NBZ][MD1*MD1];
119  MFEM_SHARED double DQ[4][NBZ][MD1*MQ1];
120  MFEM_SHARED double QQ[4][NBZ][MQ1*MQ1];
121 
122  kernels::internal::LoadX<MD1,NBZ>(e,D1D,X,XY);
123  kernels::internal::LoadBG<MD1,MQ1>(D1D,Q1D,b,g,BG);
124 
125  kernels::internal::GradX<MD1,MQ1,NBZ>(D1D,Q1D,BG,XY,DQ);
126  kernels::internal::GradY<MD1,MQ1,NBZ>(D1D,Q1D,BG,DQ,QQ);
127 
128  MFEM_FOREACH_THREAD(qy,y,Q1D)
129  {
130  MFEM_FOREACH_THREAD(qx,x,Q1D)
131  {
132  const double *Jtr = &J(0,0,qx,qy,e);
133  const double detJtr = kernels::Det<2>(Jtr);
134  const double weight = metric_normal * W(qx,qy) * detJtr;
135 
136  // Jrt = Jtr^{-1}
137  double Jrt[4];
138  kernels::CalcInverse<2>(Jtr, Jrt);
139 
140  // Jpr = X^t.DSh
141  double Jpr[4];
142  kernels::internal::PullGrad<MQ1,NBZ>(Q1D,qx,qy,QQ,Jpr);
143 
144  // Jpt = X^T.DS = (X^T.DSh).Jrt = Jpr.Jrt
145  double Jpt[4];
146  kernels::Mult(2,2,2,Jpr,Jrt,Jpt);
147 
148  // metric->EvalW(Jpt);
149  const double EvalW =
150  mid == 1 ? EvalW_001(Jpt) :
151  mid == 2 ? EvalW_002(Jpt) :
152  mid == 7 ? EvalW_007(Jpt) :
153  mid == 77 ? EvalW_077(Jpt) :
154  mid == 80 ? EvalW_080(Jpt, metric_data) :
155  mid == 94 ? EvalW_094(Jpt, metric_data) : 0.0;
156 
157  E(qx,qy,e) = weight * EvalW;
158  }
159  }
160  });
161  return energy * ones;
162 }
163 
165 {
166  const int N = PA.ne;
167  const int M = metric->Id();
168  const int D1D = PA.maps->ndof;
169  const int Q1D = PA.maps->nqpt;
170  const int id = (D1D << 4 ) | Q1D;
171  const double mn = metric_normal;
172  const DenseTensor &J = PA.Jtr;
173  const Array<double> &W = PA.ir->GetWeights();
174  const Array<double> &B = PA.maps->B;
175  const Array<double> &G = PA.maps->G;
176  const Vector &O = PA.O;
177  Vector &E = PA.E;
178 
179  Array<double> mp;
180  if (auto m = dynamic_cast<TMOP_Combo_QualityMetric *>(metric))
181  {
182  m->GetWeights(mp);
183  }
184 
185  MFEM_LAUNCH_TMOP_KERNEL(EnergyPA_2D,id,mn,mp,M,N,J,W,B,G,X,O,E);
186 }
187 
188 } // namespace mfem
const T * Read(bool on_dev=true) const
Shortcut for mfem::Read(a.GetMemory(), a.Size(), on_dev).
Definition: array.hpp:307
struct mfem::TMOP_Integrator::@23 PA
TMOP_QualityMetric * metric
Definition: tmop.hpp:1740
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
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
virtual double * Write(bool on_dev=true)
Shortcut for mfem::Write(vec.GetMemory(), vec.Size(), on_dev).
Definition: vector.hpp:461
double b
Definition: lissajous.cpp:42
void forall_2D_batch(int N, int X, int Y, int BZ, lambda &&body)
Definition: forall.hpp:757
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
double GetLocalStateEnergyPA_2D(const Vector &) const
Definition: tmop_pa_w2.cpp:164
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