Skip to content

Commit

Permalink
improve projection of PseudoDensities (#159)
Browse files Browse the repository at this point in the history
* improve projection of PseudoDensities

* bump version

* more exports and tests

* formatter toml and formatting
  • Loading branch information
mohamed82008 authored Sep 24, 2023
1 parent 2fcd133 commit 6444ff1
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 16 deletions.
1 change: 1 addition & 0 deletions .JuliaFormatter.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
style = "blue"
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.7.3"
version = "0.8.0"

[deps]
AbstractDifferentiation = "c29ec348-61ec-40c8-8164-b8c60e9d9f3d"
Expand Down
19 changes: 17 additions & 2 deletions src/TopOpt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,28 @@ using Requires, Reexport, ChainRulesCore
struct PseudoDensities{I,P,F,T,N,A<:AbstractArray{T,N}} <: AbstractArray{T,N}
x::A
end
function Base.setindex!(A::PseudoDensities, x, inds...)
return A.x[inds...] = x
end
function PseudoDensities(x::A) where {T,N,A<:AbstractArray{T,N}}
return PseudoDensities{false,false,false,T,N,A}(x)
end
function PseudoDensities{I,P,F}(x::A) where {I,P,F,T,N,A<:AbstractArray{T,N}}
return PseudoDensities{I,P,F,T,N,A}(x)
end

Base.BroadcastStyle(::Type{T}) where {T<:PseudoDensities} = Broadcast.ArrayStyle{T}()
function Base.similar(
bc::Broadcast.Broadcasted{Broadcast.ArrayStyle{T}}, ::Type{ElType}
) where {T,ElType}
return similar(T, axes(bc))
end
function Base.similar(
::Type{<:TV}, axes::Tuple{Union{Integer,Base.OneTo},Vararg{Union{Integer,Base.OneTo}}}
) where {I,P,F,T,N,A,TV<:PseudoDensities{I,P,F,T,N,A}}
return PseudoDensities{I,P,F}(similar(A, axes))
end

function ChainRulesCore.rrule(
::Type{PseudoDensities{I,P,F,T,N,A}}, x::Matrix
) where {I,P,F,T,N,A}
Expand Down Expand Up @@ -104,6 +119,6 @@ export TopOpt,
MMA87,
MMA02,
HeavisideProjection,
ProjectedPenalty,
PowerPenalty
SigmoidProjection,
ProjectedPenalty
end
1 change: 1 addition & 0 deletions src/Utilities/Utilities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ using ForwardDiff, Ferrite, IterativeSolvers, Requires
export AbstractPenalty,
PowerPenalty,
RationalPenalty,
SinhPenalty,
HeavisideProjection,
SigmoidProjection,
ProjectedPenalty,
Expand Down
21 changes: 15 additions & 6 deletions src/Utilities/penalties.jl
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
import ..TopOpt: PseudoDensities

abstract type AbstractPenalty{T} end
abstract type AbstractCPUPenalty{T} <: AbstractPenalty{T} end
abstract type AbstractProjection end

function (P::AbstractCPUPenalty)(x::PseudoDensities{I,<:Any,F}) where {I,F}
return PseudoDensities{I,true,F}(P.(x.x))
end

mutable struct PowerPenalty{T} <: AbstractCPUPenalty{T}
p::T
end
@inline (P::PowerPenalty)(x) = x^(P.p)
(P::PowerPenalty)(x::Real) = x^(P.p)

mutable struct RationalPenalty{T} <: AbstractCPUPenalty{T}
p::T
end
@inline (R::RationalPenalty)(x) = x / (1 + R.p * (1 - x))
(R::RationalPenalty)(x::Real) = x / (1 + R.p * (1 - x))

mutable struct SinhPenalty{T} <: AbstractCPUPenalty{T}
p::T
end
@inline (R::SinhPenalty)(x) = sinh(R.p * x) / sinh(R.p)
(R::SinhPenalty)(x::Real) = sinh(R.p * x) / sinh(R.p)

struct ProjectedPenalty{T,Tpen<:AbstractPenalty{T},Tproj} <: AbstractCPUPenalty{T}
penalty::Tpen
Expand All @@ -24,17 +30,20 @@ end
function ProjectedPenalty(penalty::AbstractPenalty{T}) where {T}
return ProjectedPenalty(penalty, HeavisideProjection(10 * one(T)))
end
@inline (P::ProjectedPenalty)(x) = P.penalty(P.proj(x))
@inline (P::ProjectedPenalty)(x::Real) = P.penalty(P.proj(x))
@forward_property ProjectedPenalty penalty

mutable struct HeavisideProjection{T} <: AbstractProjection
β::T
end
@inline (P::HeavisideProjection)(x) = 1 - exp(-P.β * x) + x * exp(-P.β)
@inline (P::HeavisideProjection)(x::Real) = 1 - exp(-P.β * x) + x * exp(-P.β)
(P::HeavisideProjection)(x::AbstractArray) = P.(x)

mutable struct SigmoidProjection{T} <: AbstractProjection
β::T
end
@inline (P::SigmoidProjection)(x) = 1 / (1 + exp((P.β + 1) * (-x + 0.5)))
@inline (P::SigmoidProjection)(x::Real) = 1 / (1 + exp((P.β + 1) * (-x + 0.5)))
(P::SigmoidProjection)(x::AbstractArray) = P.(x)

import Base: copy
copy(p::TP) where {TP<:AbstractPenalty} = TP(p.p)
Expand Down
34 changes: 27 additions & 7 deletions test/Functions/test_common_fns.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,31 @@ using Ferrite: ndofs_per_cell, getncells

Random.seed!(1)

get_pen_T(::PseudoDensities{<:Any,T,<:Any}) where {T} = T

@testset "Projections and penalties" begin
for proj in (HeavisideProjection(5.0), SigmoidProjection(4.0))
for T1 in (true, false), T2 in (true, false), T3 in (true, false)
x = PseudoDensities{T1,T2,T3}(rand(4))
@test typeof(proj(x)) === typeof(x)
@test typeof(proj.(x)) === typeof(x)
end
end

for pen in (PowerPenalty(3.0), RationalPenalty(3.0), SinhPenalty(3.0))
for T1 in (true, false), T2 in (true, false), T3 in (true, false)
x = PseudoDensities{T1,T2,T3}(rand(4))
@test get_pen_T(pen(x)) === true
@test get_pen_T(ProjectedPenalty(pen)(x)) === true
end
end
end

@testset "Compliance" begin
nels = (2, 2)
problem = HalfMBB(Val{:Linear}, nels, (1.0, 1.0), 1.0, 0.3, 1.0)
for p in (1.0, 2.0, 3.0)
solver = FEASolver(Direct, problem; xmin=0.01, penalty=TopOpt.PowerPenalty(p))
solver = FEASolver(Direct, problem; xmin=0.01, penalty=PowerPenalty(p))
comp = Compliance(solver)
f = x -> comp(PseudoDensities(x))
for i in 1:3
Expand All @@ -29,7 +49,7 @@ end
nels = (2, 2)
problem = HalfMBB(Val{:Linear}, nels, (1.0, 1.0), 1.0, 0.3, 1.0)
for p in (1.0, 2.0, 3.0)
solver = FEASolver(Direct, problem; xmin=0.01, penalty=TopOpt.PowerPenalty(p))
solver = FEASolver(Direct, problem; xmin=0.01, penalty=PowerPenalty(p))
dp = Displacement(solver)
u = dp(PseudoDensities(solver.vars))
for _ in 1:3
Expand All @@ -50,7 +70,7 @@ end
nels = (2, 2)
problem = HalfMBB(Val{:Linear}, nels, (1.0, 1.0), 1.0, 0.3, 1.0)
for p in (1.0, 2.0, 3.0)
solver = FEASolver(Direct, problem; xmin=0.01, penalty=TopOpt.PowerPenalty(p))
solver = FEASolver(Direct, problem; xmin=0.01, penalty=PowerPenalty(p))
vol = Volume(solver)
constr = x -> vol(PseudoDensities(x)) - 0.3
for i in 1:3
Expand All @@ -69,8 +89,8 @@ end
nels = (2, 2)
problem = HalfMBB(Val{:Linear}, nels, (1.0, 1.0), 1.0, 0.3, 1.0)
for p in (1.0, 2.0, 3.0)
solver = FEASolver(Direct, problem; xmin=0.01, penalty=TopOpt.PowerPenalty(p))
filter = TopOpt.DensityFilter(solver; rmin=4.0)
solver = FEASolver(Direct, problem; xmin=0.01, penalty=PowerPenalty(p))
filter = DensityFilter(solver; rmin=4.0)
for i in 1:3
x = rand(prod(nels))
v = rand(prod(nels))
Expand Down Expand Up @@ -114,7 +134,7 @@ end
end
problem = MultiLoad(base_problem, F)
for p in (1.0, 2.0, 3.0)
solver = FEASolver(Direct, problem; xmin=0.01, penalty=TopOpt.PowerPenalty(p))
solver = FEASolver(Direct, problem; xmin=0.01, penalty=PowerPenalty(p))
exact_svd_block = BlockCompliance(problem, solver; method=:exact)
constr = Nonconvex.FunctionWrapper(
x -> exact_svd_block(x) .- 1000.0,
Expand All @@ -138,7 +158,7 @@ end
nels = (2, 2)
problem = HalfMBB(Val{:Linear}, nels, (1.0, 1.0), 1.0, 0.3, 1.0)
for p in (1.0, 2.0, 3.0)
solver = FEASolver(Direct, problem; xmin=0.01, penalty=TopOpt.PowerPenalty(p))
solver = FEASolver(Direct, problem; xmin=0.01, penalty=PowerPenalty(p))
st = StressTensor(solver)
# element stress tensor - element 1
est = st[1]
Expand Down

2 comments on commit 6444ff1

@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/92127

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.0 -m "<description of version>" 6444ff12fcf451116b86b889f4ac27726bf442b4
git push origin v0.8.0

Please sign in to comment.