예제 #1
0
def BoxMesh(lower, upper, xspacing, yspacing=None, zspaceing=None, **unused):
    from dune.grid import structuredGrid
    if (zspaceing is None):
        if (yspacing is None):
            return structuredGrid(lower, upper, [xspacing])
        else:
            return structuredGrid(lower, upper, [xspacing, yspacing])
    else:
        return structuredGrid(lower, upper, [xspacing, yspacing, zspaceing])
예제 #2
0
def run(restore=False):
    if not restore:
        grid = structuredGrid([0, 0], [1, 1], [2, 2])
        grid.hierarchicalGrid.globalRefine(2)
        checkPointer = CheckPointer("dumpA", grid.hierarchicalGrid)
    else:
        checkPointer = CheckPointer("dumpA")
        grid = checkPointer.hierarchicalGrid().leafView

    space = lagrange(grid)
    df = space.interpolate([0], name="test")
    checkPointer.add(df)

    spc = lagrange(grid, storage='istl')
    df_istl = spc.interpolate([0], name="test_istl")

    # test discrete function assignment
    df.assign(df_istl)
    df_istl.assign(df)

    if not restore:
        print("interpolating grid function")

        @gridFunction(grid, name="gf", order=2)
        def gf(x):
            return x[0] * x[1] * (1 - x[0]) * (1 - x[1])

        df.interpolate(gf)
    else:
        print("restoring discrete function")
        checkPointer.restore()

    # df.plot()
    if not restore:
        checkPointer.backup()
예제 #3
0
def backup():
    from dune.grid import structuredGrid
    grid = structuredGrid([0,0],[1,1],[2,2])
    grid.hierarchicalGrid.globalRefine(2)
    a = numpy.array([1,2,3])

    pickle.dump([a,"hallo",grid.hierarchicalGrid,10], open("dumpA",'wb'))
    return grid
예제 #4
0
import numpy
from dune.fem.plotting import plotPointData as plot
import dune.create as create
from dune.grid import structuredGrid
from dune.istl import blockVector
import ufl

g = structuredGrid([0, 0], [1, 1], [2, 3])

s = create.space("lagrange", g, dimRange=2, storage="istl")
f1 = s.interpolate(expr=[2, 1], name="tmp")
dofs = blockVector(int(s.size / s.localBlockSize), s.localBlockSize)
# f2 = s.function("tmp", expr=[2,1], dofVector=dofs)
f2 = s.function("tmp", dofVector=dofs)
f2.interpolate([2, 1])
assert all([(d1 - d2).two_norm == 0 for d1, d2 in zip(dofs, f1.as_istl)])
assert all([(d1 - d2).two_norm == 0 for d1, d2 in zip(dofs, f2.as_istl)])
operator = create.operator(
    "galerkin",
    ufl.dot(ufl.TrialFunction(s), ufl.TestFunction(s)) * ufl.dx)
