Update Laplace class and add unit tests (#645)

This commit is contained in:
Giovanni Canali
2025-09-22 15:05:28 +02:00
committed by GitHub
parent 4a6e73fa54
commit 4e37468460
15 changed files with 673 additions and 157 deletions

View File

@@ -2,32 +2,18 @@
import torch
from ... import Condition
from ...equation import Equation
from ...domain import CartesianDomain
from ...operator import grad, laplacian
from ...problem import SpatialProblem, TimeDependentProblem
def allen_cahn_equation(input_, output_):
"""
Implementation of the Allen Cahn equation.
:param LabelTensor input_: Input data of the problem.
:param LabelTensor output_: Output data of the problem.
:return: The residual of the Allen Cahn equation.
:rtype: LabelTensor
"""
u_t = grad(output_, input_, components=["u"], d=["t"])
u_xx = laplacian(output_, input_, components=["u"], d=["x"])
return u_t - 0.0001 * u_xx + 5 * output_**3 - 5 * output_
from ...equation import Equation, AllenCahn
from ...utils import check_consistency
from ...domain import CartesianDomain
def initial_condition(input_, output_):
"""
Definition of the initial condition of the Allen Cahn 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
"""
@@ -64,6 +50,25 @@ class AllenCahnProblem(TimeDependentProblem, SpatialProblem):
}
conditions = {
"D": Condition(domain="D", equation=Equation(allen_cahn_equation)),
"t0": Condition(domain="t0", equation=Equation(initial_condition)),
}
def __init__(self, alpha=1e-4, beta=5):
"""
Initialization of the :class:`AllenCahnProblem`.
:param alpha: The diffusion coefficient.
:type alpha: float | int
:param beta: The reaction coefficient.
:type beta: float | int
"""
super().__init__()
check_consistency(alpha, (float, int))
check_consistency(beta, (float, int))
self.alpha = alpha
self.beta = beta
self.conditions["D"] = Condition(
domain="D",
equation=AllenCahn(alpha=self.alpha, beta=self.beta),
)