[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 |
|
---|
[13195] | 21 | #ifdef _HAVE_ADOLC_
|
---|
| 22 | #include "../../shared/Numerics/adolc_edf.h"
|
---|
| 23 | #endif
|
---|
[12850] | 24 |
|
---|
[13195] | 25 | void SolverxSeq(SeqVec** puf,SeqMat* Kff, SeqVec* pf, Parameters* parameters){/*{{{*/
|
---|
| 26 |
|
---|
[12850] | 27 | #ifdef _HAVE_GSL_
|
---|
[11726] | 28 | /*Intermediary: */
|
---|
| 29 | int M,N,N2,s;
|
---|
[12450] | 30 | SeqVec *uf = NULL;
|
---|
[12470] | 31 | IssmDouble *x = NULL;
|
---|
[11726] | 32 |
|
---|
| 33 | Kff->GetSize(&M,&N);
|
---|
| 34 | pf->GetSize(&N2);
|
---|
| 35 |
|
---|
[13056] | 36 | if(N!=N2)_error_("Right hand side vector of size " << N2 << ", when matrix is of size " << M << "-" << N << " !");
|
---|
| 37 | if(M!=N)_error_("Stiffness matrix should be square!");
|
---|
[11726] | 38 |
|
---|
[13195] | 39 | #ifdef _HAVE_ADOLC_
|
---|
| 40 | SolverxSeq(&x,Kff->matrix,pf->vector,N,parameters);
|
---|
| 41 | #else
|
---|
[12850] | 42 | SolverxSeq(&x,Kff->matrix,pf->vector,N);
|
---|
[13195] | 43 | #endif
|
---|
[12450] | 44 | uf=new SeqVec(x,N);
|
---|
[11726] | 45 |
|
---|
| 46 | /*Assign output pointers:*/
|
---|
| 47 | *puf=uf;
|
---|
[12850] | 48 |
|
---|
| 49 | #else
|
---|
[13056] | 50 | _error_("GSL support not compiled in!");
|
---|
[12850] | 51 | #endif
|
---|
| 52 |
|
---|
[12417] | 53 | }/*}}}*/
|
---|
[12988] | 54 | #ifdef _HAVE_ADOLC_
|
---|
[13185] | 55 |
|
---|
| 56 | int EDF_for_solverx(int n, IssmPDouble *x, int m, IssmPDouble *y) {
|
---|
| 57 | if(m*(m+1)!=n)_error_("Stiffness matrix should be square!");
|
---|
| 58 | SolverxSeq(&y,x, x+m*m, m);
|
---|
| 59 | return 0;
|
---|
| 60 | }
|
---|
| 61 |
|
---|
[13195] | 62 | void SolverxSeq(IssmDouble** pX,IssmDouble* A,IssmDouble* B,int n, Parameters* parameters){//{{{
|
---|
| 63 | // pack inputs to conform to the EDF-prescribed interface
|
---|
| 64 | IssmDouble* adoubleEDF_X=xNew<IssmDouble>(n*(n+1)); // packed inputs, i.e. matrix and right hand side
|
---|
| 65 | for(int i=0; i<n*n;i++)adoubleEDF_X[i] =A[i]; // pack matrix
|
---|
| 66 | for(int i=0; i<n; i++)adoubleEDF_X[i+n*n]=B[i]; // pack the right hand side
|
---|
| 67 | IssmPDouble* pdoubleEDF_X=xNew<IssmPDouble>(n*(n+1)); // provide space to transfer inputs during call_ext_fct
|
---|
| 68 | IssmPDouble* pdoubleEDF_Y=xNew<IssmPDouble>(n); // provide space to transfer outputs during call_ext_fct
|
---|
| 69 | // call the wrapped solver through the registry entry we retrieve from parameters
|
---|
| 70 | call_ext_fct(dynamic_cast<GenericParam<Adolc_edf> * >(parameters->FindParamObject(AdolcParamEnum))->GetParameterValue().myEDF_for_solverx_p,
|
---|
| 71 | n*(n+1), pdoubleEDF_X, adoubleEDF_X,
|
---|
| 72 | n, pdoubleEDF_Y, B);
|
---|
| 73 | xDelete(adoubleEDF_X);
|
---|
| 74 | xDelete(pdoubleEDF_X);
|
---|
| 75 | xDelete(pdoubleEDF_Y);
|
---|
[12988] | 76 | }
|
---|
| 77 | /*}}}*/
|
---|
| 78 | #endif
|
---|
[13062] | 79 | void SolverxSeq(IssmPDouble** pX,IssmPDouble* A,IssmPDouble* B,int n){ //{{{
|
---|
[12850] | 80 | #ifdef _HAVE_GSL_
|
---|
[12988] | 81 | /*GSL Matrices and vectors: */
|
---|
| 82 | int s;
|
---|
| 83 | gsl_matrix_view a;
|
---|
| 84 | gsl_vector_view b;
|
---|
| 85 | gsl_vector *x = NULL;
|
---|
| 86 | gsl_permutation *p = NULL;
|
---|
| 87 | /*A will be modified by LU decomposition. Use copy*/
|
---|
| 88 | double* Acopy = xNew<double>(n*n);
|
---|
| 89 | xMemCpy<double>(Acopy,A,n*n);
|
---|
[12417] | 90 |
|
---|
[12988] | 91 | /*Initialize gsl matrices and vectors: */
|
---|
| 92 | a = gsl_matrix_view_array (Acopy,n,n);
|
---|
| 93 | b = gsl_vector_view_array (B,n);
|
---|
| 94 | x = gsl_vector_alloc (n);
|
---|
[12417] | 95 |
|
---|
[12988] | 96 | /*Run LU and solve: */
|
---|
| 97 | p = gsl_permutation_alloc (n);
|
---|
| 98 | gsl_linalg_LU_decomp (&a.matrix, p, &s);
|
---|
| 99 | gsl_linalg_LU_solve (&a.matrix, p, &b.vector, x);
|
---|
[12417] | 100 |
|
---|
[12988] | 101 | //printf ("x = \n");
|
---|
| 102 | //gsl_vector_fprintf (stdout, x, "%g");
|
---|
[12417] | 103 |
|
---|
[12988] | 104 | /*Copy result*/
|
---|
| 105 | double* X = xNew<double>(n);
|
---|
| 106 | memcpy(X,gsl_vector_ptr(x,0),n*sizeof(double));
|
---|
[12417] | 107 |
|
---|
[12988] | 108 | /*Clean up and assign output pointer*/
|
---|
| 109 | xDelete<double>(Acopy);
|
---|
| 110 | gsl_permutation_free(p);
|
---|
| 111 | gsl_vector_free(x);
|
---|
| 112 | *pX=X;
|
---|
[12850] | 113 | #endif
|
---|
[12988] | 114 | }
|
---|
| 115 | /*}}}*/
|
---|