f1 = s.function("tmp", [2, 1],
                blockVector(s.size // s.localBlockSize, s.localBlockSize))
f2 = s.function("tmp", [2, 1],
                blockVector(s.size // s.localBlockSize, s.localBlockSize))
operator(f1, f2)

s = create.space("lagrange", g, dimRange=2, storage="numpy")
f1 = s.interpolate([2, 1], name="tmp")
dofs = numpy.ndarray(s.size)
f2 = s.function("tmp", [2, 1], dofs)
assert not (dofs - f1.as_numpy).any()
예제 #5
0
from __future__ import print_function, division
import math
import numpy

from dune.grid import structuredGrid
import dune.create as create

grid = structuredGrid([0, 0], [1, 1], [10, 10])

fvspc = create.space("finitevolume", grid, dimRange=2, storage="numpy")
estimate = fvspc.interpolate([1, 1], name="estimate")

lf = estimate.localFunction(grid.elements.__next__())
y = lf.evaluate([0, 0])
assert y == (1, 1)
# print(y)

lf = estimate.setLocalContribution()
lf.bind(grid.elements.__next__())
y1, y2 = lf[0], lf[1]
assert y1 == 0 and y2 == 0
# print(y1,y2)
lf[0] = 10
y1, y2 = lf[0], lf[1]
assert y1 == 10 and y2 == 0
# print(y1,y2)
lf.unbind()

lf = estimate.localContribution("set")
lf.bind(grid.elements.__next__())
y1, y2 = lf[0], lf[1]
예제 #6
0
from __future__ import print_function, division

from ufl import as_vector, dot, grad, cos, pi, SpatialCoordinate, triangle
from dune.grid import structuredGrid, gridFunction
from dune.fem.space import lagrange, combined, product

x = SpatialCoordinate(triangle)
exact = as_vector([cos(2. * pi * x[0]) * cos(2. * pi * x[1]), dot(x, x)])

grid = structuredGrid([0, 0], [1, 1], [16, 16])
spc1 = lagrange(grid, dimRange=1, order=1)
spc2 = lagrange(grid, dimRange=1, order=2)
test1 = spc1.interpolate(exact[0], name="test")
test2 = spc2.interpolate(exact[1], name="test")
spc = combined(spc1, spc2)
solution = spc.interpolate(exact, name="solution")

space = product(spc1, spc2, components=["p", "s"])
df = space.interpolate(exact, name="df")
# print(df.dofVector.size,solution.dofVector.size,
#       df.components[0].dofVector.size,df.p.dofVector.size,test1.dofVector.size)
assert df.components[0].dofVector.size == test1.dofVector.size
assert df.s.dofVector.size == test2.dofVector.size
assert df.dofVector.size == solution.dofVector.size
df.interpolate(solution)
solution.interpolate(df)
test1.interpolate(df.p)
df.s.interpolate(test2)
df.components[0].interpolate(solution[0])
df.p.interpolate(solution[0])
예제 #7
0
try:
    import matplotlib.pyplot as plt

    plt.ion()
except ImportError:
    print("Warning: Plots are not generated as matplotlib could not be found.")
    plotting = False

########################
# Create grid geometry #
########################

dimension = 2
cells = 20

gridView = structuredGrid([0] * dimension, [1] * dimension,
                          [cells] * dimension)

gridGeometry = GridGeometry(gridView, discMethod="cctpfa")

elementMapper = gridView.indexSet

##############################################
# Define problem (inital/boundary condtions) #
##############################################


@FVProblem(gridGeometry)
class Problem:
    numEq = 1

    def name(self):
예제 #8
0
#!/usr/bin/env python3

from dune.grid import structuredGrid
from dumux.discretization import GridGeometry

gridView = structuredGrid([0, 0], [1, 1], [5, 5])

gridGeometry = GridGeometry(gridView, discMethod="cctpfa")
gridGeometry.update()

print("The total number of scvs is {}".format(gridGeometry.numScv()))
print("The total number of scvfs is {}".format(gridGeometry.numScvf()))

for e in gridView.elements:
    fvGeometry = gridGeometry.localView()
    fvGeometry.bind(e)

    for scv in fvGeometry.scvs():
        print("scv dofIndex: {}".format(scv.dofIndex()))
        print("scv center: {}".format(scv.center()))
        print("scv volume: {}".format(scv.volume()))
예제 #9
0
    includes = problem._includes + ["test/python/test_boundaryloop.hh"]
    typeName = "Dumux::Python::PrintProblemTest<{}>".format(problem._typeName)
    moduleName = moduleName = "printbs_" + hashIt(problem._typeName)
    generator = SimpleGenerator("PrintProblemTest", "Dumux::Python")
    module = generator.load(includes, typeName, moduleName)
    return module.PrintProblemTest(problem)


############################################################
# The actual Python test
############################################################
from dune.grid import structuredGrid
from dumux.discretization import GridGeometry
from dumux.common import BoundaryTypes, FVProblem

gridView = structuredGrid([0, 0, 0], [1, 1, 1], [3, 3, 3])

gridGeometry = GridGeometry(gridView, discMethod="box")


@FVProblem(gridGeometry)
class Problem:
    numEq = 2

    def name(self):
        return "python_problem"

    def boundaryTypes(self, element, scv):
        bTypes = BoundaryTypes(self.numEq)
        bTypes.setDirichlet()
        return bTypes
예제 #10
0
import sys
import time
from dune.grid import structuredGrid, cartesianDomain
from dune.fem.view import adaptiveLeafGridView as gridView

dim = sys.argv[1]

if dim == "2":
    # 2d structured grid
    grid = structuredGrid([0, 0], [1, 1], [4, 4])

    try:
        from dune.alugrid import aluConformGrid, aluSimplexGrid, aluCubeGrid
        domain = cartesianDomain([0, 0], [1, 1], [4, 4])

        # 2d alu conform grid
        grid = gridView(aluConformGrid(domain))
        # 2d alu simplex grid
        grid = gridView(aluSimplexGrid(domain))
        # 2d alu cube grid
        grid = gridView(aluCubeGrid(domain))
    except ImportError:
        print("Cannot import module dune-alugrid, skipping grid creation!")

elif dim == "3":
    # 3d structured grid
    grid = structuredGrid([0, 0, 0], [1, 1, 1], [4, 4, 4])

    try:
        from dune.alugrid import aluConformGrid, aluSimplexGrid, aluCubeGrid
        domain = cartesianDomain([0, 0, 0], [1, 1, 1], [4, 4, 4])
예제 #11
0
import math

# constructed a Cartesian grid
from dune.grid import structuredGrid
grid = structuredGrid([-1,-1],[1,1],[10,10])
print("number of elements of Cartesian grid:",grid.size(0))
grid.plot()

# define a grid function and visualize
from dune.grid import gridFunction
@gridFunction(grid)
def f(x):
    return math.cos(2.*math.pi/(1+abs(x[0]*x[1])))
f.plot()
grid.writeVTK("example", pointdata={"gf":f})

# integrate the grid function using a quadrature rule from dune.geometry
from dune.geometry import quadratureRules
rules = quadratureRules(5)
l2norm2 = 0
for e in grid.elements:
    geo = e.geometry
    for qp in rules(e.type):
        x,w = qp.position, qp.weight
        l2norm2 += f(e,x)**2*w*geo.integrationElement(x)
print("integral of grid function=",math.sqrt(l2norm2))
예제 #12
0
use_codegen = True

import time
from dune.grid import structuredGrid
from dune.fem import parameter
from dune.fem.operator import linear as linearOperator
import dune.create as create
from ufl import TestFunction, TrialFunction, SpatialCoordinate, triangle, exp,\
                dx, grad, inner, as_vector, replace, sqrt
from dune.ufl import Constant

parameter.append({"fem.verboserank": -1})

grid = structuredGrid([0, 0], [1, 1], [40, 40])

order = 4
dimR = 5
quadOrder = 2 * order + 3
spaceName = "lagrange"
if use_codegen:
    space = create.space(spaceName, grid, dimRange=dimR, order=order,
                         interiorQuadratureOrders=[quadOrder, 2*order],\
                         skeletonQuadratureOrders=[quadOrder] )
else:
    space = create.space(spaceName, grid, dimRange=dimR, order=order)

x = SpatialCoordinate(space)

initial = 1 / 2 * (x[0]**2 + x[1]**2) - 1 / 3 * (x[0]**3 - x[1]**3) + 1

u_h = space.interpolate(as_vector([