Skip to content

Commit

Permalink
Fix INP parser and CG solvers (#170)
Browse files Browse the repository at this point in the history
* fix inp parser and test on MBB.inp

* fix and test CG solvers

* bump version

* fix typeof CG abstol

* fix typeof CG abstol
  • Loading branch information
mohamed82008 authored Feb 3, 2024
1 parent f393247 commit bfd0458
Show file tree
Hide file tree
Showing 10 changed files with 925 additions and 31 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "TopOpt"
uuid = "53a1e1a5-51bb-58a9-8a02-02056cc81109"
authors = ["mohamed82008 <[email protected]>", "yijiangh <[email protected]>"]
version = "0.8.2"
version = "0.8.3"

[deps]
AbstractDifferentiation = "c29ec348-61ec-40c8-8164-b8c60e9d9f3d"
Expand Down
2 changes: 1 addition & 1 deletion src/FEA/assembly_cg_displacement_solvers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function PCGDisplacementSolver(
conv=DefaultCriteria(),
xmin=T(1) / 1000,
cg_max_iter=700,
abstol=zero(real(T)),
abstol=T(1e-7),
penalty=PowerPenalty{T}(1),
prev_penalty=deepcopy(penalty),
preconditioner=identity,
Expand Down
12 changes: 6 additions & 6 deletions src/FEA/matrix_free_cg_displacement_solvers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ abstract type AbstractMatrixFreeSolver <: AbstractDisplacementSolver end
prev_penalty::TP
xmin::T
cg_max_iter::Integer
tol::T
abstol::T
cg_statevars::CGStateVariables{T}
preconditioner::Any
preconditioner_initialized::Base.RefValue{Bool}
Expand All @@ -37,7 +37,7 @@ function StaticMatrixFreeDisplacementSolver(
conv=DefaultCriteria(),
xmin=one(T) / 1000,
cg_max_iter=700,
tol=xmin,
abstol=T(1e-7),
penalty=PowerPenalty{T}(1),
prev_penalty=deepcopy(penalty),
preconditioner=identity,
Expand Down Expand Up @@ -77,7 +77,7 @@ function StaticMatrixFreeDisplacementSolver(
prev_penalty,
xmin,
cg_max_iter,
tol,
abstol,
cg_statevars,
preconditioner,
Ref(false),
Expand Down Expand Up @@ -114,7 +114,7 @@ function (s::StaticMatrixFreeDisplacementSolver)(;
)

@unpack cg_max_iter, cg_statevars = s
@unpack preconditioner_initialized, preconditioner, tol = s
@unpack preconditioner_initialized, preconditioner, abstol = s
operator = buildoperator(s)

if !(preconditioner === identity)
Expand All @@ -128,7 +128,7 @@ function (s::StaticMatrixFreeDisplacementSolver)(;
lhs,
operator,
rhs;
tol=tol,
abstol,
maxiter=cg_max_iter,
log=false,
statevars=cg_statevars,
Expand All @@ -139,7 +139,7 @@ function (s::StaticMatrixFreeDisplacementSolver)(;
lhs,
operator,
rhs;
tol=tol,
abstol,
maxiter=cg_max_iter,
log=false,
statevars=cg_statevars,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
function extract_cload!(
cloads::Dict{TI,Vector{TF}}, file, ::Type{Val{dim}}
) where {TI,TF,dim}
pattern = r"(\d+)\s*,\s*(\d)\s*,\s*(\-?\d+\.\d*E[\+\-]\d{2})"
pattern = r"\s*([-+]?\d*\.?\d+(?:[eE][-+]?\d+)?)\s*,\s*([-+]?\d*\.?\d+(?:[eE][-+]?\d+)?)\s*,\s*([-+]?\d*\.?\d+(?:[eE][-+]?\d+)?)\s*"
line = readline(file)
m = match(stopping_pattern, line)
while m isa Nothing
Expand Down
13 changes: 0 additions & 13 deletions src/TopOptProblems/elementmatrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -142,16 +142,3 @@ function Base.convert(
end
return element_Kes
end

for TM in (:(StaticMatrix{m,m,T}), :(Symmetric{T,<:StaticMatrix{m,m,T}}))
@eval begin
@generated function sumdiag(K::$TM) where {m,T}
return reduce((ex1, ex2) -> :($ex1 + $ex2), [:(K[$j, $j]) for j in 1:m])
end
end
end
@doc """
sumdiag(K::Union{StaticMatrix, Symmetric{<:Any, <:StaticMatrix}})
Computes the sum of the diagonal of the static matrix `K`.
""" sumdiag
3 changes: 2 additions & 1 deletion src/Utilities/Utilities.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Utilities

using ForwardDiff, Ferrite, IterativeSolvers, Requires
using ForwardDiff, Ferrite, IterativeSolvers, Requires, StaticArrays, LinearAlgebra

export AbstractPenalty,
PowerPenalty,
Expand All @@ -13,6 +13,7 @@ export AbstractPenalty,
RaggedArray,
@debug,
compliance,
sumdiag,
meandiag,
density,
find_black_and_white,
Expand Down
13 changes: 13 additions & 0 deletions src/Utilities/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,16 @@ macro forward_property(T, field)
end
end
end

for TM in (:(StaticMatrix{m,m,T}), :(Symmetric{T,<:StaticMatrix{m,m,T}}))
@eval begin
@generated function sumdiag(K::$TM) where {m,T}
return reduce((ex1, ex2) -> :($ex1 + $ex2), [:(K[$j, $j]) for j in 1:m])
end
end
end
@doc """
sumdiag(K::Union{StaticMatrix, Symmetric{<:Any, <:StaticMatrix}})
Computes the sum of the diagonal of the static matrix `K`.
""" sumdiag
10 changes: 5 additions & 5 deletions test/fea/solvers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ force = -1.0
problem = PointLoadCantilever(Val{:Linear}, nels, sizes, E, ν, force)
solver1 = FEASolver(Direct, problem)
# Takes forever to compile
# solver2 = FEASolver(CG, MatrixFree, problem)
solver3 = FEASolver(CG, Assembly, problem)
solver2 = FEASolver(CG, MatrixFree, problem, abstol = 1e-7)
solver3 = FEASolver(CG, Assembly, problem, abstol = 1e-7)

x0 = rand(length(solver1.vars))
solver1.vars .= x0
# solver2.vars .= x0
solver2.vars .= x0
solver3.vars .= x0

solver1()
# solver2()
solver2()
solver3()

#@test solver1.u ≈ solver2.u
@test solver1.u solver2.u
@test solver1.u solver3.u
Loading

2 comments on commit bfd0458

@mohamed82008
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/100194

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.8.3 -m "<description of version>" bfd0458f4f6dfa684944e8f3d6b30396f67861dd
git push origin v0.8.3

Please sign in to comment.