MFEM  v4.6.0
Finite element discretization library
tmop_pa_w3.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 // mu_302 = I1b * I2b / 9 - 1
25 static MFEM_HOST_DEVICE inline
26 double EvalW_302(const double *J)
27 {
28  double B[9];
29  kernels::InvariantsEvaluator3D ie(Args().J(J).B(B));
30  return ie.Get_I1b()*ie.Get_I2b()/9. - 1.;
31 }
32 
33 // mu_303 = I1b/3 - 1
34 static MFEM_HOST_DEVICE inline
35 double EvalW_303(const double *J)
36 {
37  double B[9];
38  kernels::InvariantsEvaluator3D ie(Args().J(J).B(B));
39  return ie.Get_I1b()/3. - 1.;
40 }
41 
42 // mu_315 = (I3b - 1)^2
43 static MFEM_HOST_DEVICE inline
44 double EvalW_315(const double *J)
45 {
46  double B[9];
47  kernels::InvariantsEvaluator3D ie(Args().J(J).B(B));
48  const double a = ie.Get_I3b() - 1.0;
49  return a*a;
50 }
51 
52 // mu_318 = 0.5 * (I3 + 1/I3) - 1.
53 static MFEM_HOST_DEVICE inline
54 double EvalW_318(const double *J)
55 {
56  double B[9];
57  kernels::InvariantsEvaluator3D ie(Args().J(J).B(B));
58  const double I3 = ie.Get_I3();
59  return 0.5*(I3 + 1.0/I3) - 1.0;
60 }
61 
62 // mu_321 = I1 + I2/I3 - 6
63 static MFEM_HOST_DEVICE inline
64 double EvalW_321(const double *J)
65 {
66  double B[9];
67  kernels::InvariantsEvaluator3D ie(Args().J(J).B(B));
68  return ie.Get_I1() + ie.Get_I2()/ie.Get_I3() - 6.0;
69 }
70 
71 static MFEM_HOST_DEVICE inline
72 double EvalW_332(const double *J, const double *w)
73 {
74  return w[0] * EvalW_302(J) + w[1] * EvalW_315(J);
75 }
76 
77 static MFEM_HOST_DEVICE inline
78 double EvalW_338(const double *J, const double *w)
79 {
80  return w[0] * EvalW_302(J) + w[1] * EvalW_318(J);
81 }
82 
83 MFEM_REGISTER_TMOP_KERNELS(double, EnergyPA_3D,
84  const double metric_normal,
85  const Array<double> &metric_param,
86  const int mid,
87  const int NE,
88  const DenseTensor &j_,
89  const Array<double> &w_,
90  const Array<double> &b_,
91  const Array<double> &g_,
92  const Vector &ones,
93  const Vector &x_,
94  Vector &energy,
95  const int d1d,
96  const int q1d)
97 {
98  MFEM_VERIFY(mid == 302 || mid == 303 || mid == 315 || mid == 318 ||
99  mid == 321 || mid == 332 || mid == 338,
100  "3D metric not yet implemented!");
101 
102  constexpr int DIM = 3;
103  const int D1D = T_D1D ? T_D1D : d1d;
104  const int Q1D = T_Q1D ? T_Q1D : q1d;
105 
106  const auto J = Reshape(j_.Read(), DIM, DIM, Q1D, Q1D, Q1D, NE);
107  const auto b = Reshape(b_.Read(), Q1D, D1D);
108  const auto g = Reshape(g_.Read(), Q1D, D1D);
109  const auto W = Reshape(w_.Read(), Q1D, Q1D, Q1D);
110  const auto X = Reshape(x_.Read(), D1D, D1D, D1D, DIM, NE);
111 
112  auto E = Reshape(energy.Write(), Q1D, Q1D, Q1D, NE);
113 
114  const double *metric_data = metric_param.Read();
115 
116  mfem::forall_3D(NE, Q1D, Q1D, Q1D, [=] MFEM_HOST_DEVICE (int e)
117  {
118  const int D1D = T_D1D ? T_D1D : d1d;
119  const int Q1D = T_Q1D ? T_Q1D : q1d;
120  constexpr int MQ1 = T_Q1D ? T_Q1D : T_MAX;
121  constexpr int MD1 = T_D1D ? T_D1D : T_MAX;
122 
123  MFEM_SHARED double BG[2][MQ1*MD1];
124  MFEM_SHARED double DDD[3][MD1*MD1*MD1];
125  MFEM_SHARED double DDQ[6][MD1*MD1*MQ1];
126  MFEM_SHARED double DQQ[9][MD1*MQ1*MQ1];
127  MFEM_SHARED double QQQ[9][MQ1*MQ1*MQ1];
128 
129  kernels::internal::LoadX<MD1>(e,D1D,X,DDD);
130  kernels::internal::LoadBG<MD1,MQ1>(D1D,Q1D,b,g,BG);
131 
132  kernels::internal::GradX<MD1,MQ1>(D1D,Q1D,BG,DDD,DDQ);
133  kernels::internal::GradY<MD1,MQ1>(D1D,Q1D,BG,DDQ,DQQ);
134  kernels::internal::GradZ<MD1,MQ1>(D1D,Q1D,BG,DQQ,QQQ);
135 
136  MFEM_FOREACH_THREAD(qz,z,Q1D)
137  {
138  MFEM_FOREACH_THREAD(qy,y,Q1D)
139  {
140  MFEM_FOREACH_THREAD(qx,x,Q1D)
141  {
142  const double *Jtr = &J(0,0,qx,qy,qz,e);
143  const double detJtr = kernels::Det<3>(Jtr);
144  const double weight = metric_normal * W(qx,qy,qz) * detJtr;
145 
146  // Jrt = Jtr^{-1}
147  double Jrt[9];
148  kernels::CalcInverse<3>(Jtr, Jrt);
149 
150  // Jpr = X^t.DSh
151  double Jpr[9];
152  kernels::internal::PullGrad<MQ1>(Q1D,qx,qy,qz, QQQ, Jpr);
153 
154  // Jpt = X^t.DS = (X^t.DSh).Jrt = Jpr.Jrt
155  double Jpt[9];
156  kernels::Mult(3,3,3, Jpr, Jrt, Jpt);
157 
158  // metric->EvalW(Jpt);
159  const double EvalW =
160  mid == 302 ? EvalW_302(Jpt) :
161  mid == 303 ? EvalW_303(Jpt) :
162  mid == 315 ? EvalW_315(Jpt) :
163  mid == 318 ? EvalW_318(Jpt) :
164  mid == 321 ? EvalW_321(Jpt) :
165  mid == 332 ? EvalW_332(Jpt, metric_data) :
166  mid == 338 ? EvalW_338(Jpt, metric_data) : 0.0;
167 
168  E(qx,qy,qz,e) = weight * EvalW;
169  }
170  }
171  }
172  });
173  return energy * ones;
174 }
175 
177 {
178  const int N = PA.ne;
179  const int M = metric->Id();
180  const int D1D = PA.maps->ndof;
181  const int Q1D = PA.maps->nqpt;
182  const int id = (D1D << 4 ) | Q1D;
183  const double mn = metric_normal;
184  const DenseTensor &J = PA.Jtr;
185  const Array<double> &W = PA.ir->GetWeights();
186  const Array<double> &B = PA.maps->B;
187  const Array<double> &G = PA.maps->G;
188  const Vector &O = PA.O;
189  Vector &E = PA.E;
190 
191  Array<double> mp;
192  if (auto m = dynamic_cast<TMOP_Combo_QualityMetric *>(metric))
193  {
194  m->GetWeights(mp);
195  }
196 
197  MFEM_LAUNCH_TMOP_KERNEL(EnergyPA_3D,id,mn,mp,M,N,J,W,B,G,O,X,E);
198 }
199 
200 } // 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
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
double GetLocalStateEnergyPA_3D(const Vector &) const
Definition: tmop_pa_w3.cpp:176
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
double a
Definition: lissajous.cpp:41
kernels::InvariantsEvaluator2D::Buffers Args
Definition: tmop_pa_h2s.cpp:21
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