source: issm/trunk/src/c/modules/Solverx/SolverxGsl.cpp@ 12706

Last change on this file since 12706 was 12706, checked in by Mathieu Morlighem, 13 years ago

merged trunk-jpl and trunk for revision 12703

File size: 2.0 KB
RevLine 
[11726]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
[12706]10#include <cstring>
[11726]11#include <gsl/gsl_linalg.h>
12
[12706]13#include "./Solverx.h"
14#include "../../shared/shared.h"
15#include "../../include/include.h"
16#include "../../io/io.h"
[11726]17
[12706]18void SolverxGsl(SeqVec** puf,SeqMat* Kff, SeqVec* pf){/*{{{*/
[11726]19
20 /*Intermediary: */
21 int M,N,N2,s;
[12706]22 SeqVec *uf = NULL;
23 IssmDouble *x = NULL;
[11726]24
25 Kff->GetSize(&M,&N);
26 pf->GetSize(&N2);
27
[12706]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!");
[11726]30
[12706]31 SolverxGsl(&x,Kff->matrix,pf->vector,N);
32 uf=new SeqVec(x,N);
33
34 /*Assign output pointers:*/
35 *puf=uf;
36}/*}}}*/
37void 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
[11726]55 /*Initialize gsl matrices and vectors: */
[12706]56 a = gsl_matrix_view_array (Acopy,n,n);
57 b = gsl_vector_view_array (B,n);
58 x = gsl_vector_alloc (n);
[11726]59
60 /*Run LU and solve: */
[12706]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);
[11726]64
65 //printf ("x = \n");
66 //gsl_vector_fprintf (stdout, x, "%g");
67
[12706]68 /*Copy result*/
69 IssmDouble* X = xNew<IssmDouble>(n);
70 memcpy(X,gsl_vector_ptr(x,0),n*sizeof(IssmDouble));
[11726]71
[12706]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}/*}}}*/
Note: See TracBrowser for help on using the repository browser.