.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/pre-processing/example_build_mesh.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_gallery_pre-processing_example_build_mesh.py: Building a model with MAPDL-style verbs ======================================= When you don't have a CDB and don't want to build a pyvista mesh from scratch, the :class:`femorph_solver.Model` exposes a set of verbs that mirror MAPDL's ``/PREP7`` commands one-to-one: ``n`` for nodes, ``e`` for elements, ``et`` / ``mp`` / ``r`` for the type / material / real tables. Here we build a 4-element cantilever beam out of SOLID185 hexes entirely with the verb API — no pyvista, no CDB. .. GENERATED FROM PYTHON SOURCE LINES 12-20 .. code-block:: Python from __future__ import annotations import numpy as np import pyvista as pv import femorph_solver .. GENERATED FROM PYTHON SOURCE LINES 21-23 Start from an empty Model ------------------------- .. GENERATED FROM PYTHON SOURCE LINES 23-31 .. code-block:: Python m = femorph_solver.Model() m.et(1, "SOLID185") m.mp("EX", 1, 2.1e11) m.mp("PRXY", 1, 0.30) m.mp("DENS", 1, 7850.0) m.type(1) m.mat(1) .. rst-class:: sphx-glr-script-out .. code-block:: none /home/runner/_work/solver/solver/examples/pre-processing/example_build_mesh.py:24: DeprecationWarning: Model.et(...) is a MAPDL-dialect shortcut and has moved off the Model public surface. Use `APDL(model).et(et_id, name)` for line-by-line APDL deck porting, or the native `Model.assign("HEX8", material)` for new code. m.et(1, "SOLID185") /home/runner/_work/solver/solver/examples/pre-processing/example_build_mesh.py:25: DeprecationWarning: Model.mp(...) is a MAPDL-dialect shortcut and has moved off the Model public surface. Use `APDL(model).mp(prop, mat_id, value)` for line-by-line APDL deck porting, or the native `Model.assign(element, {prop: value, ...})` for new code. m.mp("EX", 1, 2.1e11) /home/runner/_work/solver/solver/examples/pre-processing/example_build_mesh.py:26: DeprecationWarning: Model.mp(...) is a MAPDL-dialect shortcut and has moved off the Model public surface. Use `APDL(model).mp(prop, mat_id, value)` for line-by-line APDL deck porting, or the native `Model.assign(element, {prop: value, ...})` for new code. m.mp("PRXY", 1, 0.30) /home/runner/_work/solver/solver/examples/pre-processing/example_build_mesh.py:27: DeprecationWarning: Model.mp(...) is a MAPDL-dialect shortcut and has moved off the Model public surface. Use `APDL(model).mp(prop, mat_id, value)` for line-by-line APDL deck porting, or the native `Model.assign(element, {prop: value, ...})` for new code. m.mp("DENS", 1, 7850.0) /home/runner/_work/solver/solver/examples/pre-processing/example_build_mesh.py:28: DeprecationWarning: Model.type(...) is a MAPDL-dialect shortcut and has moved off the Model public surface. Use `APDL(model).type(et_id)` for line-by-line APDL deck porting, or the native `Model.from_grid(pv_grid)` for new code. m.type(1) /home/runner/_work/solver/solver/examples/pre-processing/example_build_mesh.py:29: DeprecationWarning: Model.mat(...) is a MAPDL-dialect shortcut and has moved off the Model public surface. Use `APDL(model).mat(mat_id)` for line-by-line APDL deck porting, or the native `Model.assign("HEX8", material, mat_id=...)` for new code. m.mat(1) .. GENERATED FROM PYTHON SOURCE LINES 32-34 Add nodes with ``n()`` ---------------------- .. GENERATED FROM PYTHON SOURCE LINES 34-45 .. code-block:: Python LX, LY, LZ = 4.0, 1.0, 1.0 NX = 4 nn = 1 node_id: dict[tuple[int, int, int], int] = {} for i in range(NX + 1): for j in range(2): for k in range(2): m.n(nn, i * LX / NX, j * LY, k * LZ) node_id[(i, j, k)] = nn nn += 1 .. rst-class:: sphx-glr-script-out .. code-block:: none /home/runner/_work/solver/solver/examples/pre-processing/example_build_mesh.py:41: DeprecationWarning: Model.n(...) is a MAPDL-dialect shortcut and has moved off the Model public surface. Use `APDL(model).n(num, x, y, z)` for line-by-line APDL deck porting, or the native `Model.from_grid(pv_grid)` for new code. m.n(nn, i * LX / NX, j * LY, k * LZ) .. GENERATED FROM PYTHON SOURCE LINES 46-48 Add elements with ``e()`` ------------------------- .. GENERATED FROM PYTHON SOURCE LINES 48-62 .. code-block:: Python for i in range(NX): m.e( node_id[(i, 0, 0)], node_id[(i + 1, 0, 0)], node_id[(i + 1, 1, 0)], node_id[(i, 1, 0)], node_id[(i, 0, 1)], node_id[(i + 1, 0, 1)], node_id[(i + 1, 1, 1)], node_id[(i, 1, 1)], ) print(f"model: {m.n_nodes} nodes, {m.n_elements} elements") .. rst-class:: sphx-glr-script-out .. code-block:: none /home/runner/_work/solver/solver/examples/pre-processing/example_build_mesh.py:49: DeprecationWarning: Model.e(...) is a MAPDL-dialect shortcut and has moved off the Model public surface. Use `APDL(model).e(*node_nums)` for line-by-line APDL deck porting, or the native `Model.from_grid(pv_grid)` for new code. m.e( model: 20 nodes, 4 elements .. GENERATED FROM PYTHON SOURCE LINES 63-65 Apply BCs and solve ------------------- .. GENERATED FROM PYTHON SOURCE LINES 65-84 .. code-block:: Python for j in range(2): for k in range(2): m.d(node_id[(0, j, k)], "ALL") for j in range(2): for k in range(2): m.f(node_id[(NX, j, k)], "FZ", -250.0) res = m.solve() dof_map = m.dof_map() tip_nodes = [node_id[(NX, j, k)] for j in range(2) for k in range(2)] tip_uz = np.array( [ res.displacement[int(np.where((dof_map[:, 0] == nn) & (dof_map[:, 1] == 2))[0][0])] for nn in tip_nodes ] ) print(f"tip UZ (mean over 4 corner nodes): {tip_uz.mean():.4e} m") .. rst-class:: sphx-glr-script-out .. code-block:: none /home/runner/_work/solver/solver/examples/pre-processing/example_build_mesh.py:67: DeprecationWarning: Model.d(...) is a MAPDL-dialect shortcut and has moved off the Model public surface. Use `APDL(model).d(node, label, value)` for line-by-line APDL deck porting, or the native `Model.fix(nodes=..., where=..., dof=...)` for new code. m.d(node_id[(0, j, k)], "ALL") /home/runner/_work/solver/solver/examples/pre-processing/example_build_mesh.py:71: DeprecationWarning: Model.f(...) is a MAPDL-dialect shortcut and has moved off the Model public surface. Use `APDL(model).f(node, label, value)` for line-by-line APDL deck porting, or the native `Model.apply_force(node, fx=..., fy=..., fz=...)` for new code. m.f(node_id[(NX, j, k)], "FZ", -250.0) tip UZ (mean over 4 corner nodes): -1.4761e-06 m .. GENERATED FROM PYTHON SOURCE LINES 85-87 Render ------ .. GENERATED FROM PYTHON SOURCE LINES 87-101 .. code-block:: Python grid = femorph_solver.io.static_result_to_grid(m, res) plotter = pv.Plotter(off_screen=True) plotter.add_mesh(grid, style="wireframe", color="gray", opacity=0.35, label="undeformed") plotter.add_mesh( grid.warp_by_vector("displacement", factor=500.0), scalars="displacement_magnitude", show_edges=True, cmap="viridis", scalar_bar_args={"title": "|u| [m]"}, label="deformed (×500)", ) plotter.add_legend() plotter.add_axes() plotter.show() .. image-sg:: /gallery/pre-processing/images/sphx_glr_example_build_mesh_001.png :alt: example build mesh :srcset: /gallery/pre-processing/images/sphx_glr_example_build_mesh_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.350 seconds) .. _sphx_glr_download_gallery_pre-processing_example_build_mesh.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: example_build_mesh.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: example_build_mesh.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: example_build_mesh.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_