Skip to content

Reinit class

The Reinit class solve the Eikonal equation

\[ \begin{aligned} \partial_t \varphi - h^2 \Delta \varphi &= S(\phi)(1 - |\nabla \varphi|) && x\in \mathcal{D},\, t>0, \\ \partial_n \varphi &= 0 && x\in \partial\mathcal{D},\, t>0, \\ \varphi(0, x) &= \phi(x) && x\in \overline{\mathcal{D}}, \end{aligned} \]

where \(\phi\) is the current level set function, \(h\) is the mesh diameter, and

\[ S(\phi) = \frac{\phi}{\sqrt{\phi^2 + h^2}}. \]

This class implements the Petrov Galerkin and two-step Adams-Bashforth methods to solve the diffusive Eikonal equation with fictitious time derivative.

Attributes:

Name Type Description
dt Constant

Time step.

phi_ini Function

Store the initial level set function to be reinitialized.

phi_prev Function

Store the previous iteration.

w Function

Auxiliary function.

uh Function

Solution function.

problem0 LinearProblem

Solver used in the first iteration.

problem LinearProblem

Solver used in subsequent iterations.

Methods:

Name Description
run

Run the iterative method over a fixed number of time steps.

Source code in code/formopt.py
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
class Reinit:
    """
    This class implements the Petrov Galerkin and
    two-step Adams-Bashforth methods to solve
    the diffusive Eikonal equation with
    fictitious time derivative.

    Attributes
    ----------
    dt : Constant
        Time step.
    phi_ini : Function
        Store the initial level set function to be reinitialized.
    phi_prev : Function
        Store the previous iteration.
    w : Function
        Auxiliary function.
    uh : Function
        Solution function.
    problem0 : LinearProblem
        Solver used in the first iteration.
    problem : LinearProblem
        Solver used in subsequent iterations.

    Methods
    -------
    run(phi: Function, steps: int, tend: float) -> None
        Run the iterative method over a fixed number of time steps.
    """

    def __init__(
        self, domain: Mesh, space: FunctionSpace, phi: Function, diam2: float
    ) -> None:
        """
        Set up the reinitialization method.
        Since the bilinear parts change across iterations
        (due to the Petrov-Galerkin test function),
        two solvers are created, which compile
        the equations at each call.

        Parameters
        ----------
        domain : Mesh
            Problem domain.
        space : FunctionSpace
            Space of functions.
        phi : Function
            Level set function.
        diam2 : float
            Square of the mesh diameter.
        """

        self.dt = const(domain, 0.0)
        dx = Measure("dx", domain=domain)

        self.phi_ini = Function(space)
        self.phi_prev = Function(space)

        self.uh = Function(space)
        self.w = Function(space)

        # Sign function
        sign_phi_ini = self.phi_ini / sqrt(self.phi_ini**2 + diam2)
        # Hamiltonian
        H = lambda p: sign_phi_ini * sqrt(dot(p, p))
        # Gradient of H
        GradH = lambda p: sign_phi_ini * p / sqrt(dot(p, p))

        u = TrialFunction(space)
        v = TestFunction(space)

        # Petrov-Galerkin test function
        tau = 2.0 * sqrt(1.0 / self.dt**2 + sign_phi_ini**2 / diam2)
        new_v = v + dot(grad(v), GradH(grad(phi))) / tau

        # Adams-Bashforth weak formulation
        a = u * new_v * dx
        L = phi * new_v * dx
        L += self.dt * sign_phi_ini * new_v * dx
        L += (
            (self.dt / 2.0) * (H(grad(self.phi_prev)) - 3.0 * H(grad(phi))) * new_v * dx
        )
        # Add diffusion
        L += (
            (self.dt / 2.0) * diam2 * dot(grad(self.phi_prev - 3.0 * phi), grad(v)) * dx
        )

        self.problem = create_solver(form(a), form(L), [], self.uh)

        # Explicit Euler weak formulation
        a0 = u * new_v * dx
        L0 = self.dt * sign_phi_ini * new_v * dx
        L0 += (self.phi_ini - self.dt * H(grad(self.phi_ini))) * new_v * dx
        # Add diffusion
        L0 -= self.dt * diam2 * dot(grad(self.phi_ini), grad(v)) * dx

        self.problem0 = create_solver(form(a0), form(L0), [], self.uh)

    def run(self, phi: Function, steps: int, tend: float) -> None:
        """
        Run the iterative method over a fixed number of time steps.
        The solver for the first iteration is called only once,
        and then the main solver is called at each time step.

        Parameters
        ----------
        phi : Function
            Level set function.
        steps : int
            Number of time steps.
        tend : float
            Final time.
        """

        self.dt.value = tend / steps
        self.phi_ini.interpolate(phi)

        self.problem0.solve()
        self.phi_prev.x.array[:] = self.uh.x.array[:]

        for _ in range(steps):
            self.w.x.array[:] = phi.x.array[:]
            self.problem.solve()
            phi.x.array[:] = self.uh.x.array[:]
            self.phi_prev.x.array[:] = self.w.x.array[:]

