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
RevLine 
[12850]1/*!\file SolverxSeq
2 * \brief implementation of sequential solver using the GSL librarie
[11726]3 */
4
[12445]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
[12444]10#include <cstring>
11
[11726]12#include "./Solverx.h"
13#include "../../shared/shared.h"
14#include "../../include/include.h"
15#include "../../io/io.h"
16
[12850]17#ifdef _HAVE_GSL_
18#include <gsl/gsl_linalg.h>
19#endif
[11726]20
[12850]21void SolverxSeq(SeqVec** puf,SeqMat* Kff, SeqVec* pf){/*{{{*/
22
23 #ifdef _HAVE_GSL_
[11726]24 /*Intermediary: */
25 int M,N,N2,s;
[12450]26 SeqVec *uf = NULL;
[12470]27 IssmDouble *x = NULL;
[11726]28
29 Kff->GetSize(&M,&N);
30 pf->GetSize(&N2);
31
[13056]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!");
[11726]34
[12850]35 SolverxSeq(&x,Kff->matrix,pf->vector,N);
[12450]36 uf=new SeqVec(x,N);
[11726]37
38 /*Assign output pointers:*/
39 *puf=uf;
[12850]40
41 #else
[13056]42 _error_("GSL support not compiled in!");
[12850]43 #endif
44
[12417]45}/*}}}*/
[12988]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 */
[12417]52
[12988]53 /*Output: */
54 IssmDouble* X=NULL;
55
56 /*Intermediary: */
57 int i;
[13062]58 IssmPDouble* pdoubleA=NULL;
59 IssmPDouble* pdoubleB=NULL;
60 IssmPDouble* pdoubleX=NULL;
[12988]61
62 /*First, transfer from IssmDouble to double all our matrices and vectors: */
[13062]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]);
[12988]67
68 /*Call wrapped solver: */
[13062]69 SolverxSeq(&pdoubleX,pdoubleA, pdoubleB, n);
[12988]70
71 /*Transfer solution vector from double to IssmDouble: */
72 X = xNew<IssmDouble>(n);
[13062]73 for(i=0;i<n;i++)X[i]=reCast<IssmDouble>(pdoubleX[i]);
[12988]74
75 /*Free ressources:*/
[13062]76 xDelete<IssmPDouble>(pdoubleA);
77 xDelete<IssmPDouble>(pdoubleB);
[12988]78
79 /*Assign output pointers: */
80 *pX=X;
81}
82/*}}}*/
83#endif
[13062]84void SolverxSeq(IssmPDouble** pX,IssmPDouble* A,IssmPDouble* B,int n){ //{{{
[12850]85 #ifdef _HAVE_GSL_
[12988]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);
[12417]95
[12988]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);
[12417]100
[12988]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);
[12417]105
[12988]106 //printf ("x = \n");
107 //gsl_vector_fprintf (stdout, x, "%g");
[12417]108
[12988]109 /*Copy result*/
110 double* X = xNew<double>(n);
111 memcpy(X,gsl_vector_ptr(x,0),n*sizeof(double));
[12417]112
[12988]113 /*Clean up and assign output pointer*/
114 xDelete<double>(Acopy);
115 gsl_permutation_free(p);
116 gsl_vector_free(x);
117 *pX=X;
[12850]118 #endif
[12988]119}
120/*}}}*/
Note: See TracBrowser for help on using the repository browser.