[equation, dbc], solver='cg', parameters={"newton.linear.preconditioning.method": "ilu"}) info = scheme.solve(target=displacement) # <markdowncell> # We can directly plot the magnitude of the displacement field and the stress # <codecell> fig = pyplot.figure(figsize=(20, 10)) displacement.plot(gridLines=None, figure=(fig, 121), colorbar="horizontal") s = sigma(displacement) - (1. / 3) * tr(sigma(displacement)) * Identity(2) von_Mises = sqrt(3. / 2 * inner(s, s)) plot(von_Mises, grid=gridView, gridLines=None, figure=(fig, 122), colorbar="horizontal") pyplot.show() # <markdowncell> # Finally we can plot the actual displaced beam using a grid view that # allows us to add a transformation of the geometry of each entity in the # grid by prociding a grid function to the constructor. Note that this also # allows for higher order transformation like in this case where the # transformation is given by a second order Lagrange discrete function. # We will highlight the flexibility of the # `GeometryGridView` in further # examples: # <codecell> from dune.fem.view import geometryGridView
######################################################### # <markdowncell> # Let us solve over a loop (solve,estimate,mark) and plot the solutions side by side. # <codecell> h1error = dot(grad(uh - exact), grad(uh - exact)) fig = pyplot.figure(figsize=(10, 10)) count = 0 errorVector = [] estimateVector = [] dofs = [] while True: laplace.solve(target=uh) if count % 9 == 8: plot(uh, figure=(fig, 131 + count // 9), colorbar=False) pointFunctional.clear() error = computeFunctional(point, pointFunctional, eh) dual.solve(target=z, rhs=pointFunctional) zh.interpolate(z) estimator(uh, estimate) eta = sum(estimate.dofVector) dofs += [uh.space.size] errorVector += [error] estimateVector += [eta] if count % 3 == 2: print(count, ": size=", uh.space.grid.size(0), "estimate=", eta, "error=", error) if eta < tolerance: break marked = fem.mark(estimate, eta / uh.space.grid.size(0))
a = (inner(u - x, v) + dt * inner(theta*grad(u) + (1 - theta)*I, grad(v))) * dx scheme = solutionScheme(a == 0, space, solver="cg") # <markdowncell> # Now we solve the scheme in time. We first set up the initial time variables, then we plot the initial figure's mesh, and finally we begin the loop, updating ```positions``` on each step and plotting the results side-by-side. # <codecell> count = 0 t = 0. end_time = 0.05 scheme.model.dt = 0.005 fig = pyplot.figure(figsize=(10, 10)) plot(solution, figure=(fig, 131+count%3), colorbar=False, gridLines="", triplot=True) while t < end_time: scheme.solve(target=solution) t += scheme.model.dt count += 1 positions.dofVector.assign(solution.dofVector) if count % 4 == 0: plot(solution, figure=(fig, 131+count%3), colorbar=False, gridLines="", triplot=True) pyplot.show() pyplot.close('all') # <markdowncell> # By chosing an initial surface with boundary we can solve the _soap film_ # problem, i.e., compute the surface of minimum curvature:
from ufl import SpatialCoordinate, triangle x = SpatialCoordinate(triangle) exact = 1/2*(x[0]**2+x[1]**2) - 1/3*(x[0]**3 - x[1]**3) + 1 from dune.fem.function import integrate mass = integrate(gridView, exact, order=5) print(mass) # <markdowncell> # and plot them using matplotlib or write a vtk file for postprocessing # <codecell> from dune.fem.plotting import plotPointData as plot plot(exact, grid=gridView) gridView.writeVTK('exact', pointdata={'exact': exact}) from dune.fem.function import uflFunction exact_gf = uflFunction(gridView, name="ufl", order=1, ufl=exact) mass = 0 for element in gridView.elements: mass += exact_gf(element,[0.5,0.5]) * element.geometry.volume print(mass) # <markdowncell> # ## Discrete Spaces # Setting up a discrete function space and some grid function # <codecell> from dune.fem.space import lagrange as solutionSpace
def plot(obj, **unused): from dune.fem.plotting import plotPointData as plot plot(obj, block=False)