Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Obtaining iteration-wise samples using TopOpt #176

Open
Shantanu21285 opened this issue May 7, 2024 · 0 comments
Open

Obtaining iteration-wise samples using TopOpt #176

Shantanu21285 opened this issue May 7, 2024 · 0 comments

Comments

@Shantanu21285
Copy link

Greetings,

I was wondering if someone could help me out with this. I wish to obtain iteration wise samples of the 3D sample i am optimizing. Let;s say if it takes 100 iterations to converge to the optimized figure, i want all the 3D sample figures at every iteration.

I checked the github but i believe this functionality is not there. Below is my code, could you tell how do i do this?

using TopOpt
using Makie, GeometryBasics, GLMakie # For visualization
using Images
using FileIO

Material properties

E = 1.0 # Young's modulus
v = 0.3 # Poisson's ratio

Force applied

f = -1.0; # Downward force magnitude

Number of elements in each dimension

nels = (30, 10, 10) # Adjust based on desired resolution and computational resources

Define the problem, assuming a cantilever fixed at one end with a point load at the other

problem = PointLoadCantilever(Val{:Linear}, nels, (1.0, 1.0, 1.0), E, v, f)

Optimization settings

V = 0.3 # Volume fraction
xmin = 1e-6 # Minimum density
rmin = 2.0 # Density filter radius

Penalty for intermediate density values

penalty = TopOpt.PowerPenalty(3.0)

Finite element solver setup

solver = FEASolver(Direct, problem; xmin=xmin, penalty=penalty)

Compliance objective

comp = TopOpt.Compliance(solver)
filter = DensityFilter(solver; rmin=rmin)
obj = x -> comp(filter(PseudoDensities(x)))

Volume constraint

volfrac = TopOpt.Volume(solver)
constr = x -> volfrac(filter(PseudoDensities(x))) - V

Initial design (volume fraction as initial guess for all elements)

x0 = fill(V, length(solver.vars))

Define optimization model

model = Model(obj)
addvar!(model, zeros(length(x0)), ones(length(x0)))
add_ineq_constraint!(model, constr)

Optimization algorithm

alg = MMA87()
convcriteria = Nonconvex.KKTCriteria()
options = MMAOptions(; maxiter=3000, tol=Nonconvex.Tolerance(; x=1e-3, f=1e-3, kkt=0.001), convcriteria)

Create a list to store iteration figures

iteration_figures = []

Optimization loop to collect iteration figures

function optimization_loop()
r = optimize(model, alg, x0; options)
push!(iteration_figures, GeometryBasics.Mesh(problem, r.minimizer))
println("Iteration $(length(iteration_figures)): Objective value = $(obj(r.minimizer))")
return r
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant