# Solve a 2D MBB 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 = "MBB"


Define the number of elements in x- and y- directions:

n_x = 50
n_y = 25


Define the full material Young’s modulus and the 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:10: Generating HTML XDSM file in : xdsm.html


Execute the scenario

scenario.execute(input_data={"max_iter": 200, "algo": "NLOPT_MMA"})

    INFO - 14:44:10:
INFO - 14:44:10: *** Start MDOScenario execution ***
INFO - 14:44:10: MDOScenario
INFO - 14:44:10:    Disciplines: DensityFilter FininiteElementAnalysis MaterialModelInterpolation VolumeFraction
INFO - 14:44:10:    MDO formulation: DisciplinaryOpt
INFO - 14:44:10: Optimization problem:
INFO - 14:44:10:    minimize compliance(x)
INFO - 14:44:10:    with respect to x
INFO - 14:44:10:    subject to constraints:
INFO - 14:44:10:       volume fraction(x) <= 0.3
INFO - 14:44:10: Solving optimization problem with algorithm NLOPT_MMA:
INFO - 14:44:10: ...   0%|          | 0/200 [00:00<?, ?it]
INFO - 14:44:10: ...   1%|          | 2/200 [00:00<00:00, 964.05 it/sec, obj=1.79e+3]
INFO - 14:44:10: ...   2%|▏         | 3/200 [00:00<00:00, 637.86 it/sec, obj=1.79e+3]
INFO - 14:44:10: ...   2%|▎         | 5/200 [00:00<00:00, 397.14 it/sec, obj=1.79e+3]
INFO - 14:44:11: ...   4%|▎         | 7/200 [00:00<00:00, 288.48 it/sec, obj=1.79e+3]
INFO - 14:44:11: ...   4%|▍         | 9/200 [00:00<00:00, 226.95 it/sec, obj=1.79e+3]
INFO - 14:44:11: ...   6%|▌         | 11/200 [00:01<00:01, 187.91 it/sec, obj=1.79e+3]
INFO - 14:44:11: ...   6%|▋         | 13/200 [00:01<00:01, 160.32 it/sec, obj=1.79e+3]
INFO - 14:44:11: ...   8%|▊         | 15/200 [00:01<00:01, 139.47 it/sec, obj=1.79e+3]
INFO - 14:44:12: ...   8%|▊         | 17/200 [00:01<00:01, 123.92 it/sec, obj=1.79e+3]
INFO - 14:44:12: ...  10%|▉         | 19/200 [00:01<00:01, 111.74 it/sec, obj=1.79e+3]
INFO - 14:44:12: ...  10%|█         | 21/200 [00:01<00:01, 101.45 it/sec, obj=1.79e+3]
INFO - 14:44:12: ...  12%|█▏        | 23/200 [00:02<00:01, 92.84 it/sec, obj=1.79e+3]
INFO - 14:44:12: ...  12%|█▎        | 25/200 [00:02<00:02, 85.62 it/sec, obj=1.79e+3]
INFO - 14:44:12: ...  14%|█▎        | 27/200 [00:02<00:02, 79.42 it/sec, obj=1.79e+3]
INFO - 14:44:13: ...  14%|█▍        | 29/200 [00:02<00:02, 74.20 it/sec, obj=1.79e+3]
INFO - 14:44:13: ...  16%|█▌        | 31/200 [00:02<00:02, 69.55 it/sec, obj=1.79e+3]
INFO - 14:44:13: ...  16%|█▋        | 33/200 [00:03<00:02, 65.49 it/sec, obj=1.79e+3]
INFO - 14:44:13: ...  18%|█▊        | 35/200 [00:03<00:02, 61.92 it/sec, obj=1.79e+3]
INFO - 14:44:13: ...  18%|█▊        | 37/200 [00:03<00:02, 58.78 it/sec, obj=1.79e+3]
INFO - 14:44:14: ...  20%|█▉        | 39/200 [00:03<00:02, 55.93 it/sec, obj=1.79e+3]
INFO - 14:44:14: ...  20%|██        | 41/200 [00:03<00:02, 53.32 it/sec, obj=1.79e+3]
INFO - 14:44:14: ...  22%|██▏       | 43/200 [00:03<00:03, 50.92 it/sec, obj=1.79e+3]
INFO - 14:44:14: ...  22%|██▎       | 45/200 [00:04<00:03, 48.73 it/sec, obj=1.79e+3]
INFO - 14:44:14: ...  24%|██▎       | 47/200 [00:04<00:03, 46.73 it/sec, obj=1.79e+3]
INFO - 14:44:14: ...  24%|██▍       | 49/200 [00:04<00:03, 44.91 it/sec, obj=1.79e+3]
INFO - 14:44:15: ...  26%|██▌       | 51/200 [00:04<00:03, 43.25 it/sec, obj=1.79e+3]
INFO - 14:44:15: ...  26%|██▋       | 53/200 [00:04<00:03, 41.68 it/sec, obj=1.79e+3]
INFO - 14:44:15: ...  28%|██▊       | 55/200 [00:04<00:03, 40.22 it/sec, obj=1.79e+3]
INFO - 14:44:15: ...  28%|██▊       | 57/200 [00:05<00:03, 38.86 it/sec, obj=1.79e+3]
INFO - 14:44:15: ...  30%|██▉       | 59/200 [00:05<00:03, 37.58 it/sec, obj=1.79e+3]
INFO - 14:44:15: ...  30%|███       | 61/200 [00:05<00:03, 36.41 it/sec, obj=1.79e+3]
INFO - 14:44:16: ...  32%|███▏      | 63/200 [00:05<00:03, 35.31 it/sec, obj=1.79e+3]
INFO - 14:44:16: ...  32%|███▎      | 65/200 [00:05<00:03, 34.25 it/sec, obj=1.79e+3]
INFO - 14:44:16: ...  34%|███▎      | 67/200 [00:06<00:04, 33.23 it/sec, obj=1.79e+3]
INFO - 14:44:16: ...  34%|███▍      | 69/200 [00:06<00:04, 32.31 it/sec, obj=1.79e+3]
INFO - 14:44:16: ...  36%|███▌      | 71/200 [00:06<00:04, 31.44 it/sec, obj=1.79e+3]
INFO - 14:44:16: ...  36%|███▋      | 73/200 [00:06<00:04, 30.61 it/sec, obj=1.79e+3]
INFO - 14:44:17: ...  38%|███▊      | 75/200 [00:06<00:04, 29.83 it/sec, obj=1.79e+3]
INFO - 14:44:17: ...  38%|███▊      | 77/200 [00:06<00:04, 29.08 it/sec, obj=1.79e+3]
INFO - 14:44:17: ...  40%|███▉      | 79/200 [00:07<00:04, 28.36 it/sec, obj=1.79e+3]
INFO - 14:44:17: ...  40%|████      | 81/200 [00:07<00:04, 27.70 it/sec, obj=1.79e+3]
INFO - 14:44:17: ...  42%|████▏     | 83/200 [00:07<00:04, 27.05 it/sec, obj=1.79e+3]
INFO - 14:44:17: ...  42%|████▎     | 85/200 [00:07<00:04, 26.44 it/sec, obj=1.79e+3]
INFO - 14:44:18: ...  44%|████▎     | 87/200 [00:07<00:04, 25.85 it/sec, obj=1.79e+3]
INFO - 14:44:18: ...  44%|████▍     | 89/200 [00:07<00:04, 25.28 it/sec, obj=1.79e+3]
INFO - 14:44:18: ...  46%|████▌     | 91/200 [00:08<00:04, 24.74 it/sec, obj=1.79e+3]
INFO - 14:44:18: ...  46%|████▋     | 93/200 [00:08<00:04, 24.21 it/sec, obj=1.79e+3]
INFO - 14:44:18: ...  48%|████▊     | 95/200 [00:08<00:04, 23.72 it/sec, obj=1.79e+3]
INFO - 14:44:19: ...  48%|████▊     | 97/200 [00:08<00:04, 23.24 it/sec, obj=1.79e+3]
INFO - 14:44:19: ...  50%|████▉     | 99/200 [00:08<00:04, 22.78 it/sec, obj=1.79e+3]
INFO - 14:44:19: ...  50%|█████     | 101/200 [00:08<00:04, 22.34 it/sec, obj=1.79e+3]
INFO - 14:44:19: ...  52%|█████▏    | 103/200 [00:09<00:04, 21.91 it/sec, obj=1.79e+3]
INFO - 14:44:19: ...  52%|█████▎    | 105/200 [00:09<00:04, 21.51 it/sec, obj=1.79e+3]
INFO - 14:44:19: ...  54%|█████▎    | 107/200 [00:09<00:04, 21.11 it/sec, obj=1.79e+3]
INFO - 14:44:20: ...  55%|█████▍    | 109/200 [00:09<00:04, 20.72 it/sec, obj=1.79e+3]
INFO - 14:44:20: ...  56%|█████▌    | 111/200 [00:09<00:04, 20.33 it/sec, obj=1.79e+3]
INFO - 14:44:20: ...  56%|█████▋    | 113/200 [00:10<00:04, 19.98 it/sec, obj=1.79e+3]
INFO - 14:44:20: ...  57%|█████▊    | 115/200 [00:10<00:04, 19.63 it/sec, obj=1.79e+3]
INFO - 14:44:20: ...  58%|█████▊    | 117/200 [00:10<00:04, 19.31 it/sec, obj=1.79e+3]
INFO - 14:44:20: ...  60%|█████▉    | 119/200 [00:10<00:04, 19.00 it/sec, obj=1.79e+3]
INFO - 14:44:21: ...  60%|██████    | 121/200 [00:10<00:04, 18.69 it/sec, obj=1.79e+3]
INFO - 14:44:21: ...  62%|██████▏   | 123/200 [00:10<00:04, 18.39 it/sec, obj=1.79e+3]
INFO - 14:44:21: ...  62%|██████▎   | 125/200 [00:11<00:04, 18.10 it/sec, obj=1.79e+3]
INFO - 14:44:21: ...  64%|██████▎   | 127/200 [00:11<00:04, 17.82 it/sec, obj=1.79e+3]
INFO - 14:44:21: ...  64%|██████▍   | 129/200 [00:11<00:04, 17.55 it/sec, obj=1.79e+3]
INFO - 14:44:21: ...  66%|██████▌   | 131/200 [00:11<00:03, 17.30 it/sec, obj=1.79e+3]
INFO - 14:44:22: ...  66%|██████▋   | 133/200 [00:11<00:03, 17.05 it/sec, obj=1.79e+3]
INFO - 14:44:22: ...  68%|██████▊   | 135/200 [00:11<00:03, 16.80 it/sec, obj=1.79e+3]
INFO - 14:44:22: ...  68%|██████▊   | 137/200 [00:12<00:03, 16.56 it/sec, obj=1.79e+3]
INFO - 14:44:22: ...  70%|██████▉   | 139/200 [00:12<00:03, 16.33 it/sec, obj=1.79e+3]
INFO - 14:44:22: ...  70%|███████   | 141/200 [00:12<00:03, 16.10 it/sec, obj=1.79e+3]
INFO - 14:44:23: ...  72%|███████▏  | 143/200 [00:12<00:03, 15.88 it/sec, obj=1.79e+3]
INFO - 14:44:23: ...  72%|███████▎  | 145/200 [00:12<00:03, 15.67 it/sec, obj=1.79e+3]
INFO - 14:44:23: ...  74%|███████▎  | 147/200 [00:12<00:03, 15.46 it/sec, obj=1.79e+3]
INFO - 14:44:23: ...  74%|███████▍  | 149/200 [00:13<00:03, 15.26 it/sec, obj=1.79e+3]
INFO - 14:44:23: ...  76%|███████▌  | 151/200 [00:13<00:03, 15.06 it/sec, obj=1.79e+3]
INFO - 14:44:23: ...  76%|███████▋  | 153/200 [00:13<00:03, 14.87 it/sec, obj=1.79e+3]
INFO - 14:44:24: ...  78%|███████▊  | 155/200 [00:13<00:03, 14.69 it/sec, obj=1.79e+3]
INFO - 14:44:24: ...  78%|███████▊  | 157/200 [00:13<00:02, 14.51 it/sec, obj=1.79e+3]
INFO - 14:44:24: ...  80%|███████▉  | 159/200 [00:13<00:02, 14.33 it/sec, obj=1.79e+3]
INFO - 14:44:24: ...  80%|████████  | 161/200 [00:14<00:02, 14.16 it/sec, obj=1.79e+3]
INFO - 14:44:24: ...  82%|████████▏ | 163/200 [00:14<00:02, 13.99 it/sec, obj=1.79e+3]
INFO - 14:44:24: ...  82%|████████▎ | 165/200 [00:14<00:02, 13.83 it/sec, obj=1.79e+3]
INFO - 14:44:24: ...  82%|████████▎ | 165/200 [00:14<00:02, 13.82 it/sec, obj=1.79e+3]
INFO - 14:44:24: Optimization result:
INFO - 14:44:24:    Optimizer info:
INFO - 14:44:24:       Status: None
INFO - 14:44:24:       Message: Successive iterates of the objective function are closer than ftol_rel or ftol_abs. GEMSEO Stopped the driver
INFO - 14:44:24:       Number of calls to the objective function by the optimizer: 165
INFO - 14:44:24:    Solution:
INFO - 14:44:24:       The solution is feasible.
INFO - 14:44:24:       Objective: 169.90513692849225
INFO - 14:44:24:       Standardized constraints:
INFO - 14:44:24:          volume fraction - 0.3 = 2.1040097875069108e-07
INFO - 14:44:24: *** End MDOScenario execution (time: 0:00:14.491618) ***

{'max_iter': 200, 'algo': 'NLOPT_MMA'}


## Results¶

Post-process the optmization 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 0x7f3c13021d00>


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

Gallery generated by Sphinx-Gallery