Note
Click here to download the full example code
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 - 07:15:26: Generating HTML XDSM file in : xdsm.html
Execute the scenario
scenario.execute(input_data={"max_iter": 200, "algo": "NLOPT_MMA"})
Out:
INFO - 07:15:26:
INFO - 07:15:26: *** Start MDOScenario execution ***
INFO - 07:15:26: MDOScenario
INFO - 07:15:26: Disciplines: DensityFilter MaterialModelInterpolation FininiteElementAnalysis VolumeFraction
INFO - 07:15:26: MDO formulation: DisciplinaryOpt
INFO - 07:15:26: Optimization problem:
INFO - 07:15:26: minimize compliance(x)
INFO - 07:15:26: with respect to x
INFO - 07:15:26: subject to constraints:
INFO - 07:15:26: volume fraction(x) <= 0.3
INFO - 07:15:26: Solving optimization problem with algorithm NLOPT_MMA:
INFO - 07:15:26: ... 0%| | 0/200 [00:00<?, ?it]
INFO - 07:15:26: ... 1%| | 2/200 [00:00<00:00, 982.65 it/sec]
INFO - 07:15:26: ... 2%|▏ | 3/200 [00:00<00:00, 654.69 it/sec]
INFO - 07:15:27: ... 2%|▎ | 5/200 [00:00<00:00, 408.63 it/sec]
INFO - 07:15:27: ... 4%|▎ | 7/200 [00:00<00:00, 297.46 it/sec]
INFO - 07:15:27: ... 4%|▍ | 9/200 [00:00<00:00, 233.51 it/sec]
INFO - 07:15:27: ... 6%|▌ | 11/200 [00:01<00:00, 192.84 it/sec]
INFO - 07:15:27: ... 6%|▋ | 13/200 [00:01<00:01, 164.08 it/sec]
INFO - 07:15:28: ... 8%|▊ | 15/200 [00:01<00:01, 142.74 it/sec]
INFO - 07:15:28: ... 8%|▊ | 17/200 [00:01<00:01, 126.44 it/sec]
INFO - 07:15:28: ... 10%|▉ | 19/200 [00:01<00:01, 113.86 it/sec]
INFO - 07:15:28: ... 10%|█ | 21/200 [00:01<00:01, 103.24 it/sec]
INFO - 07:15:28: ... 12%|█▏ | 23/200 [00:02<00:01, 94.31 it/sec]
INFO - 07:15:28: ... 12%|█▎ | 25/200 [00:02<00:02, 86.84 it/sec]
INFO - 07:15:29: ... 14%|█▎ | 27/200 [00:02<00:02, 80.52 it/sec]
INFO - 07:15:29: ... 14%|█▍ | 29/200 [00:02<00:02, 75.20 it/sec]
INFO - 07:15:29: ... 16%|█▌ | 31/200 [00:02<00:02, 70.54 it/sec]
INFO - 07:15:29: ... 16%|█▋ | 33/200 [00:03<00:02, 66.44 it/sec]
INFO - 07:15:29: ... 18%|█▊ | 35/200 [00:03<00:02, 62.84 it/sec]
INFO - 07:15:30: ... 18%|█▊ | 37/200 [00:03<00:02, 59.59 it/sec]
INFO - 07:15:30: ... 20%|█▉ | 39/200 [00:03<00:02, 56.67 it/sec]
INFO - 07:15:30: ... 20%|██ | 41/200 [00:03<00:02, 53.98 it/sec]
INFO - 07:15:30: ... 22%|██▏ | 43/200 [00:03<00:03, 51.55 it/sec]
INFO - 07:15:30: ... 22%|██▎ | 45/200 [00:04<00:03, 49.33 it/sec]
INFO - 07:15:30: ... 24%|██▎ | 47/200 [00:04<00:03, 47.33 it/sec]
INFO - 07:15:31: ... 24%|██▍ | 49/200 [00:04<00:03, 45.48 it/sec]
INFO - 07:15:31: ... 26%|██▌ | 51/200 [00:04<00:03, 43.78 it/sec]
INFO - 07:15:31: ... 26%|██▋ | 53/200 [00:04<00:03, 42.18 it/sec]
INFO - 07:15:31: ... 28%|██▊ | 55/200 [00:04<00:03, 40.70 it/sec]
INFO - 07:15:31: ... 28%|██▊ | 57/200 [00:05<00:03, 39.33 it/sec]
INFO - 07:15:31: ... 30%|██▉ | 59/200 [00:05<00:03, 38.03 it/sec]
INFO - 07:15:32: ... 30%|███ | 61/200 [00:05<00:03, 36.85 it/sec]
INFO - 07:15:32: ... 32%|███▏ | 63/200 [00:05<00:03, 35.72 it/sec]
INFO - 07:15:32: ... 32%|███▎ | 65/200 [00:05<00:03, 34.65 it/sec]
INFO - 07:15:32: ... 34%|███▎ | 67/200 [00:05<00:03, 33.65 it/sec]
INFO - 07:15:32: ... 34%|███▍ | 69/200 [00:06<00:04, 32.69 it/sec]
INFO - 07:15:32: ... 36%|███▌ | 71/200 [00:06<00:04, 31.79 it/sec]
INFO - 07:15:33: ... 36%|███▋ | 73/200 [00:06<00:04, 30.94 it/sec]
INFO - 07:15:33: ... 38%|███▊ | 75/200 [00:06<00:04, 30.15 it/sec]
INFO - 07:15:33: ... 38%|███▊ | 77/200 [00:06<00:04, 29.37 it/sec]
INFO - 07:15:33: ... 40%|███▉ | 79/200 [00:06<00:04, 28.65 it/sec]
INFO - 07:15:33: ... 40%|████ | 81/200 [00:07<00:04, 27.97 it/sec]
INFO - 07:15:33: ... 42%|████▏ | 83/200 [00:07<00:04, 27.32 it/sec]
INFO - 07:15:34: ... 42%|████▎ | 85/200 [00:07<00:04, 26.70 it/sec]
INFO - 07:15:34: ... 44%|████▎ | 87/200 [00:07<00:04, 26.10 it/sec]
INFO - 07:15:34: ... 44%|████▍ | 89/200 [00:07<00:04, 25.52 it/sec]
INFO - 07:15:34: ... 46%|████▌ | 91/200 [00:08<00:04, 24.98 it/sec]
INFO - 07:15:34: ... 46%|████▋ | 93/200 [00:08<00:04, 24.45 it/sec]
INFO - 07:15:35: ... 48%|████▊ | 95/200 [00:08<00:04, 23.95 it/sec]
INFO - 07:15:35: ... 48%|████▊ | 97/200 [00:08<00:04, 23.47 it/sec]
INFO - 07:15:35: ... 50%|████▉ | 99/200 [00:08<00:04, 23.01 it/sec]
INFO - 07:15:35: ... 50%|█████ | 101/200 [00:08<00:04, 22.56 it/sec]
INFO - 07:15:35: ... 52%|█████▏ | 103/200 [00:09<00:04, 22.13 it/sec]
INFO - 07:15:35: ... 52%|█████▎ | 105/200 [00:09<00:04, 21.73 it/sec]
INFO - 07:15:35: ... 53%|█████▎ | 106/200 [00:09<00:04, 21.52 it/sec]
INFO - 07:15:35: Optimization result:
INFO - 07:15:35: Optimizer info:
INFO - 07:15:35: Status: None
INFO - 07:15:35: Message: Successive iterates of the objective function are closer than ftol_rel or ftol_abs. GEMSEO Stopped the driver
INFO - 07:15:35: Number of calls to the objective function by the optimizer: 106
INFO - 07:15:35: Solution:
INFO - 07:15:35: The solution is feasible.
INFO - 07:15:35: Objective: 170.90194735674362
INFO - 07:15:35: Standardized constraints:
INFO - 07:15:35: volume fraction - 0.3 = -1.5002556197907246e-06
INFO - 07:15:35: *** End MDOScenario execution (time: 0:00:09.311394) ***
{'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 0x7f2928774790>

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