Discontinuous Galerkin Library
#include "dg/algorithm.h"
multiply.h
Go to the documentation of this file.
1#pragma once
2
3#include "operator.h"
4#include "dg/functors.h"
5#include "dg/blas1.h"
6#include "tensor.h"
7
13namespace dg
14{
17
19template<class value_type>
23 value_type lambda,
24 value_type t00, value_type t01,
25 value_type t10, value_type t11,
26 value_type in0, value_type in1,
27 value_type mu,
28 value_type& out0, value_type& out1) const
29 {
30 value_type tmp0 = DG_FMA(t00,in0 , t01*in1);
31 value_type tmp1 = DG_FMA(t10,in0 , t11*in1);
32 value_type temp = out1*mu;
33 out1 = DG_FMA( lambda, tmp1, temp);
34 temp = out0*mu;
35 out0 = DG_FMA( lambda, tmp0, temp);
36 }
37};
39template<class value_type>
42 void operator() ( value_type lambda,
43 value_type t00, value_type t01, value_type t02,
44 value_type t10, value_type t11, value_type t12,
45 value_type t20, value_type t21, value_type t22,
46 value_type in0, value_type in1, value_type in2,
47 value_type mu,
48 value_type& out0, value_type& out1, value_type& out2) const
49 {
50 value_type tmp0 = DG_FMA( t00,in0 , (DG_FMA( t01,in1 , t02*in2)));
51 value_type tmp1 = DG_FMA( t10,in0 , (DG_FMA( t11,in1 , t12*in2)));
52 value_type tmp2 = DG_FMA( t20,in0 , (DG_FMA( t21,in1 , t22*in2)));
53 value_type temp = out2*mu;
54 out2 = DG_FMA( lambda, tmp2, temp);
55 temp = out1*mu;
56 out1 = DG_FMA( lambda, tmp1, temp);
57 temp = out0*mu;
58 out0 = DG_FMA( lambda, tmp0, temp);
59 }
60};
62template<class value_type>
65 void operator() ( value_type lambda,
66 value_type t00, value_type t01,
67 value_type t10, value_type t11,
68 value_type in0, value_type in1,
69 value_type mu, value_type& out0, value_type& out1) const
70 {
71 value_type dett = DG_FMA( t00,t11 , (-t10*t01));
72 value_type tmp0 = DG_FMA( in0,t11 , (-in1*t01));
73 value_type tmp1 = DG_FMA( t00,in1 , (-t10*in0));
74 value_type temp = out1*mu;
75 out1 = DG_FMA( lambda, tmp1/dett, temp);
76 temp = out0*mu;
77 out0 = DG_FMA( lambda, tmp0/dett, temp);
78 }
79};
81template<class value_type>
84 void operator() ( value_type lambda,
85 value_type t00, value_type t01, value_type t02,
86 value_type t10, value_type t11, value_type t12,
87 value_type t20, value_type t21, value_type t22,
88 value_type in0, value_type in1, value_type in2,
89 value_type mu,
90 value_type& out0, value_type& out1, value_type& out2) const
91 {
92 value_type dett = det( t00,t01,t02, t10,t11,t12, t20,t21,t22);
93
94 value_type tmp0 = det( in0,t01,t02, in1,t11,t12, in2,t21,t22);
95 value_type tmp1 = det( t00,in0,t02, t10,in1,t12, t20,in2,t22);
96 value_type tmp2 = det( t00,t01,in0, t10,t11,in1, t20,t21,in2);
97 value_type temp = out2*mu;
98 out2 = DG_FMA( lambda, tmp2/dett, temp);
99 temp = out1*mu;
100 out1 = DG_FMA( lambda, tmp1/dett, temp);
101 temp = out0*mu;
102 out0 = DG_FMA( lambda, tmp0/dett, temp);
103 }
104 private:
106 value_type det( value_type t00, value_type t01, value_type t02,
107 value_type t10, value_type t11, value_type t12,
108 value_type t20, value_type t21, value_type t22)const
109 {
110 return t00*DG_FMA(t11, t22, (-t12*t21))
111 -t01*DG_FMA(t10, t22, (-t20*t12))
112 +t02*DG_FMA(t10, t21, (-t20*t11));
113 }
114};
116
119
121template<class value_type>
124 value_type operator() (
125 value_type lambda,
126 value_type v0, value_type v1,
127 value_type t00, value_type t01,
128 value_type t10, value_type t11,
129 value_type mu,
130 value_type w0, value_type w1
131 ) const
132 {
133 value_type tmp0 = DG_FMA(t00,w0 , t01*w1);
134 value_type tmp1 = DG_FMA(t10,w0 , t11*w1);
135 return lambda*mu*DG_FMA(v0,tmp0 , v1*tmp1);
136 }
137};
139template<class value_type>
142 value_type operator() (
143 value_type lambda,
144 value_type v0, value_type v1, value_type v2,
145 value_type t00, value_type t01, value_type t02,
146 value_type t10, value_type t11, value_type t12,
147 value_type t20, value_type t21, value_type t22,
148 value_type mu,
149 value_type w0, value_type w1, value_type w2) const
150 {
151 value_type tmp0 = DG_FMA( t00,w0 , (DG_FMA( t01,w1 , t02*w2)));
152 value_type tmp1 = DG_FMA( t10,w0 , (DG_FMA( t11,w1 , t12*w2)));
153 value_type tmp2 = DG_FMA( t20,w0 , (DG_FMA( t21,w1 , t22*w2)));
154 return lambda*mu*DG_FMA(v0,tmp0 , DG_FMA(v1,tmp1 , v2*tmp2));
155 }
156};
157
159template<class value_type>
161{
163 value_type operator() ( value_type t00, value_type t01,
164 value_type t10, value_type t11) const
165 {
166 return DG_FMA( t00,t11 , (-t10*t01));
167 }
168};
170template<class value_type>
172{
174 value_type operator() ( value_type t00, value_type t01, value_type t02,
175 value_type t10, value_type t11, value_type t12,
176 value_type t20, value_type t21, value_type t22) const
177 {
178 return t00*m_t(t11, t12, t21, t22)
179 -t01*m_t(t10, t12, t20, t22)
180 +t02*m_t(t10, t11, t20, t21);
181 }
182 private:
184};
186
192namespace tensor
193{
194
197
206template<class ContainerType0, class ContainerType1>
207void scal( SparseTensor<ContainerType0>& t, const ContainerType1& mu)
208{
209 unsigned size=t.values().size();
210 for( unsigned i=0; i<size; i++)
211 dg::blas1::pointwiseDot( mu, t.values()[i], t.values()[i]);
212}
213
229template<class ContainerTypeL, class ContainerType0, class ContainerType1, class ContainerType2, class ContainerTypeM, class ContainerType3, class ContainerType4>
230void multiply2d( const ContainerTypeL& lambda, const SparseTensor<ContainerType0>& t, const ContainerType1& in0, const ContainerType2& in1, const ContainerTypeM& mu, ContainerType3& out0, ContainerType4& out1)
231{
233 lambda, t.value(0,0), t.value(0,1),
234 t.value(1,0), t.value(1,1),
235 in0, in1,
236 mu, out0, out1);
237}
238
255template<class ContainerTypeL, class ContainerType0, class ContainerType1, class ContainerType2, class ContainerType3, class ContainerTypeM, class ContainerType4, class ContainerType5, class ContainerType6>
256void multiply3d( const ContainerTypeL& lambda, const SparseTensor<ContainerType0>& t, const ContainerType1& in0, const ContainerType2& in1, const ContainerType3& in2, const ContainerTypeM& mu, ContainerType4& out0, ContainerType5& out1, ContainerType6& out2)
257{
259 lambda, t.value(0,0), t.value(0,1), t.value(0,2),
260 t.value(1,0), t.value(1,1), t.value(1,2),
261 t.value(2,0), t.value(2,1), t.value(2,2),
262 in0, in1, in2,
263 mu, out0, out1, out2);
264}
265
281template<class ContainerTypeL, class ContainerType0, class ContainerType1, class ContainerType2, class ContainerTypeM, class ContainerType3, class ContainerType4>
282void inv_multiply2d( const ContainerTypeL& lambda, const SparseTensor<ContainerType0>& t, const ContainerType1& in0, const ContainerType2& in1, const ContainerTypeM& mu, ContainerType3& out0, ContainerType4& out1)
283{
285 lambda, t.value(0,0), t.value(0,1),
286 t.value(1,0), t.value(1,1),
287 in0, in1,
288 mu, out0, out1);
289}
290
308template<class ContainerTypeL, class ContainerType0, class ContainerType1, class ContainerType2, class ContainerType3, class ContainerTypeM, class ContainerType4, class ContainerType5, class ContainerType6>
309void inv_multiply3d( const ContainerTypeL& lambda, const SparseTensor<ContainerType0>& t, const ContainerType1& in0, const ContainerType2& in1, const ContainerType3& in2, const ContainerTypeM& mu, ContainerType4& out0, ContainerType5& out1, ContainerType6& out2)
310{
312 lambda, t.value(0,0), t.value(0,1), t.value(0,2),
313 t.value(1,0), t.value(1,1), t.value(1,2),
314 t.value(2,0), t.value(2,1), t.value(2,2),
315 in0, in1, in2,
316 mu, out0, out1, out2);
317}
318
328template<class ContainerType>
330{
331 ContainerType det = t.value(0,0);
333 t.value(0,0), t.value(0,1),
334 t.value(1,0), t.value(1,1));
335 return det;
336}
337
348template<class ContainerType>
350{
351 ContainerType det = t.value(0,0);
353 t.value(0,0), t.value(0,1), t.value(0,2),
354 t.value(1,0), t.value(1,1), t.value(1,2),
355 t.value(2,0), t.value(2,1), t.value(2,2));
356 return det;
357}
358
378template<class ContainerType>
379ContainerType volume2d( const SparseTensor<ContainerType>& t)
380{
381 ContainerType vol=determinant2d(t);
383 return vol;
384}
385
405template<class ContainerType>
406ContainerType volume( const SparseTensor<ContainerType>& t)
407{
408 ContainerType vol=determinant(t);
410 return vol;
411}
412
413//For convenience
427template<class ContainerType0, class ContainerType1, class ContainerType2, class ContainerType3, class ContainerType4>
428void multiply2d( const SparseTensor<ContainerType0>& t, const ContainerType1& in0, const ContainerType2& in1, ContainerType3& out0, ContainerType4& out1)
429{
430 multiply2d( 1, t, in0, in1, 0., out0, out1);
431}
432
447template<class ContainerType0, class ContainerType1, class ContainerType2, class ContainerType3, class ContainerType4, class ContainerType5, class ContainerType6>
448void multiply3d( const SparseTensor<ContainerType0>& t, const ContainerType1& in0, const ContainerType2& in1, const ContainerType3& in2, ContainerType4& out0, ContainerType5& out1, ContainerType6& out2)
449{
450 multiply3d( 1., t, in0, in1, in2, 0., out0, out1, out2);
451}
452
465template<class ContainerType0, class ContainerType1, class ContainerType2, class ContainerType3, class ContainerType4>
466void inv_multiply2d( const SparseTensor<ContainerType0>& t, const ContainerType1& in0, const ContainerType2& in1, ContainerType3& out0, ContainerType4& out1)
467{
468 inv_multiply2d( 1., t, in0, in1, out0, out1);
469}
470
486template<class ContainerType0, class ContainerType1, class ContainerType2, class ContainerType3, class ContainerType4, class ContainerType5, class ContainerType6>
487void inv_multiply3d( const SparseTensor<ContainerType0>& t, const ContainerType1& in0, const ContainerType2& in1, const ContainerType3& in2, ContainerType4& out0, ContainerType5& out1, ContainerType6& out2)
488{
489 inv_multiply3d( 1., t, in0, in1, in2, 0., out0, out1, out2);
490}
491
509template<class ContainerTypeL, class ContainerType0, class ContainerType1, class ContainerType2, class ContainerType3, class ContainerTypeM, class ContainerType4, class ContainerType5>
512 const ContainerTypeL& lambda,
513 const ContainerType0& v0,
514 const ContainerType1& v1,
516 const ContainerTypeM& mu,
517 const ContainerType3& w0,
518 const ContainerType4& w1,
520 ContainerType5& y)
521{
525 lambda,
526 v0, v1,
527 t.value(0,0), t.value(0,1),
528 t.value(1,0), t.value(1,1),
529 mu,
530 w0, w1);
531}
532
551template<class ContainerTypeL, class ContainerType0, class ContainerType1, class ContainerType2, class ContainerType3, class ContainerTypeM, class ContainerType4, class ContainerType5, class ContainerType6, class ContainerType7>
554 const ContainerTypeL& lambda,
555 const ContainerType0& v0,
556 const ContainerType1& v1,
557 const ContainerType2& v2,
559 const ContainerTypeM& mu,
560 const ContainerType4& w0,
561 const ContainerType5& w1,
562 const ContainerType6& w2,
564 ContainerType7& y)
565{
569 lambda,
570 v0, v1, v2,
571 t.value(0,0), t.value(0,1), t.value(0,2),
572 t.value(1,0), t.value(1,1), t.value(1,2),
573 t.value(2,0), t.value(2,1), t.value(2,2),
574 mu,
575 w0, w1, w2);
576}
578
579}//namespace tensor
580}//namespace dg
#define DG_DEVICE
Expands to __host__ __device__ if compiled with nvcc else is empty.
Definition: functions.h:9
void transform(const ContainerType1 &x, ContainerType &y, UnaryOp op)
Definition: blas1.h:524
void subroutine(Subroutine f, ContainerType &&x, ContainerTypes &&... xs)
; Customizable and generic blas1 function
Definition: blas1.h:618
void evaluate(ContainerType &y, BinarySubroutine f, Functor g, const ContainerType0 &x0, const ContainerTypes &...xs)
Definition: blas1.h:556
void pointwiseDot(get_value_type< ContainerType > alpha, const ContainerType1 &x1, const ContainerType2 &x2, get_value_type< ContainerType > beta, ContainerType &y)
Definition: blas1.h:336
@ y
y direction
void inv_multiply3d(const ContainerTypeL &lambda, const SparseTensor< ContainerType0 > &t, const ContainerType1 &in0, const ContainerType2 &in1, const ContainerType3 &in2, const ContainerTypeM &mu, ContainerType4 &out0, ContainerType5 &out1, ContainerType6 &out2)
i
Definition: multiply.h:309
ContainerType determinant2d(const SparseTensor< ContainerType > &t)
Definition: multiply.h:329
void scalar_product2d(get_value_type< ContainerType0 > alpha, const ContainerTypeL &lambda, const ContainerType0 &v0, const ContainerType1 &v1, const SparseTensor< ContainerType2 > &t, const ContainerTypeM &mu, const ContainerType3 &w0, const ContainerType4 &w1, get_value_type< ContainerType0 > beta, ContainerType5 &y)
Definition: multiply.h:510
void inv_multiply2d(const ContainerTypeL &lambda, const SparseTensor< ContainerType0 > &t, const ContainerType1 &in0, const ContainerType2 &in1, const ContainerTypeM &mu, ContainerType3 &out0, ContainerType4 &out1)
Definition: multiply.h:282
void multiply2d(const ContainerTypeL &lambda, const SparseTensor< ContainerType0 > &t, const ContainerType1 &in0, const ContainerType2 &in1, const ContainerTypeM &mu, ContainerType3 &out0, ContainerType4 &out1)
Definition: multiply.h:230
ContainerType determinant(const SparseTensor< ContainerType > &t)
Definition: multiply.h:349
ContainerType volume(const SparseTensor< ContainerType > &t)
Definition: multiply.h:406
void multiply3d(const ContainerTypeL &lambda, const SparseTensor< ContainerType0 > &t, const ContainerType1 &in0, const ContainerType2 &in1, const ContainerType3 &in2, const ContainerTypeM &mu, ContainerType4 &out0, ContainerType5 &out1, ContainerType6 &out2)
Definition: multiply.h:256
void scal(SparseTensor< ContainerType0 > &t, const ContainerType1 &mu)
Definition: multiply.h:207
ContainerType volume2d(const SparseTensor< ContainerType > &t)
Definition: multiply.h:379
void scalar_product3d(get_value_type< ContainerType0 > alpha, const ContainerTypeL &lambda, const ContainerType0 &v0, const ContainerType1 &v1, const ContainerType2 &v2, const SparseTensor< ContainerType3 > &t, const ContainerTypeM &mu, const ContainerType4 &w0, const ContainerType5 &w1, const ContainerType6 &w2, get_value_type< ContainerType0 > beta, ContainerType7 &y)
Definition: multiply.h:552
typename TensorTraits< std::decay_t< Vector > >::value_type get_value_type
Definition: tensor_traits.h:38
This is the namespace for all functions and classes defined and used by the discontinuous Galerkin li...
Definition: subroutines.h:245
Definition: functors.h:124
Definition: multiply.h:63
DG_DEVICE void operator()(value_type lambda, value_type t00, value_type t01, value_type t10, value_type t11, value_type in0, value_type in1, value_type mu, value_type &out0, value_type &out1) const
Definition: multiply.h:65
Definition: multiply.h:82
DG_DEVICE void operator()(value_type lambda, value_type t00, value_type t01, value_type t02, value_type t10, value_type t11, value_type t12, value_type t20, value_type t21, value_type t22, value_type in0, value_type in1, value_type in2, value_type mu, value_type &out0, value_type &out1, value_type &out2) const
Definition: multiply.h:84
Class for 2x2 and 3x3 matrices sharing elements.
Definition: tensor.h:66
std::vector< container > & values()
Return write access to the values array.
Definition: tensor.h:166
const container & value(size_t i, size_t j) const
Read access the underlying container.
Definition: tensor.h:156
Definition: multiply.h:161
DG_DEVICE value_type operator()(value_type t00, value_type t01, value_type t10, value_type t11) const
Definition: multiply.h:163
Definition: multiply.h:172
DG_DEVICE value_type operator()(value_type t00, value_type t01, value_type t02, value_type t10, value_type t11, value_type t12, value_type t20, value_type t21, value_type t22) const
Definition: multiply.h:174
Definition: multiply.h:122
DG_DEVICE value_type operator()(value_type lambda, value_type v0, value_type v1, value_type t00, value_type t01, value_type t10, value_type t11, value_type mu, value_type w0, value_type w1) const
Definition: multiply.h:124
Definition: multiply.h:140
DG_DEVICE value_type operator()(value_type lambda, value_type v0, value_type v1, value_type v2, value_type t00, value_type t01, value_type t02, value_type t10, value_type t11, value_type t12, value_type t20, value_type t21, value_type t22, value_type mu, value_type w0, value_type w1, value_type w2) const
Definition: multiply.h:142
Definition: multiply.h:20
DG_DEVICE void operator()(value_type lambda, value_type t00, value_type t01, value_type t10, value_type t11, value_type in0, value_type in1, value_type mu, value_type &out0, value_type &out1) const
Definition: multiply.h:22
Definition: multiply.h:40
DG_DEVICE void operator()(value_type lambda, value_type t00, value_type t01, value_type t02, value_type t10, value_type t11, value_type t12, value_type t20, value_type t21, value_type t22, value_type in0, value_type in1, value_type in2, value_type mu, value_type &out0, value_type &out1, value_type &out2) const
Definition: multiply.h:42
Definition: subroutines.h:22