Skip to content

Commit

Permalink
Merge pull request #19 from pyroteus/combine-metrics-demo
Browse files Browse the repository at this point in the history
Update website to add metric combination demo
  • Loading branch information
jwallwork23 authored Jan 12, 2024
2 parents 8fd186a + c2bc13f commit 36c4d8f
Show file tree
Hide file tree
Showing 54 changed files with 798 additions and 306 deletions.
Binary file modified _images/burgers-time_integrated.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added _images/combining_averaging.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added _images/combining_ellipse_intersection.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added _images/combining_intersection.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added _images/combining_two_metrics.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified _images/point_discharge2d-indicator.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions _modules/animate/metric.html
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ <h1>Source code for animate.metric</h1><div class="highlight"><pre>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">function_space</span><span class="p">,</span> <span class="n">fmesh</span><span class="o">.</span><span class="n">MeshGeometry</span><span class="p">):</span>
<span class="n">function_space</span> <span class="o">=</span> <span class="n">ffs</span><span class="o">.</span><span class="n">TensorFunctionSpace</span><span class="p">(</span><span class="n">function_space</span><span class="p">,</span> <span class="s2">&quot;CG&quot;</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">function_space</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">metric_parameters</span> <span class="o">=</span> <span class="p">{}</span>
<span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">function_space</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>

<span class="c1"># Check that we have an appropriate tensor P1 function</span>
<span class="n">fs</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">function_space</span><span class="p">()</span>
Expand Down Expand Up @@ -997,7 +997,7 @@ <h3>Navigation</h3>
</ul>
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2021-2023, Joseph G. Wallwork et al..
&#169; Copyright 2021-2024, Joseph G. Wallwork et al..
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
</div>
</body>
Expand Down
129 changes: 60 additions & 69 deletions _modules/goalie/adjoint.html

Large diffs are not rendered by default.

211 changes: 122 additions & 89 deletions _modules/goalie/go_mesh_seq.html

Large diffs are not rendered by default.

75 changes: 41 additions & 34 deletions _modules/goalie/mesh_seq.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion _modules/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ <h3>Navigation</h3>
</ul>
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2021-2023, Joseph G. Wallwork et al..
&#169; Copyright 2021-2024, Joseph G. Wallwork et al..
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
</div>
</body>
Expand Down
127 changes: 127 additions & 0 deletions _sources/demos/combining_metrics.py.rst.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
Intersecting and averaging metric fields
===========================================
As we saw in the `previous demo <./simple_metric.py.html>`__, the metric
tells the mesh adaptivity library what the desired edge lengths is in
different directions, in different parts of the domain. As we will see these
desired edge lengths are often based on an estimate of the discretisation
error we want to minimize for a certain solution field. Therefore, if in a
model we are solving for multiple fields, we may end up with multiple metric
fields telling us about the desired edge lengths for the discretisation of
each of these fields individually. If we want the model to employ a single
optimal mesh for the discretisation of all of these fields however, we need a
way to combine the metrics taking into account the different requirements for
the different solution fields. The same functionality is needed if in a
time-dependent model we have multiple metrics based on the solution at
different timesteps, and we want to adapt the mesh optimally for all of these
timesteps combined.

A natural way to combine two metrics for mesh adaptation, is the so called
intersection method. A natural, geometric interpretation exists if we
represent the individual metrics by ellipsoids, which indicate the desired
edge length in different directions through their principle axes. The
intersection of two metrics produces a metric whose associated ellipsoid is
the largest ellipsoid that can fit within the ellipsoids of the two metrics:

.. figure:: combining_ellipse_intersection.jpg
:figwidth: 70%
:align: center


Below we first set up two metrics: Metric 1 asks for a medium resolution of
:math:`hm=0.025` in the left, and a coarse resolution of :math:`hc=0.1` in
the right half of the domain. Metric 2 prescribes the coarse resolution for
:math:`y<0.3` and :math:`y>0.7`, but asks for an anisotropic resolution,
:math:`hc` in the :math:`x`-direction and :math:`hf=0.01` in the
:math:`y`-direction, for :math:`0.3<y<0.7`

::

import matplotlib.pyplot as plt
from firedrake import *
from animate import *

mesh = UnitSquareMesh(100, 100)
P1_ten = TensorFunctionSpace(mesh, "CG", 1)
metric1 = RiemannianMetric(P1_ten)
metric2 = RiemannianMetric(P1_ten)
x, y = SpatialCoordinate(mesh)
r = 0.2
hf = 0.01
hm = 0.025
hc = 0.1
anisotropic = as_matrix([[1 / hc**2, 0], [0, 1 / hf**2]])
medium = as_matrix([[1 / hm**2, 0], [0, 1 / hm**2]])
coarse = as_matrix([[1 / hc**2, 0], [0, 1 / hc**2]])
metric1.interpolate(conditional(x < 0.5, medium, coarse))
metric2.interpolate(
conditional(And(abs(x - 0.5) < r, abs(y - 0.5) < r), anisotropic, coarse)
)
metric2.interpolate(conditional(abs(y - 0.5) < r, anisotropic, coarse))
mesh1 = adapt(mesh, metric1)
mesh2 = adapt(mesh, metric2)
fig, axes = plt.subplots(figsize=(16, 8), ncols=2)
triplot(mesh1, axes=axes[0])
axes[0].set_aspect("equal")
axes[0].set_title("Mesh based on metric1")
triplot(mesh2, axes=axes[1])
axes[1].set_aspect("equal")
axes[1].set_title("Mesh based on metric2")
fig.show()
fig.savefig("combining_two_metrics.jpg")


.. figure:: combining_two_metrics.jpg
:figwidth: 90%
:align: center

We can intersect multiple metric by using the
:meth:`~.RiemannianMetric.intersect()` method of a
:class:`~.RiemannianMetric`, which intersects the given metric with another,
or multiple, provided metrics. We therefore first make a copy of metric1 and
then intersect it with metric2:

::

intersected_metric = metric1.copy(deepcopy=True)
intersected_metric.intersect(metric2)
mesh_intersected = adapt(mesh, intersected_metric)
fig, axes = plt.subplots(figsize=(16, 16))
triplot(mesh_intersected, axes=axes)
axes.set_aspect("equal")
axes.set_title("Mesh based on intersected metric")
fig.show()
fig.savefig("combining_intersection.jpg")


.. figure:: combining_intersection.jpg
:figwidth: 90%
:align: center

As we can observe, in every region the resolution respects the minimum
resolution in all directions asked for by both metrics. For example, in the
region :math:`x<0.5`, :math:`0.3<y<0.7` the resolution in the :math:`x`-direction is :math:`hf=0.01`, as
required by metric2, but in the :math:`y`-direction the resolution is :math:`hm=0.02` as
required by metric1.

If instead we average the metrics using the
:meth:`~.RiemannianMetric.average()` method,

::

averaged_metric = metric1.copy(deepcopy=True)
averaged_metric.average(metric2)
mesh_averaged = adapt(mesh, averaged_metric)
fig, axes = plt.subplots(figsize=(16, 16))
triplot(mesh_averaged, axes=axes)
axes.set_aspect("equal")
axes.set_title("Mesh based on averaged metric")
fig.show()
fig.savefig("combining_averaging.jpg")


.. figure:: combining_averaging.jpg
:figwidth: 90%
:align: center

the resolution in, e.g., the region :math:`x<0.5, y<0.3` is now based on an
average of :math:`hm=0.02` and :math:`hc=0.1`, i.e. an edge length of 0.06.
1 change: 1 addition & 0 deletions _sources/index.rst.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ source code for both packages is hosted on GitHub:
:maxdepth: 1

Defining a simple metric and adapting a mesh with respect to it <demos/simple_metric.py>
Combining metrics <demos/combining_metrics.py>

.. rubric:: Goalie demos

Expand Down
6 changes: 1 addition & 5 deletions _static/nature.css
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

/* -- page layout ----------------------------------------------------------- */


body {
font-family: Arial, sans-serif;
font-size: 100%;
Expand All @@ -29,7 +28,7 @@ div.documentwrapper {
}

div.bodywrapper {
margin: 0 0 0 400px; /*acse-ej321 : custom for sidebar*/
margin: 0 0 0 230px;
}

hr {
Expand Down Expand Up @@ -75,13 +74,10 @@ div.related a {
div.sphinxsidebar {
font-size: 0.75em;
line-height: 1.5em;
width: 400px; /*acse-ej321 : custom for sidebar*/
}

div.sphinxsidebarwrapper{
padding: 20px 0;
overflow: auto; /*acse-ej321 : custom for sidebar*/
width: 400px; /*acse-ej321 : custom for sidebar*/
}

div.sphinxsidebar h3,
Expand Down
2 changes: 1 addition & 1 deletion animate.html
Original file line number Diff line number Diff line change
Expand Up @@ -802,7 +802,7 @@ <h3>Navigation</h3>
</ul>
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2021-2023, Joseph G. Wallwork et al..
&#169; Copyright 2021-2024, Joseph G. Wallwork et al..
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
</div>
</body>
Expand Down
2 changes: 1 addition & 1 deletion animate/1-metric-based.html
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ <h3>Navigation</h3>
</ul>
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2021-2023, Joseph G. Wallwork et al..
&#169; Copyright 2021-2024, Joseph G. Wallwork et al..
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
</div>
</body>
Expand Down
2 changes: 1 addition & 1 deletion animate/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ <h3>Navigation</h3>
</ul>
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2021-2023, Joseph G. Wallwork et al..
&#169; Copyright 2021-2024, Joseph G. Wallwork et al..
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
</div>
</body>
Expand Down
2 changes: 1 addition & 1 deletion demos/burgers-hessian.py.html
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ <h3>Navigation</h3>
</ul>
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2021-2023, Joseph G. Wallwork et al..
&#169; Copyright 2021-2024, Joseph G. Wallwork et al..
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
</div>
</body>
Expand Down
Binary file modified demos/burgers-time_integrated.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion demos/burgers.py.html
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ <h3>Navigation</h3>
</ul>
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2021-2023, Joseph G. Wallwork et al..
&#169; Copyright 2021-2024, Joseph G. Wallwork et al..
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
</div>
</body>
Expand Down
2 changes: 1 addition & 1 deletion demos/burgers1.py.html
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ <h3>Navigation</h3>
</ul>
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2021-2023, Joseph G. Wallwork et al..
&#169; Copyright 2021-2024, Joseph G. Wallwork et al..
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
</div>
</body>
Expand Down
2 changes: 1 addition & 1 deletion demos/burgers2.py.html
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ <h3>Navigation</h3>
</ul>
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2021-2023, Joseph G. Wallwork et al..
&#169; Copyright 2021-2024, Joseph G. Wallwork et al..
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
</div>
</body>
Expand Down
2 changes: 1 addition & 1 deletion demos/burgers_ee.py.html
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ <h3>Navigation</h3>
</ul>
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2021-2023, Joseph G. Wallwork et al..
&#169; Copyright 2021-2024, Joseph G. Wallwork et al..
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
</div>
</body>
Expand Down
2 changes: 1 addition & 1 deletion demos/burgers_oo.py.html
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ <h3>Navigation</h3>
</ul>
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2021-2023, Joseph G. Wallwork et al..
&#169; Copyright 2021-2024, Joseph G. Wallwork et al..
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
</div>
</body>
Expand Down
2 changes: 1 addition & 1 deletion demos/burgers_time_integrated.py.html
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ <h3>Navigation</h3>
</ul>
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2021-2023, Joseph G. Wallwork et al..
&#169; Copyright 2021-2024, Joseph G. Wallwork et al..
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
</div>
</body>
Expand Down
Binary file added demos/combining_averaging.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added demos/combining_ellipse_intersection.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added demos/combining_intersection.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 36c4d8f

Please sign in to comment.