Repository navigation
Create a public API #194
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Create a public API #194
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
eedbfbf
Add an explicit public API
FrederikSchnack 9ec5073
Adopt explicit internal imports
FrederikSchnack 7c22e57
Replace legacy tutorials with executable examples
FrederikSchnack cdece53
Document the public API and examples
FrederikSchnack d418e0f
Document public API changes
FrederikSchnack 0d0b1fe
Import from defining modules in the unit tests
FrederikSchnack 6e83591
Keep __version__ in the top-level package
FrederikSchnack d55c546
Clarify the wording in the quickstart guide
FrederikSchnack 1e85500
State the complex-square mapping formula explicitly
FrederikSchnack 76b8bc6
Inline the integration helper in the nonlinear example
FrederikSchnack a0b8d1e
Clarify the plot test for unmapped domains
FrederikSchnack 67ff19a
Say "circular dependency" in the lazy-import comment
FrederikSchnack 306674a
Reference PR #194 in the changelog
FrederikSchnack 2c2aa9d
Restrict sympde.api to the objects used downstream
FrederikSchnack 5807f07
Remove unused expression-tree images
FrederikSchnack 65271a0
Rename the "Nonlinear linearization" example heading
FrederikSchnack d27b7de
Draw expression trees in the documentation
FrederikSchnack 23f35db
Remove a duplicate import in test_logical_expr
FrederikSchnack 13f9ce1
Use sympde.api in the calculus docstring examples
FrederikSchnack 38c3b10
Use defining-module paths in the plotting docstrings
FrederikSchnack 74c5b2b
Record the removal of the notebooks in the changelog
FrederikSchnack d2ec86d
Make module-level names used outside their module public
FrederikSchnack 9db4bd0
Update CHANGELOG.md
FrederikSchnack 3b2a584
Update CHANGELOG.md
FrederikSchnack 3ce21d0
Update CHANGELOG.md
FrederikSchnack 0d3efd0
Update CHANGELOG.md
FrederikSchnack 309b06e
Draw the expression trees without writing files
FrederikSchnack File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,39 +1,34 @@ | ||
| Advanced expression manipulation | ||
| ******************************** | ||
|
|
||
| In this section, we shall present different different features for manipulating expressions such us finding the Kronecker representation for bilinear forms. | ||
| Tensor-product representation | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
|
||
| Tensorization | ||
| ^^^^^^^^^^^^^ | ||
| ``TensorExpr`` lowers an integrated form and expresses it through one- | ||
| dimensional symbolic factors. This is useful to inspect the Kronecker | ||
| structure consumed by tensor-product discretizations. | ||
|
|
||
| Assume we have the following bilinear form for Laplace | ||
| .. literalinclude:: examples/tensorization.py | ||
| :language: python | ||
|
|
||
| .. code-block:: python | ||
| The example is a complete script, which the documentation workflow runs | ||
| before Sphinx builds the page. | ||
|
|
||
| from sympde import grad, dot | ||
| from sympde import FunctionSpace | ||
| from sympde import TestFunction | ||
| from sympde import BilinearForm | ||
| from sympde.core import tensorize | ||
| Logical expressions | ||
| ^^^^^^^^^^^^^^^^^^^ | ||
|
|
||
| V = FunctionSpace('V', ldim=2) | ||
| U = FunctionSpace('U', ldim=2) | ||
| ``LogicalExpr`` pulls expressions on a mapped physical domain back to its | ||
| logical domain. The analytical-mapping example demonstrates symbolic and | ||
| numerical use of this transformation: | ||
|
|
||
| v = TestFunction(V, name='v') | ||
| u = TestFunction(U, name='u') | ||
| .. literalinclude:: examples/analytical_mappings.py | ||
| :language: python | ||
|
|
||
| a = BilinearForm((v,u), dot(grad(v), grad(u)) + v*u) | ||
| print(tensorize(a)) | ||
| Nonlinear forms | ||
| ^^^^^^^^^^^^^^^ | ||
|
|
||
| the result is then | ||
| ``linearize`` differentiates a nonlinear residual with respect to a field and | ||
| returns its symbolic Jacobian form: | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| >>> Mass(v1,u1)xMass(v0,u0) + Mass(v1,u1)xStiffness(v0,u0) + Stiffness(v1,u1)xMass(v0,u0) | ||
|
|
||
| which is what we expect, when splitting the integrals with respect to the two directions. | ||
| Printing the corresponding latex code gives | ||
|
|
||
| .. math:: | ||
|
|
||
| {\int_{0}^{1} u_{1} v_{1} dy}\otimes {\int_{0}^{1} u_{0} v_{0} dx} + {\int_{0}^{1} u_{1} v_{1} dy}\otimes {\int_{0}^{1} u_{0}^\prime v_{0}^\prime dx} + {\int_{0}^{1} u_{1}^\prime v_{1}^\prime dy}\otimes {\int_{0}^{1} u_{0} v_{0} dx} | ||
| .. literalinclude:: examples/nonlinear_poisson.py | ||
| :language: python |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| Public API | ||
| ========== | ||
|
|
||
| The :mod:`sympde.api` package provides the supported flat namespace for | ||
| user-facing objects. For example: | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| from sympde.api import Cube, Mapping, Constant | ||
| from sympde.api import ScalarFunctionSpace, elements_of | ||
| from sympde.api import BilinearForm, LinearForm, Norm | ||
|
|
||
| The complete namespace is listed below. These objects are also documented and | ||
| indexed at their defining modules, so this page does not create duplicate | ||
| cross-reference targets. | ||
|
|
||
| .. automodule:: sympde.api | ||
| :members: | ||
| :no-index: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| """Work with built-in and user-defined analytical mappings.""" | ||
|
|
||
| import numpy as np | ||
| from sympy import pi, sin, symbols | ||
|
|
||
| from sympde.api import CollelaMapping2D, Constant, Domain, LogicalExpr, Mapping | ||
|
|
||
|
|
||
| # Inspect a built-in mapping symbolically. | ||
| mapping = CollelaMapping2D('M', dim=2) | ||
| mapped_domain = mapping(Domain('Omega', dim=2)) | ||
| x1, x2 = symbols('x1, x2', real=True) | ||
| eps, k1, k2 = [Constant(name) for name in ('eps', 'k1', 'k2')] | ||
|
|
||
| expected_x = 2.0 * ( | ||
| x1 + eps * sin(2.0 * pi * k1 * x1) * sin(2.0 * pi * k2 * x2) | ||
| ) - 1.0 | ||
| assert LogicalExpr(mapping[0], mapped_domain) == expected_x | ||
|
|
||
|
|
||
| # Fix its parameters and evaluate it numerically. | ||
| numerical_mapping = CollelaMapping2D( | ||
| 'M_numeric', | ||
| dim=2, | ||
| eps=0.1, | ||
| k1=1.0, | ||
| k2=1.0, | ||
| ).get_callable_mapping() | ||
| np.testing.assert_allclose(numerical_mapping(0.25, 0.5), (-0.5, 0.0)) | ||
|
|
||
|
|
||
| # Define a custom mapping by specifying its coordinate expressions. | ||
| class ComplexSquareMapping(Mapping): | ||
| """ | ||
| Map (x1, x2) to (x, y) such that x + i*y = A * (x1 + i*x2)**2. | ||
|
|
||
| Expanding the square gives x = A * (x1**2 - x2**2) and y = 2 * A * x1 * x2. | ||
| """ | ||
|
|
||
| _expressions = { | ||
| 'x': 'A * (x1**2 - x2**2)', | ||
| 'y': '2 * A * x1 * x2', | ||
| } | ||
|
|
||
|
|
||
| custom_mapping = ComplexSquareMapping('F', dim=2, A=1.0).get_callable_mapping() | ||
| np.testing.assert_allclose(custom_mapping(0.5, 0.25), (0.1875, 0.25)) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| """Verify representative symbolic vector-calculus identities.""" | ||
|
|
||
| from sympy import expand | ||
|
|
||
| from sympde.api import Constant, Domain, ScalarFunctionSpace, VectorFunctionSpace | ||
| from sympde.api import curl, div, elements_of, grad, laplace | ||
|
|
||
|
|
||
| domain = Domain('Omega', dim=3) | ||
| scalar_space = ScalarFunctionSpace('V', domain) | ||
| vector_space = VectorFunctionSpace('W', domain) | ||
|
|
||
| alpha, beta = [Constant(name) for name in ('alpha', 'beta')] | ||
| f, g, h = elements_of(scalar_space, names='f, g, h') | ||
| field_f, field_g, field_h = elements_of(vector_space, names='F, G, H') | ||
|
|
||
| # Gradient linearity and product rules. | ||
| assert grad(f + g) == grad(f) + grad(g) | ||
| assert grad(alpha * f + beta * g) == alpha * grad(f) + beta * grad(g) | ||
| assert grad(f * g) == f * grad(g) + g * grad(f) | ||
| assert grad(f / g) == -f * grad(g) / g**2 + grad(f) / g | ||
| assert expand(grad(f * g * h)) == ( | ||
| f * g * grad(h) + f * h * grad(g) + g * h * grad(f) | ||
| ) | ||
|
|
||
| # Linearity of curl, Laplace, and divergence operators. | ||
| assert curl(field_f + field_g) == curl(field_f) + curl(field_g) | ||
| assert curl(alpha * field_f + beta * field_g) == ( | ||
| alpha * curl(field_f) + beta * curl(field_g) | ||
| ) | ||
| assert laplace(f + g) == laplace(f) + laplace(g) | ||
| assert laplace(alpha * f + beta * g) == alpha * laplace(f) + beta * laplace(g) | ||
| assert div(field_f + field_g) == div(field_f) + div(field_g) | ||
| assert div(alpha * field_f + beta * field_g) == ( | ||
| alpha * div(field_f) + beta * div(field_g) | ||
| ) | ||
|
|
||
| # Exact-sequence identities. | ||
| assert curl(grad(h)) == 0 | ||
| assert div(curl(field_h)) == 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| """Write the expression trees of symbolic expressions in the DOT language.""" | ||
|
|
||
| from sympy.printing.dot import dotprint | ||
|
|
||
| from sympde.api import Domain, ScalarFunctionSpace, div, dx, dy, elements_of, grad | ||
|
|
||
|
|
||
| domain = Domain('Omega', dim=2) | ||
| space = ScalarFunctionSpace('V', domain) | ||
| u, v = elements_of(space, names='u, v') | ||
|
|
||
|
|
||
| # [operator-start] | ||
| def L(w): | ||
| return -div(grad(w)) + w | ||
|
|
||
|
|
||
| operator_graph = dotprint(L(u)) | ||
| # [operator-end] | ||
|
|
||
|
|
||
| # [derivative-start] | ||
| expr = dx(dy(u * v)) | ||
| assert expr == u * dx(dy(v)) + v * dx(dy(u)) + dx(u) * dy(v) + dx(v) * dy(u) | ||
|
|
||
| derivative_graph = dotprint(expr) | ||
| # [derivative-end] | ||
|
|
||
|
|
||
| # Graphs drawn in the documentation, by file name | ||
| graphs = { | ||
| 'operator': operator_graph, | ||
| 'derivative': derivative_graph, | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.