# Topology optimization using NLopt

**URL:** https://discourse.julialang.org/t/topology-optimization-using-nlopt/101032
**Category:** New to Julia
**Tags:** nlopt
**Created:** [June 30, 2023, 8:50pm UTC](https://discourse.julialang.org/t/topology-optimization-using-nlopt/101032 "2023-06-30T20:50:30Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![mary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mary/32/53139_2.png) [@mary](https://discourse.julialang.org/u/mary)
#### Post date: [June 30, 2023, 8:50pm UTC](https://discourse.julialang.org/t/topology-optimization-using-nlopt/101032/1 "2023-06-30T20:50:30Z")

</div>

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?

```julia
    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.

---

<div class="post-metadata">

### Author: ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)
#### Post date: [June 30, 2023, 9:48pm UTC](https://discourse.julialang.org/t/topology-optimization-using-nlopt/101032/2 "2023-06-30T21:48:28Z")

</div>

You will have to share a complete example to help people help you ([Please read: make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757)). 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](https://github.com/JuliaTopOpt/TopOpt.jl) which is more high level topopt.

---

<div class="post-metadata">

### Author: ![mary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mary/32/53139_2.png) [@mary](https://discourse.julialang.org/u/mary)
#### Post date: [June 30, 2023, 9:56pm UTC](https://discourse.julialang.org/t/topology-optimization-using-nlopt/101032/3 "2023-06-30T21:56:32Z")

</div>

thank you for your suggestion  
it is the code

```julia
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
    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 = [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?

---

<div class="post-metadata">

### Author: ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)
#### Post date: [July 1, 2023, 7:33am UTC](https://discourse.julialang.org/t/topology-optimization-using-nlopt/101032/4 "2023-07-01T07:33:53Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![mary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mary/32/53139_2.png) [@mary](https://discourse.julialang.org/u/mary)
#### Post date: [July 1, 2023, 8:28pm UTC](https://discourse.julialang.org/t/topology-optimization-using-nlopt/101032/5 "2023-07-01T20:28:24Z")

</div>

great thank you i will take a look at it
