TRUST 1.9.8
HPC thermohydraulic platform
Loading...
Searching...
No Matches
Domaine_Poly_tools.h
1/****************************************************************************
2* Copyright (c) 2025, 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
16#ifndef Domaine_Poly_tools_included
17#define Domaine_Poly_tools_included
18
19#include <TRUSTTrav.h>
20#include <Lapack.h>
21#include <math.h>
22#include <vector>
23
24/* produit matricel et transposee de DoubleTab */
25static inline DoubleTab prod(DoubleTab a, DoubleTab b)
26{
27 int i, j, k, m = a.dimension(0), n = a.dimension(1), p = b.dimension(1);
28 assert(n == b.dimension(0));
29 DoubleTab r(m, p);
30 for (i = 0; i < m; i++)
31 for (j = 0; j < n; j++)
32 for (k = 0; k < p; k++) r(i, k) += a(i, j) * b(j, k);
33 return r;
34}
35static inline DoubleTab transp(DoubleTab a)
36{
37 int i, j, m = a.dimension(0), n = a.dimension(1);
38 DoubleTab r(n, m);
39 for (i = 0; i < m; i++)
40 for (j = 0; j < n; j++) r(j, i) = a(i, j);
41 return r;
42}
43
44/*! @brief Solves the least squares problem ||M.x - b||_2, placing the kernel of M into P and returning the residual.
45 *
46 * This function uses Singular Value Decomposition (SVD) to solve the least squares problem for the equation M.x = b.
47 * It decomposes the matrix M into its constituent parts using the LAPACK routine DGESVD, which is a Fortran subroutine
48 * for computing the SVD of a general rectangular matrix. The kernel of M (null space) is placed into matrix P.
49 *
50 * @param M The input matrix of the linear system.
51 * @param b The right-hand side vector of the linear system.
52 * @param eps The threshold for considering singular values as zero.
53 * @param P Pointer to a matrix where the kernel of M will be stored.
54 * @param x The solution vector that minimizes the least squares problem.
55 * @param S Vector to store the singular values of M.
56 *
57 * @return The residual of the least squares solution, i.e., the Euclidean norm of (M.x - b).
58 */
59static inline double kersol(const DoubleTab& M, DoubleTab& b, double eps, DoubleTab *P, DoubleTab& x, DoubleTab& S)
60{
61 int i, nk, m = M.dimension(0), n = M.dimension(1), k = std::min(m, n), l = std::max(m, n), w = 5 * l, info=-1, iP, jP;
62 double res2 = 0;
63 char a = 'A';
64 //lapack en mode Fortran -> on decompose en fait Mt!!
65 DoubleTab A = M, U(m, m), Vt(n, n), W(w), iS(n, m);
66 S.resize(k);
67 F77NAME(dgesvd)(&a, &a, &n, &m, A.addr(), &n, S.addr(), Vt.addr(), &n, U.addr(), &m, W.addr(), &w, &info);
68 for (i = 0, nk = n; i < k && S(i) > eps * S(0); i++) nk--;
69 if (P) P->resize(n, nk);
70 for (i = 0, jP = -1; i < n; i++)
71 if (i < k && S(i) > eps * S(0)) iS(i, i) = 1 / S(i); //terme diagonal de iS
72 else if (P)
73 for (iP = 0, jP++; iP < n; iP++) (*P)(iP, jP) = Vt(i, iP); //colonne de V -> colonne de P
74 x = prod(transp(Vt), prod(iS, prod(transp(U), b)));
75 DoubleTab res = prod(M, x);
76 for (i = 0; i < m; i++) res2 += std::pow(res(i, 0) - b(i, 0), 2);
77 return sqrt(res2);
78}
79
80/*! @def CRIMP(a)
81 * @brief Compacts a multi-dimensional array by resizing it.
82 *
83 * This macro adjusts the size of a multi-dimensional array `a` by incrementing and then decrementing its first dimension.
84 * It handles arrays with 1, 2, or 3 dimensions.
85 *
86 * @param a The array to be compacted.
87 */
88#define CRIMP(a) a.nb_dim() > 2 ? a.resize(a.dimension(0) + 1, a.dimension(1), a.dimension(2)) : a.nb_dim() > 1 ? a.resize(a.dimension(0) + 1, a.dimension(1)) : a.resize(a.dimension(0) + 1), \
89 a.nb_dim() > 2 ? a.resize(a.dimension(0) - 1, a.dimension(1), a.dimension(2)) : a.nb_dim() > 1 ? a.resize(a.dimension(0) - 1, a.dimension(1)) : a.resize(a.dimension(0) - 1)
90#endif
_TYPE_ * addr()
void resize(_SIZE_ n, RESIZE_OPTIONS opt=RESIZE_OPTIONS::COPY_INIT)
Definition TRUSTTab.tpp:469
_SIZE_ dimension(int d) const
Definition TRUSTTab.tpp:133