Adding new problems to problem.zoo (#484)

* adding problems
* add tests
* update doc + formatting

---------

Co-authored-by: Dario Coscia <dariocos99@gmail.com>
This commit is contained in:
Giovanni Canali
2025-03-12 10:42:16 +01:00
committed by Nicola Demo
parent 2ae4a94e49
commit f67467e5bd
21 changed files with 570 additions and 168 deletions

View File

@@ -1,22 +1,26 @@
"""Definition of the diffusion-reaction problem."""
"""Formulation of the diffusion-reaction problem."""
import torch
from pina import Condition
from pina.problem import SpatialProblem, TimeDependentProblem
from pina.equation.equation import Equation
from pina.domain import CartesianDomain
from pina.operator import grad
from ... import Condition
from ...domain import CartesianDomain
from ...operator import grad, laplacian
from ...equation import Equation, FixedValue
from ...problem import SpatialProblem, TimeDependentProblem
def diffusion_reaction(input_, output_):
"""
Implementation of the diffusion-reaction equation.
:param LabelTensor input_: Input data of the problem.
:param LabelTensor output_: Output data of the problem.
:return: The residual of the diffusion-reaction equation.
:rtype: LabelTensor
"""
x = input_.extract("x")
t = input_.extract("t")
u_t = grad(output_, input_, d="t")
u_x = grad(output_, input_, d="x")
u_xx = grad(u_x, input_, d="x")
u_t = grad(output_, input_, components=["u"], d=["t"])
u_xx = laplacian(output_, input_, components=["u"], d=["x"])
r = torch.exp(-t) * (
1.5 * torch.sin(2 * x)
+ (8 / 3) * torch.sin(3 * x)
@@ -26,30 +30,72 @@ def diffusion_reaction(input_, output_):
return u_t - u_xx - r
class DiffusionReactionProblem(TimeDependentProblem, SpatialProblem):
def initial_condition(input_, output_):
"""
Implementation of the diffusion-reaction problem on the spatial interval
[-pi, pi] and temporal interval [0,1].
Definition of the initial condition of the diffusion-reaction problem.
:param LabelTensor input_: Input data of the problem.
:param LabelTensor output_: Output data of the problem.
:return: The residual of the initial condition.
:rtype: LabelTensor
"""
x = input_.extract("x")
u_0 = (
torch.sin(x)
+ (1 / 2) * torch.sin(2 * x)
+ (1 / 3) * torch.sin(3 * x)
+ (1 / 4) * torch.sin(4 * x)
+ (1 / 8) * torch.sin(8 * x)
)
return output_ - u_0
class DiffusionReactionProblem(TimeDependentProblem, SpatialProblem):
r"""
Implementation of the diffusion-reaction problem in the spatial interval
:math:`[-\pi, \pi]` and temporal interval :math:`[0, 1]`.
.. seealso::
**Original reference**: Si, Chenhao, et al. *Complex Physics-Informed
Neural Network.* arXiv preprint arXiv:2502.04917 (2025).
DOI: `arXiv:2502.04917 <https://arxiv.org/abs/2502.04917>`_.
"""
output_variables = ["u"]
spatial_domain = CartesianDomain({"x": [-torch.pi, torch.pi]})
temporal_domain = CartesianDomain({"t": [0, 1]})
conditions = {
"D": Condition(
domain=CartesianDomain({"x": [-torch.pi, torch.pi], "t": [0, 1]}),
equation=Equation(diffusion_reaction),
)
domains = {
"D": CartesianDomain({"x": [-torch.pi, torch.pi], "t": [0, 1]}),
"g1": CartesianDomain({"x": -torch.pi, "t": [0, 1]}),
"g2": CartesianDomain({"x": torch.pi, "t": [0, 1]}),
"t0": CartesianDomain({"x": [-torch.pi, torch.pi], "t": 0.0}),
}
def _solution(self, pts):
conditions = {
"D": Condition(domain="D", equation=Equation(diffusion_reaction)),
"g1": Condition(domain="g1", equation=FixedValue(0.0)),
"g2": Condition(domain="g2", equation=FixedValue(0.0)),
"t0": Condition(domain="t0", equation=Equation(initial_condition)),
}
def solution(self, pts):
"""
Implementation of the analytical solution of the diffusion-reaction
problem.
:param LabelTensor pts: Points where the solution is evaluated.
:return: The analytical solution of the diffusion-reaction problem.
:rtype: LabelTensor
"""
t = pts.extract("t")
x = pts.extract("x")
return torch.exp(-t) * (
sol = torch.exp(-t) * (
torch.sin(x)
+ (1 / 2) * torch.sin(2 * x)
+ (1 / 3) * torch.sin(3 * x)
+ (1 / 4) * torch.sin(4 * x)
+ (1 / 8) * torch.sin(8 * x)
)
sol.labels = self.output_variables
return sol