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

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

NEW: large change to the code, to adapt to ADOLC requirements.

This change relates to the introduction of template classes and functions for the
Option.h abstract class. This is needed, because we want to make the Matlab
API independent from the libCore objects, which are dependent on the IssmDouble*
ADOLC type (adouble), when the Matlab API is dependent on the IssmPDouble* type (double).

To make them independent, we need to be able to specify at run time Options, Matrix and
Vector objects that hold either IssmDouble or IssmPDouble objects. The only way to do
that is through the use of templated classes for Option.h, Matrix and Vector.

The change gets rid of a lot of useless code (especially in the classes/objects/Options
directory), by introducing template versions of the same code.

The bulk of the changes to src/modules and src/mex modules is to adapt to this
new runtime declaration of templated Matrix, Vector and Option objects.

File size: 3.3 KB
Line 
1/*!\file SolverxSeq
2 * \brief implementation of sequential solver using the GSL librarie
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
10#include <cstring>
11
12#include "./Solverx.h"
13#include "../../shared/shared.h"
14#include "../../include/include.h"
15#include "../../io/io.h"
16
17#ifdef _HAVE_GSL_
18#include <gsl/gsl_linalg.h>
19#endif
20
21#ifdef _HAVE_ADOLC_
22#include "../../shared/Numerics/adolc_edf.h"
23#endif
24
25void SolverxSeq(SeqVec<IssmDouble>** puf,SeqMat<IssmDouble>* Kff, SeqVec<IssmDouble>* pf, Parameters* parameters){/*{{{*/
26
27 #ifdef _HAVE_GSL_
28 /*Intermediary: */
29 int M,N,N2,s;
30 SeqVec<IssmDouble> *uf = NULL;
31
32 Kff->GetSize(&M,&N);
33 pf->GetSize(&N2);
34
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!");
37 IssmDouble *x = xNew<IssmDouble>(N);
38#ifdef _HAVE_ADOLC_
39 SolverxSeq(x,Kff->matrix,pf->vector,N,parameters);
40#else
41 SolverxSeq(x,Kff->matrix,pf->vector,N);
42#endif
43 uf=new SeqVec<IssmDouble>(x,N);
44 xDelete(x);
45
46 /*Assign output pointers:*/
47 *puf=uf;
48
49 #else
50 _error_("GSL support not compiled in!");
51 #endif
52
53}/*}}}*/
54#ifdef _HAVE_ADOLC_
55
56int 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
62void SolverxSeq(IssmDouble *X,IssmDouble *A,IssmDouble *B,int n, Parameters* parameters){//{{{
63 // pack inputs to conform to the EDF-prescribed interface
64 IssmDouble* adoubleEDFin=xNew<IssmDouble>(n*(n+1)); // packed inputs, i.e. matrix and right hand side
65 for(int i=0; i<n*n;i++)adoubleEDFin[i] =A[i]; // pack matrix
66 for(int i=0; i<n; i++)adoubleEDFin[i+n*n]=B[i]; // pack the right hand side
67 IssmPDouble* pdoubleEDFin=xNew<IssmPDouble>(n*(n+1)); // provide space to transfer inputs during call_ext_fct
68 IssmPDouble* pdoubleEDFout=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), pdoubleEDFin, adoubleEDFin,
72 n, pdoubleEDFout,X);
73 xDelete(adoubleEDFin);
74 xDelete(pdoubleEDFin);
75 xDelete(pdoubleEDFout);
76}
77/*}}}*/
78#endif
79void SolverxSeq(IssmPDouble * X, IssmPDouble * A, IssmPDouble * B,int n){ //{{{
80 #ifdef _HAVE_GSL_
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(Acopy,A,n*n);
90
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);
95
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);
100
101 //printf ("x = \n");
102 //gsl_vector_fprintf (stdout, x, "%g");
103
104 /*Copy result*/
105 xMemCpy(X,gsl_vector_ptr(x,0),n);
106
107 /*Clean up and assign output pointer*/
108 xDelete(Acopy);
109 gsl_permutation_free(p);
110 gsl_vector_free(x);
111 #endif
112}
113/*}}}*/
Note: See TracBrowser for help on using the repository browser.