def test_oriented_saveload(m: Mesh): m = m.refined(4) m = m.with_boundaries({"mid": m.facets_around([5]),}) assert len(m.boundaries["mid"].ori) == m.refdom.nfacets M = from_meshio(to_meshio(m)) assert_array_equal( m.boundaries["mid"].ori, M.boundaries["mid"].ori, )
def __init__(self, mesh: Mesh, elem: Element, mapping: MappingMortar, intorder: Optional[int] = None, quadrature: Optional[Tuple[ndarray, ndarray]] = None, facets: Optional[Any] = None, dofs: Optional[Dofs] = None, side: int = 0): """Precomputed global basis on the mortar mesh.""" if side not in (0, 1): raise Exception("'side' must be 0 or 1.") if facets is None: from copy import deepcopy mapping = deepcopy(mapping) mapping.side = side facets = mapping.helper_to_orig[side] facets = mesh.normalize_facets(facets) super(MortarFacetBasis, self).__init__( mesh, elem, mapping=mapping, intorder=intorder, quadrature=quadrature, facets=facets, dofs=dofs, )
def __init__(self, mesh: Mesh, elem: Element, mapping: Optional[Mapping] = None, intorder: Optional[int] = None, quadrature: Optional[Tuple[ndarray, ndarray]] = None, facets: Optional[Any] = None, dofs: Optional[Dofs] = None, side: int = 0): """Precomputed global basis on interior facets.""" if facets is None: facets = np.nonzero(mesh.f2t[1] != -1)[0] facets = mesh.normalize_facets(facets) super(InteriorFacetBasis, self).__init__( mesh, elem, mapping=mapping, intorder=intorder, quadrature=quadrature, facets=facets, dofs=dofs, side=side, )
def runTest(self): from tempfile import NamedTemporaryFile m = self.cls().refined(2) f = NamedTemporaryFile(delete=False) m.save(f.name + ".vtk") with self.assertWarnsRegex(UserWarning, '^Unable to load tagged'): m2 = Mesh.load(f.name + ".vtk") self.assertTrue(((m.p[0, :] - m2.p[0, :]) < 1e-6).all())
def test_saveload_cycle_vtk(m): from tempfile import NamedTemporaryFile m = m.refined(2) with NamedTemporaryFile(suffix='.vtk') as f: m.save(f.name) m2 = Mesh.load(f.name) assert_array_equal(m.p, m2.p) assert_array_equal(m.t, m2.t)
def __init__(self, mesh: Mesh, elem: Element, mapping: Optional[Mapping] = None, intorder: Optional[int] = None, quadrature: Optional[Tuple[ndarray, ndarray]] = None, refdom: Type[Refdom] = Refdom, dofs: Optional[Dofs] = None): if mesh.refdom != elem.refdom: raise ValueError("Incompatible Mesh and Element.") self.mapping = mesh._mapping() if mapping is None else mapping self.dofs = Dofs(mesh, elem) if dofs is None else dofs # global degree-of-freedom location try: doflocs = self.mapping.F(elem.doflocs.T) self.doflocs = np.zeros((doflocs.shape[0], self.N)) # match mapped dofs and global dof numbering for itr in range(doflocs.shape[0]): for jtr in range(self.dofs.element_dofs.shape[0]): self.doflocs[itr, self.dofs.element_dofs[jtr]] =\ doflocs[itr, :, jtr] except Exception: logger.warning("Unable to calculate global DOF locations.") self.mesh = mesh self.elem = elem self.Nbfun = self.dofs.element_dofs.shape[0] self.nelems = 0 # subclasses should overwrite if quadrature is not None: self.X, self.W = quadrature else: self.X, self.W = get_quadrature( refdom, intorder if intorder is not None else 2 * self.elem.maxdeg )
def test_saveload_cycle_tags(fmt, kwargs, m): m = (m.refined(2).with_subdomains(_test_lambda).with_boundaries({ 'test': lambda x: (x[0] == 0) * (x[1] < 0.6), 'set': lambda x: (x[0] == 0) * (x[1] > 0.3) })) from tempfile import NamedTemporaryFile with NamedTemporaryFile(suffix=fmt) as f: m.save(f.name, point_data={'foo': m.p[0]}, **kwargs) out = ['point_data', 'cells_dict'] m2 = Mesh.load(f.name, out=out) assert_array_equal(m.p, m2.p) assert_array_equal(m.t, m2.t) assert_array_equal(out[0]['foo'], m.p[0]) for key in m.subdomains: assert_array_equal(m2.subdomains[key].sort(), m.subdomains[key].sort()) for key in m.boundaries: assert_array_equal(m2.boundaries[key].sort(), m.boundaries[key].sort())
def to_file(mesh: Mesh, filename: str): with open(filename, 'w') as handle: json.dump(mesh.to_dict(), handle)
def __init__(self, mesh: Mesh, elem: Element, mapping: Optional[Mapping] = None, intorder: Optional[int] = None, quadrature: Optional[Tuple[ndarray, ndarray]] = None, facets: Optional[Any] = None, dofs: Optional[Dofs] = None, side: int = 0): """Precomputed global basis on boundary facets. Parameters ---------- mesh An object of type :class:`~skfem.mesh.Mesh`. elem An object of type :class:`~skfem.element.Element`. mapping An object of type :class:`skfem.mapping.Mapping`. If `None`, uses `mesh.mapping`. intorder Optional integration order, i.e. the degree of polynomials that are integrated exactly by the used quadrature. Not used if `quadrature` is specified. quadrature Optional tuple of quadrature points and weights. facets Optional subset of facet indices. dofs Optional :class:`~skfem.assembly.Dofs` object. """ typestr = ("{}({}, {})".format(type(self).__name__, type(mesh).__name__, type(elem).__name__)) logger.info("Initializing {}".format(typestr)) super(FacetBasis, self).__init__( mesh, elem, mapping, intorder, quadrature, mesh.brefdom, dofs, ) # by default use boundary facets if facets is None: self.find = np.nonzero(self.mesh.f2t[1] == -1)[0] else: self.find = mesh.normalize_facets(facets) # fix the orientation if isinstance(self.find, OrientedBoundary): self.tind = self.mesh.f2t[(-1) ** side * self.find.ori - side, self.find] self.tind_normals = self.mesh.f2t[self.find.ori, self.find] else: self.tind = self.mesh.f2t[side, self.find] self.tind_normals = self.mesh.f2t[0, self.find] if len(self.find) == 0: logger.warning("Initializing {} with no facets.".format(typestr)) # boundary refdom to global facet x = self.mapping.G(self.X, find=self.find) # global facet to refdom facet Y = self.mapping.invF(x, tind=self.tind) # calculate normals Y0 = self.mapping.invF(x, tind=self.tind_normals) assert self.tind_normals is not None # satisfy mypy self.normals = DiscreteField( value=self.mapping.normals(Y0, self.tind_normals, self.find, self.mesh.t2f) ) self.nelems = len(self.find) self.basis = [self.elem.gbasis(self.mapping, Y, j, tind=self.tind) for j in range(self.Nbfun)] self.dx = (np.abs(self.mapping.detDG(self.X, find=self.find)) * np.tile(self.W, (self.nelems, 1))) logger.info("Initializing finished.")
def __init__(self, mesh: Mesh, elem: Element, mapping: Optional[Mapping] = None, intorder: Optional[int] = None, elements: Optional[Any] = None, quadrature: Optional[Tuple[ndarray, ndarray]] = None, dofs: Optional[Dofs] = None): """Combine :class:`~skfem.mesh.Mesh` and :class:`~skfem.element.Element` into a set of precomputed global basis functions. Parameters ---------- mesh An object of type :class:`~skfem.mesh.Mesh`. elem An object of type :class:`~skfem.element.Element`. mapping An object of type :class:`skfem.mapping.Mapping`. If `None`, uses `mesh.mapping`. intorder Optional integration order, i.e. the degree of polynomials that are integrated exactly by the used quadrature. Not used if `quadrature` is specified. elements Optional subset of element indices. quadrature Optional tuple of quadrature points and weights. dofs Optional :class:`~skfem.assembly.Dofs` object. """ logger.info("Initializing {}({}, {})".format( type(self).__name__, type(mesh).__name__, type(elem).__name__)) super(CellBasis, self).__init__( mesh, elem, mapping, intorder, quadrature, mesh.refdom, dofs, ) if elements is None: self.tind = None self.nelems = mesh.nelements else: self.tind = mesh.normalize_elements(elements) self.nelems = len(self.tind) self.basis = [ self.elem.gbasis(self.mapping, self.X, j, tind=self.tind) for j in range(self.Nbfun) ] self.dx = (np.abs(self.mapping.detDF(self.X, tind=self.tind)) * np.tile(self.W, (self.nelems, 1))) logger.info("Initializing finished.")