double gradSolver(double *A, double *b, double *x, int n, double e, int it, double *timeGrad, double *timeError){ int i = 0; double prev_norm, norm; double *r = (double *) malloc(n*sizeof(double)); memset(x, 0, n*sizeof(double)); *timeGrad = *timeError = 0.0; *timeError -= timestamp(); residue(A, b, x, r, n); *timeError += timestamp(); prev_norm = residualNorm(r, n); double absErr = e + 1.0; while ((fabs(absErr) > e) && (i++<it)){ *timeGrad -= timestamp(); calcGrad(A, x, r, n); *timeGrad += timestamp(); *timeError -= timestamp(); residue(A, b, x, r, n); *timeError += timestamp(); norm = residualNorm(r, n); absErr = norm - prev_norm; prev_norm = norm; } //*timeGrad /= i; free(r); return norm; }
/// Solve pressure equation, by Newton iterations. void CompressibleTpfa::solve(const double dt, BlackoilState& state, WellState& well_state) { const int nc = grid_.number_of_cells; const int nw = (wells_ != 0) ? wells_->number_of_wells : 0; // Set up dynamic data. computePerSolveDynamicData(dt, state, well_state); computePerIterationDynamicData(dt, state, well_state); // Assemble J and F. assemble(dt, state, well_state); double inc_norm = 0.0; int iter = 0; double res_norm = residualNorm(); std::cout << "\nIteration Residual Change in p\n" << std::setw(9) << iter << std::setw(18) << res_norm << std::setw(18) << '*' << std::endl; while ((iter < maxiter_) && (res_norm > residual_tol_)) { // Solve for increment in Newton method: // incr = x_{n+1} - x_{n} = -J^{-1}F // (J is Jacobian matrix, F is residual) solveIncrement(); ++iter; // Update pressure vars with increment. for (int c = 0; c < nc; ++c) { state.pressure()[c] += pressure_increment_[c]; } for (int w = 0; w < nw; ++w) { well_state.bhp()[w] += pressure_increment_[nc + w]; } // Stop iterating if increment is small. inc_norm = incrementNorm(); if (inc_norm <= change_tol_) { std::cout << std::setw(9) << iter << std::setw(18) << '*' << std::setw(18) << inc_norm << std::endl; break; } // Set up dynamic data. computePerIterationDynamicData(dt, state, well_state); // Assemble J and F. assemble(dt, state, well_state); // Update residual norm. res_norm = residualNorm(); std::cout << std::setw(9) << iter << std::setw(18) << res_norm << std::setw(18) << inc_norm << std::endl; } if ((iter == maxiter_) && (res_norm > residual_tol_) && (inc_norm > change_tol_)) { OPM_THROW(std::runtime_error, "CompressibleTpfa::solve() failed to converge in " << maxiter_ << " iterations."); } std::cout << "Solved pressure in " << iter << " iterations." << std::endl; // Compute fluxes and face pressures. computeResults(state, well_state); }