# examples.levelSet.distanceFunction.mesh1DΒΆ

Create a level set variable in one dimension.

The level set variable calculates its value over the domain to be the distance from the zero level set. This can be represented succinctly in the following equation with a boundary condition at the zero level set such that,

with the boundary condition, at .

The solution to this problem will be demonstrated in the following script. Firstly, setup the parameters.

>>> from fipy import CellVariable, Grid1D, DistanceVariable, TransientTerm, FirstOrderAdvectionTerm, AdvectionTerm, Viewer
>>> from fipy.tools import numerix, serialComm

>>> dx = 0.5
>>> nx = 10


Construct the mesh.

>>> mesh = Grid1D(dx=dx, nx=nx, communicator=serialComm)


Construct a distanceVariable object.

>>> var = DistanceVariable(name='level set variable',
...                        mesh=mesh,
...                        value=-1.,
...                        hasOld=1)
>>> x = mesh.cellCenters[0]
>>> var.setValue(1, where=x > dx * nx / 2)


Once the initial positive and negative regions have been initialized the calcDistanceFunction() method can be used to recalculate var as a distance function from the zero level set.

>>> var.calcDistanceFunction()


The problem can then be solved by executing the solve() method of the equation.

>>> if __name__ == '__main__':
...     viewer = Viewer(vars=var, datamin=-5., datamax=5.)
...     viewer.plot()


The result can be tested with the following commands.

>>> print(numerix.allclose(var, x - dx * nx / 2))
1

Last updated on Jun 27, 2023. Created using Sphinx 6.2.1.