public void constructLinearSystem(BlkSch BS, SparseLinearSystem SPS) { BoundaryCondition[] bc = BS.bc; double[] rhs = SPS.b; double c2 = BS.c2; double c1 = BS.c1; double c0 = BS.c0; double[] x = BS.x; double[] sol = BS.sol; SPS.solution = BS.sol; double[] source = BS.source; int n = BS.sol.Length; applyBoundaryCondition(BS, bc[0], SPS.lower_bc, rhs, x, DIR.LOWER); applyBoundaryCondition(BS, bc[1], SPS.upper_bc, rhs, x, DIR.UPPER); for (int i = 1; i < n - 1; ++i) { double alpha = 2 * c2 * x[i] * x[i] / ((x[i] - x[i - 1]) * (x[i + 1] - x[i - 1])) - c1 * x[i] / (x[i + 1] - x[i - 1]); double beta = 2 * c2 * x[i] * x[i] / ((x[i + 1] - x[i]) * (x[i + 1] - x[i - 1])) + c1 * x[i] / (x[i + 1] - x[i - 1]); SPS.L[i - 1] = -alpha * theta * dt; SPS.D[i - 1] = 1 + (alpha + beta - c0) * theta * dt; SPS.R[i - 1] = -beta * theta * dt; rhs[i] = sol[i - 1] * alpha * (1 - theta) * dt + sol[i] * (1 - (alpha + beta - c0) * (1 - theta) * dt) + sol[i + 1] * beta * (1 - theta) * dt; if (source != null) { rhs[i] += source[i] * dt; } } }
public void advance(double dt) { this.dt = dt; SparseLinearSystem[] sps = new SparseLinearSystem[bs.Length]; for (int i = 0; i < bs.Length; ++i) { sps[i] = new SparseLinearSystem(bs[i].sol.Length); constructLinearSystem(bs[i], sps[i]); } PSOR psor_solver = new PSOR(sps, 1.55, constrain); psor_solver.tolerance = Math.Min(dt, min_dx) * 0.01; psor_solver.maxIter = 200; psor_solver.solve(); Console.WriteLine("Iteration: {0}, Error Norm: {1}", psor_solver.NumIter, psor_solver.ErrorNorm); }