Skip to content

Commit

Permalink
fix and add scripts for new issue
Browse files Browse the repository at this point in the history
  • Loading branch information
zjwegert committed Mar 14, 2024
1 parent 274a03f commit f548e1e
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 4 deletions.
4 changes: 2 additions & 2 deletions scripts/MPI/3d_inverse_homenisation_ALM.jl
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ function main(mesh_partition,distribute,el_size)

## Optimisation functionals
Cᴴ(r,s,u,φ,dΩ) = ((I φ)*(C (ε(u[r])+εᴹ[r]) εᴹ[s]))dΩ
dCᴴ(r,s,q,u,φ,dΩ) = (q*(C (ε(u[r])+εᴹ[r]) (ε(u[s])+εᴹ[s]))*(DH φ)*(norm (φ)))dΩ
dCᴴ(r,s,q,u,φ,dΩ) = (-q*(C (ε(u[r])+εᴹ[r]) (ε(u[s])+εᴹ[s]))*(DH φ)*(norm (φ)))dΩ
κ(u,φ,dΩ) = -1/9*(Cᴴ(1,1,u,φ,dΩ)+Cᴴ(2,2,u,φ,dΩ)+Cᴴ(3,3,u,φ,dΩ)+
2*(Cᴴ(1,2,u,φ,dΩ)+Cᴴ(1,3,u,φ,dΩ)+Cᴴ(2,3,u,φ,dΩ)))
(q,u,φ,dΩ) = 1/9*(dCᴴ(1,1,q,u,φ,dΩ)+dCᴴ(2,2,q,u,φ,dΩ)+dCᴴ(3,3,q,u,φ,dΩ)+
(q,u,φ,dΩ) = -1/9*(dCᴴ(1,1,q,u,φ,dΩ)+dCᴴ(2,2,q,u,φ,dΩ)+dCᴴ(3,3,q,u,φ,dΩ)+
2*(dCᴴ(1,2,q,u,φ,dΩ)+dCᴴ(1,3,q,u,φ,dΩ)+dCᴴ(2,3,q,u,φ,dΩ)))
Vol(u,φ,dΩ) = (((ρ φ) - vf)/vol_D)dΩ;
dVol(q,u,φ,dΩ) = (-1/vol_D*q*(DH φ)*(norm (φ)))dΩ
Expand Down
4 changes: 2 additions & 2 deletions scripts/Serial/inverse_homenisation_ALM.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ function main()

## Optimisation functionals
Cᴴ(r,s,u,φ,dΩ) = ((I φ)*(C (ε(u[r])+εᴹ[r]) εᴹ[s]))dΩ
dCᴴ(r,s,q,u,φ,dΩ) = (q*(C (ε(u[r])+εᴹ[r]) (ε(u[s])+εᴹ[s]))*(DH φ)*(norm (φ)))dΩ
dCᴴ(r,s,q,u,φ,dΩ) = (-q*(C (ε(u[r])+εᴹ[r]) (ε(u[s])+εᴹ[s]))*(DH φ)*(norm (φ)))dΩ
κ(u,φ,dΩ) = -1/4*(Cᴴ(1,1,u,φ,dΩ)+Cᴴ(2,2,u,φ,dΩ)+2*Cᴴ(1,2,u,φ,dΩ))
(q,u,φ,dΩ) = 1/4*(dCᴴ(1,1,q,u,φ,dΩ)+dCᴴ(2,2,q,u,φ,dΩ)+2*dCᴴ(1,2,q,u,φ,dΩ))
(q,u,φ,dΩ) = -1/4*(dCᴴ(1,1,q,u,φ,dΩ)+dCᴴ(2,2,q,u,φ,dΩ)+2*dCᴴ(1,2,q,u,φ,dΩ))
Vol(u,φ,dΩ) = (((ρ φ) - vf)/vol_D)dΩ
dVol(q,u,φ,dΩ) = (-1/vol_D*q*(DH φ)*(norm (φ)))dΩ

Expand Down
91 changes: 91 additions & 0 deletions scripts/_dev/bug_issue_46/thermal_MPI.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
using LevelSetTopOpt, Gridap, GridapDistributed, GridapPETSc, PartitionedArrays,
SparseMatricesCSR

function main(mesh_partition,distribute,use_l::Bool)
ranks = distribute(LinearIndices((prod(mesh_partition),)))
# FE parameters
order = 1 # Finite element order
xmax = ymax = 1.0 # Domain size
dom = (0,xmax,0,ymax) # Bounding domain
el_size = (200,200) # Mesh partition size
prop_Γ_N = 0.2 # Γ_N size parameter
prop_Γ_D = 0.2 # Γ_D size parameter
f_Γ_N(x) = (x[1] xmax && # Γ_N indicator function
ymax/2-ymax*prop_Γ_N/2 - eps() <= x[2] <= ymax/2+ymax*prop_Γ_N/2 + eps())
f_Γ_D(x) = (x[1] 0.0 && # Γ_D indicator function
(x[2] <= ymax*prop_Γ_D + eps() || x[2] >= ymax-ymax*prop_Γ_D - eps())) # @$\lvert\lvert$@
# FD parameters
γ = 0.1 # HJ eqn time step coeff
γ_reinit = 0.5 # Reinit. eqn time step coeff
max_steps = floor(Int,minimum(el_size)/10) # Max steps for advection
tol = 1/(5order^2)/minimum(el_size) # Advection tolerance
# Problem parameters
κ = 1 # Diffusivity
g = 1 # Heat flow in
vf = 0.4 # Volume fraction constraint
lsf_func = initial_lsf(4,0.2) # Initial level set function
iter_mod = 10 # VTK Output modulo
path = "./results/AD_tut1_MPI_lobj_$use_l/" # Output path
i_am_main(ranks) && mkpath(path) # Create path
# Model
model = CartesianDiscreteModel(ranks,mesh_partition,dom,el_size);
update_labels!(1,model,f_Γ_D,"Gamma_D")
update_labels!(2,model,f_Γ_N,"Gamma_N")
# Triangulation and measures
Ω = Triangulation(model)
Γ_N = BoundaryTriangulation(model,tags="Gamma_N")
= Measure(Ω,2*order)
dΓ_N = Measure(Γ_N,2*order)
# Spaces
reffe = ReferenceFE(lagrangian,Float64,order)
V = TestFESpace(model,reffe;dirichlet_tags=["Gamma_D"])
U = TrialFESpace(V,0.0)
V_φ = TestFESpace(model,reffe)
V_reg = TestFESpace(model,reffe;dirichlet_tags=["Gamma_N"])
U_reg = TrialFESpace(V_reg,0)
# Level set and interpolator
φh = interpolate(lsf_func,V_φ)
interp = SmoothErsatzMaterialInterpolation= 2*maximum(get_el_Δ(model)))
I,H,DH,ρ = interp.I,interp.H,interp.DH,interp.ρ
# Weak formulation
a(u,v,φ,dΩ,dΓ_N) = ((I φ)*κ*(u)(v))dΩ
l(v,φ,dΩ,dΓ_N) = (g*v)dΓ_N
state_map = AffineFEStateMap(a,l,U,V,V_φ,U_reg,φh,dΩ,dΓ_N)
# Objective and constraints
J(u,φ,dΩ,dΓ_N) = if use_l
(g*u)dΓ_N #+ ∫(0)dΩ
else
((I φ)*κ*(u)(u))dΩ
end
dJ(q,u,φ,dΩ,dΓ_N) = *(u)(u)*q*(DH φ)*(norm (φ)))dΩ
vol_D = sum((1)dΩ)
C(u,φ,dΩ,dΓ_N) = (((ρ φ) - vf)/vol_D)dΩ
dC(q,u,φ,dΩ,dΓ_N) = (-1/vol_D*q*(DH φ)*(norm (φ)))dΩ
pcfs = PDEConstrainedFunctionals(J,[C],state_map)
# Velocity extension
α = 4*maximum(get_el_Δ(model))
a_hilb(p,q) =^2*(p)(q) + p*q)dΩ
vel_ext = VelocityExtension(a_hilb, U_reg, V_reg)
# Finite difference scheme
scheme = FirstOrderStencil(length(el_size),Float64)
stencil = AdvectionStencil(scheme,model,V_φ,tol,max_steps)
# Optimiser
optimiser = AugmentedLagrangian(pcfs,stencil,vel_ext,φh;
γ,γ_reinit,verbose=i_am_main(ranks),constraint_names=[:Vol])
# Solve
for (it,uh,φh) in optimiser
data = ["φ"=>φh,"H(φ)"=>(H φh),"|nabla(φ)|"=>(norm (φh)),"uh"=>uh]
iszero(it % iter_mod) && (writevtk(Ω,path*"out$it",cellfields=data);GC.gc())
write_history(path*"/history.txt",get_history(optimiser);ranks)
end
# Final structure
it = get_history(optimiser).niter; uh = get_state(pcfs)
writevtk(Ω,path*"out$it",cellfields=["φ"=>φh,
"H(φ)"=>(H φh),"|nabla(φ)|"=>(norm (φh)),"uh"=>uh])
end

