|
| 1 | +"""Formulation of the acoustic wave problem.""" |
| 2 | + |
| 3 | +import torch |
| 4 | +from ... import Condition |
| 5 | +from ...problem import SpatialProblem, TimeDependentProblem |
| 6 | +from ...utils import check_consistency |
| 7 | +from ...domain import CartesianDomain |
| 8 | +from ...equation import ( |
| 9 | + Equation, |
| 10 | + SystemEquation, |
| 11 | + FixedValue, |
| 12 | + FixedGradient, |
| 13 | + AcousticWave, |
| 14 | +) |
| 15 | + |
| 16 | + |
| 17 | +def initial_condition(input_, output_): |
| 18 | + """ |
| 19 | + Definition of the initial condition of the acoustic wave problem. |
| 20 | +
|
| 21 | + :param LabelTensor input_: The input data of the problem. |
| 22 | + :param LabelTensor output_: The output data of the problem. |
| 23 | + :return: The residual of the initial condition. |
| 24 | + :rtype: LabelTensor |
| 25 | + """ |
| 26 | + arg = torch.pi * input_["x"] |
| 27 | + return output_ - torch.sin(arg) - 0.5 * torch.sin(4 * arg) |
| 28 | + |
| 29 | + |
| 30 | +class AcousticWaveProblem(TimeDependentProblem, SpatialProblem): |
| 31 | + r""" |
| 32 | + Implementation of the acoustic wave problem in the spatial interval |
| 33 | + :math:`[0, 1]` and temporal interval :math:`[0, 1]`. |
| 34 | +
|
| 35 | + .. seealso:: |
| 36 | +
|
| 37 | + **Original reference**: Wang, Sifan, Xinling Yu, and |
| 38 | + Paris Perdikaris. *When and why PINNs fail to train: |
| 39 | + A neural tangent kernel perspective*. Journal of |
| 40 | + Computational Physics 449 (2022): 110768. |
| 41 | + DOI: `10.1016 <https://doi.org/10.1016/j.jcp.2021.110768>`_. |
| 42 | +
|
| 43 | + :Example: |
| 44 | +
|
| 45 | + >>> problem = AcousticWaveProblem(c=2.0) |
| 46 | + """ |
| 47 | + |
| 48 | + output_variables = ["u"] |
| 49 | + spatial_domain = CartesianDomain({"x": [0, 1]}) |
| 50 | + temporal_domain = CartesianDomain({"t": [0, 1]}) |
| 51 | + |
| 52 | + domains = { |
| 53 | + "D": CartesianDomain({"x": [0, 1], "t": [0, 1]}), |
| 54 | + "t0": CartesianDomain({"x": [0, 1], "t": 0.0}), |
| 55 | + "g1": CartesianDomain({"x": 0.0, "t": [0, 1]}), |
| 56 | + "g2": CartesianDomain({"x": 1.0, "t": [0, 1]}), |
| 57 | + } |
| 58 | + |
| 59 | + conditions = { |
| 60 | + "g1": Condition(domain="g1", equation=FixedValue(value=0.0)), |
| 61 | + "g2": Condition(domain="g2", equation=FixedValue(value=0.0)), |
| 62 | + "t0": Condition( |
| 63 | + domain="t0", |
| 64 | + equation=SystemEquation( |
| 65 | + [Equation(initial_condition), FixedGradient(value=0.0, d="t")] |
| 66 | + ), |
| 67 | + ), |
| 68 | + } |
| 69 | + |
| 70 | + def __init__(self, c=2.0): |
| 71 | + """ |
| 72 | + Initialization of the :class:`AcousticWaveProblem` class. |
| 73 | +
|
| 74 | + :param c: The wave propagation speed. Default is 2.0. |
| 75 | + :type c: float | int |
| 76 | + """ |
| 77 | + super().__init__() |
| 78 | + check_consistency(c, (float, int)) |
| 79 | + self.c = c |
| 80 | + |
| 81 | + self.conditions["D"] = Condition( |
| 82 | + domain="D", equation=AcousticWave(self.c) |
| 83 | + ) |
| 84 | + |
| 85 | + def solution(self, pts): |
| 86 | + """ |
| 87 | + Implementation of the analytical solution of the acoustic wave problem. |
| 88 | +
|
| 89 | + :param LabelTensor pts: Points where the solution is evaluated. |
| 90 | + :return: The analytical solution of the acoustic wave problem. |
| 91 | + :rtype: LabelTensor |
| 92 | + """ |
| 93 | + arg_x = torch.pi * pts["x"] |
| 94 | + arg_t = self.c * torch.pi * pts["t"] |
| 95 | + term1 = torch.sin(arg_x) * torch.cos(arg_t) |
| 96 | + term2 = 0.5 * torch.sin(4 * arg_x) * torch.cos(4 * arg_t) |
| 97 | + return term1 + term2 |
0 commit comments