Solve the distance function equation in one dimension and then advect it.
This example first solves the distance function equation in one dimension:
with at .
The variable is then advected with,
The scheme used in the FirstOrderAdvectionTerm preserves the var as a distance function.
The solution to this problem will be demonstrated in the following script. Firstly, setup the parameters.
>>> from fipy import *
>>> velocity = 1. >>> dx = 1. >>> nx = 10 >>> timeStepDuration = 1. >>> steps = 2 >>> L = nx * dx >>> interfacePosition = L / 5.
Construct the mesh.
>>> from fipy.tools import serialComm >>> mesh = Grid1D(dx=dx, nx=nx, communicator=serialComm)
Construct a distanceVariable object.
>>> var = DistanceVariable(name='level set variable', ... mesh=mesh, ... value=-1., ... hasOld=1) >>> var.setValue(1., where=mesh.cellCenters > interfacePosition) >>> var.calcDistanceFunction()
The advectionEquation is constructed.
>>> advEqn = TransientTerm() + FirstOrderAdvectionTerm(velocity)
The problem can then be solved by executing a serious of time steps.
>>> if __name__ == '__main__': ... viewer = Viewer(vars=var, datamin=-10., datamax=10.) ... viewer.plot() ... for step in range(steps): ... var.updateOld() ... advEqn.solve(var, dt=timeStepDuration) ... viewer.plot()
The result can be tested with the following code:
>>> for step in range(steps): ... var.updateOld() ... advEqn.solve(var, dt=timeStepDuration) >>> x = mesh.cellCenters >>> distanceTravelled = timeStepDuration * steps * velocity >>> answer = x - interfacePosition - timeStepDuration * steps * velocity >>> answer = numerix.where(x < distanceTravelled, ... x - interfacePosition, answer) >>> print var.allclose(answer) 1