Note
Click here to download the full example code
Solve a 2D L-shape topology optimization problem¶
from __future__ import annotations
import matplotlib.pyplot as plt
from gemseo.api import configure_logger
from gemseo.api import create_scenario
from gemseo.problems.topo_opt.topopt_initialize import (
initialize_design_space_and_discipline_to,
)
from matplotlib import colors
configure_logger()
<RootLogger root (INFO)>
Setup the topology optimization problem¶
Define the target volume fractio:
volume_fraction = 0.3
Define the problem type:
problem_name = "L-Shape"
Define the number of elements in the x- and y- directions:
n_x = 25
n_y = 25
Define the full material Young’s modulus and Poisson’s ratio:
e0 = 1
nu = 0.3
Define the penalty of the SIMP approach:
penalty = 3
Define the minimum member size in the solution:
min_member_size = 1.5
Instantiate the DesignSpace
and the disciplines:
design_space, disciplines = initialize_design_space_and_discipline_to(
problem=problem_name,
n_x=n_x,
n_y=n_y,
e0=e0,
nu=nu,
penalty=penalty,
min_member_size=min_member_size,
vf0=volume_fraction,
)
Solve the topology optimization problem¶
Generate a MDOScenario
:
scenario = create_scenario(
disciplines,
formulation="DisciplinaryOpt",
objective_name="compliance",
design_space=design_space,
)
Add the volume fraction constraint to the scenario:
scenario.add_constraint("volume fraction", "ineq", value=volume_fraction)
Generate the XDSM
scenario.xdsmize()
INFO - 14:44:04: Generating HTML XDSM file in : xdsm.html
Execute the scenario
scenario.execute({"max_iter": 200, "algo": "NLOPT_MMA"})
INFO - 14:44:04:
INFO - 14:44:04: *** Start MDOScenario execution ***
INFO - 14:44:04: MDOScenario
INFO - 14:44:04: Disciplines: DensityFilter FininiteElementAnalysis MaterialModelInterpolation VolumeFraction
INFO - 14:44:04: MDO formulation: DisciplinaryOpt
INFO - 14:44:04: Optimization problem:
INFO - 14:44:04: minimize compliance(x)
INFO - 14:44:04: with respect to x
INFO - 14:44:04: subject to constraints:
INFO - 14:44:04: volume fraction(x) <= 0.3
INFO - 14:44:04: Solving optimization problem with algorithm NLOPT_MMA:
INFO - 14:44:04: ... 0%| | 0/200 [00:00<?, ?it]
INFO - 14:44:04: ... 1%| | 2/200 [00:00<00:00, 1975.19 it/sec, obj=2.59e+3]
INFO - 14:44:04: ... 2%|▎ | 5/200 [00:00<00:00, 870.94 it/sec, obj=2.59e+3]
INFO - 14:44:04: ... 4%|▍ | 8/200 [00:00<00:00, 552.35 it/sec, obj=2.59e+3]
INFO - 14:44:04: ... 6%|▌ | 11/200 [00:00<00:00, 404.04 it/sec, obj=2.59e+3]
INFO - 14:44:05: ... 7%|▋ | 14/200 [00:00<00:00, 319.06 it/sec, obj=2.59e+3]
INFO - 14:44:05: ... 8%|▊ | 17/200 [00:00<00:00, 264.49 it/sec, obj=2.59e+3]
INFO - 14:44:05: ... 10%|█ | 20/200 [00:00<00:00, 226.86 it/sec, obj=2.59e+3]
INFO - 14:44:05: ... 12%|█▏ | 23/200 [00:01<00:00, 198.64 it/sec, obj=2.59e+3]
INFO - 14:44:05: ... 13%|█▎ | 26/200 [00:01<00:00, 176.24 it/sec, obj=2.59e+3]
INFO - 14:44:05: ... 14%|█▍ | 29/200 [00:01<00:01, 158.83 it/sec, obj=2.59e+3]
INFO - 14:44:05: ... 16%|█▌ | 32/200 [00:01<00:01, 144.63 it/sec, obj=2.59e+3]
INFO - 14:44:05: ... 18%|█▊ | 35/200 [00:01<00:01, 133.02 it/sec, obj=2.59e+3]
INFO - 14:44:06: ... 19%|█▉ | 38/200 [00:01<00:01, 123.05 it/sec, obj=2.59e+3]
INFO - 14:44:06: ... 20%|██ | 41/200 [00:01<00:01, 114.53 it/sec, obj=2.59e+3]
INFO - 14:44:06: ... 22%|██▏ | 44/200 [00:01<00:01, 107.08 it/sec, obj=2.59e+3]
INFO - 14:44:06: ... 24%|██▎ | 47/200 [00:01<00:01, 100.61 it/sec, obj=2.59e+3]
INFO - 14:44:06: ... 25%|██▌ | 50/200 [00:02<00:01, 94.87 it/sec, obj=2.59e+3]
INFO - 14:44:06: ... 26%|██▋ | 53/200 [00:02<00:01, 89.61 it/sec, obj=2.59e+3]
INFO - 14:44:06: ... 28%|██▊ | 56/200 [00:02<00:01, 84.96 it/sec, obj=2.59e+3]
INFO - 14:44:06: ... 30%|██▉ | 59/200 [00:02<00:01, 80.74 it/sec, obj=2.59e+3]
INFO - 14:44:06: ... 31%|███ | 62/200 [00:02<00:01, 76.95 it/sec, obj=2.59e+3]
INFO - 14:44:07: ... 32%|███▎ | 65/200 [00:02<00:01, 73.44 it/sec, obj=2.59e+3]
INFO - 14:44:07: ... 34%|███▍ | 68/200 [00:02<00:01, 70.29 it/sec, obj=2.59e+3]
INFO - 14:44:07: ... 36%|███▌ | 71/200 [00:02<00:01, 67.38 it/sec, obj=2.59e+3]
INFO - 14:44:07: ... 37%|███▋ | 74/200 [00:03<00:01, 64.66 it/sec, obj=2.59e+3]
INFO - 14:44:07: ... 38%|███▊ | 77/200 [00:03<00:01, 62.26 it/sec, obj=2.59e+3]
INFO - 14:44:07: ... 40%|████ | 80/200 [00:03<00:01, 60.06 it/sec, obj=2.59e+3]
INFO - 14:44:07: ... 42%|████▏ | 83/200 [00:03<00:02, 57.98 it/sec, obj=2.59e+3]
INFO - 14:44:07: ... 43%|████▎ | 86/200 [00:03<00:02, 55.95 it/sec, obj=2.59e+3]
INFO - 14:44:08: ... 44%|████▍ | 89/200 [00:03<00:02, 54.07 it/sec, obj=2.59e+3]
INFO - 14:44:08: ... 46%|████▌ | 92/200 [00:03<00:02, 52.32 it/sec, obj=2.59e+3]
INFO - 14:44:08: ... 48%|████▊ | 95/200 [00:03<00:02, 50.74 it/sec, obj=2.59e+3]
INFO - 14:44:08: ... 49%|████▉ | 98/200 [00:04<00:02, 49.27 it/sec, obj=2.59e+3]
INFO - 14:44:08: ... 50%|█████ | 101/200 [00:04<00:02, 47.84 it/sec, obj=2.59e+3]
INFO - 14:44:08: ... 52%|█████▏ | 104/200 [00:04<00:02, 46.50 it/sec, obj=2.59e+3]
INFO - 14:44:08: ... 54%|█████▎ | 107/200 [00:04<00:02, 45.26 it/sec, obj=2.59e+3]
INFO - 14:44:08: ... 55%|█████▌ | 110/200 [00:04<00:02, 44.09 it/sec, obj=2.59e+3]
INFO - 14:44:09: ... 56%|█████▋ | 113/200 [00:04<00:02, 42.96 it/sec, obj=2.59e+3]
INFO - 14:44:09: ... 58%|█████▊ | 116/200 [00:04<00:02, 41.89 it/sec, obj=2.59e+3]
INFO - 14:44:09: ... 60%|█████▉ | 119/200 [00:04<00:01, 40.88 it/sec, obj=2.59e+3]
INFO - 14:44:09: ... 61%|██████ | 122/200 [00:05<00:01, 39.91 it/sec, obj=2.59e+3]
INFO - 14:44:09: ... 62%|██████▎ | 125/200 [00:05<00:01, 38.99 it/sec, obj=2.59e+3]
INFO - 14:44:09: ... 64%|██████▍ | 128/200 [00:05<00:01, 38.11 it/sec, obj=2.59e+3]
INFO - 14:44:09: ... 64%|██████▍ | 129/200 [00:05<00:01, 37.80 it/sec, obj=2.59e+3]
INFO - 14:44:09: Optimization result:
INFO - 14:44:09: Optimizer info:
INFO - 14:44:09: Status: None
INFO - 14:44:09: Message: Successive iterates of the objective function are closer than ftol_rel or ftol_abs. GEMSEO Stopped the driver
INFO - 14:44:09: Number of calls to the objective function by the optimizer: 129
INFO - 14:44:09: Solution:
INFO - 14:44:09: The solution is feasible.
INFO - 14:44:09: Objective: 151.62873248988768
INFO - 14:44:09: Standardized constraints:
INFO - 14:44:09: volume fraction - 0.3 = 1.0969287427831098e-06
INFO - 14:44:09: *** End MDOScenario execution (time: 0:00:05.305800) ***
{'max_iter': 200, 'algo': 'NLOPT_MMA'}
Results¶
Post-process the optimization history:
scenario.post_process(
"BasicHistory",
variable_names=["compliance"],
save=True,
show=False,
file_name=problem_name + "_history.png",
)
<gemseo.post.basic_history.BasicHistory object at 0x7f3c13328760>

Plot the solution
plt.ion() # Ensure that redrawing is possible
fig, ax = plt.subplots()
im = ax.imshow(
-scenario.optimization_result.x_opt.reshape((n_x, n_y)).T,
cmap="gray",
interpolation="none",
norm=colors.Normalize(vmin=-1, vmax=0),
)
fig.show()
im.set_array(-scenario.optimization_result.x_opt.reshape((n_x, n_y)).T)
fig.canvas.draw()
plt.savefig(problem_name + "_solution.png")


Total running time of the script: ( 0 minutes 5.821 seconds)