Example #1
0
mesh = dolfin.UnitSquareMesh(4, 4)
domains = dolfin.MeshFunction('size_t', mesh, 2, 0)
half = dolfin.AutoSubDomain(lambda xx, on: xx[0] > 0.5)
half.mark(domains, 1)
facets = dolfin.MeshFunction('size_t', mesh, 1, 0)
left = dolfin.AutoSubDomain(lambda xx, on: on and dolfin.near(xx[0], 0))
right = dolfin.AutoSubDomain(lambda xx, on: on and dolfin.near(xx[0], 1))
front = dolfin.AutoSubDomain(lambda xx, on: on and dolfin.near(xx[1], 0))
back = dolfin.AutoSubDomain(lambda xx, on: on and dolfin.near(xx[1], 1))
left.mark(facets, 1)
right.mark(facets, 2)
front.mark(facets, 3)
back.mark(facets, 4)

h5file = io.H5File('square', 'w')
h5file.set_mesh(mesh)
h5file.add_attribute(domains, 'sides')

problem = problems.PoissonProblem(mesh, domains=domains, facets=facets)
ff = dolfin.Constant(-1.)
uu = problem.assemble_and_solve(ff, [(dolfin.Constant(1), 1),
                                     (dolfin.Constant(-1), 2)])
h5file.add_function_group([uu], 'p_constant')
del problem, ff, uu

coeff = problems.PoissonProblem.default_coefficient
problem = problems.PoissonProblem(mesh,
                                  coefficients=[coeff(),
                                                coeff(aa=100)],
                                  domains=domains,
Example #2
0
head = csg2d.Circle([0, 2], 2, maxh=maxh, eps=eps)
neck = csg2d.Rectangle([-1, -1], [1, 2])
mouth = csg2d.Circle([0, 1], 0.5, maxh=maxh, eps=eps)
face = head + neck - mouth
nose = csg2d.Rectangle([-0.2, 2], [0.2, 3])
eyes = csg2d.Circle([-1, 3], 0.2, maxh=maxh, eps=eps) + csg2d.Circle(
    [1, 3], 0.2, maxh=maxh, eps=eps)

geo = head.getCollection()
geo.add(face)
geo.add(nose, add_boundaries=False)
geo.add(eyes, add_boundaries=False)
geo.generateMesh(0.2)
geo.writeMesh('face')

filea = io.H5File('face', 'r')
print('a cell attributes:', filea.cell_attributes)
print('a facet attributes:', filea.facet_attributes)
domains = filea.read_attribute('domains')
boundaries = filea.read_attribute('boundaries')

space = dolfin.FunctionSpace(filea.mesh, 'CG', 1)
functiona = dolfin.interpolate(dolfin.Expression('x[0]', degree=1), space)
functionb = dolfin.interpolate(dolfin.Expression('x[0]*x[1]', degree=1), space)
vspace = dolfin.VectorFunctionSpace(filea.mesh, 'CG', 1)
vfunctiona = dolfin.interpolate(dolfin.Expression(('x[0]', '0'), degree=1),
                                vspace)
vfunctionb = dolfin.interpolate(dolfin.Expression(('x[1]', '-x[0]'), degree=1),
                                vspace)
filea.close()
half.mark(domains, 1)
facets = dolfin.MeshFunction('size_t', mesh, 2, 0)
left = dolfin.AutoSubDomain(lambda xx, on: on and dolfin.near(xx[0], 0))
right = dolfin.AutoSubDomain(lambda xx, on: on and dolfin.near(xx[0], 1))
front = dolfin.AutoSubDomain(lambda xx, on: on and dolfin.near(xx[1], 0))
back = dolfin.AutoSubDomain(lambda xx, on: on and dolfin.near(xx[1], 1))
bottom = dolfin.AutoSubDomain(lambda xx, on: on and dolfin.near(xx[2], 0))
top = dolfin.AutoSubDomain(lambda xx, on: on and dolfin.near(xx[2], 1))
left.mark(facets, 1)
right.mark(facets, 2)
front.mark(facets, 3)
back.mark(facets, 4)
bottom.mark(facets, 5)
top.mark(facets, 6)

h5file = io.H5File('cube', 'w')
h5file.set_mesh(mesh)
h5file.add_attribute(domains, 'sides')

problem = problems.PoissonProblem(mesh, domains=domains, facets=facets)
ff = dolfin.Constant(-1.)
uu = problem.assemble_and_solve(ff, [(dolfin.Constant(1), 1),
                                     (dolfin.Constant(-1), 2)])
h5file.add_function_group([uu], 'p_constant')
del problem, ff, uu

coeff = problems.PoissonProblem.default_coefficient
problem = problems.PoissonProblem(mesh,
                                  coefficients=[coeff(),
                                                coeff(aa=100)],
                                  domains=domains,
back = dolfin.AutoSubDomain(lambda xx, on: on and dolfin.near(xx[1], 1))
left.mark(facets, 1)
right.mark(facets, 2)
front.mark(facets, 3)
back.mark(facets, 4)

coeff = problems.PoissonProblem.default_coefficient
problem = problems.PoissonProblem(mesh, coefficients = [coeff(), coeff(aa = 100)], domains = domains, facets = facets)
ff = dolfin.Constant(-1.)
gg = [(dolfin.Constant(1), 1), (dolfin.Constant(-1), 2)]

lower = dolfin.MeshFunction('size_t', mesh, 2, 0)
lower_subdomain = dolfin.AutoSubDomain(lambda xx, on: xx[1] > 0)
lower_subdomain.mark(lower, 1)

h5file = io.H5File('whole_2d', 'w')
h5file.set_mesh(mesh)
h5file.add_attribute(lower, 'lower')
uu = problem.assemble_and_solve(ff, gg)
h5file.add_function_group([uu], 'p_variable')
h5file.write_xdmf()
h5file.close()

h5file = io.H5File('lower_2d', 'w')
submesh = dolfin.SubMesh(mesh, lower, 1)
h5file.set_mesh(submesh)
subfacets = dolfin.MeshFunction('size_t', submesh, 1, 0)
left.mark(subfacets, 1)
right.mark(subfacets, 2)
front.mark(subfacets, 3)
back.mark(subfacets, 4)
import sa_thesis.helpers.io as io
import sa_thesis.meshing.csg3d as csg3d
import sa_thesis.computation.problems as problems

cube = csg3d.OrthoBrick([-20, -5, -1], [20, 5, 1])
hole = csg3d.Cylinder([0, 0, -2], [0, 0, 2], 1, eps=1e-1)
layers = csg3d.Layers(cube - hole, [0, 0, -1], [0, 0, 1],
                      3,
                      elements_per_layer=3)
layers.generateMesh(0.3)

mesh = layers.mesh
domains = layers.domains
facets = layers.facets

h5file = io.H5File('3layers', 'w')
h5file.set_mesh(mesh)
h5file.add_attribute(domains, 'layers')

problem = problems.PoissonProblem(mesh, domains=domains, facets=facets)
ff = dolfin.Constant(-1.)
uu = problem.assemble_and_solve(ff, [(dolfin.Constant(1), 5),
                                     (dolfin.Constant(-1), 6)])
h5file.add_function_group([uu], 'p_constant')
del problem, ff, uu

coeff = problems.PoissonProblem.default_coefficient
problem = problems.PoissonProblem(
    mesh,
    coefficients=[coeff(aa=100), coeff(),
                  coeff(aa=100)],