Exemplo n.º 1
0
def adaptiveLeafGridView(grid, *args, **kwargs):
    """create an adaptive view of the leaf grid

    Args:
        grid:  grid to create the adaptive view for.
               The grid must either be a hierarchical grid or a leaf view of one.

    Returns:
        GridView: the constructed grid view
    """
    if isinstance(grid, str):
        import dune.create as create
        grid = create.grid(grid, *args, **kwargs)
    else:
        assert args.__len__()==0 and kwargs.__len__()==0,\
            "too many arguments passed to adaptiveLeafGridView method"

    try:
        grid = grid.hierarchicalGrid
    except:
        pass
    gridModule = importlib.import_module(type(grid).__module__)

    if not isinstance(grid, getattr(gridModule, "HierarchicalGrid")):
        raise ValueError(
            'Cannot only create an adaptiveLeafGridView from a DUNE grid.')

    gridPartName = "Dune::Fem::AdaptiveLeafGridPart< " + grid.cppTypeName + " >"
    typeName = gridPartName  # + "::GridViewType"
    includes = grid.cppIncludes + ["dune/fem/gridpart/adaptiveleafgridpart.hh"]

    # Note: AGP are constructed from the hierarchical grid like other grid
    # views so the default ctor can be used
    '''
    constructor = Constructor([grid.cppTypeName + " &grid","int"],
                 ["std::cout << 'hallo\\n';",
                  "Dune::FemPy::detail::addGridModificationListener( grid );",
                  "return Dune::FemPy::constructGridPart<"+gridPartName+">( grid );"],
                 ["pybind11::keep_alive< 1, 2 >()"])
    '''
    return setup(includes, typeName, ctorArgs=[grid])
Exemplo n.º 2
0
    test_istl = False
try:
    import petsc4py
    # test_petsc = True
    test_petsc = False  # issue with petsc state
except:
    test_petsc = False

testLoop = 1
# testLoop = 10
# testLoop = 1000

# grid = create.grid("ALUConform", dune.grid.cartesianDomain([0, 0], [1, 1], [89, 89]), dimgrid=2)
try:
    grid = create.grid("ALUConform",
                       dune.grid.cartesianDomain([0, 0], [1, 1], [9, 9]),
                       dimgrid=2)
except:
    grid = create.grid("Yasp",
                       dune.grid.cartesianDomain([0, 0], [1, 1], [9, 9]),
                       dimgrid=2)

d = 0.001
p = 1.7

uflSpace = Space((2, 2), 1)
u = TrialFunction(uflSpace)
v = TestFunction(uflSpace)
x = SpatialCoordinate(uflSpace.cell())

rhs = (x[0] + x[1]) * v[0]
Exemplo n.º 3
0
############################################

# polynomial order of surface approximation
order = 1

fem.parameter.append({
    "fem.verboserank": 0,
    "fem.solver.verbose": 0,
    "istl.preconditioning.method": "ilu",
    "istl.preconditioning.iterations": 1,
    "istl.preconditioning.relaxation": 1
})

# set up grid

grid = create.grid("ALUSimplex", gridSelect, dimgrid=2, dimworld=3)
grid.hierarchicalGrid.globalRefine(2)
print("Number of elements:", grid.size(0))
print("Number of vertices:", grid.size(grid.dimension))

# set up a lagrange scalar space with polynomial order 2 over that grid
spc = create.space("Lagrange",
                   grid,
                   dimrange=grid.dimWorld,
                   order=order,
                   store="istl")

# non spherical initial surgface

uDimWorld = 6
positions = spc.interpolate(lambda x: surfaceOptions[case](x), name="position")
Exemplo n.º 4
0
import matplotlib

matplotlib.rc('image', cmap='jet')
from matplotlib import pyplot
import numpy
from scipy.sparse import bmat, linalg
import dune.create as create
from dune.grid import cartesianDomain
from ufl import SpatialCoordinate, CellVolume, TrialFunction, TestFunction,\
                inner, dot, div, grad, dx, as_vector, transpose, Identity
from dune.ufl import Constant, DirichletBC
import dune.fem
from dune.fem.operator import linear as linearOperator

order = 2
grid = create.grid("ALUCube",
                   constructor=cartesianDomain([0, 0], [3, 1], [30, 10]))
spcU = create.space("lagrange",
                    grid,
                    dimRange=grid.dimension,
                    order=order,
                    storage="fem")
spcP = create.space("lagrange",
                    grid,
                    dimRange=1,
                    order=order - 1,
                    storage="fem")

cell = spcU.cell()
x = SpatialCoordinate(cell)
mu = Constant(0, "mu")
nu = Constant(0, "nu")
Exemplo n.º 5
0
from ufl import *

import math
import dune.fem
from dune.fem.function import integrate
import dune.create as create
from dune.ufl import DirichletBC, Space

