def RunAcoustic(q):
    Nxy = 100
    tf = 0.1  # Final time
    direction = 0
    u0_expr = Expression(\
    '100*pow(x[i]-.25,2)*pow(x[i]-0.75,2)*(x[i]<=0.75)*(x[i]>=0.25)', i=direction)

    class LeftRight(SubDomain):
        def inside(self, x, on_boundary):
            return (x[direction] < 1e-16 or x[direction] > 1.0 - 1e-16) \
            and on_boundary

    h = 1. / Nxy
    mesh = UnitSquareMesh(Nxy, Nxy, "crossed")
    V = FunctionSpace(mesh, 'Lagrange', q)
    Vl = FunctionSpace(mesh, 'Lagrange', 1)
    Dt = h / (q * 10.)

    Wave = AcousticWave({'V': V, 'Vl': Vl, 'Vr': Vl})
    Wave.verbose = True
    Wave.lump = True
    Wave.set_abc(mesh, LeftRight())
    Wave.update({'lambda':1.0, 'rho':1.0, 't0':0.0, 'tf':tf, 'Dt':Dt,\
    'u0init':interpolate(u0_expr, V), 'utinit':Function(V)})
    sol, tmp = Wave.solve()
def RunAcoustic(q):
    Nxy = 100
    tf = 0.1  # Final time
    direction = 0
    u0_expr = Expression("100*pow(x[i]-.25,2)*pow(x[i]-0.75,2)*(x[i]<=0.75)*(x[i]>=0.25)", i=direction)

    class LeftRight(SubDomain):
        def inside(self, x, on_boundary):
            return (x[direction] < 1e-16 or x[direction] > 1.0 - 1e-16) and on_boundary

    h = 1.0 / Nxy
    mesh = UnitSquareMesh(Nxy, Nxy, "crossed")
    V = FunctionSpace(mesh, "Lagrange", q)
    Vl = FunctionSpace(mesh, "Lagrange", 1)
    Dt = h / (q * 10.0)

    Wave = AcousticWave({"V": V, "Vl": Vl, "Vr": Vl})
    Wave.verbose = True
    Wave.lump = True
    Wave.set_abc(mesh, LeftRight())
    Wave.update(
        {
            "lambda": 1.0,
            "rho": 1.0,
            "t0": 0.0,
            "tf": tf,
            "Dt": Dt,
            "u0init": interpolate(u0_expr, V),
            "utinit": Function(V),
        }
    )
    sol, tmp = Wave.solve()
    Dt = h/(q*5.*c)

    Wave = AcousticWave({'V':V, 'Vl':V, 'Vr':V})
    Wave.timestepper = 'backward'
    Wave.lump = True
    #Wave.verbose = True
    Wave.exact = interpolate(exact_expr, V)
    Wave.update({'lambda':lam, 'rho':rho, 't0':0.0, 'tf':tf, 'Dt':Dt,\
    'u0init':Function(V), 'utinit':Function(V)})
    test = TestFunction(V)
    def srcterm(tt):
        src_expr = source(tt)
        src_vect = assemble(src_expr*test*dx)
        return src_vect.array()
    Wave.ftime = srcterm
    sol, error = Wave.solve()
    ERROR.append(error)
    print 'relative error = {:.5e}'.format(error)

# Convergence order:
CONVORDER = []
for ii in range(len(ERROR)-1):
    CONVORDER.append(np.log(ERROR[ii+1]/ERROR[ii])/np.log((1./NN[ii+1])/(1./NN[ii])))
print '\n\norder of convergence:', CONVORDER

# Save plots:
try:
    boolplot = int(sys.argv[1])
except:
    boolplot = 0
if boolplot > 0:
Пример #4
0
Vex = dl.FunctionSpace(mesh, 'Lagrange', r)
Pt = PointSources(Vex, [[.5, .5]])
mydelta = Pt[0].array()


def mysrc(tt):
    return Ricker(tt) * mydelta


Waveex = AcousticWave({'V': Vex, 'Vm': Vl})
Waveex.timestepper = 'backward'
Waveex.lump = True
Waveex.update({'a':1.0, 'b':1.0, 't0':0.0, 'tf':tf, 'Dt':Dt,\
'u0init':dl.Function(Vex), 'utinit':dl.Function(Vex)})
Waveex.ftime = mysrc
sol, _ = Waveex.solve()
Waveex.exact = dl.Function(Vex)
normex = Waveex.computeabserror()
# plot
myplot.set_varname('u-q' + str(qq))
plotu = dl.Function(Vex)
for index, uu in enumerate(sol):
    if index % boolplot == 0:
        setfct(plotu, uu[0])
        myplot.plot_vtk(plotu, index)
myplot.gather_vtkplots()

print 'Check different spatial sampling'
QQ = [4, 5, 6, 10]
for qq in QQ:
    N = int(qq / cmin)
    def mysrc(tt):
        return Ricker(tt) * mydelta

    # Computation:
    if myrank == 0: print '\n\th = {}, Dt = {}'.format(h, Dt)
    Wave = AcousticWave({'V': V, 'Vm': Vl})
    #Wave.verbose = True
    Wave.timestepper = 'centered'
    Wave.lump = True
    Wave.set_abc(mesh, AllFour(), True)
    Wave.exact = Function(V)
    Wave.update({'b':1.0, 'a':1.0, 't0':0.0, 'tf':tf, 'Dt':Dt,\
    'u0init':Function(V), 'utinit':Function(V)})
    Wave.ftime = mysrc
    sol, error = Wave.solve()
    ERROR.append(error)
    if myrank == 0: print 'relative error = {:.5e}'.format(error)
    if not mycomm == None: MPI.barrier(mycomm)

