def load_state_1D_vtk(name): """Load one VTK file containing state in time Parameters ---------- name : str Returns ------- coors : ndarray u : ndarray """ from sfepy.discrete.fem.meshio import MeshioLibIO io = MeshioLibIO(name) coors = io.read(Mesh()).coors[:, 0, None] data = io.read_data(step=0) var_name = head([k for k in data.keys() if "_modal" in k])[:-1] if var_name is None: print("File {} does not contain modal data.".format(name)) return porder = len([k for k in data.keys() if var_name in k]) u = nm.zeros((porder, coors.shape[0] - 1, 1, 1)) for ii in range(porder): u[ii, :, 0, 0] = data[var_name+'{}'.format(ii)].data return coors, u
def load_1D_vtks(fold, name): """Reads series of .vtk files and crunches them into form suitable for plot10_DG_sol. Attempts to read modal cell data for variable mod_data. i.e. ``?_modal{i}``, where i is number of modal DOF Resulting solution data have shape: ``(order, nspace_steps, ntime_steps, 1)`` Parameters ---------- fold : folder where to look for files name : used in ``{name}.i.vtk, i = 0,1, ... tns - 1`` Returns ------- coors : ndarray mod_data : ndarray solution data """ files = glob(pjoin(fold, name) + ".[0-9]*") if len(files) == 0: # no multiple time steps, try loading single file print("No files {} found in {}".format(pjoin(fold, name) + ".[0-9]*", fold)) print("Trying {}".format(pjoin(fold, name) + ".vtk")) files = glob(pjoin(fold, name) + ".vtk") if files: return load_state_1D_vtk(files[0]) else: print("Nothing found.") return io = MeshioLibIO(files[0]) coors = io.read(Mesh()).coors[:, 0, None] data = io.read_data(step=0) var_name = head([k for k in data.keys() if "_modal" in k])[:-1] if var_name is None: print("File {} does not contain modal data.".format(files[0])) return porder = len([k for k in data.keys() if var_name in k]) tn = len(files) nts = sorted([int(f.split(".")[-2]) for f in files]) digs = len(files[0].split(".")[-2]) full_name_form = ".".join((pjoin(fold, name), ("{:0" + str(digs) + "d}"), "vtk")) mod_data = nm.zeros((porder, coors.shape[0] - 1, tn, 1)) for i, nt in enumerate(nts): io = MeshioLibIO(full_name_form.format(nt)) # parameter "step" does nothing, but is obligatory data = io.read_data(step=0) for ii in range(porder): mod_data[ii, :, i, 0] = data[var_name+'{}'.format(ii)].data return coors, mod_data
def gen_mesh_from_poly(filename, verbose=True): """ Import mesh generated by tetgen or triangle. Parameters ---------- filename : string file name Returns ------- mesh : Mesh instance triangular or tetrahedral mesh """ def getnodes(fnods, up): f = file(fnods) l = [int(x) for x in f.readline().split()] npoints, dim, nattrib, nbound = l if verbose: up.init(npoints) nodes = [] for line in f: if line[0] == "#": continue l = [float(x) for x in line.split()] l = l[:(dim + 1)] l[0] = int(l[0]) nodes.append(tuple(l)) assert l[0] == len(nodes) assert npoints == len(nodes) return nodes def getele(fele, up): f = file(fele) l = [int(x) for x in f.readline().split()] nele, nnod, nattrib = l #we have either linear or quadratic tetrahedra: if nnod in [4, 10]: elem = 'tetra' linear = (nnod == 4) if nnod in [3, 7]: elem = 'tri' linear = (nnod == 3) # if nattrib!=1: # raise "tetgen didn't assign an entity number to each element (option -A)" els = [] regions = {} for line in f: if line[0] == "#": continue l = [int(x) for x in line.split()] if elem == 'tri': if linear: assert (len(l) - 1 - nattrib) == 3 els.append((l[0], l[1], l[2], l[3])) regionnum = l[5] else: assert len(l) - 2 == 10 els.append((l[0], 54, l[1], l[2], l[3], l[4], l[5], l[6], l[7], l[8], l[9], l[10])) regionnum = l[11] if elem == 'tetra': if linear: assert len(l) - 2 == 4 els.append((l[0], 54, l[1], l[2], l[3], l[4])) regionnum = l[5] else: assert len(l) - 2 == 10 els.append((l[0], 54, l[1], l[2], l[3], l[4], l[5], l[6], l[7], l[8], l[9], l[10])) regionnum = l[11] if regionnum == 0: print "see %s, element # %d" % (fele, l[0]) raise "there are elements not belonging to any physical entity" if regions.has_key(regionnum): regions[regionnum].append(l[0]) else: regions[regionnum] = [l[0]] assert l[0] == len(els) if verbose: up.update(l[0]) return els, regions, linear def getBCfaces(ffaces, up): f = file(ffaces) l = [int(x) for x in f.readline().split()] nfaces, nattrib = l if nattrib != 1: raise "tetgen didn't assign an entity number to each face \ (option -A)" if verbose: up.init(nfaces) faces = {} for line in f: if line[0] == "#": continue l = [int(x) for x in line.split()] assert len(l) == 5 regionnum = l[4] if regionnum == 0: continue if faces.has_key(regionnum): faces[regionnum].append((l[1], l[2], l[3])) else: faces[regionnum] = [(l[1], l[2], l[3])] if verbose: up.update(l[0]) return faces def calculatexyz(nodes, els): """Calculate the missing xyz values in place""" def avg(i, j, n4, nodes): a = nodes[n4[i - 1] - 1] b = nodes[n4[j - 1] - 1] return (a[1] + b[1]) / 2, (a[2] + b[2]) / 2, (a[3] + b[3]) / 2 def getxyz(i, n4, nodes): if i + 5 == 5: return avg(1, 2, n4, nodes) if i + 5 == 6: return avg(2, 3, n4, nodes) if i + 5 == 7: return avg(1, 3, n4, nodes) if i + 5 == 8: return avg(1, 4, n4, nodes) if i + 5 == 9: return avg(2, 4, n4, nodes) if i + 5 == 10: return avg(3, 4, n4, nodes) raise "wrong topology" for e in els: n4 = e[2:2 + 4] n6 = e[2 + 4:2 + 4 + 10] for i, n in enumerate(n6): x, y, z = getxyz(i, n4, nodes) nodes[n - 1] = (n, x, y, z) if verbose: print "Reading geometry from poly file..." m = Mesh() m.nodes = getnodes(filename + ".node") m.elements, m.regions, lin = getele(filename + ".ele") if not lin: #tetgen doesn't compute xyz coordinates of the aditional 6 nodes #(only of the 4 corner nodes) in tetrahedra. calculatexyz(m.nodes, m.elements) m.faces = getBCfaces(filename + ".face") return m