def set_boundary_conditions(self): if self._space_dim == 2: # boundary conditions gamma01 = PointSubDomain((0.0, 0.0), tol=1e-10) gamma02 = dlfn.CompiledSubDomain("near(x[1], 0.0)") self._bcs = [ (DisplacementBCType.fixed_pointwise, gamma01, None), (DisplacementBCType.fixed_component_pointwise, gamma02, 1, None), (DisplacementBCType.constant_component, CylinderBoundaryMarkers.top.value, 1, self._top_displacement) ] if self._space_dim == 3: # boundary conditions gamma01 = PointSubDomain((0.0, 0.0, 0.0), tol=1e-10) gamma02 = dlfn.CompiledSubDomain("near(x[2], 0.0)") gamma03 = PointSubDomain((0.0, 0.1, 0.0), tol=1e-10) self._bcs = [ (DisplacementBCType.fixed_pointwise, gamma01, None), (DisplacementBCType.fixed_component_pointwise, gamma02, 2, None), (DisplacementBCType.fixed_component_pointwise, gamma03, 0, None), (DisplacementBCType.constant_component, CylinderBoundaryMarkers.top.value, 2, self._top_displacement) ]
def __init__(self, pointa, pointb, *, eps = csg_eps): eps *= numpy.linalg.norm(numpy.array(pointb)-numpy.array(pointa)) super().__init__(mshr.Rectangle(dolfin.Point(*pointa), dolfin.Point(*pointb)), csg_boundaries = [ dolfin.CompiledSubDomain('on_boundary && near(x[0], xx, eps)', xx = pointa[0], eps = eps), dolfin.CompiledSubDomain('on_boundary && near(x[0], xx, eps)', xx = pointb[0], eps = eps), dolfin.CompiledSubDomain('on_boundary && near(x[1], xx, eps)', xx = pointa[1], eps = eps), dolfin.CompiledSubDomain('on_boundary && near(x[1], xx, eps)', xx = pointb[1], eps = eps) ])
def get_2d_rectangular_mesh_boundaries(xl, xr, yb, yt): left_bndry = dl.CompiledSubDomain( "near(x[0],%e)&&on_boundary" % xl) right_bndry = dl.CompiledSubDomain( "near(x[0],%e)&&on_boundary" % xr) bottom_bndry = dl.CompiledSubDomain( "near(x[1],%e)&&on_boundary" % yb) top_bndry = dl.CompiledSubDomain( "near(x[1],%e)&&on_boundary" % yt) return left_bndry, right_bndry, bottom_bndry, top_bndry
def test_centers(self): mesh = df.UnitCubeMesh(4, 4, 4) facet_f = df.MeshFunction('size_t', mesh, 2, 0) df.CompiledSubDomain('near(x[0], 0)').mark(facet_f, 1) df.CompiledSubDomain('near(x[0], 1)').mark(facet_f, 2) x = np.array(get_geom_centers(facet_f, (1, 2))) y = np.array([[0, 0.5, 0.5], [1, 0.5, 0.5]]) self.assertTrue(np.linalg.norm(x - y) < 1E-13)
def test_from_DG0(self): def main(n, chi, measure, restrict): mesh = df.UnitCubeMesh(2, n, n) facet_f = df.MeshFunction('size_t', mesh, 2, 0) chi.mark(facet_f, 1) submesh = EmbeddedMesh(facet_f, 1) transfer = SubMeshTransfer(mesh, submesh) V = df.FunctionSpace(mesh, 'Discontinuous Lagrange Trace', 0) Vsub = df.FunctionSpace(submesh, 'DG', 0) to_V = transfer.compute_map(V, Vsub, strict=False) # Set degree 0 to get the quad order right f = df.Expression('x[0] + 2*x[1] - x[2]', degree=0) fsub = df.interpolate(f, Vsub) fV = df.Function(V) to_V(fV, fsub) y = V.tabulate_dof_coordinates().reshape((V.dim(), -1)) x = Vsub.tabulate_dof_coordinates().reshape((Vsub.dim(), -1)) # Correspondence of coordinates self.assertTrue(np.linalg.norm(y[transfer.cache] - x) < 1E-13) # These are all the coordinates idx = list(set(range(V.dim())) - set(transfer.cache)) self.assertTrue(not any(chi.inside(xi, True) for xi in y[idx])) dS_ = df.Measure(measure, domain=mesh, subdomain_data=facet_f) # Stange that this is not exact error = df.inner(restrict(fV - f), restrict(fV - f))*dS_(1) error = df.sqrt(abs(df.assemble(error, form_compiler_parameters={'quadrature_degree': 0}))) return error # Trick domains boundary beacuse we want to use inside properly? bdry = df.CompiledSubDomain(' || '.join(['near(x[0]*(1-x[0]), 0)', 'near(x[1]*(1-x[1]), 0)', 'near(x[2]*(1-x[2]), 0)'])) inputs = ((df.CompiledSubDomain('near(x[0], 0.5)'), 'dS', df.avg), (bdry, 'ds', lambda x: x)) for chi, measure, restrict in inputs: errors = [] for n in (2, 4, 8, 16): e = main(n, chi, measure, restrict) self.assertTrue(not errors or e < errors[-1]) errors.append(e)
def boundaries_of_rectangle_mesh(mesh): x0, y0 = mesh.coordinates().min(0) x1, y1 = mesh.coordinates().max(0) atol_x = (x1 - x0) * EPS atol_y = (y1 - y0) * EPS bot = dolfin.CompiledSubDomain('x[1] < y0 && on_boundary', y0=y0+atol_y) rhs = dolfin.CompiledSubDomain('x[0] > x1 && on_boundary', x1=x1-atol_x) top = dolfin.CompiledSubDomain('x[1] > y1 && on_boundary', y1=y1-atol_y) lhs = dolfin.CompiledSubDomain('x[0] < x0 && on_boundary', x0=x0+atol_x) return bot, rhs, top, lhs
def get_brain(case_id, conductivity) -> CoupledBrainModel: time_constant = df.Constant(0) mesh, cell_function, interface_function = get_mesh("rose_meshes", "rose") cell_tags = CellTags(CSF=3, GM=2, WM=1, Kinf=4) interface_tags = InterfaceTags(skull=None, CSF_GM=None, GM_WM=None, CSF=None, GM=None, WM=None) test_cell_function = df.MeshFunction("size_t", mesh, mesh.geometric_dimension()) test_cell_function.set_all(0) df.CompiledSubDomain("x[1] >= -pi/16*x[0]").mark(test_cell_function, 4) if case_id == 1: df.CompiledSubDomain("x[1] >= 25*pi/16*x[0]").mark(test_cell_function, 2) elif case_id == 2: df.CompiledSubDomain("x[1] >= 7*pi/32*x[0]").mark(test_cell_function, 2) cell_function.array()[(cell_function.array() == 2) & (test_cell_function.array() == 4)] = 4 Chi = 1.26e3 # 1/cm -- Dougherty 2015 Cm = 1.0 # muF/cm^2 -- Dougherty 2015 Mi_dict = { 3: df.Constant(1e-12), # Set to zero? 1e-12 1: df.Constant(conductivity), # Dlougherty isotropic WM intracellular conductivity 1.0 [mS/cm] 2: df.Constant(conductivity), # Dlougherty isotropic GM intracellular conductivity 1.0 [mS/cm] 4: df.Constant(conductivity), # Dlougherty isotropic GM intracellular conductivity 1.0 [mS/cm] } Me_dict = { 3: df.Constant(16.54), # Dougherty isotropic CSF conductivity 16.54 [mS/cm] 16.54 1: df.Constant(1.26), # 1.26 # Dougherty isotropic WM extracellular conductivity 1.26 [mS/cm] 2: df.Constant(2.76), # 2.76 # Dougherty isotropic GM extracellular conductivity 2.76 [mS/cm] 4: df.Constant(2.76), # 2.76 # Dougherty isotropic GM extracellular conductivity 2.76 [mS/cm] } brain = CoupledBrainModel( time=time_constant, mesh=mesh, cell_model=Cressman(), cell_function=cell_function, cell_tags=cell_tags, interface_function=interface_function, interface_tags=interface_tags, intracellular_conductivity=Mi_dict, other_conductivity=Me_dict, # Either lmbda or extracellular surface_to_volume_factor=Chi, membrane_capacitance=Cm ) return brain
def test3D(self): nx = 10 ny = 15 nz = 20 mesh = dl.UnitCubeMesh(nx, ny, nz) true_sub = dl.CompiledSubDomain("x[0] <= .5") true_marker = dl.MeshFunction('size_t', mesh, 3, value=0) true_sub.mark(true_marker, 1) np_sub_x = np.ones(nx, dtype=np.uint) np_sub_x[nx // 2:] = 0 np_sub_y = np.ones(ny, dtype=np.uint) np_sub_z = np.ones(nz, dtype=np.uint) np_sub_xx, np_sub_yy, np_sub_zz = np.meshgrid(np_sub_x, np_sub_y, np_sub_z, indexing='ij') np_sub = np_sub_xx * np_sub_yy * np_sub_zz h = np.array([1. / nx, 1. / ny, 1. / nz]) marker = dl.MeshFunction('size_t', mesh, 3) numpy2MeshFunction(mesh, h, np_sub, marker) assert_allclose(marker.array(), true_marker.array(), rtol=1e-7, atol=1e-9)
def set_boundary_conditions(self): # boundary conditions self._bcs = [] BCType = DisplacementBCType if self._bc_type == "floating": self._bcs.append((BCType.fixed_component, HyperCubeBoundaryMarkers.left.value, 0, None)) self._bcs.append((BCType.fixed_component, HyperCubeBoundaryMarkers.bottom.value, 1, None)) self._bcs.append((BCType.constant_component, HyperCubeBoundaryMarkers.right.value, 0, 0.1)) elif self._bc_type == "clamped": self._bcs.append( (BCType.fixed, HyperCubeBoundaryMarkers.left.value, None)) self._bcs.append( (BCType.constant, HyperCubeBoundaryMarkers.right.value, (0.1, 0.0))) elif self._bc_type == "clamped_free": self._bcs.append( (BCType.fixed, HyperCubeBoundaryMarkers.left.value, None)) self._bcs.append((BCType.constant_component, HyperCubeBoundaryMarkers.right.value, 0, 0.1)) elif self._bc_type == "pointwise": gamma01 = PointSubDomain((0.0, ) * self._space_dim, tol=1e-10) gamma02 = dlfn.CompiledSubDomain("near(x[0], 0.0)") self._bcs.append((BCType.fixed_pointwise, gamma01, None)) self._bcs.append( (BCType.fixed_component_pointwise, gamma02, 0, None)) self._bcs.append((BCType.constant_component, HyperCubeBoundaryMarkers.right.value, 0, 0.1))
def mark_rectangular_subdomain(p0, p1, mesh): '''Get markers for a rectangular subdomain. Parameters ---------- p0 : point-like Bottom-left vertex coordinates. p1 : point-like Top-right vertex coordinates. mesh : dolfin.Mesh Domain mesh. Returns ------- domain_markers : dolfin.MeshFunction Domain markers where displacement measurements are known. The subdomain is identified with mesh function values equal to 1. ''' SUBDOMAIN_MARKER_ID = 1 subdomain = dolfin.CompiledSubDomain( "x[0] > x0 && x[0] < x1 && x[1] > y0 && x[1] < y1", x0=p0[0], y0=p0[1], x1=p1[0], y1=p1[1]) subdomain_markers = dolfin.MeshFunction('size_t', mesh, dim=mesh.topology().dim(), value=0) subdomain.mark(subdomain_markers, SUBDOMAIN_MARKER_ID) return subdomain_markers, SUBDOMAIN_MARKER_ID
def test_to_DG0_subdomain(self): mesh = df.UnitSquareMesh(4, 4) cell_f = df.MeshFunction('size_t', mesh, 2, 0) df.CompiledSubDomain('x[0] < 0.5 + DOLFIN_EPS').mark(cell_f, 1) submesh = EmbeddedMesh(cell_f, 1) transfer = SubMeshTransfer(mesh, submesh) V = df.FunctionSpace(mesh, 'DG', 0) Vsub = df.FunctionSpace(submesh, 'DG', 0) to_Vsub = transfer.compute_map(Vsub, V, strict=True) # Set degree 0 to get the quad order right f = df.Expression('x[0] + 2*x[1]', degree=0) fV = df.interpolate(f, V) fsub = df.Function(Vsub) to_Vsub(fsub, fV) error = df.inner(fsub - f, fsub - f)*df.dx(domain=submesh) error = df.sqrt(abs(df.assemble(error))) self.assertTrue(error < 1E-13)
def test_to_DG0(self): subdomains = (df.CompiledSubDomain('near(x[0], 0.5)'), df.DomainBoundary()) for subd in subdomains: mesh = df.UnitCubeMesh(4, 4, 4) facet_f = df.MeshFunction('size_t', mesh, 2, 0) subd.mark(facet_f, 1) submesh = EmbeddedMesh(facet_f, 1) transfer = SubMeshTransfer(mesh, submesh) V = df.FunctionSpace(mesh, 'Discontinuous Lagrange Trace', 0) Vsub = df.FunctionSpace(submesh, 'DG', 0) to_Vsub = transfer.compute_map(Vsub, V, strict=False) # Set degree 0 to get the quad order right f = df.Expression('x[0] + 2*x[1] - x[2]', degree=0) fV = df.interpolate(f, V) fsub = df.Function(Vsub) to_Vsub(fsub, fV) error = df.inner(fsub - f, fsub - f)*df.dx(domain=submesh) error = df.sqrt(abs(df.assemble(error))) self.assertTrue(error < 1E-13)
def __init__(self, center, radius, *, maxh = 1., eps = csg_eps): segments = max(int(4*radius/maxh), 32) eps *= radius super().__init__(mshr.Circle(dolfin.Point(*center), radius, segments), csg_boundaries = [ dolfin.CompiledSubDomain( 'on_boundary && near((x[0]-c0)*(x[0]-c0) + (x[1]-c1)*(x[1]-c1), rr, eps)', c0 = center[0], c1 = center[1], rr = radius * radius, eps = eps ) ])
def union(domains, A=inner_size, tol=1E-10): def body(domains): if isinstance(domains, str): if domains: return '( %s )' % domains else: return '' else: return ' || '.join(map(body, domains)) return df.CompiledSubDomain(body(domains), A=A, tol=tol)