[13980] | 1 | Index: ../trunk-jpl/src/c/modules/Solverx/SolverxSeq.cpp
|
---|
| 2 | ===================================================================
|
---|
| 3 | --- ../trunk-jpl/src/c/modules/Solverx/SolverxSeq.cpp (revision 13513)
|
---|
| 4 | +++ ../trunk-jpl/src/c/modules/Solverx/SolverxSeq.cpp (revision 13514)
|
---|
| 5 | @@ -70,113 +70,113 @@
|
---|
| 6 |
|
---|
| 7 | #ifdef _HAVE_ADOLC_
|
---|
| 8 | int EDF_for_solverx(int n, IssmPDouble *x, int m, IssmPDouble *y){ /*{{{*/
|
---|
| 9 | - SolverxSeq(y,x, x+m*m, m); // x is where the matrix starts, x+m*m is where the right-hand side starts
|
---|
| 10 | - return 0;
|
---|
| 11 | + SolverxSeq(y,x, x+m*m, m); // x is where the matrix starts, x+m*m is where the right-hand side starts
|
---|
| 12 | + return 0;
|
---|
| 13 | } /*}}}*/
|
---|
| 14 | int EDF_fos_forward_for_solverx(int n, IssmPDouble *inVal, IssmPDouble *inDeriv, int m, IssmPDouble *outVal, IssmPDouble *outDeriv) { /*{{{*/
|
---|
| 15 | #ifdef _HAVE_GSL_
|
---|
| 16 | -// for (int i=0; i<m*m; ++i) std::cout << "EDF_fos_forward_for_solverx A["<< i << "]=" << inVal[i] << std::endl;
|
---|
| 17 | -// for (int i=0; i<m; ++i) std::cout << "EDF_fos_forward_for_solverx b["<< i << "]=" << inVal[i+m*m] << std::endl;
|
---|
| 18 | - // the matrix will be modified by LU decomposition. Use gsl_A copy
|
---|
| 19 | - double* Acopy = xNew<double>(m*m);
|
---|
| 20 | - xMemCpy(Acopy,inVal,m*m);
|
---|
| 21 | - /*Initialize gsl matrices and vectors: */
|
---|
| 22 | - gsl_matrix_view gsl_A = gsl_matrix_view_array (Acopy,m,m);
|
---|
| 23 | - gsl_vector_view gsl_b = gsl_vector_view_array (inVal+m*m,m); // the right hand side starts at address inVal+m*m
|
---|
| 24 | - gsl_permutation *perm_p = gsl_permutation_alloc (m);
|
---|
| 25 | - int signPerm;
|
---|
| 26 | - // factorize
|
---|
| 27 | - gsl_linalg_LU_decomp (&gsl_A.matrix, perm_p, &signPerm);
|
---|
| 28 | - gsl_vector *gsl_x_p = gsl_vector_alloc (m);
|
---|
| 29 | - // solve for the value
|
---|
| 30 | - gsl_linalg_LU_solve (&gsl_A.matrix, perm_p, &gsl_b.vector, gsl_x_p);
|
---|
| 31 | - /*Copy result*/
|
---|
| 32 | - xMemCpy(outVal,gsl_vector_ptr(gsl_x_p,0),m);
|
---|
| 33 | - gsl_vector_free(gsl_x_p);
|
---|
| 34 | -// for (int i=0; i<m; ++i) std::cout << "EDF_fos_forward_for_solverx x["<< i << "]=" << outVal[i] << std::endl;
|
---|
| 35 | - // solve for the derivatives acc. to A * dx = r with r=db - dA * x
|
---|
| 36 | - // compute the RHS
|
---|
| 37 | - double* r=xNew<double>(m);
|
---|
| 38 | - for (int i=0; i<m; i++) {
|
---|
| 39 | - r[i]=inDeriv[m*m+i]; // this is db[i]
|
---|
| 40 | - for (int j=0;j<m; j++) {
|
---|
| 41 | - r[i]-=inDeriv[i*m+j]*outVal[j]; // this is dA[i][j]*x[j]
|
---|
| 42 | - }
|
---|
| 43 | - }
|
---|
| 44 | - gsl_vector_view gsl_r=gsl_vector_view_array(r,m);
|
---|
| 45 | - gsl_vector *gsl_dx_p = gsl_vector_alloc(m);
|
---|
| 46 | - gsl_linalg_LU_solve (&gsl_A.matrix, perm_p, &gsl_r.vector, gsl_dx_p);
|
---|
| 47 | - xMemCpy(outDeriv,gsl_vector_ptr(gsl_dx_p,0),m);
|
---|
| 48 | - gsl_vector_free(gsl_dx_p);
|
---|
| 49 | - xDelete(r);
|
---|
| 50 | - gsl_permutation_free(perm_p);
|
---|
| 51 | - xDelete(Acopy);
|
---|
| 52 | - #endif
|
---|
| 53 | - return 0;
|
---|
| 54 | + // for (int i=0; i<m*m; ++i) std::cout << "EDF_fos_forward_for_solverx A["<< i << "]=" << inVal[i] << std::endl;
|
---|
| 55 | + // for (int i=0; i<m; ++i) std::cout << "EDF_fos_forward_for_solverx b["<< i << "]=" << inVal[i+m*m] << std::endl;
|
---|
| 56 | + // the matrix will be modified by LU decomposition. Use gsl_A copy
|
---|
| 57 | + double* Acopy = xNew<double>(m*m);
|
---|
| 58 | + xMemCpy(Acopy,inVal,m*m);
|
---|
| 59 | + /*Initialize gsl matrices and vectors: */
|
---|
| 60 | + gsl_matrix_view gsl_A = gsl_matrix_view_array (Acopy,m,m);
|
---|
| 61 | + gsl_vector_view gsl_b = gsl_vector_view_array (inVal+m*m,m); // the right hand side starts at address inVal+m*m
|
---|
| 62 | + gsl_permutation *perm_p = gsl_permutation_alloc (m);
|
---|
| 63 | + int signPerm;
|
---|
| 64 | + // factorize
|
---|
| 65 | + gsl_linalg_LU_decomp (&gsl_A.matrix, perm_p, &signPerm);
|
---|
| 66 | + gsl_vector *gsl_x_p = gsl_vector_alloc (m);
|
---|
| 67 | + // solve for the value
|
---|
| 68 | + gsl_linalg_LU_solve (&gsl_A.matrix, perm_p, &gsl_b.vector, gsl_x_p);
|
---|
| 69 | + /*Copy result*/
|
---|
| 70 | + xMemCpy(outVal,gsl_vector_ptr(gsl_x_p,0),m);
|
---|
| 71 | + gsl_vector_free(gsl_x_p);
|
---|
| 72 | + // for (int i=0; i<m; ++i) std::cout << "EDF_fos_forward_for_solverx x["<< i << "]=" << outVal[i] << std::endl;
|
---|
| 73 | + // solve for the derivatives acc. to A * dx = r with r=db - dA * x
|
---|
| 74 | + // compute the RHS
|
---|
| 75 | + double* r=xNew<double>(m);
|
---|
| 76 | + for (int i=0; i<m; i++) {
|
---|
| 77 | + r[i]=inDeriv[m*m+i]; // this is db[i]
|
---|
| 78 | + for (int j=0;j<m; j++) {
|
---|
| 79 | + r[i]-=inDeriv[i*m+j]*outVal[j]; // this is dA[i][j]*x[j]
|
---|
| 80 | + }
|
---|
| 81 | + }
|
---|
| 82 | + gsl_vector_view gsl_r=gsl_vector_view_array(r,m);
|
---|
| 83 | + gsl_vector *gsl_dx_p = gsl_vector_alloc(m);
|
---|
| 84 | + gsl_linalg_LU_solve (&gsl_A.matrix, perm_p, &gsl_r.vector, gsl_dx_p);
|
---|
| 85 | + xMemCpy(outDeriv,gsl_vector_ptr(gsl_dx_p,0),m);
|
---|
| 86 | + gsl_vector_free(gsl_dx_p);
|
---|
| 87 | + xDelete(r);
|
---|
| 88 | + gsl_permutation_free(perm_p);
|
---|
| 89 | + xDelete(Acopy);
|
---|
| 90 | +#endif
|
---|
| 91 | + return 0;
|
---|
| 92 | } /*}}}*/
|
---|
| 93 | int EDF_fov_forward_for_solverx(int n, IssmPDouble *inVal, int directionCount, IssmPDouble **inDeriv, int m, IssmPDouble *outVal, IssmPDouble **outDeriv) { /*{{{*/
|
---|
| 94 | #ifdef _HAVE_GSL_
|
---|
| 95 | - // the matrix will be modified by LU decomposition. Use gsl_A copy
|
---|
| 96 | - double* Acopy = xNew<double>(m*m);
|
---|
| 97 | - xMemCpy(Acopy,inVal,m*m);
|
---|
| 98 | - /*Initialize gsl matrices and vectors: */
|
---|
| 99 | - gsl_matrix_view gsl_A = gsl_matrix_view_array (Acopy,m,m);
|
---|
| 100 | - gsl_vector_view gsl_b = gsl_vector_view_array (inVal+m*m,m); // the right hand side starts at address inVal+m*m
|
---|
| 101 | - gsl_permutation *perm_p = gsl_permutation_alloc (m);
|
---|
| 102 | - int signPerm;
|
---|
| 103 | - // factorize
|
---|
| 104 | - gsl_linalg_LU_decomp (&gsl_A.matrix, perm_p, &signPerm);
|
---|
| 105 | - gsl_vector *gsl_x_p = gsl_vector_alloc (m);
|
---|
| 106 | - // solve for the value
|
---|
| 107 | - gsl_linalg_LU_solve (&gsl_A.matrix, perm_p, &gsl_b.vector, gsl_x_p);
|
---|
| 108 | - /*Copy result*/
|
---|
| 109 | - xMemCpy(outVal,gsl_vector_ptr(gsl_x_p,0),m);
|
---|
| 110 | - gsl_vector_free(gsl_x_p);
|
---|
| 111 | - // solve for the derivatives acc. to A * dx = r with r=db - dA * x
|
---|
| 112 | - double* r=xNew<double>(m);
|
---|
| 113 | - gsl_vector *gsl_dx_p = gsl_vector_alloc(m);
|
---|
| 114 | - for (int dir=0;dir<directionCount;++dir) {
|
---|
| 115 | - // compute the RHS
|
---|
| 116 | - for (int i=0; i<m; i++) {
|
---|
| 117 | - r[i]=inDeriv[m*m+i][dir]; // this is db[i]
|
---|
| 118 | - for (int j=0;j<m; j++) {
|
---|
| 119 | - r[i]-=inDeriv[i*m+j][dir]*outVal[j]; // this is dA[i][j]*x[j]
|
---|
| 120 | - }
|
---|
| 121 | - }
|
---|
| 122 | - gsl_vector_view gsl_r=gsl_vector_view_array(r,m);
|
---|
| 123 | - gsl_linalg_LU_solve (&gsl_A.matrix, perm_p, &gsl_r.vector, gsl_dx_p);
|
---|
| 124 | - // reuse r
|
---|
| 125 | - xMemCpy(r,gsl_vector_ptr(gsl_dx_p,0),m);
|
---|
| 126 | - for (int i=0; i<m; i++) {
|
---|
| 127 | - outDeriv[i][dir]=r[i];
|
---|
| 128 | - }
|
---|
| 129 | - }
|
---|
| 130 | - gsl_vector_free(gsl_dx_p);
|
---|
| 131 | - xDelete(r);
|
---|
| 132 | - gsl_permutation_free(perm_p);
|
---|
| 133 | - xDelete(Acopy);
|
---|
| 134 | - #endif
|
---|
| 135 | - return 0;
|
---|
| 136 | + // the matrix will be modified by LU decomposition. Use gsl_A copy
|
---|
| 137 | + double* Acopy = xNew<double>(m*m);
|
---|
| 138 | + xMemCpy(Acopy,inVal,m*m);
|
---|
| 139 | + /*Initialize gsl matrices and vectors: */
|
---|
| 140 | + gsl_matrix_view gsl_A = gsl_matrix_view_array (Acopy,m,m);
|
---|
| 141 | + gsl_vector_view gsl_b = gsl_vector_view_array (inVal+m*m,m); // the right hand side starts at address inVal+m*m
|
---|
| 142 | + gsl_permutation *perm_p = gsl_permutation_alloc (m);
|
---|
| 143 | + int signPerm;
|
---|
| 144 | + // factorize
|
---|
| 145 | + gsl_linalg_LU_decomp (&gsl_A.matrix, perm_p, &signPerm);
|
---|
| 146 | + gsl_vector *gsl_x_p = gsl_vector_alloc (m);
|
---|
| 147 | + // solve for the value
|
---|
| 148 | + gsl_linalg_LU_solve (&gsl_A.matrix, perm_p, &gsl_b.vector, gsl_x_p);
|
---|
| 149 | + /*Copy result*/
|
---|
| 150 | + xMemCpy(outVal,gsl_vector_ptr(gsl_x_p,0),m);
|
---|
| 151 | + gsl_vector_free(gsl_x_p);
|
---|
| 152 | + // solve for the derivatives acc. to A * dx = r with r=db - dA * x
|
---|
| 153 | + double* r=xNew<double>(m);
|
---|
| 154 | + gsl_vector *gsl_dx_p = gsl_vector_alloc(m);
|
---|
| 155 | + for (int dir=0;dir<directionCount;++dir) {
|
---|
| 156 | + // compute the RHS
|
---|
| 157 | + for (int i=0; i<m; i++) {
|
---|
| 158 | + r[i]=inDeriv[m*m+i][dir]; // this is db[i]
|
---|
| 159 | + for (int j=0;j<m; j++) {
|
---|
| 160 | + r[i]-=inDeriv[i*m+j][dir]*outVal[j]; // this is dA[i][j]*x[j]
|
---|
| 161 | + }
|
---|
| 162 | + }
|
---|
| 163 | + gsl_vector_view gsl_r=gsl_vector_view_array(r,m);
|
---|
| 164 | + gsl_linalg_LU_solve (&gsl_A.matrix, perm_p, &gsl_r.vector, gsl_dx_p);
|
---|
| 165 | + // reuse r
|
---|
| 166 | + xMemCpy(r,gsl_vector_ptr(gsl_dx_p,0),m);
|
---|
| 167 | + for (int i=0; i<m; i++) {
|
---|
| 168 | + outDeriv[i][dir]=r[i];
|
---|
| 169 | + }
|
---|
| 170 | + }
|
---|
| 171 | + gsl_vector_free(gsl_dx_p);
|
---|
| 172 | + xDelete(r);
|
---|
| 173 | + gsl_permutation_free(perm_p);
|
---|
| 174 | + xDelete(Acopy);
|
---|
| 175 | +#endif
|
---|
| 176 | + return 0;
|
---|
| 177 | }
|
---|
| 178 | /*}}}*/
|
---|
| 179 | int EDF_fos_reverse_for_solverx(int m, double *dp_U, int n, double *dp_Z, double* dp_x, double* dp_y) { /*{{{*/
|
---|
| 180 | - // copy to transpose the matrix
|
---|
| 181 | - double* transposed=xNew<double>(m*m);
|
---|
| 182 | - for (int i=0; i<m; ++i) for (int j=0; j<m; ++j) transposed[j*m+i]=dp_x[i*m+j];
|
---|
| 183 | - gsl_matrix_view aTransposed = gsl_matrix_view_array (transposed,m,m);
|
---|
| 184 | - // the adjoint of the solution is our right-hand side
|
---|
| 185 | - gsl_vector_view x_bar=gsl_vector_view_array(dp_U,m);
|
---|
| 186 | - // the last m elements of dp_Z representing the adjoint of the right-hand side we want to compute:
|
---|
| 187 | - gsl_vector_view b_bar=gsl_vector_view_array(dp_Z+m*m,m);
|
---|
| 188 | - gsl_permutation *p = gsl_permutation_alloc (m);
|
---|
| 189 | - int permSign;
|
---|
| 190 | - gsl_linalg_LU_decomp (&aTransposed.matrix, p, &permSign);
|
---|
| 191 | - gsl_linalg_LU_solve (&aTransposed.matrix, p, &x_bar.vector, &b_bar.vector);
|
---|
| 192 | - // now do the adjoint of the matrix
|
---|
| 193 | - 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];
|
---|
| 194 | - gsl_permutation_free(p);
|
---|
| 195 | - xDelete(transposed);
|
---|
| 196 | - return 0;
|
---|
| 197 | + // copy to transpose the matrix
|
---|
| 198 | + double* transposed=xNew<double>(m*m);
|
---|
| 199 | + for (int i=0; i<m; ++i) for (int j=0; j<m; ++j) transposed[j*m+i]=dp_x[i*m+j];
|
---|
| 200 | + gsl_matrix_view aTransposed = gsl_matrix_view_array (transposed,m,m);
|
---|
| 201 | + // the adjoint of the solution is our right-hand side
|
---|
| 202 | + gsl_vector_view x_bar=gsl_vector_view_array(dp_U,m);
|
---|
| 203 | + // the last m elements of dp_Z representing the adjoint of the right-hand side we want to compute:
|
---|
| 204 | + gsl_vector_view b_bar=gsl_vector_view_array(dp_Z+m*m,m);
|
---|
| 205 | + gsl_permutation *p = gsl_permutation_alloc (m);
|
---|
| 206 | + int permSign;
|
---|
| 207 | + gsl_linalg_LU_decomp (&aTransposed.matrix, p, &permSign);
|
---|
| 208 | + gsl_linalg_LU_solve (&aTransposed.matrix, p, &x_bar.vector, &b_bar.vector);
|
---|
| 209 | + // now do the adjoint of the matrix
|
---|
| 210 | + 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];
|
---|
| 211 | + gsl_permutation_free(p);
|
---|
| 212 | + xDelete(transposed);
|
---|
| 213 | + return 0;
|
---|
| 214 | }
|
---|
| 215 | /*}}}*/
|
---|
| 216 | int EDF_fov_reverse_for_solverx(int m, int p, double **dpp_U, int n, double **dpp_Z, double* dp_x, double* dp_y) { /*{{{*/
|
---|