-
Notifications
You must be signed in to change notification settings - Fork 90
Implementation of the acoustic wave problem #717
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
Changes from all commits
Commits
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| AcousticWaveProblem | ||
| ===================== | ||
| .. currentmodule:: pina.problem.zoo.acoustic_wave | ||
|
|
||
| .. automodule:: pina.problem.zoo.acoustic_wave | ||
|
|
||
| .. autoclass:: AcousticWaveProblem | ||
| :members: | ||
| :show-inheritance: |
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 |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| """Formulation of the acoustic wave problem.""" | ||
|
|
||
| import torch | ||
| from ... import Condition | ||
| from ...problem import SpatialProblem, TimeDependentProblem | ||
| from ...utils import check_consistency | ||
| from ...domain import CartesianDomain | ||
| from ...equation import ( | ||
| Equation, | ||
| SystemEquation, | ||
| FixedValue, | ||
| FixedGradient, | ||
| AcousticWave, | ||
| ) | ||
|
|
||
|
|
||
| def initial_condition(input_, output_): | ||
| """ | ||
| Definition of the initial condition of the acoustic wave problem. | ||
|
|
||
| :param LabelTensor input_: The input data of the problem. | ||
| :param LabelTensor output_: The output data of the problem. | ||
| :return: The residual of the initial condition. | ||
| :rtype: LabelTensor | ||
| """ | ||
| arg = torch.pi * input_["x"] | ||
| return output_ - torch.sin(arg) - 0.5 * torch.sin(4 * arg) | ||
|
|
||
|
|
||
| class AcousticWaveProblem(TimeDependentProblem, SpatialProblem): | ||
| r""" | ||
| Implementation of the acoustic wave problem in the spatial interval | ||
| :math:`[0, 1]` and temporal interval :math:`[0, 1]`. | ||
|
|
||
| .. seealso:: | ||
|
|
||
| **Original reference**: Wang, Sifan, Xinling Yu, and | ||
| Paris Perdikaris. *When and why PINNs fail to train: | ||
| A neural tangent kernel perspective*. Journal of | ||
| Computational Physics 449 (2022): 110768. | ||
| DOI: `10.1016 <https://doi.org/10.1016/j.jcp.2021.110768>`_. | ||
|
|
||
| :Example: | ||
|
|
||
| >>> problem = AcousticWaveProblem(c=2.0) | ||
| """ | ||
|
|
||
| output_variables = ["u"] | ||
| spatial_domain = CartesianDomain({"x": [0, 1]}) | ||
| temporal_domain = CartesianDomain({"t": [0, 1]}) | ||
|
|
||
| domains = { | ||
| "D": CartesianDomain({"x": [0, 1], "t": [0, 1]}), | ||
| "t0": CartesianDomain({"x": [0, 1], "t": 0.0}), | ||
| "g1": CartesianDomain({"x": 0.0, "t": [0, 1]}), | ||
| "g2": CartesianDomain({"x": 1.0, "t": [0, 1]}), | ||
| } | ||
|
|
||
| conditions = { | ||
| "g1": Condition(domain="g1", equation=FixedValue(value=0.0)), | ||
| "g2": Condition(domain="g2", equation=FixedValue(value=0.0)), | ||
| "t0": Condition( | ||
| domain="t0", | ||
| equation=SystemEquation( | ||
| [Equation(initial_condition), FixedGradient(value=0.0, d="t")] | ||
| ), | ||
| ), | ||
| } | ||
|
|
||
| def __init__(self, c=2.0): | ||
| """ | ||
| Initialization of the :class:`AcousticWaveProblem` class. | ||
|
|
||
| :param c: The wave propagation speed. Default is 2.0. | ||
| :type c: float | int | ||
| """ | ||
| super().__init__() | ||
| check_consistency(c, (float, int)) | ||
| self.c = c | ||
|
|
||
| self.conditions["D"] = Condition( | ||
| domain="D", equation=AcousticWave(self.c) | ||
| ) | ||
|
|
||
| def solution(self, pts): | ||
| """ | ||
| Implementation of the analytical solution of the acoustic wave problem. | ||
|
|
||
| :param LabelTensor pts: Points where the solution is evaluated. | ||
| :return: The analytical solution of the acoustic wave problem. | ||
| :rtype: LabelTensor | ||
| """ | ||
| arg_x = torch.pi * pts["x"] | ||
| arg_t = self.c * torch.pi * pts["t"] | ||
| term1 = torch.sin(arg_x) * torch.cos(arg_t) | ||
| term2 = 0.5 * torch.sin(4 * arg_x) * torch.cos(4 * arg_t) | ||
| return term1 + term2 | ||
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,19 @@ | ||
| import pytest | ||
| from pina.problem.zoo import AcousticWaveProblem | ||
| from pina.problem import SpatialProblem, TimeDependentProblem | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("c", [0.1, 1]) | ||
| def test_constructor(c): | ||
|
|
||
| problem = AcousticWaveProblem(c=c) | ||
| problem.discretise_domain(n=10, mode="random", domains="all") | ||
| assert problem.are_all_domains_discretised | ||
| assert isinstance(problem, SpatialProblem) | ||
| assert isinstance(problem, TimeDependentProblem) | ||
| assert hasattr(problem, "conditions") | ||
| assert isinstance(problem.conditions, dict) | ||
|
|
||
| # Should fail if c is not a float or int | ||
| with pytest.raises(ValueError): | ||
| AcousticWaveProblem(c="invalid") |
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.