예제 #1
0
# Define variational problem
(u, p) = TrialFunctions(W)
(v, q) = TestFunctions(W)
f = Constant((0, 0))
a = (inner(grad(u), grad(v)) - div(v) * p + q * div(u)) * dx
L = inner(f, v) * dx
w = Function(W)

solve(a == L, w, bcs, solver_parameters={'linear_solver': 'mumps'})
tf = time()
print("time to solve:", tf - t0)

# Split the mixed solution using a shallow copy
(u, p) = w.split()

######################################################## vtkplotter:
f = r'-\nabla \cdot(\nabla u+p I)=f ~\mathrm{in}~\Omega'
formula = Latex(f, pos=(0.55, 0.45, -.05), s=0.1)

plot(u,
     formula,
     at=0,
     N=2,
     text="velocity",
     mode='mesh and arrows',
     scale=.03,
     wire=1,
     scalarbar=False,
     style=1)
plot(p, at=1, text="pressure", cmap='jet')
예제 #2
0
from dolfin import *
from mshr import Circle, generate_mesh
from vtkplotter.dolfin import plot, printc, Latex
# Credits:
# https://github.com/pf4d/fenics_scripts/blob/master/pi_estimate.py

domain = Circle(Point(0.0, 0.0), 1.0)

for res in [2**k for k in range(7)]:
    mesh = generate_mesh(domain, res)
    A = assemble(Constant(1) * dx(domain=mesh))
    printc("resolution = %i, \t A-pi = %.5e" % (res, A - pi))

printc('~pi is about', A, c='yellow')

l = Latex(r'\mathrm{Area}(r)=\pi=\int\int_D 1 \cdot d(x,y)', s=0.3)
l.crop(0.3, 0.3).z(0.1)  # crop invisible top and bottom and set at z=0.1

plot(mesh, l, alpha=0.4, style=1, axes=3)
예제 #3
0
u_0 = Expression('exp(-5*pow(x[0],2) - 5*pow(x[1],2))', degree=2)
u_n = interpolate(u_0, V)

# Define variational problem
u = TrialFunction(V)
v = TestFunction(V)
f = Constant(0)

F = u*v*dx + dt*dot(grad(u), grad(v))*dx - (u_n + dt*f)*v*dx
a, L = lhs(F), rhs(F)

############################################################# vtkplotter
from vtkplotter.dolfin import plot, Latex

f = r'\frac{\partial u}{\partial t}=\nabla^{2} u+f~\mathrm{in}~\Omega\times(0,T]'
formula = Latex(f, pos=(-.2,-1, .1), s=.5, bg='w', alpha=.7)

# Time-stepping
u = Function(V)
for n in range(num_steps):

    # Compute solution
    solve(a == L, u, bc)

    # Plot solution
    plot(u, formula, interactive=False, scalarbar=False)

    # Update previous solution
    u_n.assign(u)

plot(u, interactive=True)
예제 #4
0
# Create mesh and define function space
mesh = UnitSquareMesh(8, 8)
V = FunctionSpace(mesh, "P", 1)

# Define boundary condition
uD = Expression("1 + x[0]*x[0] + 2*x[1]*x[1]", degree=2)
bc = DirichletBC(V, uD, "on_boundary")

# Define variational problem
w = TrialFunction(V)
v = TestFunction(V)
u = Function(V)
f = Constant(-6.0)

# Compute solution
solve(dot(grad(w), grad(v)) * dx == f * v * dx, u, bc)

f = r'-\nabla^{2} u=f'

########################################################### vtkplotter
from vtkplotter.dolfin import plot, Latex, clear

l = Latex(f, s=0.2, c='w').addPos(.6, .6, .1)

plot(u, l, cmap='jet', scalarbar='h', text=__doc__)

# Now show uD values on the boundary of a much finer mesh
clear()
bmesh = BoundaryMesh(UnitSquareMesh(80, 80), "exterior")
plot(uD, bmesh, cmap='cool', ps=5, legend='boundary')  # ps = point size
예제 #5
0
u_0 = Expression('exp(-5*pow(x[0],2) - 5*pow(x[1],2))', degree=2)
u_n = interpolate(u_0, V)

# Define variational problem
u = TrialFunction(V)
v = TestFunction(V)
f = Constant(0)

F = u * v * dx + dt * dot(grad(u), grad(v)) * dx - (u_n + dt * f) * v * dx
a, L = lhs(F), rhs(F)

############################################################# vtkplotter
from vtkplotter.dolfin import plot, Latex

f = r'\frac{\partial u}{\partial t}=\nabla^2 u+f~\mathrm{in}~\Omega\times(0,T]'
formula = Latex(f, pos=(-.4, -.8, .1), s=0.6, c='w')
formula.crop(0.2, 0.4)  # crop top and bottom 20% and 40%

# Time-stepping
u = Function(V)
for n in range(num_steps):

    # Compute solution
    solve(a == L, u, bc)

    # Plot solution
    plot(u, formula, scalarbar=False, interactive=False)

    # Update previous solution
    u_n.assign(u)