def setup(): vertices = numpy.zeros((8, 2)) vertices[0] = [0, 0] for i in range(0, 7): vertices[i + 1] = [ math.cos(cornerAngle / 6 * math.pi / 180 * i), math.sin(cornerAngle / 6 * math.pi / 180 * i) ] triangles = numpy.array([[2, 1, 0], [0, 3, 2], [4, 3, 0], [0, 5, 4], [6, 5, 0], [0, 7, 6]]) domain = {"vertices": vertices, "simplices": triangles} gridView = adaptiveGridView(leafGridView(domain)) gridView.hierarchicalGrid.globalRefine(2) space = solutionSpace(gridView, order=order) from dune.fem.scheme import galerkin as solutionScheme u = TrialFunction(space) v = TestFunction(space) x = SpatialCoordinate(space.cell()) # exact solution for this angle Phi = cornerAngle / 180 * pi phi = atan_2(x[1], x[0]) + conditional(x[1] < 0, 2 * pi, 0) exact = dot(x, x)**(pi / 2 / Phi) * sin(pi / Phi * phi) a = dot(grad(u), grad(v)) * dx # set up the scheme laplace = solutionScheme( [a == 0, DirichletBC(space, exact, 1)], solver="cg", parameters={"newton.linear.preconditioning.method": "jacobi"}) uh = space.interpolate(0, name="solution") return uh, exact, laplace
T = 3 dt = 0.005 t = 0 # <markdowncell> # We use *gmsh* to define the domain and then set up a first order # scalar Lagrange space which we will use both for $\psi$ and $p$. We can # construct general grids by either defining the grids directly in Python # (as demonstrated in the following example) or by reading the grid from # files using readers provided by Dune, e.g., Dune Grid Format (dgf) files # or Gmsh files. # <codecell> domain = (reader.gmsh, "wave_tank.msh") gridView = leafGridView( domain, dimgrid=2 ) gridView.hierarchicalGrid.loadBalance() V = solutionSpace(gridView, order=1, storage="fem") p = V.interpolate(0,name="p") phi = V.interpolate(0,name="phi") pVec = p.as_numpy phiVec = phi.as_numpy # <markdowncell> # Next we define an operator for the stiffness matrix including the # boundary condition which are time dependent so we use a `Constant` for # this. We use the `BoxDirichletBC` class which is derived from the more # general `DirichletBC` class which takes a function space, the boundary # function $g$ as a ufl expression, and finally a description of the part
import ufl from ufl import grad, dot, dx, inner, Identity, sqrt, sin, pi, cos import dune.ufl import dune.grid import dune.fem from dune.alugrid import aluConformGrid as leafGridView from dune.fem.view import geometryGridView from dune.fem.space import lagrange as solutionSpace from dune.fem.scheme import galerkin as solutionScheme order = 3 storage = "istl" # setup reference surface referenceView = leafGridView("sphere.dgf", dimgrid=2, dimworld=3) space = solutionSpace(referenceView, dimRange=referenceView.dimWorld, order=order, storage=storage) # setup deformed surface x = ufl.SpatialCoordinate(space) # positions = space.interpolate(x, name="position") positions = space.interpolate( x * (1 + 0.5 * sin(2 * pi * (x[0] + x[1])) * cos(0.25 * pi * x[2])), name="position") gridView = geometryGridView(positions) space = solutionSpace(gridView, dimRange=gridView.dimWorld, order=order, storage=storage)
# <codecell> import time import dune.fem as fem from dune.grid import cartesianDomain from dune.alugrid import aluConformGrid as leafGridView from dune.fem.view import adaptiveLeafGridView as adaptiveGridView from dune.fem.space import lagrange as solutionSpace fem.parameter.append({"fem.verboserank": -1}) order = 1 dimDomain = 2 # we are solving this in 2D dimRange = 2 # we have a system with two unknowns domain = cartesianDomain( [4, 4], [8, 8], [40, 40]) # fails with 20x20 in adapt due to petsc error gridView = adaptiveGridView(leafGridView(domain, dimgrid=dimDomain)) space = solutionSpace(gridView, dimRange=dimRange, order=order, storage="petscadapt") # <markdowncell> # We want to solve the following system of equations of variables $\phi$ (phase field) and $T$ (temperature field) # # \begin{gather*} # \tau \frac{\partial \phi}{\partial t} = \nabla \cdot D \nabla \phi + \phi(1-\phi)m(\phi, T), \\ # \frac{\partial T}{\partial t} = D_T \nabla^2 T + \frac{\partial \phi}{\partial t}, # \end{gather*} # # where $D_T$ = 2.25, m is given by #
from ufl import * from dune.fem import spaceAdapt, adapt from dune.grid import cartesianDomain, gridFunction from dune.alugrid import aluConformGrid as leafGridView from dune.fem.view import adaptiveLeafGridView as adaptiveGridView from dune.fem import parameter, doerflerMark, globalRefine from dune.fem.function import levelFunction from dune.ufl import Space import dune.create as create parameter.append({"fem.verboserank": 0}) domain = cartesianDomain([0, 0], [1, 1], [16, 16]) grid = adaptiveGridView(leafGridView(domain)) space = create.space("lagrangehp", grid, maxOrder=4, storage="istl") u = TrialFunction(space) v = TestFunction(space) x = SpatialCoordinate(space) n = FacetNormal(space) mu = 20 * 16 hT = MaxCellEdgeLength(space) hS = avg(MaxFacetEdgeLength(space)) hs = MaxFacetEdgeLength(space)('+') diffusiveFlux = lambda w, d: d source = -sin(pi * x[0]) * sin(6 * pi * x[1]) a = (inner(diffusiveFlux(u, grad(u)), grad(v)) + source * v) * dx
from ufl import SpatialCoordinate, dot from dune.grid import cartesianDomain from dune.alugrid import aluConformGrid as leafGridView from dune.fem.view import filteredGridView from dune.fem.space import lagrange gridView = leafGridView( cartesianDomain([0,0],[1,1],[16,16]) ) filteredView = filteredGridView(gridView, lambda e: e.geometry.center.two_norm > 0.5, domainId=1) space = lagrange(filteredView, order=2) x = SpatialCoordinate(space) solution = space.interpolate(dot(x,x),name="solution") solution.plot() print("number of dofs:", solution.size,\ "integral over filtered domain",solution.integrate()) filteredView = filteredGridView(gridView, lambda e: e.geometry.center.two_norm < 0.5, domainId=1, useFilteredIndexSet=True) space = lagrange(filteredView, order=2) x = SpatialCoordinate(space) solution = space.interpolate(dot(x,x),name="solution") solution.plot() print("number of dofs:", solution.size,\ "integral over filtered domain",solution.integrate()) space = lagrange(gridView, order=2) solution = space.interpolate(dot(x,x),name="solution") solution.plot() print("number of dofs:", solution.size,\ "integral over filtered domain",solution.integrate())
inner, dot, div, grad, dx, as_vector, transpose, Identity from dune.ufl import Constant, DirichletBC from dune.fem.space import lagrange as lagrangeSpace from dune.fem.operator import galerkin as galerkinOperator from dune.fem.operator import linear as linearOperator from dune.fem.scheme import galerkin as galerkinScheme from dune.fem import parameter parameter.append({"fem.verboserank": 0}) order = 2 # Note: structuredGrid fails in precon step! # grid = structuredGrid([0,0],[3,1],[30,10]) from dune.alugrid import aluCubeGrid as leafGridView grid = leafGridView( cartesianDomain([0,0],[3,1],[30,10]) ) spcU = lagrangeSpace(grid, dimRange=grid.dimension, order=order, storage="petsc") spcP = lagrangeSpace(grid, dimRange=1, order=order-1, storage="petsc") cell = spcU.cell() x = SpatialCoordinate(cell) mu = Constant(0.1, "mu") nu = Constant(0.01, "nu") u = TrialFunction(spcU) v = TestFunction(spcU) p = TrialFunction(spcP) q = TestFunction(spcP) exact_u = as_vector( [x[1] * (1.-x[1]), 0] ) exact_p = as_vector( [ (-2*x[0] + 2)*mu ] ) f = as_vector( [0,]*grid.dimension )