print "dt = ", dt 
print "Mesh size = ", mesh.Diam
raw_input('Press any key to start time loop')
        
NSteps = 200
u_old = u0
err = []
loop = True # turn this off if you already have the data and just want to plot
if loop:
        for i in range(NSteps):
                print 'Timestep: ', i
                pde.u = u_old
                M_free, vecs = pde.AssembPDE()
                F = vecs[0]; G = vecs[1]
                x_old = pde.wrapSol(u_old)
                rhs = linsolv.mul(a = M_free, b = x_old) - dt*linsolv.mul(a = W_free, b = x_old) + dt*F + dt*G 
                x_new = pde.Solve(M_free, rhs)
                u_new = pde.unwrapSol(x_new)
                        
                # write to file and update
                np.savetxt(outfile_fmt%(i+1), u_new)     
                err.append(abs(u_new[:,0] - u_old[:,0]).max())
                u_old = u_new
                  
# pattern animations
print '\nTime stepping ended...'
raw_input('Press any key to start animation...')
p = fem.Plot(Mesh = mesh)
fig = plt.figure(figsize = (7,5), facecolor = 'w', edgecolor = 'w')
datafilelist = []
[datafilelist.append(outfile_fmt % x) for x in range(NSteps)]
Example #2
0
def testLinDiff():
    # init mesh
    K0 = importInitMesh(matfile=initmeshfile)
    m = Mesh(K0)

    # set zero Neumann boundary conditions
    m.NeumannEdges = m.BoundaryEdges
    m.NumNeumannEdges = len(m.NeumannEdges)

    def gNeumann(Mesh=m):
        return [lambda p: 0.]

    # get boundary partition
    m.partitionNodes()

    # assemble stiffness and mass matrices
    a = Assemb(Mesh=m)
    a.AssembStiffMat()
    a.AssembMassMat()
    K = a.globalStiffMat
    M = a.globalMassMat

    # set source function
    def fsrc(Mesh=m):
        return [lambda p, m, u: 0.]

    # assemble LHS of final lin. alg problem
    SDC = 100.  # Diffusion coefficient bumped up for quick diffusion

    def makeBlockMat(x):
        return SDC * x

    def getlhs(K, M):
        return M

    # define the PDE
    pde = Parabolic(Mesh=m, StiffMat=a.globalStiffMat, MassMat=a.globalMassMat)
    pde.setNeumannFunc = gNeumann
    pde.setSrcFunc = fsrc
    pde.AssembBlockStiffMat = makeBlockMat
    pde.AssembBlockMassMat = makeBlockMat
    pde.AssembLHS = getlhs
    import linsolv
    K_free = makeBlockMat(pde.getFreeNodeArray(K))

    # initial condition
    f0 = [lambda p: 1. if np.sqrt(p[0]**2 + p[1]**2) <= 0.1 else 0.]
    u0 = np.zeros([m.NumNodes, 1])
    for i, node in enumerate(m.Nodes):
        u0[i, 0] = f0[0](node)

    # start time loop
    outfile_fmt = os.path.join(testdata_dir, 'testdiff%d.dat')
    np.savetxt(outfile_fmt % 0, u0)
    dt = 0.5e-3

    print "dt = ", dt
    print "Mesh size = ", m.Diam
    print "Characteristic time step # = ", int((1. / SDC) / dt)
    raw_input('Press any key to start time loop')

    NSteps = 100
    u_old = u0
    err = []
    loop = True
    if loop:
        for i in range(NSteps):
            print 'Timestep: ', i
            pde.u = u_old
            M_free, vecs = pde.AssembPDE()
            F = vecs[0]
            G = vecs[1]
            D = vecs[2]
            x_old = pde.wrapSol(u_old)
            rhs = linsolv.mul(a = M_free, b = x_old) - dt * linsolv.mul(a = K_free, b = x_old) + dt * F + \
                  dt * G - linsolv.mul(a = M_free, b = D)
            x_new = pde.Solve(M_free, rhs)
            u_new = pde.unwrapSol(x_new)

            # write to file and update
            np.savetxt(outfile_fmt % (i + 1), u_new)
            err.append(abs(x_new - x_old).max())
            u_old = u_new

    # pattern animations
    print '\nTime stepping ended...'
    raw_input('Press any key to start animation...')
    p = Plot(Mesh=m)
    fig = plt.figure(figsize=(7, 5), facecolor='w', edgecolor='w')
    datafilelist = []
    [datafilelist.append(outfile_fmt % x) for x in range(NSteps)]
    ax = fig.add_subplot(1, 1, 1)
    p.ax = ax
    p.patternAnimate(dataFileList=datafilelist,
                     Component=0,
                     delay=0.01,
                     frameRate=10,
                     Prefix='../testdata/testdiff')

    # plot convergence
    fig_err = plt.figure(figsize=(7, 5), facecolor='w', edgecolor='w')
    ax_err = fig_err.add_subplot(1, 1, 1)
    ax_err.set_xlabel('Timestep', fontsize='xx-large')
    ax_err.set_ylabel('Convergence to steady state', fontsize='xx-large')
    ax_err.plot(err)
