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 - 10:05:29: Generating HTML XDSM file in : xdsm.html

Execute the scenario:

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

Out:

    INFO - 10:05:29:
    INFO - 10:05:29: *** Start MDOScenario execution ***
    INFO - 10:05:29: MDOScenario
    INFO - 10:05:29:    Disciplines: DensityFilter MaterialModelInterpolation FininiteElementAnalysis VolumeFraction
    INFO - 10:05:29:    MDO formulation: DisciplinaryOpt
    INFO - 10:05:29: Optimization problem:
    INFO - 10:05:29:    minimize compliance(x)
    INFO - 10:05:29:    with respect to x
    INFO - 10:05:29:    subject to constraints:
    INFO - 10:05:29:       volume fraction(x) <= 0.3
    INFO - 10:05:29: Solving optimization problem with algorithm NLOPT_MMA:
    INFO - 10:05:29: ...   0%|          | 0/200 [00:00<?, ?it]
    INFO - 10:05:29: ...   1%|          | 2/200 [00:00<00:00, 973.02 it/sec]
    INFO - 10:05:29: ...   2%|▏         | 3/200 [00:00<00:00, 651.04 it/sec]
    INFO - 10:05:29: ...   2%|▎         | 5/200 [00:00<00:00, 412.01 it/sec]
    INFO - 10:05:29: ...   4%|▎         | 7/200 [00:00<00:00, 298.46 it/sec]
    INFO - 10:05:30: ...   4%|▍         | 9/200 [00:00<00:00, 232.84 it/sec]
    INFO - 10:05:30: ...   6%|▌         | 11/200 [00:01<00:00, 190.99 it/sec]
    INFO - 10:05:30: ...   6%|▋         | 13/200 [00:01<00:01, 161.68 it/sec]
    INFO - 10:05:30: ...   8%|▊         | 15/200 [00:01<00:01, 140.95 it/sec]
    INFO - 10:05:30: ...   8%|▊         | 17/200 [00:01<00:01, 124.81 it/sec]
    INFO - 10:05:31: ...  10%|▉         | 19/200 [00:01<00:01, 111.79 it/sec]
    INFO - 10:05:31: ...  10%|█         | 21/200 [00:01<00:01, 101.52 it/sec]
    INFO - 10:05:31: ...  12%|█▏        | 23/200 [00:02<00:01, 93.05 it/sec]
    INFO - 10:05:31: ...  12%|█▎        | 25/200 [00:02<00:02, 85.90 it/sec]
    INFO - 10:05:31: ...  14%|█▎        | 27/200 [00:02<00:02, 79.70 it/sec]
    INFO - 10:05:31: ...  14%|█▍        | 29/200 [00:02<00:02, 74.49 it/sec]
    INFO - 10:05:32: ...  16%|█▌        | 31/200 [00:02<00:02, 70.04 it/sec]
    INFO - 10:05:32: ...  16%|█▋        | 33/200 [00:03<00:02, 66.02 it/sec]
    INFO - 10:05:32: ...  18%|█▊        | 35/200 [00:03<00:02, 62.40 it/sec]
    INFO - 10:05:32: ...  18%|█▊        | 37/200 [00:03<00:02, 59.25 it/sec]
    INFO - 10:05:32: ...  20%|█▉        | 39/200 [00:03<00:02, 56.38 it/sec]
    INFO - 10:05:33: ...  20%|██        | 41/200 [00:03<00:02, 53.78 it/sec]
    INFO - 10:05:33: ...  22%|██▏       | 43/200 [00:03<00:03, 51.41 it/sec]
    INFO - 10:05:33: ...  22%|██▎       | 45/200 [00:04<00:03, 49.18 it/sec]
    INFO - 10:05:33: ...  24%|██▎       | 47/200 [00:04<00:03, 47.21 it/sec]
    INFO - 10:05:33: ...  24%|██▍       | 49/200 [00:04<00:03, 45.38 it/sec]
    INFO - 10:05:33: ...  26%|██▌       | 51/200 [00:04<00:03, 43.66 it/sec]
    INFO - 10:05:34: ...  26%|██▋       | 53/200 [00:04<00:03, 42.07 it/sec]
    INFO - 10:05:34: ...  28%|██▊       | 55/200 [00:04<00:03, 40.62 it/sec]
    INFO - 10:05:34: ...  28%|██▊       | 57/200 [00:05<00:03, 39.24 it/sec]
    INFO - 10:05:34: ...  30%|██▉       | 59/200 [00:05<00:03, 37.93 it/sec]
    INFO - 10:05:34: ...  30%|███       | 61/200 [00:05<00:03, 36.73 it/sec]
    INFO - 10:05:34: ...  32%|███▏      | 63/200 [00:05<00:03, 35.62 it/sec]
    INFO - 10:05:35: ...  32%|███▎      | 65/200 [00:05<00:03, 34.55 it/sec]
    INFO - 10:05:35: ...  34%|███▎      | 67/200 [00:05<00:03, 33.55 it/sec]
    INFO - 10:05:35: ...  34%|███▍      | 69/200 [00:06<00:04, 32.63 it/sec]
    INFO - 10:05:35: ...  36%|███▌      | 71/200 [00:06<00:04, 31.73 it/sec]
    INFO - 10:05:35: ...  36%|███▋      | 73/200 [00:06<00:04, 30.90 it/sec]
    INFO - 10:05:35: ...  38%|███▊      | 75/200 [00:06<00:04, 30.11 it/sec]
    INFO - 10:05:36: ...  38%|███▊      | 77/200 [00:06<00:04, 29.36 it/sec]
    INFO - 10:05:36: ...  40%|███▉      | 79/200 [00:06<00:04, 28.65 it/sec]
    INFO - 10:05:36: ...  40%|████      | 81/200 [00:07<00:04, 27.96 it/sec]
    INFO - 10:05:36: ...  42%|████▏     | 83/200 [00:07<00:04, 27.29 it/sec]
    INFO - 10:05:36: ...  42%|████▎     | 85/200 [00:07<00:04, 26.68 it/sec]
    INFO - 10:05:36: ...  44%|████▎     | 87/200 [00:07<00:04, 26.09 it/sec]
    INFO - 10:05:37: ...  44%|████▍     | 89/200 [00:07<00:04, 25.52 it/sec]
    INFO - 10:05:37: ...  46%|████▌     | 91/200 [00:08<00:04, 24.97 it/sec]
    INFO - 10:05:37: ...  46%|████▋     | 93/200 [00:08<00:04, 24.46 it/sec]
    INFO - 10:05:37: ...  48%|████▊     | 95/200 [00:08<00:04, 23.95 it/sec]
    INFO - 10:05:37: ...  48%|████▊     | 97/200 [00:08<00:04, 23.47 it/sec]
    INFO - 10:05:37: ...  50%|████▉     | 99/200 [00:08<00:04, 23.02 it/sec]
    INFO - 10:05:38: ...  50%|█████     | 101/200 [00:08<00:04, 22.59 it/sec]
    INFO - 10:05:38: ...  52%|█████▏    | 103/200 [00:09<00:04, 22.16 it/sec]
    INFO - 10:05:38: ...  52%|█████▎    | 105/200 [00:09<00:04, 21.74 it/sec]
    INFO - 10:05:38: ...  54%|█████▎    | 107/200 [00:09<00:04, 21.34 it/sec]
    INFO - 10:05:38: ...  55%|█████▍    | 109/200 [00:09<00:04, 20.97 it/sec]
    INFO - 10:05:39: ...  56%|█████▌    | 111/200 [00:09<00:04, 20.60 it/sec]
    INFO - 10:05:39: ...  56%|█████▋    | 113/200 [00:09<00:04, 20.25 it/sec]
    INFO - 10:05:39: ...  57%|█████▊    | 115/200 [00:10<00:04, 19.91 it/sec]
    INFO - 10:05:39: ...  58%|█████▊    | 117/200 [00:10<00:04, 19.58 it/sec]
    INFO - 10:05:39: ...  60%|█████▉    | 119/200 [00:10<00:04, 19.26 it/sec]
    INFO - 10:05:39: ...  60%|██████    | 121/200 [00:10<00:04, 18.95 it/sec]
    INFO - 10:05:40: ...  62%|██████▏   | 123/200 [00:10<00:04, 18.65 it/sec]
    INFO - 10:05:40: ...  62%|██████▎   | 125/200 [00:10<00:04, 18.36 it/sec]
    INFO - 10:05:40: ...  64%|██████▎   | 127/200 [00:11<00:04, 18.08 it/sec]
    INFO - 10:05:40: ...  64%|██████▍   | 129/200 [00:11<00:03, 17.80 it/sec]
    INFO - 10:05:40: ...  66%|██████▌   | 131/200 [00:11<00:03, 17.54 it/sec]
    INFO - 10:05:40: ...  66%|██████▋   | 133/200 [00:11<00:03, 17.29 it/sec]
    INFO - 10:05:41: ...  68%|██████▊   | 135/200 [00:11<00:03, 17.04 it/sec]
    INFO - 10:05:41: ...  68%|██████▊   | 137/200 [00:11<00:03, 16.79 it/sec]
    INFO - 10:05:41: ...  70%|██████▉   | 139/200 [00:12<00:03, 16.56 it/sec]
    INFO - 10:05:41: ...  70%|███████   | 141/200 [00:12<00:03, 16.32 it/sec]
    INFO - 10:05:41: ...  72%|███████▏  | 143/200 [00:12<00:03, 16.10 it/sec]
    INFO - 10:05:41: ...  72%|███████▎  | 145/200 [00:12<00:03, 15.88 it/sec]
    INFO - 10:05:42: ...  74%|███████▎  | 147/200 [00:12<00:03, 15.66 it/sec]
    INFO - 10:05:42: ...  74%|███████▍  | 149/200 [00:12<00:03, 15.46 it/sec]
    INFO - 10:05:42: ...  76%|███████▌  | 151/200 [00:13<00:03, 15.25 it/sec]
    INFO - 10:05:42: ...  76%|███████▋  | 153/200 [00:13<00:03, 15.06 it/sec]
    INFO - 10:05:42: ...  78%|███████▊  | 155/200 [00:13<00:03, 14.87 it/sec]
    INFO - 10:05:42: ...  78%|███████▊  | 157/200 [00:13<00:02, 14.68 it/sec]
    INFO - 10:05:43: ...  80%|███████▉  | 159/200 [00:13<00:02, 14.50 it/sec]
    INFO - 10:05:43: ...  80%|████████  | 161/200 [00:13<00:02, 14.33 it/sec]
    INFO - 10:05:43: ...  82%|████████▏ | 163/200 [00:14<00:02, 14.15 it/sec]
    INFO - 10:05:43: ...  82%|████████▎ | 165/200 [00:14<00:02, 13.99 it/sec]
    INFO - 10:05:43: ...  84%|████████▎ | 167/200 [00:14<00:02, 13.82 it/sec]
    INFO - 10:05:43: ...  84%|████████▍ | 169/200 [00:14<00:02, 13.66 it/sec]
    INFO - 10:05:44: ...  86%|████████▌ | 171/200 [00:14<00:02, 13.51 it/sec]
    INFO - 10:05:44: ...  86%|████████▋ | 173/200 [00:14<00:02, 13.36 it/sec]
    INFO - 10:05:44: ...  88%|████████▊ | 175/200 [00:15<00:01, 13.21 it/sec]
    INFO - 10:05:44: ...  88%|████████▊ | 177/200 [00:15<00:01, 13.06 it/sec]
    INFO - 10:05:44: ...  90%|████████▉ | 179/200 [00:15<00:01, 12.92 it/sec]
    INFO - 10:05:44: ...  90%|█████████ | 181/200 [00:15<00:01, 12.78 it/sec]
    INFO - 10:05:45: ...  92%|█████████▏| 183/200 [00:15<00:01, 12.64 it/sec]
    INFO - 10:05:45: ...  92%|█████████▎| 185/200 [00:15<00:01, 12.51 it/sec]
    INFO - 10:05:45: ...  93%|█████████▎| 186/200 [00:16<00:01, 12.44 it/sec]
    INFO - 10:05:45: Optimization result:
    INFO - 10:05:45:    Optimizer info:
    INFO - 10:05:45:       Status: None
    INFO - 10:05:45:       Message: Successive iterates of the objective function are closer than ftol_rel or ftol_abs. GEMSEO Stopped the driver
    INFO - 10:05:45:       Number of calls to the objective function by the optimizer: 186
    INFO - 10:05:45:    Solution:
    INFO - 10:05:45:       The solution is feasible.
    INFO - 10:05:45:       Objective: 136.56131771172448
    INFO - 10:05:45:       Standardized constraints:
    INFO - 10:05:45:          volume fraction - 0.3 = -1.0460364907594055e-08
    INFO - 10:05:45: *** End MDOScenario execution (time: 0:00:16.098554) ***

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

Gallery generated by Sphinx-Gallery