def unstructuredEx3dinCpp(initFunc,
                          Lx,
                          Ly,
                          Lz,
                          nx,
                          ny,
                          nz,
                          method='FMM',
                          verbose=0):
    """
    run a redistancing example in 3d:
    """
    import numpy
    from proteus import cfmmfsw

    mesh = MeshTools.TetrahedralMesh()
    mesh.generateTetrahedralMeshFromRectangularGrid(nx, ny, nz, Lx, Ly, Lz)

    femSpace = FemTools.C0_AffineLinearOnSimplexWithNodalBasis(mesh)

    FemPhi0 = FemTools.FiniteElementFunction(femSpace, name="phi0")
    FemPhi0p = FemTools.FiniteElementFunction(femSpace, name="phi0p")
    FemPhi0m = FemTools.FiniteElementFunction(femSpace, name="phi0m")
    FemTp = FemTools.FiniteElementFunction(femSpace, name="Tp")
    FemTm = FemTools.FiniteElementFunction(femSpace, name="Tm")

    phi0 = FemPhi0.dof
    phi0p = FemPhi0p.dof
    phi0m = FemPhi0m.dof
    Tp = FemTp.dof
    Tm = FemTm.dof

    icout = open("phi0.dat", 'w')

    #construct initial level set, short cut assuming dofs <--> node numbers
    for I in range(mesh.nNodes_global):
        x = mesh.nodeArray[I, 0]
        y = mesh.nodeArray[I, 1]
        z = mesh.nodeArray[I, 2]
        phi0[I] = initFunc(x, y, z)
        phi0p[I] = max(phi0[I], 0.0)
        phi0m[I] = abs(min(phi0[I], 0.0))
        icout.write("%g %g %g %g \n" % (x, y, z, phi0[I]))
    #
    failed = False
    nd = 3
    nodalSpeeds = numpy.ones((mesh.nNodes_global, ), 'd')
    if method == 'FSW':
        solver = cfmmfsw.FSWEikonalSolver(nd,
                                          mesh.cmesh,
                                          atol=1.0e-8,
                                          rtol=1.0e-8,
                                          maxIts=100,
                                          initFlag=0)
        print "calling FSWEikonalSolver.solve for + ..."
        failed = solver.solve(FemPhi0p.dof,
                              nodalSpeeds,
                              FemTp.dof,
                              zeroTol=1.0e-4,
                              trialTol=1.0e-1,
                              initFlag=0,
                              verbose=verbose)
        print "back. failed= %s  calling FSWEikonalSolver.solve for - ..." % failed
        failed = solver.solve(FemPhi0m.dof,
                              nodalSpeeds,
                              FemTm.dof,
                              zeroTol=1.0e-4,
                              trialTol=1.0e-1,
                              initFlag=0,
                              verbose=verbose)
        print "back. failed= %s" % failed
    else:
        solver = cfmmfsw.FMMEikonalSolver(nd, mesh.cmesh)
        print "calling FMMEikonalSolver.solve for + ..."
        failed = solver.solve(FemPhi0p.dof,
                              nodalSpeeds,
                              FemTp.dof,
                              zeroTol=1.0e-4,
                              trialTol=1.0e-1,
                              initFlag=0,
                              verbose=verbose)
        print "back. calling FMMEikonalSolver.solve for - ..."
        failed = solver.solve(FemPhi0m.dof,
                              nodalSpeeds,
                              FemTm.dof,
                              zeroTol=1.0e-4,
                              trialTol=1.0e-1,
                              initFlag=0,
                              verbose=verbose)
    print "back."
    #method switch

    fout = open("T.dat", 'w')
    phout = open("phi.dat", 'w')
    for I in range(mesh.nNodes_global):
        x = mesh.nodeArray[I, 0]
        y = mesh.nodeArray[I, 1]
        z = mesh.nodeArray[I, 2]
        fout.write("%g %g %g %g \n" % (x, y, z, Tp[I]))
        phout.write("%g %g %g %g \n" % (x, y, z, Tp[I] - Tm[I]))

    icout.close()
    fout.close()
    phout.close()
def unstructuredEx1dInCpp(initFunc, Lx, nx, method='FMM', verbose=0):
    """
    run a couple of redistancing examples in 1d: circle and two circles
    use c++ interface
    """
    import numpy
    from proteus import cfmmfsw

    mesh = MeshTools.EdgeMesh()
    mesh.generateEdgeMeshFromRectangularGrid(nx, Lx)

    femSpace = FemTools.C0_AffineLinearOnSimplexWithNodalBasis(mesh)

    FemPhi0 = FemTools.FiniteElementFunction(femSpace, name="phi0")
    FemPhi0p = FemTools.FiniteElementFunction(femSpace, name="phi0p")
    FemPhi0m = FemTools.FiniteElementFunction(femSpace, name="phi0m")
    FemTp = FemTools.FiniteElementFunction(femSpace, name="Tp")
    FemTm = FemTools.FiniteElementFunction(femSpace, name="Tm")

    phi0 = FemPhi0.dof
    phi0p = FemPhi0p.dof
    phi0m = FemPhi0m.dof
    Tp = FemTp.dof
    Tm = FemTm.dof

    icout = open("phi0.dat", 'w')

    #construct initial level set, short cut assuming dofs <--> node numbers
    for I in range(mesh.nNodes_global):
        x = mesh.nodeArray[I, 0]
        phi0[I] = initFunc(x)
        phi0p[I] = max(phi0[I], 0.0)
        phi0m[I] = abs(min(phi0[I], 0.0))
        icout.write("%g %g \n" % (x, phi0[I]))
    #
    failed = False
    nd = 1
    nodalSpeeds = numpy.ones((mesh.nNodes_global, ), 'd')
    if method == 'FSW':
        solver = cfmmfsw.FSWEikonalSolver(nd,
                                          mesh.cmesh,
                                          atol=1.0e-8,
                                          rtol=1.0e-8,
                                          maxIts=100,
                                          initFlag=0)
        print "calling FSWEikonalSolver.solve for + ..."
        failed = solver.solve(FemPhi0p.dof,
                              nodalSpeeds,
                              FemTp.dof,
                              initFlag=0,
                              verbose=verbose)
        print "back. calling FSWEikonalSolver.solve for - ..."
        failed = solver.solve(FemPhi0m.dof,
                              nodalSpeeds,
                              FemTm.dof,
                              initFlag=0,
                              verbose=verbose)
        print "back."
    else:
        solver = cfmmfsw.FMMEikonalSolver(nd, mesh.cmesh)
        print "calling FMMEikonalSolver.solve for + ..."
        failed = solver.solve(FemPhi0p.dof,
                              nodalSpeeds,
                              FemTp.dof,
                              initFlag=0,
                              verbose=verbose)
        print "back. calling FMMEikonalSolver.solve for - ..."
        failed = solver.solve(FemPhi0m.dof,
                              nodalSpeeds,
                              FemTm.dof,
                              initFlag=0,
                              verbose=verbose)
        print "back."

    fout = open("T.dat", 'w')
    phout = open("phi.dat", 'w')
    for I in range(mesh.nNodes_global):
        x = mesh.nodeArray[I, 0]
        fout.write("%g %g \n" % (x, Tp[I]))
        phout.write("%g %g \n" % (x, Tp[I] - Tm[I]))

    icout.close()
    fout.close()
    phout.close()