1 | /*!\file SolverxSeq
|
---|
2 | * \brief implementation of sequential solver using the GSL librarie
|
---|
3 | */
|
---|
4 |
|
---|
5 | #ifdef HAVE_CONFIG_H
|
---|
6 | #include <config.h>
|
---|
7 | #else
|
---|
8 | #error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
|
---|
9 | #endif
|
---|
10 | #include <cstring>
|
---|
11 |
|
---|
12 | #include "./Solverx.h"
|
---|
13 | #include "../../shared/shared.h"
|
---|
14 | #include "../../include/include.h"
|
---|
15 | #include "../../io/io.h"
|
---|
16 |
|
---|
17 | #ifdef _HAVE_GSL_
|
---|
18 | #include <gsl/gsl_linalg.h>
|
---|
19 | #endif
|
---|
20 |
|
---|
21 | void SolverxSeq(SeqVec** puf,SeqMat* Kff, SeqVec* pf){/*{{{*/
|
---|
22 |
|
---|
23 | #ifdef _HAVE_GSL_
|
---|
24 | /*Intermediary: */
|
---|
25 | int M,N,N2,s;
|
---|
26 | SeqVec *uf = NULL;
|
---|
27 | IssmDouble *x = NULL;
|
---|
28 |
|
---|
29 | Kff->GetSize(&M,&N);
|
---|
30 | pf->GetSize(&N2);
|
---|
31 |
|
---|
32 | if(N!=N2)_error2_("Right hand side vector of size " << N2 << ", when matrix is of size " << M << "-" << N << " !");
|
---|
33 | if(M!=N)_error2_("Stiffness matrix should be square!");
|
---|
34 |
|
---|
35 | SolverxSeq(&x,Kff->matrix,pf->vector,N);
|
---|
36 | uf=new SeqVec(x,N);
|
---|
37 |
|
---|
38 | /*Assign output pointers:*/
|
---|
39 | *puf=uf;
|
---|
40 |
|
---|
41 | #else
|
---|
42 | _error2_("GSL support not compiled in!");
|
---|
43 | #endif
|
---|
44 |
|
---|
45 | }/*}}}*/
|
---|
46 | void SolverxSeq(IssmDouble** pX,IssmDouble* A,IssmDouble* B,int n){/*{{{*/
|
---|
47 |
|
---|
48 | #ifdef _HAVE_GSL_
|
---|
49 | /*GSL Matrices and vectors: */
|
---|
50 | int s;
|
---|
51 | gsl_matrix_view a;
|
---|
52 | gsl_vector_view b;
|
---|
53 | gsl_vector *x = NULL;
|
---|
54 | gsl_permutation *p = NULL;
|
---|
55 | #ifdef _HAVE_ADOLC_
|
---|
56 | // if we use Adol-C then the IssmDouble will be an adouble
|
---|
57 | // and the calls to gsl_... will not work
|
---|
58 | // and we should call a suitable wrapped solve instead
|
---|
59 | _error2_("SolverxSeq: should not be here with Adol-C");
|
---|
60 | #else
|
---|
61 | /*A will be modified by LU decomposition. Use copy*/
|
---|
62 | IssmDouble* Acopy = xNew<IssmDouble>(n*n);
|
---|
63 | xMemCpy<IssmDouble>(Acopy,A,n*n);
|
---|
64 |
|
---|
65 | /*Initialize gsl matrices and vectors: */
|
---|
66 | a = gsl_matrix_view_array (Acopy,n,n);
|
---|
67 | b = gsl_vector_view_array (B,n);
|
---|
68 | x = gsl_vector_alloc (n);
|
---|
69 |
|
---|
70 | /*Run LU and solve: */
|
---|
71 | p = gsl_permutation_alloc (n);
|
---|
72 | gsl_linalg_LU_decomp (&a.matrix, p, &s);
|
---|
73 | gsl_linalg_LU_solve (&a.matrix, p, &b.vector, x);
|
---|
74 |
|
---|
75 | //printf ("x = \n");
|
---|
76 | //gsl_vector_fprintf (stdout, x, "%g");
|
---|
77 |
|
---|
78 | /*Copy result*/
|
---|
79 | IssmDouble* X = xNew<IssmDouble>(n);
|
---|
80 | memcpy(X,gsl_vector_ptr(x,0),n*sizeof(IssmDouble));
|
---|
81 |
|
---|
82 | /*Clean up and assign output pointer*/
|
---|
83 | xDelete<IssmDouble>(Acopy);
|
---|
84 | gsl_permutation_free(p);
|
---|
85 | gsl_vector_free(x);
|
---|
86 | *pX=X;
|
---|
87 | #endif
|
---|
88 | #endif
|
---|
89 | }/*}}}*/
|
---|