{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%matplotlib inline"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Solve a 2D MBB topology optimization problem\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from __future__ import annotations\n\nimport matplotlib.pyplot as plt\nfrom gemseo.api import configure_logger\nfrom gemseo.api import create_scenario\nfrom gemseo.problems.topo_opt.topopt_initialize import (\n initialize_design_space_and_discipline_to,\n)\nfrom matplotlib import colors\n\nconfigure_logger()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Setup the topology optimization problem\n\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Define the target volume fraction:\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"volume_fraction = 0.3"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Define the problem type:\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"problem_name = \"MBB\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Define the number of elements in x- and y- directions:\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"n_x = 50\nn_y = 25"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Define the full material Young's modulus and the Poisson's ratio:\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"e0 = 1\nnu = 0.3"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Define the penalty of the SIMP approach:\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"penalty = 3"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Define the minimum member size in the solution:\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"min_member_size = 1.5"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Instantiate the :class:`.DesignSpace` and the disciplines:\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"design_space, disciplines = initialize_design_space_and_discipline_to(\n problem=problem_name,\n n_x=n_x,\n n_y=n_y,\n e0=e0,\n nu=nu,\n penalty=penalty,\n min_member_size=min_member_size,\n vf0=volume_fraction,\n)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Solve the topology optimization problem\nGenerate a :class:`.MDOScenario`\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"scenario = create_scenario(\n disciplines,\n formulation=\"DisciplinaryOpt\",\n objective_name=\"compliance\",\n design_space=design_space,\n)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Add the volume fraction constraint to the scenario:\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"scenario.add_constraint(\"volume fraction\", \"ineq\", value=volume_fraction)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Generate the XDSM\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"scenario.xdsmize()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Execute the scenario\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"scenario.execute(input_data={\"max_iter\": 200, \"algo\": \"NLOPT_MMA\"})"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Results\nPost-process the optmization history:\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"scenario.post_process(\n \"BasicHistory\",\n variable_names=[\"compliance\"],\n save=True,\n show=False,\n file_name=problem_name + \"_history.png\",\n)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Plot the solution\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"plt.ion() # Ensure that redrawing is possible\nfig, ax = plt.subplots()\nim = ax.imshow(\n -scenario.optimization_result.x_opt.reshape((n_x, n_y)).T,\n cmap=\"gray\",\n interpolation=\"none\",\n norm=colors.Normalize(vmin=-1, vmax=0),\n)\nfig.show()\nim.set_array(-scenario.optimization_result.x_opt.reshape((n_x, n_y)).T)\nfig.canvas.draw()\nplt.savefig(problem_name + \"_solution.png\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
}
},
"nbformat": 4,
"nbformat_minor": 0
}