Examples¶
Parameters¶
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Explore affects of parameters using complicance problem and MBB Beam."""
from __future__ import division
import context # noqa
from topopt import cli
def main():
"""Explore affects of parameters using complicance problem and MBB Beam."""
# Default input parameters
nelx, nely, volfrac, penalty, rmin, ft = cli.parse_args()
cli.main(nelx, nely, volfrac, penalty, rmin, ft)
# Vary the filter radius
for scaled_factor in [0.25, 2]:
cli.main(nelx, nely, volfrac, penalty, scaled_factor * rmin, ft)
# Vary the penalization power
for scaled_factor in [0.5, 4]:
cli.main(nelx, nely, volfrac, scaled_factor * penalty, rmin, ft)
# Vary the discreization
for scale_factor in [0.5, 2]:
cli.main(int(scale_factor * nelx), int(scale_factor * nely),
volfrac, penalty, rmin, ft)
if __name__ == "__main__":
main()
Simultaneous Point Loads¶
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Two simultaneous point loads."""
from __future__ import division
import numpy
import context # noqa
from topopt.boundary_conditions import BoundaryConditions
from topopt.utils import xy_to_id
from topopt import cli
class SimultaneousLoadsBoundaryConditions(BoundaryConditions):
"""Two simultaneous point loads along the top boundary."""
@property
def fixed_nodes(self):
"""Return a list of fixed nodes for the problem."""
bottom_left = 2 * xy_to_id(0, self.nely, self.nelx, self.nely)
bottom_right = 2 * xy_to_id(self.nelx, self.nely, self.nelx, self.nely)
fixed = numpy.array(
[bottom_left, bottom_left + 1, bottom_right, bottom_right + 1])
return fixed
@property
def forces(self):
"""Return the force vector for the problem."""
f = numpy.zeros((2 * (self.nelx + 1) * (self.nely + 1), 1))
id1 = 2 * xy_to_id(7 * self.nelx // 20, 0, self.nelx, self.nely) + 1
id2 = 2 * xy_to_id(13 * self.nelx // 20, 0, self.nelx, self.nely) + 1
f[[id1, id2], 0] = -1
return f
def main():
"""Two simultaneous point loads."""
# Default input parameters
nelx, nely, volfrac, penalty, rmin, ft = cli.parse_args(
nelx=120, volfrac=0.2, rmin=1.5)
cli.main(nelx, nely, volfrac, penalty, rmin, ft,
bc=SimultaneousLoadsBoundaryConditions(nelx, nely))
if __name__ == "__main__":
main()
Distributed Load¶
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Distributed load."""
from __future__ import division
import numpy
import context # noqa
from topopt.boundary_conditions import BoundaryConditions
from topopt.utils import xy_to_id
from topopt import cli
class DistributedLoadBoundaryConditions(BoundaryConditions):
"""Distributed load along the top boundary."""
@property
def fixed_nodes(self):
"""Return a list of fixed nodes for the problem."""
bottom_left = 2 * xy_to_id(0, self.nely, self.nelx, self.nely)
bottom_right = 2 * xy_to_id(
self.nelx, self.nely, self.nelx, self.nely)
fixed = numpy.array([bottom_left, bottom_left + 1,
bottom_right, bottom_right + 1])
return fixed
@property
def forces(self):
"""Return the force vector for the problem."""
topx_to_id = numpy.vectorize(
lambda x: xy_to_id(x, 0, self.nelx, self.nely))
topx = 2 * topx_to_id(numpy.arange(self.nelx + 1)) + 1
f = numpy.zeros((2 * (self.nelx + 1) * (self.nely + 1), 1))
f[topx, 0] = -1
return f
@property
def nonuniform_forces(self):
"""Return the force vector for the problem."""
topx_to_id = numpy.vectorize(
lambda x: xy_to_id(x, 0, self.nelx, self.nely))
topx = 2 * topx_to_id(numpy.arange(self.nelx + 1)) + 1
f = numpy.zeros((2 * (self.nelx + 1) * (self.nely + 1), 1))
f[topx, 0] = (0.5 * numpy.cos(
numpy.linspace(0, 2 * numpy.pi, topx.shape[0])) - 0.5)
return f
def main():
"""Distributed load."""
# Default input parameters
nelx, nely, volfrac, penalty, rmin, ft = cli.parse_args(
nelx=120, volfrac=0.2, rmin=1.2)
cli.main(nelx, nely, volfrac, penalty, rmin, ft,
bc=DistributedLoadBoundaryConditions(nelx, nely))
if __name__ == "__main__":
main()
Multiple Loads¶
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Multiple loads."""
from __future__ import division
import numpy
import context # noqa
from topopt.boundary_conditions import BoundaryConditions
from topopt.problems import ComplianceProblem
from topopt.utils import xy_to_id
from topopt import cli
class MultipleLoadsBoundaryConditions(BoundaryConditions):
"""Multiple loads applied to the top boundary."""
@property
def fixed_nodes(self):
"""Return a list of fixed nodes for the problem."""
bottom_left = 2 * xy_to_id(0, self.nely, self.nelx, self.nely)
bottom_right = 2 * xy_to_id(self.nelx, self.nely, self.nelx, self.nely)
fixed = numpy.array(
[bottom_left, bottom_left + 1, bottom_right, bottom_right + 1])
return fixed
@property
def forces(self):
"""Return the force vector for the problem."""
f = numpy.zeros((2 * (self.nelx + 1) * (self.nely + 1), 2))
id1 = 2 * xy_to_id(7 * self.nelx // 20, 0, self.nelx, self.nely) + 1
id2 = 2 * xy_to_id(13 * self.nelx // 20, 0, self.nelx, self.nely) + 1
f[id1, 0] = -1
f[id2, 1] = -1
return f
def main():
"""Multiple loads."""
# Default input parameters
nelx, nely, volfrac, penalty, rmin, ft = cli.parse_args(
nelx=120, volfrac=0.2, rmin=1.5)
bc = MultipleLoadsBoundaryConditions(nelx, nely)
problem = ComplianceProblem(bc, penalty)
cli.main(nelx, nely, volfrac, penalty, rmin, ft, bc=bc,
problem=problem)
if __name__ == "__main__":
main()
Computing Stress¶
Distributed Load¶
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Multiple simultaneous point loads with stress computation."""
from __future__ import division
import context # noqa
from topopt.guis import StressGUI
from topopt.problems import VonMisesStressProblem
from topopt import cli
from distributed_load import DistributedLoadBoundaryConditions
def main():
"""Multiple simultaneous point loads with stress computation."""
# Default input parameters
nelx, nely, volfrac, penalty, rmin, ft = cli.parse_args(
nelx=120, volfrac=0.2, rmin=1.2)
bc = DistributedLoadBoundaryConditions(nelx, nely)
problem = VonMisesStressProblem(nelx, nely, penalty, bc)
gui = StressGUI(problem, title="Stresses of Distributed Load Example")
cli.main(nelx, nely, volfrac, penalty, rmin, ft, bc=bc,
problem=problem, gui=gui)
if __name__ == "__main__":
main()
Multiple Load¶
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Multiple loads with stresses."""
from __future__ import division
import context # noqa
from topopt.problems import VonMisesStressProblem
from topopt.guis import StressGUI
from topopt import cli
from multiple_loads_mma import MultipleLoadsBoundaryConditions
def main():
"""Multiple loads with stresses."""
# Default input parameters
nelx, nely, volfrac, penalty, rmin, ft = cli.parse_args(
nelx=120, volfrac=0.2, rmin=1.5)
bc = MultipleLoadsBoundaryConditions(nelx, nely)
problem = VonMisesStressProblem(nelx, nely, penalty, bc)
title = cli.title_str(nelx, nely, volfrac, rmin, penalty)
gui = StressGUI(problem, title)
cli.main(nelx, nely, volfrac, penalty, rmin, ft, bc=bc,
problem=problem, gui=gui)
if __name__ == "__main__":
main()