TRUST 1.9.8
HPC thermohydraulic platform
Loading...
Searching...
No Matches
Solv_Cholesky.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_Cholesky.h>
17#include <Matrice_Bloc_Sym.h>
18#include <Motcle.h>
19#include <Sparskit.h>
20#include <Lapack.h>
21#include <EFichier.h>
22#include <Param.h>
23
24//Solv_Cholesky::deja_factorise_=0;
25
26Implemente_instanciable_sans_constructeur_ni_destructeur(Solv_Cholesky,"Solv_Cholesky",SolveurSys_base);
27// XD cholesky solveur_sys_base cholesky INHERITS_BRACE Cholesky direct method.
28// XD attr impr rien impr OPT Keyword which may be used to print the resolution time.
29// XD attr quiet rien quiet OPT To disable printing of information
30
31// printOn et readOn
32
34{
35 return s << que_suis_je() << finl;
36}
37
39{
41 {
42 Cerr << finl;
43 Cerr << "Cholesky solver not parallelized in TRUST!!!" << finl;
44 Cerr << "Change your solver." << finl;
45 Cerr << "Try using Petsc Cholesky solver." << finl;
46 exit();
47 }
48 Param param(que_suis_je());
49 set_param(param);
50 param.lire_avec_accolades_depuis(is);
51 return is;
52}
53
55{
56 param.ajouter_non_std("impr",(this));
57 param.ajouter("save_matrice|save_matrix",&save_matrice_);
58 param.ajouter_non_std("quiet",(this));
59}
60
62{
63 int retval = 1;
64
65 if (mot=="impr") fixer_limpr(1);
66 else if (mot=="quiet") fixer_limpr(-1);
67 else retval = -1;
68
69 return retval;
70}
71
73 const DoubleVect& secmem,
74 DoubleVect& solution)
75{
76 Cerr << "Warning, Sparskit based Cholesky solver will be deprecated soon. Consider using PETSc Cholesky solver. Contact trust@cea.fr if you can't for some specific reason." << finl;
77 if(sub_type(Matrice_Morse_Sym,la_matrice))
78 {
79 const Matrice_Morse_Sym& matrice = ref_cast(Matrice_Morse_Sym,la_matrice);
80 return Cholesky(matrice,secmem,solution);
81 }
82 else
83 {
84 if(sub_type(Matrice_Bloc_Sym,la_matrice))
85 {
86 // Conversion d'une matrice au format Matrice_Bloc_Sym au format Matrice Morse
87 if (nouvelle_matrice())
88 ref_cast(Matrice_Bloc_Sym,la_matrice).BlocSymToMatMorseSym(matrice_de_travail);
89 return Cholesky(matrice_de_travail,secmem,solution);
90 }
91 else if(sub_type(Matrice_Bloc,la_matrice))
92 {
93 const Matrice_Bloc& matrice = ref_cast(Matrice_Bloc,la_matrice);
94 if((matrice.nb_bloc_lignes()>1 && ref_cast(Matrice_Morse,matrice.get_bloc(1,0).valeur()).nb_lignes()>0))
95 {
96 Cerr<<"Solv_Cholesky : WARNING : Cholesky by blocks cannot be done"<<finl;
97 exit();
98 return(-1);
99 }
100 const Matrice_Morse_Sym& MB00 = ref_cast(Matrice_Morse_Sym,matrice.get_bloc(0,0).valeur());
101 return Cholesky(MB00,secmem,solution);
102 }
103 else
104 {
105 Cerr<<"Solv_Cholesky : Warning : one is not able to carry out preconditionning for linear systems based on Matrice_Morse_Sym or Matrice_Bloc type matrixes"<<finl;
106 exit();
107 return(-1);
108 }
109 }
110}
111
113 const DoubleVect& secmem,
114 DoubleVect& solution)
115{
116 auto nzmax = matrice.get_tab1().size_array();
117 if (nzmax>std::numeric_limits<int>::max())
118 Process::exit("Cholesky can't solve this system.");
119
120 if (nouvelle_matrice())
121 {
123 // On rend definie la matrice dans le cas sequentiel ou Cholesky est le solveur en pression
124 // En parallele, Cholesky est utilise en preconditionnement local et il ne faut
125 // pas modifier la matrice locale.
126 Cerr << "Order of the matrix : " << matrice.ordre() << finl;
127 // Les champs P1bulle n'ont pas de size_reelle et en parallele ca ne marche pas...
128 const int sz = secmem.size_totale();
129 if(matrice.get_est_definie()==0 && nproc()==1)
130 {
131 Cerr << "Matrix not defined : " << finl;
132 Cerr << "zero value is imposed at point 0" << finl;
133 Matrice_Morse_Sym& mat = ref_cast_non_const(Matrice_Morse_Sym,matrice);
134 mat(0,0)*=1E+6;
135 }
136 matrice.renumerote();
138 }
139
140
141 DoubleVect vecteur(secmem);
142 const int n = secmem.size_totale();//ordre;
143
144 // permutation du second membre
145 // subroutine dvperm (n, x, perm)
146 // SPARSKIT2/FORMATS/unary.f
147 F77NAME(DVPERM)(&n, vecteur.addr(), matrice.permutation().addr());
148
149 char UPLO = 'U'; // A est triangulaire superieure
150 int KD = largeur_de_bande_-1; // largeur de bande sup
151 int LDAB = largeur_de_bande_; //n_;// nb de lg de abd_
152 int LDB = n; //m_;// nb de lg de b
153 int NRHS = 1; // nb de col de b (=vecteur)
154 int INFO = 0;
155
156 // resolution du systeme
157 F77NAME(DPBTRS)(&UPLO, &n, &KD, &NRHS, matrice_bande_factorisee_fortran_.addr() , &LDAB, vecteur.addr(), &LDB, &INFO);
158
159 if(INFO)
160 {
161 Cerr << "Solv_Cholesky::resoudre_systeme error : ";
162 if(INFO<0) Cerr << "F77NAME(DPBTRS) param. " << -INFO << " invalid" << finl;
163 if(INFO>0) Cerr << "singular matrix ! resolution impossible" << finl;
164 exit();
165 };
166 // permutation inverse du resultat
167 // subroutine dvperm (n, x, perm)
168 // SPARSKIT2/FORMATS/unary.f
169 F77NAME(DVPERM)(&n, vecteur.addr(), matrice.permutation_inverse().addr());
170
171 solution = vecteur;
172 solution.echange_espace_virtuel();
173 // test
174 // DoubleVect res;
175 // Cout<<"second membre "<<secmem.norme()<<finl;
176 // Cout<<"solution "<<solution.norme()<<finl;
177 // res = matrice*solution - secmem;
178 // Cout<<"residu "<<res.norme()<<finl;
179
180 return 1;
181}
182
183
184
185int Solv_Cholesky::Fact_Cholesky(const Matrice_Morse_Sym& mat1, const int size_reelle)
186{
188 // stockage de la matrice en format bande FORTRAN
190
191 for (int j=0; j<size_reelle; j++)
192 {
193 int i;
194 i = j+1 - largeur_de_bande_;
195 int k = 0;
196 while (k < largeur_de_bande_)
197 {
198 if (i >= 0)
199 {
200 //Cout<<"i j "<<i<<" "<<j<<" "<<mat1(i,j)<<" "<<me()<<finl;
201 const double tmpa = mat1(i,j);
203 }
204
205 i++;
206 k++;
207 }
208 }
209
210 // Factorisation de Cholesky
211
212 // int DBG=1;
213 // int PRC=0;
214 char UPLO = 'U';
215 int N = size_reelle;
216 int KD = largeur_de_bande_ - 1;// mu nbre de diagonales superieures
217 int LDAB = largeur_de_bande_; //n_
218 int INFO = 0;
219
220
221 Cerr << "Starting factorisation ..." << finl;
222 F77NAME(DPBTRF)(&UPLO, &N, &KD,
223 matrice_bande_factorisee_fortran_.addr() , &LDAB, &INFO);
224 Cerr << "End factorization." << finl;
225
226 // {
227 // double ANORM=1.0;
228 // double RCOND;
229 // ArrOfDouble WORK(3*N);
230 // ArrOfInt IWORK(N);
231 // F77NAME(DPBCON)(&UPLO, &N, &KD,
232 // matrice_bande_factorisee_fortran_.addr() , &LDAB,
233 // &ANORM, &RCOND, WORK.addr(), IWORK.addr(), &INFO);
234 // Cerr<<"//////////// RCOND : "<<RCOND<<finl;
235 // }
236 if(INFO)
237 {
238 Cerr << "Solv_Cholesky::Fact_Cholesky() error : ";
239 if(INFO<0) Cerr << "F77NAME(DPBTRF) param. " << -INFO
240 << " invalid" << finl;
241 if(INFO>0) Cerr << "submatrix of order " << INFO
242 << " not positive definite!" << finl;
243 if (INFO<100)
244 {
245 for (int i=0; i<INFO; i++)
246 Cerr << "A(" <<i<<","<<i<<")="<<mat1(i,i)<<finl;
247 Cerr << "non-positive definite matrix ="<<finl;
248 mat1.imprimer_formatte(Cerr);
249 }
250 exit();
251 };
252
253 // rovisoire : on pourrait vider matrice et matrice_renumerotee
254 return 1;
255
256}
257
258
259
Class defining operators and methods for all reading operation in an input flow (file,...
Definition Entree.h:42
Classe Matrice_Base Classe de base de la hierarchie des matrices.
int nb_bloc_lignes() const
virtual const Matrice & get_bloc(int i, int j) const
Classe Matrice_Morse_Sym Represente une matrice M (creuse) symetrique stockee au format Morse.
ArrOfInt & permutation()
Matrice & matrice_renumerotee()
void renumerote() const
Renumerotation d'une matrice afin de reduire la largeur de bande.
ArrOfInt & permutation_inverse()
Sortie & imprimer_formatte(Sortie &s) const override
Classe Matrice_Morse Represente une matrice M (creuse), non necessairement carree.
int largeur_de_bande() const
Calcule la largeur de bande d'une matrice morse.
int ordre() const override
Renvoie l'ordre de la matrice: - le nombre de lignes si la matrice est carree.
const auto & get_tab1() const
int get_est_definie() const
Une chaine de caractere (Nom) en majuscules.
Definition Motcle.h:26
friend class Entree
Definition Objet_U.h:76
const Nom & que_suis_je() const
renvoie la chaine identifiant la classe.
Definition Objet_U.cpp:104
virtual Entree & readOn(Entree &)
Lecture d'un Objet_U sur un flot d'entree Methode a surcharger.
Definition Objet_U.cpp:293
virtual Sortie & printOn(Sortie &) const
Ecriture de l'objet sur un flot de sortie Methode a surcharger.
Definition Objet_U.cpp:282
Helper class to factorize the readOn method of Objet_U classes.
Definition Param.h:112
void ajouter(const char *keyword, const int *value, Param::Nature nat=Param::OPTIONAL)
Register an integer parameter.
Definition Param.cpp:364
void ajouter_non_std(const char *keyword, const Objet_U *value, Param::Nature nat=Param::OPTIONAL)
Register a keyword handled by Objet_U::lire_motcle_non_standard.
Definition Param.cpp:489
static bool is_parallel()
Definition Process.cpp:110
static int nproc()
renvoie le nombre de processeurs dans le groupe courant Voir Comm_Group::nproc() et PE_Groups::curren...
Definition Process.cpp:104
static void exit(int exit_code=-1)
Routine de sortie de TRUST dans une region Kokkos.
Definition Process.cpp:455
void set_param(Param &param) const override
int resoudre_systeme(const Matrice_Base &, const DoubleVect &, DoubleVect &) override
Matrice_Morse_Sym matrice_de_travail
int Fact_Cholesky(const Matrice_Morse_Sym &, const int)
int Cholesky(const Matrice_Morse_Sym &, const DoubleVect &, DoubleVect &)
ArrOfDouble matrice_bande_factorisee_fortran_
int lire_motcle_non_standard(const Motcle &, Entree &) override
Lecture des parametres de type non simple d'un objet_U a partir d'un flot d'entree.
bool nouvelle_matrice() const
void fixer_limpr(int l)
void fixer_nouvelle_matrice(bool i)
Classe de base des flux de sortie.
Definition Sortie.h:52
_TYPE_ * addr()
_SIZE_ size_totale() const
Definition TRUSTVect.tpp:61
virtual void echange_espace_virtuel(IsExchangeBlocking exchange_type=IsExchangeBlocking::DefaultBlocking, const std::string kernel_name="noname")