dimRange = 12  # needs to be >= 4, test with 4,8,11
grid = create.grid("ALUConform", "../data/mixed.dgf", dimgrid=2)

from ufl import SpatialCoordinate
uflSpace = dune.ufl.Space(2, dimRange)
x = SpatialCoordinate(uflSpace.cell())
from math import pi, log, sqrt
from ufl import cos, sin, as_vector
exact = as_vector(
    [sin(3 * pi * x[0]), x[1] * x[1], x[0] *
     x[0], cos(3. * pi * x[1])] + [0] * (dimRange - 4))
v1 = integrate(grid, exact, 5).two_norm

space = create.space("Lagrange", grid, dimRange=dimRange, order=1)
u = space.interpolate(exact, name="u")
v2 = integrate(grid, u, 5).two_norm

print(v1, v2, v1 - v2)
v3 = integrate(grid, inner(grad(u[11]), grad(u[11])), 5)
Exemplo n.º 6
0
#create a blockvector which composes of 1 Fieldvector of size 2
vec = BlockVector(2)(10)

# print(vec, len(vec), vec[0], vec[0][0])
#set first element in first field vector equal to one
vec[0][0] = 2
# print(vec, len(vec), vec[0], vec[0][0])

#change element to 1
vec[0][0] = 1

assert (vec[0][0] == 1), "dune.istl blockvector not changing value correctly "
#now test with with other fieldvector created in dune=fempy

view = create.grid("yasp",
                   dune.grid.cartesianDomain([0, 0], [1, 1], [1, 1]),
                   dimgrid=2)

spc = create.space("Lagrange", view, dimRange=2, order=3, storage='istl')

#initialise all to 2
uh = spc.interpolate([2, 3], name="solution")

df = uh.as_istl
# print(df, len(df), df[0], df[0][0])
df[0][0] = 1
# print(df, len(df), df[0], df[0][0])

# print(uh.dofVector[0][0])

#reset equalt to one againt
Exemplo n.º 7
0
# <codecell>
u = TrialFunction(uflSpace)
v = TestFunction(uflSpace)
a = (diffCoeff * inner(grad(u), grad(v)) + massCoeff * dot(u, v)) * dx

# finally the right hand side and the boundary conditions
b = (-div(diffCoeff * grad(exact[0])) + massCoeff * exact[0]) * v[0] * dx
dbc = [dune.ufl.DirichletBC(uflSpace, exact, i + 1) for i in range(4)]

# <markdowncell>
# Now we define a grid build up of voronoi cells around $50$ random points

# <codecell>
constructor = cartesianDomain([-0.5, -0.5], [1, 1], [10, 10])
polyGrid = create.grid(
    "polygrid", voronoiCells(constructor, 50, "voronoiseeds", True, lloyd=100))

# <markdowncell>
# In general we can construct a `polygrid` by providing a dictionary with
# the `vertices` and the `polygons`. The `voronoiCells` function creates
# such a dictonary using random seeds to generate voronoi cells which are
# cut off using the provided `cartesianDomain`. The seeds can be
# provided as list of points as second argument:
# ```
# voronoiCells(constructor, towers, fileName=None, load=False):
# ```
# If a `fileName` is provided the seeds will be written to disc and loaded
# from that file is `load=True`. As an example the output of
# `voronoiCells(constructor,5)` is
# ```
# {'polygons': [ [4, 5, 2, 3], [ 8, 10,  9,  7], [7, 9, 1, 3, 4],
Exemplo n.º 8
0
import dune.create as create

parameter.append({"fem.verboserank": 0})
newtonParameter = {
    "tolerance": 1e-7,
    "verbose": "false",
    "linear.absolutetol": 1e-8,
    "linear.reductiontol": 1e-8,
    "linear.preconditioning.method": "jacobi",
    "linear.preconditioning.iterations": 1,
    "linear.preconditioning.relaxation": 1.2,
    "linear.verbose": "false"
}

grid = create.grid("ALUConform",
                   cartesianDomain([0, 0], [1, 1], [10, 10]),
                   dimgrid=2)
spc = create.space("lagrange", grid, dimRange=1, order=2, storage="istl")

uflSpace = Space((grid.dimGrid, grid.dimWorld), 1)
u = TrialFunction(uflSpace)
v = TestFunction(uflSpace)
x = SpatialCoordinate(uflSpace.cell())
n = FacetNormal(uflSpace.cell())
mu = 1.
# he = MaxFacetEdgeLength(uflSpace.cell())('+') # this is wrong
# hT = MaxCellEdgeLength(uflSpace.cell())
hT = CellVolume(uflSpace.cell())
hF = FacetArea(uflSpace.cell())
# he = FacetArea(uflSpace.cell()) / Min( avg('+'), avg('-') )
heInv = hF / avg(hT)