def setupMeshes(cls, mesh, N, num_refine=0, randref=(1.0, 1.0)): """Create a set of N meshes based on provided mesh. Parameters num_refine>=0 and randref specify refinement adjustments. num_refine specifies the number of refinements per mesh, randref[0] specifies the probability that a given mesh is refined, and randref[1] specifies the probability that an element of the mesh is refined (if it is refined at all). """ assert num_refine >= 0 assert 0 < randref[0] <= 1.0 assert 0 < randref[1] <= 1.0 # create set of (refined) meshes meshes = list(); for _ in range(N): m = Mesh(mesh) for _ in range(num_refine): if randref[0] == 1.0 and randref[1] == 1.0: m = refine(m) elif random() <= randref[0]: cell_markers = CellFunction("bool", m) cell_markers.set_all(False) cell_ids = range(m.num_cells()) shuffle(cell_ids) num_ref_cells = int(ceil(m.num_cells() * randref[1])) for cell_id in cell_ids[0:num_ref_cells]: cell_markers[cell_id] = True m = refine(m, cell_markers) meshes.append(m) return meshes
def test_convert_diffpack(self): from dolfin import Mesh, MPI, MeshFunction if MPI.num_processes() != 1: return fname = os.path.join("data", "diffpack_tet") dfname = fname+".xml" # Read triangle file and convert to a dolfin xml mesh file meshconvert.diffpack2xml(fname+".grid", dfname) # Read in dolfin mesh and check number of cells and vertices mesh = Mesh(dfname) self.assertEqual(mesh.num_vertices(), 27) self.assertEqual(mesh.num_cells(), 48) self.assertEqual(mesh.domains().markers(3).size(), 48) self.assertEqual(mesh.domains().markers(2).size(), 16) mf_basename = dfname.replace(".xml", "_marker_%d.xml") for marker, num in [(3, 9), (6, 9), (7, 3), (8, 1)]: mf_name = mf_basename % marker mf = MeshFunction("uint", mesh, mf_name) self.assertEqual(sum(mf.array()==marker), num) os.unlink(mf_name) # Clean up os.unlink(dfname)
def test_convert_diffpack_2d(self): from dolfin import Mesh, MPI, MeshFunction fname = os.path.join(os.path.dirname(__file__), "data", "diffpack_tri") dfname = fname + ".xml" # Read triangle file and convert to a dolfin xml mesh file meshconvert.diffpack2xml(fname + ".grid", dfname) # Read in dolfin mesh and check number of cells and vertices mesh = Mesh(dfname) self.assertEqual(mesh.num_vertices(), 41) self.assertEqual(mesh.num_cells(), 64) self.assertEqual(len(mesh.domains().markers(2)), 64) mf_basename = dfname.replace(".xml", "_marker_%d.xml") for marker, num in [(1, 10), (2, 5), (3, 5)]: mf_name = mf_basename % marker mf = MeshFunction("size_t", mesh, mf_name) self.assertEqual(sum(mf.array() == marker), num) os.unlink(mf_name) # Clean up os.unlink(dfname)
def test_convert_diffpack(self): from dolfin import Mesh, MPI, MeshFunction, mpi_comm_world if MPI.size(mpi_comm_world()) != 1: return fname = os.path.join("data", "diffpack_tet") dfname = fname + ".xml" # Read triangle file and convert to a dolfin xml mesh file meshconvert.diffpack2xml(fname + ".grid", dfname) # Read in dolfin mesh and check number of cells and vertices mesh = Mesh(dfname) self.assertEqual(mesh.num_vertices(), 27) self.assertEqual(mesh.num_cells(), 48) self.assertEqual(len(mesh.domains().markers(3)), 48) self.assertEqual(len(mesh.domains().markers(2)), 16) mf_basename = dfname.replace(".xml", "_marker_%d.xml") for marker, num in [(3, 9), (6, 9), (7, 3), (8, 1)]: mf_name = mf_basename % marker mf = MeshFunction("size_t", mesh, mf_name) self.assertEqual(sum(mf.array() == marker), num) os.unlink(mf_name) # Clean up os.unlink(dfname)
def test_convert_diffpack_2d(self): from dolfin import Mesh, MPI, MeshFunction, mpi_comm_world fname = os.path.join(os.path.dirname(__file__), "data", "diffpack_tri") dfname = fname+".xml" # Read triangle file and convert to a dolfin xml mesh file meshconvert.diffpack2xml(fname+".grid", dfname) # Read in dolfin mesh and check number of cells and vertices mesh = Mesh(dfname) self.assertEqual(mesh.num_vertices(), 41) self.assertEqual(mesh.num_cells(), 64) self.assertEqual(len(mesh.domains().markers(2)), 64) mf_basename = dfname.replace(".xml", "_marker_%d.xml") for marker, num in [(1,10), (2,5), (3,5)]: mf_name = mf_basename % marker mf = MeshFunction("size_t", mesh, mf_name) self.assertEqual(sum(mf.array()==marker), num) os.unlink(mf_name) # Clean up os.unlink(dfname)
def convert(msh_file, h5_file): '''Temporary version of convert from msh to h5''' root, _ = os.path.splitext(msh_file) assert os.path.splitext(msh_file)[1] == '.msh' assert os.path.splitext(h5_file)[1] == '.h5' # Get the xml mesh xml_file = '.'.join([root, 'xml']) subprocess.call(['dolfin-convert %s %s' % (msh_file, xml_file)], shell=True) # Success? assert os.path.exists(xml_file) mesh = Mesh(xml_file) out = HDF5File(mesh.mpi_comm(), h5_file, 'w') out.write(mesh, 'mesh') info('Mesh has %d cells' % mesh.num_cells()) info('Mesh size %g %g' % (mesh.hmin(), mesh.hmax())) outputs = [mesh] # Save ALL data as facet_functions names = ('surfaces', 'volumes') for name, region in zip(names, ('facet_region.xml', 'physical_region.xml')): r_xml_file = '_'.join([root, region]) f = MeshFunction('size_t', mesh, r_xml_file) out.write(f, name) outputs.append(f) return outputs
def CladdingR_solver(FC_mesh, FC_facets, FC_fs, FC_info, D_post, R_path): FC_fi, FC_fo = FC_fs h, Tfc_inner, Tb, FC_Tave = FC_info # Reading mesh data stored in .xdmf files. mesh = Mesh() with XDMFFile(FC_mesh) as infile: infile.read(mesh) mvc = MeshValueCollection("size_t", mesh, 1) with XDMFFile(FC_facets) as infile: infile.read(mvc, "name_to_read") mf = cpp.mesh.MeshFunctionSizet(mesh, mvc) #File("Circle_facet.pvd").write(mf) # Mesh data print('CladdingRod_mesh data\n', 'Number of cells: ', mesh.num_cells(), '\n Number of nodes: ', mesh.num_vertices()) # Define variational problem V = FunctionSpace(mesh, 'P', 1) u = Function(V) v = TestFunction(V) # Define boundary conditions base on pygmsh mesh mark bc = DirichletBC(V, Constant(Tfc_inner), mf, FC_fi) ds = Measure("ds", domain=mesh, subdomain_data=mf, subdomain_id=FC_fo) # Variational formulation # Klbda_ZrO2 = 18/1000 # W/(m K) --> Nuclear reactor original source # Klbda_ZrO2 = Constant(K_ZrO2(FC_Tave)) F = K_ZrO2(u) * dot(grad(u), grad(v)) * dx + h * (u - Tb) * v * ds # Compute solution du = TrialFunction(V) Gain = derivative(F, u, du) solve(F==0, u, bc, J=Gain, \ solver_parameters={"newton_solver": {"linear_solver": "lu", "relative_tolerance": 1e-9}}, \ form_compiler_parameters={"cpp_optimize": True, "representation": "uflacs", "quadrature_degree" : 2} ) # mumps # Save solution if D_post: fU_out = XDMFFile(os.path.join(R_path, 'FuelCladding', 'u.xdmf')) fU_out.write_checkpoint(u, "T", 0, XDMFFile.Encoding.HDF5, False) fU_out.close()
def FuelR_solver(FR_mesh, FR_facets, FR_f, FRod_info, D_post, R_path): Qv, Tfr_outer, FR_Tave = FRod_info # Reading mesh data stored in .xdmf files. mesh = Mesh() with XDMFFile(FR_mesh) as infile: infile.read(mesh) mvc = MeshValueCollection("size_t", mesh, 1) with XDMFFile(FR_facets) as infile: infile.read(mvc, "name_to_read") mf = cpp.mesh.MeshFunctionSizet(mesh, mvc) #File("Circle_facet.pvd").write(mf) # Mesh data print('FuelRod_mesh data\n', 'Number of cells: ', mesh.num_cells(), '\n Number of nodes: ', mesh.num_vertices()) # Define variational problem V = FunctionSpace(mesh, 'P', 1) u = Function(V) v = TestFunction(V) f = Constant(Qv) # Define boundary conditions base on pygmsh mesh mark bc = DirichletBC(V, Constant(Tfr_outer), mf, FR_f) # Variational formulation # Klbda_UO2 = Constant(K_UO2(FR_Tave)) F = K_UO2(u) * dot(grad(u), grad(v)) * dx - f * v * dx # Compute solution du = TrialFunction(V) Gain = derivative(F, u, du) solve(F==0, u, bc, J=Gain, \ solver_parameters={"newton_solver": {"linear_solver": "lu", "relative_tolerance": 1e-9}}, \ form_compiler_parameters={"cpp_optimize": True, "representation": "uflacs", "quadrature_degree" : 2} ) # mumps # Save solution if D_post: fU_out = XDMFFile(os.path.join(R_path, 'FuelRod', 'u.xdmf')) fU_out.write_checkpoint(u, "T", 0, XDMFFile.Encoding.HDF5, False) fU_out.close()
def convert(msh_file, h5_file, save_mvc=False): '''Temporary version of convertin from msh to h5''' root, _ = os.path.splitext(msh_file) assert os.path.splitext(msh_file)[1] == '.msh' assert os.path.splitext(h5_file)[1] == '.h5' # Get the xml mesh xml_file = '.'.join([root, 'xml']) subprocess.call(['dolfin-convert %s %s' % (msh_file, xml_file)], shell=True) # Success? assert os.path.exists(xml_file) mesh = Mesh(xml_file) out = HDF5File(mesh.mpi_comm(), h5_file, 'w') out.write(mesh, 'mesh') print('Mesh has %d cells' % mesh.num_cells()) print('Mesh size %g %g' % (mesh.hmin(), mesh.hmax())) # Save ALL data as facet_functions names = ('surfaces', 'volumes') if not save_mvc: for name, region in zip(names, ('facet_region.xml', 'physical_region.xml')): r_xml_file = '_'.join([root, region]) f = MeshFunction('size_t', mesh, r_xml_file) print('%d %s with 1' % (sum(1 for _ in SubsetIterator(f, 1)), name)) out.write(f, name) return True for name, region in zip(names, ('facet_region.xml', 'physical_region.xml')): r_xml_file = '_'.join([root, region]) f = MeshFunction('size_t', mesh, r_xml_file) # With mesh value collection we only store nonzero tags mvc = MeshValueCollection('size_t', mesh, f.dim()) # Fill fill_mvc_from_mf(f, mvc) # And save out.write(mvc, name) return True
def test_convert_triangle(self): # Disabled because it fails, see FIXME below # test no. 1 from dolfin import Mesh, MPI if MPI.num_processes() != 1: return fname = os.path.join("data", "triangle") dfname = fname+".xml" # Read triangle file and convert to a dolfin xml mesh file meshconvert.triangle2xml(fname, dfname) # Read in dolfin mesh and check number of cells and vertices mesh = Mesh(dfname) self.assertEqual(mesh.num_vertices(), 96) self.assertEqual(mesh.num_cells(), 159) # Clean up os.unlink(dfname) # test no. 2 from dolfin import MPI, Mesh, MeshFunction, \ edges, Edge, faces, Face, \ SubsetIterator, facets, CellFunction if MPI.num_processes() != 1: return fname = os.path.join("data", "test_Triangle_3") dfname = fname+".xml" dfname0 = fname+".attr0.xml" # Read triangle file and convert to a dolfin xml mesh file meshconvert.triangle2xml(fname, dfname) # Read in dolfin mesh and check number of cells and vertices mesh = Mesh(dfname) mesh.init() mfun = MeshFunction('double', mesh, dfname0) self.assertEqual(mesh.num_vertices(), 58) self.assertEqual(mesh.num_cells(), 58) # Create a size_t CellFunction and assign the values based on the # converted Meshfunction cf = CellFunction("size_t", mesh) cf.array()[mfun.array()==10.0] = 0 cf.array()[mfun.array()==-10.0] = 1 # Meassure total area of cells with 1 and 2 marker add = lambda x, y : x+y area0 = reduce(add, (Face(mesh, cell.index()).area() \ for cell in SubsetIterator(cf, 0)), 0.0) area1 = reduce(add, (Face(mesh, cell.index()).area() \ for cell in SubsetIterator(cf, 1)), 0.0) total_area = reduce(add, (face.area() for face in faces(mesh)), 0.0) # Check that all cells in the two domains are either above or below y=0 self.assertTrue(all(cell.midpoint().y()<0 for cell in SubsetIterator(cf, 0))) self.assertTrue(all(cell.midpoint().y()>0 for cell in SubsetIterator(cf, 1))) # Check that the areas add up self.assertAlmostEqual(area0+area1, total_area) # Measure the edge length of the two edge domains edge_markers = mesh.domains().facet_domains() self.assertTrue(edge_markers is not None) length0 = reduce(add, (Edge(mesh, e.index()).length() \ for e in SubsetIterator(edge_markers, 0)), 0.0) length1 = reduce(add, (Edge(mesh, e.index()).length() \ for e in SubsetIterator(edge_markers, 1)), 0.0) # Total length of all edges and total length of boundary edges total_length = reduce(add, (e.length() for e in edges(mesh)), 0.0) boundary_length = reduce(add, (Edge(mesh, f.index()).length() \ for f in facets(mesh) if f.exterior()), 0.0) # Check that the edges add up self.assertAlmostEqual(length0+length1, total_length) self.assertAlmostEqual(length1, boundary_length) # Clean up os.unlink(dfname) os.unlink(dfname0)
def test_convert_triangle( self): # Disabled because it fails, see FIXME below # test no. 1 from dolfin import Mesh, MPI fname = os.path.join(os.path.dirname(__file__), "data", "triangle") dfname = fname + ".xml" # Read triangle file and convert to a dolfin xml mesh file meshconvert.triangle2xml(fname, dfname) # Read in dolfin mesh and check number of cells and vertices mesh = Mesh(dfname) self.assertEqual(mesh.num_vertices(), 96) self.assertEqual(mesh.num_cells(), 159) # Clean up os.unlink(dfname) # test no. 2 from dolfin import MPI, Mesh, MeshFunction, \ edges, Edge, faces, Face, \ SubsetIterator, facets fname = os.path.join(os.path.dirname(__file__), "data", "test_Triangle_3") dfname = fname + ".xml" dfname0 = fname + ".attr0.xml" # Read triangle file and convert to a dolfin xml mesh file meshconvert.triangle2xml(fname, dfname) # Read in dolfin mesh and check number of cells and vertices mesh = Mesh(dfname) mesh.init() mfun = MeshFunction('double', mesh, dfname0) self.assertEqual(mesh.num_vertices(), 58) self.assertEqual(mesh.num_cells(), 58) # Create a size_t MeshFunction and assign the values based on the # converted Meshfunction cf = MeshFunction("size_t", mesh, mesh.topology().dim()) cf.array()[mfun.array() == 10.0] = 0 cf.array()[mfun.array() == -10.0] = 1 # Meassure total area of cells with 1 and 2 marker add = lambda x, y: x + y area0 = reduce(add, (Face(mesh, cell.index()).area() \ for cell in SubsetIterator(cf, 0)), 0.0) area1 = reduce(add, (Face(mesh, cell.index()).area() \ for cell in SubsetIterator(cf, 1)), 0.0) total_area = reduce(add, (face.area() for face in faces(mesh)), 0.0) # Check that all cells in the two domains are either above or below y=0 self.assertTrue( all(cell.midpoint().y() < 0 for cell in SubsetIterator(cf, 0))) self.assertTrue( all(cell.midpoint().y() > 0 for cell in SubsetIterator(cf, 1))) # Check that the areas add up self.assertAlmostEqual(area0 + area1, total_area) # Measure the edge length of the two edge domains #edge_markers = mesh.domains().facet_domains() edge_markers = mesh.domains().markers(mesh.topology().dim() - 1) self.assertTrue(edge_markers is not None) #length0 = reduce(add, (Edge(mesh, e.index()).length() \ # for e in SubsetIterator(edge_markers, 0)), 0.0) length0, length1 = 0.0, 0.0 for item in list(edge_markers.items()): if item[1] == 0: e = Edge(mesh, int(item[0])) length0 += Edge(mesh, int(item[0])).length() elif item[1] == 1: length1 += Edge(mesh, int(item[0])).length() # Total length of all edges and total length of boundary edges total_length = reduce(add, (e.length() for e in edges(mesh)), 0.0) boundary_length = reduce(add, (Edge(mesh, f.index()).length() \ for f in facets(mesh) if f.exterior()), 0.0) # Check that the edges add up self.assertAlmostEqual(length0 + length1, total_length) self.assertAlmostEqual(length1, boundary_length) # Clean up os.unlink(dfname) os.unlink(dfname0)
if __name__ == "__main__": xlim = 0.0, 3.0 ylim = 0.0, 0.0127 WAid = 12 INid = 13 BND_ids = WAid, INid meszf = 0.001 # mesh element size factor Domain, Facets = Generate_PBRpygmsh(xlim, ylim, BND_ids, meszf, folder='pygmeshio_test') mesh_D = Mesh() with XDMFFile(Domain) as infile: infile.read(mesh_D) mesh_F = Mesh() with XDMFFile(Facets) as infile: infile.read(mesh_F) print('num_cells :', mesh_D.num_cells()) print('num_vertices:', mesh_D.num_vertices()) print('cell_type :', mesh_D.ufl_cell()) #print('num_facets:', mesh_F.num_facets()) #print('num_edges:', mesh_F.num_edges())
def mesh_around_1d(mesh, size=1, scale=10, padding=0.05): ''' From a 1d in xd (X > 1) mesh (in XML format) produce a Xd mesh where the 1d structure is embedded. Mesh size close to strucure should be size(given as multiple of hmin(), elsewhere scale * size. Padding controls size of the bounding box. ''' dot = mesh.find('.') root, ext = mesh[:dot], mesh[dot:] assert ext == '.xml' or ext == '.xml.gz', ext mesh = Mesh(mesh) gdim = mesh.geometry().dim() assert gdim > 1 and mesh.topology().dim() == 1 x = mesh.coordinates() mesh.init(1, 0) # Compute fall back mesh size: assert size > 0 size = mesh.hmin() * size # Don't allow zero padding - collision of lines with bdry segfaults # too ofter so we prevent it assert padding > 0 # Finally scale better be positive assert scale > 0 point = (lambda xi: tuple(xi) + (0, ))\ if gdim == 2 else (lambda xi: tuple(xi)) geo = '.'.join([root, 'geo']) with open(geo, 'w') as outfile: # Setup outfile.write('SetFactory("OpenCASCADE");\n') outfile.write('size = %g;\n' % size) outfile.write('SIZE = %g;\n' % (size * scale)) # Points fmt = 'Point(%d) = {%.16f, %.16f, %.16f, size};\n' for i, xi in enumerate(x, 1): outfile.write(fmt % ((i, ) + point(xi))) # Lines fmt = 'Line(%d) = {%d, %d};\n' for i, cell in enumerate(cells(mesh), 1): outfile.write(fmt % ((i, ) + tuple(cell.entities(0) + 1))) # BBox xmin, xmax = x.min(0), x.max(0) padding = (xmax - xmin) * padding / 2. xmin -= padding xmax += padding dx = xmax - xmin if gdim == 2 or dx[-1] < 1E-14: # All points are on a plane rect = 'Rectangle(1) = {%g, %g, %g, %g, %g};\n' % ( xmin[0], xmin[1], 0 if gdim == 2 else xmin[2], dx[0], dx[1]) outfile.write(rect) bbox = 'Surface' else: box = 'Box(1) = {%g, %g, %g, %g, %g, %g};\n' % ( xmin[0], xmin[1], xmin[2], dx[0], dx[1], dx[2]) outfile.write(box) bbox = 'Volume' # Crack for line in xrange(1, mesh.num_cells() + 1): outfile.write('Line{%d} In %s{1};\n' % (line, bbox)) # Add Physical volume/surface outfile.write('Physical %s(1) = {1};\n' % bbox) # Add Physical surface/line lines = ', '.join( map(lambda v: '%d' % v, xrange(1, mesh.num_cells() + 1))) outfile.write('Physical Line(1) = {%s};\n' % lines) return geo, gdim
def scalar_laplacians( mesh: df.Mesh, markers: Optional[Dict[str, int]] = None, ffun: Optional[MeshFunction] = None, use_krylov_solver: bool = False, krylov_solver_atol: Optional[float] = None, krylov_solver_rtol: Optional[float] = None, krylov_solver_max_its: Optional[int] = None, verbose: bool = False, strict: bool = False, ) -> Dict[str, df.Function]: """ Calculate the laplacians Arguments --------- mesh : dolfin.Mesh A dolfin mesh markers : dict (optional) A dictionary with the markers for the different bondaries defined in the facet function or within the mesh itself. The follwing markers must be provided: 'base', 'lv', 'epi, 'rv' (optional). If the markers are not provided the following default vales will be used: base = 10, rv = 20, lv = 30, epi = 40. fiber_space : str A string on the form {familiy}_{degree} which determines for what space the fibers should be calculated for. use_krylov_solver: bool If True use Krylov solver, by default False krylov_solver_atol: float (optional) If a Krylov solver is used, this option specifies a convergence criterion in terms of the absolute residual. Default: 1e-15. krylov_solver_rtol: float (optional) If a Krylov solver is used, this option specifies a convergence criterion in terms of the relative residual. Default: 1e-10. krylov_solver_max_its: int (optional) If a Krylov solver is used, this option specifies the maximum number of iterations to perform. Default: 10000. verbose: bool If true, print more info, by default False strict: bool If true raise RuntimeError if solutions does not sum to 1.0 """ if not isinstance(mesh, df.Mesh): raise TypeError("Expected a dolfin.Mesh as the mesh argument.") # Init connectivities mesh.init(2) if ffun is None: ffun = df.MeshFunction("size_t", mesh, 2, mesh.domains()) # Boundary markers, solutions and cases cases, boundaries, markers = find_cases_and_boundaries(ffun, markers) markers_str = "\n".join( ["{}: {}".format(k, v) for k, v in markers.items()]) df.info(("Compute scalar laplacian solutions with the markers: \n" "{}").format(markers_str, ), ) check_boundaries_are_marked( mesh=mesh, ffun=ffun, markers=markers, boundaries=boundaries, ) # Compte the apex to base solutons num_vertices = mesh.num_vertices() num_cells = mesh.num_cells() if mesh.mpi_comm().size > 1: num_vertices = mesh.mpi_comm().allreduce(num_vertices) num_cells = mesh.mpi_comm().allreduce(num_cells) df.info(" Num vertices: {0}".format(num_vertices)) df.info(" Num cells: {0}".format(num_cells)) if "mv" in cases and "av" in cases: # Use Doste approach pass # Else use the Bayer approach return bayer( cases=cases, mesh=mesh, markers=markers, ffun=ffun, verbose=verbose, use_krylov_solver=use_krylov_solver, strict=strict, krylov_solver_atol=krylov_solver_atol, krylov_solver_rtol=krylov_solver_rtol, krylov_solver_max_its=krylov_solver_max_its, )
mesh_size_parser.add_argument('--no_mesh_size', dest='mesh_size', action='store_false') parser.set_defaults(mesh_size=False) args = parser.parse_args() h5_file = convert(args.input) # VTK visualize tags if args.save_pvd or args.mesh_size: h5 = HDF5File(mpi_comm_world(), h5_file, 'r') mesh = Mesh() h5.read(mesh, 'mesh', False) info('Mesh has %d cells' % mesh.num_cells()) info('Mesh has %d vertices' % mesh.num_vertices()) info('Box size %s' % (mesh.coordinates().max(axis=0) - mesh.coordinates().min(axis=0))) hmin, hmax = mesh.hmin(), mesh.hmax() info('Mesh has sizes %g %g' % (hmin, hmax)) root = os.path.splitext(args.input)[0] tdim = mesh.topology().dim() data_sets = ('curves', 'surfaces', 'volumes') dims = (1, tdim - 1, tdim) for ds, dim in zip(data_sets, dims): if h5.has_dataset(ds): f = MeshFunction('size_t', mesh, dim, 0)