Skip to content

Commit

Permalink
Removed matrix from the arguments of solve! with a NumericalSetup
Browse files Browse the repository at this point in the history
  • Loading branch information
fverdugo committed Sep 20, 2019
1 parent 901be87 commit ac212d3
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/Algebra/JuliaNLSolvers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function solve!(
kwargs = nls.kwargs
function linsolve!(x,A,b)
ns = numerical_setup(ss,A)
solve!(x,ns,A,b)
solve!(x,ns,b)
end
r = nlsolve(df,x;linsolve=linsolve!,kwargs...)
cache.result = r
Expand Down
19 changes: 11 additions & 8 deletions src/Algebra/LinearSolvers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ numerical_setup(::SymbolicSetup,mat::AbstractMatrix)::NumericalSetup = @abstract

numerical_setup!(::NumericalSetup,mat::AbstractMatrix) = @abstractmethod

solve!(x::AbstractVector,::NumericalSetup,A::AbstractMatrix,b::AbstractVector) = @abstractmethod
solve!(x::AbstractVector,::NumericalSetup,b::AbstractVector) = @abstractmethod

function LinearSolver end

function solve(ls::LinearSolver,A::AbstractMatrix,b::AbstractVector)
ss = symbolic_setup(ls,A)
ns = numerical_setup(ss,A)
x = similar(b)
solve!(x,ns,A,b)
solve!(x,ns,b)
x
end

Expand All @@ -50,7 +50,7 @@ function test_linear_solver(
ss = symbolic_setup(ls,A)
ns = numerical_setup(ss,A)
numerical_setup!(ns,A)
solve!(y,ns,A,b)
solve!(y,ns,b)
@test x y

end
Expand All @@ -76,7 +76,7 @@ function numerical_setup!(ns::LUNumericalSetup, mat::AbstractMatrix)
end

function solve!(
x::AbstractVector,ns::LUNumericalSetup,A::AbstractMatrix,b::AbstractVector)
x::AbstractVector,ns::LUNumericalSetup,b::AbstractVector)
y = ns.factors\b # the allocation of y can be avoided
x .= y
end
Expand All @@ -89,18 +89,21 @@ struct BackslashSolver <: LinearSolver end

struct BackslashSymbolicSetup <: SymbolicSetup end

struct BackslashNumericalSetup <: NumericalSetup end
mutable struct BackslashNumericalSetup{T<:AbstractMatrix} <: NumericalSetup
A::T
end

symbolic_setup(::BackslashSolver,mat::AbstractMatrix) = BackslashSymbolicSetup()

numerical_setup(::BackslashSymbolicSetup,mat::AbstractMatrix) = BackslashNumericalSetup()
numerical_setup(::BackslashSymbolicSetup,mat::AbstractMatrix) = BackslashNumericalSetup(mat)

function numerical_setup!(ns::BackslashNumericalSetup, mat::AbstractMatrix)
ns.A = mat
end

function solve!(
x::AbstractVector,ns::BackslashNumericalSetup,A::AbstractMatrix,b::AbstractVector)
copyto!(x, A\b)
x::AbstractVector,ns::BackslashNumericalSetup,b::AbstractVector)
copyto!(x, ns.A\b)
end

end
2 changes: 1 addition & 1 deletion src/Algebra/NonLinearSolvers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ function _solve_nr!(x,A,b,dx,ns,nls,op)

# Solve linearized problem
broadcast!(*,b,b,-1)
solve!(dx,ns,A,b)
solve!(dx,ns,b)
broadcast!(+,x,x,dx)

# Check convergence for the current residual
Expand Down
4 changes: 2 additions & 2 deletions src/FESpaces/FEOperators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ function solve!(uh::FEFunctionLike,s::LinearFESolver,o::LinearFEOperator)
b = o.vec
ss = symbolic_setup(s.ls,A)
ns = numerical_setup(ss,A)
solve!(x,ns,A,b)
solve!(x,ns,b)
U = TrialFESpace(o)
FEFunction(U,x), ns
end
Expand All @@ -253,7 +253,7 @@ function solve!(uh::FEFunctionLike,s::LinearFESolver,o::LinearFEOperator,ns::Num
x = free_dofs(uh)
A = o.mat
b = o.vec
solve!(x,ns,A,b)
solve!(x,ns,b)
U = TrialFESpace(o)
FEFunction(U,x)
end
Expand Down

0 comments on commit ac212d3

Please sign in to comment.