Example #3
0
def testLinDiff():
    # init mesh
    K0 = importInitMesh(matfile=initmeshfile)
    m = Mesh(K0)

    # set zero Neumann boundary conditions
    m.NeumannEdges = m.BoundaryEdges
    m.NumNeumannEdges = len(m.NeumannEdges)

    def gNeumann(Mesh=m):
        return [lambda p: 0.0]

    # get boundary partition
    m.partitionNodes()

    # assemble stiffness and mass matrices
    a = Assemb(Mesh=m)
    a.AssembStiffMat()
    a.AssembMassMat()
    K = a.globalStiffMat
    M = a.globalMassMat

    # set source function
    def fsrc(Mesh=m):
        return [lambda p, m, u: 0.0]

    # assemble LHS of final lin. alg problem
    SDC = 100.0  # Diffusion coefficient bumped up for quick diffusion

    def makeBlockMat(x):
        return SDC * x

    def getlhs(K, M):
        return M

    # define the PDE
    pde = Parabolic(Mesh=m, StiffMat=a.globalStiffMat, MassMat=a.globalMassMat)
    pde.setNeumannFunc = gNeumann
    pde.setSrcFunc = fsrc
    pde.AssembBlockStiffMat = makeBlockMat
    pde.AssembBlockMassMat = makeBlockMat
    pde.AssembLHS = getlhs
    import linsolv

    K_free = makeBlockMat(pde.getFreeNodeArray(K))

    # initial condition
    f0 = [lambda p: 1.0 if np.sqrt(p[0] ** 2 + p[1] ** 2) <= 0.1 else 0.0]
    u0 = np.zeros([m.NumNodes, 1])
    for i, node in enumerate(m.Nodes):
        u0[i, 0] = f0[0](node)

    # start time loop
    outfile_fmt = os.path.join(testdata_dir, "testdiff%d.dat")
    np.savetxt(outfile_fmt % 0, u0)
    dt = 0.5e-3

    print "dt = ", dt
    print "Mesh size = ", m.Diam
    print "Characteristic time step # = ", int((1.0 / SDC) / dt)
    raw_input("Press any key to start time loop")

    NSteps = 100
    u_old = u0
    err = []
    loop = True
    if loop:
        for i in range(NSteps):
            print "Timestep: ", i
            pde.u = u_old
            M_free, vecs = pde.AssembPDE()
            F = vecs[0]
            G = vecs[1]
            D = vecs[2]
            x_old = pde.wrapSol(u_old)
            rhs = (
                linsolv.mul(a=M_free, b=x_old)
                - dt * linsolv.mul(a=K_free, b=x_old)
                + dt * F
                + dt * G
                - linsolv.mul(a=M_free, b=D)
            )
            x_new = pde.Solve(M_free, rhs)
            u_new = pde.unwrapSol(x_new)

            # write to file and update
            np.savetxt(outfile_fmt % (i + 1), u_new)
            err.append(abs(x_new - x_old).max())
            u_old = u_new

    # pattern animations
    print "\nTime stepping ended..."
    raw_input("Press any key to start animation...")
    p = Plot(Mesh=m)
    fig = plt.figure(figsize=(7, 5), facecolor="w", edgecolor="w")
    datafilelist = []
    [datafilelist.append(outfile_fmt % x) for x in range(NSteps)]
    ax = fig.add_subplot(1, 1, 1)
    p.ax = ax
    p.patternAnimate(dataFileList=datafilelist, Component=0, delay=0.01, frameRate=10, Prefix="../testdata/testdiff")

    # plot convergence
    fig_err = plt.figure(figsize=(7, 5), facecolor="w", edgecolor="w")
    ax_err = fig_err.add_subplot(1, 1, 1)
    ax_err.set_xlabel("Timestep", fontsize="xx-large")
    ax_err.set_ylabel("Convergence to steady state", fontsize="xx-large")
    ax_err.plot(err)