TrioCFD 1.9.9_beta
TrioCFD documentation
Loading...
Searching...
No Matches
Solv_Externe.cpp
1/****************************************************************************
2* Copyright (c) 2026, 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#include <Solv_Externe.h>
17#include <Matrice_Base.h>
18#include <Matrice_Morse_Sym.h>
19#include <Matrice_Bloc_Sym.h>
20#include <Device.h>
21
22Implemente_base_sans_constructeur_ni_destructeur(Solv_Externe,"Solv_Externe",SolveurSys_base);
23
25{
26 //s << chaine_lue_;
27 return s;
28}
29
30// readOn
32{
33 //create_solver(is);
34 return is;
35}
36
38{
39 M = MS;
40 Matrice_Morse mattmp(MS);
41 M.transpose(mattmp);
42 int ordre = M.ordre();
43 for (int i=0; i<ordre; i++)
44 if (M.nb_vois(i))
45 M(i, i) = 0.;
46 M = mattmp + M;
47}
48
49void Solv_Externe::construit_matrice_morse_intermediaire(const Matrice_Base& la_matrice, Matrice_Morse& matrice_morse_intermediaire)
50{
51 if (sub_type(Matrice_Morse_Sym, la_matrice))
52 {
53 // Example: pressure matrix in VEFPreP1B
54 const Matrice_Morse_Sym& matrice_morse_sym = ref_cast(Matrice_Morse_Sym, la_matrice);
55 assert(matrice_morse_sym.get_est_definie());
56 MorseSymToMorse(matrice_morse_sym, matrice_morse_intermediaire);
57 }
58 else if (sub_type(Matrice_Bloc_Sym, la_matrice))
59 {
60 // Example: pressure matrix in VEF P0+P1+Pa
61 const Matrice_Bloc_Sym& matrice = ref_cast(Matrice_Bloc_Sym, la_matrice);
62 // Convert the Matrice_Bloc_Sym to Matrice_Morse_Sym format
63 Matrice_Morse_Sym matrice_morse_sym;
64 matrice.BlocSymToMatMorseSym(matrice_morse_sym);
65 MorseSymToMorse(matrice_morse_sym, matrice_morse_intermediaire);
66 matrice_morse_sym.dimensionner(0, 0); // Destroy the Morse sym matrix now that it is no longer needed
67 }
68 else if (sub_type(Matrice_Morse, la_matrice))
69 {
70 // Example: implicit matrix
71 matrice_symetrique_ = false;
72 }
73 else if (sub_type(Matrice_Bloc, la_matrice))
74 {
75 // Example: pressure matrix in VDF
76 const Matrice_Bloc& matrice_bloc = ref_cast(Matrice_Bloc, la_matrice);
77 if (!sub_type(Matrice_Morse_Sym, matrice_bloc.get_bloc(0, 0).valeur()))
78 matrice_symetrique_ = false;
79 else
80 {
81 // For a direct solver: fix the matrix if it is not defined (in incompressible VDF, nothing was done before...)
82 Matrice_Morse_Sym& mat00 = ref_cast_non_const(Matrice_Morse_Sym, matrice_bloc.get_bloc(0, 0).valeur());
84 mat00(0, 0) *= 2;
85 }
86 matrice_bloc.BlocToMatMorse(matrice_morse_intermediaire);
87 }
88 else
89 {
90 Cerr << "Error, we do not know yet treat a matrix of type " << la_matrice.que_suis_je() << finl;
91 exit();
92 }
93}
94
95const ArrOfInt& Solv_Externe::indice_coeff_to_keep(const Matrice_Morse& matrice_morse)
96{
97 if (!indice_coeff_to_keep_.size_array())
98 {
99 // Build indice_coeff_to_keep_
100 const auto& tab1 = matrice_morse.get_tab1();
101 const int n = tab1.size_array() - 1;
102 auto nnz(tab1[0]);
103 nnz = 0;
104 for (int i = 0; i < n; i++)
105 {
106 if (items_to_keep_[i])
107 nnz += tab1[i + 1] - tab1[i];
108 }
109 indice_coeff_to_keep_.resize((int)nnz);
110 nnz = 0;
111 for (int i = 0; i < n; i++)
112 {
113 if (items_to_keep_[i])
114 {
115 const auto k0 = tab1[i] - 1;
116 const auto k1 = tab1[i + 1] - 1;
117 for (auto k = k0; k < k1; k++)
118 {
119 indice_coeff_to_keep_[(int)nnz] = (int)k;
120 nnz++;
121 }
122 }
123 }
124 }
125 return indice_coeff_to_keep_;
126}
127
129{
130 lhs_.resize(nb_rows_);
131 rhs_.resize(nb_rows_);
132}
133
134template<typename ExecSpace>
135void Solv_Externe::Update_lhs_rhs(const DoubleVect& tab_b, DoubleVect& tab_x)
136{
137 const unsigned int size = tab_b.size_array();
138 auto x = tab_x.template view_ro<1, ExecSpace>();
139 auto b = tab_b.template view_ro<1, ExecSpace>();
140 auto index = static_cast<const ArrOfInt&>(index_).template view_ro<1, ExecSpace>();
141 auto lhs = lhs_.template view_wo<1, ExecSpace>();
142 auto rhs = rhs_.template view_wo<1, ExecSpace>();
143 Kokkos::RangePolicy<ExecSpace> policy(0, size);
144 Kokkos::parallel_for(start_gpu_timer(__KERNEL_NAME__), policy, KOKKOS_LAMBDA(
145 const int i)
146 {
147 int ind = index[i];
148 if (ind != -1)
149 {
150 lhs[ind] = x[i];
151 rhs[ind] = b[i];
152 }
153 });
154 static constexpr bool kernelOnDevice = !std::is_same<ExecSpace, Kokkos::DefaultHostExecutionSpace>::value;
155 end_gpu_timer(__KERNEL_NAME__, kernelOnDevice);
156}
157
158template<typename ExecSpace>
159void Solv_Externe::Update_solution(DoubleVect& tab_x)
160{
161 const unsigned int size = tab_x.size_array();
162 auto index = static_cast<const ArrOfInt&>(index_).template view_ro<1, ExecSpace>();
163 auto lhs = lhs_.template view_ro<1, ExecSpace>();
164 auto x = tab_x.template view_wo<1, ExecSpace>();
165 Kokkos::RangePolicy<ExecSpace> policy(0, size);
166 Kokkos::parallel_for(start_gpu_timer(__KERNEL_NAME__), policy, KOKKOS_LAMBDA(
167 const int i)
168 {
169 int ind = index[i];
170 if (ind != -1)
171 x[i] = lhs[ind];
172 });
173 static constexpr bool kernelOnDevice = !std::is_same<ExecSpace, Kokkos::DefaultHostExecutionSpace>::value;
174 end_gpu_timer(__KERNEL_NAME__, kernelOnDevice);
175}
176
177#ifdef TRUST_USE_GPU
178template void Solv_Externe::Update_lhs_rhs<Kokkos::DefaultExecutionSpace>(const DoubleVect&, DoubleVect&);
180#endif
181template void Solv_Externe::Update_lhs_rhs<Kokkos::DefaultHostExecutionSpace>(const DoubleVect&, DoubleVect&);
183
Class defining operators and methods for all reading operation in an input flow (file,...
Definition Entree.h:42
Matrice_Base class - Base class of the matrix hierarchy.
void BlocSymToMatMorseSym(Matrice_Morse_Sym &mat) const
void BlocToMatMorse(Matrice_Morse &matrix) const
virtual const Matrice & get_bloc(int i, int j) const
Matrice_Morse_Sym class - Represents a sparse symmetric matrix M stored in Morse format.
Matrice_Morse class - Represents a (sparse) matrix M, not necessarily square,.
int nb_vois(int i) const
int ordre() const override
Returns the order of the matrix: - the number of rows if the matrix is square.
virtual Matrice_Morse & transpose(const Matrice_Morse &a)
*this = transpose of a.
void dimensionner(int n, _SIZE_ nnz)
Size the matrix with n lines and n columns and nnz zero-values coefficients.
const auto & get_tab1() const
int get_est_definie() const
const Nom & que_suis_je() const
Returns the string identifying the class.
Definition Objet_U.cpp:104
virtual Entree & readOn(Entree &)
Reads an Objet_U from an input stream. Virtual method to override.
Definition Objet_U.cpp:289
virtual Sortie & printOn(Sortie &) const
Writes the object to an output stream. Virtual method to override.
Definition Objet_U.cpp:278
static void exit(int exit_code=-1)
Exit routine for TRUST within a Kokkos region.
Definition Process.cpp:466
static int je_suis_maitre()
Returns 1 if on the master processor of the current group (i.e. me() == 0), 0 otherwise.
Definition Process.cpp:82
Common stuff for several external solvers.
int matrice_symetrique_
ArrOfDouble lhs_
public_for_cuda void Update_lhs_rhs(const DoubleVect &b, DoubleVect &x)
ArrOfDouble rhs_
void Create_lhs_rhs_onDevice()
void MorseSymToMorse(const Matrice_Morse_Sym &MS, Matrice_Morse &M)
const ArrOfInt & indice_coeff_to_keep(const Matrice_Morse &)
void construit_matrice_morse_intermediaire(const Matrice_Base &, Matrice_Morse &)
void Update_solution(DoubleVect &x)
ArrOfBit items_to_keep_
Definition Solv_tools.h:31
IntTab index_
Definition Solv_tools.h:30
virtual int solveur_direct() const =0
Base class for output streams.
Definition Sortie.h:52
_SIZE_ size_array() const