Exemplo n.º 1
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)
Exemplo n.º 2
0
'''
compute_collision() will compute the collision of all the entities with
a Point while compute_first_collision() will always return its first entry. 
Especially if a point is on an element edge this can be tricky.
You may also want to compare with the Cell.contains(Point) tool.
'''
# Script by Rudy at https://fenicsproject.discourse.group/t/
#           any-function-to-determine-if-the-point-is-in-the-mesh/275/3
import dolfin
from vtkplotter.dolfin import shapes, plot, printc

n = 4
Px = 0.5
Py = 0.5
mesh = dolfin.UnitSquareMesh(n, n)
bbt = mesh.bounding_box_tree()
collisions = bbt.compute_collisions(dolfin.Point(Px, Py))
collisions1st = bbt.compute_first_entity_collision(dolfin.Point(Px, Py))
printc("collisions    : ", collisions)
printc("collisions 1st: ", collisions1st)

for cell in dolfin.cells(mesh):
    contains = cell.contains(dolfin.Point(Px, Py))
    printc("Cell", cell.index(), "contains P:", contains, c=contains)

###########################################
pt = shapes.Point([Px, Py], c='blue')

plot(mesh, pt, text=__doc__)
Exemplo n.º 3
0
def awefem(mesh, t, source_loc=None):

    # Function space
    V = FunctionSpace(mesh, "Lagrange", 1)

    # Boundary condition
    bc = DirichletBC(V, Constant(0), "on_boundary")

    # Trial and test functions
    u = TrialFunction(V)
    v = TestFunction(V)

    # Discretization
    c = 6
    dt = t[1] - t[0]
    u0 = Function(V)  # u0 = uN-1
    u1 = Function(V)  # u1 = uN1

    # Variational formulation
    F = (u - 2 * u1 + u0) * v * dx + (dt * c)**2 * dot(
        grad(u + 2 * u1 + u0) / 4, grad(v)) * dx
    a, L = lhs(F), rhs(F)

    # Solver
    A, b = assemble_system(a, L)
    solver = LUSolver(A, "mumps")
    solver.parameters["symmetric"] = True
    bc.apply(A, b)

    # Solution
    u = Function(V)  # uN+1

    # Source
    if source_loc is None:
        mesh_center = np.mean(mesh.coordinates(), axis=0)
        source_loc = Point(mesh_center)
    else:
        source_loc = Point(source_loc)

    # Time stepping
    printc('~bomb Hit F1 to interrupt.', c='yellow')
    pb = ProgressBar(0, len(t))
    for i, t_ in enumerate(t[1:]):
        pb.print()
        b = assemble(L)
        delta = PointSource(V, source_loc, ricker_source(t_) * dt**2)
        delta.apply(b)
        solver.solve(u.vector(), b)

        u0.assign(u1)
        u1.assign(u)

        if t_ > 0.03:
            plot(
                u,
                warpZfactor=20,  # set elevation along z
                vmin=.0,  # sets a minimum to the color scale
                vmax=0.003,
                cmap='rainbow',  # the color map style
                alpha=1,  # transparency of the mesh
                lw=0.1,  # linewidth of mesh
                scalarbar=None,
                #lighting='plastic',
                #elevation=-.3,
                interactive=0)  # continue execution

    interactive()
Exemplo n.º 4
0
    "scale*(y0 + (x[1]-y0)*cos(theta) - (x[2]-z0)*sin(theta)-x[1])",
    "scale*(z0 + (x[1]-y0)*sin(theta) + (x[2]-z0)*cos(theta)-x[2])",
),
               scale=0.5,
               y0=0.5,
               z0=0.5,
               theta=pi / 4,
               degree=2)
bcl = DirichletBC(V, c, left)
bcr = DirichletBC(V, r, right)

w = TrialFunction(V)  # Incremental displacement
v = TestFunction(V)  # Test function
u = Function(V)  # Solution

solve(inner(grad(w), grad(v)) * dx == inner(c, v) * dx, u, [bcl, bcr])

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

# print out some funny text
printc("""~idea Try out plot options: 
           ~pin color='gold'
           ~pin alpha=0.2, depthpeeling=True
           ~pin mode='mesh warp lines', lw=.05""",
       c='blue')

plot(u, mode='my warped mesh please!!', azimuth=45)

printc('~smile Thanks for using vtkplotter!', c='green')
Exemplo n.º 5
0
    - dt*inner(Constant(1.0),p)
L = (L0 + L1) * dx

# Compute directional derivative about u in the direction of du
a = derivative(L, w, du)

problem = TuringPattern(a, L)
solver = NewtonSolver()
solver.parameters["linear_solver"] = "lu"
solver.parameters["convergence_criterion"] = "incremental"
solver.parameters["relative_tolerance"] = 1e-2

########################################### time steps
from vtkplotter.dolfin import plot, printc

t = 0
printc('~bomb Press Esc to quit.', c='y', invert=True)
while t < T:
    t += dt
    w0.vector()[:] = w.vector()
    solver.solve(problem, w.vector())

    plot(w.split()[0],
         style=4,
         lw=0,
         scalarbar='h',
         text='time: ' + str(t),
         interactive=0)

plot()
Exemplo n.º 6
0
    - dt*inner(Constant(1.0),p)
L = (L0 + L1) * dx

# Compute directional derivative about u in the direction of du
a = derivative(L, w, du)

problem = TuringPattern(a, L)
solver = NewtonSolver()
solver.parameters["linear_solver"] = "lu"
solver.parameters["convergence_criterion"] = "incremental"
solver.parameters["relative_tolerance"] = 1e-2

########################################### time steps
from vtkplotter.dolfin import plot, printc

t = 0
printc('~bomb Press F1 to abort.', c='y', invert=True)
while t < T:
    t += dt
    w0.vector()[:] = w.vector()
    solver.solve(problem, w.vector())

    plot(w.split()[0],
         style=4,
         lw=0,
         scalarbar='h',
         text='time: ' + str(t),
         interactive=0)

plot()