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)_error_("Right hand side vector of size " << N2 << ", when matrix is of size " << M << "-" << N << " !");
|
---|
33 | if(M!=N)_error_("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 | _error_("GSL support not compiled in!");
|
---|
43 | #endif
|
---|
44 |
|
---|
45 | }/*}}}*/
|
---|
46 | #ifdef _HAVE_ADOLC_
|
---|
47 |
|
---|
48 | int EDF_for_solverx(int n, IssmPDouble *x, int m, IssmPDouble *y) {
|
---|
49 | if(m*(m+1)!=n)_error_("Stiffness matrix should be square!");
|
---|
50 | SolverxSeq(&y,x, x+m*m, m);
|
---|
51 | return 0;
|
---|
52 | }
|
---|
53 |
|
---|
54 | void SolverxSeq(IssmDouble** pX,IssmDouble* A,IssmDouble* B,int n){//{{{
|
---|
55 | /* if we use Adol-C then the IssmDouble will be an adouble
|
---|
56 | and the calls to gsl_... will not work.
|
---|
57 | We therefore call a wrapped solver instead.
|
---|
58 | */
|
---|
59 |
|
---|
60 | /*Output: */
|
---|
61 | IssmDouble* X=NULL;
|
---|
62 |
|
---|
63 | /*Intermediary: */
|
---|
64 | int i;
|
---|
65 | IssmPDouble* pdoubleA=NULL;
|
---|
66 | IssmPDouble* pdoubleB=NULL;
|
---|
67 | IssmPDouble* pdoubleX=NULL;
|
---|
68 |
|
---|
69 | /*First, transfer from IssmDouble to double all our matrices and vectors: */
|
---|
70 | pdoubleA=xNew<IssmPDouble>(n*n);
|
---|
71 | pdoubleB=xNew<IssmPDouble>(n);
|
---|
72 | for(i=0;i<n*n;i++)pdoubleA[i]=reCast<IssmPDouble>(A[i]);
|
---|
73 | for(i=0;i<n;i++)pdoubleB[i]=reCast<IssmPDouble>(B[i]);
|
---|
74 |
|
---|
75 | /*Call wrapped solver: */
|
---|
76 | SolverxSeq(&pdoubleX,pdoubleA, pdoubleB, n);
|
---|
77 |
|
---|
78 | /*Transfer solution vector from double to IssmDouble: */
|
---|
79 | X = xNew<IssmDouble>(n);
|
---|
80 | for(i=0;i<n;i++)X[i]=reCast<IssmDouble>(pdoubleX[i]);
|
---|
81 |
|
---|
82 | /*Free ressources:*/
|
---|
83 | xDelete<IssmPDouble>(pdoubleA);
|
---|
84 | xDelete<IssmPDouble>(pdoubleB);
|
---|
85 |
|
---|
86 | /*Assign output pointers: */
|
---|
87 | *pX=X;
|
---|
88 | }
|
---|
89 | /*}}}*/
|
---|
90 | #endif
|
---|
91 | void SolverxSeq(IssmPDouble** pX,IssmPDouble* A,IssmPDouble* B,int n){ //{{{
|
---|
92 | #ifdef _HAVE_GSL_
|
---|
93 | /*GSL Matrices and vectors: */
|
---|
94 | int s;
|
---|
95 | gsl_matrix_view a;
|
---|
96 | gsl_vector_view b;
|
---|
97 | gsl_vector *x = NULL;
|
---|
98 | gsl_permutation *p = NULL;
|
---|
99 | /*A will be modified by LU decomposition. Use copy*/
|
---|
100 | double* Acopy = xNew<double>(n*n);
|
---|
101 | xMemCpy<double>(Acopy,A,n*n);
|
---|
102 |
|
---|
103 | /*Initialize gsl matrices and vectors: */
|
---|
104 | a = gsl_matrix_view_array (Acopy,n,n);
|
---|
105 | b = gsl_vector_view_array (B,n);
|
---|
106 | x = gsl_vector_alloc (n);
|
---|
107 |
|
---|
108 | /*Run LU and solve: */
|
---|
109 | p = gsl_permutation_alloc (n);
|
---|
110 | gsl_linalg_LU_decomp (&a.matrix, p, &s);
|
---|
111 | gsl_linalg_LU_solve (&a.matrix, p, &b.vector, x);
|
---|
112 |
|
---|
113 | //printf ("x = \n");
|
---|
114 | //gsl_vector_fprintf (stdout, x, "%g");
|
---|
115 |
|
---|
116 | /*Copy result*/
|
---|
117 | double* X = xNew<double>(n);
|
---|
118 | memcpy(X,gsl_vector_ptr(x,0),n*sizeof(double));
|
---|
119 |
|
---|
120 | /*Clean up and assign output pointer*/
|
---|
121 | xDelete<double>(Acopy);
|
---|
122 | gsl_permutation_free(p);
|
---|
123 | gsl_vector_free(x);
|
---|
124 | *pX=X;
|
---|
125 | #endif
|
---|
126 | }
|
---|
127 | /*}}}*/
|
---|