.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/01_exploring_a_model.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_auto_examples_01_exploring_a_model.py: Exploring a Model ================= Load a model, poke at what is inside, extract a region, and build a stereonet from scratch. No config files. .. GENERATED FROM PYTHON SOURCE LINES 10-12 Setup ----- .. GENERATED FROM PYTHON SOURCE LINES 12-21 .. code-block:: Python import os import matplotlib.pyplot as plt import mplstereonet # noqa: F401 from fem2geo import Model, dir_testdata from fem2geo.plots import stereo_axes .. GENERATED FROM PYTHON SOURCE LINES 22-27 Loading a model --------------- ``Model.from_file`` takes a path and a schema name. The schema says which solver format the file has .. GENERATED FROM PYTHON SOURCE LINES 27-31 .. code-block:: Python path = os.path.join(dir_testdata, "reverse_fault.vtu") model = Model.from_file(path, schema="adeli3") .. GENERATED FROM PYTHON SOURCE LINES 32-35 What is inside -------------- .. GENERATED FROM PYTHON SOURCE LINES 35-42 .. code-block:: Python print("cells: ", model.n_cells) print("points: ", model.n_points) print("u: ", model.u.shape) print("stress: ", model.stress.shape) print("j2_stress: ", model.j2_stress.shape) .. rst-class:: sphx-glr-script-out .. code-block:: none cells: 52388 points: 9783 u: (9783, 3) stress: (52388, 3, 3) j2_stress: (52388,) .. GENERATED FROM PYTHON SOURCE LINES 43-46 ``model.stress`` is a plain numpy array of shape ``(n_cells, 3, 3)`` Principal directions come as unit vectors. Displacement is a vector per cell. All the model attributes are regular arrays. .. GENERATED FROM PYTHON SOURCE LINES 49-55 A horizontal slice of the whole model ------------------------------------- ``model.grid`` is the underlying PyVista dataset. A horizontal slice at y=5000, colored by vertical displacement, shows where the fault zone is located. .. GENERATED FROM PYTHON SOURCE LINES 55-65 .. code-block:: Python sl = model.grid.slice(normal="y", origin=(0, 5000, 0)) sl.plot( scalars="u", component=2, cmap="RdBu_r", cpos="xz", scalar_bar_args={"title": "u_z (m)"}, ) .. image-sg:: /auto_examples/images/sphx_glr_01_exploring_a_model_001.png :alt: 01 exploring a model :srcset: /auto_examples/images/sphx_glr_01_exploring_a_model_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 66-73 Extracting a region ------------------- Analyses don't use the whole model, but they are done on a site; e.g., a sphere around a point of interest. ``model.extract`` returns a new model restricted to the cells inside the sphere. It has the same interface as the original. .. GENERATED FROM PYTHON SOURCE LINES 73-77 .. code-block:: Python sub = model.extract(center=[8000, 5000, -2500], radius=300) print(f"site: {sub.n_cells} cells out of {model.n_cells}") .. rst-class:: sphx-glr-script-out .. code-block:: none site: 139 cells out of 52388 .. GENERATED FROM PYTHON SOURCE LINES 78-84 Average principal stress ------------------------ ``avg_principals`` returns the eigenvalues and eigenvectors of the volume-weighted average tensor inside the site. Eigenvalues come sorted ascending, so index 0 is the most compressive. .. GENERATED FROM PYTHON SOURCE LINES 84-90 .. code-block:: Python values, vectors = sub.avg_principals("stress") print("eigenvalues:", values) print("eigenvectors (columns = sigma_1, sigma_2, sigma_3):") print(vectors) .. rst-class:: sphx-glr-script-out .. code-block:: none eigenvalues: [-132.90893237 -89.62799877 -56.67045117] eigenvectors (columns = sigma_1, sigma_2, sigma_3): [[-0.97426241 -0.01602738 -0.22484633] [-0.01724908 0.9998452 0.00347008] [ 0.22475591 0.00725916 -0.97438806]] .. GENERATED FROM PYTHON SOURCE LINES 91-96 A stereonet ----------- Plot functions in :mod:`fem2geo.plots` all take a matplotlib axes as the first argument and draw into it. You own the figure. .. GENERATED FROM PYTHON SOURCE LINES 96-109 .. code-block:: Python fig = plt.figure(figsize=(5, 5)) ax = fig.add_subplot(111, projection="stereonet") ax.grid(True) stereo_axes( ax, vectors, style={"color": "red", "markersize": 10}, labels=(r"$\sigma_1$", r"$\sigma_2$", r"$\sigma_3$"), ) ax.legend() ax.set_title("Site 1 - principal stress", y=1.08) .. image-sg:: /auto_examples/images/sphx_glr_01_exploring_a_model_002.png :alt: Site 1 - principal stress :srcset: /auto_examples/images/sphx_glr_01_exploring_a_model_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Text(0.5, 1.08, 'Site 1 - principal stress') .. GENERATED FROM PYTHON SOURCE LINES 110-115 A second site, and a two-panel figure ------------------------------------- Extracting more sites is just a loop. Placing them in a custom layout is just matplotlib. .. GENERATED FROM PYTHON SOURCE LINES 115-137 .. code-block:: Python sites = [ model.extract(center=[8000, 5000, -2000], radius=300), model.extract(center=[8000, 5000, -4000], radius=300)] site_names = ["Site 1 (shallow)", "Site 2 (deep)"] fig, axes = plt.subplots( 1, 2, figsize=(12, 6), subplot_kw={"projection": "stereonet"}, ) for ax, site, site_name in zip(axes, sites, site_names): _, vecs = site.avg_principals("stress") ax.grid(True) stereo_axes( ax, vecs, style={"color": "red", "markersize": 10}, labels=(r"$\sigma_1$", r"$\sigma_2$", r"$\sigma_3$"), ) ax.set_title(site_name, y=1.08) ax.legend() .. image-sg:: /auto_examples/images/sphx_glr_01_exploring_a_model_003.png :alt: Site 1 (shallow), Site 2 (deep) :srcset: /auto_examples/images/sphx_glr_01_exploring_a_model_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 138-142 Other plots ----------- Here are histograms of stress J2 in the model vs. the sites .. GENERATED FROM PYTHON SOURCE LINES 142-160 .. code-block:: Python j2_model = model.j2_stress j2_site1 = sites[0].j2_stress j2_site2 = sites[1].j2_stress fig, ax = plt.subplots(figsize=(7, 4)) ax.hist(j2_model, bins="auto", color="steelblue", density=True, alpha=0.8, label="Model") ax.hist(j2_site1, bins="auto", color="red", density=True, alpha=0.8, label="Site 1") ax.hist(j2_site2, bins="auto", color="green", density=True, alpha=0.8, label="Site 2") ax.set_xlabel(r"$J_2$ (MPa)") ax.set_ylabel("Density distribution") ax.set_title(r"$J_2$ distribution") ax.legend() plt.tight_layout() .. image-sg:: /auto_examples/images/sphx_glr_01_exploring_a_model_004.png :alt: $J_2$ distribution :srcset: /auto_examples/images/sphx_glr_01_exploring_a_model_004.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 161-166 Saving the extracted site ------------------------- The extracted subset is also a model. Save it to ``.vtu`` and open it in ParaView to see which cells were picked. .. GENERATED FROM PYTHON SOURCE LINES 166-167 .. code-block:: Python sites[0].save("site_1.vtu") .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 2.275 seconds) .. _sphx_glr_download_auto_examples_01_exploring_a_model.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 01_exploring_a_model.ipynb <01_exploring_a_model.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 01_exploring_a_model.py <01_exploring_a_model.py>` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: 01_exploring_a_model.zip <01_exploring_a_model.zip>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_