Solve a 2D short cantilever 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 = "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()
INFO - 10:39:57: Generating HTML XDSM file in : xdsm.html

Execute the scenario:

scenario.execute(input_data={"max_iter": 200, "algo": "NLOPT_MMA"})
    INFO - 10:39:57:
    INFO - 10:39:57: *** Start MDOScenario execution ***
    INFO - 10:39:57: MDOScenario
    INFO - 10:39:57:    Disciplines: DensityFilter FininiteElementAnalysis MaterialModelInterpolation VolumeFraction
    INFO - 10:39:57:    MDO formulation: DisciplinaryOpt
    INFO - 10:39:57: Optimization problem:
    INFO - 10:39:57:    minimize compliance(x)
    INFO - 10:39:57:    with respect to x
    INFO - 10:39:57:    subject to constraints:
    INFO - 10:39:57:       volume fraction(x) <= 0.3
    INFO - 10:39:57: Solving optimization problem with algorithm NLOPT_MMA:
    INFO - 10:39:57: ...   0%|          | 0/200 [00:00<?, ?it]
    INFO - 10:39:57: ...   1%|          | 2/200 [00:00<00:00, 995.07 it/sec, obj=1.46e+3]
    INFO - 10:39:57: ...   2%|▏         | 3/200 [00:00<00:00, 650.13 it/sec, obj=1.46e+3]
    INFO - 10:39:57: ...   2%|▏         | 4/200 [00:00<00:00, 482.77 it/sec, obj=1.46e+3]
    INFO - 10:39:57: ...   3%|▎         | 6/200 [00:00<00:00, 328.66 it/sec, obj=1.46e+3]
    INFO - 10:39:58: ...   4%|▎         | 7/200 [00:00<00:00, 281.33 it/sec, obj=1.46e+3]
    INFO - 10:39:58: ...   4%|▍         | 8/200 [00:00<00:00, 243.56 it/sec, obj=1.46e+3]
    INFO - 10:39:58: ...   5%|▌         | 10/200 [00:01<00:00, 194.45 it/sec, obj=1.46e+3]
    INFO - 10:39:58: ...   6%|▌         | 12/200 [00:01<00:01, 163.55 it/sec, obj=1.46e+3]
    INFO - 10:39:58: ...   6%|▋         | 13/200 [00:01<00:01, 150.93 it/sec, obj=1.46e+3]
    INFO - 10:39:58: ...   7%|▋         | 14/200 [00:01<00:01, 139.13 it/sec, obj=1.46e+3]
    INFO - 10:39:58: ...   8%|▊         | 16/200 [00:01<00:01, 121.98 it/sec, obj=1.46e+3]
    INFO - 10:39:59: ...   9%|▉         | 18/200 [00:01<00:01, 109.23 it/sec, obj=1.46e+3]
    INFO - 10:39:59: ...  10%|▉         | 19/200 [00:01<00:01, 103.47 it/sec, obj=1.46e+3]
    INFO - 10:39:59: ...  10%|█         | 20/200 [00:02<00:01, 98.05 it/sec, obj=1.46e+3]
    INFO - 10:39:59: ...  11%|█         | 22/200 [00:02<00:01, 89.40 it/sec, obj=1.46e+3]
    INFO - 10:39:59: ...  12%|█▏        | 24/200 [00:02<00:02, 82.51 it/sec, obj=1.46e+3]
    INFO - 10:39:59: ...  13%|█▎        | 26/200 [00:02<00:02, 76.00 it/sec, obj=1.46e+3]
    INFO - 10:40:00: ...  14%|█▍        | 28/200 [00:02<00:02, 70.60 it/sec, obj=1.46e+3]
    INFO - 10:40:00: ...  15%|█▌        | 30/200 [00:03<00:02, 66.32 it/sec, obj=1.46e+3]
    INFO - 10:40:00: ...  16%|█▌        | 32/200 [00:03<00:02, 62.16 it/sec, obj=1.46e+3]
    INFO - 10:40:00: ...  17%|█▋        | 34/200 [00:03<00:02, 58.67 it/sec, obj=1.46e+3]
    INFO - 10:40:00: ...  18%|█▊        | 36/200 [00:03<00:02, 55.66 it/sec, obj=1.46e+3]
    INFO - 10:40:01: ...  19%|█▉        | 38/200 [00:03<00:03, 52.66 it/sec, obj=1.46e+3]
    INFO - 10:40:01: ...  20%|██        | 40/200 [00:03<00:03, 50.12 it/sec, obj=1.46e+3]
    INFO - 10:40:01: ...  21%|██        | 42/200 [00:04<00:03, 47.95 it/sec, obj=1.46e+3]
    INFO - 10:40:01: ...  22%|██▏       | 44/200 [00:04<00:03, 45.73 it/sec, obj=1.46e+3]
    INFO - 10:40:01: ...  23%|██▎       | 46/200 [00:04<00:03, 43.80 it/sec, obj=1.46e+3]
    INFO - 10:40:02: ...  24%|██▍       | 48/200 [00:04<00:03, 42.10 it/sec, obj=1.46e+3]
    INFO - 10:40:02: ...  25%|██▌       | 50/200 [00:04<00:03, 40.38 it/sec, obj=1.46e+3]
    INFO - 10:40:02: ...  26%|██▌       | 52/200 [00:05<00:03, 38.85 it/sec, obj=1.46e+3]
    INFO - 10:40:02: ...  27%|██▋       | 54/200 [00:05<00:03, 37.50 it/sec, obj=1.46e+3]
    INFO - 10:40:02: ...  28%|██▊       | 56/200 [00:05<00:03, 36.13 it/sec, obj=1.46e+3]
    INFO - 10:40:03: ...  29%|██▉       | 58/200 [00:05<00:04, 34.91 it/sec, obj=1.46e+3]
    INFO - 10:40:03: ...  30%|███       | 60/200 [00:05<00:04, 33.82 it/sec, obj=1.46e+3]
    INFO - 10:40:03: ...  31%|███       | 62/200 [00:06<00:04, 32.71 it/sec, obj=1.46e+3]
    INFO - 10:40:03: ...  32%|███▏      | 64/200 [00:06<00:04, 31.71 it/sec, obj=1.46e+3]
    INFO - 10:40:03: ...  33%|███▎      | 66/200 [00:06<00:04, 30.81 it/sec, obj=1.46e+3]
    INFO - 10:40:04: ...  34%|███▍      | 68/200 [00:06<00:04, 29.89 it/sec, obj=1.46e+3]
    INFO - 10:40:04: ...  35%|███▌      | 70/200 [00:06<00:04, 29.05 it/sec, obj=1.46e+3]
    INFO - 10:40:04: ...  36%|███▌      | 72/200 [00:07<00:04, 28.30 it/sec, obj=1.46e+3]
    INFO - 10:40:04: ...  37%|███▋      | 74/200 [00:07<00:04, 27.52 it/sec, obj=1.46e+3]
    INFO - 10:40:04: ...  38%|███▊      | 76/200 [00:07<00:04, 26.81 it/sec, obj=1.46e+3]
    INFO - 10:40:04: ...  39%|███▉      | 78/200 [00:07<00:04, 26.17 it/sec, obj=1.46e+3]
    INFO - 10:40:05: ...  40%|████      | 80/200 [00:07<00:04, 25.48 it/sec, obj=1.46e+3]
    INFO - 10:40:05: ...  41%|████      | 82/200 [00:08<00:04, 24.87 it/sec, obj=1.46e+3]
    INFO - 10:40:05: ...  42%|████▏     | 84/200 [00:08<00:04, 24.32 it/sec, obj=1.46e+3]
    INFO - 10:40:05: ...  43%|████▎     | 86/200 [00:08<00:04, 23.75 it/sec, obj=1.46e+3]
    INFO - 10:40:05: ...  44%|████▍     | 88/200 [00:08<00:04, 23.22 it/sec, obj=1.46e+3]
    INFO - 10:40:06: ...  45%|████▌     | 90/200 [00:08<00:04, 22.75 it/sec, obj=1.46e+3]
    INFO - 10:40:06: ...  46%|████▌     | 92/200 [00:08<00:04, 22.24 it/sec, obj=1.46e+3]
    INFO - 10:40:06: ...  47%|████▋     | 94/200 [00:09<00:04, 21.78 it/sec, obj=1.46e+3]
    INFO - 10:40:06: ...  48%|████▊     | 96/200 [00:09<00:04, 21.36 it/sec, obj=1.46e+3]
    INFO - 10:40:06: ...  49%|████▉     | 98/200 [00:09<00:04, 20.92 it/sec, obj=1.46e+3]
    INFO - 10:40:07: ...  50%|█████     | 100/200 [00:09<00:04, 20.51 it/sec, obj=1.46e+3]
    INFO - 10:40:07: ...  51%|█████     | 102/200 [00:09<00:04, 20.14 it/sec, obj=1.46e+3]
    INFO - 10:40:07: ...  52%|█████▏    | 104/200 [00:10<00:04, 19.74 it/sec, obj=1.46e+3]
    INFO - 10:40:07: ...  53%|█████▎    | 106/200 [00:10<00:04, 19.37 it/sec, obj=1.46e+3]
    INFO - 10:40:07: ...  54%|█████▍    | 108/200 [00:10<00:04, 19.04 it/sec, obj=1.46e+3]
    INFO - 10:40:08: ...  55%|█████▌    | 110/200 [00:10<00:04, 18.68 it/sec, obj=1.46e+3]
    INFO - 10:40:08: ...  56%|█████▌    | 112/200 [00:10<00:04, 18.36 it/sec, obj=1.46e+3]
    INFO - 10:40:08: ...  57%|█████▋    | 114/200 [00:11<00:04, 18.06 it/sec, obj=1.46e+3]
    INFO - 10:40:08: ...  58%|█████▊    | 116/200 [00:11<00:04, 17.75 it/sec, obj=1.46e+3]
    INFO - 10:40:08: ...  59%|█████▉    | 118/200 [00:11<00:04, 17.45 it/sec, obj=1.46e+3]
    INFO - 10:40:08: ...  60%|██████    | 120/200 [00:11<00:04, 17.18 it/sec, obj=1.46e+3]
    INFO - 10:40:09: ...  61%|██████    | 122/200 [00:11<00:04, 16.89 it/sec, obj=1.46e+3]
    INFO - 10:40:09: ...  62%|██████▏   | 124/200 [00:12<00:04, 16.62 it/sec, obj=1.46e+3]
    INFO - 10:40:09: ...  63%|██████▎   | 126/200 [00:12<00:04, 16.37 it/sec, obj=1.46e+3]
    INFO - 10:40:09: ...  64%|██████▍   | 128/200 [00:12<00:04, 16.11 it/sec, obj=1.46e+3]
    INFO - 10:40:09: ...  65%|██████▌   | 130/200 [00:12<00:04, 15.87 it/sec, obj=1.46e+3]
    INFO - 10:40:10: ...  66%|██████▌   | 132/200 [00:12<00:04, 15.65 it/sec, obj=1.46e+3]
    INFO - 10:40:10: ...  67%|██████▋   | 134/200 [00:12<00:04, 15.41 it/sec, obj=1.46e+3]
    INFO - 10:40:10: ...  68%|██████▊   | 136/200 [00:13<00:04, 15.19 it/sec, obj=1.46e+3]
    INFO - 10:40:10: ...  69%|██████▉   | 138/200 [00:13<00:04, 14.97 it/sec, obj=1.46e+3]
    INFO - 10:40:10: ...  70%|███████   | 140/200 [00:13<00:04, 14.74 it/sec, obj=1.46e+3]
    INFO - 10:40:11: ...  71%|███████   | 142/200 [00:13<00:03, 14.53 it/sec, obj=1.46e+3]
    INFO - 10:40:11: ...  72%|███████▏  | 144/200 [00:13<00:03, 14.35 it/sec, obj=1.46e+3]
    INFO - 10:40:11: ...  73%|███████▎  | 146/200 [00:14<00:03, 14.14 it/sec, obj=1.46e+3]
    INFO - 10:40:11: ...  74%|███████▍  | 148/200 [00:14<00:03, 13.96 it/sec, obj=1.46e+3]
    INFO - 10:40:11: ...  75%|███████▌  | 150/200 [00:14<00:03, 13.79 it/sec, obj=1.46e+3]
    INFO - 10:40:12: ...  76%|███████▌  | 152/200 [00:14<00:03, 13.60 it/sec, obj=1.46e+3]
    INFO - 10:40:12: ...  77%|███████▋  | 154/200 [00:14<00:03, 13.43 it/sec, obj=1.46e+3]
    INFO - 10:40:12: ...  78%|███████▊  | 156/200 [00:15<00:03, 13.27 it/sec, obj=1.46e+3]
    INFO - 10:40:12: ...  79%|███████▉  | 158/200 [00:15<00:03, 13.09 it/sec, obj=1.46e+3]
    INFO - 10:40:12: ...  80%|████████  | 160/200 [00:15<00:03, 12.93 it/sec, obj=1.46e+3]
    INFO - 10:40:13: ...  81%|████████  | 162/200 [00:15<00:02, 12.78 it/sec, obj=1.46e+3]
    INFO - 10:40:13: ...  82%|████████▏ | 164/200 [00:15<00:02, 12.62 it/sec, obj=1.46e+3]
    INFO - 10:40:13: ...  83%|████████▎ | 166/200 [00:16<00:02, 12.47 it/sec, obj=1.46e+3]
    INFO - 10:40:13: ...  84%|████████▍ | 168/200 [00:16<00:02, 12.33 it/sec, obj=1.46e+3]
    INFO - 10:40:13: ...  85%|████████▌ | 170/200 [00:16<00:02, 12.19 it/sec, obj=1.46e+3]
    INFO - 10:40:13: ...  86%|████████▌ | 172/200 [00:16<00:02, 12.05 it/sec, obj=1.46e+3]
    INFO - 10:40:14: ...  87%|████████▋ | 174/200 [00:16<00:02, 11.92 it/sec, obj=1.46e+3]
    INFO - 10:40:14: ...  88%|████████▊ | 176/200 [00:16<00:02, 11.78 it/sec, obj=1.46e+3]
    INFO - 10:40:14: ...  89%|████████▉ | 178/200 [00:17<00:01, 11.65 it/sec, obj=1.46e+3]
    INFO - 10:40:14: ...  90%|█████████ | 180/200 [00:17<00:01, 11.53 it/sec, obj=1.46e+3]
    INFO - 10:40:14: ...  91%|█████████ | 182/200 [00:17<00:01, 11.40 it/sec, obj=1.46e+3]
    INFO - 10:40:15: ...  92%|█████████▏| 184/200 [00:17<00:01, 11.28 it/sec, obj=1.46e+3]
    INFO - 10:40:15: ...  93%|█████████▎| 186/200 [00:17<00:01, 11.16 it/sec, obj=1.46e+3]
    INFO - 10:40:15: ...  93%|█████████▎| 186/200 [00:17<00:01, 11.16 it/sec, obj=1.46e+3]
    INFO - 10:40:15: Optimization result:
    INFO - 10:40:15:    Optimizer info:
    INFO - 10:40:15:       Status: None
    INFO - 10:40:15:       Message: Successive iterates of the objective function are closer than ftol_rel or ftol_abs. GEMSEO Stopped the driver
    INFO - 10:40:15:       Number of calls to the objective function by the optimizer: 186
    INFO - 10:40:15:    Solution:
    INFO - 10:40:15:       The solution is feasible.
    INFO - 10:40:15:       Objective: 136.56131771172448
    INFO - 10:40:15:       Standardized constraints:
    INFO - 10:40:15:          volume fraction - 0.3 = -1.0460364907594055e-08
    INFO - 10:40:15: *** End MDOScenario execution (time: 0:00:17.940621) ***

{'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",
)
<gemseo.post.basic_history.BasicHistory object at 0x7f875d0b5a60>
../../_images/Short_Cantilever_history.png

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")
topology optimization short cantilever../../_images/Short_Cantilever_solution.png

Total running time of the script: ( 0 minutes 18.526 seconds)

Gallery generated by Sphinx-Gallery