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 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
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) h5.read(f, ds) if args.save_pvd: File('%s_%s.pvd' % (root, ds)) << f