def read(self, mesh, **kwargs): self.fd = fd = open(self.filename, "r") mode = "header" nod = conns = desc = None while 1: if mode == "header": line = skip_read_line(fd) n_tags = self._read_commented_int() for ii in xrange(n_tags): skip_read_line(fd) n_types = self._read_commented_int() for ii in xrange(n_types): skip_read_line(fd) skip_read_line(fd) assert_(skip_read_line(fd).split()[1] == "Mesh") skip_read_line(fd) dim = self._read_commented_int() assert_((dim == 2) or (dim == 3)) n_nod = self._read_commented_int() i0 = self._read_commented_int() mode = "points" elif mode == "points": nod = read_array(fd, n_nod, dim, nm.float64) mode = "cells" elif mode == "cells": n_types = self._read_commented_int() conns = [] descs = [] mat_ids = [] for it in xrange(n_types): t_name = skip_read_line(fd).split()[1] n_ep = self._read_commented_int() n_el = self._read_commented_int() if dim == 2: aux = read_array(fd, n_el, n_ep, nm.int32) if t_name == "tri": conns.append(aux) descs.append("2_3") is_conn = True else: is_conn = False else: raise NotImplementedError # Skip parameters. n_pv = self._read_commented_int() n_par = self._read_commented_int() for ii in xrange(n_par): skip_read_line(fd) n_domain = self._read_commented_int() assert_(n_domain == n_el) if is_conn: mat_id = read_array(fd, n_domain, 1, nm.int32) mat_ids.append(mat_id) else: for ii in xrange(n_domain): skip_read_line(fd) # Skip up/down pairs. n_ud = self._read_commented_int() for ii in xrange(n_ud): skip_read_line(fd) break nod = nm.concatenate((nod, nm.zeros((n_nod, 1), dtype=nm.int32)), 1) nod = nm.ascontiguousarray(nod) dim = nod.shape[1] - 1 conns2 = [] for ii, conn in enumerate(conns): conns2.append(nm.c_[conn, mat_ids[ii]]) conns_in, mat_ids = sort_by_mat_id(conns2) conns, mat_ids, descs = split_by_mat_id(conns_in, mat_ids, descs) mesh._set_data(nod, conns, mat_ids, descs) # mesh.write( 'pokus.mesh', io = 'auto' ) self.fd = None return mesh
def _read_commented_int(self): return int(skip_read_line(self.fd).split("#")[0])