.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/howto_postprocessing/plot_convergence_study_steady_state_diffusion.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_howto_postprocessing_plot_convergence_study_steady_state_diffusion.py: Spatial refinement - steady state diffusion =========================================== This example shows one possible implementation of how to do a convergence study. It uses the project file from the following benchmark with multiple discretizations to evaluate the accuracy of the numerical solutions. `ogs: elliptic neumann benchmark `_ Here is some theoretical background for the topic of grid convergence: `Nasa convergence reference `_ `More comprehensive reference `_ At least three meshes of increasing refinement are required for the convergence study. The three finest meshes are used to calculated the Richardson extrapolation. The third coarsest mesh will be used for the topology to evaluate the results. Its nodes should be shared by the finer meshes, otherwise interpolation will influence the results. With unstructured grids this can be achieved as well with refinement by splitting. The results to analyze are generated on the fly with the following code. If you are only interested in the convergence study, please skip to `Hydraulic pressure convergence`_. First, the required packages are imported and an output directory is created: .. GENERATED FROM PYTHON SOURCE LINES 34-47 .. code-block:: Python from pathlib import Path from tempfile import mkdtemp from IPython.display import HTML import ogstools as ogs from ogstools import examples, msh2vtu, variables, workflow from ogstools.studies import convergence temp_dir = Path(mkdtemp(suffix="steady_state_diffusion")) report_name = str(temp_dir / "report.ipynb") result_paths = [] .. GENERATED FROM PYTHON SOURCE LINES 48-52 The meshes and their boundaries are generated easily via gmsh and :py:mod:`ogstools.msh2vtu`. Then we run the different simulations with increasingly fine spatial discretization via ogs6py and store the results for the convergence study. .. GENERATED FROM PYTHON SOURCE LINES 54-74 .. code-block:: Python refinements = 6 edge_cells = [2**i for i in range(refinements)] for n_edge_cells in edge_cells: msh_path = temp_dir / "square.msh" ogs.meshlib.gmsh_meshing.rect( n_edge_cells=n_edge_cells, structured_grid=True, out_name=msh_path ) msh2vtu.msh2vtu(filename=msh_path, output_path=temp_dir, log_level="ERROR") model = ogs.Project( output_file=temp_dir / "default.prj", input_file=examples.prj_steady_state_diffusion, ) prefix = "steady_state_diffusion_" + str(n_edge_cells) model.replace_text(prefix, ".//prefix") model.write_input() ogs_args = f"-m {temp_dir} -o {temp_dir}" model.run_model(write_logs=False, args=ogs_args) result_paths += [str(temp_dir / (prefix + ".pvd"))] .. rst-class:: sphx-glr-script-out .. code-block:: none OGS finished with project file /tmp/tmplai5ja_2steady_state_diffusion/default.prj. Execution took 0.11521482467651367 s Project file written to output. OGS finished with project file /tmp/tmplai5ja_2steady_state_diffusion/default.prj. Execution took 0.11556768417358398 s Project file written to output. OGS finished with project file /tmp/tmplai5ja_2steady_state_diffusion/default.prj. Execution took 0.1143960952758789 s Project file written to output. OGS finished with project file /tmp/tmplai5ja_2steady_state_diffusion/default.prj. Execution took 0.11415266990661621 s Project file written to output. OGS finished with project file /tmp/tmplai5ja_2steady_state_diffusion/default.prj. Execution took 0.11604905128479004 s Project file written to output. OGS finished with project file /tmp/tmplai5ja_2steady_state_diffusion/default.prj. Execution took 0.12266898155212402 s Project file written to output. .. GENERATED FROM PYTHON SOURCE LINES 75-76 Here we calculate the analytical solution on one of the meshes: .. GENERATED FROM PYTHON SOURCE LINES 78-86 .. code-block:: Python analytical_solution_path = temp_dir / "analytical_solution.vtu" solution = examples.analytical_diffusion( ogs.MeshSeries(result_paths[-1]).mesh(0) ) ogs.plot.setup.show_element_edges = True fig = ogs.plot.contourf(solution, variables.hydraulic_head) solution.save(analytical_solution_path) .. image-sg:: /auto_examples/howto_postprocessing/images/sphx_glr_plot_convergence_study_steady_state_diffusion_001.png :alt: plot convergence study steady state diffusion :srcset: /auto_examples/howto_postprocessing/images/sphx_glr_plot_convergence_study_steady_state_diffusion_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 87-93 Hydraulic pressure convergence ------------------------------ The pressure field of this model is converging well. The convergence ratio is approximately 1 on the whole mesh and looking at the relative errors we see a quadratic convergence behavior. .. GENERATED FROM PYTHON SOURCE LINES 95-105 .. code-block:: Python convergence.run_convergence_study( output_name=report_name, mesh_paths=result_paths, variable_name="hydraulic_head", timevalue=1, refinement_ratio=2.0, reference_solution_path=str(analytical_solution_path), ) HTML(workflow.jupyter_to_html(report_name, show_input=False)) .. raw:: html
report


.. GENERATED FROM PYTHON SOURCE LINES 106-120 Darcy velocity convergence -------------------------- For the velocity we some discrepancy of the convergence ratio in the bottom right corner. Thus we know, at these points the mesh isn't properly converging (at least for the velocity field). We see, that in the bottom right corner, the velocity magnitude seems to be steadily increasing, which is also reflected in the Richardson extrapolation, which shows an anomalous high value in this spot, hinting at a singularity there. This is explained by the notion in the benchmark's documentation of "incompatible boundary conditions imposed on the bottom right corner of the domain." Regardless of this, the benchmark gives a convergent solution for the pressure field. The code cells from the templated notebook are show here for transparency. .. GENERATED FROM PYTHON SOURCE LINES 122-131 .. code-block:: Python convergence.run_convergence_study( output_name=report_name, mesh_paths=result_paths, variable_name="velocity", timevalue=1, refinement_ratio=2.0, ) HTML(workflow.jupyter_to_html(report_name, show_input=True)) .. raw:: html
report


.. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 15.696 seconds) .. _sphx_glr_download_auto_examples_howto_postprocessing_plot_convergence_study_steady_state_diffusion.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_convergence_study_steady_state_diffusion.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_convergence_study_steady_state_diffusion.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_convergence_study_steady_state_diffusion.zip `