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


Out:

INFO - 10:03:24: Generating HTML XDSM file in : xdsm.html


Execute the scenario

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


Out:

    INFO - 10:03:24:
INFO - 10:03:24: *** Start MDOScenario execution ***
INFO - 10:03:24: MDOScenario
INFO - 10:03:24:    Disciplines: DensityFilter MaterialModelInterpolation FininiteElementAnalysis VolumeFraction
INFO - 10:03:24:    MDO formulation: DisciplinaryOpt
INFO - 10:03:24: Optimization problem:
INFO - 10:03:24:    minimize compliance(x)
INFO - 10:03:24:    with respect to x
INFO - 10:03:24:    subject to constraints:
INFO - 10:03:24:       volume fraction(x) <= 0.3
INFO - 10:03:24: Solving optimization problem with algorithm NLOPT_MMA:
INFO - 10:03:24: ...   0%|          | 0/200 [00:00<?, ?it]
INFO - 10:03:24: ...   1%|          | 2/200 [00:00<00:00, 1010.68 it/sec]
INFO - 10:03:25: ...   2%|▏         | 4/200 [00:00<00:00, 512.81 it/sec]
INFO - 10:03:25: ...   3%|▎         | 6/200 [00:00<00:00, 347.83 it/sec]
INFO - 10:03:25: ...   4%|▍         | 8/200 [00:00<00:00, 263.65 it/sec]
INFO - 10:03:25: ...   5%|▌         | 10/200 [00:00<00:00, 212.90 it/sec]
INFO - 10:03:25: ...   6%|▌         | 12/200 [00:01<00:01, 178.35 it/sec]
INFO - 10:03:26: ...   7%|▋         | 14/200 [00:01<00:01, 153.55 it/sec]
INFO - 10:03:26: ...   8%|▊         | 16/200 [00:01<00:01, 135.15 it/sec]
INFO - 10:03:26: ...   9%|▉         | 18/200 [00:01<00:01, 120.69 it/sec]
INFO - 10:03:26: ...  10%|█         | 20/200 [00:01<00:01, 109.11 it/sec]
INFO - 10:03:26: ...  11%|█         | 22/200 [00:02<00:01, 99.33 it/sec]
INFO - 10:03:26: ...  12%|█▏        | 24/200 [00:02<00:01, 91.24 it/sec]
INFO - 10:03:27: ...  13%|█▎        | 26/200 [00:02<00:02, 84.40 it/sec]
INFO - 10:03:27: ...  14%|█▍        | 28/200 [00:02<00:02, 78.44 it/sec]
INFO - 10:03:27: ...  15%|█▌        | 30/200 [00:02<00:02, 73.46 it/sec]
INFO - 10:03:27: ...  16%|█▌        | 32/200 [00:02<00:02, 69.09 it/sec]
INFO - 10:03:27: ...  17%|█▋        | 34/200 [00:03<00:02, 65.21 it/sec]
INFO - 10:03:28: ...  18%|█▊        | 36/200 [00:03<00:02, 61.78 it/sec]
INFO - 10:03:28: ...  19%|█▉        | 38/200 [00:03<00:02, 58.63 it/sec]
INFO - 10:03:28: ...  20%|██        | 40/200 [00:03<00:02, 55.83 it/sec]
INFO - 10:03:28: ...  21%|██        | 42/200 [00:03<00:02, 53.23 it/sec]
INFO - 10:03:28: ...  22%|██▏       | 44/200 [00:03<00:03, 50.90 it/sec]
INFO - 10:03:28: ...  23%|██▎       | 46/200 [00:04<00:03, 48.77 it/sec]
INFO - 10:03:29: ...  24%|██▍       | 48/200 [00:04<00:03, 46.84 it/sec]
INFO - 10:03:29: ...  25%|██▌       | 50/200 [00:04<00:03, 44.53 it/sec]
INFO - 10:03:29: ...  26%|██▌       | 52/200 [00:04<00:03, 42.86 it/sec]
INFO - 10:03:29: ...  27%|██▋       | 54/200 [00:04<00:03, 41.34 it/sec]
INFO - 10:03:29: ...  28%|██▊       | 56/200 [00:05<00:03, 39.93 it/sec]
INFO - 10:03:29: ...  29%|██▉       | 58/200 [00:05<00:03, 38.62 it/sec]
INFO - 10:03:30: ...  30%|███       | 60/200 [00:05<00:03, 37.38 it/sec]
INFO - 10:03:30: ...  31%|███       | 62/200 [00:05<00:03, 36.26 it/sec]
INFO - 10:03:30: ...  32%|███▏      | 64/200 [00:05<00:03, 35.18 it/sec]
INFO - 10:03:30: ...  33%|███▎      | 66/200 [00:05<00:03, 34.15 it/sec]
INFO - 10:03:30: ...  34%|███▍      | 68/200 [00:06<00:03, 33.20 it/sec]
INFO - 10:03:30: ...  35%|███▌      | 70/200 [00:06<00:04, 32.29 it/sec]
INFO - 10:03:31: ...  36%|███▌      | 72/200 [00:06<00:04, 31.43 it/sec]
INFO - 10:03:31: ...  37%|███▋      | 74/200 [00:06<00:04, 30.62 it/sec]
INFO - 10:03:31: ...  38%|███▊      | 76/200 [00:06<00:04, 29.84 it/sec]
INFO - 10:03:31: ...  39%|███▉      | 78/200 [00:06<00:04, 29.10 it/sec]
INFO - 10:03:31: ...  40%|████      | 80/200 [00:07<00:04, 28.41 it/sec]
INFO - 10:03:31: ...  41%|████      | 82/200 [00:07<00:04, 27.75 it/sec]
INFO - 10:03:32: ...  42%|████▏     | 84/200 [00:07<00:04, 27.11 it/sec]
INFO - 10:03:32: ...  43%|████▎     | 86/200 [00:07<00:04, 26.50 it/sec]
INFO - 10:03:32: ...  44%|████▍     | 88/200 [00:07<00:04, 25.92 it/sec]
INFO - 10:03:32: ...  45%|████▌     | 90/200 [00:07<00:04, 25.36 it/sec]
INFO - 10:03:32: ...  46%|████▌     | 92/200 [00:08<00:04, 24.82 it/sec]
INFO - 10:03:33: ...  47%|████▋     | 94/200 [00:08<00:04, 24.30 it/sec]
INFO - 10:03:33: ...  48%|████▊     | 96/200 [00:08<00:04, 23.81 it/sec]
INFO - 10:03:33: ...  49%|████▉     | 98/200 [00:08<00:04, 23.34 it/sec]
INFO - 10:03:33: ...  50%|█████     | 100/200 [00:08<00:04, 22.89 it/sec]
INFO - 10:03:33: ...  51%|█████     | 102/200 [00:08<00:04, 22.46 it/sec]
INFO - 10:03:33: ...  52%|█████▏    | 104/200 [00:09<00:04, 22.04 it/sec]
INFO - 10:03:34: ...  53%|█████▎    | 106/200 [00:09<00:04, 21.64 it/sec]
INFO - 10:03:34: ...  53%|█████▎    | 106/200 [00:09<00:04, 21.63 it/sec]
INFO - 10:03:34: Optimization result:
INFO - 10:03:34:    Optimizer info:
INFO - 10:03:34:       Status: None
INFO - 10:03:34:       Message: Successive iterates of the objective function are closer than ftol_rel or ftol_abs. GEMSEO Stopped the driver
INFO - 10:03:34:       Number of calls to the objective function by the optimizer: 106
INFO - 10:03:34:    Solution:
INFO - 10:03:34:       The solution is feasible.
INFO - 10:03:34:       Objective: 170.90194735674362
INFO - 10:03:34:       Standardized constraints:
INFO - 10:03:34:          volume fraction - 0.3 = -1.5002556197907246e-06
INFO - 10:03:34: *** End MDOScenario execution (time: 0:00:09.263465) ***

{'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",
)


Out:

<gemseo.post.basic_history.BasicHistory object at 0x7fdbfb433b80>


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

Gallery generated by Sphinx-Gallery