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