MBDyn-1.7.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
cctest.cc
Go to the documentation of this file.
1 /* $Header: /var/cvs/mbdyn/mbdyn/mbdyn-1.0/libraries/libmbwrap/cctest.cc,v 1.28 2017/01/12 14:44:25 masarati Exp $ */
2 /*
3  * This library comes with MBDyn (C), a multibody analysis code.
4  * http://www.mbdyn.org
5  *
6  * Copyright (C) 1996-2017
7  *
8  * Pierangelo Masarati <masarati@aero.polimi.it>
9  *
10  * Dipartimento di Ingegneria Aerospaziale - Politecnico di Milano
11  * via La Masa, 34 - 20156 Milano, Italy
12  * http://www.aero.polimi.it
13  *
14  * Changing this copyright notice is forbidden.
15  *
16  * This program is free software; you can redistribute it and/or modify
17  * it under the terms of the GNU General Public License as published by
18  * the Free Software Foundation (version 2 of the License).
19  *
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24  * GNU General Public License for more details.
25  *
26  * You should have received a copy of the GNU General Public License
27  * along with this program; if not, write to the Free Software
28  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29  */
30 
31 #include "mbconfig.h"
32 
33 #include <cstring>
34 #include <stdlib.h>
35 #include <unistd.h>
36 #include <iostream>
37 #include <iomanip>
38 #include "ac/getopt.h"
39 
40 #include "solman.h"
41 #include "spmapmh.h"
42 #include "ccmh.h"
43 #include "dirccmh.h"
44 #include "y12wrap.h"
45 #include "harwrap.h"
46 #include "mschwrap.h"
47 #include "umfpackwrap.h"
48 #include "parsuperluwrap.h"
49 #include "superluwrap.h"
50 
51 const char *solvers[] = {
52 #ifdef USE_UMFPACK
53  "umfpack",
54 #endif /* USE_UMFPACK */
55 #ifdef USE_Y12
56  "y12",
57 #endif /* USE_Y12 */
58 #ifdef USE_SUPERLU
59  "superlu",
60 #endif /* USE_SUPERLU */
61  NULL
62 };
63 
64 static void
65 usage(void)
66 {
67  std::cerr << "usage: cctest [-d] [-m <solver>] [-t <nthreads>]"
68  << std::endl;
69 
70  if (!solvers[0]) {
71  std::cerr << "\tno solvers available!!!" << std::endl;
72 
73  } else {
74  std::cerr << "\t<solver>={" << solvers[0];
75  for (unsigned i = 1; solvers[i]; i++) {
76  std::cerr << "|" << solvers[i];
77  }
78  std::cerr << "}" << std::endl;
79  }
80 
81  exit(EXIT_FAILURE);
82 }
83 
84 int
85 main(int argc, char *argv[])
86 {
87  SolutionManager *pSM = NULL;
88  char *solver = NULL;
89  bool dir(false);
90  unsigned nt = 1;
91  const int size(3);
92 
93  while (1) {
94  int opt = getopt(argc, argv, "dm:t:");
95 
96  if (opt == EOF) {
97  break;
98  }
99 
100  switch (opt) {
101  case 'd':
102  dir = true;
103  break;
104 
105  case 'm':
106  solver = optarg;
107  break;
108 
109  case 't':
110  nt = atoi(optarg);
111  if (nt < 1) {
112  nt = 1;
113  }
114  break;
115 
116  default:
117  usage();
118  }
119  }
120 
121  if (solver == NULL) {
122  usage();
123  }
124 
125  if (strcasecmp(solver, "superlu") == 0) {
126 #ifdef USE_SUPERLU
127 #ifdef USE_SUPERLU_MT
128  if (dir) {
129  typedef ParSuperLUSparseCCSolutionManager<DirCColMatrixHandler<0> > CCMH;
130  SAFENEWWITHCONSTRUCTOR(pSM, CCMH, CCMH(nt, size));
131 
132  } else {
133  typedef ParSuperLUSparseCCSolutionManager<CColMatrixHandler<0> > CCMH;
134  SAFENEWWITHCONSTRUCTOR(pSM, CCMH, CCMH(nt, size));
135  }
136 #else /* !USE_SUPERLU_MT */
137  if (dir) {
138  typedef SuperLUSparseCCSolutionManager<DirCColMatrixHandler<0> > CCMH;
139  SAFENEWWITHCONSTRUCTOR(pSM, CCMH, CCMH(size));
140 
141  } else {
142  typedef SuperLUSparseCCSolutionManager<CColMatrixHandler<0> > CCMH;
143  SAFENEWWITHCONSTRUCTOR(pSM, CCMH, CCMH(size));
144  }
145 #endif /* USE_SUPERLU_MT */
146 #else /* !USE_SUPERLU */
147  std::cerr << "need --with-superlu to use SuperLU library"
148  << std::endl;
149  usage();
150 #endif /* !USE_SUPERLU */
151 
152  } else if (strcasecmp(solver, "y12") == 0) {
153 #ifdef USE_Y12
154  if (dir) {
155  typedef Y12SparseCCSolutionManager<DirCColMatrixHandler<1> > CCMH;
156  SAFENEWWITHCONSTRUCTOR(pSM, CCMH, CCMH(size));
157 
158  } else {
159  typedef Y12SparseCCSolutionManager<CColMatrixHandler<1> > CCMH;
160  SAFENEWWITHCONSTRUCTOR(pSM, CCMH, CCMH(size));
161  }
162 #else /* !USE_Y12 */
163  std::cerr << "need --with-y12 to use y12m library"
164  << std::endl;
165  usage();
166 #endif /* !USE_Y12 */
167 
168  } else if (strcasecmp(solver, "umfpack") == 0
169  || strcasecmp(solver, "umfpack3") == 0) {
170 #ifdef USE_UMFPACK
171  if (dir) {
172  typedef UmfpackSparseCCSolutionManager<DirCColMatrixHandler<0> > CCMH;
173  SAFENEWWITHCONSTRUCTOR(pSM, CCMH, CCMH(size));
174 
175  } else {
176  typedef UmfpackSparseCCSolutionManager<CColMatrixHandler<0> > CCMH;
177  SAFENEWWITHCONSTRUCTOR(pSM, CCMH, CCMH(size));
178  }
179 #else /* !USE_UMFPACK */
180  std::cerr << "need --with-umfpack to use Umfpack library"
181  << std::endl;
182  usage();
183 #endif /* !USE_UMFPACK */
184 
185  } else {
186  std::cerr << "unknown solver '" << solver << "'" << std::endl;
187  usage();
188  }
189 
190  std::cout << "using " << solver << " solver..." << std::endl;
191 
192  MatrixHandler *pM = pSM->pMatHdl();
193  VectorHandler *pV = pSM->pResHdl();
194  VectorHandler *px = pSM->pSolHdl();
195 
196  int count = 0;
197 
198  int p = 2;
199  int w = 7 + p;
200  std::cout.setf(std::ios::scientific);
201  std::cout.precision(p);
202 
203  double m11 = 4.,
204  m22 = 4.,
205  m33 = 2.,
206  d = 0.,
207  b1 = 0.,
208  b2 = 0.,
209  b3 = 1.;
210 
211 retry:;
212  pSM->MatrReset();
213 
214  try {
215  pM = pSM->pMatHdl();
216  pM->Reset();
217 
218  pM->operator()(1, 1) = m11;
219  pM->operator()(2, 2) = m22;
220  pM->operator()(3, 3) = m33;
221  if (d) {
222  pM->operator()(1, 2) = d;
223  pM->operator()(2, 1) = d;
224  pM->operator()(2, 3) = d;
225  pM->operator()(3, 2) = d;
226  }
227 
228  pV->operator()(1) = b1;
229  pV->operator()(2) = b2;
230  pV->operator()(3) = b3;
231 
233  std::cerr << "need to rebuild matrix..." << std::endl;
234  pSM->MatrInitialize();
235  goto retry;
236 
237  } catch (...) {
238  std::cerr << "build failure" << std::endl;
239  exit(EXIT_FAILURE);
240  }
241 
242  try {
243  count++;
244  std::cout << "solution " << count << "..." << std::endl;
245 
246  pSM->Solve();
247 
248  } catch (...) {
249  std::cerr << "solution failure" << std::endl;
250  exit(EXIT_FAILURE);
251  }
252 
253  std::cout
254  << "{x1} [" << std::setw(w) << m11
255  << "," << std::setw(w) << d
256  << "," << std::setw(w) << 0.
257  << "]^-1 {" << std::setw(w) << b1
258  << "} {" << std::setw(w) << (*px)(1) << "}" << std::endl
259  << "{x2} = ["<< std::setw(w) << d
260  << "," << std::setw(w) << m22
261  << "," << std::setw(w) << d
262  << "] {" << std::setw(w) << b2
263  << "} = {" << std::setw(w) << (*px)(2) << "}" << std::endl
264  << "{x3} [" << std::setw(w) << 0.
265  << "," << std::setw(w) << d
266  << "," << std::setw(w) << m33
267  << "] {" << std::setw(w) << b3
268  << "} {" << std::setw(w) << (*px)(3) << "}" << std::endl;
269 
270  switch (count) {
271  case 1:
272  d = -2.;
273  break;
274 
275  case 2:
276  d = 0.;
277  break;
278 
279  case 3:
280  return 0;
281  }
282 
283  goto retry;
284 }
285 
virtual VectorHandler * pResHdl(void) const =0
virtual void Reset(void)=0
const LinSol::solver_t solver[]
Definition: linsol.cc:58
const char * solvers[]
Definition: cctest.cc:51
virtual MatrixHandler * pMatHdl(void) const =0
static int count
Definition: modules.cc:41
virtual void MatrReset(void)=0
virtual void Solve(void)=0
static void usage(void)
Definition: cctest.cc:65
virtual void MatrInitialize(void)
Definition: solman.cc:72
#define SAFENEWWITHCONSTRUCTOR(pnt, item, constructor)
Definition: mynewmem.h:698
int main(int argc, char *argv[])
Definition: cctest.cc:85
int getopt(int argc, char *const argv[], const char *opts)
Definition: getopt.c:93
char * optarg
Definition: getopt.c:74
virtual VectorHandler * pSolHdl(void) const =0