Note
Click here to download the full example code
Solve a 2D L-shape 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 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()
Out:
INFO - 10:03:18: Generating HTML XDSM file in : xdsm.html
Execute the scenario
scenario.execute({"max_iter": 200, "algo": "NLOPT_MMA"})
Out:
INFO - 10:03:18:
INFO - 10:03:18: *** Start MDOScenario execution ***
INFO - 10:03:18: MDOScenario
INFO - 10:03:18: Disciplines: DensityFilter MaterialModelInterpolation FininiteElementAnalysis VolumeFraction
INFO - 10:03:18: MDO formulation: DisciplinaryOpt
INFO - 10:03:18: Optimization problem:
INFO - 10:03:18: minimize compliance(x)
INFO - 10:03:18: with respect to x
INFO - 10:03:18: subject to constraints:
INFO - 10:03:18: volume fraction(x) <= 0.3
INFO - 10:03:18: Solving optimization problem with algorithm NLOPT_MMA:
INFO - 10:03:18: ... 0%| | 0/200 [00:00<?, ?it]
INFO - 10:03:18: ... 1%| | 2/200 [00:00<00:00, 1966.22 it/sec]
INFO - 10:03:18: ... 2%|▎ | 5/200 [00:00<00:00, 845.12 it/sec]
INFO - 10:03:19: ... 4%|▍ | 8/200 [00:00<00:00, 538.32 it/sec]
INFO - 10:03:19: ... 6%|▌ | 11/200 [00:00<00:00, 395.56 it/sec]
INFO - 10:03:19: ... 7%|▋ | 14/200 [00:00<00:00, 313.23 it/sec]
INFO - 10:03:19: ... 8%|▊ | 17/200 [00:00<00:00, 259.45 it/sec]
INFO - 10:03:19: ... 10%|█ | 20/200 [00:00<00:00, 222.23 it/sec]
INFO - 10:03:19: ... 12%|█▏ | 23/200 [00:01<00:00, 193.79 it/sec]
INFO - 10:03:19: ... 13%|█▎ | 26/200 [00:01<00:01, 172.57 it/sec]
INFO - 10:03:19: ... 14%|█▍ | 29/200 [00:01<00:01, 155.86 it/sec]
INFO - 10:03:20: ... 16%|█▌ | 32/200 [00:01<00:01, 141.92 it/sec]
INFO - 10:03:20: ... 18%|█▊ | 35/200 [00:01<00:01, 130.07 it/sec]
INFO - 10:03:20: ... 19%|█▉ | 38/200 [00:01<00:01, 120.31 it/sec]
INFO - 10:03:20: ... 20%|██ | 41/200 [00:01<00:01, 111.88 it/sec]
INFO - 10:03:20: ... 22%|██▏ | 44/200 [00:01<00:01, 104.41 it/sec]
INFO - 10:03:20: ... 24%|██▎ | 47/200 [00:02<00:01, 98.10 it/sec]
INFO - 10:03:20: ... 25%|██▌ | 50/200 [00:02<00:01, 92.60 it/sec]
INFO - 10:03:20: ... 26%|██▋ | 53/200 [00:02<00:01, 87.39 it/sec]
INFO - 10:03:21: ... 28%|██▊ | 56/200 [00:02<00:01, 82.87 it/sec]
INFO - 10:03:21: ... 30%|██▉ | 59/200 [00:02<00:01, 78.85 it/sec]
INFO - 10:03:21: ... 31%|███ | 62/200 [00:02<00:01, 75.17 it/sec]
INFO - 10:03:21: ... 32%|███▎ | 65/200 [00:02<00:01, 71.82 it/sec]
INFO - 10:03:21: ... 34%|███▍ | 68/200 [00:02<00:01, 68.76 it/sec]
INFO - 10:03:21: ... 36%|███▌ | 71/200 [00:03<00:01, 65.91 it/sec]
INFO - 10:03:21: ... 37%|███▋ | 74/200 [00:03<00:01, 63.37 it/sec]
INFO - 10:03:21: ... 38%|███▊ | 77/200 [00:03<00:02, 61.05 it/sec]
INFO - 10:03:22: ... 40%|████ | 80/200 [00:03<00:02, 58.88 it/sec]
INFO - 10:03:22: ... 42%|████▏ | 83/200 [00:03<00:02, 56.81 it/sec]
INFO - 10:03:22: ... 43%|████▎ | 86/200 [00:03<00:02, 54.89 it/sec]
INFO - 10:03:22: ... 44%|████▍ | 89/200 [00:03<00:02, 53.08 it/sec]
INFO - 10:03:22: ... 46%|████▌ | 92/200 [00:03<00:02, 51.42 it/sec]
INFO - 10:03:22: ... 48%|████▊ | 95/200 [00:04<00:02, 49.90 it/sec]
INFO - 10:03:22: ... 49%|████▉ | 98/200 [00:04<00:02, 48.48 it/sec]
INFO - 10:03:22: ... 50%|█████ | 101/200 [00:04<00:02, 47.07 it/sec]
INFO - 10:03:23: ... 52%|█████▏ | 104/200 [00:04<00:02, 45.77 it/sec]
INFO - 10:03:23: ... 54%|█████▎ | 107/200 [00:04<00:02, 44.56 it/sec]
INFO - 10:03:23: ... 55%|█████▌ | 110/200 [00:04<00:02, 43.39 it/sec]
INFO - 10:03:23: ... 56%|█████▋ | 113/200 [00:04<00:02, 42.30 it/sec]
INFO - 10:03:23: ... 58%|█████▊ | 116/200 [00:04<00:02, 41.25 it/sec]
INFO - 10:03:23: ... 60%|█████▉ | 119/200 [00:04<00:02, 40.26 it/sec]
INFO - 10:03:23: ... 61%|██████ | 122/200 [00:05<00:01, 39.31 it/sec]
INFO - 10:03:23: ... 62%|██████▎ | 125/200 [00:05<00:01, 38.42 it/sec]
INFO - 10:03:23: ... 64%|██████▍ | 128/200 [00:05<00:01, 37.57 it/sec]
INFO - 10:03:24: ... 64%|██████▍ | 129/200 [00:05<00:01, 37.26 it/sec]
INFO - 10:03:24: Optimization result:
INFO - 10:03:24: Optimizer info:
INFO - 10:03:24: Status: None
INFO - 10:03:24: Message: Successive iterates of the objective function are closer than ftol_rel or ftol_abs. GEMSEO Stopped the driver
INFO - 10:03:24: Number of calls to the objective function by the optimizer: 129
INFO - 10:03:24: Solution:
INFO - 10:03:24: The solution is feasible.
INFO - 10:03:24: Objective: 151.62873248988768
INFO - 10:03:24: Standardized constraints:
INFO - 10:03:24: volume fraction - 0.3 = 1.0969287427831098e-06
INFO - 10:03:24: *** End MDOScenario execution (time: 0:00:05.381230) ***
{'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 0x7fdbfb2b50d0>

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