MBDyn-1.7.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
taucswrap.cc
Go to the documentation of this file.
1 /* $Header: /var/cvs/mbdyn/mbdyn/mbdyn-1.0/libraries/libmbwrap/taucswrap.cc,v 1.26 2017/01/12 14:44:25 masarati Exp $ */
2 /*
3  * HmFe (C) is a FEM analysis code.
4  *
5  * Copyright (C) 1996-2017
6  *
7  * Marco Morandini <morandini@aero.polimi.it>
8  *
9  * Dipartimento di Ingegneria Aerospaziale - Politecnico di Milano
10  * via La Masa, 34 - 20156 Milano, Italy
11  * http://www.aero.polimi.it
12  *
13  * Changing this copyright notice is forbidden.
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17  *
18  */
19 /* December 2001
20  * Modified to add a Sparse matrix in row form and to implement methods
21  * to be used in the parallel MBDyn Solver.
22  *
23  * Copyright (C) 2001-2017
24  *
25  * Giuseppe Quaranta <quaranta@aero.polimi.it>
26  *
27  * Dipartimento di Ingegneria Aerospaziale - Politecnico di Milano
28  * via La Masa, 34 - 20156 Milano, Italy
29  * http://www.aero.polimi.it
30  *
31  */
32 /*
33  * MBDyn (C) is a multibody analysis code.
34  * http://www.mbdyn.org
35  *
36  * Copyright (C) 1996-2017
37  *
38  * Pierangelo Masarati <masarati@aero.polimi.it>
39  * Paolo Mantegazza <mantegazza@aero.polimi.it>
40  *
41  * Dipartimento di Ingegneria Aerospaziale - Politecnico di Milano
42  * via La Masa, 34 - 20156 Milano, Italy
43  * http://www.aero.polimi.it
44  *
45  * Changing this copyright notice is forbidden.
46  *
47  * This program is free software; you can redistribute it and/or modify
48  * it under the terms of the GNU General Public License as published by
49  * the Free Software Foundation (version 2 of the License).
50  *
51  *
52  * This program is distributed in the hope that it will be useful,
53  * but WITHOUT ANY WARRANTY; without even the implied warranty of
54  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
55  * GNU General Public License for more details.
56  *
57  * You should have received a copy of the GNU General Public License
58  * along with this program; if not, write to the Free Software
59  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
60  */
61 
62 /*
63  * Taucs is used by permission; please read its Copyright,
64  * License and Availability note.
65  */
66 
67 #include "mbconfig.h" /* This goes first in every *.c,*.cc file */
68 
69 #ifdef USE_TAUCS
70 #include "solman.h"
71 #include "spmapmh.h"
72 #include "ccmh.h"
73 #include "dirccmh.h"
74 #include "taucswrap.h"
75 
76 
77 /* TaucsSolver - begin */
78 
79 TaucsSolver::TaucsSolver(const integer &size)
80 : LinearSolver(0),
81 iSize(size),
82 Axp(0),
83 Aip(0),
84 App(0),
85 Symbolic(false),
86 Factorization(0)
87 {
88  NO_OP;
89 }
90 
91 TaucsSolver::~TaucsSolver(void)
92 {
93  if (Factorization) {
94  /* de-allocate factorization */
95  taucs_linsolve(NULL,&Factorization,0, NULL,NULL,NULL,NULL);
96  Factorization = 0;
97  }
98 }
99 
100 void
101 TaucsSolver::Reset(void)
102 {
103  if (Factorization) {
104  /* de-allocate factorization */
105  taucs_linsolve(NULL,&Factorization,0, NULL,NULL,NULL,NULL);
106  Factorization = 0;
107  }
108 
109  bHasBeenReset = true;
110 }
111 
112 void
113 TaucsSolver::Solve(void) const
114 {
115  if (bHasBeenReset) {
116  const_cast<TaucsSolver *>(this)->Factor();
117  bHasBeenReset = false;
118  }
119 
120  int status;
121 // char* solve [] = {
122 // "taucs.factor=false",
123 // "taucs.factor.ordering=colamd",
124 // NULL};
125 
126  /*
127  * NOTE: Axp, Aip, App should have been set by * MakeCompactForm()
128  */
129  status = taucs_linsolve(&A, &Factorization, 1,
132  NULL,
133  NULL);
134  if (status != TAUCS_SUCCESS) {
135  silent_cerr("Taucs back-solve failed" << std::endl);
137  }
138 }
139 
140 void
141 TaucsSolver::Factor(void)
142 {
143  int status;
144 
145  /*
146  * NOTE: Axp, Aip, App should have been set by * MakeCompactForm()
147  */
148 // A.n = iSize;
149 // A.m = iSize;
150 // A.flags = TAUCS_DOUBLE;
151 // A.colptr = App;
152 // A.rowind = Aip;
153 // A.values.d = Axp;
154 
155 // if (Factorization) {
156 // /* de-allocate factorization */
157 // taucs_linsolve(NULL,&Factorization,0, NULL,NULL,NULL,NULL);
158 // }
159  //if (Symbolic == 0 && !bPrepareSymbolic()) {
160  char * options_factor_prevordering[] = {
161  "taucs.factor.mf=true",
162  "taucs.factor.ordering=colamd",
163  "taucs.factor.symbolic=false",
164  0};
165  char * options_factor_ordering[] = {
166  "taucs.factor.mf=true",
167  "taucs.factor.ordering=colamd",
168  "taucs.factor.symbolic=true",
169  0};
170  char ** opts;
171  if (Symbolic) {
172  opts = options_factor_prevordering;
173  } else {
174  opts = options_factor_ordering;
175  }
176  /* factor */
177  status = taucs_linsolve(&A, &Factorization, 0, NULL, NULL, opts, NULL);
178  if (status != TAUCS_SUCCESS) {
179  if (Symbolic == true) {
180  /* try to re-do symbolic analisys */
181  Symbolic = false;
182  const_cast<TaucsSolver *>(this)->Factor();
183  } else {
184  /* bail out */
185  silent_cerr("Taucs factorization failed" << std::endl);
187  }
188  }
189  Symbolic = true;
190 }
191 
192 void
193 TaucsSolver::MakeCompactForm(SparseMatrixHandler& mh,
194  std::vector<doublereal>& Ax,
195  std::vector<integer>& Ai,
196  std::vector<integer>& Ac,
197  std::vector<integer>& Ap) const
198 {
199  if (!bHasBeenReset) {
200  return;
201  }
202 
203  mh.MakeCompressedColumnForm(Ax, Ai, Ap, 0);
204 
205  Axp = &(Ax[0]);
206  Aip = &(Ai[0]);
207  App = &(Ap[0]);
208 
209  A.n = iSize;
210  A.m = iSize;
211  A.flags = TAUCS_DOUBLE;
212  A.colptr = App;
213  A.rowind = Aip;
214  A.values.d = Axp;
215 }
216 
217 /* TaucsSolver - end */
218 
219 /* TaucsSparseSolutionManager - begin */
220 
221 TaucsSparseSolutionManager::TaucsSparseSolutionManager(integer Dim)
222 : A(Dim),
223 x(Dim),
224 b(Dim),
225 xVH(Dim, &x[0]),
226 bVH(Dim, &b[0])
227 {
228  SAFENEWWITHCONSTRUCTOR(pLS, TaucsSolver, TaucsSolver(Dim));
229 
230  (void)pLS->pdSetResVec(&b[0]);
231  (void)pLS->pdSetSolVec(&x[0]);
232  pLS->SetSolutionManager(this);
233 }
234 
235 TaucsSparseSolutionManager::~TaucsSparseSolutionManager(void)
236 {
237  NO_OP;
238 }
239 
240 void
241 TaucsSparseSolutionManager::MatrReset(void)
242 {
243  pLS->Reset();
244 }
245 
246 void
247 TaucsSparseSolutionManager::MakeCompressedColumnForm(void)
248 {
249  pLS->MakeCompactForm(A, Ax, Ai, Adummy, Ap);
250 }
251 
252 /* Risolve il sistema Fattorizzazione + Backward Substitution */
253 void
254 TaucsSparseSolutionManager::Solve(void)
255 {
256  MakeCompressedColumnForm();
257  pLS->Solve();
258 }
259 
260 /* Rende disponibile l'handler per la matrice */
262 TaucsSparseSolutionManager::pMatHdl(void) const
263 {
264  return &A;
265 }
266 
267 /* Rende disponibile l'handler per il termine noto */
269 TaucsSparseSolutionManager::pResHdl(void) const
270 {
271  return &bVH;
272 }
273 
274 /* Rende disponibile l'handler per la soluzione */
276 TaucsSparseSolutionManager::pSolHdl(void) const
277 {
278  return &xVH;
279 }
280 
281 /* TaucsSparseSolutionManager - end */
282 
283 template <class CC>
284 TaucsSparseCCSolutionManager<CC>::TaucsSparseCCSolutionManager(integer Dim)
285 : TaucsSparseSolutionManager(Dim),
286 CCReady(false),
287 Ac(0)
288 {
289  NO_OP;
290 }
291 
292 template <class CC>
293 TaucsSparseCCSolutionManager<CC>::~TaucsSparseCCSolutionManager(void)
294 {
295  if (Ac) {
296  SAFEDELETE(Ac);
297  }
298 }
299 
300 template <class CC>
301 void
302 TaucsSparseCCSolutionManager<CC>::MatrReset(void)
303 {
304  /* FIXME */
305  pLS->Reset();
306 }
307 
308 template <class CC>
309 void
310 TaucsSparseCCSolutionManager<CC>::MakeCompressedColumnForm(void)
311 {
312  if (!CCReady) {
313  pLS->MakeCompactForm(A, Ax, Ai, Adummy, Ap);
314 
315  ASSERT(Ac == 0);
316 
317  SAFENEWWITHCONSTRUCTOR(Ac, CC, CC(Ax, Ai, Ap));
318 
319  CCReady = true;
320  }
321 }
322 
323 /* Inizializzatore "speciale" */
324 template <class CC>
325 void
326 TaucsSparseCCSolutionManager<CC>::MatrInitialize(void)
327 {
328  SAFEDELETE(Ac);
329  Ac = 0;
330 
331  CCReady = false;
332 
333  MatrReset();
334 }
335 
336 /* Rende disponibile l'handler per la matrice */
337 template <class CC>
339 TaucsSparseCCSolutionManager<CC>::pMatHdl(void) const
340 {
341  if (!CCReady) {
342  return &A;
343  }
344 
345  ASSERT(Ac != 0);
346  return Ac;
347 }
348 
349 template class TaucsSparseCCSolutionManager<CColMatrixHandler<0> >;
350 template class TaucsSparseCCSolutionManager<DirCColMatrixHandler<0> >;
351 
352 /* TaucsSparseCCSolutionManager - end */
353 
354 #endif /* USE_TAUCS */
355 
virtual integer MakeCompressedColumnForm(doublereal *const Ax, integer *const Ai, integer *const Ap, int offset=0) const =0
#define MBDYN_EXCEPT_ARGS
Definition: except.h:63
#define NO_OP
Definition: myassert.h:74
void Reset(scalar_func_type &d)
Definition: gradient.h:2836
Definition: mbdyn.h:76
#define ASSERT(expression)
Definition: colamd.c:977
doublereal * pdSol
Definition: ls.h:75
#define SAFENEWWITHCONSTRUCTOR(pnt, item, constructor)
Definition: mynewmem.h:698
doublereal * pdRhs
Definition: ls.h:74
long int integer
Definition: colamd.c:51
#define SAFEDELETE(pnt)
Definition: mynewmem.h:710