TrioCFD 1.9.8
TrioCFD documentation
Loading...
Searching...
No Matches
Linear_algebra_tools_impl.h
1/****************************************************************************
2* Copyright (c) 2023, CEA
3* All rights reserved.
4*
5* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
6* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
7* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8* 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
9*
10* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
11* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
12* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
13*
14*****************************************************************************/
15#ifndef Linear_algebra_tools_impl_H
16#define Linear_algebra_tools_impl_H
17#include <Linear_algebra_tools.h>
18
19/*! @brief calcul de la norme Linfini de la matrice Propriete: on note |x| la norme Linfini de x (vecteur ou matrice)
20 *
21 * On a |m * x| <= |m| * |x|
22 * En pratique: c'est le max sur j de la somme sur i de std::fabs(m(i,j))
23 *
24 */
26{
27 double x = std::fabs(m[0][0]) + std::fabs(m[0][1]) + std::fabs(m[0][2]);
28 double y = std::fabs(m[1][0]) + std::fabs(m[1][1]) + std::fabs(m[1][2]);
29 double z = std::fabs(m[2][0]) + std::fabs(m[2][1]) + std::fabs(m[2][2]);
30 double resu = ((x > y) ? x : y);
31 resu = ((resu > z) ? resu : z);
32 return resu;
33}
34
35/*! @brief produit avec de la matrice avec le vecteur x.
36 *
37 */
38inline void Matrice33::produit(const Matrice33& m, const Vecteur3& x, Vecteur3& y)
39{
40 y.init();
41 for (int i=0; i<3; i++)
42 for (int j=0; j<3; j++)
43 y.v[i] += m.m[i][j] * x.v[j];
44}
45
46inline void Matrice33::produit_matriciel(const Matrice33& mat1, const Matrice33& mat2, Matrice33& res)
47{
48 res.init();
49 for (int i=0; i<3; i++)
50 for (int j=0; j<3; j++)
51 for (int k=0; k<3; k++)
52 res.m[i][j] += mat1.m[i][k] * mat2.m[k][j];
53}
54
55inline void Vecteur3::produit_vectoriel(const Vecteur3& x, const Vecteur3& y, Vecteur3& z)
56{
57 z.v[0] = x.v[1] * y.v[2] - x.v[2] * y.v[1];
58 z.v[1] = x.v[2] * y.v[0] - x.v[0] * y.v[2];
59 z.v[2] = x.v[0] * y.v[1] - x.v[1] * y.v[0];
60}
61
62inline double Vecteur3::produit_scalaire(const Vecteur3& x, const Vecteur3& y)
63{
64 double r = 0.;
65 for (int i=0; i<3; i++)
66 r += x.v[i] * y.v[i];
67 return r;
68}
69
70/*! @brief norme L_infini, c'est le max des abs(v[i])
71 *
72 */
74{
75 double x = std::fabs(v[0]);
76 double y = std::fabs(v[1]);
77 double z = std::fabs(v[2]);
78 double resu = ((x > y) ? x : y);
79 resu = ((resu > z) ? resu : z);
80 return resu;
81}
82
83/* ! @brief calcul de la transpose
84 *
85 */
86inline void Matrice33::transpose(const Matrice33& matrice, Matrice33& matrice_transpose)
87{
88 for (int i=0; i<3; i++)
89 for (int j=0; j<3; j++)
90 matrice_transpose.m[i][j] = matrice.m[j][i];
91}
92
93/*! @brief calcul de l'inverse.
94 *
95 * Si le determinant de "matrice" est nul, exit() si exit_on_error (valeur par defaut)
96 * sinon on ne remplit pas matrice_inv et on renvoie 0.
97 * Valeur de retour: determinant de la "matrice" (pas de l'inverse !)
98 *
99 */
100inline double Matrice33::inverse(const Matrice33& matrice, Matrice33& matrice_inv, int exit_on_error)
101{
102 const double a00 = matrice.m[0][0];
103 const double a01 = matrice.m[0][1];
104 const double a02 = matrice.m[0][2];
105 const double a10 = matrice.m[1][0];
106 const double a11 = matrice.m[1][1];
107 const double a12 = matrice.m[1][2];
108 const double a20 = matrice.m[2][0];
109 const double a21 = matrice.m[2][1];
110 const double a22 = matrice.m[2][2];
111 // calcul de valeurs temporaires pour optimisation
112 const double t4 = a00*a11;
113 const double t6 = a00*a12;
114 const double t8 = a01*a10;
115 const double t10 = a02*a10;
116 const double t12 = a01*a20;
117 const double t14 = a02*a20;
118 const double t = t4*a22-t6*a21-t8*a22+t10*a21+t12*a12-t14*a11;
119 if (t==0.)
120 {
121 if (exit_on_error)
122 {
123 Cerr << "Error in Matrice33::inverse: determinant is null" << finl;
125 }
126 // To avoid the compiler to complain about "might be non initialized":
127 for (int i = 0; i < 3; i++)
128 for (int j = 0; j < 3; j++)
129 matrice_inv.m[i][j] = 0.;
130 return 0.;
131 }
132 const double t17 = 1/(t);
133
134 //calcul de la matrice inverse
135 matrice_inv.m[0][0] = (a11*a22-a12*a21)*t17;
136 matrice_inv.m[0][1] = -(a01*a22-a02*a21)*t17;
137 matrice_inv.m[0][2] = -(-a01*a12+a02*a11)*t17;
138 matrice_inv.m[1][0] = (-a10*a22+a12*a20)*t17;
139 matrice_inv.m[1][1] = (a00*a22-t14)*t17;
140 matrice_inv.m[1][2] = -(t6-t10)*t17;
141 matrice_inv.m[2][0] = -(-a10*a21+a11*a20)*t17;
142 matrice_inv.m[2][1] = -(a00*a21-t12)*t17;
143 matrice_inv.m[2][2] = (t4-t8)*t17;
144 return t;
145}
146
147inline Vecteur3 operator-(const Vecteur3& x, const Vecteur3& y)
148{
149 Vecteur3 z;
150 for (int i=0; i<3; i++)
151 z.v[i] = x.v[i] - y.v[i];
152 return z;
153}
154#endif
double norme_Linfini()
calcul de la norme Linfini de la matrice Propriete: on note |x| la norme Linfini de x (vecteur ou mat...
static void produit_matriciel(const Matrice33 &m1, const Matrice33 &m2, Matrice33 &res)
static void produit(const Matrice33 &m, const Vecteur3 &x, Vecteur3 &y)
produit avec de la matrice avec le vecteur x.
void init()
Definition Matrice33.h:71
static void transpose(const Matrice33 &matrice, Matrice33 &matrice_transpose)
double m[3][3]
Definition Matrice33.h:70
static double inverse(const Matrice33 &m, Matrice33 &resu, int exit_on_error=1)
calcul de l'inverse.
static void exit(int exit_code=-1)
Routine de sortie de TRUST dans une region Kokkos.
Definition Process.cpp:455
static double produit_scalaire(const Vecteur3 &x, const Vecteur3 &y)
Vecteur3()
Definition Vecteur3.h:24
static void produit_vectoriel(const Vecteur3 &x, const Vecteur3 &y, Vecteur3 &resu)
double norme_Linfini()
norme L_infini, c'est le max des abs(v[i])
friend Vecteur3 operator-(const Vecteur3 &, const Vecteur3 &)
double v[3]
Definition Vecteur3.h:110
void init()
Definition Vecteur3.h:111