Note
Click here to download the full example code
Solve a 2D short cantilever 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 fraction:
volume_fraction = 0.3
Define the problem type:
problem_name = "Short_Cantilever"
Define the number of elements in the x- and y- directions:
n_x = 50
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_memeber_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_memeber_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:25: Generating HTML XDSM file in : xdsm.html
Execute the scenario:
scenario.execute(input_data={"max_iter": 200, "algo": "NLOPT_MMA"})
INFO - 14:44:25:
INFO - 14:44:25: *** Start MDOScenario execution ***
INFO - 14:44:25: MDOScenario
INFO - 14:44:25: Disciplines: DensityFilter FininiteElementAnalysis MaterialModelInterpolation VolumeFraction
INFO - 14:44:25: MDO formulation: DisciplinaryOpt
INFO - 14:44:25: Optimization problem:
INFO - 14:44:25: minimize compliance(x)
INFO - 14:44:25: with respect to x
INFO - 14:44:25: subject to constraints:
INFO - 14:44:25: volume fraction(x) <= 0.3
INFO - 14:44:25: Solving optimization problem with algorithm NLOPT_MMA:
INFO - 14:44:25: ... 0%| | 0/200 [00:00<?, ?it]
INFO - 14:44:25: ... 1%| | 2/200 [00:00<00:00, 960.74 it/sec, obj=1.46e+3]
INFO - 14:44:26: ... 2%|▏ | 3/200 [00:00<00:00, 643.70 it/sec, obj=1.46e+3]
INFO - 14:44:26: ... 2%|▎ | 5/200 [00:00<00:00, 406.14 it/sec, obj=1.46e+3]
INFO - 14:44:26: ... 4%|▎ | 7/200 [00:00<00:00, 294.29 it/sec, obj=1.46e+3]
INFO - 14:44:26: ... 4%|▍ | 9/200 [00:00<00:00, 231.19 it/sec, obj=1.46e+3]
INFO - 14:44:26: ... 6%|▌ | 11/200 [00:01<00:00, 190.37 it/sec, obj=1.46e+3]
INFO - 14:44:26: ... 6%|▋ | 13/200 [00:01<00:01, 161.98 it/sec, obj=1.46e+3]
INFO - 14:44:27: ... 8%|▊ | 15/200 [00:01<00:01, 141.31 it/sec, obj=1.46e+3]
INFO - 14:44:27: ... 8%|▊ | 17/200 [00:01<00:01, 125.34 it/sec, obj=1.46e+3]
INFO - 14:44:27: ... 10%|▉ | 19/200 [00:01<00:01, 112.67 it/sec, obj=1.46e+3]
INFO - 14:44:27: ... 10%|█ | 21/200 [00:01<00:01, 102.44 it/sec, obj=1.46e+3]
INFO - 14:44:27: ... 12%|█▏ | 23/200 [00:02<00:01, 93.97 it/sec, obj=1.46e+3]
INFO - 14:44:27: ... 12%|█▎ | 25/200 [00:02<00:02, 86.78 it/sec, obj=1.46e+3]
INFO - 14:44:28: ... 14%|█▎ | 27/200 [00:02<00:02, 80.55 it/sec, obj=1.46e+3]
INFO - 14:44:28: ... 14%|█▍ | 29/200 [00:02<00:02, 75.35 it/sec, obj=1.46e+3]
INFO - 14:44:28: ... 16%|█▌ | 31/200 [00:02<00:02, 70.85 it/sec, obj=1.46e+3]
INFO - 14:44:28: ... 16%|█▋ | 33/200 [00:02<00:02, 66.85 it/sec, obj=1.46e+3]
INFO - 14:44:28: ... 18%|█▊ | 35/200 [00:03<00:02, 63.25 it/sec, obj=1.46e+3]
INFO - 14:44:29: ... 18%|█▊ | 37/200 [00:03<00:02, 60.07 it/sec, obj=1.46e+3]
INFO - 14:44:29: ... 20%|█▉ | 39/200 [00:03<00:02, 57.19 it/sec, obj=1.46e+3]
INFO - 14:44:29: ... 20%|██ | 41/200 [00:03<00:02, 54.55 it/sec, obj=1.46e+3]
INFO - 14:44:29: ... 22%|██▏ | 43/200 [00:03<00:03, 52.17 it/sec, obj=1.46e+3]
INFO - 14:44:29: ... 22%|██▎ | 45/200 [00:04<00:03, 49.88 it/sec, obj=1.46e+3]
INFO - 14:44:29: ... 24%|██▎ | 47/200 [00:04<00:03, 47.87 it/sec, obj=1.46e+3]
INFO - 14:44:30: ... 24%|██▍ | 49/200 [00:04<00:03, 45.98 it/sec, obj=1.46e+3]
INFO - 14:44:30: ... 26%|██▌ | 51/200 [00:04<00:03, 44.27 it/sec, obj=1.46e+3]
INFO - 14:44:30: ... 26%|██▋ | 53/200 [00:04<00:03, 42.67 it/sec, obj=1.46e+3]
INFO - 14:44:30: ... 28%|██▊ | 55/200 [00:04<00:03, 41.18 it/sec, obj=1.46e+3]
INFO - 14:44:30: ... 28%|██▊ | 57/200 [00:05<00:03, 39.80 it/sec, obj=1.46e+3]
INFO - 14:44:30: ... 30%|██▉ | 59/200 [00:05<00:03, 38.49 it/sec, obj=1.46e+3]
INFO - 14:44:31: ... 30%|███ | 61/200 [00:05<00:03, 37.28 it/sec, obj=1.46e+3]
INFO - 14:44:31: ... 32%|███▏ | 63/200 [00:05<00:03, 36.16 it/sec, obj=1.46e+3]
INFO - 14:44:31: ... 32%|███▎ | 65/200 [00:05<00:03, 35.09 it/sec, obj=1.46e+3]
INFO - 14:44:31: ... 34%|███▎ | 67/200 [00:05<00:03, 34.08 it/sec, obj=1.46e+3]
INFO - 14:44:31: ... 34%|███▍ | 69/200 [00:06<00:03, 33.14 it/sec, obj=1.46e+3]
INFO - 14:44:31: ... 36%|███▌ | 71/200 [00:06<00:04, 32.23 it/sec, obj=1.46e+3]
INFO - 14:44:32: ... 36%|███▋ | 73/200 [00:06<00:04, 31.38 it/sec, obj=1.46e+3]
INFO - 14:44:32: ... 38%|███▊ | 75/200 [00:06<00:04, 30.57 it/sec, obj=1.46e+3]
INFO - 14:44:32: ... 38%|███▊ | 77/200 [00:06<00:04, 29.81 it/sec, obj=1.46e+3]
INFO - 14:44:32: ... 40%|███▉ | 79/200 [00:06<00:04, 29.08 it/sec, obj=1.46e+3]
INFO - 14:44:32: ... 40%|████ | 81/200 [00:07<00:04, 28.37 it/sec, obj=1.46e+3]
INFO - 14:44:32: ... 42%|████▏ | 83/200 [00:07<00:04, 27.71 it/sec, obj=1.46e+3]
INFO - 14:44:33: ... 42%|████▎ | 85/200 [00:07<00:04, 27.08 it/sec, obj=1.46e+3]
INFO - 14:44:33: ... 44%|████▎ | 87/200 [00:07<00:04, 26.47 it/sec, obj=1.46e+3]
INFO - 14:44:33: ... 44%|████▍ | 89/200 [00:07<00:04, 25.89 it/sec, obj=1.46e+3]
INFO - 14:44:33: ... 46%|████▌ | 91/200 [00:07<00:04, 25.34 it/sec, obj=1.46e+3]
INFO - 14:44:33: ... 46%|████▋ | 93/200 [00:08<00:04, 24.82 it/sec, obj=1.46e+3]
INFO - 14:44:33: ... 48%|████▊ | 95/200 [00:08<00:04, 24.31 it/sec, obj=1.46e+3]
INFO - 14:44:34: ... 48%|████▊ | 97/200 [00:08<00:04, 23.82 it/sec, obj=1.46e+3]
INFO - 14:44:34: ... 50%|████▉ | 99/200 [00:08<00:04, 23.36 it/sec, obj=1.46e+3]
INFO - 14:44:34: ... 50%|█████ | 101/200 [00:08<00:04, 22.91 it/sec, obj=1.46e+3]
INFO - 14:44:34: ... 52%|█████▏ | 103/200 [00:08<00:04, 22.47 it/sec, obj=1.46e+3]
INFO - 14:44:34: ... 52%|█████▎ | 105/200 [00:09<00:04, 22.05 it/sec, obj=1.46e+3]
INFO - 14:44:34: ... 54%|█████▎ | 107/200 [00:09<00:04, 21.65 it/sec, obj=1.46e+3]
INFO - 14:44:35: ... 55%|█████▍ | 109/200 [00:09<00:04, 21.25 it/sec, obj=1.46e+3]
INFO - 14:44:35: ... 56%|█████▌ | 111/200 [00:09<00:04, 20.88 it/sec, obj=1.46e+3]
INFO - 14:44:35: ... 56%|█████▋ | 113/200 [00:09<00:04, 20.51 it/sec, obj=1.46e+3]
INFO - 14:44:35: ... 57%|█████▊ | 115/200 [00:09<00:04, 20.17 it/sec, obj=1.46e+3]
INFO - 14:44:35: ... 58%|█████▊ | 117/200 [00:10<00:04, 19.83 it/sec, obj=1.46e+3]
INFO - 14:44:35: ... 60%|█████▉ | 119/200 [00:10<00:04, 19.50 it/sec, obj=1.46e+3]
INFO - 14:44:36: ... 60%|██████ | 121/200 [00:10<00:04, 19.19 it/sec, obj=1.46e+3]
INFO - 14:44:36: ... 62%|██████▏ | 123/200 [00:10<00:04, 18.88 it/sec, obj=1.46e+3]
INFO - 14:44:36: ... 62%|██████▎ | 125/200 [00:10<00:04, 18.58 it/sec, obj=1.46e+3]
INFO - 14:44:36: ... 64%|██████▎ | 127/200 [00:10<00:03, 18.30 it/sec, obj=1.46e+3]
INFO - 14:44:36: ... 64%|██████▍ | 129/200 [00:11<00:03, 18.02 it/sec, obj=1.46e+3]
INFO - 14:44:36: ... 66%|██████▌ | 131/200 [00:11<00:03, 17.76 it/sec, obj=1.46e+3]
INFO - 14:44:37: ... 66%|██████▋ | 133/200 [00:11<00:03, 17.49 it/sec, obj=1.46e+3]
INFO - 14:44:37: ... 68%|██████▊ | 135/200 [00:11<00:03, 17.24 it/sec, obj=1.46e+3]
INFO - 14:44:37: ... 68%|██████▊ | 137/200 [00:11<00:03, 16.99 it/sec, obj=1.46e+3]
INFO - 14:44:37: ... 70%|██████▉ | 139/200 [00:11<00:03, 16.76 it/sec, obj=1.46e+3]
INFO - 14:44:37: ... 70%|███████ | 141/200 [00:12<00:03, 16.52 it/sec, obj=1.46e+3]
INFO - 14:44:37: ... 72%|███████▏ | 143/200 [00:12<00:03, 16.30 it/sec, obj=1.46e+3]
INFO - 14:44:38: ... 72%|███████▎ | 145/200 [00:12<00:03, 16.07 it/sec, obj=1.46e+3]
INFO - 14:44:38: ... 74%|███████▎ | 147/200 [00:12<00:03, 15.86 it/sec, obj=1.46e+3]
INFO - 14:44:38: ... 74%|███████▍ | 149/200 [00:12<00:03, 15.65 it/sec, obj=1.46e+3]
INFO - 14:44:38: ... 76%|███████▌ | 151/200 [00:12<00:03, 15.45 it/sec, obj=1.46e+3]
INFO - 14:44:38: ... 76%|███████▋ | 153/200 [00:13<00:03, 15.25 it/sec, obj=1.46e+3]
INFO - 14:44:38: ... 78%|███████▊ | 155/200 [00:13<00:02, 15.06 it/sec, obj=1.46e+3]
INFO - 14:44:39: ... 78%|███████▊ | 157/200 [00:13<00:02, 14.87 it/sec, obj=1.46e+3]
INFO - 14:44:39: ... 80%|███████▉ | 159/200 [00:13<00:02, 14.69 it/sec, obj=1.46e+3]
INFO - 14:44:39: ... 80%|████████ | 161/200 [00:13<00:02, 14.51 it/sec, obj=1.46e+3]
INFO - 14:44:39: ... 82%|████████▏ | 163/200 [00:13<00:02, 14.33 it/sec, obj=1.46e+3]
INFO - 14:44:39: ... 82%|████████▎ | 165/200 [00:14<00:02, 14.16 it/sec, obj=1.46e+3]
INFO - 14:44:39: ... 84%|████████▎ | 167/200 [00:14<00:02, 13.99 it/sec, obj=1.46e+3]
INFO - 14:44:40: ... 84%|████████▍ | 169/200 [00:14<00:02, 13.83 it/sec, obj=1.46e+3]
INFO - 14:44:40: ... 86%|████████▌ | 171/200 [00:14<00:02, 13.68 it/sec, obj=1.46e+3]
INFO - 14:44:40: ... 86%|████████▋ | 173/200 [00:14<00:01, 13.52 it/sec, obj=1.46e+3]
INFO - 14:44:40: ... 88%|████████▊ | 175/200 [00:14<00:01, 13.37 it/sec, obj=1.46e+3]
INFO - 14:44:40: ... 88%|████████▊ | 177/200 [00:15<00:01, 13.22 it/sec, obj=1.46e+3]
INFO - 14:44:41: ... 90%|████████▉ | 179/200 [00:15<00:01, 13.07 it/sec, obj=1.46e+3]
INFO - 14:44:41: ... 90%|█████████ | 181/200 [00:15<00:01, 12.92 it/sec, obj=1.46e+3]
INFO - 14:44:41: ... 92%|█████████▏| 183/200 [00:15<00:01, 12.78 it/sec, obj=1.46e+3]
INFO - 14:44:41: ... 92%|█████████▎| 185/200 [00:15<00:01, 12.65 it/sec, obj=1.46e+3]
INFO - 14:44:41: ... 93%|█████████▎| 186/200 [00:15<00:01, 12.58 it/sec, obj=1.46e+3]
INFO - 14:44:41: Optimization result:
INFO - 14:44:41: Optimizer info:
INFO - 14:44:41: Status: None
INFO - 14:44:41: Message: Successive iterates of the objective function are closer than ftol_rel or ftol_abs. GEMSEO Stopped the driver
INFO - 14:44:41: Number of calls to the objective function by the optimizer: 186
INFO - 14:44:41: Solution:
INFO - 14:44:41: The solution is feasible.
INFO - 14:44:41: Objective: 136.56131771172448
INFO - 14:44:41: Standardized constraints:
INFO - 14:44:41: volume fraction - 0.3 = -1.0460364907594055e-08
INFO - 14:44:41: *** End MDOScenario execution (time: 0:00:15.916469) ***
{'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 0x7f3c4e9f91c0>

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 16.520 seconds)