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

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

CHG: homoegeization of the matrix class. Is now split into a pector (petsc vector) and svector (sequential vector).
New objects PetscMat and PetscVec in the toolkits allow for this homoegeization. Things are not perfect yet,
as we reference the matrix and vector internals across the code, which could create some serious issues.

File size: 2.2 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
[12493]32 if(N!=N2)_error2_("Right hand side vector of size " << N2 << ", when matrix is of size " << M << "-" << N << " !");
33 if(M!=N)_error2_("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
42 _error2_("GSL support not compiled in!");
43 #endif
44
[12417]45}/*}}}*/
[12850]46void SolverxSeq(IssmDouble** pX,IssmDouble* A,IssmDouble* B,int n){/*{{{*/
[12417]47
[12850]48 #ifdef _HAVE_GSL_
49 /*GSL Matrices and vectors: */
50 int s;
51 gsl_matrix_view a;
52 gsl_vector_view b;
53 gsl_vector *x = NULL;
54 gsl_permutation *p = NULL;
55 #ifdef _HAVE_ADOLC_
56 // if we use Adol-C then the IssmDouble will be an adouble
57 // and the calls to gsl_... will not work
58 // and we should call a suitable wrapped solve instead
59 _error2_("SolverxSeq: should not be here with Adol-C");
60 #else
61 /*A will be modified by LU decomposition. Use copy*/
62 IssmDouble* Acopy = xNew<IssmDouble>(n*n);
63 xMemCpy<IssmDouble>(Acopy,A,n*n);
[12417]64
[12850]65 /*Initialize gsl matrices and vectors: */
66 a = gsl_matrix_view_array (Acopy,n,n);
67 b = gsl_vector_view_array (B,n);
68 x = gsl_vector_alloc (n);
[12417]69
[12850]70 /*Run LU and solve: */
71 p = gsl_permutation_alloc (n);
72 gsl_linalg_LU_decomp (&a.matrix, p, &s);
73 gsl_linalg_LU_solve (&a.matrix, p, &b.vector, x);
[12417]74
[12850]75 //printf ("x = \n");
76 //gsl_vector_fprintf (stdout, x, "%g");
[12417]77
[12850]78 /*Copy result*/
79 IssmDouble* X = xNew<IssmDouble>(n);
80 memcpy(X,gsl_vector_ptr(x,0),n*sizeof(IssmDouble));
[12417]81
[12850]82 /*Clean up and assign output pointer*/
83 xDelete<IssmDouble>(Acopy);
84 gsl_permutation_free(p);
85 gsl_vector_free(x);
86 *pX=X;
87 #endif
88 #endif
[12417]89}/*}}}*/
Note: See TracBrowser for help on using the repository browser.