-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
225 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
using Pkg; Pkg.activate() | ||
|
||
using Gridap,GridapEmbedded,GridapTopOpt | ||
|
||
using GridapEmbedded | ||
using GridapEmbedded.LevelSetCutters | ||
using Gridap.Geometry, Gridap.FESpaces, Gridap.CellData | ||
import Gridap.Geometry: get_node_coordinates, collect1d | ||
include("../embedded_measures.jl") | ||
|
||
function main() | ||
path="./results/UnfittedFEM_thermal_compliance_ALM_AD/" | ||
n = 100 | ||
order = 1 | ||
γ = 0.1 | ||
γ_reinit = 0.5 | ||
max_steps = floor(Int,order*minimum(n)/10) | ||
tol = 1/(5*order^2)/minimum(n) | ||
vf = 0.4 | ||
α_coeff = 4max_steps*γ | ||
iter_mod = 1 | ||
|
||
model = CartesianDiscreteModel((0,1,0,1),(n,n)); | ||
el_Δ = get_el_Δ(model) | ||
f_Γ_D(x) = (x[1] ≈ 0.0 && (x[2] <= 0.2 + eps() || x[2] >= 0.8 - eps())) | ||
f_Γ_N(x) = (x[1] ≈ 1 && 0.4 - eps() <= x[2] <= 0.6 + eps()) | ||
update_labels!(1,model,f_Γ_D,"Gamma_D") | ||
update_labels!(2,model,f_Γ_N,"Gamma_N") | ||
|
||
## Triangulations and measures | ||
Ω = Triangulation(model) | ||
Γ_N = BoundaryTriangulation(model,tags="Gamma_N") | ||
dΩ = Measure(Ω,2*order) | ||
dΓ_N = Measure(Γ_N,2*order) | ||
vol_D = sum(∫(1)dΩ) | ||
|
||
## Spaces | ||
reffe_scalar = ReferenceFE(lagrangian,Float64,order) | ||
V = TestFESpace(model,reffe_scalar;dirichlet_tags=["Gamma_D"]) | ||
U = TrialFESpace(V,0.0) | ||
V_φ = TestFESpace(model,reffe_scalar) | ||
V_reg = TestFESpace(model,reffe_scalar;dirichlet_tags=["Gamma_N"]) | ||
U_reg = TrialFESpace(V_reg,0) | ||
|
||
φh = interpolate(x->-cos(4π*x[1])*cos(4*pi*x[2])/4-0.2/4,V_φ) | ||
embedded_meas = EmbeddedMeasureCache(φh,V_φ) | ||
update_meas(args...) = update_embedded_measures!(embedded_meas,args...) | ||
get_meas(args...) = get_embedded_measures(embedded_meas,args...) | ||
|
||
## Weak form | ||
a(u,v,φ,dΩ,dΓ_N,dΩ1,dΩ2,dΓ) = ∫(∇(u)⋅∇(v))dΩ1 + ∫(10^-3*∇(u)⋅∇(v))dΩ2 | ||
l(v,φ,dΩ,dΓ_N,dΩ1,dΩ2,dΓ) = ∫(v)dΓ_N | ||
|
||
## Optimisation functionals | ||
J(u,φ,dΩ,dΓ_N,dΩ1,dΩ2,dΓ) = ∫(∇(u)⋅∇(u))dΩ1 | ||
Vol(u,φ,dΩ,dΓ_N,dΩ1,dΩ2,dΓ) = ∫(1/vol_D)dΩ1 - ∫(vf/vol_D)dΩ; | ||
|
||
## IntegrandWithEmbeddedMeasure | ||
a_iem = IntegrandWithEmbeddedMeasure(a,(dΩ,dΓ_N),update_meas) | ||
l_iem = IntegrandWithEmbeddedMeasure(l,(dΩ,dΓ_N),update_meas) | ||
J_iem = IntegrandWithEmbeddedMeasure(J,(dΩ,dΓ_N),update_meas) | ||
Vol_iem = IntegrandWithEmbeddedMeasure(Vol,(dΩ,dΓ_N),update_meas) | ||
|
||
## Evolution Method | ||
ls_evo = HamiltonJacobiEvolution(FirstOrderStencil(2,Float64),model,V_φ,tol,max_steps) | ||
|
||
## Setup solver and FE operators | ||
state_map = AffineFEStateMap(a_iem,l_iem,U,V,V_φ,U_reg,φh,(dΩ,dΓ_N)) | ||
pcfs = PDEConstrainedFunctionals(J_iem,[Vol_iem],state_map) | ||
|
||
## Hilbertian extension-regularisation problems | ||
α = α_coeff*maximum(el_Δ) | ||
a_hilb(p,q) =∫(α^2*∇(p)⋅∇(q) + p*q)dΩ; | ||
vel_ext = VelocityExtension(a_hilb,U_reg,V_reg) | ||
|
||
## Optimiser | ||
rm(path,force=true,recursive=true) | ||
mkpath(path) | ||
optimiser = AugmentedLagrangian(pcfs,ls_evo,vel_ext,φh;reinit_mod=5, | ||
γ,γ_reinit,verbose=true,constraint_names=[:Vol]) | ||
for (it,uh,φh) in optimiser | ||
_Ω1,_,_Γ = get_embedded_triangulations(embedded_meas,φh) | ||
if iszero(it % iter_mod) | ||
writevtk(_Ω1,path*"Omega_out$it",cellfields=["φ"=>φh,"|∇(φ)|"=>(norm ∘ ∇(φh)),"uh"=>uh]) | ||
writevtk(_Γ,path*"Gamma_out$it",cellfields=["normal"=>get_normal_vector(_Γ)]) | ||
end | ||
write_history(path*"/history.txt",optimiser.history) | ||
end | ||
it = get_history(optimiser).niter; uh = get_state(pcfs) | ||
_Ω1,_,_Γ = get_embedded_triangulations(embedded_meas,φh) | ||
writevtk(_Ω1,path*"Omega_out$it",cellfields=["φ"=>φh,"|∇(φ)|"=>(norm ∘ ∇(φh)),"uh"=>uh]) | ||
writevtk(_Γ,path*"Gamma_out$it",cellfields=["normal"=>get_normal_vector(_Γ)]) | ||
end | ||
|
||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
using Gridap,GridapTopOpt | ||
|
||
using GridapEmbedded | ||
using GridapEmbedded.LevelSetCutters | ||
using Gridap.Geometry, Gridap.FESpaces, Gridap.CellData | ||
import Gridap.Geometry: get_node_coordinates, collect1d | ||
using GridapDistributed, GridapPETSc, PartitionedArrays | ||
|
||
include("../embedded_measures.jl") | ||
|
||
function main(parts,distribute) | ||
ranks = distribute(LinearIndices((prod(parts),))) | ||
|
||
path="./results/UnfittedFEM_thermal_compliance_ALM_MPI_AD/" | ||
n = 64 | ||
order = 2 | ||
γ = 0.1 | ||
γ_reinit = 0.5 | ||
max_steps = floor(Int,order*minimum(n)/10) | ||
tol = 1/(5*order^2)/minimum(n) | ||
κ = 1 | ||
vf = 0.4 | ||
α_coeff = 4max_steps*γ | ||
iter_mod = 1 | ||
i_am_main(ranks) && rm(path,force=true,recursive=true) | ||
i_am_main(ranks) && mkpath(path) | ||
|
||
model = CartesianDiscreteModel(ranks,parts,(0,1,0,1),(n,n)); | ||
el_Δ = get_el_Δ(model) | ||
f_Γ_D(x) = (x[1] ≈ 0.0 && (x[2] <= 0.2 + eps() || x[2] >= 0.8 - eps())) | ||
f_Γ_N(x) = (x[1] ≈ 1 && 0.4 - eps() <= x[2] <= 0.6 + eps()) | ||
update_labels!(1,model,f_Γ_D,"Gamma_D") | ||
update_labels!(2,model,f_Γ_N,"Gamma_N") | ||
|
||
## Triangulations and measures | ||
Ω = Triangulation(model) | ||
Γ_N = BoundaryTriangulation(model,tags="Gamma_N") | ||
dΩ = Measure(Ω,2*order) | ||
dΓ_N = Measure(Γ_N,2*order) | ||
vol_D = sum(∫(1)dΩ) | ||
|
||
## Spaces | ||
reffe_scalar = ReferenceFE(lagrangian,Float64,order) | ||
V = TestFESpace(model,reffe_scalar;dirichlet_tags=["Gamma_D"]) | ||
U = TrialFESpace(V,0.0) | ||
V_φ = TestFESpace(model,reffe_scalar) | ||
V_reg = TestFESpace(model,reffe_scalar;dirichlet_tags=["Gamma_N"]) | ||
U_reg = TrialFESpace(V_reg,0) | ||
|
||
φh = interpolate(initial_lsf(4,0.2),V_φ) | ||
embedded_meas = EmbeddedMeasureCache(φh,V_φ) | ||
update_meas(args...) = update_embedded_measures!(embedded_meas,args...) | ||
get_meas(args...) = get_embedded_measures(embedded_meas,args...) | ||
|
||
## Weak form | ||
a(u,v,φ,dΩ,dΓ_N,dΩ1,dΩ2,dΓ) = ∫(∇(u)⋅∇(v))dΩ1 + ∫(10^-3*∇(u)⋅∇(v))dΩ2 | ||
l(v,φ,dΩ,dΓ_N,dΩ1,dΩ2,dΓ) = ∫(v)dΓ_N | ||
|
||
## Optimisation functionals | ||
J(u,φ,dΩ,dΓ_N,dΩ1,dΩ2,dΓ) = ∫(∇(u)⋅∇(u))dΩ1 | ||
Vol(u,φ,dΩ,dΓ_N,dΩ1,dΩ2,dΓ) = ∫(1/vol_D)dΩ1 - ∫(vf/vol_D)dΩ; | ||
|
||
## IntegrandWithEmbeddedMeasure | ||
a_iem = IntegrandWithEmbeddedMeasure(a,(dΩ,dΓ_N),update_meas) | ||
l_iem = IntegrandWithEmbeddedMeasure(l,(dΩ,dΓ_N),update_meas) | ||
J_iem = IntegrandWithEmbeddedMeasure(J,(dΩ,dΓ_N),update_meas) | ||
Vol_iem = IntegrandWithEmbeddedMeasure(Vol,(dΩ,dΓ_N),update_meas) | ||
|
||
## Evolution Method | ||
ls_evo = HamiltonJacobiEvolution(FirstOrderStencil(2,Float64),model,V_φ,tol,max_steps) | ||
|
||
## Setup solver and FE operators | ||
state_map = AffineFEStateMap(a_iem,l_iem,U,V,V_φ,U_reg,φh,(dΩ,dΓ_N)) | ||
pcfs = PDEConstrainedFunctionals(J_iem,[Vol_iem],state_map) | ||
|
||
## Hilbertian extension-regularisation problems | ||
α = α_coeff*maximum(el_Δ) | ||
a_hilb(p,q) =∫(α^2*∇(p)⋅∇(q) + p*q)dΩ; | ||
vel_ext = VelocityExtension(a_hilb,U_reg,V_reg) | ||
|
||
## Optimiser | ||
optimiser = AugmentedLagrangian(pcfs,ls_evo,vel_ext,φh;reinit_mod=5, | ||
γ,γ_reinit,verbose=i_am_main(ranks),constraint_names=[:Vol]) | ||
for (it,uh,φh) in optimiser | ||
_Ω1,_,_Γ = get_embedded_triangulations(embedded_meas,φh) | ||
if iszero(it % iter_mod) | ||
writevtk(_Ω1,path*"Omega_out$it",cellfields=["φ"=>φh,"|∇(φ)|"=>(norm ∘ ∇(φh)),"uh"=>uh]) | ||
writevtk(_Γ,path*"Gamma_out$it",cellfields=["normal"=>get_normal_vector(_Γ)]) | ||
end | ||
write_history(path*"/history.txt",optimiser.history;ranks) | ||
end | ||
it = get_history(optimiser).niter; uh = get_state(pcfs) | ||
_Ω1,_,_Γ = get_embedded_triangulations(embedded_meas,φh) | ||
writevtk(_Ω1,path*"Omega_out$it",cellfields=["φ"=>φh,"|∇(φ)|"=>(norm ∘ ∇(φh)),"uh"=>uh]) | ||
writevtk(_Γ,path*"Gamma_out$it",cellfields=["normal"=>get_normal_vector(_Γ)]) | ||
end | ||
|
||
with_mpi() do distribute | ||
parts = (3,3); | ||
main(parts,distribute) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters