diff --git a/tutorials/tutorial-6-k.py b/tutorials/tutorial-6-k.py new file mode 100755 index 0000000..0be7bef --- /dev/null +++ b/tutorials/tutorial-6-k.py @@ -0,0 +1,17 @@ +import pygem as pg +from pygem.khandler import KHandler + +ffd = pg.FFD() +ffd.read_parameters('../tests/test_datasets/parameters_test_ffd_pipe_unv_C0.prm') + +handler = KHandler() +mesh_points = handler.parse('../tests/test_datasets/test_pipe.k') + +new_mesh_points = ffd(mesh_points) + +ffd = pg.FFD() +ffd.read_parameters('../tests/test_datasets/parameters_test_ffd_pipe_unv_C1.prm') + +new_mesh_points = ffd(mesh_points) + +handler.write(new_mesh_points, 'test_pipe_mod_C1.k') diff --git a/tutorials/tutorial-7-mdpa.ipynb b/tutorials/tutorial-7-mdpa.ipynb index d31d056..9eb6bf2 100644 --- a/tutorials/tutorial-7-mdpa.ipynb +++ b/tutorials/tutorial-7-mdpa.ipynb @@ -11,48 +11,52 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "###### Tutorial 6: Free Form Deformation on a k mesh on cylinder: continuity of the deformation" + "###### Tutorial 7: Free Form Deformation on a Kratos mdpa mesh on cylinder: continuity of the deformation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "In this tutorial we show how to perform the Free Form Deformation on a hexaedral mesh of a cylinder. As seen in Tutorial 3, but with a LS-Dyna k file.\n", + "In this tutorial we show how to perform the Free Form Deformation on a hexaedral mesh of a cylinder. As seen in Tutorial 3, but with a Kratos mdpa file.\n", + "\n", + "As PyGeM cannot visualize mdpa files directly, meshio and vedo are needed to convert the file to a vtk file and then plot it. The library vedo allows for interactive plot within Jupyter. See below. These can be installed with pip install vedo meshio.\n", "\n", "First of all we just import the required PyGeM classes and we read a parameters file." ] }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 8, "metadata": {}, "outputs": [], "source": [ - "from pygem import FFD\n", - "from pygem.mdpahandler import MdpaHandler" + "import pygem as pg\n", + "from pygem.mdpahandler import MdpaHandler\n", + "import vedo\n", + "import meshio" ] }, { - "cell_type": "code", - "execution_count": 14, + "cell_type": "markdown", "metadata": {}, - "outputs": [], "source": [ - "ffd = FFD()\n", - "ffd.read_parameters(filename='../tests/test_datasets/parameters_test_ffd_pipe_unv_C0.prm')" + "The following is the parameters file for the case at hand. In particular, if you look at the Box info section, there is 2-by-2-by-3 lattice around a cylinder. Since we want to shift the middle section of the cylinder along the x direction we modify only the parameter x weights corresponding to the middle point in z direction (index==1). In the following we show only the important parts of the parameters file" ] }, { - "cell_type": "markdown", + "cell_type": "code", + "execution_count": 9, "metadata": {}, + "outputs": [], "source": [ - "The following is the parameters file for the case at hand. In particular, if you look at the Box info section, there is 2-by-2-by-3 lattice around a cylinder. Since we want to shift the middle section of the cylinder along the x direction we modify only the parameter x weights corresponding to the middle point in z direction (index==1). In the following we show only the important parts of the parameters file" + "ffd = pg.FFD()\n", + "ffd.read_parameters('../tests/test_datasets/parameters_test_ffd_pipe_unv_C0.prm')" ] }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 10, "metadata": {}, "outputs": [ { @@ -159,48 +163,64 @@ "collapsed": true }, "source": [ - "We now load the k file!" + "We now load the mdpa file!" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, + "outputs": [], + "source": [ + "mdpa_handler = MdpaHandler()\n", + "mesh_points = mdpa_handler.parse('../tests/test_datasets/test_pipe.mdpa')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "and visualize the undeformed mesh by tranlating with meshio from MPDA to VTK format." + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "[[ 1.00000000e+00 -1.22464675e-16 0.00000000e+00]\n", - " [ 1.00000000e+00 -1.22464675e-16 1.00000000e+01]\n", - " [ 6.12323400e-17 1.00000000e+00 0.00000000e+00]\n", - " ...\n", - " [-9.19198149e-01 1.94291324e-01 9.90000000e+00]\n", - " [-9.53327549e-01 9.77311899e-02 9.90000000e+00]\n", - " [-9.78719220e-01 -2.06085149e-15 9.90000000e+00]]\n" - ] + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "2ddd63dcb9bd4991aca20282974a2d7f", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "Plot(antialias=3, axes=['x', 'y', 'z'], axes_helper=1.0, background_color=16777215, camera=[5.469634129164876,…" + ] + }, + "metadata": {}, + "output_type": "display_data" } ], "source": [ - "mdpa_handler = MdpaHandler()\n", - "mesh_points = mdpa_handler.parse('../tests/test_datasets/test_pipe.mdpa')\n", - "print(mesh_points)" + "UndeformedMDPA = meshio.read('../tests/test_datasets/test_pipe.mdpa')\n", + "meshio.write('test_pipe_undeformed_mdpa.vtk', UndeformedMDPA)\n", + "UndeformedVTK = vedo.load('test_pipe_undeformed_mdpa.vtk')\n", + "vedo.settings.embedWindow(backend='k3d', verbose=True)\n", + "vedo.show(UndeformedVTK, viewup=\"z\", resetcam=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "and visualize the undeformed mesh.\n", - "\n", - "![](pictures/cylinder_orig_unv.png)\n", - "\n", - "We now, as always, perform the FFD and write out the results in a new k file." + "We now perform the FFD and write out the results with a new mdpa file." ] }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 18, "metadata": {}, "outputs": [], "source": [ @@ -212,10 +232,41 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Let us see the result.\n", - "\n", - "![](pictures/cylinder_mod_unv_C0.png)\n", - "\n", + "Let us see the result." + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "07a27a24f4b340679146689d9b14896c", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "Plot(antialias=3, axes=['x', 'y', 'z'], axes_helper=1.0, background_color=16777215, camera=[6.017318527774512,…" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "DeformedMDPA = meshio.read('test_pipe_mod_C0.mdpa')\n", + "meshio.write('test_pipe_deformed_mdpa.vtk', DeformedMDPA)\n", + "DeformedVTK = vedo.load('test_pipe_deformed_mdpa.vtk')\n", + "vedo.settings.embedWindow(backend='k3d', verbose=True)\n", + "vedo.show(DeformedVTK, viewup=\"z\", resetcam=True)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ "As you can easily see the deformation makes the mesh only C0 continuous.\n", "\n", "This is not wrong a priori, but it can have some drawbacks.\n", @@ -231,127 +282,9 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\r\n", - "[Box info]\r\n", - "# This section collects all the properties of the FFD bounding box.\r\n", - "\r\n", - "# n control points indicates the number of control points in each direction (x, y, z).\r\n", - "# For example, to create a 2 x 3 x 2 grid, use the following: n control points: 2, 3, 2\r\n", - "n control points x: 2\r\n", - "n control points y: 2\r\n", - "n control points z: 5\r\n", - "\r\n", - "# box length indicates the length of the FFD bounding box along the three canonical directions (x, y, z).\r\n", - "# It uses the local coordinate system.\r\n", - "# For example to create a 2 x 1.5 x 3 meters box use the following: box length: 2.0, 1.5, 3.0\r\n", - "box length x: 2.2\r\n", - "box length y: 2.2\r\n", - "box length z: 6.0\r\n", - "\r\n", - "# box origin indicates the x, y, and z coordinates of the origin of the FFD bounding box. That is center of\r\n", - "# rotation of the bounding box. It corresponds to the point coordinates with position [0][0][0].\r\n", - "# See section \"Parameters weights\" for more details.\r\n", - "# For example, if the origin is equal to 0., 0., 0., use the following: box origin: 0., 0., 0.\r\n", - "box origin x: -1.1\r\n", - "box origin y: -1.1\r\n", - "box origin z: 2.0\r\n", - "\r\n", - "# rotation angle indicates the rotation angle around the x, y, and z axis of the FFD bounding box in degrees.\r\n", - "# The rotation is done with respect to the box origin.\r\n", - "# For example, to rotate the box by 2 deg along the z direction, use the following: rotation angle: 0., 0., 2.\r\n", - "rotation angle x: 0.0\r\n", - "rotation angle y: 0.0\r\n", - "rotation angle z: 0.0\r\n", - "\r\n", - "\r\n", - "[Parameters weights]\r\n", - "# This section describes the weights of the FFD control points.\r\n", - "# We adopt the following convention:\r\n", - "# For example with a 2x2x2 grid of control points we have to fill a 2x2x2 matrix of weights.\r\n", - "# If a weight is equal to zero you can discard the line since the default is zero.\r\n", - "#\r\n", - "# | x index | y index | z index | weight |\r\n", - "# --------------------------------------\r\n", - "# | 0 | 0 | 0 | 1.0 |\r\n", - "# | 0 | 1 | 1 | 0.0 | --> you can erase this line without effects\r\n", - "# | 0 | 1 | 0 | -2.1 |\r\n", - "# | 0 | 0 | 1 | 3.4 |\r\n", - "\r\n", - "# parameter x collects the displacements along x, normalized with the box length x.\r\n", - "parameter x: 0 0 0 0.0\r\n", - " 0 0 1 0.0\r\n", - " 0 0 2 0.8\r\n", - " 0 0 3 0.0\r\n", - " 0 0 4 0.0\r\n", - " 0 1 0 0.0\r\n", - " 0 1 1 0.0\r\n", - " 0 1 2 0.8\r\n", - " 0 1 3 0.0\r\n", - " 0 1 4 0.0\r\n", - " 1 0 0 0.0\r\n", - " 1 0 1 0.0\r\n", - " 1 0 2 0.8\r\n", - " 1 0 3 0.0\r\n", - " 1 0 4 0.0\r\n", - " 1 1 0 0.0\r\n", - " 1 1 1 0.0\r\n", - " 1 1 2 0.8\r\n", - " 1 1 3 0.0\r\n", - " 1 1 4 0.0\r\n", - "\r\n", - "# parameter y collects the displacements along y, normalized with the box length y.\r\n", - "parameter y: 0 0 0 0.0\r\n", - " 0 0 1 0.0\r\n", - " 0 0 2 0.0\r\n", - " 0 0 3 0.0\r\n", - " 0 0 4 0.0\r\n", - " 0 1 0 0.0\r\n", - " 0 1 1 0.0\r\n", - " 0 1 2 0.0\r\n", - " 0 1 3 0.0\r\n", - " 0 1 4 0.0\r\n", - " 1 0 0 0.0\r\n", - " 1 0 1 0.0\r\n", - " 1 0 2 0.0\r\n", - " 1 0 3 0.0\r\n", - " 1 0 4 0.0\r\n", - " 1 1 0 0.0\r\n", - " 1 1 1 0.0\r\n", - " 1 1 2 0.0\r\n", - " 1 1 3 0.0\r\n", - " 1 1 4 0.0\r\n", - "\r\n", - "# parameter z collects the displacements along z, normalized with the box length z.\r\n", - "parameter z: 0 0 0 0.0\r\n", - " 0 0 1 0.0\r\n", - " 0 0 2 0.0\r\n", - " 0 0 3 0.0\r\n", - " 0 0 4 0.0\r\n", - " 0 1 0 0.0\r\n", - " 0 1 1 0.0\r\n", - " 0 1 2 0.0\r\n", - " 0 1 3 0.0\r\n", - " 0 1 4 0.0\r\n", - " 1 0 0 0.0\r\n", - " 1 0 1 0.0\r\n", - " 1 0 2 0.0\r\n", - " 1 0 3 0.0\r\n", - " 1 0 4 0.0\r\n", - " 1 1 0 0.0\r\n", - " 1 1 1 0.0\r\n", - " 1 1 2 0.0\r\n", - " 1 1 3 0.0\r\n", - " 1 1 4 0.0\r\n" - ] - } - ], + "outputs": [], "source": [ "%cat ../tests/test_datasets/parameters_test_ffd_pipe_unv_C1.prm" ] @@ -360,31 +293,56 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "We again load the new parameter file, perform the FFD and write out the results with a new k file." + "We again load the new parameter file, perform the FFD and write out the results with a new mdpa file." ] }, { "cell_type": "code", - "execution_count": 22, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ - "ffd = FFD()\n", - "ffd.read_parameters(filename='../tests/test_datasets/parameters_test_ffd_pipe_unv_C1.prm')\n", + "ffd = pg.FFD()\n", + "ffd.read_parameters('../tests/test_datasets/parameters_test_ffd_pipe_unv_C1.prm')\n", "new_mesh_points = ffd(mesh_points)\n", - "\n", - "mdpa_handler.write(new_mesh_points, 'test_pipe_mod_C0.mdpa')" + "mdpa_handler.write(new_mesh_points, 'test_pipe_mod_C1.mdpa')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "And here it is the C1 mesh on the cylinder.\n", - "\n", - "![](pictures/cylinder_mod_unv_C1.png)\n", - "\n", - "You can add some other points to increase again the continuity of the mesh!" + "And here it is the C1 mesh on the cylinder." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "mesh = meshio.read('test_pipe_mod_C1.mdpa')\n", + "meshio.write('test_pipe_mod_C1_mdpa.vtk', mesh)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "DeformedMDPA = meshio.read('test_pipe_mod_C1.mdpa')\n", + "meshio.write('test_pipe_deformed_mdpa.vtk', DeformedMDPA)\n", + "DeformedVTK = vedo.load('test_pipe_deformed_mdpa.vtk')\n", + "vedo.settings.embedWindow(backend='k3d', verbose=True)\n", + "vedo.show(DeformedVTK, viewup=\"z\", resetcam=True)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "You can add further points to increase again the continuity of the mesh!" ] } ], @@ -404,7 +362,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.8" + "version": "3.8.5" }, "latex_envs": { "LaTeX_envs_menu_present": true, diff --git a/tutorials/tutorial-7-mdpa.py b/tutorials/tutorial-7-mdpa.py new file mode 100755 index 0000000..f035539 --- /dev/null +++ b/tutorials/tutorial-7-mdpa.py @@ -0,0 +1,38 @@ +import pygem as pg +from pygem.mdpahandler import MdpaHandler +import vedo +import meshio + +ffd = pg.FFD() +ffd.read_parameters('../tests/test_datasets/parameters_test_ffd_pipe_unv_C0.prm') + +mdpa_handler = MdpaHandler() +mesh_points = mdpa_handler.parse('../tests/test_datasets/test_pipe.mdpa') + +UndeformedMDPA = meshio.read('../tests/test_datasets/test_pipe.mdpa') +meshio.write('test_pipe_undeformed_mdpa.vtk', UndeformedMDPA) +UndeformedVTK = vedo.load('test_pipe_undeformed_mdpa.vtk') +vedo.settings.embedWindow(backend='k3d', verbose=True) +vedo.show(UndeformedVTK, viewup="z", resetcam=True) + +new_mesh_points = ffd(mesh_points) + +DeformedMDPA = meshio.read('test_pipe_mod_C0.mdpa') +meshio.write('test_pipe_deformed_mdpa.vtk', DeformedMDPA) +DeformedVTK = vedo.load('test_pipe_deformed_mdpa.vtk') +vedo.settings.embedWindow(backend='k3d', verbose=True) +vedo.show(DeformedVTK, viewup="z", resetcam=True) + +ffd = pg.FFD() +ffd.read_parameters('../tests/test_datasets/parameters_test_ffd_pipe_unv_C1.prm') +new_mesh_points = ffd(mesh_points) +mdpa_handler.write(new_mesh_points, 'test_pipe_mod_C1.mdpa') + +mesh = meshio.read('test_pipe_mod_C1.mdpa') +meshio.write('test_pipe_mod_C1_mdpa.vtk', mesh) + +DeformedMDPA = meshio.read('test_pipe_mod_C1.mdpa') +meshio.write('test_pipe_deformed_mdpa.vtk', DeformedMDPA) +DeformedVTK = vedo.load('test_pipe_deformed_mdpa.vtk') +vedo.settings.embedWindow(backend='k3d', verbose=True) +vedo.show(DeformedVTK, viewup="z", resetcam=True)