def evolve(nx, C, tmax, xmax=None): xmin = 0.0 if xmax == None: xmax = 1.0 # create a dummy patch to store some info in the same way the MG # solver will myGrid = patch1d.grid1d(nx, ng=1, xmin=xmin, xmax=xmax) # initialize the data phi = myGrid.scratchArray() # initial solution -- this fills the GC too phi[:] = phi_a(myGrid, 0.0) # time info dt = C * 0.5 * myGrid.dx**2 / k t = 0.0 # evolve while (t < tmax): if (t + dt > tmax): dt = tmax - t # create the multigrid object a = multigrid.ccMG1d(nx, xmin=xmin, xmax=xmax, alpha=1.0, beta=0.5 * dt * k, xlBCtype="neumann", xrBCtype="neumann", verbose=0) # initialize the RHS a.initRHS(phi + 0.5 * dt * k * lap(a.solnGrid, phi)) # initialize the solution to 0 a.initZeros() # solve to a relative tolerance of 1.e-11 a.solve(rtol=1.e-11) # get the solution v = a.getSolution() # store the new solution phi[:] = v[:] t += dt return a.solnGrid, phi
def evolve(nx, C, tmax, xmax=None): xmin = 0.0 if xmax == None: xmax = 1.0 # create a dummy patch to store some info in the same way the MG # solver will myGrid = patch1d.grid1d(nx, ng=1, xmin=xmin, xmax=xmax) # initialize the data phi = myGrid.scratchArray() # initial solution -- this fills the GC too phi[:] = phi_a(myGrid, 0.0) # time info dt = C*0.5*myGrid.dx**2/k t = 0.0 # evolve while (t < tmax): if (t + dt > tmax): dt = tmax - t # create the multigrid object a = multigrid.ccMG1d(nx, xmin=xmin, xmax=xmax, alpha = 1.0, beta = 0.5*dt*k, xlBCtype="neumann", xrBCtype="neumann", verbose=0) # initialize the RHS a.initRHS(phi + 0.5*dt*k*lap(a.solnGrid, phi)) # initialize the solution to 0 a.initZeros() # solve to a relative tolerance of 1.e-11 a.solve(rtol=1.e-11) # get the solution v = a.getSolution() # store the new solution phi[:] = v[:] t += dt return a.solnGrid, phi
def mgsolve(nx): # create the multigrid object a = multigrid.ccMG1d(nx, xlBCtype="dirichlet", xrBCtype="dirichlet", verbose=0) # initialize the solution to 0 a.initSolution(a.solnGrid.scratchArray()) # initialize the RHS using the function f a.initRHS(f(a.x)) # solve to a relative tolerance of 1.e-11 a.solve(rtol=1.e-11) # get the solution v = a.getSolution() # compute the error from the analytic solution return error(a.solnGrid, v - true(a.x))
def smoothRun(nx): # create the multigrid object a = multigrid.ccMG1d(nx, xlBCtype="dirichlet", xrBCtype="dirichlet", verbose=0) # initialize the solution to 0 a.initSolution(a.solnGrid.scratchArray()) # initialize the RHS using the function f a.initRHS(f(a.x)) # smooth n = numpy.arange(20000) + 1 e = [] r = [] for i in n: # do 1 smoothing at the finest level a.smooth(a.nlevels - 1, 1) # compute the true error (wrt the analytic solution) v = a.getSolution() e.append(error(a.solnGrid, v - true(a.x))) # compute the residual a.computeResidual(a.nlevels - 1) r.append(error(a.solnGrid, a.grids[a.nlevels - 1].getVarPtr("r"))) r = numpy.array(r) e = numpy.array(e) return n, r, e
# normalize return numpy.sqrt(myg.dx * numpy.sum((r[myg.ilo:myg.ihi + 1]**2))) # the righthand side def f(x): return numpy.sin(x) # test the multigrid solver nx = 64 # create the multigrid object a = multigrid.ccMG1d(nx, xlBCtype="dirichlet", xrBCtype="dirichlet", verbose=1) # initialize the solution to 0 init = a.solnGrid.scratchArray() a.initSolution(init) # initialize the RHS using the function f rhs = f(a.x) a.initRHS(rhs) # solve to a relative tolerance of 1.e-11 a.solve(rtol=1.e-11) # alternately, we can just use smoothing by uncommenting the following
# L2 norm of elements in r, multiplied by dx to # normalize return numpy.sqrt(myg.dx * numpy.sum((r[myg.ilo:myg.ihi + 1]**2))) # the righthand side def f(x): return numpy.sin(x) # test the multigrid solver nx = 64 # create the multigrid object a = multigrid.ccMG1d(nx, xlBCtype="dirichlet", xrBCtype="dirichlet", verbose=1) # initialize the solution to 0 init = a.solnGrid.scratchArray() a.initSolution(init) # initialize the RHS using the function f rhs = f(a.x) a.initRHS(rhs) # solve to a relative tolerance of 1.e-11 a.solve(rtol=1.e-11) # alternately, we can just use smoothing by uncommenting the following # a.smooth(a.nlevels-1,50000)