# Topology optimization using NLopt

Hi, I am doing a topology optimization using Nlop. I have two question i really appreciate your helps.
after defining objective and gradients by using below line nlopt understand to minimize the objective function?

``````    opt.min_objective = (p0, grad) -> gf_p(p0, grad; r, β, η, fem_params)
``````

also when i am doing this it runs without any error but the results are not correct. it exactly delete material below load line which should be strengthen.

You will have to share a complete example to help people help you (Please read: make it easier to help you). You can also look at wrappers of NLopt which might be easier to use and better documented, e.g. Nonconvex.jl, Optimization.jl and JuMP.jl. Then there is TopOpt.jl which is more high level topopt.

1 Like

it is the code

``````using NLopt

function gf_p_optimize(p_init; r, β, η, TOL = 1e-10, MAX_ITER = 800, 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

(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 = [1.0, 1.5, 4.0]
g_opt = 0
# g_opt = Inf

TOL = 1e-10
MAX_ITER = 800
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
@show g_opt,p_opt
``````

also do you know that is TopOpt compatible with a beam that has defined boundary displacement instead of force?

Non-zero boundary conditions are supported but that depends on how you model your beam. If you model it as a continuum 2D/3D volume, then yes it’s possible. If you want discrete topopt with special beam elements which can be bend, then we only support trusses in discrete topopt now. (I am a maintainer of TopOpt.jl). In any case, it may be easier to implement support for what you want in TopOpt.jl instead of rolling your own package from scratch. If you are interested to contribute to TopOpt.jl, message me personally and let’s have a chat.

1 Like

great thank you i will take a look at it