source:
issm/oecreview/Archive/13393-13976/ISSM-13513-13514.diff@
17986
| Last change on this file since 17986 was 13980, checked in by , 13 years ago | |
|---|---|
| File size: 9.3 KB | |
-
../trunk-jpl/src/c/modules/Solverx/SolverxSeq.cpp
70 70 71 71 #ifdef _HAVE_ADOLC_ 72 72 int EDF_for_solverx(int n, IssmPDouble *x, int m, IssmPDouble *y){ /*{{{*/ 73 SolverxSeq(y,x, x+m*m, m); // x is where the matrix starts, x+m*m is where the right-hand side starts74 return 0;73 SolverxSeq(y,x, x+m*m, m); // x is where the matrix starts, x+m*m is where the right-hand side starts 74 return 0; 75 75 } /*}}}*/ 76 76 int EDF_fos_forward_for_solverx(int n, IssmPDouble *inVal, IssmPDouble *inDeriv, int m, IssmPDouble *outVal, IssmPDouble *outDeriv) { /*{{{*/ 77 77 #ifdef _HAVE_GSL_ 78 // for (int i=0; i<m*m; ++i) std::cout << "EDF_fos_forward_for_solverx A["<< i << "]=" << inVal[i] << std::endl;79 // for (int i=0; i<m; ++i) std::cout << "EDF_fos_forward_for_solverx b["<< i << "]=" << inVal[i+m*m] << std::endl;80 // the matrix will be modified by LU decomposition. Use gsl_A copy81 double* Acopy = xNew<double>(m*m);82 xMemCpy(Acopy,inVal,m*m);83 /*Initialize gsl matrices and vectors: */84 gsl_matrix_view gsl_A = gsl_matrix_view_array (Acopy,m,m);85 gsl_vector_view gsl_b = gsl_vector_view_array (inVal+m*m,m); // the right hand side starts at address inVal+m*m86 gsl_permutation *perm_p = gsl_permutation_alloc (m);87 int signPerm;88 // factorize89 gsl_linalg_LU_decomp (&gsl_A.matrix, perm_p, &signPerm);90 gsl_vector *gsl_x_p = gsl_vector_alloc (m);91 // solve for the value92 gsl_linalg_LU_solve (&gsl_A.matrix, perm_p, &gsl_b.vector, gsl_x_p);93 /*Copy result*/94 xMemCpy(outVal,gsl_vector_ptr(gsl_x_p,0),m);95 gsl_vector_free(gsl_x_p);96 // for (int i=0; i<m; ++i) std::cout << "EDF_fos_forward_for_solverx x["<< i << "]=" << outVal[i] << std::endl;97 // solve for the derivatives acc. to A * dx = r with r=db - dA * x98 // compute the RHS99 double* r=xNew<double>(m);100 for (int i=0; i<m; i++) {101 r[i]=inDeriv[m*m+i]; // this is db[i]102 for (int j=0;j<m; j++) {103 r[i]-=inDeriv[i*m+j]*outVal[j]; // this is dA[i][j]*x[j]104 }105 }106 gsl_vector_view gsl_r=gsl_vector_view_array(r,m);107 gsl_vector *gsl_dx_p = gsl_vector_alloc(m);108 gsl_linalg_LU_solve (&gsl_A.matrix, perm_p, &gsl_r.vector, gsl_dx_p);109 xMemCpy(outDeriv,gsl_vector_ptr(gsl_dx_p,0),m);110 gsl_vector_free(gsl_dx_p);111 xDelete(r);112 gsl_permutation_free(perm_p);113 xDelete(Acopy);114 #endif115 return 0;78 // for (int i=0; i<m*m; ++i) std::cout << "EDF_fos_forward_for_solverx A["<< i << "]=" << inVal[i] << std::endl; 79 // for (int i=0; i<m; ++i) std::cout << "EDF_fos_forward_for_solverx b["<< i << "]=" << inVal[i+m*m] << std::endl; 80 // the matrix will be modified by LU decomposition. Use gsl_A copy 81 double* Acopy = xNew<double>(m*m); 82 xMemCpy(Acopy,inVal,m*m); 83 /*Initialize gsl matrices and vectors: */ 84 gsl_matrix_view gsl_A = gsl_matrix_view_array (Acopy,m,m); 85 gsl_vector_view gsl_b = gsl_vector_view_array (inVal+m*m,m); // the right hand side starts at address inVal+m*m 86 gsl_permutation *perm_p = gsl_permutation_alloc (m); 87 int signPerm; 88 // factorize 89 gsl_linalg_LU_decomp (&gsl_A.matrix, perm_p, &signPerm); 90 gsl_vector *gsl_x_p = gsl_vector_alloc (m); 91 // solve for the value 92 gsl_linalg_LU_solve (&gsl_A.matrix, perm_p, &gsl_b.vector, gsl_x_p); 93 /*Copy result*/ 94 xMemCpy(outVal,gsl_vector_ptr(gsl_x_p,0),m); 95 gsl_vector_free(gsl_x_p); 96 // for (int i=0; i<m; ++i) std::cout << "EDF_fos_forward_for_solverx x["<< i << "]=" << outVal[i] << std::endl; 97 // solve for the derivatives acc. to A * dx = r with r=db - dA * x 98 // compute the RHS 99 double* r=xNew<double>(m); 100 for (int i=0; i<m; i++) { 101 r[i]=inDeriv[m*m+i]; // this is db[i] 102 for (int j=0;j<m; j++) { 103 r[i]-=inDeriv[i*m+j]*outVal[j]; // this is dA[i][j]*x[j] 104 } 105 } 106 gsl_vector_view gsl_r=gsl_vector_view_array(r,m); 107 gsl_vector *gsl_dx_p = gsl_vector_alloc(m); 108 gsl_linalg_LU_solve (&gsl_A.matrix, perm_p, &gsl_r.vector, gsl_dx_p); 109 xMemCpy(outDeriv,gsl_vector_ptr(gsl_dx_p,0),m); 110 gsl_vector_free(gsl_dx_p); 111 xDelete(r); 112 gsl_permutation_free(perm_p); 113 xDelete(Acopy); 114 #endif 115 return 0; 116 116 } /*}}}*/ 117 117 int EDF_fov_forward_for_solverx(int n, IssmPDouble *inVal, int directionCount, IssmPDouble **inDeriv, int m, IssmPDouble *outVal, IssmPDouble **outDeriv) { /*{{{*/ 118 118 #ifdef _HAVE_GSL_ 119 // the matrix will be modified by LU decomposition. Use gsl_A copy120 double* Acopy = xNew<double>(m*m);121 xMemCpy(Acopy,inVal,m*m);122 /*Initialize gsl matrices and vectors: */123 gsl_matrix_view gsl_A = gsl_matrix_view_array (Acopy,m,m);124 gsl_vector_view gsl_b = gsl_vector_view_array (inVal+m*m,m); // the right hand side starts at address inVal+m*m125 gsl_permutation *perm_p = gsl_permutation_alloc (m);126 int signPerm;127 // factorize128 gsl_linalg_LU_decomp (&gsl_A.matrix, perm_p, &signPerm);129 gsl_vector *gsl_x_p = gsl_vector_alloc (m);130 // solve for the value131 gsl_linalg_LU_solve (&gsl_A.matrix, perm_p, &gsl_b.vector, gsl_x_p);132 /*Copy result*/133 xMemCpy(outVal,gsl_vector_ptr(gsl_x_p,0),m);134 gsl_vector_free(gsl_x_p);135 // solve for the derivatives acc. to A * dx = r with r=db - dA * x136 double* r=xNew<double>(m);137 gsl_vector *gsl_dx_p = gsl_vector_alloc(m);138 for (int dir=0;dir<directionCount;++dir) {139 // compute the RHS140 for (int i=0; i<m; i++) {141 r[i]=inDeriv[m*m+i][dir]; // this is db[i]142 for (int j=0;j<m; j++) {143 r[i]-=inDeriv[i*m+j][dir]*outVal[j]; // this is dA[i][j]*x[j]144 }145 }146 gsl_vector_view gsl_r=gsl_vector_view_array(r,m);147 gsl_linalg_LU_solve (&gsl_A.matrix, perm_p, &gsl_r.vector, gsl_dx_p);148 // reuse r149 xMemCpy(r,gsl_vector_ptr(gsl_dx_p,0),m);150 for (int i=0; i<m; i++) {151 outDeriv[i][dir]=r[i];152 }153 }154 gsl_vector_free(gsl_dx_p);155 xDelete(r);156 gsl_permutation_free(perm_p);157 xDelete(Acopy);158 #endif159 return 0;119 // the matrix will be modified by LU decomposition. Use gsl_A copy 120 double* Acopy = xNew<double>(m*m); 121 xMemCpy(Acopy,inVal,m*m); 122 /*Initialize gsl matrices and vectors: */ 123 gsl_matrix_view gsl_A = gsl_matrix_view_array (Acopy,m,m); 124 gsl_vector_view gsl_b = gsl_vector_view_array (inVal+m*m,m); // the right hand side starts at address inVal+m*m 125 gsl_permutation *perm_p = gsl_permutation_alloc (m); 126 int signPerm; 127 // factorize 128 gsl_linalg_LU_decomp (&gsl_A.matrix, perm_p, &signPerm); 129 gsl_vector *gsl_x_p = gsl_vector_alloc (m); 130 // solve for the value 131 gsl_linalg_LU_solve (&gsl_A.matrix, perm_p, &gsl_b.vector, gsl_x_p); 132 /*Copy result*/ 133 xMemCpy(outVal,gsl_vector_ptr(gsl_x_p,0),m); 134 gsl_vector_free(gsl_x_p); 135 // solve for the derivatives acc. to A * dx = r with r=db - dA * x 136 double* r=xNew<double>(m); 137 gsl_vector *gsl_dx_p = gsl_vector_alloc(m); 138 for (int dir=0;dir<directionCount;++dir) { 139 // compute the RHS 140 for (int i=0; i<m; i++) { 141 r[i]=inDeriv[m*m+i][dir]; // this is db[i] 142 for (int j=0;j<m; j++) { 143 r[i]-=inDeriv[i*m+j][dir]*outVal[j]; // this is dA[i][j]*x[j] 144 } 145 } 146 gsl_vector_view gsl_r=gsl_vector_view_array(r,m); 147 gsl_linalg_LU_solve (&gsl_A.matrix, perm_p, &gsl_r.vector, gsl_dx_p); 148 // reuse r 149 xMemCpy(r,gsl_vector_ptr(gsl_dx_p,0),m); 150 for (int i=0; i<m; i++) { 151 outDeriv[i][dir]=r[i]; 152 } 153 } 154 gsl_vector_free(gsl_dx_p); 155 xDelete(r); 156 gsl_permutation_free(perm_p); 157 xDelete(Acopy); 158 #endif 159 return 0; 160 160 } 161 161 /*}}}*/ 162 162 int EDF_fos_reverse_for_solverx(int m, double *dp_U, int n, double *dp_Z, double* dp_x, double* dp_y) { /*{{{*/ 163 // copy to transpose the matrix164 double* transposed=xNew<double>(m*m);165 for (int i=0; i<m; ++i) for (int j=0; j<m; ++j) transposed[j*m+i]=dp_x[i*m+j];166 gsl_matrix_view aTransposed = gsl_matrix_view_array (transposed,m,m);167 // the adjoint of the solution is our right-hand side168 gsl_vector_view x_bar=gsl_vector_view_array(dp_U,m);169 // the last m elements of dp_Z representing the adjoint of the right-hand side we want to compute:170 gsl_vector_view b_bar=gsl_vector_view_array(dp_Z+m*m,m);171 gsl_permutation *p = gsl_permutation_alloc (m);172 int permSign;173 gsl_linalg_LU_decomp (&aTransposed.matrix, p, &permSign);174 gsl_linalg_LU_solve (&aTransposed.matrix, p, &x_bar.vector, &b_bar.vector);175 // now do the adjoint of the matrix176 for (int i=0; i<m; ++i) for (int j=0; j<m; ++j) dp_Z[i*m+j]-=dp_Z[m*m+i]*dp_y[j];177 gsl_permutation_free(p);178 xDelete(transposed);179 return 0;163 // copy to transpose the matrix 164 double* transposed=xNew<double>(m*m); 165 for (int i=0; i<m; ++i) for (int j=0; j<m; ++j) transposed[j*m+i]=dp_x[i*m+j]; 166 gsl_matrix_view aTransposed = gsl_matrix_view_array (transposed,m,m); 167 // the adjoint of the solution is our right-hand side 168 gsl_vector_view x_bar=gsl_vector_view_array(dp_U,m); 169 // the last m elements of dp_Z representing the adjoint of the right-hand side we want to compute: 170 gsl_vector_view b_bar=gsl_vector_view_array(dp_Z+m*m,m); 171 gsl_permutation *p = gsl_permutation_alloc (m); 172 int permSign; 173 gsl_linalg_LU_decomp (&aTransposed.matrix, p, &permSign); 174 gsl_linalg_LU_solve (&aTransposed.matrix, p, &x_bar.vector, &b_bar.vector); 175 // now do the adjoint of the matrix 176 for (int i=0; i<m; ++i) for (int j=0; j<m; ++j) dp_Z[i*m+j]-=dp_Z[m*m+i]*dp_y[j]; 177 gsl_permutation_free(p); 178 xDelete(transposed); 179 return 0; 180 180 } 181 181 /*}}}*/ 182 182 int EDF_fov_reverse_for_solverx(int m, int p, double **dpp_U, int n, double **dpp_Z, double* dp_x, double* dp_y) { /*{{{*/
Note:
See TracBrowser
for help on using the repository browser.
![(please configure the [header_logo] section in trac.ini)](/trac/issm/chrome/common/trac_banner.png)