Set fixed values in optimization

Hi, I am doing topology optimization using Nlopt. it seems that it incorrectly delete material under load line? do you know how can i silve this issue? also how can i set constant values in my domain. for example force the model to put material under load line. and this is the code

using NLopt

function gf_p_optimize(p_init; r, β, η,TOL = 1e-4, MAX_ITER = 500, fem_params)
    ##################### Optimize #################
    opt = Opt(:LD_MMA, fem_params.np)
    opt.lower_bounds = 0
    opt.upper_bounds = 1
    opt.ftol_rel = TOL
    opt.maxeval = MAX_ITER
    opt.min_objective = (p0, grad) -> gf_p(p0, grad; r, β, η, fem_params)
    
    (g_opt, p_opt, ret) = optimize(opt, p_init)
    @show numevals = opt.numevals # the number of function evaluations
    return g_opt, p_opt

end

p_opt = fill(0.4, fem_params.np)   # Initial guess
β_list = [4, 8, 16]
g_opt = 0
# g_opt = Inf

TOL = 1e-8
MAX_ITER = 200
for bi = 1 : 3
    β = β_list[bi]
    g_opt, p_temp_opt = gf_p_optimize(p_opt; r, β, η,TOL, MAX_ITER, fem_params)
    global p_opt = p_temp_opt
end

thank you

You can just set the lower bound equal to the upper bound at those points.

1 Like

@stevengj thank you. is there any examples on this to help how to define that?

Use opt.lower_bounds = [0, 0, 0, 0] instead to set a different lower bound for each variable.

I just have one variable (density). but i want to make this variable in loadline as 1.

I just did for those points but as i want to plot both togatehr it gives me error because number of nodes are different in 2 optimizations.

writevtk(Ω,"shapmbb",cellfields=["p_opt"=>p_opt, "p_opt2"=>p_opt2])

error

AssertionError: 

You are trying to build a CellField from an array of length 20
on a Triangulation with 14573 cells. The length of the given array
and the number of cells should match.


Stacktrace:
 [1] macro expansion
   @ C:\Users\marya\.julia\packages\Gridap\971dU\src\Helpers\Macros.jl:60 [inlined]
 [2] CellField(f::Vector{Float64}, trian::BodyFittedTriangulation{2, 2, UnstructuredDiscreteModel{2, 2, Float64, NonOriented}, UnstructuredGrid{2, 2, Float64, NonOriented, Nothing}, Gridap.Arrays.IdentityVector{Int64}}, domain_style::ReferenceDomain)
   @ Gridap.CellData C:\Users\marya\.julia\packages\Gridap\971dU\src\CellData\CellFields.jl:90
 [3] CellField
   @ C:\Users\marya\.julia\packages\Gridap\971dU\src\CellData\CellFields.jl:104 [inlined]
 [4] _prepare_pdata(trian::BodyFittedTriangulation{2, 2, UnstructuredDiscreteModel{2, 2, Float64, NonOriented}, UnstructuredGrid{2, 2, Float64, NonOriented, Nothing}, Gridap.Arrays.IdentityVector{Int64}}, cellfields::Vector{Pair{String, Vector{Float64}}}, samplingpoints::Gridap.Arrays.CompressedArray{Vector{VectorValue{2, Float64}}, 1, Vector{Vector{VectorValue{2, Float64}}}, Vector{Int8}})
   @ Gridap.Visualization C:\Users\marya\.julia\packages\Gridap\971dU\src\Visualization\VisualizationData.jl:140
 [5] visualization_data(trian::BodyFittedTriangulation{2, 2, UnstructuredDiscreteModel{2, 2, Float64, NonOriented}, UnstructuredGrid{2, 2, Float64, NonOriented, Nothing}, Gridap.Arrays.IdentityVector{Int64}}, filebase::String; order::Int64, nsubcells::Int64, celldata::Dict{Any, Any}, cellfields::Vector{Pair{String, Vector{Float64}}})
   @ Gridap.Visualization C:\Users\marya\.julia\packages\Gridap\971dU\src\Visualization\VisualizationData.jl:47
 [6] writevtk(::BodyFittedTriangulation{2, 2, UnstructuredDiscreteModel{2, 2, Float64, NonOriented}, UnstructuredGrid{2, 2, Float64, NonOriented, Nothing}, Gridap.Arrays.IdentityVector{Int64}}, ::Vararg{Any}; kwargs::Base.Pairs{Symbol, Vector{Pair{String, Vector{Float64}}}, Tuple{Symbol}, NamedTuple{(:cellfields,), Tuple{Vector{Pair{String, Vector{Float64}}}}}})
   @ Gridap.Visualization C:\Users\marya\.julia\packages\Gridap\971dU\src\Visualization\Vtk.jl:4
 [7] top-level scope
   @ In[137]:1

and p_opt2 are optimization in those special points.