1- """Definition of the diffusion-reaction problem."""
1+ """Formulation of the diffusion-reaction problem."""
22
33import torch
4- from pina import Condition
5- from pina . problem import SpatialProblem , TimeDependentProblem
6- from pina . equation . equation import Equation
7- from pina . domain import CartesianDomain
8- from pina . operator import grad
4+ from ... import Condition
5+ from ... domain import CartesianDomain
6+ from ... operator import grad , laplacian
7+ from ... equation import Equation , FixedValue
8+ from ... problem import SpatialProblem , TimeDependentProblem
99
1010
1111def diffusion_reaction (input_ , output_ ):
1212 """
1313 Implementation of the diffusion-reaction equation.
14+
15+ :param LabelTensor input_: Input data of the problem.
16+ :param LabelTensor output_: Output data of the problem.
17+ :return: The residual of the diffusion-reaction equation.
18+ :rtype: LabelTensor
1419 """
1520 x = input_ .extract ("x" )
1621 t = input_ .extract ("t" )
17- u_t = grad (output_ , input_ , d = "t" )
18- u_x = grad (output_ , input_ , d = "x" )
19- u_xx = grad (u_x , input_ , d = "x" )
22+ u_t = grad (output_ , input_ , components = ["u" ], d = ["t" ])
23+ u_xx = laplacian (output_ , input_ , components = ["u" ], d = ["x" ])
2024 r = torch .exp (- t ) * (
2125 1.5 * torch .sin (2 * x )
2226 + (8 / 3 ) * torch .sin (3 * x )
@@ -26,30 +30,72 @@ def diffusion_reaction(input_, output_):
2630 return u_t - u_xx - r
2731
2832
29- class DiffusionReactionProblem (TimeDependentProblem , SpatialProblem ):
33+ def initial_condition (input_ , output_ ):
34+ """
35+ Definition of the initial condition of the diffusion-reaction problem.
36+
37+ :param LabelTensor input_: Input data of the problem.
38+ :param LabelTensor output_: Output data of the problem.
39+ :return: The residual of the initial condition.
40+ :rtype: LabelTensor
3041 """
31- Implementation of the diffusion-reaction problem on the spatial interval
32- [-pi, pi] and temporal interval [0,1].
42+ x = input_ .extract ("x" )
43+ u_0 = (
44+ torch .sin (x )
45+ + (1 / 2 ) * torch .sin (2 * x )
46+ + (1 / 3 ) * torch .sin (3 * x )
47+ + (1 / 4 ) * torch .sin (4 * x )
48+ + (1 / 8 ) * torch .sin (8 * x )
49+ )
50+ return output_ - u_0
51+
52+
53+ class DiffusionReactionProblem (TimeDependentProblem , SpatialProblem ):
54+ r"""
55+ Implementation of the diffusion-reaction problem in the spatial interval
56+ :math:`[-\pi, \pi]` and temporal interval :math:`[0, 1]`.
57+
58+ .. seealso::
59+ **Original reference**: Si, Chenhao, et al. *Complex Physics-Informed
60+ Neural Network.* arXiv preprint arXiv:2502.04917 (2025).
61+ DOI: `arXiv:2502.04917 <https://arxiv.org/abs/2502.04917>`_.
3362 """
3463
3564 output_variables = ["u" ]
3665 spatial_domain = CartesianDomain ({"x" : [- torch .pi , torch .pi ]})
3766 temporal_domain = CartesianDomain ({"t" : [0 , 1 ]})
3867
68+ domains = {
69+ "D" : CartesianDomain ({"x" : [- torch .pi , torch .pi ], "t" : [0 , 1 ]}),
70+ "g1" : CartesianDomain ({"x" : - torch .pi , "t" : [0 , 1 ]}),
71+ "g2" : CartesianDomain ({"x" : torch .pi , "t" : [0 , 1 ]}),
72+ "t0" : CartesianDomain ({"x" : [- torch .pi , torch .pi ], "t" : 0.0 }),
73+ }
74+
3975 conditions = {
40- "D" : Condition (
41- domain = CartesianDomain ({ "x" : [ - torch . pi , torch . pi ], "t" : [ 0 , 1 ]} ),
42- equation = Equation ( diffusion_reaction ),
43- )
76+ "D" : Condition (domain = "D" , equation = Equation ( diffusion_reaction )),
77+ "g1" : Condition ( domain = "g1" , equation = FixedValue ( 0.0 ) ),
78+ "g2" : Condition ( domain = "g2" , equation = FixedValue ( 0.0 ) ),
79+ "t0" : Condition ( domain = "t0" , equation = Equation ( initial_condition )),
4480 }
4581
46- def _solution (self , pts ):
82+ def solution (self , pts ):
83+ """
84+ Implementation of the analytical solution of the diffusion-reaction
85+ problem.
86+
87+ :param LabelTensor pts: Points where the solution is evaluated.
88+ :return: The analytical solution of the diffusion-reaction problem.
89+ :rtype: LabelTensor
90+ """
4791 t = pts .extract ("t" )
4892 x = pts .extract ("x" )
49- return torch .exp (- t ) * (
93+ sol = torch .exp (- t ) * (
5094 torch .sin (x )
5195 + (1 / 2 ) * torch .sin (2 * x )
5296 + (1 / 3 ) * torch .sin (3 * x )
5397 + (1 / 4 ) * torch .sin (4 * x )
5498 + (1 / 8 ) * torch .sin (8 * x )
5599 )
100+ sol .labels = self .output_variables
101+ return sol
0 commit comments