[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] | 25 | void 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] | 54 | void 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_
|
---|
[13295] | 68 | int 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
|
---|
[13185] | 70 | return 0;
|
---|
[13334] | 71 | } /*}}}*/
|
---|
[13295] | 72 | int 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
|
---|
[13320] | 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;
|
---|
[13295] | 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;
|
---|
[13334] | 113 | } /*}}}*/
|
---|
[13297] | 114 | int 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 | /*}}}*/
|
---|
[13313] | 159 | int EDF_fos_reverse_for_solverx(int m, double *dp_U, int n, double *dp_Z) { /*{{{*/
|
---|
| 160 | return 0;
|
---|
| 161 | }
|
---|
| 162 | /*}}}*/
|
---|
[13261] | 163 | void SolverxSeq(IssmDouble *X,IssmDouble *A,IssmDouble *B,int n, Parameters* parameters){/*{{{*/
|
---|
[13195] | 164 | // pack inputs to conform to the EDF-prescribed interface
|
---|
[13261] | 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
|
---|
[13196] | 168 | IssmPDouble* pdoubleEDFin=xNew<IssmPDouble>(n*(n+1)); // provide space to transfer inputs during call_ext_fct
|
---|
[13261] | 169 | IssmPDouble* pdoubleEDFout=xNew<IssmPDouble>(n); // provide space to transfer outputs during call_ext_fct
|
---|
[13195] | 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,
|
---|
[13196] | 172 | n*(n+1), pdoubleEDFin, adoubleEDFin,
|
---|
| 173 | n, pdoubleEDFout,X);
|
---|
| 174 | xDelete(adoubleEDFin);
|
---|
| 175 | xDelete(pdoubleEDFin);
|
---|
| 176 | xDelete(pdoubleEDFout);
|
---|
[12988] | 177 | }
|
---|
| 178 | /*}}}*/
|
---|
| 179 | #endif
|
---|
[13261] | 180 | void SolverxSeq(IssmPDouble *X, IssmPDouble *A, IssmPDouble *B,int n){ /*{{{*/
|
---|
| 181 | #ifdef _HAVE_GSL_
|
---|
[12988] | 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*/
|
---|
[13320] | 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;
|
---|
[12988] | 193 | double* Acopy = xNew<double>(n*n);
|
---|
[13196] | 194 | xMemCpy(Acopy,A,n*n);
|
---|
[12417] | 195 |
|
---|
[12988] | 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);
|
---|
[12417] | 200 |
|
---|
[12988] | 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);
|
---|
[12417] | 205 |
|
---|
[12988] | 206 | //printf ("x = \n");
|
---|
| 207 | //gsl_vector_fprintf (stdout, x, "%g");
|
---|
[12417] | 208 |
|
---|
[12988] | 209 | /*Copy result*/
|
---|
[13196] | 210 | xMemCpy(X,gsl_vector_ptr(x,0),n);
|
---|
[12417] | 211 |
|
---|
[12988] | 212 | /*Clean up and assign output pointer*/
|
---|
[13196] | 213 | xDelete(Acopy);
|
---|
[12988] | 214 | gsl_permutation_free(p);
|
---|
| 215 | gsl_vector_free(x);
|
---|
[13261] | 216 | #endif
|
---|
[12988] | 217 | }
|
---|
| 218 | /*}}}*/
|
---|