MFEM v4.7.0
Finite element discretization library
Loading...
Searching...
No Matches
tmop_pa_p2.cpp
Go to the documentation of this file.
1// Copyright (c) 2010-2024, 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"
17
18namespace mfem
19{
20
21using Args = kernels::InvariantsEvaluator2D::Buffers;
22
23static MFEM_HOST_DEVICE inline
24void EvalP_001(const real_t *Jpt, real_t *P)
25{
26 real_t dI1[4];
27 kernels::InvariantsEvaluator2D ie(Args().J(Jpt).dI1(dI1));
28 kernels::Set(2,2, 1.0, ie.Get_dI1(), P);
29}
30
31static MFEM_HOST_DEVICE inline
32void EvalP_002(const real_t *Jpt, real_t *P)
33{
34 real_t dI1b[4], dI2b[4];
35 kernels::InvariantsEvaluator2D ie(Args().J(Jpt).dI1b(dI1b).dI2b(dI2b));
36 kernels::Set(2,2, 0.5, ie.Get_dI1b(), P);
37}
38
39static MFEM_HOST_DEVICE inline
40void EvalP_007(const real_t *Jpt, real_t *P)
41{
42 real_t dI1[4], dI2[4], dI2b[4];
43 kernels::InvariantsEvaluator2D ie(Args().J(Jpt).dI1(dI1)
44 .dI2(dI2).dI2b(dI2b));
45 const real_t 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// P_56 = 0.5*(1 - 1/I2b^2)*dI2b.
51static MFEM_HOST_DEVICE inline
52void EvalP_056(const real_t *Jpt, real_t *P)
53{
54 real_t dI2b[4];
55 kernels::InvariantsEvaluator2D ie(Args().J(Jpt).dI2b(dI2b));
56 const real_t I2b = ie.Get_I2b();
57 kernels::Set(2,2, 0.5 * (1.0 - 1.0 / (I2b * I2b)), ie.Get_dI2b(), P);
58}
59
60static MFEM_HOST_DEVICE inline
61void EvalP_077(const real_t *Jpt, real_t *P)
62{
63 real_t dI2[4], dI2b[4];
64 kernels::InvariantsEvaluator2D ie(Args().
65 J(Jpt).
66 dI2(dI2).dI2b(dI2b));
67 const real_t I2 = ie.Get_I2();
68 kernels::Set(2,2, 0.5 * (1.0 - 1.0 / (I2 * I2)), ie.Get_dI2(), P);
69}
70
71// P_80 = w0 P_2 + w1 P_77.
72static MFEM_HOST_DEVICE inline
73void EvalP_080(const real_t *Jpt, const real_t *w, real_t *P)
74{
75 real_t dI1b[4], dI2[4], dI2b[4];
76 kernels::InvariantsEvaluator2D ie(Args().J(Jpt).
77 dI1b(dI1b).dI2(dI2).dI2b(dI2b));
78
79 kernels::Set(2,2, w[0] * 0.5, ie.Get_dI1b(), P);
80
81 const real_t I2 = ie.Get_I2();
82 kernels::Add(2,2, w[1] * 0.5 * (1.0 - 1.0 / (I2 * I2)), ie.Get_dI2(), P);
83}
84
85// P_94 = w0 P_2 + w1 P_56.
86static MFEM_HOST_DEVICE inline
87void EvalP_094(const real_t *Jpt, const real_t *w, real_t *P)
88{
89 real_t dI1b[4], dI2b[4];
90 kernels::InvariantsEvaluator2D ie(Args().J(Jpt).
91 dI1b(dI1b).dI2b(dI2b));
92
93 kernels::Set(2,2, w[0] * 0.5, ie.Get_dI1b(), P);
94
95 const real_t I2b = ie.Get_I2b();
96 kernels::Add(2,2, w[1] * 0.5 * (1.0 - 1.0 / (I2b * I2b)), ie.Get_dI2b(), P);
97}
98
99MFEM_REGISTER_TMOP_KERNELS(void, AddMultPA_Kernel_2D,
100 const real_t metric_normal,
101 const Vector &mc_,
102 const Array<real_t> &metric_param,
103 const int mid,
104 const int NE,
105 const DenseTensor &j_,
106 const Array<real_t> &w_,
107 const Array<real_t> &b_,
108 const Array<real_t> &g_,
109 const Vector &x_,
110 Vector &y_,
111 const int d1d,
112 const int q1d)
113{
114 MFEM_VERIFY(mid == 1 || mid == 2 || mid == 7 || mid == 77
115 || mid == 80 || mid == 94,
116 "2D metric not yet implemented!");
117
118 const bool const_m0 = mc_.Size() == 1;
119
120 constexpr int DIM = 2;
121 constexpr int NBZ = 1;
122
123 const int D1D = T_D1D ? T_D1D : d1d;
124 const int Q1D = T_Q1D ? T_Q1D : q1d;
125
126 const auto MC = const_m0 ?
127 Reshape(mc_.Read(), 1, 1, 1) :
128 Reshape(mc_.Read(), Q1D, Q1D, NE);
129 const auto J = Reshape(j_.Read(), DIM, DIM, Q1D, Q1D, NE);
130 const auto W = Reshape(w_.Read(), Q1D, Q1D);
131 const auto b = Reshape(b_.Read(), Q1D, D1D);
132 const auto g = Reshape(g_.Read(), Q1D, D1D);
133 auto X = Reshape(x_.Read(), D1D, D1D, DIM, NE);
134 auto Y = Reshape(y_.ReadWrite(), D1D, D1D, DIM, NE);
135
136 const real_t *metric_data = metric_param.Read();
137
138 mfem::forall_2D_batch(NE, Q1D, Q1D, NBZ, [=] MFEM_HOST_DEVICE (int e)
139 {
140 constexpr int NBZ = 1;
141 constexpr int MQ1 = T_Q1D ? T_Q1D : T_MAX;
142 constexpr int MD1 = T_D1D ? T_D1D : T_MAX;
143 const int D1D = T_D1D ? T_D1D : d1d;
144 const int Q1D = T_Q1D ? T_Q1D : q1d;
145
146 MFEM_SHARED real_t BG[2][MQ1*MD1];
147 MFEM_SHARED real_t XY[2][NBZ][MD1*MD1];
148 MFEM_SHARED real_t DQ[4][NBZ][MD1*MQ1];
149 MFEM_SHARED real_t QQ[4][NBZ][MQ1*MQ1];
150
151 kernels::internal::LoadX<MD1,NBZ>(e,D1D,X,XY);
152 kernels::internal::LoadBG<MD1,MQ1>(D1D,Q1D,b,g,BG);
153
154 kernels::internal::GradX<MD1,MQ1,NBZ>(D1D,Q1D,BG,XY,DQ);
155 kernels::internal::GradY<MD1,MQ1,NBZ>(D1D,Q1D,BG,DQ,QQ);
156
157 MFEM_FOREACH_THREAD(qy,y,Q1D)
158 {
159 MFEM_FOREACH_THREAD(qx,x,Q1D)
160 {
161 const real_t *Jtr = &J(0,0,qx,qy,e);
162 const real_t detJtr = kernels::Det<2>(Jtr);
163 const real_t m_coef = const_m0 ? MC(0,0,0) : MC(qx,qy,e);
164 const real_t weight = metric_normal * m_coef *
165 W(qx,qy) * detJtr;
166
167 // Jrt = Jtr^{-1}
168 real_t Jrt[4];
169 kernels::CalcInverse<2>(Jtr, Jrt);
170
171 // Jpr = X{^T}.DSh
172 real_t Jpr[4];
173 kernels::internal::PullGrad<MQ1,NBZ>(Q1D,qx,qy,QQ,Jpr);
174
175 // Jpt = X{^T}.DS = (X{^T}.DSh).Jrt = Jpr.Jrt
176 real_t Jpt[4];
177 kernels::Mult(2,2,2, Jpr, Jrt, Jpt);
178
179 // metric->EvalP(Jpt, P);
180 real_t P[4];
181 if (mid == 1) { EvalP_001(Jpt, P); }
182 if (mid == 2) { EvalP_002(Jpt, P); }
183 if (mid == 7) { EvalP_007(Jpt, P); }
184 if (mid == 56) { EvalP_056(Jpt, P); }
185 if (mid == 77) { EvalP_077(Jpt, P); }
186 if (mid == 80) { EvalP_080(Jpt, metric_data, P); }
187 if (mid == 94) { EvalP_094(Jpt, metric_data, P); }
188 for (int i = 0; i < 4; i++) { P[i] *= weight; }
189
190 // PMatO += DS . P^t += DSh . (Jrt . P^t)
191 real_t A[4];
192 kernels::MultABt(2,2,2, Jrt, P, A);
193 kernels::internal::PushGrad<MQ1,NBZ>(Q1D,qx,qy,A,QQ);
194 }
195 }
196 MFEM_SYNC_THREAD;
197 kernels::internal::LoadBGt<MD1,MQ1>(D1D,Q1D,b,g,BG);
198 kernels::internal::GradYt<MD1,MQ1,NBZ>(D1D,Q1D,BG,QQ,DQ);
199 kernels::internal::GradXt<MD1,MQ1,NBZ>(D1D,Q1D,BG,DQ,Y,e);
200 });
201}
202
204{
205 const int N = PA.ne;
206 const int M = metric->Id();
207 const int D1D = PA.maps->ndof;
208 const int Q1D = PA.maps->nqpt;
209 const int id = (D1D << 4 ) | Q1D;
210 const DenseTensor &J = PA.Jtr;
211 const Array<real_t> &W = PA.ir->GetWeights();
212 const Array<real_t> &B = PA.maps->B;
213 const Array<real_t> &G = PA.maps->G;
214 const real_t mn = metric_normal;
215 const Vector &MC = PA.MC;
216
217 Array<real_t> mp;
218 if (auto m = dynamic_cast<TMOP_Combo_QualityMetric *>(metric))
219 {
220 m->GetWeights(mp);
221 }
222
223 MFEM_LAUNCH_TMOP_KERNEL(AddMultPA_Kernel_2D,id,mn,MC,mp,M,N,J,W,B,G,X,Y);
224}
225
226} // namespace mfem
const T * Read(bool on_dev=true) const
Shortcut for mfem::Read(a.GetMemory(), a.Size(), on_dev).
Definition array.hpp:317
Rank 3 tensor (array of matrices)
const real_t * Read(bool on_dev=true) const
Shortcut for mfem::Read( GetMemory(), TotalSize(), on_dev).
TMOP_QualityMetric * metric
Definition tmop.hpp:1744
void AddMultPA_2D(const Vector &, Vector &) const
struct mfem::TMOP_Integrator::@23 PA
virtual int Id() const
Return the metric ID.
Definition tmop.hpp:78
Vector data type.
Definition vector.hpp:80
virtual const real_t * Read(bool on_dev=true) const
Shortcut for mfem::Read(vec.GetMemory(), vec.Size(), on_dev).
Definition vector.hpp:474
virtual real_t * ReadWrite(bool on_dev=true)
Shortcut for mfem::ReadWrite(vec.GetMemory(), vec.Size(), on_dev).
Definition vector.hpp:490
int Size() const
Returns the size of the vector.
Definition vector.hpp:218
real_t b
Definition lissajous.cpp:42
constexpr int DIM
MFEM_HOST_DEVICE void CalcInverse(const T *data, T *inv_data)
Return the inverse of a matrix with given size and data into the matrix with data inv_data.
Definition kernels.hpp:246
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
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
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
MFEM_HOST_DEVICE void Set(const int height, const int width, const real_t 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
MFEM_HOST_DEVICE T Det(const T *data)
Compute the determinant of a square matrix of size dim with given data.
Definition kernels.hpp:237
MFEM_REGISTER_TMOP_KERNELS(void, DatcSize, const int NE, const int ncomp, const int sizeidx, const real_t input_min_size, const DenseMatrix &w_, const Array< real_t > &b_, const Vector &x_, const Vector &nc_reduce, DenseTensor &j_, const int d1d, const int q1d)
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 forall_2D_batch(int N, int X, int Y, int BZ, lambda &&body)
Definition forall.hpp:769
kernels::InvariantsEvaluator2D::Buffers Args
float real_t
Definition config.hpp:43