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

Out:

INFO - 15:01:49: Generating HTML XDSM file in : xdsm.html

Execute the scenario:

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

Out:

    INFO - 15:01:49:
    INFO - 15:01:49: *** Start MDOScenario execution ***
    INFO - 15:01:49: MDOScenario
    INFO - 15:01:49:    Disciplines: DensityFilter MaterialModelInterpolation FininiteElementAnalysis VolumeFraction
    INFO - 15:01:49:    MDO formulation: DisciplinaryOpt
    INFO - 15:01:49: Optimization problem:
    INFO - 15:01:49:    minimize compliance(x)
    INFO - 15:01:49:    with respect to x
    INFO - 15:01:49:    subject to constraints:
    INFO - 15:01:49:       volume fraction(x) <= 0.3
    INFO - 15:01:49: Solving optimization problem with algorithm NLOPT_MMA:
    INFO - 15:01:49: ...   0%|          | 0/200 [00:00<?, ?it]
    INFO - 15:01:49: ...   1%|          | 2/200 [00:00<00:00, 996.82 it/sec]
    INFO - 15:01:49: ...   2%|▏         | 3/200 [00:00<00:00, 661.68 it/sec]
    INFO - 15:01:50: ...   2%|▎         | 5/200 [00:00<00:00, 413.55 it/sec]
    INFO - 15:01:50: ...   4%|▎         | 7/200 [00:00<00:00, 300.61 it/sec]
    INFO - 15:01:50: ...   4%|▍         | 9/200 [00:00<00:00, 236.54 it/sec]
    INFO - 15:01:50: ...   6%|▌         | 11/200 [00:01<00:00, 194.62 it/sec]
    INFO - 15:01:50: ...   6%|▋         | 13/200 [00:01<00:01, 165.18 it/sec]
    INFO - 15:01:51: ...   8%|▊         | 15/200 [00:01<00:01, 144.02 it/sec]
    INFO - 15:01:51: ...   8%|▊         | 17/200 [00:01<00:01, 127.73 it/sec]
    INFO - 15:01:51: ...  10%|▉         | 19/200 [00:01<00:01, 114.73 it/sec]
    INFO - 15:01:51: ...  10%|█         | 21/200 [00:01<00:01, 104.21 it/sec]
    INFO - 15:01:51: ...  12%|█▏        | 23/200 [00:02<00:01, 95.45 it/sec]
    INFO - 15:01:51: ...  12%|█▎        | 25/200 [00:02<00:01, 88.17 it/sec]
    INFO - 15:01:52: ...  14%|█▎        | 27/200 [00:02<00:02, 81.78 it/sec]
    INFO - 15:01:52: ...  14%|█▍        | 29/200 [00:02<00:02, 76.47 it/sec]
    INFO - 15:01:52: ...  16%|█▌        | 31/200 [00:02<00:02, 71.91 it/sec]
    INFO - 15:01:52: ...  16%|█▋        | 33/200 [00:02<00:02, 67.82 it/sec]
    INFO - 15:01:52: ...  18%|█▊        | 35/200 [00:03<00:02, 64.06 it/sec]
    INFO - 15:01:52: ...  18%|█▊        | 37/200 [00:03<00:02, 60.79 it/sec]
    INFO - 15:01:53: ...  20%|█▉        | 39/200 [00:03<00:02, 57.83 it/sec]
    INFO - 15:01:53: ...  20%|██        | 41/200 [00:03<00:02, 55.15 it/sec]
    INFO - 15:01:53: ...  22%|██▏       | 43/200 [00:03<00:02, 52.72 it/sec]
    INFO - 15:01:53: ...  22%|██▎       | 45/200 [00:03<00:03, 50.46 it/sec]
    INFO - 15:01:53: ...  24%|██▎       | 47/200 [00:04<00:03, 48.38 it/sec]
    INFO - 15:01:53: ...  24%|██▍       | 49/200 [00:04<00:03, 46.49 it/sec]
    INFO - 15:01:54: ...  26%|██▌       | 51/200 [00:04<00:03, 44.70 it/sec]
    INFO - 15:01:54: ...  26%|██▋       | 53/200 [00:04<00:03, 43.06 it/sec]
    INFO - 15:01:54: ...  28%|██▊       | 55/200 [00:04<00:03, 41.54 it/sec]
    INFO - 15:01:54: ...  28%|██▊       | 57/200 [00:04<00:03, 40.14 it/sec]
    INFO - 15:01:54: ...  30%|██▉       | 59/200 [00:05<00:03, 38.79 it/sec]
    INFO - 15:01:54: ...  30%|███       | 61/200 [00:05<00:03, 37.56 it/sec]
    INFO - 15:01:55: ...  32%|███▏      | 63/200 [00:05<00:03, 36.40 it/sec]
    INFO - 15:01:55: ...  32%|███▎      | 65/200 [00:05<00:03, 35.31 it/sec]
    INFO - 15:01:55: ...  34%|███▎      | 67/200 [00:05<00:03, 34.29 it/sec]
    INFO - 15:01:55: ...  34%|███▍      | 69/200 [00:06<00:03, 33.32 it/sec]
    INFO - 15:01:55: ...  36%|███▌      | 71/200 [00:06<00:03, 32.40 it/sec]
    INFO - 15:01:56: ...  36%|███▋      | 73/200 [00:06<00:04, 31.55 it/sec]
    INFO - 15:01:56: ...  38%|███▊      | 75/200 [00:06<00:04, 30.73 it/sec]
    INFO - 15:01:56: ...  38%|███▊      | 77/200 [00:06<00:04, 29.95 it/sec]
    INFO - 15:01:56: ...  40%|███▉      | 79/200 [00:06<00:04, 29.22 it/sec]
    INFO - 15:01:56: ...  40%|████      | 81/200 [00:07<00:04, 28.51 it/sec]
    INFO - 15:01:56: ...  42%|████▏     | 83/200 [00:07<00:04, 27.85 it/sec]
    INFO - 15:01:57: ...  42%|████▎     | 85/200 [00:07<00:04, 27.20 it/sec]
    INFO - 15:01:57: ...  44%|████▎     | 87/200 [00:07<00:04, 26.59 it/sec]
    INFO - 15:01:57: ...  44%|████▍     | 89/200 [00:07<00:04, 26.01 it/sec]
    INFO - 15:01:57: ...  46%|████▌     | 91/200 [00:07<00:04, 25.46 it/sec]
    INFO - 15:01:57: ...  46%|████▋     | 93/200 [00:08<00:04, 24.93 it/sec]
    INFO - 15:01:57: ...  48%|████▊     | 95/200 [00:08<00:04, 24.42 it/sec]
    INFO - 15:01:58: ...  48%|████▊     | 97/200 [00:08<00:04, 23.92 it/sec]
    INFO - 15:01:58: ...  50%|████▉     | 99/200 [00:08<00:04, 23.46 it/sec]
    INFO - 15:01:58: ...  50%|█████     | 101/200 [00:08<00:04, 23.00 it/sec]
    INFO - 15:01:58: ...  52%|█████▏    | 103/200 [00:08<00:04, 22.56 it/sec]
    INFO - 15:01:58: ...  52%|█████▎    | 105/200 [00:09<00:04, 22.14 it/sec]
    INFO - 15:01:58: ...  54%|█████▎    | 107/200 [00:09<00:04, 21.73 it/sec]
    INFO - 15:01:59: ...  55%|█████▍    | 109/200 [00:09<00:04, 21.34 it/sec]
    INFO - 15:01:59: ...  56%|█████▌    | 111/200 [00:09<00:04, 20.96 it/sec]
    INFO - 15:01:59: ...  56%|█████▋    | 113/200 [00:09<00:04, 20.62 it/sec]
    INFO - 15:01:59: ...  57%|█████▊    | 115/200 [00:09<00:04, 20.27 it/sec]
    INFO - 15:01:59: ...  58%|█████▊    | 117/200 [00:10<00:04, 19.93 it/sec]
    INFO - 15:01:59: ...  60%|█████▉    | 119/200 [00:10<00:04, 19.60 it/sec]
    INFO - 15:02:00: ...  60%|██████    | 121/200 [00:10<00:04, 19.28 it/sec]
    INFO - 15:02:00: ...  62%|██████▏   | 123/200 [00:10<00:04, 18.97 it/sec]
    INFO - 15:02:00: ...  62%|██████▎   | 125/200 [00:10<00:04, 18.68 it/sec]
    INFO - 15:02:00: ...  64%|██████▎   | 127/200 [00:10<00:03, 18.39 it/sec]
    INFO - 15:02:00: ...  64%|██████▍   | 129/200 [00:11<00:03, 18.12 it/sec]
    INFO - 15:02:00: ...  66%|██████▌   | 131/200 [00:11<00:03, 17.86 it/sec]
    INFO - 15:02:01: ...  66%|██████▋   | 133/200 [00:11<00:03, 17.60 it/sec]
    INFO - 15:02:01: ...  68%|██████▊   | 135/200 [00:11<00:03, 17.35 it/sec]
    INFO - 15:02:01: ...  68%|██████▊   | 137/200 [00:11<00:03, 17.10 it/sec]
    INFO - 15:02:01: ...  70%|██████▉   | 139/200 [00:11<00:03, 16.87 it/sec]
    INFO - 15:02:01: ...  70%|███████   | 141/200 [00:12<00:03, 16.63 it/sec]
    INFO - 15:02:01: ...  72%|███████▏  | 143/200 [00:12<00:03, 16.41 it/sec]
    INFO - 15:02:02: ...  72%|███████▎  | 145/200 [00:12<00:03, 16.19 it/sec]
    INFO - 15:02:02: ...  74%|███████▎  | 147/200 [00:12<00:03, 15.97 it/sec]
    INFO - 15:02:02: ...  74%|███████▍  | 149/200 [00:12<00:03, 15.76 it/sec]
    INFO - 15:02:02: ...  76%|███████▌  | 151/200 [00:12<00:03, 15.56 it/sec]
    INFO - 15:02:02: ...  76%|███████▋  | 153/200 [00:13<00:03, 15.36 it/sec]
    INFO - 15:02:02: ...  78%|███████▊  | 155/200 [00:13<00:02, 15.16 it/sec]
    INFO - 15:02:03: ...  78%|███████▊  | 157/200 [00:13<00:02, 14.98 it/sec]
    INFO - 15:02:03: ...  80%|███████▉  | 159/200 [00:13<00:02, 14.79 it/sec]
    INFO - 15:02:03: ...  80%|████████  | 161/200 [00:13<00:02, 14.61 it/sec]
    INFO - 15:02:03: ...  82%|████████▏ | 163/200 [00:13<00:02, 14.43 it/sec]
    INFO - 15:02:03: ...  82%|████████▎ | 165/200 [00:14<00:02, 14.25 it/sec]
    INFO - 15:02:03: ...  84%|████████▎ | 167/200 [00:14<00:02, 14.09 it/sec]
    INFO - 15:02:04: ...  84%|████████▍ | 169/200 [00:14<00:02, 13.93 it/sec]
    INFO - 15:02:04: ...  86%|████████▌ | 171/200 [00:14<00:02, 13.77 it/sec]
    INFO - 15:02:04: ...  86%|████████▋ | 173/200 [00:14<00:01, 13.62 it/sec]
    INFO - 15:02:04: ...  88%|████████▊ | 175/200 [00:14<00:01, 13.47 it/sec]
    INFO - 15:02:04: ...  88%|████████▊ | 177/200 [00:15<00:01, 13.31 it/sec]
    INFO - 15:02:04: ...  90%|████████▉ | 179/200 [00:15<00:01, 13.16 it/sec]
    INFO - 15:02:05: ...  90%|█████████ | 181/200 [00:15<00:01, 13.02 it/sec]
    INFO - 15:02:05: ...  92%|█████████▏| 183/200 [00:15<00:01, 12.88 it/sec]
    INFO - 15:02:05: ...  92%|█████████▎| 185/200 [00:15<00:01, 12.75 it/sec]
    INFO - 15:02:05: ...  93%|█████████▎| 186/200 [00:15<00:01, 12.68 it/sec]
    INFO - 15:02:05: Optimization result:
    INFO - 15:02:05:    Optimizer info:
    INFO - 15:02:05:       Status: None
    INFO - 15:02:05:       Message: Successive iterates of the objective function are closer than ftol_rel or ftol_abs. GEMSEO Stopped the driver
    INFO - 15:02:05:       Number of calls to the objective function by the optimizer: 186
    INFO - 15:02:05:    Solution:
    INFO - 15:02:05:       The solution is feasible.
    INFO - 15:02:05:       Objective: 136.56131771172448
    INFO - 15:02:05:       Standardized constraints:
    INFO - 15:02:05:          volume fraction - 0.3 = -1.0460364907594055e-08
    INFO - 15:02:05: *** End MDOScenario execution (time: 0:00:15.787837) ***

{'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 0x7f7f7851e970>
../../_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 16.393 seconds)

Gallery generated by Sphinx-Gallery