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

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

CHG: cosmetics

File size: 7.2 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}/*}}}*/
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
67#ifdef _HAVE_ADOLC_
68int EDF_for_solverx(int n, IssmPDouble *x, int m, IssmPDouble *y){ /*{{{*/
69 SolverxSeq(y,x, x+m*m, m); // x is where the matrix starts, x+m*m is where the right-hand side starts
70 return 0;
71} /*}}}*/
72int EDF_fos_forward_for_solverx(int n, IssmPDouble *inVal, IssmPDouble *inDeriv, int m, IssmPDouble *outVal, IssmPDouble *outDeriv) { /*{{{*/
73#ifdef _HAVE_GSL_
74 // the matrix will be modified by LU decomposition. Use gsl_A copy
75 for (int i=0; i<m*m; ++i)
76 std::cout << "EDF_fos_forward_for_solverx A["<< i << "]=" << inVal[i] << std::endl;
77 for (int i=0; i<m; ++i)
78 std::cout << "EDF_fos_forward_for_solverx b["<< i << "]=" << inVal[i+m*m] << std::endl;
79 double* Acopy = xNew<double>(m*m);
80 xMemCpy(Acopy,inVal,m*m);
81 /*Initialize gsl matrices and vectors: */
82 gsl_matrix_view gsl_A = gsl_matrix_view_array (Acopy,m,m);
83 gsl_vector_view gsl_b = gsl_vector_view_array (inVal+m*m,m); // the right hand side starts at address inVal+m*m
84 gsl_permutation *perm_p = gsl_permutation_alloc (m);
85 int signPerm;
86 // factorize
87 gsl_linalg_LU_decomp (&gsl_A.matrix, perm_p, &signPerm);
88 gsl_vector *gsl_x_p = gsl_vector_alloc (m);
89 // solve for the value
90 gsl_linalg_LU_solve (&gsl_A.matrix, perm_p, &gsl_b.vector, gsl_x_p);
91 /*Copy result*/
92 xMemCpy(outVal,gsl_vector_ptr(gsl_x_p,0),m);
93 gsl_vector_free(gsl_x_p);
94 // solve for the derivatives acc. to A * dx = r with r=db - dA * x
95 // compute the RHS
96 double* r=xNew<double>(m);
97 for (int i=0; i<m; i++) {
98 r[i]=inDeriv[m*m+i]; // this is db[i]
99 for (int j=0;j<m; j++) {
100 r[i]-=inDeriv[i*n+j]*outVal[j]; // this is dA[i][j]*x[j]
101 }
102 }
103 gsl_vector_view gsl_r=gsl_vector_view_array(r,m);
104 gsl_vector *gsl_dx_p = gsl_vector_alloc(m);
105 gsl_linalg_LU_solve (&gsl_A.matrix, perm_p, &gsl_r.vector, gsl_dx_p);
106 xMemCpy(outDeriv,gsl_vector_ptr(gsl_dx_p,0),m);
107 gsl_vector_free(gsl_dx_p);
108 xDelete(r);
109 gsl_permutation_free(perm_p);
110 xDelete(Acopy);
111 #endif
112 return 0;
113} /*}}}*/
114int EDF_fov_forward_for_solverx(int n, IssmPDouble *inVal, int directionCount, IssmPDouble **inDeriv, int m, IssmPDouble *outVal, IssmPDouble **outDeriv) { /*{{{*/
115#ifdef _HAVE_GSL_
116 // the matrix will be modified by LU decomposition. Use gsl_A copy
117 double* Acopy = xNew<double>(m*m);
118 xMemCpy(Acopy,inVal,m*m);
119 /*Initialize gsl matrices and vectors: */
120 gsl_matrix_view gsl_A = gsl_matrix_view_array (Acopy,m,m);
121 gsl_vector_view gsl_b = gsl_vector_view_array (inVal+m*m,m); // the right hand side starts at address inVal+m*m
122 gsl_permutation *perm_p = gsl_permutation_alloc (m);
123 int signPerm;
124 // factorize
125 gsl_linalg_LU_decomp (&gsl_A.matrix, perm_p, &signPerm);
126 gsl_vector *gsl_x_p = gsl_vector_alloc (m);
127 // solve for the value
128 gsl_linalg_LU_solve (&gsl_A.matrix, perm_p, &gsl_b.vector, gsl_x_p);
129 /*Copy result*/
130 xMemCpy(outVal,gsl_vector_ptr(gsl_x_p,0),m);
131 gsl_vector_free(gsl_x_p);
132 // solve for the derivatives acc. to A * dx = r with r=db - dA * x
133 double* r=xNew<double>(m);
134 gsl_vector *gsl_dx_p = gsl_vector_alloc(m);
135 for (int dir=0;dir<directionCount;++dir) {
136 // compute the RHS
137 for (int i=0; i<m; i++) {
138 r[i]=inDeriv[m*m+i][dir]; // this is db[i]
139 for (int j=0;j<m; j++) {
140 r[i]-=inDeriv[i*n+j][dir]*outVal[j]; // this is dA[i][j]*x[j]
141 }
142 }
143 gsl_vector_view gsl_r=gsl_vector_view_array(r,m);
144 gsl_linalg_LU_solve (&gsl_A.matrix, perm_p, &gsl_r.vector, gsl_dx_p);
145 // reuse r
146 xMemCpy(r,gsl_vector_ptr(gsl_dx_p,0),m);
147 for (int i=0; i<m; i++) {
148 outDeriv[i][dir]=r[i];
149 }
150 }
151 gsl_vector_free(gsl_dx_p);
152 xDelete(r);
153 gsl_permutation_free(perm_p);
154 xDelete(Acopy);
155 #endif
156 return 0;
157}
158/*}}}*/
159int EDF_fos_reverse_for_solverx(int m, double *dp_U, int n, double *dp_Z) { /*{{{*/
160 return 0;
161}
162/*}}}*/
163void SolverxSeq(IssmDouble *X,IssmDouble *A,IssmDouble *B,int n, Parameters* parameters){/*{{{*/
164 // pack inputs to conform to the EDF-prescribed interface
165 IssmDouble* adoubleEDFin=xNew<IssmDouble>(n*(n+1)); // packed inputs, i.e. matrix and right hand side
166 for(int i=0; i<n*n;i++)adoubleEDFin[i] =A[i]; // pack matrix
167 for(int i=0; i<n; i++)adoubleEDFin[i+n*n]=B[i]; // pack the right hand side
168 IssmPDouble* pdoubleEDFin=xNew<IssmPDouble>(n*(n+1)); // provide space to transfer inputs during call_ext_fct
169 IssmPDouble* pdoubleEDFout=xNew<IssmPDouble>(n); // provide space to transfer outputs during call_ext_fct
170 // call the wrapped solver through the registry entry we retrieve from parameters
171 call_ext_fct(dynamic_cast<GenericParam<Adolc_edf> * >(parameters->FindParamObject(AdolcParamEnum))->GetParameterValue().myEDF_for_solverx_p,
172 n*(n+1), pdoubleEDFin, adoubleEDFin,
173 n, pdoubleEDFout,X);
174 xDelete(adoubleEDFin);
175 xDelete(pdoubleEDFin);
176 xDelete(pdoubleEDFout);
177}
178/*}}}*/
179#endif
180void SolverxSeq(IssmPDouble *X, IssmPDouble *A, IssmPDouble *B,int n){ /*{{{*/
181#ifdef _HAVE_GSL_
182 /*GSL Matrices and vectors: */
183 int s;
184 gsl_matrix_view a;
185 gsl_vector_view b;
186 gsl_vector *x = NULL;
187 gsl_permutation *p = NULL;
188 /*A will be modified by LU decomposition. Use copy*/
189 for (int i=0; i<n*n; ++i)
190 std::cout << "SolverxSeq A["<< i << "]=" << A[i] << std::endl;
191 for (int i=0; i<n; ++i)
192 std::cout << "SolverxSeq b["<< i << "]=" << B[i] << std::endl;
193 double* Acopy = xNew<double>(n*n);
194 xMemCpy(Acopy,A,n*n);
195
196 /*Initialize gsl matrices and vectors: */
197 a = gsl_matrix_view_array (Acopy,n,n);
198 b = gsl_vector_view_array (B,n);
199 x = gsl_vector_alloc (n);
200
201 /*Run LU and solve: */
202 p = gsl_permutation_alloc (n);
203 gsl_linalg_LU_decomp (&a.matrix, p, &s);
204 gsl_linalg_LU_solve (&a.matrix, p, &b.vector, x);
205
206 //printf ("x = \n");
207 //gsl_vector_fprintf (stdout, x, "%g");
208
209 /*Copy result*/
210 xMemCpy(X,gsl_vector_ptr(x,0),n);
211
212 /*Clean up and assign output pointer*/
213 xDelete(Acopy);
214 gsl_permutation_free(p);
215 gsl_vector_free(x);
216#endif
217}
218/*}}}*/
Note: See TracBrowser for help on using the repository browser.