source: issm/trunk-jpl/src/c/modules/Solverx/SolverxSeq.cpp@ 13062

Last change on this file since 13062 was 13062, checked in by Eric.Larour, 13 years ago

CHG: used IssmPDouble instead of double for Solver prototype -> simplified code layout

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