# Plots:
try:
    boolplot = int(sys.argv[1])
except:
    boolplot = 0
if boolplot > 0:
    filename, ext = splitext(sys.argv[0])
    if myrank == 0:
        if isdir(filename + '/'): rmtree(filename + '/')
    if not mycomm == None: MPI.barrier(mycomm)
    myplot = PlotFenics(filename)

# Computation:
if myrank == 0: print '\n\th = {}, Dt = {}'.format(h, Dt)
Wave = AcousticWave({'V': V, 'Vm': Vl})
#Wave.verbose = True
Wave.timestepper = 'backward'
Wave.lump = True
#Wave.set_abc(mesh, AllFour(), True)
lambda_target = Expression('1.0 + 3.0*(' \
'(x[0]>=0.3)*(x[0]<=0.7)*(x[1]>=0.3)*(x[1]<=0.7))')
lambda_target_fn = interpolate(lambda_target, Vl)
Wave.update({'b':lambda_target_fn, 'a':1.0, \
't0':0.0, 'tf':tf, 'Dt':Dt, 'u0init':Function(V), 'utinit':Function(V)})
Wave.ftime = mysrc
sol, tmp = Wave.solve()
# Observations
myObs = TimeObsPtwise({
    'V': V,
    'Points': [[.5, .2], [.5, .8], [.2, .5], [.8, .5]]
})
Bp = np.zeros((4, len(sol)))
mytimes = np.zeros(len(sol))
solp = Function(V)
for index, pp in enumerate(sol):
    setfct(solp, pp[0])
    Bp[:, index] = myObs.obs(solp)
    mytimes[index] = pp[1]
Bpf = Bp * mytf.evaluate(mytimes)

# Plots:
Пример #7
0
    'Vm': Vl
}, {
    'print': (not mpirank),
    'lumpM': True,
    'timestepper': 'backward'
})
Wave.set_abc(mesh, ABCdom(), lumpD=False)
#Wave.exact = dl.Function(V)
Wave.ftime = mysrc
#
af, bf, _, _, _ = targetmediumparameters(Vl, X, myplot)
#
Wave.update({'b':bf, 'a':af, 't0':0.0, 'tf':tf, 'Dt':Dt,\
'u0init':dl.Function(V), 'utinit':dl.Function(V)})

sol, _, _, error = Wave.solve()
if mpirank == 0: print 'relative error = {:.5e}'.format(error)
MPI.barrier(mesh.mpi_comm())

# Plots:
try:
    boolplot = int(sys.argv[1])
except:
    boolplot = 0
if boolplot > 0:
    myplot.set_varname('p')
    plotp = dl.Function(V)
    for index, pp in enumerate(sol):
        if index % boolplot == 0:
            plotp.vector()[:] = pp[0]
            myplot.plot_vtk(plotp, index)
Dt = 1e-3
checkdt_abc(Dt, h, q, c_max, True, True, 'centered')
if myrank == 0: print '\n\th = {}, Dt = {}'.format(h, Dt)

Wave = AcousticWave({'V':V, 'Vl':Vl, 'Vr':Vl})
#Wave.verbose = True
Wave.timestepper = 'centered'
Wave.lump = True
Wave.set_abc(mesh, LeftRight(), True)
# Medium ppties:
lam_expr = Expression('1.0 + 3.0*(x[i]<=0.25)', i=direction)
lam = interpolate(lam_expr, Vl)
Wave.update({'lambda':lam, 'rho':1.0, 't0':0.0, 'tf':1.5, 'Dt':Dt,\
'u0init':interpolate(u0_expr, V), 'utinit':Function(V)})
Wave.ftime = lambda t: 0.0
sol, tmp = Wave.solve()
if not mycomm == None:  MPI.barrier(mycomm)

# Save plots:
try:
    boolplot = int(sys.argv[1])
except:
    boolplot = 10
if boolplot > 0:
    filename, ext = splitext(sys.argv[0])
    if myrank == 0: 
        if isdir(filename + '/'):   rmtree(filename + '/')
    if not mycomm == None:  MPI.barrier(mycomm)
    myplot = PlotFenics(filename)
    myplot.set_varname('p')
    plotp = Function(V)
Пример #9
0
N = int(qq/cmin)
h = 1./N
mesh = dl.UnitSquareMesh(N,N)
Vl = dl.FunctionSpace(mesh, 'Lagrange', 1)
Vex = dl.FunctionSpace(mesh, 'Lagrange', r)
Pt = PointSources(Vex, [[.5,.5]])
mydelta = Pt[0].array()
def mysrc(tt):
    return Ricker(tt)*mydelta
Waveex = AcousticWave({'V':Vex, 'Vl':Vl, 'Vr':Vl})
Waveex.timestepper = 'backward'
Waveex.lump = True
Waveex.update({'lambda':1.0, 'rho':1.0, 't0':0.0, 'tf':tf, 'Dt':Dt,\
'u0init':dl.Function(Vex), 'utinit':dl.Function(Vex)})
Waveex.ftime = mysrc
sol,_ = Waveex.solve()
Waveex.exact = dl.Function(Vex)
normex = Waveex.computeabserror()
# plot
myplot.set_varname('u-q'+str(qq))
plotu = dl.Function(Vex)
for index, uu in enumerate(sol):
    if index%boolplot == 0:
        setfct(plotu, uu[0])
        myplot.plot_vtk(plotu, index)
myplot.gather_vtkplots()

print 'Check different spatial sampling'
QQ = [4, 5, 6, 10]
for qq in QQ:
    N = int(qq/cmin)