Solve a 2D MBB 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 = "MBB"

Define the number of elements in x- and y- directions:

n_x = 50
n_y = 25

Define the full material Young’s modulus and the 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:25: Generating HTML XDSM file in : xdsm.html

Execute the scenario

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

Out:

    INFO - 13:04:25:
    INFO - 13:04:25: *** Start MDOScenario execution ***
    INFO - 13:04:25: MDOScenario
    INFO - 13:04:25:    Disciplines: DensityFilter MaterialModelInterpolation FininiteElementAnalysis VolumeFraction
    INFO - 13:04:25:    MDO formulation: DisciplinaryOpt
    INFO - 13:04:25: Optimization problem:
    INFO - 13:04:25:    minimize compliance(x)
    INFO - 13:04:25:    with respect to x
    INFO - 13:04:25:    subject to constraints:
    INFO - 13:04:25:       volume fraction(x) <= 0.3
    INFO - 13:04:25: Solving optimization problem with algorithm NLOPT_MMA:
    INFO - 13:04:25: ...   0%|          | 0/200 [00:00<?, ?it]
    INFO - 13:04:25: ...   1%|          | 2/200 [00:00<00:00, 972.38 it/sec]
    INFO - 13:04:25: ...   2%|▏         | 3/200 [00:00<00:00, 638.99 it/sec]
    INFO - 13:04:25: ...   2%|▎         | 5/200 [00:00<00:00, 397.19 it/sec]
    INFO - 13:04:26: ...   4%|▎         | 7/200 [00:00<00:00, 287.63 it/sec]
    INFO - 13:04:26: ...   4%|▍         | 9/200 [00:00<00:00, 225.94 it/sec]
    INFO - 13:04:26: ...   6%|▌         | 11/200 [00:01<00:01, 186.59 it/sec]
    INFO - 13:04:26: ...   6%|▋         | 13/200 [00:01<00:01, 159.09 it/sec]
    INFO - 13:04:26: ...   8%|▊         | 15/200 [00:01<00:01, 138.49 it/sec]
    INFO - 13:04:27: ...   8%|▊         | 17/200 [00:01<00:01, 122.86 it/sec]
    INFO - 13:04:27: ...  10%|▉         | 19/200 [00:01<00:01, 110.53 it/sec]
    INFO - 13:04:27: ...  10%|█         | 21/200 [00:01<00:01, 100.20 it/sec]
    INFO - 13:04:27: ...  12%|█▏        | 23/200 [00:02<00:01, 91.61 it/sec]
    INFO - 13:04:27: ...  12%|█▎        | 25/200 [00:02<00:02, 84.48 it/sec]
    INFO - 13:04:28: ...  14%|█▎        | 27/200 [00:02<00:02, 78.34 it/sec]
    INFO - 13:04:28: ...  14%|█▍        | 29/200 [00:02<00:02, 73.14 it/sec]
    INFO - 13:04:28: ...  16%|█▌        | 31/200 [00:02<00:02, 68.60 it/sec]
    INFO - 13:04:28: ...  16%|█▋        | 33/200 [00:03<00:02, 64.58 it/sec]
    INFO - 13:04:28: ...  18%|█▊        | 35/200 [00:03<00:02, 61.02 it/sec]
    INFO - 13:04:28: ...  18%|█▊        | 37/200 [00:03<00:02, 57.87 it/sec]
    INFO - 13:04:29: ...  20%|█▉        | 39/200 [00:03<00:02, 54.94 it/sec]
    INFO - 13:04:29: ...  20%|██        | 41/200 [00:03<00:03, 52.31 it/sec]
    INFO - 13:04:29: ...  22%|██▏       | 43/200 [00:04<00:03, 49.95 it/sec]
    INFO - 13:04:29: ...  22%|██▎       | 45/200 [00:04<00:03, 47.81 it/sec]
    INFO - 13:04:29: ...  24%|██▎       | 47/200 [00:04<00:03, 45.85 it/sec]
    INFO - 13:04:29: ...  24%|██▍       | 49/200 [00:04<00:03, 44.06 it/sec]
    INFO - 13:04:30: ...  26%|██▌       | 51/200 [00:04<00:03, 42.40 it/sec]
    INFO - 13:04:30: ...  26%|██▋       | 53/200 [00:04<00:03, 40.84 it/sec]
    INFO - 13:04:30: ...  28%|██▊       | 55/200 [00:05<00:03, 39.33 it/sec]
    INFO - 13:04:30: ...  28%|██▊       | 57/200 [00:05<00:03, 37.98 it/sec]
    INFO - 13:04:30: ...  30%|██▉       | 59/200 [00:05<00:03, 36.70 it/sec]
    INFO - 13:04:31: ...  30%|███       | 61/200 [00:05<00:03, 35.50 it/sec]
    INFO - 13:04:31: ...  32%|███▏      | 63/200 [00:05<00:03, 34.40 it/sec]
    INFO - 13:04:31: ...  32%|███▎      | 65/200 [00:05<00:04, 33.37 it/sec]
    INFO - 13:04:31: ...  34%|███▎      | 67/200 [00:06<00:04, 32.38 it/sec]
    INFO - 13:04:31: ...  34%|███▍      | 69/200 [00:06<00:04, 31.45 it/sec]
    INFO - 13:04:31: ...  36%|███▌      | 71/200 [00:06<00:04, 30.59 it/sec]
    INFO - 13:04:32: ...  36%|███▋      | 73/200 [00:06<00:04, 29.77 it/sec]
    INFO - 13:04:32: ...  38%|███▊      | 75/200 [00:06<00:04, 29.00 it/sec]
    INFO - 13:04:32: ...  38%|███▊      | 77/200 [00:07<00:04, 28.24 it/sec]
    INFO - 13:04:32: ...  40%|███▉      | 79/200 [00:07<00:04, 27.54 it/sec]
    INFO - 13:04:32: ...  40%|████      | 81/200 [00:07<00:04, 26.88 it/sec]
    INFO - 13:04:33: ...  42%|████▏     | 83/200 [00:07<00:04, 26.25 it/sec]
    INFO - 13:04:33: ...  42%|████▎     | 85/200 [00:07<00:04, 25.65 it/sec]
    INFO - 13:04:33: ...  44%|████▎     | 87/200 [00:07<00:04, 25.08 it/sec]
    INFO - 13:04:33: ...  44%|████▍     | 89/200 [00:08<00:04, 24.52 it/sec]
    INFO - 13:04:33: ...  46%|████▌     | 91/200 [00:08<00:04, 23.99 it/sec]
    INFO - 13:04:33: ...  46%|████▋     | 93/200 [00:08<00:04, 23.49 it/sec]
    INFO - 13:04:34: ...  48%|████▊     | 95/200 [00:08<00:04, 23.00 it/sec]
    INFO - 13:04:34: ...  48%|████▊     | 97/200 [00:08<00:04, 22.54 it/sec]
    INFO - 13:04:34: ...  50%|████▉     | 99/200 [00:09<00:04, 22.08 it/sec]
    INFO - 13:04:34: ...  50%|█████     | 101/200 [00:09<00:04, 21.65 it/sec]
    INFO - 13:04:34: ...  52%|█████▏    | 103/200 [00:09<00:04, 21.24 it/sec]
    INFO - 13:04:35: ...  52%|█████▎    | 105/200 [00:09<00:04, 20.85 it/sec]
    INFO - 13:04:35: ...  53%|█████▎    | 106/200 [00:09<00:04, 20.66 it/sec]
    INFO - 13:04:35: Optimization result:
    INFO - 13:04:35:    Optimizer info:
    INFO - 13:04:35:       Status: None
    INFO - 13:04:35:       Message: Successive iterates of the objective function are closer than ftol_rel or ftol_abs. GEMSEO Stopped the driver
    INFO - 13:04:35:       Number of calls to the objective function by the optimizer: 106
    INFO - 13:04:35:    Solution:
    INFO - 13:04:35:       The solution is feasible.
    INFO - 13:04:35:       Objective: 170.90194735674362
    INFO - 13:04:35:       Standardized constraints:
    INFO - 13:04:35:          volume fraction - 0.3 = -1.5002556197907246e-06
    INFO - 13:04:35: *** End MDOScenario execution (time: 0:00:09.701873) ***

{'max_iter': 200, 'algo': 'NLOPT_MMA'}

Results

Post-process the optmization 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 0x7fce8b7e3460>
../../_images/MBB_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 MBB../../_images/MBB_solution.png

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

Gallery generated by Sphinx-Gallery