__init__(domain, space, phi, diam2)

Set up the reinitialization method. Since the bilinear parts change across iterations (due to the Petrov-Galerkin test function), two solvers are created, which compile the equations at each call.

Parameters:

Name Type Description Default
domain Mesh

Problem domain.

required
space FunctionSpace

Space of functions.

required
phi Function

Level set function.

required
diam2 float

Square of the mesh diameter.

required
Source code in code/formopt.py
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
def __init__(
    self, domain: Mesh, space: FunctionSpace, phi: Function, diam2: float
) -> None:
    """
    Set up the reinitialization method.
    Since the bilinear parts change across iterations
    (due to the Petrov-Galerkin test function),
    two solvers are created, which compile
    the equations at each call.

    Parameters
    ----------
    domain : Mesh
        Problem domain.
    space : FunctionSpace
        Space of functions.
    phi : Function
        Level set function.
    diam2 : float
        Square of the mesh diameter.
    """

    self.dt = const(domain, 0.0)
    dx = Measure("dx", domain=domain)

    self.phi_ini = Function(space)
    self.phi_prev = Function(space)

    self.uh = Function(space)
    self.w = Function(space)

    # Sign function
    sign_phi_ini = self.phi_ini / sqrt(self.phi_ini**2 + diam2)
    # Hamiltonian
    H = lambda p: sign_phi_ini * sqrt(dot(p, p))
    # Gradient of H
    GradH = lambda p: sign_phi_ini * p / sqrt(dot(p, p))

    u = TrialFunction(space)
    v = TestFunction(space)

    # Petrov-Galerkin test function
    tau = 2.0 * sqrt(1.0 / self.dt**2 + sign_phi_ini**2 / diam2)
    new_v = v + dot(grad(v), GradH(grad(phi))) / tau

    # Adams-Bashforth weak formulation
    a = u * new_v * dx
    L = phi * new_v * dx
    L += self.dt * sign_phi_ini * new_v * dx
    L += (
        (self.dt / 2.0) * (H(grad(self.phi_prev)) - 3.0 * H(grad(phi))) * new_v * dx
    )
    # Add diffusion
    L += (
        (self.dt / 2.0) * diam2 * dot(grad(self.phi_prev - 3.0 * phi), grad(v)) * dx
    )

    self.problem = create_solver(form(a), form(L), [], self.uh)

    # Explicit Euler weak formulation
    a0 = u * new_v * dx
    L0 = self.dt * sign_phi_ini * new_v * dx
    L0 += (self.phi_ini - self.dt * H(grad(self.phi_ini))) * new_v * dx
    # Add diffusion
    L0 -= self.dt * diam2 * dot(grad(self.phi_ini), grad(v)) * dx

    self.problem0 = create_solver(form(a0), form(L0), [], self.uh)

run(phi, steps, tend)

Run the iterative method over a fixed number of time steps. The solver for the first iteration is called only once, and then the main solver is called at each time step.

Parameters:

Name Type Description Default
phi Function

Level set function.

required
steps int

Number of time steps.

required
tend float

Final time.

required
Source code in code/formopt.py
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
def run(self, phi: Function, steps: int, tend: float) -> None:
    """
    Run the iterative method over a fixed number of time steps.
    The solver for the first iteration is called only once,
    and then the main solver is called at each time step.

    Parameters
    ----------
    phi : Function
        Level set function.
    steps : int
        Number of time steps.
    tend : float
        Final time.
    """

    self.dt.value = tend / steps
    self.phi_ini.interpolate(phi)

    self.problem0.solve()
    self.phi_prev.x.array[:] = self.uh.x.array[:]

    for _ in range(steps):
        self.w.x.array[:] = phi.x.array[:]
        self.problem.solve()
        phi.x.array[:] = self.uh.x.array[:]
        self.phi_prev.x.array[:] = self.w.x.array[:]