with_mpi() do distribute
mesh_partition = (2,2)
main(mesh_partition,distribute,false)
main(mesh_partition,distribute,true) # <- this should crash
end
87 changes: 87 additions & 0 deletions scripts/_dev/bug_issue_46/thermal_serial.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
using LevelSetTopOpt, Gridap

function main(use_l::Bool)
# FE parameters
order = 1 # Finite element order
xmax = ymax = 1.0 # Domain size
dom = (0,xmax,0,ymax) # Bounding domain
el_size = (200,200) # Mesh partition size
prop_Γ_N = 0.2 # Γ_N size parameter
prop_Γ_D = 0.2 # Γ_D size parameter
f_Γ_N(x) = (x[1] xmax && # Γ_N indicator function
ymax/2-ymax*prop_Γ_N/2 - eps() <= x[2] <= ymax/2+ymax*prop_Γ_N/2 + eps())
f_Γ_D(x) = (x[1] 0.0 && # Γ_D indicator function
(x[2] <= ymax*prop_Γ_D + eps() || x[2] >= ymax-ymax*prop_Γ_D - eps()))
# FD parameters
γ = 0.1 # HJ eqn time step coeff
γ_reinit = 0.5 # Reinit. eqn time step coeff
max_steps = floor(Int,minimum(el_size)/10) # Max steps for advection
tol = 1/(5order^2)/minimum(el_size) # Reinitialisation tolerance
# Problem parameters
κ = 1 # Diffusivity
g = 1 # Heat flow in
vf = 0.4 # Volume fraction constraint
lsf_func = initial_lsf(4,0.2) # Initial level set function
iter_mod = 10 # VTK Output modulo
path = "./results/tut1_lobj_$use_l/" # Output path
mkpath(path) # Create path
# Model
model = CartesianDiscreteModel(dom,el_size);
update_labels!(1,model,f_Γ_D,"Gamma_D")
update_labels!(2,model,f_Γ_N,"Gamma_N")
# Triangulation and measures
Ω = Triangulation(model)
Γ_N = BoundaryTriangulation(model,tags="Gamma_N")
= Measure(Ω,2*order)
dΓ_N = Measure(Γ_N,2*order)
# Spaces
reffe = ReferenceFE(lagrangian,Float64,order)
V = TestFESpace(model,reffe;dirichlet_tags=["Gamma_D"])
U = TrialFESpace(V,0.0)
V_φ = TestFESpace(model,reffe)
V_reg = TestFESpace(model,reffe;dirichlet_tags=["Gamma_N"])
U_reg = TrialFESpace(V_reg,0)
# Level set and interpolator
φh = interpolate(lsf_func,V_φ)
interp = SmoothErsatzMaterialInterpolation= 2*maximum(get_el_Δ(model)))
I,H,DH,ρ = interp.I,interp.H,interp.DH,interp.ρ
# Weak formulation
a(u,v,φ,dΩ,dΓ_N) = ((I φ)*κ*(u)(v))dΩ
l(v,φ,dΩ,dΓ_N) = (g*v)dΓ_N
state_map = AffineFEStateMap(a,l,U,V,V_φ,U_reg,φh,dΩ,dΓ_N)
# Objective and constraints
J(u,φ,dΩ,dΓ_N) = if use_l
(g*u)dΓ_N
else
((I φ)*κ*(u)(u))dΩ
end
dJ(q,u,φ,dΩ,dΓ_N) = *(u)(u)*q*(DH φ)*(norm (φ)))dΩ
vol_D = sum((1)dΩ)
C(u,φ,dΩ,dΓ_N) = (((ρ φ) - vf)/vol_D)dΩ
dC(q,u,φ,dΩ,dΓ_N) = (-1/vol_D*q*(DH φ)*(norm (φ)))dΩ
pcfs = PDEConstrainedFunctionals(J,[C],state_map,
analytic_dJ=dJ,analytic_dC=[dC])
# Velocity extension
α = 4*maximum(get_el_Δ(model))
a_hilb(p,q) =^2*(p)(q) + p*q)dΩ
vel_ext = VelocityExtension(a_hilb,U_reg,V_reg)
# Finite difference scheme
scheme = FirstOrderStencil(length(el_size),Float64)
stencil = AdvectionStencil(scheme,model,V_φ,tol,max_steps)
# Optimiser
optimiser = AugmentedLagrangian(pcfs,stencil,vel_ext,φh;
γ,γ_reinit,verbose=true,constraint_names=[:Vol])
# Solve
for (it,uh,φh) in optimiser
data = ["φ"=>φh,"H(φ)"=>(H φh),"|∇(φ)|"=>(norm (φh)),"uh"=>uh]
iszero(it % iter_mod) && (writevtk(Ω,path*"out$it",cellfields=data);GC.gc())
write_history(path*"/history.txt",get_history(optimiser))
end
# Final structure
it = get_history(optimiser).niter; uh = get_state(pcfs)
writevtk(Ω,path*"out$it",cellfields=["φ"=>φh,
"H(φ)"=>(H φh),"|∇(φ)|"=>(norm (φh)),"uh"=>uh])
end

main(false)
main(true)

0 comments on commit f548e1e

Please sign in to comment.