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 - 07:15:36: Generating HTML XDSM file in : xdsm.html

Execute the scenario:

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

Out:

    INFO - 07:15:36:
    INFO - 07:15:36: *** Start MDOScenario execution ***
    INFO - 07:15:36: MDOScenario
    INFO - 07:15:36:    Disciplines: DensityFilter MaterialModelInterpolation FininiteElementAnalysis VolumeFraction
    INFO - 07:15:36:    MDO formulation: DisciplinaryOpt
    INFO - 07:15:36: Optimization problem:
    INFO - 07:15:36:    minimize compliance(x)
    INFO - 07:15:36:    with respect to x
    INFO - 07:15:36:    subject to constraints:
    INFO - 07:15:36:       volume fraction(x) <= 0.3
    INFO - 07:15:36: Solving optimization problem with algorithm NLOPT_MMA:
    INFO - 07:15:36: ...   0%|          | 0/200 [00:00<?, ?it]
    INFO - 07:15:36: ...   1%|          | 2/200 [00:00<00:00, 972.27 it/sec]
    INFO - 07:15:37: ...   2%|▏         | 3/200 [00:00<00:00, 647.99 it/sec]
    INFO - 07:15:37: ...   2%|▎         | 5/200 [00:00<00:00, 403.33 it/sec]
    INFO - 07:15:37: ...   4%|▎         | 7/200 [00:00<00:00, 293.32 it/sec]
    INFO - 07:15:37: ...   4%|▍         | 9/200 [00:00<00:00, 231.64 it/sec]
    INFO - 07:15:37: ...   6%|▌         | 11/200 [00:01<00:00, 191.12 it/sec]
    INFO - 07:15:37: ...   6%|▋         | 13/200 [00:01<00:01, 162.78 it/sec]
    INFO - 07:15:38: ...   8%|▊         | 15/200 [00:01<00:01, 142.06 it/sec]
    INFO - 07:15:38: ...   8%|▊         | 17/200 [00:01<00:01, 126.10 it/sec]
    INFO - 07:15:38: ...  10%|▉         | 19/200 [00:01<00:01, 113.37 it/sec]
    INFO - 07:15:38: ...  10%|█         | 21/200 [00:01<00:01, 103.06 it/sec]
    INFO - 07:15:38: ...  12%|█▏        | 23/200 [00:02<00:01, 94.67 it/sec]
    INFO - 07:15:39: ...  12%|█▎        | 25/200 [00:02<00:02, 87.43 it/sec]
    INFO - 07:15:39: ...  14%|█▎        | 27/200 [00:02<00:02, 81.19 it/sec]
    INFO - 07:15:39: ...  14%|█▍        | 29/200 [00:02<00:02, 75.97 it/sec]
    INFO - 07:15:39: ...  16%|█▌        | 31/200 [00:02<00:02, 71.42 it/sec]
    INFO - 07:15:39: ...  16%|█▋        | 33/200 [00:02<00:02, 67.40 it/sec]
    INFO - 07:15:39: ...  18%|█▊        | 35/200 [00:03<00:02, 63.77 it/sec]
    INFO - 07:15:40: ...  18%|█▊        | 37/200 [00:03<00:02, 60.57 it/sec]
    INFO - 07:15:40: ...  20%|█▉        | 39/200 [00:03<00:02, 57.62 it/sec]
    INFO - 07:15:40: ...  20%|██        | 41/200 [00:03<00:02, 54.94 it/sec]
    INFO - 07:15:40: ...  22%|██▏       | 43/200 [00:03<00:02, 52.53 it/sec]
    INFO - 07:15:40: ...  22%|██▎       | 45/200 [00:03<00:03, 50.32 it/sec]
    INFO - 07:15:40: ...  24%|██▎       | 47/200 [00:04<00:03, 48.26 it/sec]
    INFO - 07:15:41: ...  24%|██▍       | 49/200 [00:04<00:03, 46.35 it/sec]
    INFO - 07:15:41: ...  26%|██▌       | 51/200 [00:04<00:03, 44.58 it/sec]
    INFO - 07:15:41: ...  26%|██▋       | 53/200 [00:04<00:03, 42.97 it/sec]
    INFO - 07:15:41: ...  28%|██▊       | 55/200 [00:04<00:03, 41.47 it/sec]
    INFO - 07:15:41: ...  28%|██▊       | 57/200 [00:04<00:03, 40.08 it/sec]
    INFO - 07:15:41: ...  30%|██▉       | 59/200 [00:05<00:03, 38.78 it/sec]
    INFO - 07:15:42: ...  30%|███       | 61/200 [00:05<00:03, 37.56 it/sec]
    INFO - 07:15:42: ...  32%|███▏      | 63/200 [00:05<00:03, 36.42 it/sec]
    INFO - 07:15:42: ...  32%|███▎      | 65/200 [00:05<00:03, 35.34 it/sec]
    INFO - 07:15:42: ...  34%|███▎      | 67/200 [00:05<00:03, 34.32 it/sec]
    INFO - 07:15:42: ...  34%|███▍      | 69/200 [00:05<00:03, 33.38 it/sec]
    INFO - 07:15:42: ...  36%|███▌      | 71/200 [00:06<00:03, 32.47 it/sec]
    INFO - 07:15:43: ...  36%|███▋      | 73/200 [00:06<00:04, 31.61 it/sec]
    INFO - 07:15:43: ...  38%|███▊      | 75/200 [00:06<00:04, 30.79 it/sec]
    INFO - 07:15:43: ...  38%|███▊      | 77/200 [00:06<00:04, 30.02 it/sec]
    INFO - 07:15:43: ...  40%|███▉      | 79/200 [00:06<00:04, 29.28 it/sec]
    INFO - 07:15:43: ...  40%|████      | 81/200 [00:06<00:04, 28.58 it/sec]
    INFO - 07:15:43: ...  42%|████▏     | 83/200 [00:07<00:04, 27.91 it/sec]
    INFO - 07:15:44: ...  42%|████▎     | 85/200 [00:07<00:04, 27.28 it/sec]
    INFO - 07:15:44: ...  44%|████▎     | 87/200 [00:07<00:04, 26.66 it/sec]
    INFO - 07:15:44: ...  44%|████▍     | 89/200 [00:07<00:04, 26.09 it/sec]
    INFO - 07:15:44: ...  46%|████▌     | 91/200 [00:07<00:04, 25.53 it/sec]
    INFO - 07:15:44: ...  46%|████▋     | 93/200 [00:08<00:04, 25.00 it/sec]
    INFO - 07:15:44: ...  48%|████▊     | 95/200 [00:08<00:04, 24.48 it/sec]
    INFO - 07:15:45: ...  48%|████▊     | 97/200 [00:08<00:04, 23.99 it/sec]
    INFO - 07:15:45: ...  50%|████▉     | 99/200 [00:08<00:04, 23.52 it/sec]
    INFO - 07:15:45: ...  50%|█████     | 101/200 [00:08<00:04, 23.07 it/sec]
    INFO - 07:15:45: ...  52%|█████▏    | 103/200 [00:08<00:04, 22.63 it/sec]
    INFO - 07:15:45: ...  52%|█████▎    | 105/200 [00:09<00:04, 22.20 it/sec]
    INFO - 07:15:45: ...  54%|█████▎    | 107/200 [00:09<00:04, 21.79 it/sec]
    INFO - 07:15:46: ...  55%|█████▍    | 109/200 [00:09<00:04, 21.40 it/sec]
    INFO - 07:15:46: ...  56%|█████▌    | 111/200 [00:09<00:04, 21.03 it/sec]
    INFO - 07:15:46: ...  56%|█████▋    | 113/200 [00:09<00:04, 20.67 it/sec]
    INFO - 07:15:46: ...  57%|█████▊    | 115/200 [00:09<00:04, 20.33 it/sec]
    INFO - 07:15:46: ...  58%|█████▊    | 117/200 [00:10<00:04, 19.99 it/sec]
    INFO - 07:15:46: ...  60%|█████▉    | 119/200 [00:10<00:04, 19.66 it/sec]
    INFO - 07:15:47: ...  60%|██████    | 121/200 [00:10<00:04, 19.34 it/sec]
    INFO - 07:15:47: ...  62%|██████▏   | 123/200 [00:10<00:04, 19.03 it/sec]
    INFO - 07:15:47: ...  62%|██████▎   | 125/200 [00:10<00:04, 18.74 it/sec]
    INFO - 07:15:47: ...  64%|██████▎   | 127/200 [00:10<00:03, 18.45 it/sec]
    INFO - 07:15:47: ...  64%|██████▍   | 129/200 [00:11<00:03, 18.17 it/sec]
    INFO - 07:15:47: ...  66%|██████▌   | 131/200 [00:11<00:03, 17.90 it/sec]
    INFO - 07:15:48: ...  66%|██████▋   | 133/200 [00:11<00:03, 17.63 it/sec]
    INFO - 07:15:48: ...  68%|██████▊   | 135/200 [00:11<00:03, 17.38 it/sec]
    INFO - 07:15:48: ...  68%|██████▊   | 137/200 [00:11<00:03, 17.13 it/sec]
    INFO - 07:15:48: ...  70%|██████▉   | 139/200 [00:11<00:03, 16.89 it/sec]
    INFO - 07:15:48: ...  70%|███████   | 141/200 [00:12<00:03, 16.65 it/sec]
    INFO - 07:15:48: ...  72%|███████▏  | 143/200 [00:12<00:03, 16.43 it/sec]
    INFO - 07:15:49: ...  72%|███████▎  | 145/200 [00:12<00:03, 16.21 it/sec]
    INFO - 07:15:49: ...  74%|███████▎  | 147/200 [00:12<00:03, 15.99 it/sec]
    INFO - 07:15:49: ...  74%|███████▍  | 149/200 [00:12<00:03, 15.78 it/sec]
    INFO - 07:15:49: ...  76%|███████▌  | 151/200 [00:12<00:03, 15.58 it/sec]
    INFO - 07:15:49: ...  76%|███████▋  | 153/200 [00:13<00:03, 15.38 it/sec]
    INFO - 07:15:49: ...  78%|███████▊  | 155/200 [00:13<00:02, 15.19 it/sec]
    INFO - 07:15:50: ...  78%|███████▊  | 157/200 [00:13<00:02, 15.00 it/sec]
    INFO - 07:15:50: ...  80%|███████▉  | 159/200 [00:13<00:02, 14.81 it/sec]
    INFO - 07:15:50: ...  80%|████████  | 161/200 [00:13<00:02, 14.63 it/sec]
    INFO - 07:15:50: ...  82%|████████▏ | 163/200 [00:13<00:02, 14.45 it/sec]
    INFO - 07:15:50: ...  82%|████████▎ | 165/200 [00:14<00:02, 14.28 it/sec]
    INFO - 07:15:50: ...  84%|████████▎ | 167/200 [00:14<00:02, 14.12 it/sec]
    INFO - 07:15:51: ...  84%|████████▍ | 169/200 [00:14<00:02, 13.95 it/sec]
    INFO - 07:15:51: ...  86%|████████▌ | 171/200 [00:14<00:02, 13.80 it/sec]
    INFO - 07:15:51: ...  86%|████████▋ | 173/200 [00:14<00:01, 13.64 it/sec]
    INFO - 07:15:51: ...  88%|████████▊ | 175/200 [00:14<00:01, 13.49 it/sec]
    INFO - 07:15:51: ...  88%|████████▊ | 177/200 [00:14<00:01, 13.34 it/sec]
    INFO - 07:15:51: ...  90%|████████▉ | 179/200 [00:15<00:01, 13.20 it/sec]
    INFO - 07:15:52: ...  90%|█████████ | 181/200 [00:15<00:01, 13.05 it/sec]
    INFO - 07:15:52: ...  92%|█████████▏| 183/200 [00:15<00:01, 12.91 it/sec]
    INFO - 07:15:52: ...  92%|█████████▎| 185/200 [00:15<00:01, 12.78 it/sec]
    INFO - 07:15:52: ...  93%|█████████▎| 186/200 [00:15<00:01, 12.71 it/sec]
    INFO - 07:15:52: Optimization result:
    INFO - 07:15:52:    Optimizer info:
    INFO - 07:15:52:       Status: None
    INFO - 07:15:52:       Message: Successive iterates of the objective function are closer than ftol_rel or ftol_abs. GEMSEO Stopped the driver
    INFO - 07:15:52:       Number of calls to the objective function by the optimizer: 186
    INFO - 07:15:52:    Solution:
    INFO - 07:15:52:       The solution is feasible.
    INFO - 07:15:52:       Objective: 136.56131771172448
    INFO - 07:15:52:       Standardized constraints:
    INFO - 07:15:52:          volume fraction - 0.3 = -1.0460364907594055e-08
    INFO - 07:15:52: *** End MDOScenario execution (time: 0:00:15.758134) ***

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

Gallery generated by Sphinx-Gallery