MBDyn-1.7.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
y12wrap.h
Go to the documentation of this file.
1 /* $Header: /var/cvs/mbdyn/mbdyn/mbdyn-1.0/libraries/libmbwrap/y12wrap.h,v 1.44 2017/01/12 14:44:25 masarati Exp $ */
2 /*
3  * MBDyn (C) is a multibody analysis code.
4  * http://www.mbdyn.org
5  *
6  * Copyright (C) 1996-2017
7  *
8  * Pierangelo Masarati <masarati@aero.polimi.it>
9  * Paolo Mantegazza <mantegazza@aero.polimi.it>
10  *
11  * Dipartimento di Ingegneria Aerospaziale - Politecnico di Milano
12  * via La Masa, 34 - 20156 Milano, Italy
13  * http://www.aero.polimi.it
14  *
15  * Changing this copyright notice is forbidden.
16  *
17  * This program is free software; you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation (version 2 of the License).
20  *
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with this program; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */
31 
32 /*****************************************************************************
33  * *
34  * Y12 C++ WRAPPER *
35  * *
36  *****************************************************************************/
37 
38 /*
39  * Wrapper for Y12 sparse LU solution
40  * http://www.netlib.org/y12/
41  */
42 
43 /*
44  * Uso:
45  * il Y12SparseSolutionManager rende disponibile lo spazio per la matrice,
46  * la soluzione ed il termine noto. Inoltre rende disponibili
47  * metodi per l'handling della matrice e dei vettori
48  * (inserimento e lettura dei coefficienti). Infine consente la soluzione del
49  * problema lineare mediante fattorizzazione LU e successiva sostituzone.
50  *
51  * Uso consigliato:
52  * // creare un oggetto Y12SparseSolutionManager:
53  * Y12SparseSolutionManager SM(size, workspacesize, pivotfactor);
54  *
55  * // ottenere l'handling alla matrice ed al termine noto
56  * MatrixHandler* pMH = SM.pMatHdl();
57  * VectorHandler* pRH = SM.pResHdl();
58  *
59  * // Ogni volta che si desidera riassemblare la matrice:
60  * // inizializzare il MatrixHandler
61  * SM.MatrReset();
62  *
63  * // Operare sulla matrice e sui vettori
64  * pMH->PutCoef(row, col, coef);
65  * coef = pMH->operator()(row, col);
66  * pRH->PutCoef(row, coef);
67  * coef = pRH->operator()(row);
68  *
69  * // Risolvere il problema; in questa fase si assume che:
70  * // - la matrice sia stata completata;
71  * // - il termine noto sia stato completato.
72  * // In tale caso viene eseguita la fattorizzazione e quindi la soluzione.
73  * // Se la fattorizzazione e' gia' stata compiuta, la matrice non e' piu'
74  * // stata modificata e si e' modificato il termine noto, la chiamata a
75  * // Y12SparseSolutionManager::Solve()
76  * // semplicemente esegue una nuova sostituzione all'indietro.
77  * SM.Solve();
78  *
79  * // Se si desidera modificare la matrice per una nuova soluzione, occorre
80  * // inizializzare di nuovo il MatrixHandler, con:
81  * SM.MatrReset();
82  *
83  * // Per i parametri di inizializzazione e per eventuali modifiche
84  * // fare riferimento al codice sorgente ed alla libreria originaria
85  * // in FORTRAN od al suo porting in C, files <harwlib.f>, <harwlib.c>
86  * // Il pacchetto e' costituito dai files:
87  *
88  * <y12lib.f> libreria originaria in FORTRAN
89  * <y12lib.h> header per <y12lib> con dichiarazione delle funzioni
90  * e dei common
91  * <y12wrap.cc> wrapper in C++
92  * <y12wrap.h> header per <y12wrap.cc>.
93  * E' sufficiente includere questo per usare il wrapper.
94  */
95 
96 #ifndef Y12WRAP_H
97 #define Y12WRAP_H
98 
99 #ifdef USE_Y12
100 
101 #include <iostream>
102 #include <vector>
103 
104 #include "myassert.h"
105 #include "mynewmem.h"
106 #include "except.h"
107 #include "solman.h"
108 #include "submat.h"
109 #include "spmapmh.h"
110 #include "ls.h"
111 
112 /* Y12Solver - begin */
113 
114 /*
115  * Solutore LU per matrici sparse. usa spazio messo a disposizione da altri
116  * e si aspetta le matrici gia' bell'e preparate
117  */
118 
119 class Y12Solver : public LinearSolver {
120 public:
121  class ErrFactorization : public MBDynErrBase {
122  private:
123  integer iErrCode;
124 
125  public:
126  ErrFactorization(integer i, MBDYN_EXCEPT_ARGS_DECL) :
128  NO_OP;
129  };
130 
131  integer iGetErrCode(void) const {
132  return iErrCode;
133  };
135  };
136 
137 private:
138 
139  /*
140  * indices of array iIFLAG
141  */
142  enum {
143  I_1 = 0,
144  I_2 = 1,
145  I_3 = 2,
146  I_4 = 3,
147  I_5 = 4,
148  I_6 = 5,
149  I_7 = 6,
150  I_8 = 7,
151  I_9 = 8,
152  I10 = 9
153  };
154 
155  mutable integer iMaxSize;
156  mutable integer iCurSize;
157 
158  mutable integer *piRow;
159  mutable integer *piCol;
160  mutable doublereal *pdMat;
161 
162  integer *pir, *pic;
163 
164  /*
165  * NOTE: Y12 alters the index arrays :(
166  */
167  bool bDuplicateIndices; /* true if need to duplicate indices */
168  std::vector<integer> iRow;
169  mutable std::vector<integer> iCol;
170 
171  mutable integer iN; /* ordine della matrice */
172  mutable integer iNonZeroes; /* coeff. non nulli */
173 
174  integer *piHA; /* vettore di lavoro */
175  mutable integer iIFLAG[10]; /* vettore di lavoro */
176  doublereal dAFLAG[8]; /* vettore di lavoro */
177  doublereal* pdPIVOT; /* vettore di lavoro */
178 
179  void PutError(integer rc) const; /* scrive l'errore */
180 
181  /* Fattorizza la matrice */
182  void Factor(void);
183 
184 public:
185  /* Costruttore: si limita ad allocare la memoria */
186  Y12Solver(integer iMatOrd, integer iWorkSpaceSize,
187  doublereal* pdTmpRhs,
188  integer iPivotParam, bool bDupInd = false);
189  /* Distruttore */
190  ~Y12Solver(void);
191 
192 #ifdef DEBUG
193  void IsValid(void) const;
194 #endif /* DEBUG */
195 
196  /* Risolve */
197  void Solve(void) const;
198 
199  /* Index Form */
201  std::vector<doublereal>& Ax,
202  std::vector<integer>& Ar,
203  std::vector<integer>& Ac,
204  std::vector<integer>& Ap) const;
205 };
206 
207 /* Y12Solver - end */
208 
209 
210 /* Y12SparseSolutionManager - begin */
211 
212 /*
213  * Gestisce la soluzione del problema; alloca le matrici occorrenti
214  * e gli oggetti dedicati alla gestione delle matrici ed alla soluzione
215  */
216 
217 class Y12SparseSolutionManager : public SolutionManager {
218 protected:
219  integer iMatSize; /* ordine della matrice */
220  std::vector<integer> iRow; /* array di interi con
221  * indici di riga di Y12Solver */
222  std::vector<integer> iCol; /* array di interi con
223  * indici di colonna di Y12Solver */
224  std::vector<integer> iColStart; /* array di interi con
225  * indici di colonna CC */
226  std::vector<doublereal> dMat; /* reali con la matrice */
227  std::vector<doublereal> dVec; /* reali con residuo/soluzione */
228 
229  mutable SpMapMatrixHandler MH; /* sparse MatrixHandler */
230  mutable MyVectorHandler VH; /* puntatore a VectorHandler */
231 
232  /* Fattorizza la matrice (non viene mai chiamato direttamente,
233  * ma da Solve se la matrice ancora non e' stata fattorizzata) */
234  void Factor(void);
235 
236 #ifdef DEBUG
237  /* Usata per il debug */
238  void IsValid(void) const;
239 #endif /* DEBUG */
240 
241  virtual void MakeIndexForm(void);
242 public:
243  /* Costruttore: usa e mette a disposizione matrici che gli sono date */
244  Y12SparseSolutionManager(integer iSize,
245  integer iWorkSpaceSize = 0,
246  const doublereal& dPivotFactor = 1.0,
247  bool bDupInd = false);
248 
249  /* Distruttore: dealloca le matrici e distrugge gli oggetti propri */
250  ~Y12SparseSolutionManager(void);
251 
252  /* Inizializza il gestore delle matrici */
253  void MatrReset(void);
254 
255  /* Risolve il sistema */
256  void Solve(void);
257 
258  /* Rende disponibile l'handler per la matrice */
259  MatrixHandler* pMatHdl(void) const {
260  return &MH;
261  };
262 
263  /* Rende disponibile l'handler per il termine noto */
264  VectorHandler* pResHdl(void) const {
265  return &VH;
266  };
267 
268  /* Rende disponibile l'handler per la soluzione (e' lo stesso
269  * del termine noto, ma concettualmente sono separati) */
270  VectorHandler* pSolHdl(void) const {
271  return &VH;
272  };
273 };
274 
275 /* Y12SparseSolutionManager - end */
276 
277 /* Y12SparseCCSolutionManager - begin */
278 template <class CC>
279 class Y12SparseCCSolutionManager: public Y12SparseSolutionManager {
280 protected:
281  bool CCReady;
283 
284  virtual void MatrReset(void);
285  virtual void MakeIndexForm(void);
286 
287 public:
288  Y12SparseCCSolutionManager(integer Dim, integer /* unused */ = 0,
289  doublereal dPivot = -1.);
290  virtual ~Y12SparseCCSolutionManager(void);
291 
292  /* Inizializzatore "speciale" */
293  virtual void MatrInitialize(void);
294 
295  /* Rende disponibile l'handler per la matrice */
296  virtual MatrixHandler* pMatHdl(void) const;
297 };
298 
299 /* Y12SparseCCSolutionManager - end */
300 
301 #endif /* USE_Y12 */
302 
303 #endif /* Y12WRAP_H */
304 
virtual VectorHandler * pResHdl(void) const =0
#define MBDYN_EXCEPT_ARGS_PASSTHRU
Definition: except.h:55
#define MBDYN_EXCEPT_ARGS_DECL
Definition: except.h:43
#define NO_OP
Definition: myassert.h:74
virtual MatrixHandler * pMatHdl(void) const =0
virtual void MatrReset(void)=0
virtual void Solve(void)=0
virtual void Solve(void) const =0
virtual VectorHandler * pSolHdl(void) const =0
double doublereal
Definition: colamd.c:52
long int integer
Definition: colamd.c:51
virtual void MakeCompactForm(SparseMatrixHandler &mh, std::vector< doublereal > &Ax, std::vector< integer > &Ar, std::vector< integer > &Ac, std::vector< integer > &Ap) const
Definition: ls.cc:123