Solve a 2D L-shape 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 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()
INFO - 09:02:15: Generating HTML XDSM file in : xdsm.html

Execute the scenario

scenario.execute({"max_iter": 200, "algo": "NLOPT_MMA"})
    INFO - 09:02:15:
    INFO - 09:02:15: *** Start MDOScenario execution ***
    INFO - 09:02:15: MDOScenario
    INFO - 09:02:15:    Disciplines: DensityFilter FininiteElementAnalysis MaterialModelInterpolation VolumeFraction
    INFO - 09:02:15:    MDO formulation: DisciplinaryOpt
    INFO - 09:02:15: Optimization problem:
    INFO - 09:02:15:    minimize compliance(x)
    INFO - 09:02:15:    with respect to x
    INFO - 09:02:15:    subject to constraints:
    INFO - 09:02:15:       volume fraction(x) <= 0.3
    INFO - 09:02:15: Solving optimization problem with algorithm NLOPT_MMA:
    INFO - 09:02:15: ...   0%|          | 0/200 [00:00<?, ?it]
    INFO - 09:02:15: ...   0%|          | 1/200 [00:00<00:10, 19.11 it/sec, obj=2.59e+3]
    INFO - 09:02:16: ...   1%|          | 2/200 [00:00<00:15, 13.10 it/sec, obj=2.23e+3]
    INFO - 09:02:16: ...   2%|▏         | 3/200 [00:00<00:12, 15.18 it/sec, obj=1.96e+3]
    INFO - 09:02:16: ...   2%|▏         | 4/200 [00:00<00:11, 16.61 it/sec, obj=1.75e+3]
    INFO - 09:02:16: ...   2%|▎         | 5/200 [00:00<00:11, 17.52 it/sec, obj=1.57e+3]
    INFO - 09:02:16: ...   3%|▎         | 6/200 [00:00<00:10, 18.15 it/sec, obj=1.2e+3]
    INFO - 09:02:16: ...   4%|▎         | 7/200 [00:00<00:10, 18.57 it/sec, obj=963]
    INFO - 09:02:16: ...   4%|▍         | 8/200 [00:00<00:10, 18.93 it/sec, obj=624]
    INFO - 09:02:16: ...   4%|▍         | 9/200 [00:00<00:09, 19.16 it/sec, obj=497]
    INFO - 09:02:16: ...   5%|▌         | 10/200 [00:00<00:09, 19.43 it/sec, obj=395]
    INFO - 09:02:16: ...   6%|▌         | 11/200 [00:00<00:09, 19.65 it/sec, obj=306]
    INFO - 09:02:16: ...   6%|▌         | 12/200 [00:00<00:09, 19.86 it/sec, obj=251]
    INFO - 09:02:16: ...   6%|▋         | 13/200 [00:00<00:09, 20.04 it/sec, obj=225]
    INFO - 09:02:16: ...   7%|▋         | 14/200 [00:00<00:09, 20.19 it/sec, obj=209]
    INFO - 09:02:16: ...   8%|▊         | 15/200 [00:00<00:09, 20.35 it/sec, obj=201]
    INFO - 09:02:16: ...   8%|▊         | 16/200 [00:00<00:08, 20.49 it/sec, obj=195]
    INFO - 09:02:16: ...   8%|▊         | 17/200 [00:00<00:08, 20.61 it/sec, obj=190]
    INFO - 09:02:16: ...   9%|▉         | 18/200 [00:00<00:08, 20.74 it/sec, obj=186]
    INFO - 09:02:16: ...  10%|▉         | 19/200 [00:00<00:08, 20.81 it/sec, obj=182]
    INFO - 09:02:16: ...  10%|█         | 20/200 [00:00<00:08, 20.93 it/sec, obj=178]
    INFO - 09:02:16: ...  10%|█         | 21/200 [00:01<00:08, 20.99 it/sec, obj=174]
    INFO - 09:02:16: ...  11%|█         | 22/200 [00:01<00:08, 21.08 it/sec, obj=169]
    INFO - 09:02:17: ...  12%|█▏        | 23/200 [00:01<00:08, 21.15 it/sec, obj=164]
    INFO - 09:02:17: ...  12%|█▏        | 24/200 [00:01<00:08, 21.22 it/sec, obj=161]
    INFO - 09:02:17: ...  12%|█▎        | 25/200 [00:01<00:08, 21.29 it/sec, obj=159]
    INFO - 09:02:17: ...  13%|█▎        | 26/200 [00:01<00:08, 21.38 it/sec, obj=157]
    INFO - 09:02:17: ...  14%|█▎        | 27/200 [00:01<00:08, 21.47 it/sec, obj=156]
    INFO - 09:02:17: ...  14%|█▍        | 28/200 [00:01<00:07, 21.54 it/sec, obj=155]
    INFO - 09:02:17: ...  14%|█▍        | 29/200 [00:01<00:07, 21.60 it/sec, obj=154]
    INFO - 09:02:17: ...  15%|█▌        | 30/200 [00:01<00:07, 21.68 it/sec, obj=154]
    INFO - 09:02:17: ...  16%|█▌        | 31/200 [00:01<00:07, 21.73 it/sec, obj=153]
    INFO - 09:02:17: ...  16%|█▌        | 32/200 [00:01<00:07, 21.79 it/sec, obj=153]
    INFO - 09:02:17: ...  16%|█▋        | 33/200 [00:01<00:07, 21.84 it/sec, obj=152]
    INFO - 09:02:17: ...  17%|█▋        | 34/200 [00:01<00:07, 21.89 it/sec, obj=152]
    INFO - 09:02:17: ...  18%|█▊        | 35/200 [00:01<00:07, 21.93 it/sec, obj=152]
    INFO - 09:02:17: ...  18%|█▊        | 36/200 [00:01<00:07, 21.97 it/sec, obj=152]
    INFO - 09:02:17: ...  18%|█▊        | 37/200 [00:01<00:07, 21.99 it/sec, obj=152]
    INFO - 09:02:17: ...  19%|█▉        | 38/200 [00:01<00:07, 22.03 it/sec, obj=152]
    INFO - 09:02:17: ...  20%|█▉        | 39/200 [00:01<00:07, 22.07 it/sec, obj=152]
    INFO - 09:02:17: ...  20%|██        | 40/200 [00:01<00:07, 22.10 it/sec, obj=152]
    INFO - 09:02:17: ...  20%|██        | 41/200 [00:01<00:07, 22.14 it/sec, obj=152]
    INFO - 09:02:17: ...  21%|██        | 42/200 [00:01<00:07, 22.18 it/sec, obj=152]
    INFO - 09:02:17: ...  22%|██▏       | 43/200 [00:01<00:07, 22.20 it/sec, obj=152]
    INFO - 09:02:17: ...  22%|██▏       | 44/200 [00:01<00:07, 22.22 it/sec, obj=152]
    INFO - 09:02:17: ...  22%|██▎       | 45/200 [00:02<00:06, 22.25 it/sec, obj=152]
    INFO - 09:02:17: ...  23%|██▎       | 46/200 [00:02<00:06, 22.26 it/sec, obj=152]
    INFO - 09:02:18: ...  24%|██▎       | 47/200 [00:02<00:06, 22.28 it/sec, obj=152]
    INFO - 09:02:18: ...  24%|██▍       | 48/200 [00:02<00:06, 22.32 it/sec, obj=152]
    INFO - 09:02:18: ...  24%|██▍       | 49/200 [00:02<00:06, 22.35 it/sec, obj=152]
    INFO - 09:02:18: ...  25%|██▌       | 50/200 [00:02<00:06, 22.37 it/sec, obj=152]
    INFO - 09:02:18: ...  26%|██▌       | 51/200 [00:02<00:06, 22.40 it/sec, obj=152]
    INFO - 09:02:18: ...  26%|██▌       | 52/200 [00:02<00:06, 22.43 it/sec, obj=152]
    INFO - 09:02:18: ...  26%|██▋       | 53/200 [00:02<00:06, 22.45 it/sec, obj=152]
    INFO - 09:02:18: ...  27%|██▋       | 54/200 [00:02<00:06, 22.47 it/sec, obj=152]
    INFO - 09:02:18: ...  28%|██▊       | 55/200 [00:02<00:06, 22.49 it/sec, obj=152]
    INFO - 09:02:18: ...  28%|██▊       | 56/200 [00:02<00:06, 22.51 it/sec, obj=152]
    INFO - 09:02:18: ...  28%|██▊       | 57/200 [00:02<00:06, 22.52 it/sec, obj=152]
    INFO - 09:02:18: ...  29%|██▉       | 58/200 [00:02<00:06, 22.53 it/sec, obj=152]
    INFO - 09:02:18: ...  30%|██▉       | 59/200 [00:02<00:06, 22.53 it/sec, obj=152]
    INFO - 09:02:18: ...  30%|███       | 60/200 [00:02<00:06, 22.54 it/sec, obj=152]
    INFO - 09:02:18: ...  30%|███       | 61/200 [00:02<00:06, 22.54 it/sec, obj=152]
    INFO - 09:02:18: ...  31%|███       | 62/200 [00:02<00:06, 22.55 it/sec, obj=152]
    INFO - 09:02:18: ...  32%|███▏      | 63/200 [00:02<00:06, 22.56 it/sec, obj=152]
    INFO - 09:02:18: ...  32%|███▏      | 64/200 [00:02<00:06, 22.57 it/sec, obj=152]
    INFO - 09:02:18: ...  32%|███▎      | 65/200 [00:02<00:05, 22.58 it/sec, obj=152]
    INFO - 09:02:18: ...  33%|███▎      | 66/200 [00:02<00:05, 22.59 it/sec, obj=152]
    INFO - 09:02:18: ...  34%|███▎      | 67/200 [00:02<00:05, 22.61 it/sec, obj=152]
    INFO - 09:02:18: ...  34%|███▍      | 68/200 [00:03<00:05, 22.63 it/sec, obj=152]
    INFO - 09:02:18: ...  34%|███▍      | 69/200 [00:03<00:05, 22.64 it/sec, obj=152]
    INFO - 09:02:19: ...  35%|███▌      | 70/200 [00:03<00:05, 22.64 it/sec, obj=152]
    INFO - 09:02:19: ...  36%|███▌      | 71/200 [00:03<00:05, 22.67 it/sec, obj=152]
    INFO - 09:02:19: ...  36%|███▌      | 72/200 [00:03<00:05, 22.68 it/sec, obj=152]
    INFO - 09:02:19: ...  36%|███▋      | 73/200 [00:03<00:05, 22.70 it/sec, obj=152]
    INFO - 09:02:19: ...  37%|███▋      | 74/200 [00:03<00:05, 22.72 it/sec, obj=152]
    INFO - 09:02:19: ...  38%|███▊      | 75/200 [00:03<00:05, 22.74 it/sec, obj=152]
    INFO - 09:02:19: ...  38%|███▊      | 76/200 [00:03<00:05, 22.76 it/sec, obj=152]
    INFO - 09:02:19: ...  38%|███▊      | 77/200 [00:03<00:05, 22.77 it/sec, obj=152]
    INFO - 09:02:19: ...  39%|███▉      | 78/200 [00:03<00:05, 22.78 it/sec, obj=152]
    INFO - 09:02:19: ...  40%|███▉      | 79/200 [00:03<00:05, 22.79 it/sec, obj=152]
    INFO - 09:02:19: ...  40%|████      | 80/200 [00:03<00:05, 22.80 it/sec, obj=152]
    INFO - 09:02:19: ...  40%|████      | 81/200 [00:03<00:05, 22.82 it/sec, obj=152]
    INFO - 09:02:19: ...  41%|████      | 82/200 [00:03<00:05, 22.83 it/sec, obj=152]
    INFO - 09:02:19: ...  42%|████▏     | 83/200 [00:03<00:05, 22.85 it/sec, obj=152]
    INFO - 09:02:19: ...  42%|████▏     | 84/200 [00:03<00:05, 22.86 it/sec, obj=152]
    INFO - 09:02:19: ...  42%|████▎     | 85/200 [00:03<00:05, 22.88 it/sec, obj=152]
    INFO - 09:02:19: ...  43%|████▎     | 86/200 [00:03<00:04, 22.89 it/sec, obj=152]
    INFO - 09:02:19: ...  44%|████▎     | 87/200 [00:03<00:04, 22.91 it/sec, obj=152]
    INFO - 09:02:19: ...  44%|████▍     | 88/200 [00:03<00:04, 22.93 it/sec, obj=152]
    INFO - 09:02:19: ...  44%|████▍     | 89/200 [00:03<00:04, 22.94 it/sec, obj=152]
    INFO - 09:02:19: ...  45%|████▌     | 90/200 [00:03<00:04, 22.95 it/sec, obj=152]
    INFO - 09:02:19: ...  46%|████▌     | 91/200 [00:03<00:04, 22.96 it/sec, obj=152]
    INFO - 09:02:19: ...  46%|████▌     | 92/200 [00:04<00:04, 22.97 it/sec, obj=152]
    INFO - 09:02:19: ...  46%|████▋     | 93/200 [00:04<00:04, 22.98 it/sec, obj=152]
    INFO - 09:02:20: ...  47%|████▋     | 94/200 [00:04<00:04, 22.98 it/sec, obj=152]
    INFO - 09:02:20: ...  48%|████▊     | 95/200 [00:04<00:04, 22.99 it/sec, obj=152]
    INFO - 09:02:20: ...  48%|████▊     | 96/200 [00:04<00:04, 22.99 it/sec, obj=152]
    INFO - 09:02:20: ...  48%|████▊     | 97/200 [00:04<00:04, 23.01 it/sec, obj=152]
    INFO - 09:02:20: ...  49%|████▉     | 98/200 [00:04<00:04, 23.02 it/sec, obj=152]
    INFO - 09:02:20: ...  50%|████▉     | 99/200 [00:04<00:04, 23.03 it/sec, obj=152]
    INFO - 09:02:20: ...  50%|█████     | 100/200 [00:04<00:04, 23.04 it/sec, obj=152]
    INFO - 09:02:20: ...  50%|█████     | 101/200 [00:04<00:04, 23.04 it/sec, obj=152]
    INFO - 09:02:20: ...  51%|█████     | 102/200 [00:04<00:04, 23.05 it/sec, obj=152]
    INFO - 09:02:20: ...  52%|█████▏    | 103/200 [00:04<00:04, 23.05 it/sec, obj=152]
    INFO - 09:02:20: ...  52%|█████▏    | 104/200 [00:04<00:04, 23.06 it/sec, obj=152]
    INFO - 09:02:20: ...  52%|█████▎    | 105/200 [00:04<00:04, 23.06 it/sec, obj=152]
    INFO - 09:02:20: ...  53%|█████▎    | 106/200 [00:04<00:04, 23.07 it/sec, obj=152]
    INFO - 09:02:20: ...  54%|█████▎    | 107/200 [00:04<00:04, 23.09 it/sec, obj=152]
    INFO - 09:02:20: ...  54%|█████▍    | 108/200 [00:04<00:03, 23.09 it/sec, obj=152]
    INFO - 09:02:20: ...  55%|█████▍    | 109/200 [00:04<00:03, 23.10 it/sec, obj=152]
    INFO - 09:02:20: ...  55%|█████▌    | 110/200 [00:04<00:03, 23.11 it/sec, obj=152]
    INFO - 09:02:20: ...  56%|█████▌    | 111/200 [00:04<00:03, 23.13 it/sec, obj=152]
    INFO - 09:02:20: ...  56%|█████▌    | 112/200 [00:04<00:03, 23.14 it/sec, obj=152]
    INFO - 09:02:20: ...  56%|█████▋    | 113/200 [00:04<00:03, 23.15 it/sec, obj=152]
    INFO - 09:02:20: ...  57%|█████▋    | 114/200 [00:04<00:03, 23.16 it/sec, obj=152]
    INFO - 09:02:20: ...  57%|█████▊    | 115/200 [00:04<00:03, 23.17 it/sec, obj=152]
    INFO - 09:02:20: ...  58%|█████▊    | 116/200 [00:05<00:03, 23.19 it/sec, obj=152]
    INFO - 09:02:20: ...  58%|█████▊    | 117/200 [00:05<00:03, 23.20 it/sec, obj=152]
    INFO - 09:02:21: ...  59%|█████▉    | 118/200 [00:05<00:03, 23.12 it/sec, obj=152]
    INFO - 09:02:21: ...  60%|█████▉    | 119/200 [00:05<00:03, 23.05 it/sec, obj=152]
    INFO - 09:02:21: ...  60%|██████    | 120/200 [00:05<00:03, 23.06 it/sec, obj=152]
    INFO - 09:02:21: ...  60%|██████    | 121/200 [00:05<00:03, 23.07 it/sec, obj=152]
    INFO - 09:02:21: ...  61%|██████    | 122/200 [00:05<00:03, 23.09 it/sec, obj=152]
    INFO - 09:02:21: ...  62%|██████▏   | 123/200 [00:05<00:03, 23.10 it/sec, obj=152]
    INFO - 09:02:21: ...  62%|██████▏   | 124/200 [00:05<00:03, 23.12 it/sec, obj=152]
    INFO - 09:02:21: ...  62%|██████▎   | 125/200 [00:05<00:03, 23.13 it/sec, obj=152]
    INFO - 09:02:21: ...  63%|██████▎   | 126/200 [00:05<00:03, 23.14 it/sec, obj=152]
    INFO - 09:02:21: ...  64%|██████▎   | 127/200 [00:05<00:03, 23.15 it/sec, obj=152]
    INFO - 09:02:21: ...  64%|██████▍   | 128/200 [00:05<00:03, 23.16 it/sec, obj=152]
    INFO - 09:02:21: ...  64%|██████▍   | 129/200 [00:05<00:03, 23.32 it/sec, obj=Not evaluated]
    INFO - 09:02:21: Optimization result:
    INFO - 09:02:21:    Optimizer info:
    INFO - 09:02:21:       Status: None
    INFO - 09:02:21:       Message: Successive iterates of the objective function are closer than ftol_rel or ftol_abs. GEMSEO Stopped the driver
    INFO - 09:02:21:       Number of calls to the objective function by the optimizer: 129
    INFO - 09:02:21:    Solution:
    INFO - 09:02:21:       The solution is feasible.
    INFO - 09:02:21:       Objective: 151.62873248988768
    INFO - 09:02:21:       Standardized constraints:
    INFO - 09:02:21:          volume fraction - 0.3 = 1.0969287427831098e-06
    INFO - 09:02:21: *** End MDOScenario execution (time: 0:00:05.549742) ***

{'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 0x7fd39a2985b0>
../../_images/L-Shape_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 L shape../../_images/L-Shape_solution.png

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

Gallery generated by Sphinx-Gallery