Update Laplace class and add unit tests (#645)
This commit is contained in:
@@ -2,40 +2,18 @@
|
||||
|
||||
import torch
|
||||
from ... import Condition
|
||||
from ...domain import CartesianDomain
|
||||
from ...operator import grad, laplacian
|
||||
from ...equation import Equation, FixedValue
|
||||
from ...equation import Equation, FixedValue, DiffusionReaction
|
||||
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_, 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)
|
||||
+ (15 / 4) * torch.sin(4 * x)
|
||||
+ (63 / 8) * torch.sin(8 * x)
|
||||
)
|
||||
return u_t - u_xx - r
|
||||
from ...utils import check_consistency
|
||||
from ...domain import CartesianDomain
|
||||
|
||||
|
||||
def initial_condition(input_, output_):
|
||||
"""
|
||||
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.
|
||||
: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
|
||||
"""
|
||||
@@ -76,12 +54,43 @@ class DiffusionReactionProblem(TimeDependentProblem, SpatialProblem):
|
||||
}
|
||||
|
||||
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 __init__(self, alpha=1e-4):
|
||||
"""
|
||||
Initialization of the :class:`DiffusionReactionProblem`.
|
||||
|
||||
:param alpha: The diffusion coefficient.
|
||||
:type alpha: float | int
|
||||
"""
|
||||
super().__init__()
|
||||
check_consistency(alpha, (float, int))
|
||||
self.alpha = alpha
|
||||
|
||||
def forcing_term(input_):
|
||||
"""
|
||||
Implementation of the forcing term.
|
||||
"""
|
||||
# Extract spatial and temporal variables
|
||||
spatial_d = [di for di in input_.labels if di != "t"]
|
||||
x = input_.extract(spatial_d)
|
||||
t = input_.extract("t")
|
||||
|
||||
return torch.exp(-t) * (
|
||||
1.5 * torch.sin(2 * x)
|
||||
+ (8 / 3) * torch.sin(3 * x)
|
||||
+ (15 / 4) * torch.sin(4 * x)
|
||||
+ (63 / 8) * torch.sin(8 * x)
|
||||
)
|
||||
|
||||
self.conditions["D"] = Condition(
|
||||
domain="D",
|
||||
equation=DiffusionReaction(self.alpha, forcing_term),
|
||||
)
|
||||
|
||||
def solution(self, pts):
|
||||
"""
|
||||
Implementation of the analytical solution of the diffusion-reaction
|
||||
|
||||
Reference in New Issue
Block a user