Skip to content

Commit

Permalink
Enhance hello_world() with new arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
adtzlr committed Nov 28, 2024
1 parent c77b69f commit 444a12f
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 20 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ All notable changes to this project will be documented in this file. The format
### Added
- Add `SolidBody.assemble.mass(density=1.0)` and `SolidBodyNearlyIncompressible.assemble.mass(density=1.0)` to assemble the mass matrix.
- Add `SolidBody.evaluate.stress(field)` to evaluate the (first Piola-Kirchhoff) stress tensor (engineering stress in linear elasticity).
- Add a free-vibration modal analysis Step/Job `FreeVibration(items, boundaries)` with methods to evaluate `FreeVibration.evaluate()` and to extract `field, frequency = FreeVibration.extract(n)` its n-th result.

### Changed
- The first Piola-Kirchhoff stress tensor is evaluated if `ViewSolid(stress_type=None)`.
- Autodetect the stress-type in `SolidBody.plot(name)` from `name`.
- Enhance the `hello_world(axisymmetric=False, planestrain=False, curve=False, xdmf=False, container=False)` function with new arguments to customize the generated template script.

## [9.1.0] - 2024-11-23

Expand Down
156 changes: 136 additions & 20 deletions src/felupe/tools/_hello_world.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,16 @@
"""


def hello_world(pypardiso=False, parallel=False):
"""Print felupe's hyperelastic hello world.
def hello_world(
pypardiso=False,
parallel=False,
axisymmetric=False,
planestrain=False,
curve=False,
xdmf=False,
container=False,
):
"""Print FElupe's hyperelastic hello world.
Parameters
----------
Expand All @@ -27,6 +35,16 @@ def hello_world(pypardiso=False, parallel=False):
False).
parallel : bool, optional
Flag to activate a threaded vector- and matrix-assembly (default is False).
axisymmetric : bool, optional
Flag to create a template for an axisymmetric analysis (default is False).
planestrain : bool, optional
Flag to create a template for an plane strain analysis (default is False).
curve : bool, optional
Flag to use a characteristic-curve job (default is False).
xdmf : bool, optional
Flag to write a XDMF time-series result file (default is False).
container : bool, optional
Flag to use a mesh-container with multiple solid bodies (default is False).
"""

imports = [
Expand All @@ -42,23 +60,121 @@ def hello_world(pypardiso=False, parallel=False):
if parallel:
kwargs.append("parallel=True")

lines = [
"mesh = fem.Cube(n=6)",
"region = fem.RegionHexahedron(mesh)",
"field = fem.FieldContainer([fem.Field(region, dim=3)])",
"",
"boundaries, loadcase = fem.dof.uniaxial(field, clamped=True)",
"umat = fem.NeoHooke(mu=1, bulk=2)",
"solid = fem.SolidBody(umat=umat, field=field)",
"",
"move = fem.math.linsteps([0, 1], num=5)",
'ramp = {boundaries["move"]: move}',
"step = fem.Step(items=[solid], ramp=ramp, boundaries=boundaries)",
"",
"job = fem.Job(steps=[step])",
f'job.evaluate({", ".join(kwargs)})',
"",
'ax = solid.imshow("Principal Values of Cauchy Stress")',
]
if xdmf:
kwargs.append('filename="result.xdmf"')

region = "Region"
field = "Field"

if axisymmetric:
mesh = "Rectangle"
region += "Quad"
field += "Axisymmetric"
dim = 2

elif planestrain:
mesh = "Rectangle"
field += "PlaneStrain"
dim = 2

else:
mesh = "Cube"
region += "Hexahedron"
dim = 3

job = "Job"
kwargs_job = []
plot = []
if curve:
job = "CharacteristicCurve"
kwargs_job.append("")
kwargs_job.append('boundary=boundaries["move"]')
plot = [
"fig, ax = job.plot(",
r' xlabel=r"Displacement $d$ in mm $\longrightarrow$",',
r' ylabel=r"Normal Force $F$ in N $\longrightarrow$",',
")",
"",
]

kwargs_job = ", ".join(kwargs_job)
plot = "\n".join(plot)

if not container:

kwargs = ", ".join(kwargs)

# fmt: off
lines = [
f"mesh = fem.{mesh}(n=6)",
f"region = fem.{region}(mesh)",
f"field = fem.FieldContainer([fem.{field}(region, dim={dim})])",
"",
"boundaries, loadcase = fem.dof.uniaxial(field, clamped=True)",
"umat = fem.NeoHooke(mu=1, bulk=2)",
"solid = fem.SolidBody(umat=umat, field=field)",
"",
"move = fem.math.linsteps([0, 1], num=5)",
'ramp = {boundaries["move"]: move}',
"step = fem.Step(items=[solid], ramp=ramp, boundaries=boundaries)",
"",
f"job = fem.{job}(steps=[step]{kwargs_job})",
f"job.evaluate({kwargs})",
f"{plot}",
'ax = solid.imshow("Principal Values of Cauchy Stress")',
]
# fmt: on

else:

kwargs.insert(0, "x0=field")
kwargs = ", ".join(kwargs)

# fmt: off
lines = [
"meshes = [",
f" fem.{mesh}(n=6),",
f" fem.{mesh}(n=6).translate(1, axis=0),",
"]",
"container = fem.MeshContainer(meshes, merge=True)",
"mesh = container.stack()",
"",
f"region = fem.{region}(mesh)",
f"field = fem.FieldContainer([fem.{field}(region, dim={dim})])",
"",
"regions = [",
f" fem.{region}(container.meshes[0]),",
f" fem.{region}(container.meshes[1]),",
"]",
"fields = [",
f" fem.FieldContainer([fem.{field}(regions[0], dim={dim})]),",
f" fem.FieldContainer([fem.{field}(regions[1], dim={dim})]),",
"]",
"",
"boundaries, loadcase = fem.dof.uniaxial(field, clamped=True)",
"umats = [",
" fem.LinearElasticLargeStrain(E=2.1e5, nu=0.3),",
" fem.NeoHooke(mu=1),",
"]",
"solids = [",
" fem.SolidBody(umat=umats[0], field=fields[0]),",
" fem.SolidBodyNearlyIncompressible(umat=umats[1], field=fields[1], bulk=5000),",
"]",
"",
"move = fem.math.linsteps([0, 1], num=5)",
'ramp = {boundaries["move"]: move}',
"step = fem.Step(items=solids, ramp=ramp, boundaries=boundaries)",
"",
f"job = fem.{job}(steps=[step]{kwargs_job})",
f"job.evaluate({kwargs})",
f"{plot}",
'plotter = solids[0].plot(show_undeformed=False, style="wireframe")',
'solids[1].plot(',
' "Principal Values of Cauchy Stress",',
' plotter=plotter,',
' show_undeformed=False',
').show()',
]
# fmt: off

print("\n\n".join(["\n".join(imports), "\n".join(lines)]))

0 comments on commit 444a12f

Please sign in to comment.