discretize.tests.OrderTest#

Inheritance diagram of OrderTest
class discretize.tests.OrderTest(methodName='runTest')[source]#

Base class for testing convergence of discrete operators with respect to cell size.

OrderTest is a base class for testing the order of convergence of discrete operators with respect to cell size. OrderTest is inherited by the test class for the given operator. Within the test class, the user sets the parameters for the convergence testing and defines a method getError which defines the error as a norm of the residual (see example).

OrderTest inherits from unittest.TestCase.

Parameters:
namestr

Name the convergence test

meshTypeslist of str

List denoting the mesh types on which the convergence will be tested. List entries must of the list {‘uniformTensorMesh’, ‘randomTensorMesh’, ‘uniformCylindricalMesh’, ‘randomCylindricalMesh’, ‘uniformTree’, ‘randomTree’, ‘uniformCurv’, ‘rotateCurv’, ‘sphereCurv’}

expectedOrdersfloat or list of float (default = 2.0)

Defines the expect orders of convergence for all meshes defined in meshTypes. If list, must have same length as argument meshTypes.

tolerancefloat or list of float (default = 0.85)

Defines tolerance for numerical approximate of convergence order. If list, must have same length as argument meshTypes.

meshSizeslist of int

From coarsest to finest, defines the number of mesh cells in each axis direction for the meshes used in the convergence test; e.g. [4, 8, 16, 32]

meshDimensionint

Mesh dimension. Must be 1, 2 or 3

Notes

Consider an operator \(A(f)\) that acts on a test function \(f\). And let \(A_h (f)\) be the discrete approximation to the original operator constructed on a mesh will cell size \(h\). OrderTest assesses the convergence of

\[error(h) = \| A_h(f) - A(f) \|\]

as \(h \rightarrow 0\). Note that you can provide any norm to quantify the error. The convergence test is passed when the numerically estimated rate of convergence is within a specified tolerance of the expected convergence rate supplied by the user.

Examples

Here, we utilize the OrderTest class to validate the rate of convergence for the face_divergence. Our convergence test is done for a uniform 2D tensor mesh. Under the test class TestDIV2D, we define the static parameters for the order test. We then define a method getError for this class which returns the norm of some residual. With these two pieces defined, we can call the order test as shown below.

>>> from discretize.tests import OrderTest
>>> import unittest
>>> import numpy as np
>>> class TestDIV2D(OrderTest):
...     # Static properties for OrderTest
...     name = "Face Divergence 2D"
...     meshTypes = ["uniformTensorMesh"]
...     meshDimension = 2
...     expectedOrders = 2.0
...     tolerance = 0.85
...     meshSizes = [8, 16, 32, 64]
...     def getError(self):
...         # Test function
...         fx = lambda x, y: np.sin(2 * np.pi * x)
...         fy = lambda x, y: np.sin(2 * np.pi * y)
...         # Analytic solution for operator acting on test function
...         sol = lambda x, y: 2 * np.pi * (np.cos(2 * np.pi * x) + np.cos(2 * np.pi * y))
...         # Evaluate test function on faces
...         f = np.r_[
...             fx(self.M.faces_x[:, 0], self.M.faces_x[:, 1]),
...             fy(self.M.faces_y[:, 0], self.M.faces_y[:, 1])
...         ]
...         # Analytic solution at cell centers
...         div_f = sol(self.M.cell_centers[:, 0], self.M.cell_centers[:, 1])
...         # Numerical approximation of divergence at cell centers
...         div_f_num = self.M.face_divergence * f
...         # Define the error function as a norm
...         err = np.linalg.norm((div_f_num - div_f), np.inf)
...         return err
...     def test_order(self):
...         self.orderTest()

Methods

getError()

Compute error defined as a norm of the residual.

orderTest()

Perform an order test.

setupMesh(nC)

Generate mesh and set as current mesh for testing.