Solve a 2D L-shape 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 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()

Out:

INFO - 13:04:19: Generating HTML XDSM file in : xdsm.html

Execute the scenario

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

Out:

    INFO - 13:04:19:
    INFO - 13:04:19: *** Start MDOScenario execution ***
    INFO - 13:04:19: MDOScenario
    INFO - 13:04:19:    Disciplines: DensityFilter MaterialModelInterpolation FininiteElementAnalysis VolumeFraction
    INFO - 13:04:19:    MDO formulation: DisciplinaryOpt
    INFO - 13:04:19: Optimization problem:
    INFO - 13:04:19:    minimize compliance(x)
    INFO - 13:04:19:    with respect to x
    INFO - 13:04:19:    subject to constraints:
    INFO - 13:04:19:       volume fraction(x) <= 0.3
    INFO - 13:04:19: Solving optimization problem with algorithm NLOPT_MMA:
    INFO - 13:04:19: ...   0%|          | 0/200 [00:00<?, ?it]
    INFO - 13:04:19: ...   1%|          | 2/200 [00:00<00:00, 1887.69 it/sec]
    INFO - 13:04:19: ...   2%|▎         | 5/200 [00:00<00:00, 828.86 it/sec]
    INFO - 13:04:19: ...   4%|▍         | 8/200 [00:00<00:00, 529.21 it/sec]
    INFO - 13:04:19: ...   6%|▌         | 11/200 [00:00<00:00, 388.33 it/sec]
    INFO - 13:04:19: ...   7%|▋         | 14/200 [00:00<00:00, 307.54 it/sec]
    INFO - 13:04:19: ...   8%|▊         | 17/200 [00:00<00:00, 255.12 it/sec]
    INFO - 13:04:20: ...  10%|█         | 20/200 [00:00<00:00, 218.07 it/sec]
    INFO - 13:04:20: ...  12%|█▏        | 23/200 [00:01<00:00, 190.78 it/sec]
    INFO - 13:04:20: ...  13%|█▎        | 26/200 [00:01<00:01, 169.55 it/sec]
    INFO - 13:04:20: ...  14%|█▍        | 29/200 [00:01<00:01, 152.70 it/sec]
    INFO - 13:04:20: ...  16%|█▌        | 32/200 [00:01<00:01, 138.86 it/sec]
    INFO - 13:04:20: ...  18%|█▊        | 35/200 [00:01<00:01, 127.40 it/sec]
    INFO - 13:04:20: ...  19%|█▉        | 38/200 [00:01<00:01, 117.70 it/sec]
    INFO - 13:04:20: ...  20%|██        | 41/200 [00:01<00:01, 109.38 it/sec]
    INFO - 13:04:21: ...  22%|██▏       | 44/200 [00:01<00:01, 102.19 it/sec]
    INFO - 13:04:21: ...  24%|██▎       | 47/200 [00:02<00:01, 95.93 it/sec]
    INFO - 13:04:21: ...  25%|██▌       | 50/200 [00:02<00:01, 90.60 it/sec]
    INFO - 13:04:21: ...  26%|██▋       | 53/200 [00:02<00:01, 85.61 it/sec]
    INFO - 13:04:21: ...  28%|██▊       | 56/200 [00:02<00:01, 81.16 it/sec]
    INFO - 13:04:21: ...  30%|██▉       | 59/200 [00:02<00:01, 77.16 it/sec]
    INFO - 13:04:21: ...  31%|███       | 62/200 [00:02<00:01, 73.57 it/sec]
    INFO - 13:04:21: ...  32%|███▎      | 65/200 [00:02<00:01, 70.21 it/sec]
    INFO - 13:04:22: ...  34%|███▍      | 68/200 [00:02<00:01, 67.19 it/sec]
    INFO - 13:04:22: ...  36%|███▌      | 71/200 [00:03<00:02, 64.45 it/sec]
    INFO - 13:04:22: ...  37%|███▋      | 74/200 [00:03<00:02, 61.94 it/sec]
    INFO - 13:04:22: ...  38%|███▊      | 77/200 [00:03<00:02, 59.60 it/sec]
    INFO - 13:04:22: ...  40%|████      | 80/200 [00:03<00:02, 57.46 it/sec]
    INFO - 13:04:22: ...  42%|████▏     | 83/200 [00:03<00:02, 55.49 it/sec]
    INFO - 13:04:22: ...  43%|████▎     | 86/200 [00:03<00:02, 53.60 it/sec]
    INFO - 13:04:22: ...  44%|████▍     | 89/200 [00:03<00:02, 51.87 it/sec]
    INFO - 13:04:23: ...  46%|████▌     | 92/200 [00:03<00:02, 50.26 it/sec]
    INFO - 13:04:23: ...  48%|████▊     | 95/200 [00:04<00:02, 48.74 it/sec]
    INFO - 13:04:23: ...  49%|████▉     | 98/200 [00:04<00:02, 47.31 it/sec]
    INFO - 13:04:23: ...  50%|█████     | 101/200 [00:04<00:02, 45.94 it/sec]
    INFO - 13:04:23: ...  52%|█████▏    | 104/200 [00:04<00:02, 44.64 it/sec]
    INFO - 13:04:23: ...  54%|█████▎    | 107/200 [00:04<00:02, 43.45 it/sec]
    INFO - 13:04:23: ...  55%|█████▌    | 110/200 [00:04<00:02, 42.32 it/sec]
    INFO - 13:04:23: ...  56%|█████▋    | 113/200 [00:04<00:02, 41.26 it/sec]
    INFO - 13:04:24: ...  58%|█████▊    | 116/200 [00:04<00:02, 40.22 it/sec]
    INFO - 13:04:24: ...  60%|█████▉    | 119/200 [00:05<00:02, 39.26 it/sec]
    INFO - 13:04:24: ...  61%|██████    | 122/200 [00:05<00:02, 38.33 it/sec]
    INFO - 13:04:24: ...  62%|██████▎   | 125/200 [00:05<00:02, 37.44 it/sec]
    INFO - 13:04:24: ...  64%|██████▍   | 128/200 [00:05<00:01, 36.60 it/sec]
    INFO - 13:04:24: ...  64%|██████▍   | 129/200 [00:05<00:01, 36.29 it/sec]
    INFO - 13:04:24: Optimization result:
    INFO - 13:04:24:    Optimizer info:
    INFO - 13:04:24:       Status: None
    INFO - 13:04:24:       Message: Successive iterates of the objective function are closer than ftol_rel or ftol_abs. GEMSEO Stopped the driver
    INFO - 13:04:24:       Number of calls to the objective function by the optimizer: 129
    INFO - 13:04:24:    Solution:
    INFO - 13:04:24:       The solution is feasible.
    INFO - 13:04:24:       Objective: 151.62873248988768
    INFO - 13:04:24:       Standardized constraints:
    INFO - 13:04:24:          volume fraction - 0.3 = 1.0969287427831098e-06
    INFO - 13:04:24: *** End MDOScenario execution (time: 0:00:05.526994) ***

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

Gallery generated by Sphinx-Gallery