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

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

BUG: fixed Kriging with new solver prototype

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