Note
Click here to download the full example code
Solve a 2D short cantilever topology optimization problem¶
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()
Out:
<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()
Out:
INFO - 10:03:34: Generating HTML XDSM file in : xdsm.html
Execute the scenario:
scenario.execute(input_data={"max_iter": 200, "algo": "NLOPT_MMA"})
Out:
INFO - 10:03:34:
INFO - 10:03:34: *** Start MDOScenario execution ***
INFO - 10:03:34: MDOScenario
INFO - 10:03:34: Disciplines: DensityFilter MaterialModelInterpolation FininiteElementAnalysis VolumeFraction
INFO - 10:03:34: MDO formulation: DisciplinaryOpt
INFO - 10:03:34: Optimization problem:
INFO - 10:03:34: minimize compliance(x)
INFO - 10:03:34: with respect to x
INFO - 10:03:34: subject to constraints:
INFO - 10:03:34: volume fraction(x) <= 0.3
INFO - 10:03:34: Solving optimization problem with algorithm NLOPT_MMA:
INFO - 10:03:34: ... 0%| | 0/200 [00:00<?, ?it]
INFO - 10:03:35: ... 1%| | 2/200 [00:00<00:00, 982.79 it/sec]
INFO - 10:03:35: ... 2%|▏ | 4/200 [00:00<00:00, 510.53 it/sec]
INFO - 10:03:35: ... 3%|▎ | 6/200 [00:00<00:00, 351.28 it/sec]
INFO - 10:03:35: ... 4%|▍ | 8/200 [00:00<00:00, 267.39 it/sec]
INFO - 10:03:35: ... 5%|▌ | 10/200 [00:00<00:00, 215.24 it/sec]
INFO - 10:03:35: ... 6%|▌ | 12/200 [00:01<00:01, 180.43 it/sec]
INFO - 10:03:36: ... 7%|▋ | 14/200 [00:01<00:01, 155.48 it/sec]
INFO - 10:03:36: ... 8%|▊ | 16/200 [00:01<00:01, 136.65 it/sec]
INFO - 10:03:36: ... 9%|▉ | 18/200 [00:01<00:01, 121.72 it/sec]
INFO - 10:03:36: ... 10%|█ | 20/200 [00:01<00:01, 109.96 it/sec]
INFO - 10:03:36: ... 11%|█ | 22/200 [00:01<00:01, 100.31 it/sec]
INFO - 10:03:36: ... 12%|█▏ | 24/200 [00:02<00:01, 92.08 it/sec]
INFO - 10:03:37: ... 13%|█▎ | 26/200 [00:02<00:02, 85.19 it/sec]
INFO - 10:03:37: ... 14%|█▍ | 28/200 [00:02<00:02, 79.34 it/sec]
INFO - 10:03:37: ... 15%|█▌ | 30/200 [00:02<00:02, 74.32 it/sec]
INFO - 10:03:37: ... 16%|█▌ | 32/200 [00:02<00:02, 69.91 it/sec]
INFO - 10:03:37: ... 17%|█▋ | 34/200 [00:03<00:02, 66.05 it/sec]
INFO - 10:03:37: ... 18%|█▊ | 36/200 [00:03<00:02, 62.61 it/sec]
INFO - 10:03:38: ... 19%|█▉ | 38/200 [00:03<00:02, 59.52 it/sec]
INFO - 10:03:38: ... 20%|██ | 40/200 [00:03<00:02, 56.70 it/sec]
INFO - 10:03:38: ... 21%|██ | 42/200 [00:03<00:02, 54.19 it/sec]
INFO - 10:03:38: ... 22%|██▏ | 44/200 [00:03<00:03, 51.86 it/sec]
INFO - 10:03:38: ... 23%|██▎ | 46/200 [00:04<00:03, 49.71 it/sec]
INFO - 10:03:38: ... 24%|██▍ | 48/200 [00:04<00:03, 47.75 it/sec]
INFO - 10:03:39: ... 25%|██▌ | 50/200 [00:04<00:03, 45.94 it/sec]
INFO - 10:03:39: ... 26%|██▌ | 52/200 [00:04<00:03, 44.24 it/sec]
INFO - 10:03:39: ... 27%|██▋ | 54/200 [00:04<00:03, 42.65 it/sec]
INFO - 10:03:39: ... 28%|██▊ | 56/200 [00:04<00:03, 41.20 it/sec]
INFO - 10:03:39: ... 29%|██▉ | 58/200 [00:05<00:03, 39.85 it/sec]
INFO - 10:03:39: ... 30%|███ | 60/200 [00:05<00:03, 38.57 it/sec]
INFO - 10:03:40: ... 31%|███ | 62/200 [00:05<00:03, 37.39 it/sec]
INFO - 10:03:40: ... 32%|███▏ | 64/200 [00:05<00:03, 36.26 it/sec]
INFO - 10:03:40: ... 33%|███▎ | 66/200 [00:05<00:03, 35.18 it/sec]
INFO - 10:03:40: ... 34%|███▍ | 68/200 [00:05<00:03, 34.20 it/sec]
INFO - 10:03:40: ... 35%|███▌ | 70/200 [00:06<00:03, 33.25 it/sec]
INFO - 10:03:40: ... 36%|███▌ | 72/200 [00:06<00:03, 32.34 it/sec]
INFO - 10:03:41: ... 37%|███▋ | 74/200 [00:06<00:04, 31.50 it/sec]
INFO - 10:03:41: ... 38%|███▊ | 76/200 [00:06<00:04, 30.70 it/sec]
INFO - 10:03:41: ... 39%|███▉ | 78/200 [00:06<00:04, 29.95 it/sec]
INFO - 10:03:41: ... 40%|████ | 80/200 [00:06<00:04, 29.22 it/sec]
INFO - 10:03:41: ... 41%|████ | 82/200 [00:07<00:04, 28.53 it/sec]
INFO - 10:03:41: ... 42%|████▏ | 84/200 [00:07<00:04, 27.87 it/sec]
INFO - 10:03:42: ... 43%|████▎ | 86/200 [00:07<00:04, 27.23 it/sec]
INFO - 10:03:42: ... 44%|████▍ | 88/200 [00:07<00:04, 26.63 it/sec]
INFO - 10:03:42: ... 45%|████▌ | 90/200 [00:07<00:04, 26.06 it/sec]
INFO - 10:03:42: ... 46%|████▌ | 92/200 [00:07<00:04, 25.51 it/sec]
INFO - 10:03:42: ... 47%|████▋ | 94/200 [00:08<00:04, 24.98 it/sec]
INFO - 10:03:42: ... 48%|████▊ | 96/200 [00:08<00:04, 24.48 it/sec]
INFO - 10:03:43: ... 49%|████▉ | 98/200 [00:08<00:04, 23.99 it/sec]
INFO - 10:03:43: ... 50%|█████ | 100/200 [00:08<00:04, 23.51 it/sec]
INFO - 10:03:43: ... 51%|█████ | 102/200 [00:08<00:04, 23.04 it/sec]
INFO - 10:03:43: ... 52%|█████▏ | 104/200 [00:08<00:04, 22.61 it/sec]
INFO - 10:03:43: ... 53%|█████▎ | 106/200 [00:09<00:04, 22.19 it/sec]
INFO - 10:03:43: ... 54%|█████▍ | 108/200 [00:09<00:04, 21.79 it/sec]
INFO - 10:03:44: ... 55%|█████▌ | 110/200 [00:09<00:04, 21.40 it/sec]
INFO - 10:03:44: ... 56%|█████▌ | 112/200 [00:09<00:04, 21.03 it/sec]
INFO - 10:03:44: ... 57%|█████▋ | 114/200 [00:09<00:04, 20.67 it/sec]
INFO - 10:03:44: ... 58%|█████▊ | 116/200 [00:09<00:04, 20.33 it/sec]
INFO - 10:03:44: ... 59%|█████▉ | 118/200 [00:10<00:04, 19.99 it/sec]
INFO - 10:03:44: ... 60%|██████ | 120/200 [00:10<00:04, 19.67 it/sec]
INFO - 10:03:45: ... 61%|██████ | 122/200 [00:10<00:04, 19.35 it/sec]
INFO - 10:03:45: ... 62%|██████▏ | 124/200 [00:10<00:03, 19.05 it/sec]
INFO - 10:03:45: ... 63%|██████▎ | 126/200 [00:10<00:03, 18.75 it/sec]
INFO - 10:03:45: ... 64%|██████▍ | 128/200 [00:10<00:03, 18.46 it/sec]
INFO - 10:03:45: ... 65%|██████▌ | 130/200 [00:10<00:03, 18.19 it/sec]
INFO - 10:03:45: ... 66%|██████▌ | 132/200 [00:11<00:03, 17.93 it/sec]
INFO - 10:03:46: ... 67%|██████▋ | 134/200 [00:11<00:03, 17.66 it/sec]
INFO - 10:03:46: ... 68%|██████▊ | 136/200 [00:11<00:03, 17.41 it/sec]
INFO - 10:03:46: ... 69%|██████▉ | 138/200 [00:11<00:03, 17.17 it/sec]
INFO - 10:03:46: ... 70%|███████ | 140/200 [00:11<00:03, 16.93 it/sec]
INFO - 10:03:46: ... 71%|███████ | 142/200 [00:11<00:03, 16.70 it/sec]
INFO - 10:03:46: ... 72%|███████▏ | 144/200 [00:12<00:03, 16.47 it/sec]
INFO - 10:03:47: ... 73%|███████▎ | 146/200 [00:12<00:03, 16.25 it/sec]
INFO - 10:03:47: ... 74%|███████▍ | 148/200 [00:12<00:03, 16.04 it/sec]
INFO - 10:03:47: ... 75%|███████▌ | 150/200 [00:12<00:03, 15.83 it/sec]
INFO - 10:03:47: ... 76%|███████▌ | 152/200 [00:12<00:03, 15.62 it/sec]
INFO - 10:03:47: ... 77%|███████▋ | 154/200 [00:12<00:02, 15.43 it/sec]
INFO - 10:03:47: ... 78%|███████▊ | 156/200 [00:13<00:02, 15.23 it/sec]
INFO - 10:03:48: ... 79%|███████▉ | 158/200 [00:13<00:02, 15.04 it/sec]
INFO - 10:03:48: ... 80%|████████ | 160/200 [00:13<00:02, 14.86 it/sec]
INFO - 10:03:48: ... 81%|████████ | 162/200 [00:13<00:02, 14.68 it/sec]
INFO - 10:03:48: ... 82%|████████▏ | 164/200 [00:13<00:02, 14.50 it/sec]
INFO - 10:03:48: ... 83%|████████▎ | 166/200 [00:13<00:02, 14.33 it/sec]
INFO - 10:03:48: ... 84%|████████▍ | 168/200 [00:14<00:02, 14.16 it/sec]
INFO - 10:03:49: ... 85%|████████▌ | 170/200 [00:14<00:02, 14.00 it/sec]
INFO - 10:03:49: ... 86%|████████▌ | 172/200 [00:14<00:02, 13.85 it/sec]
INFO - 10:03:49: ... 87%|████████▋ | 174/200 [00:14<00:01, 13.69 it/sec]
INFO - 10:03:49: ... 88%|████████▊ | 176/200 [00:14<00:01, 13.54 it/sec]
INFO - 10:03:49: ... 89%|████████▉ | 178/200 [00:14<00:01, 13.39 it/sec]
INFO - 10:03:49: ... 90%|█████████ | 180/200 [00:15<00:01, 13.24 it/sec]
INFO - 10:03:50: ... 91%|█████████ | 182/200 [00:15<00:01, 13.10 it/sec]
INFO - 10:03:50: ... 92%|█████████▏| 184/200 [00:15<00:01, 12.96 it/sec]
INFO - 10:03:50: ... 93%|█████████▎| 186/200 [00:15<00:01, 12.83 it/sec]
INFO - 10:03:50: ... 93%|█████████▎| 186/200 [00:15<00:01, 12.82 it/sec]
INFO - 10:03:50: Optimization result:
INFO - 10:03:50: Optimizer info:
INFO - 10:03:50: Status: None
INFO - 10:03:50: Message: Successive iterates of the objective function are closer than ftol_rel or ftol_abs. GEMSEO Stopped the driver
INFO - 10:03:50: Number of calls to the objective function by the optimizer: 186
INFO - 10:03:50: Solution:
INFO - 10:03:50: The solution is feasible.
INFO - 10:03:50: Objective: 136.56131771172448
INFO - 10:03:50: Standardized constraints:
INFO - 10:03:50: volume fraction - 0.3 = -1.0460364907594055e-08
INFO - 10:03:50: *** End MDOScenario execution (time: 0:00:15.619092) ***
{'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",
)
Out:
<gemseo.post.basic_history.BasicHistory object at 0x7fdbfb54deb0>
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.226 seconds)