public void NewtonRaphson() { internalForcesTotalVector = discretization.CreateTotalInternalForcesVector(); residual = VectorOperations.VectorVectorSubtraction(BoundaryConditionsImposition.ReducedVector(internalForcesTotalVector, boundaryDof), forceVector); residualNorm = VectorOperations.VectorNorm2(residual); int iteration = 0; while (residualNorm > tolerance && iteration < maxIterations) { Array.Clear(discretization.TotalStiffnessMatrix, 0, discretization.TotalStiffnessMatrix.Length); discretization.CreateTotalStiffnessMatrix(); double[,] reducedStiffnessMatrix = BoundaryConditionsImposition.ReducedTotalStiff(discretization.TotalStiffnessMatrix, boundaryDof); DirectSolver linearSolution = new DirectSolver(reducedStiffnessMatrix, residual); linearSolution.SolveWithMethod("PCG"); deltaU = linearSolution.GetSolutionVector; double[] reducedSolutionVector = BoundaryConditionsImposition.ReducedVector(solutionVector, boundaryDof); reducedSolutionVector = VectorOperations.VectorVectorSubtraction(reducedSolutionVector, deltaU); solutionVector = BoundaryConditionsImposition.CreateFullVectorFromReducedVector(reducedSolutionVector, boundaryDof); discretization.UpdateValues(solutionVector); Array.Clear(discretization.internalForcesTotalVector, 0, discretization.internalForcesTotalVector.Length); internalForcesTotalVector = discretization.CreateTotalInternalForcesVector(); residual = VectorOperations.VectorVectorSubtraction(BoundaryConditionsImposition.ReducedVector(internalForcesTotalVector, boundaryDof), forceVector); residualNorm = VectorOperations.VectorNorm2(residual); iteration = iteration + 1; } }
private double [] PCG() { double [,] preconditioner = new double[stiffnessMatrix.GetLength(0), stiffnessMatrix.GetLength(1)]; for (int i = 0; i < preconditioner.GetLength(0); i++) { preconditioner [i, i] = 1 / stiffnessMatrix [i, i]; } double[] residual = VectorOperations.VectorVectorSubtraction( forceVector, VectorOperations.MatrixVectorProduct(stiffnessMatrix, solutionVector) ); double[] preconVector = VectorOperations.MatrixVectorProduct(preconditioner, residual); for (int iter = 0; iter < maxIterations; iter++) { double[] u = VectorOperations.MatrixVectorProduct(stiffnessMatrix, preconVector); double residualDotOld = VectorOperations.VectorDotProduct(residual, preconVector); double alpha = residualDotOld / VectorOperations.VectorDotProduct(preconVector, u); solutionVector = VectorOperations.VectorVectorAddition(solutionVector, VectorOperations.VectorScalarProduct(preconVector, alpha)); residual = VectorOperations.VectorVectorSubtraction(residual, VectorOperations.VectorScalarProduct(u, alpha)); if (VectorOperations.VectorDotProduct(residual, residual) < tolerance) { break; } double residualDotNew = VectorOperations.VectorDotProduct(residual, VectorOperations.MatrixVectorProduct(preconditioner, residual)); double beta = residualDotNew / residualDotOld; preconVector = VectorOperations.VectorVectorAddition( VectorOperations.MatrixVectorProduct(preconditioner, residual), VectorOperations.VectorScalarProduct(preconVector, beta) ); } return(solutionVector); }
private double[] Jacobi() { double[] oldSolution = new double[forceVector.Length]; double[,] Diag = MatrixOperations.PutZerosInDiag(stiffnessMatrix); MatrixOperations.InvertDiagMatrix(Diag); double[,] zeroDiagStiff = MatrixOperations.GetDiagMatrix(stiffnessMatrix); for (int i = 0; i < maxIterations; i++) { oldSolution = solutionVector; solutionVector = VectorOperations.MatrixVectorProduct( Diag, VectorOperations.VectorVectorSubtraction( forceVector, VectorOperations.MatrixVectorProduct(zeroDiagStiff, solutionVector) )); if (VectorOperations.VectorDotProduct(solutionVector, oldSolution) < tolerance) { break; } } return(solutionVector); }