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 - 16:55:52: Generating HTML XDSM file in : xdsm.html
Execute the scenario
scenario.execute({"max_iter": 200, "algo": "NLOPT_MMA"})
INFO - 16:55:52:
INFO - 16:55:52: *** Start MDOScenario execution ***
INFO - 16:55:52: MDOScenario
INFO - 16:55:52: Disciplines: DensityFilter FininiteElementAnalysis MaterialModelInterpolation VolumeFraction
INFO - 16:55:52: MDO formulation: DisciplinaryOpt
INFO - 16:55:52: Optimization problem:
INFO - 16:55:52: minimize compliance(x)
INFO - 16:55:52: with respect to x
INFO - 16:55:52: subject to constraints:
INFO - 16:55:52: volume fraction(x) <= 0.3
INFO - 16:55:52: Solving optimization problem with algorithm NLOPT_MMA:
INFO - 16:55:52: ... 0%| | 0/200 [00:00<?, ?it]
INFO - 16:55:52: ... 0%| | 1/200 [00:00<00:09, 20.08 it/sec, obj=2.59e+3]
INFO - 16:55:52: ... 1%| | 2/200 [00:00<00:13, 14.40 it/sec, obj=2.23e+3]
INFO - 16:55:52: ... 2%|▏ | 3/200 [00:00<00:11, 16.83 it/sec, obj=1.96e+3]
INFO - 16:55:52: ... 2%|▏ | 4/200 [00:00<00:10, 18.47 it/sec, obj=1.75e+3]
INFO - 16:55:52: ... 2%|▎ | 5/200 [00:00<00:09, 19.54 it/sec, obj=1.57e+3]
INFO - 16:55:52: ... 3%|▎ | 6/200 [00:00<00:09, 20.30 it/sec, obj=1.2e+3]
INFO - 16:55:52: ... 4%|▎ | 7/200 [00:00<00:09, 20.85 it/sec, obj=963]
INFO - 16:55:52: ... 4%|▍ | 8/200 [00:00<00:09, 21.31 it/sec, obj=624]
INFO - 16:55:52: ... 4%|▍ | 9/200 [00:00<00:08, 21.65 it/sec, obj=497]
INFO - 16:55:52: ... 5%|▌ | 10/200 [00:00<00:08, 21.98 it/sec, obj=395]
INFO - 16:55:52: ... 6%|▌ | 11/200 [00:00<00:08, 22.26 it/sec, obj=306]
INFO - 16:55:52: ... 6%|▌ | 12/200 [00:00<00:08, 22.52 it/sec, obj=251]
INFO - 16:55:52: ... 6%|▋ | 13/200 [00:00<00:08, 22.71 it/sec, obj=225]
INFO - 16:55:52: ... 7%|▋ | 14/200 [00:00<00:08, 22.93 it/sec, obj=209]
INFO - 16:55:52: ... 8%|▊ | 15/200 [00:00<00:08, 23.10 it/sec, obj=201]
INFO - 16:55:52: ... 8%|▊ | 16/200 [00:00<00:07, 23.28 it/sec, obj=195]
INFO - 16:55:52: ... 8%|▊ | 17/200 [00:00<00:07, 23.43 it/sec, obj=190]
INFO - 16:55:52: ... 9%|▉ | 18/200 [00:00<00:07, 23.58 it/sec, obj=186]
INFO - 16:55:53: ... 10%|▉ | 19/200 [00:00<00:07, 23.70 it/sec, obj=182]
INFO - 16:55:53: ... 10%|█ | 20/200 [00:00<00:07, 23.84 it/sec, obj=178]
INFO - 16:55:53: ... 10%|█ | 21/200 [00:00<00:07, 23.93 it/sec, obj=174]
INFO - 16:55:53: ... 11%|█ | 22/200 [00:00<00:07, 24.05 it/sec, obj=169]
INFO - 16:55:53: ... 12%|█▏ | 23/200 [00:00<00:07, 24.13 it/sec, obj=164]
INFO - 16:55:53: ... 12%|█▏ | 24/200 [00:00<00:07, 24.24 it/sec, obj=161]
INFO - 16:55:53: ... 12%|█▎ | 25/200 [00:01<00:07, 24.33 it/sec, obj=159]
INFO - 16:55:53: ... 13%|█▎ | 26/200 [00:01<00:07, 24.42 it/sec, obj=157]
INFO - 16:55:53: ... 14%|█▎ | 27/200 [00:01<00:07, 24.49 it/sec, obj=156]
INFO - 16:55:53: ... 14%|█▍ | 28/200 [00:01<00:06, 24.57 it/sec, obj=155]
INFO - 16:55:53: ... 14%|█▍ | 29/200 [00:01<00:06, 24.65 it/sec, obj=154]
INFO - 16:55:53: ... 15%|█▌ | 30/200 [00:01<00:06, 24.71 it/sec, obj=154]
INFO - 16:55:53: ... 16%|█▌ | 31/200 [00:01<00:06, 24.78 it/sec, obj=153]
INFO - 16:55:53: ... 16%|█▌ | 32/200 [00:01<00:06, 24.85 it/sec, obj=153]
INFO - 16:55:53: ... 16%|█▋ | 33/200 [00:01<00:06, 24.93 it/sec, obj=152]
INFO - 16:55:53: ... 17%|█▋ | 34/200 [00:01<00:06, 24.99 it/sec, obj=152]
INFO - 16:55:53: ... 18%|█▊ | 35/200 [00:01<00:06, 25.05 it/sec, obj=152]
INFO - 16:55:53: ... 18%|█▊ | 36/200 [00:01<00:06, 25.10 it/sec, obj=152]
INFO - 16:55:53: ... 18%|█▊ | 37/200 [00:01<00:06, 25.16 it/sec, obj=152]
INFO - 16:55:53: ... 19%|█▉ | 38/200 [00:01<00:06, 25.22 it/sec, obj=152]
INFO - 16:55:53: ... 20%|█▉ | 39/200 [00:01<00:06, 25.26 it/sec, obj=152]
INFO - 16:55:53: ... 20%|██ | 40/200 [00:01<00:06, 25.31 it/sec, obj=152]
INFO - 16:55:53: ... 20%|██ | 41/200 [00:01<00:06, 25.35 it/sec, obj=152]
INFO - 16:55:53: ... 21%|██ | 42/200 [00:01<00:06, 25.39 it/sec, obj=152]
INFO - 16:55:53: ... 22%|██▏ | 43/200 [00:01<00:06, 25.43 it/sec, obj=152]
INFO - 16:55:53: ... 22%|██▏ | 44/200 [00:01<00:06, 25.47 it/sec, obj=152]
INFO - 16:55:53: ... 22%|██▎ | 45/200 [00:01<00:06, 25.50 it/sec, obj=152]
INFO - 16:55:54: ... 23%|██▎ | 46/200 [00:01<00:06, 25.54 it/sec, obj=152]
INFO - 16:55:54: ... 24%|██▎ | 47/200 [00:01<00:05, 25.60 it/sec, obj=152]
INFO - 16:55:54: ... 24%|██▍ | 48/200 [00:01<00:05, 25.65 it/sec, obj=152]
INFO - 16:55:54: ... 24%|██▍ | 49/200 [00:01<00:05, 25.70 it/sec, obj=152]
INFO - 16:55:54: ... 25%|██▌ | 50/200 [00:01<00:05, 25.74 it/sec, obj=152]
INFO - 16:55:54: ... 26%|██▌ | 51/200 [00:01<00:05, 25.77 it/sec, obj=152]
INFO - 16:55:54: ... 26%|██▌ | 52/200 [00:02<00:05, 25.79 it/sec, obj=152]
INFO - 16:55:54: ... 26%|██▋ | 53/200 [00:02<00:05, 25.81 it/sec, obj=152]
INFO - 16:55:54: ... 27%|██▋ | 54/200 [00:02<00:05, 25.84 it/sec, obj=152]
INFO - 16:55:54: ... 28%|██▊ | 55/200 [00:02<00:05, 25.87 it/sec, obj=152]
INFO - 16:55:54: ... 28%|██▊ | 56/200 [00:02<00:05, 25.90 it/sec, obj=152]
INFO - 16:55:54: ... 28%|██▊ | 57/200 [00:02<00:05, 25.92 it/sec, obj=152]
INFO - 16:55:54: ... 29%|██▉ | 58/200 [00:02<00:05, 25.93 it/sec, obj=152]
INFO - 16:55:54: ... 30%|██▉ | 59/200 [00:02<00:05, 25.95 it/sec, obj=152]
INFO - 16:55:54: ... 30%|███ | 60/200 [00:02<00:05, 25.97 it/sec, obj=152]
INFO - 16:55:54: ... 30%|███ | 61/200 [00:02<00:05, 26.00 it/sec, obj=152]
INFO - 16:55:54: ... 31%|███ | 62/200 [00:02<00:05, 26.02 it/sec, obj=152]
INFO - 16:55:54: ... 32%|███▏ | 63/200 [00:02<00:05, 26.04 it/sec, obj=152]
INFO - 16:55:54: ... 32%|███▏ | 64/200 [00:02<00:05, 26.06 it/sec, obj=152]
INFO - 16:55:54: ... 32%|███▎ | 65/200 [00:02<00:05, 26.08 it/sec, obj=152]
INFO - 16:55:54: ... 33%|███▎ | 66/200 [00:02<00:05, 26.10 it/sec, obj=152]
INFO - 16:55:54: ... 34%|███▎ | 67/200 [00:02<00:05, 26.11 it/sec, obj=152]
INFO - 16:55:54: ... 34%|███▍ | 68/200 [00:02<00:05, 26.13 it/sec, obj=152]
INFO - 16:55:54: ... 34%|███▍ | 69/200 [00:02<00:05, 26.15 it/sec, obj=152]
INFO - 16:55:54: ... 35%|███▌ | 70/200 [00:02<00:04, 26.16 it/sec, obj=152]
INFO - 16:55:54: ... 36%|███▌ | 71/200 [00:02<00:04, 26.19 it/sec, obj=152]
INFO - 16:55:54: ... 36%|███▌ | 72/200 [00:02<00:04, 26.21 it/sec, obj=152]
INFO - 16:55:54: ... 36%|███▋ | 73/200 [00:02<00:04, 26.23 it/sec, obj=152]
INFO - 16:55:55: ... 37%|███▋ | 74/200 [00:02<00:04, 26.24 it/sec, obj=152]
INFO - 16:55:55: ... 38%|███▊ | 75/200 [00:02<00:04, 26.27 it/sec, obj=152]
INFO - 16:55:55: ... 38%|███▊ | 76/200 [00:02<00:04, 26.28 it/sec, obj=152]
INFO - 16:55:55: ... 38%|███▊ | 77/200 [00:02<00:04, 26.30 it/sec, obj=152]
INFO - 16:55:55: ... 39%|███▉ | 78/200 [00:02<00:04, 26.32 it/sec, obj=152]
INFO - 16:55:55: ... 40%|███▉ | 79/200 [00:02<00:04, 26.34 it/sec, obj=152]
INFO - 16:55:55: ... 40%|████ | 80/200 [00:03<00:04, 26.36 it/sec, obj=152]
INFO - 16:55:55: ... 40%|████ | 81/200 [00:03<00:04, 26.38 it/sec, obj=152]
INFO - 16:55:55: ... 41%|████ | 82/200 [00:03<00:04, 26.40 it/sec, obj=152]
INFO - 16:55:55: ... 42%|████▏ | 83/200 [00:03<00:04, 26.41 it/sec, obj=152]
INFO - 16:55:55: ... 42%|████▏ | 84/200 [00:03<00:04, 26.40 it/sec, obj=152]
INFO - 16:55:55: ... 42%|████▎ | 85/200 [00:03<00:04, 26.42 it/sec, obj=152]
INFO - 16:55:55: ... 43%|████▎ | 86/200 [00:03<00:04, 26.44 it/sec, obj=152]
INFO - 16:55:55: ... 44%|████▎ | 87/200 [00:03<00:04, 26.45 it/sec, obj=152]
INFO - 16:55:55: ... 44%|████▍ | 88/200 [00:03<00:04, 26.47 it/sec, obj=152]
INFO - 16:55:55: ... 44%|████▍ | 89/200 [00:03<00:04, 26.49 it/sec, obj=152]
INFO - 16:55:55: ... 45%|████▌ | 90/200 [00:03<00:04, 26.51 it/sec, obj=152]
INFO - 16:55:55: ... 46%|████▌ | 91/200 [00:03<00:04, 26.52 it/sec, obj=152]
INFO - 16:55:55: ... 46%|████▌ | 92/200 [00:03<00:04, 26.54 it/sec, obj=152]
INFO - 16:55:55: ... 46%|████▋ | 93/200 [00:03<00:04, 26.56 it/sec, obj=152]
INFO - 16:55:55: ... 47%|████▋ | 94/200 [00:03<00:03, 26.57 it/sec, obj=152]
INFO - 16:55:55: ... 48%|████▊ | 95/200 [00:03<00:03, 26.59 it/sec, obj=152]
INFO - 16:55:55: ... 48%|████▊ | 96/200 [00:03<00:03, 26.60 it/sec, obj=152]
INFO - 16:55:55: ... 48%|████▊ | 97/200 [00:03<00:03, 26.62 it/sec, obj=152]
INFO - 16:55:55: ... 49%|████▉ | 98/200 [00:03<00:03, 26.63 it/sec, obj=152]
INFO - 16:55:55: ... 50%|████▉ | 99/200 [00:03<00:03, 26.64 it/sec, obj=152]
INFO - 16:55:55: ... 50%|█████ | 100/200 [00:03<00:03, 26.65 it/sec, obj=152]
INFO - 16:55:55: ... 50%|█████ | 101/200 [00:03<00:03, 26.66 it/sec, obj=152]
INFO - 16:55:56: ... 51%|█████ | 102/200 [00:03<00:03, 26.67 it/sec, obj=152]
INFO - 16:55:56: ... 52%|█████▏ | 103/200 [00:03<00:03, 26.69 it/sec, obj=152]
INFO - 16:55:56: ... 52%|█████▏ | 104/200 [00:03<00:03, 26.70 it/sec, obj=152]
INFO - 16:55:56: ... 52%|█████▎ | 105/200 [00:03<00:03, 26.71 it/sec, obj=152]
INFO - 16:55:56: ... 53%|█████▎ | 106/200 [00:03<00:03, 26.73 it/sec, obj=152]
INFO - 16:55:56: ... 54%|█████▎ | 107/200 [00:04<00:03, 26.74 it/sec, obj=152]
INFO - 16:55:56: ... 54%|█████▍ | 108/200 [00:04<00:03, 26.75 it/sec, obj=152]
INFO - 16:55:56: ... 55%|█████▍ | 109/200 [00:04<00:03, 26.76 it/sec, obj=152]
INFO - 16:55:56: ... 55%|█████▌ | 110/200 [00:04<00:03, 26.78 it/sec, obj=152]
INFO - 16:55:56: ... 56%|█████▌ | 111/200 [00:04<00:03, 26.79 it/sec, obj=152]
INFO - 16:55:56: ... 56%|█████▌ | 112/200 [00:04<00:03, 26.80 it/sec, obj=152]
INFO - 16:55:56: ... 56%|█████▋ | 113/200 [00:04<00:03, 26.81 it/sec, obj=152]
INFO - 16:55:56: ... 57%|█████▋ | 114/200 [00:04<00:03, 26.82 it/sec, obj=152]
INFO - 16:55:56: ... 57%|█████▊ | 115/200 [00:04<00:03, 26.83 it/sec, obj=152]
INFO - 16:55:56: ... 58%|█████▊ | 116/200 [00:04<00:03, 26.84 it/sec, obj=152]
INFO - 16:55:56: ... 58%|█████▊ | 117/200 [00:04<00:03, 26.86 it/sec, obj=152]
INFO - 16:55:56: ... 59%|█████▉ | 118/200 [00:04<00:03, 26.87 it/sec, obj=152]
INFO - 16:55:56: ... 60%|█████▉ | 119/200 [00:04<00:03, 26.88 it/sec, obj=152]
INFO - 16:55:56: ... 60%|██████ | 120/200 [00:04<00:02, 26.89 it/sec, obj=152]
INFO - 16:55:56: ... 60%|██████ | 121/200 [00:04<00:02, 26.90 it/sec, obj=152]
INFO - 16:55:56: ... 61%|██████ | 122/200 [00:04<00:02, 26.91 it/sec, obj=152]
INFO - 16:55:56: ... 62%|██████▏ | 123/200 [00:04<00:02, 26.92 it/sec, obj=152]
INFO - 16:55:56: ... 62%|██████▏ | 124/200 [00:04<00:02, 26.93 it/sec, obj=152]
INFO - 16:55:56: ... 62%|██████▎ | 125/200 [00:04<00:02, 26.94 it/sec, obj=152]
INFO - 16:55:56: ... 63%|██████▎ | 126/200 [00:04<00:02, 26.95 it/sec, obj=152]
INFO - 16:55:56: ... 64%|██████▎ | 127/200 [00:04<00:02, 26.96 it/sec, obj=152]
INFO - 16:55:56: ... 64%|██████▍ | 128/200 [00:04<00:02, 26.97 it/sec, obj=152]
INFO - 16:55:56: ... 64%|██████▍ | 129/200 [00:04<00:02, 27.16 it/sec, obj=Not evaluated]
INFO - 16:55:56: Optimization result:
INFO - 16:55:56: Optimizer info:
INFO - 16:55:56: Status: None
INFO - 16:55:56: Message: Successive iterates of the objective function are closer than ftol_rel or ftol_abs. GEMSEO Stopped the driver
INFO - 16:55:56: Number of calls to the objective function by the optimizer: 129
INFO - 16:55:56: Solution:
INFO - 16:55:56: The solution is feasible.
INFO - 16:55:56: Objective: 151.62873248988768
INFO - 16:55:56: Standardized constraints:
INFO - 16:55:56: volume fraction - 0.3 = 1.0969287427831098e-06
INFO - 16:55:56: *** End MDOScenario execution (time: 0:00:04.767217) ***
{'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 0x7fbc386dd880>

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.264 seconds)