[2.0, 0.0, 0.0], [3.0, 0.0, 0.0], [2.5, 0.8, 0.0], [2.5, 0.0, 0.0], [2.75, 0.4, 0.0], [2.25, 0.4, 0.0]]) triPoints.SetData(ntov(triPointsCoords)) triScalars = vtk.vtkFloatArray() triScalars.SetNumberOfTuples(6) triScalars.InsertValue(0, 0.0) triScalars.InsertValue(1, 0.0) triScalars.InsertValue(2, 0.0) triScalars.InsertValue(3, 1.0) triScalars.InsertValue(4, 1.0) triScalars.InsertValue(5, 0.0) aTri = vtk.vtkQuadraticTriangle() for i in range(aTri.GetNumberOfPoints()): aTri.GetPointIds().SetId(i, i) aTriGrid = vtk.vtkUnstructuredGrid() aTriGrid.Allocate(1, 1) aTriGrid.InsertNextCell(aTri.GetCellType(), aTri.GetPointIds()) aTriGrid.SetPoints(triPoints) aTriGrid.GetPointData().SetScalars(triScalars) # Quadratic quadrilateral quadPoints = vtk.vtkPoints() quadPoints.SetNumberOfPoints(8) quadPointsCoords = np.array([ [4.0, 0.0, 0.0], [5.0, 0.0, 0.0], [5.0, 1.0, 0.0],
triPoints.SetNumberOfPoints(6) triPoints.InsertPoint(0, 0.0, 0.0, 0.0) triPoints.InsertPoint(1, 1.0, 0.0, 0.0) triPoints.InsertPoint(2, 0.5, 0.8, 0.0) triPoints.InsertPoint(3, 0.5, 0.0, 0.0) triPoints.InsertPoint(4, 0.75, 0.4, 0.0) triPoints.InsertPoint(5, 0.25, 0.4, 0.0) triScalars = vtk.vtkFloatArray() triScalars.SetNumberOfTuples(6) triScalars.InsertValue(0, 0.0) triScalars.InsertValue(1, 0.0) triScalars.InsertValue(2, 0.0) triScalars.InsertValue(3, 1.0) triScalars.InsertValue(4, 0.0) triScalars.InsertValue(5, 0.0) aTri = vtk.vtkQuadraticTriangle() aTri.GetPointIds().SetId(0, 0) aTri.GetPointIds().SetId(1, 1) aTri.GetPointIds().SetId(2, 2) aTri.GetPointIds().SetId(3, 3) aTri.GetPointIds().SetId(4, 4) aTri.GetPointIds().SetId(5, 5) aTriGrid = vtk.vtkUnstructuredGrid() aTriGrid.Allocate(1, 1) aTriGrid.InsertNextCell(aTri.GetCellType(), aTri.GetPointIds()) aTriGrid.SetPoints(triPoints) aTriGrid.GetPointData().SetScalars(triScalars) aTriMapper = vtk.vtkDataSetMapper() aTriMapper.SetInputData(aTriGrid) aTriMapper.ScalarVisibilityOff() aTriActor = vtk.vtkActor()
def mapElements(self, points, points2, nidMap, model, j): for eid, element in sorted(model.elements.iteritems()): if isinstance(element, CTRIA3): #print "ctria3" elem = vtkTriangle() nodeIDs = element.nodeIDs() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) self.grid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) elif isinstance(element, CTRIA6): nodeIDs = element.nodeIDs() if None not in nodeIDs: elem = vtkQuadraticTriangle() elem.GetPointIds().SetId(3, nidMap[nodeIDs[3]]) elem.GetPointIds().SetId(4, nidMap[nodeIDs[4]]) elem.GetPointIds().SetId(5, nidMap[nodeIDs[5]]) else: elem = vtkTriangle() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) self.grid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) elif isinstance(element, CQUAD4): nodeIDs = element.nodeIDs() elem = vtkQuad() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) elem.GetPointIds().SetId(3, nidMap[nodeIDs[3]]) self.grid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) elif isinstance(element, CQUAD8): nodeIDs = element.nodeIDs() if None not in nodeIDs: elem = vtkQuadraticQuad() elem.GetPointIds().SetId(4, nidMap[nodeIDs[4]]) elem.GetPointIds().SetId(5, nidMap[nodeIDs[5]]) elem.GetPointIds().SetId(6, nidMap[nodeIDs[6]]) elem.GetPointIds().SetId(7, nidMap[nodeIDs[7]]) else: elem = vtkQuad() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) elem.GetPointIds().SetId(3, nidMap[nodeIDs[3]]) self.grid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) elif isinstance(element, CTETRA4): elem = vtkTetra() nodeIDs = element.nodeIDs() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) elem.GetPointIds().SetId(3, nidMap[nodeIDs[3]]) self.grid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) elif isinstance(element, CTETRA10): nodeIDs = element.nodeIDs() if None not in nodeIDs: elem = vtkQuadraticTetra() elem.GetPointIds().SetId(4, nidMap[nodeIDs[4]]) elem.GetPointIds().SetId(5, nidMap[nodeIDs[5]]) elem.GetPointIds().SetId(6, nidMap[nodeIDs[6]]) elem.GetPointIds().SetId(7, nidMap[nodeIDs[7]]) elem.GetPointIds().SetId(8, nidMap[nodeIDs[8]]) elem.GetPointIds().SetId(9, nidMap[nodeIDs[9]]) else: elem = vtkTetra() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) elem.GetPointIds().SetId(3, nidMap[nodeIDs[3]]) self.grid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) elif isinstance(element, CPENTA6): elem = vtkWedge() nodeIDs = element.nodeIDs() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) elem.GetPointIds().SetId(3, nidMap[nodeIDs[3]]) elem.GetPointIds().SetId(4, nidMap[nodeIDs[4]]) elem.GetPointIds().SetId(5, nidMap[nodeIDs[5]]) self.grid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) elif isinstance(element, CPENTA15): nodeIDs = element.nodeIDs() if None not in nodeIDs: elem = vtkQuadraticWedge() elem.GetPointIds().SetId(6, nidMap[nodeIDs[6]]) elem.GetPointIds().SetId(7, nidMap[nodeIDs[7]]) elem.GetPointIds().SetId(8, nidMap[nodeIDs[8]]) elem.GetPointIds().SetId(9, nidMap[nodeIDs[9]]) elem.GetPointIds().SetId(10, nidMap[nodeIDs[10]]) elem.GetPointIds().SetId(11, nidMap[nodeIDs[11]]) elem.GetPointIds().SetId(12, nidMap[nodeIDs[12]]) elem.GetPointIds().SetId(13, nidMap[nodeIDs[13]]) elem.GetPointIds().SetId(14, nidMap[nodeIDs[14]]) else: elem = vtkWedge() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) elem.GetPointIds().SetId(3, nidMap[nodeIDs[3]]) elem.GetPointIds().SetId(4, nidMap[nodeIDs[4]]) elem.GetPointIds().SetId(5, nidMap[nodeIDs[5]]) self.grid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) elif isinstance(element, CHEXA8): nodeIDs = element.nodeIDs() elem = vtkHexahedron() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) elem.GetPointIds().SetId(3, nidMap[nodeIDs[3]]) elem.GetPointIds().SetId(4, nidMap[nodeIDs[4]]) elem.GetPointIds().SetId(5, nidMap[nodeIDs[5]]) elem.GetPointIds().SetId(6, nidMap[nodeIDs[6]]) elem.GetPointIds().SetId(7, nidMap[nodeIDs[7]]) self.grid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) elif isinstance(element, CHEXA20): nodeIDs = element.nodeIDs() #print "nodeIDs = ",nodeIDs if None not in nodeIDs: elem = vtkQuadraticHexahedron() elem.GetPointIds().SetId(8, nidMap[nodeIDs[8]]) elem.GetPointIds().SetId(9, nidMap[nodeIDs[9]]) elem.GetPointIds().SetId(10, nidMap[nodeIDs[10]]) elem.GetPointIds().SetId(11, nidMap[nodeIDs[11]]) elem.GetPointIds().SetId(12, nidMap[nodeIDs[12]]) elem.GetPointIds().SetId(13, nidMap[nodeIDs[13]]) elem.GetPointIds().SetId(14, nidMap[nodeIDs[14]]) elem.GetPointIds().SetId(15, nidMap[nodeIDs[15]]) elem.GetPointIds().SetId(16, nidMap[nodeIDs[16]]) elem.GetPointIds().SetId(17, nidMap[nodeIDs[17]]) elem.GetPointIds().SetId(18, nidMap[nodeIDs[18]]) elem.GetPointIds().SetId(19, nidMap[nodeIDs[19]]) else: elem = vtkHexahedron() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) elem.GetPointIds().SetId(3, nidMap[nodeIDs[3]]) elem.GetPointIds().SetId(4, nidMap[nodeIDs[4]]) elem.GetPointIds().SetId(5, nidMap[nodeIDs[5]]) elem.GetPointIds().SetId(6, nidMap[nodeIDs[6]]) elem.GetPointIds().SetId(7, nidMap[nodeIDs[7]]) self.grid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) elif isinstance(element, LineElement) or isinstance( element, SpringElement): elem = vtk.vtkLine() nodeIDs = element.nodeIDs() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) self.grid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) ### elif isinstance(element, CONM2): # not perfectly located nid = element.Nid() c = element.Centroid() elem = vtk.vtkVertex() #elem = vtk.vtkSphere() #elem.SetRadius(1.0) #print str(element) points2.InsertPoint(j, *c) elem.GetPointIds().SetId(0, j) #elem.SetCenter(points.GetPoint(nidMap[nid])) self.grid2.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) j += 1 else: print "skipping %s" % (element.type) ### self.grid.SetPoints(points) self.grid2.SetPoints(points2) self.grid.Update() self.grid2.Update()
if type.GetElementTypeId() == elements.ELEMENT_QUAD: newNodes = copy.deepcopy(nodes) newNodes[2] = nodes[3] newNodes[3] = nodes[2] return newNodes if VtkSupport(): VTK_UNKNOWN = None VTK_EMPTY_CELL = vtk.vtkEmptyCell().GetCellType() VTK_VERTEX = vtk.vtkVertex().GetCellType() VTK_LINE = vtk.vtkLine().GetCellType() VTK_QUADRATIC_LINE = vtk.vtkQuadraticEdge().GetCellType() VTK_TRIANGLE = vtk.vtkTriangle().GetCellType() VTK_QUADRATIC_TRIANGLE = vtk.vtkQuadraticTriangle().GetCellType() VTK_TETRAHEDRON = vtk.vtkTetra().GetCellType() VTK_QUADRATIC_TETRAHEDRON = vtk.vtkQuadraticTetra().GetCellType() VTK_QUAD = vtk.vtkQuad().GetCellType() VTK_HEXAHEDRON = vtk.vtkHexahedron().GetCellType() vtkTypeIds = ( \ VTK_UNKNOWN, \ VTK_EMPTY_CELL, \ VTK_VERTEX, \ VTK_LINE, VTK_QUADRATIC_LINE, \ VTK_TRIANGLE, VTK_QUADRATIC_TRIANGLE, VTK_QUAD, \ VTK_TETRAHEDRON, VTK_QUADRATIC_TETRAHEDRON, VTK_HEXAHEDRON \ ) class VtkType(elements.ElementType):
def write_vtu(filename, fns, index = None, t = None): """ Write the supplied Function or Function s to a vtu or pvtu file with the supplied filename base. If the Function s are defined on multiple function spaces then separate output files are written for each function space. The optional integer index can be used to add an index to the output filenames. If t is supplied then a scalar field equal to t and with name "time" is added to the output files. All Function s should be on the same mesh and have unique names. In 1D all Function s must have Lagrange basis functions (continuous or discontinous) with degree 0 to 3. In 2D and 3D all Function s must have Lagrange basis functions (continuous or discontinuous) with degree 0 to 2. """ if isinstance(fns, dolfin.Function): return write_vtu(filename, [fns], index = index, t = t) if not isinstance(filename, str): raise InvalidArgumentException("filename must be a string") if not isinstance(fns, list): raise InvalidArgumentException("fns must be a Function or a list of Function s") if len(fns) == 0: raise InvalidArgumentException("fns must include at least one Function") for fn in fns: if not isinstance(fn, dolfin.Function): raise InvalidArgumentException("fns must be a Function or a list of Function s") if not index is None and not isinstance(index, int): raise InvalidArgumentException("index must be an integer") if not t is None and not isinstance(t, (float, dolfin.Constant)): raise InvalidArgumentException("t must be a float or Constant") mesh = fns[0].function_space().mesh() dim = mesh.geometry().dim() if not dim in [1, 2, 3]: raise NotImplementedException("Mesh dimension %i not supported by write_vtu" % dim) def expand_sub_fns(fn): n_sub_spaces = fn.function_space().num_sub_spaces() if n_sub_spaces > 1: fns = fn.split(deepcopy = True) for i, sfn in enumerate(copy.copy(fns)): sfn.rename("%s_%i" % (fn.name(), i + 1), "%s_%i" % (fn.name(), i + 1)) if sfn.function_space().num_sub_spaces() > 0: fns.remove(sfn) fns += expand_sub_fns(sfn) return fns elif n_sub_spaces == 1: e = fn.function_space().ufl_element().extract_component(0)[1] space = dolfin.FunctionSpace(mesh, e.family(), e.degree()) sfn = dolfin.Function(space, name = "%s_1" % fn.name()) sfn.vector()[:] = fn.vector() return [sfn] else: return [fn] nfns = [] for i, fn in enumerate(fns): space = fn.function_space() if not space.mesh().id() == mesh.id(): raise InvalidArgumentException("Require exactly one mesh in write_vtu") n_sub_spaces = space.num_sub_spaces() nfns += expand_sub_fns(fn) fns = nfns; del(nfns) spaces = [] lfns = OrderedDict() for fn in fns: space = fn.function_space() assert(space.mesh().id() == mesh.id()) e = space.ufl_element() assert(e.cell().geometric_dimension() == dim) assert(e.cell().topological_dimension() == dim) if (not e.family() in ["Lagrange", "Discontinuous Lagrange"] or not dim in [1, 2, 3] or (dim == 1 and not e.degree() in [1, 2, 3]) or (dim in [2, 3] and not e.degree() in [1, 2])) and \ (not e.family() == "Discontinuous Lagrange" or not dim in [1, 2, 3] or not e.degree() == 0): raise NotImplementedException('Element family "%s" with degree %i in %i dimension(s) not supported by write_vtu' % (e.family(), e.degree(), dim)) if e in lfns: lfns[e].append(fn) else: spaces.append(space) lfns[e] = [fn] fns = lfns if len(spaces) == 1: filenames = [filename] else: filenames = [] for space in spaces: e = space.ufl_element() lfilename = "%s_P%i" % (filename, e.degree()) if e.family() == "Discontinuous Lagrange": lfilename = "%s_DG" % lfilename filenames.append(lfilename) if not t is None: for space in spaces: lt = dolfin.Function(space, name = "time") if isinstance(t, float): lt.vector()[:] = t else: lt.assign(t) fns[space.ufl_element()].append(lt) names = {e:[] for e in fns} for e in fns: for fn in fns[e]: name = fn.name() if name in names[e]: raise InvalidArgumentException("Duplicate Function name: %s" % name) names[e].append(name) for filename, space in zip(filenames, spaces): e = space.ufl_element() degree = e.degree() vtu = vtk.vtkUnstructuredGrid() dof = space.dofmap() nodes = set() for i in xrange(mesh.num_cells()): cell = dof.cell_dofs(i) for node in cell: nodes.add(node) nodes = numpy.array(sorted(list(nodes)), dtype = numpy.intc) if degree == 0: xspace = dolfin.FunctionSpace(mesh, "CG", 1) xdof = xspace.dofmap() xnodes = set() for i in xrange(mesh.num_cells()): cell = xdof.cell_dofs(i) for node in cell: xnodes.add(node) xnodes = numpy.array(sorted(list(xnodes)), dtype = numpy.intc) else: xspace = space xdof = dof xnodes = nodes xnode_map = {node:i for i, node in enumerate(xnodes)} x = dolfin.interpolate(dolfin.Expression("x[0]"), xspace).vector().gather(xnodes) if dim > 1: y = dolfin.interpolate(dolfin.Expression("x[1]"), xspace).vector().gather(xnodes) if dim > 2: z = dolfin.interpolate(dolfin.Expression("x[2]"), xspace).vector().gather(xnodes) n = x.shape[0] points = vtk.vtkPoints() points.SetDataTypeToDouble() points.SetNumberOfPoints(n) if dim == 1: for i in xrange(n): points.SetPoint(i, x[i], 0.0, 0.0) elif dim == 2: for i in xrange(n): points.SetPoint(i, x[i], y[i], 0.0) else: for i in xrange(n): points.SetPoint(i, x[i], y[i], z[i]) vtu.SetPoints(points) id_list = vtk.vtkIdList() if dim == 1: if degree in [0, 1]: cell_type = vtk.vtkLine().GetCellType() id_list.SetNumberOfIds(2) cell_map = None elif degree == 2: cell_type = vtk.vtkQuadraticEdge().GetCellType() id_list.SetNumberOfIds(3) cell_map = None else: cell_type = vtk.vtkCubicLine().GetCellType() id_list.SetNumberOfIds(4) cell_map = None elif dim == 2: if degree in [0, 1]: cell_type = vtk.vtkTriangle().GetCellType() id_list.SetNumberOfIds(3) cell_map = None else: cell_type = vtk.vtkQuadraticTriangle().GetCellType() id_list.SetNumberOfIds(6) cell_map = {0:0, 1:1, 2:2, 3:5, 4:3, 5:4} else: if degree in [0, 1]: cell_type = vtk.vtkTetra().GetCellType() id_list.SetNumberOfIds(4) cell_map = None else: cell_type = vtk.vtkQuadraticTetra().GetCellType() id_list.SetNumberOfIds(10) cell_map = {0:0, 1:1, 2:2, 3:3, 4:9, 5:6, 6:8, 7:7, 8:5, 9:4} for i in xrange(mesh.num_cells()): cell = xdof.cell_dofs(i) assert(len(cell) == id_list.GetNumberOfIds()) if not cell_map is None: cell = [cell[cell_map[j]] for j in xrange(len(cell))] for j in xrange(len(cell)): id_list.SetId(j, xnode_map[cell[j]]) vtu.InsertNextCell(cell_type, id_list) if degree == 0: for fn in fns[e]: if not fn.value_rank() == 0: raise NotImplementException("Function rank %i not supported by write_vtu" % fn.value_rank()) data = fn.vector().gather(nodes) cell_data = vtk.vtkDoubleArray() cell_data.SetNumberOfComponents(1) cell_data.SetNumberOfValues(mesh.num_cells()) cell_data.SetName(fn.name()) for i, datum in enumerate(data): cell_data.SetValue(i, datum) vtu.GetCellData().AddArray(cell_data) vtu.GetCellData().SetActiveScalars(names[e][0]) else: for fn in fns[e]: if not fn.value_rank() == 0: raise NotImplementException("Function rank %i not supported by write_vtu" % fn.value_rank()) data = fn.vector().gather(nodes) point_data = vtk.vtkDoubleArray() point_data.SetNumberOfComponents(1) point_data.SetNumberOfValues(n) point_data.SetName(fn.name()) for i, datum in enumerate(data): point_data.SetValue(i, datum) vtu.GetPointData().AddArray(point_data) vtu.GetPointData().SetActiveScalars(names[e][0]) if dolfin.MPI.num_processes() > 1: writer = vtk.vtkXMLPUnstructuredGridWriter() writer.SetNumberOfPieces(dolfin.MPI.num_processes()) writer.SetStartPiece(dolfin.MPI.process_number()) writer.SetEndPiece(dolfin.MPI.process_number()) ext = ".pvtu" else: writer = vtk.vtkXMLUnstructuredGridWriter() ext = ".vtu" if index is None: filename = "%s%s" % (filename, ext) else: filename = "%s_%i%s" % (filename, index, ext) writer.SetFileName(filename) writer.SetDataModeToAppended() writer.EncodeAppendedDataOff() writer.SetCompressorTypeToZLib() writer.GetCompressor().SetCompressionLevel(9) writer.SetBlockSize(2 ** 15) writer.SetInput(vtu) writer.Write() if not writer.GetProgress() == 1.0 or not writer.GetErrorCode() == 0: raise IOException("Failed to write vtu file: %s" % filename) return
def mapElements(self, points, points2, nidMap, model, j): for eid, element in sorted(model.elements.iteritems()): if isinstance(element, CTRIA3): #print "ctria3" elem = vtkTriangle() nodeIDs = element.nodeIDs() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) self.grid.InsertNextCell( elem.GetCellType(), elem.GetPointIds()) elif isinstance(element, CTRIA6): nodeIDs = element.nodeIDs() if None not in nodeIDs: elem = vtkQuadraticTriangle() elem.GetPointIds().SetId(3, nidMap[nodeIDs[3]]) elem.GetPointIds().SetId(4, nidMap[nodeIDs[4]]) elem.GetPointIds().SetId(5, nidMap[nodeIDs[5]]) else: elem = vtkTriangle() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) self.grid.InsertNextCell( elem.GetCellType(), elem.GetPointIds()) elif isinstance(element, CQUAD4): nodeIDs = element.nodeIDs() elem = vtkQuad() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) elem.GetPointIds().SetId(3, nidMap[nodeIDs[3]]) self.grid.InsertNextCell( elem.GetCellType(), elem.GetPointIds()) elif isinstance(element, CQUAD8): nodeIDs = element.nodeIDs() if None not in nodeIDs: elem = vtkQuadraticQuad() elem.GetPointIds().SetId(4, nidMap[nodeIDs[4]]) elem.GetPointIds().SetId(5, nidMap[nodeIDs[5]]) elem.GetPointIds().SetId(6, nidMap[nodeIDs[6]]) elem.GetPointIds().SetId(7, nidMap[nodeIDs[7]]) else: elem = vtkQuad() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) elem.GetPointIds().SetId(3, nidMap[nodeIDs[3]]) self.grid.InsertNextCell( elem.GetCellType(), elem.GetPointIds()) elif isinstance(element, CTETRA4): elem = vtkTetra() nodeIDs = element.nodeIDs() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) elem.GetPointIds().SetId(3, nidMap[nodeIDs[3]]) self.grid.InsertNextCell( elem.GetCellType(), elem.GetPointIds()) elif isinstance(element, CTETRA10): nodeIDs = element.nodeIDs() if None not in nodeIDs: elem = vtkQuadraticTetra() elem.GetPointIds().SetId(4, nidMap[nodeIDs[4]]) elem.GetPointIds().SetId(5, nidMap[nodeIDs[5]]) elem.GetPointIds().SetId(6, nidMap[nodeIDs[6]]) elem.GetPointIds().SetId(7, nidMap[nodeIDs[7]]) elem.GetPointIds().SetId(8, nidMap[nodeIDs[8]]) elem.GetPointIds().SetId(9, nidMap[nodeIDs[9]]) else: elem = vtkTetra() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) elem.GetPointIds().SetId(3, nidMap[nodeIDs[3]]) self.grid.InsertNextCell( elem.GetCellType(), elem.GetPointIds()) elif isinstance(element, CPENTA6): elem = vtkWedge() nodeIDs = element.nodeIDs() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) elem.GetPointIds().SetId(3, nidMap[nodeIDs[3]]) elem.GetPointIds().SetId(4, nidMap[nodeIDs[4]]) elem.GetPointIds().SetId(5, nidMap[nodeIDs[5]]) self.grid.InsertNextCell( elem.GetCellType(), elem.GetPointIds()) elif isinstance(element, CPENTA15): nodeIDs = element.nodeIDs() if None not in nodeIDs: elem = vtkQuadraticWedge() elem.GetPointIds().SetId(6, nidMap[nodeIDs[6]]) elem.GetPointIds().SetId(7, nidMap[nodeIDs[7]]) elem.GetPointIds().SetId(8, nidMap[nodeIDs[8]]) elem.GetPointIds().SetId(9, nidMap[nodeIDs[9]]) elem.GetPointIds().SetId(10, nidMap[nodeIDs[10]]) elem.GetPointIds().SetId(11, nidMap[nodeIDs[11]]) elem.GetPointIds().SetId(12, nidMap[nodeIDs[12]]) elem.GetPointIds().SetId(13, nidMap[nodeIDs[13]]) elem.GetPointIds().SetId(14, nidMap[nodeIDs[14]]) else: elem = vtkWedge() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) elem.GetPointIds().SetId(3, nidMap[nodeIDs[3]]) elem.GetPointIds().SetId(4, nidMap[nodeIDs[4]]) elem.GetPointIds().SetId(5, nidMap[nodeIDs[5]]) self.grid.InsertNextCell( elem.GetCellType(), elem.GetPointIds()) elif isinstance(element, CHEXA8): nodeIDs = element.nodeIDs() elem = vtkHexahedron() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) elem.GetPointIds().SetId(3, nidMap[nodeIDs[3]]) elem.GetPointIds().SetId(4, nidMap[nodeIDs[4]]) elem.GetPointIds().SetId(5, nidMap[nodeIDs[5]]) elem.GetPointIds().SetId(6, nidMap[nodeIDs[6]]) elem.GetPointIds().SetId(7, nidMap[nodeIDs[7]]) self.grid.InsertNextCell( elem.GetCellType(), elem.GetPointIds()) elif isinstance(element, CHEXA20): nodeIDs = element.nodeIDs() #print "nodeIDs = ",nodeIDs if None not in nodeIDs: elem = vtkQuadraticHexahedron() elem.GetPointIds().SetId(8, nidMap[nodeIDs[8]]) elem.GetPointIds().SetId(9, nidMap[nodeIDs[9]]) elem.GetPointIds().SetId(10, nidMap[nodeIDs[10]]) elem.GetPointIds().SetId(11, nidMap[nodeIDs[11]]) elem.GetPointIds().SetId(12, nidMap[nodeIDs[12]]) elem.GetPointIds().SetId(13, nidMap[nodeIDs[13]]) elem.GetPointIds().SetId(14, nidMap[nodeIDs[14]]) elem.GetPointIds().SetId(15, nidMap[nodeIDs[15]]) elem.GetPointIds().SetId(16, nidMap[nodeIDs[16]]) elem.GetPointIds().SetId(17, nidMap[nodeIDs[17]]) elem.GetPointIds().SetId(18, nidMap[nodeIDs[18]]) elem.GetPointIds().SetId(19, nidMap[nodeIDs[19]]) else: elem = vtkHexahedron() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) elem.GetPointIds().SetId(3, nidMap[nodeIDs[3]]) elem.GetPointIds().SetId(4, nidMap[nodeIDs[4]]) elem.GetPointIds().SetId(5, nidMap[nodeIDs[5]]) elem.GetPointIds().SetId(6, nidMap[nodeIDs[6]]) elem.GetPointIds().SetId(7, nidMap[nodeIDs[7]]) self.grid.InsertNextCell( elem.GetCellType(), elem.GetPointIds()) elif isinstance(element, LineElement) or isinstance(element, SpringElement): elem = vtk.vtkLine() nodeIDs = element.nodeIDs() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) self.grid.InsertNextCell( elem.GetCellType(), elem.GetPointIds()) ### elif isinstance(element, CONM2): # not perfectly located nid = element.Nid() c = element.Centroid() elem = vtk.vtkVertex() #elem = vtk.vtkSphere() #elem.SetRadius(1.0) #print str(element) points2.InsertPoint(j, *c) elem.GetPointIds().SetId(0, j) #elem.SetCenter(points.GetPoint(nidMap[nid])) self.grid2.InsertNextCell( elem.GetCellType(), elem.GetPointIds()) j += 1 else: print "skipping %s" % (element.type) ### self.grid.SetPoints(points) self.grid2.SetPoints(points2) self.grid.Update() self.grid2.Update()
def mapElements(self, points, points2, nidMap, model, j): #self.eidMap = {} i = 0 for (eid, element) in sorted(model.elements.iteritems()): self.eidMap[eid] = i #print element.type if isinstance(element, CTRIA3) or isinstance(element, CTRIAR): elem = vtkTriangle() nodeIDs = element.nodeIDs() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) self.grid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) elif isinstance(element, CTRIA6): nodeIDs = element.nodeIDs() if None not in nodeIDs: elem = vtkQuadraticTriangle() elem.GetPointIds().SetId(3, nidMap[nodeIDs[3]]) elem.GetPointIds().SetId(4, nidMap[nodeIDs[4]]) elem.GetPointIds().SetId(5, nidMap[nodeIDs[5]]) else: elem = vtkTriangle() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) self.grid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) elif isinstance(element, CTRIAX6): # midside nodes are required, nodes out of order nodeIDs = element.nodeIDs() if None not in nodeIDs: elem = vtkQuadraticTriangle() elem.GetPointIds().SetId(3, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(4, nidMap[nodeIDs[3]]) elem.GetPointIds().SetId(5, nidMap[nodeIDs[5]]) else: elem = vtkTriangle() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[2]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[4]]) #a = [0,2,4] #msg = "CTRIAX6 %i %i %i" %(nidMap[nodeIDs[a[0]]], # nidMap[nodeIDs[a[1]]], # nidMap[nodeIDs[a[2]]] ) #raise RuntimeError(msg) #sys.stdout.flush() #elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) #elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) #elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) self.grid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) elif (isinstance(element, CQUAD4) or isinstance(element, CSHEAR) or isinstance(element, CQUADR)): nodeIDs = element.nodeIDs() elem = vtkQuad() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) elem.GetPointIds().SetId(3, nidMap[nodeIDs[3]]) self.grid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) elif isinstance(element, CQUAD8): nodeIDs = element.nodeIDs() if None not in nodeIDs: elem = vtkQuadraticQuad() elem.GetPointIds().SetId(4, nidMap[nodeIDs[4]]) elem.GetPointIds().SetId(5, nidMap[nodeIDs[5]]) elem.GetPointIds().SetId(6, nidMap[nodeIDs[6]]) elem.GetPointIds().SetId(7, nidMap[nodeIDs[7]]) else: elem = vtkQuad() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) elem.GetPointIds().SetId(3, nidMap[nodeIDs[3]]) self.grid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) elif isinstance(element, CTETRA4): elem = vtkTetra() nodeIDs = element.nodeIDs() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) elem.GetPointIds().SetId(3, nidMap[nodeIDs[3]]) self.grid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) elif isinstance(element, CTETRA10): nodeIDs = element.nodeIDs() if None not in nodeIDs: elem = vtkQuadraticTetra() elem.GetPointIds().SetId(4, nidMap[nodeIDs[4]]) elem.GetPointIds().SetId(5, nidMap[nodeIDs[5]]) elem.GetPointIds().SetId(6, nidMap[nodeIDs[6]]) elem.GetPointIds().SetId(7, nidMap[nodeIDs[7]]) elem.GetPointIds().SetId(8, nidMap[nodeIDs[8]]) elem.GetPointIds().SetId(9, nidMap[nodeIDs[9]]) else: elem = vtkTetra() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) elem.GetPointIds().SetId(3, nidMap[nodeIDs[3]]) self.grid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) elif isinstance(element, CPENTA6): elem = vtkWedge() nodeIDs = element.nodeIDs() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) elem.GetPointIds().SetId(3, nidMap[nodeIDs[3]]) elem.GetPointIds().SetId(4, nidMap[nodeIDs[4]]) elem.GetPointIds().SetId(5, nidMap[nodeIDs[5]]) self.grid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) elif isinstance(element, CPENTA15): nodeIDs = element.nodeIDs() if None not in nodeIDs: elem = vtkQuadraticWedge() elem.GetPointIds().SetId(6, nidMap[nodeIDs[6]]) elem.GetPointIds().SetId(7, nidMap[nodeIDs[7]]) elem.GetPointIds().SetId(8, nidMap[nodeIDs[8]]) elem.GetPointIds().SetId(9, nidMap[nodeIDs[9]]) elem.GetPointIds().SetId(10, nidMap[nodeIDs[10]]) elem.GetPointIds().SetId(11, nidMap[nodeIDs[11]]) elem.GetPointIds().SetId(12, nidMap[nodeIDs[12]]) elem.GetPointIds().SetId(13, nidMap[nodeIDs[13]]) elem.GetPointIds().SetId(14, nidMap[nodeIDs[14]]) else: elem = vtkWedge() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) elem.GetPointIds().SetId(3, nidMap[nodeIDs[3]]) elem.GetPointIds().SetId(4, nidMap[nodeIDs[4]]) elem.GetPointIds().SetId(5, nidMap[nodeIDs[5]]) self.grid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) elif isinstance(element, CHEXA8): nodeIDs = element.nodeIDs() elem = vtkHexahedron() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) elem.GetPointIds().SetId(3, nidMap[nodeIDs[3]]) elem.GetPointIds().SetId(4, nidMap[nodeIDs[4]]) elem.GetPointIds().SetId(5, nidMap[nodeIDs[5]]) elem.GetPointIds().SetId(6, nidMap[nodeIDs[6]]) elem.GetPointIds().SetId(7, nidMap[nodeIDs[7]]) self.grid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) elif isinstance(element, CHEXA20): nodeIDs = element.nodeIDs() #print "nodeIDs = ",nodeIDs if None not in nodeIDs: elem = vtkQuadraticHexahedron() elem.GetPointIds().SetId(8, nidMap[nodeIDs[8]]) elem.GetPointIds().SetId(9, nidMap[nodeIDs[9]]) elem.GetPointIds().SetId(10, nidMap[nodeIDs[10]]) elem.GetPointIds().SetId(11, nidMap[nodeIDs[11]]) elem.GetPointIds().SetId(12, nidMap[nodeIDs[12]]) elem.GetPointIds().SetId(13, nidMap[nodeIDs[13]]) elem.GetPointIds().SetId(14, nidMap[nodeIDs[14]]) elem.GetPointIds().SetId(15, nidMap[nodeIDs[15]]) elem.GetPointIds().SetId(16, nidMap[nodeIDs[16]]) elem.GetPointIds().SetId(17, nidMap[nodeIDs[17]]) elem.GetPointIds().SetId(18, nidMap[nodeIDs[18]]) elem.GetPointIds().SetId(19, nidMap[nodeIDs[19]]) else: elem = vtkHexahedron() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) elem.GetPointIds().SetId(3, nidMap[nodeIDs[3]]) elem.GetPointIds().SetId(4, nidMap[nodeIDs[4]]) elem.GetPointIds().SetId(5, nidMap[nodeIDs[5]]) elem.GetPointIds().SetId(6, nidMap[nodeIDs[6]]) elem.GetPointIds().SetId(7, nidMap[nodeIDs[7]]) self.grid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) elif (isinstance(element, LineElement) or isinstance(element, SpringElement) or element.type in ['CBUSH', 'CBUSH1D', 'CFAST', 'CROD', 'CONROD', 'CELAS1', 'CELAS2', 'CELAS3', 'CELAS4', 'CDAMP1', 'CDAMP2', 'CDAMP3', 'CDAMP4', 'CDAMP5', 'CVISC', ]): nodeIDs = element.nodeIDs() if None not in nodeIDs: # used to be 0... elem = vtk.vtkLine() try: elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) except KeyError: print "nodeIDs =", nodeIDs print str(element) continue self.grid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) elif isinstance(element, CONM2): # not perfectly located del self.eidMap[eid] i -= 1 #nid = element.Nid() c = element.Centroid() elem = vtk.vtkVertex() #elem = vtk.vtkSphere() #elem.SetRadius(1.0) #print str(element) points2.InsertPoint(j, *c) elem.GetPointIds().SetId(0, j) #elem.SetCenter(points.GetPoint(nidMap[nid])) self.grid2.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) j += 1 else: del self.eidMap[eid] self.log_info("skipping %s" % element.type) continue i += 1 self.grid.SetPoints(points) self.grid2.SetPoints(points2) #self.grid.GetPointData().SetScalars(self.gridResult) #print dir(self.grid) #.SetNumberOfComponents(0) #self.grid.GetCellData().SetNumberOfTuples(1); #self.grid.GetCellData().SetScalars(self.gridResult) self.grid.Modified() self.grid2.Modified() self.grid.Update() self.grid2.Update() self.log_info("updated grid") cases = {} nelements = len(model.elements) print "len(elements) =", nelements pids = [] # zeros(nelements, 'int32') nxs = [] nys = [] nzs = [] i = 0 for eid, element in sorted(model.elements.iteritems()): pids.append(element.Pid()) if isinstance(element, ShellElement): (nx, ny, nz) = element.Normal() else: nx = ny = nz = 0.0 nxs.append(nx) nys.append(ny) nzs.append(nz) self.iSubcaseNameMap = {1: ['Nastran', '']} # subcaseID, resultType, vectorSize, location, dataFormat if 1: cases[(0, 'Pid', 1, 'centroid', '%.0f')] = pids # if not a flat plate??? #if min(nxs) == max(nxs) and min(nxs) != 0.0: # subcaseID, resultType, vectorSize, location, dataFormat cases[(0, 'Normal_x', 1, 'centroid', '%.1f')] = nxs cases[(0, 'Normal_y', 1, 'centroid', '%.1f')] = nys cases[(0, 'Normal_z', 1, 'centroid', '%.1f')] = nzs self.log.info(cases.keys()) self.finish_io(cases)
def mapElements(self, points, points2, nidMap, model, j): self.eidMap = {} i = 0 for (eid, element) in sorted(model.elements.iteritems()): self.eidMap[eid] = i #print element.type if isinstance(element, CTRIA3) or isinstance(element, CTRIAR): #print "ctria3" elem = vtkTriangle() nodeIDs = element.nodeIDs() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) self.grid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) elif isinstance(element, CTRIA6): nodeIDs = element.nodeIDs() if None not in nodeIDs: elem = vtkQuadraticTriangle() elem.GetPointIds().SetId(3, nidMap[nodeIDs[3]]) elem.GetPointIds().SetId(4, nidMap[nodeIDs[4]]) elem.GetPointIds().SetId(5, nidMap[nodeIDs[5]]) else: elem = vtkTriangle() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) self.grid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) elif isinstance(element, CTRIAX6): # midside nodes are required, nodes out of order nodeIDs = element.nodeIDs() if None not in nodeIDs: elem = vtkQuadraticTriangle() elem.GetPointIds().SetId(3, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(4, nidMap[nodeIDs[3]]) elem.GetPointIds().SetId(5, nidMap[nodeIDs[5]]) else: elem = vtkTriangle() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[2]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[4]]) #a = [0,2,4] #msg = "CTRIAX6 %i %i %i" %(nidMap[nodeIDs[a[0]]], # nidMap[nodeIDs[a[1]]], # nidMap[nodeIDs[a[2]]] ) #raise RuntimeError(msg) #sys.stdout.flush() #elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) #elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) #elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) self.grid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) elif (isinstance(element, CQUAD4) or isinstance(element, CSHEAR) or isinstance(element, CQUADR)): nodeIDs = element.nodeIDs() elem = vtkQuad() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) elem.GetPointIds().SetId(3, nidMap[nodeIDs[3]]) self.grid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) elif isinstance(element, CQUAD8): nodeIDs = element.nodeIDs() if None not in nodeIDs: elem = vtkQuadraticQuad() elem.GetPointIds().SetId(4, nidMap[nodeIDs[4]]) elem.GetPointIds().SetId(5, nidMap[nodeIDs[5]]) elem.GetPointIds().SetId(6, nidMap[nodeIDs[6]]) elem.GetPointIds().SetId(7, nidMap[nodeIDs[7]]) else: elem = vtkQuad() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) elem.GetPointIds().SetId(3, nidMap[nodeIDs[3]]) self.grid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) elif isinstance(element, CTETRA4): elem = vtkTetra() nodeIDs = element.nodeIDs() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) elem.GetPointIds().SetId(3, nidMap[nodeIDs[3]]) self.grid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) elif isinstance(element, CTETRA10): nodeIDs = element.nodeIDs() if None not in nodeIDs: elem = vtkQuadraticTetra() elem.GetPointIds().SetId(4, nidMap[nodeIDs[4]]) elem.GetPointIds().SetId(5, nidMap[nodeIDs[5]]) elem.GetPointIds().SetId(6, nidMap[nodeIDs[6]]) elem.GetPointIds().SetId(7, nidMap[nodeIDs[7]]) elem.GetPointIds().SetId(8, nidMap[nodeIDs[8]]) elem.GetPointIds().SetId(9, nidMap[nodeIDs[9]]) else: elem = vtkTetra() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) elem.GetPointIds().SetId(3, nidMap[nodeIDs[3]]) self.grid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) elif isinstance(element, CPENTA6): elem = vtkWedge() nodeIDs = element.nodeIDs() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) elem.GetPointIds().SetId(3, nidMap[nodeIDs[3]]) elem.GetPointIds().SetId(4, nidMap[nodeIDs[4]]) elem.GetPointIds().SetId(5, nidMap[nodeIDs[5]]) self.grid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) elif isinstance(element, CPENTA15): nodeIDs = element.nodeIDs() if None not in nodeIDs: elem = vtkQuadraticWedge() elem.GetPointIds().SetId(6, nidMap[nodeIDs[6]]) elem.GetPointIds().SetId(7, nidMap[nodeIDs[7]]) elem.GetPointIds().SetId(8, nidMap[nodeIDs[8]]) elem.GetPointIds().SetId(9, nidMap[nodeIDs[9]]) elem.GetPointIds().SetId(10, nidMap[nodeIDs[10]]) elem.GetPointIds().SetId(11, nidMap[nodeIDs[11]]) elem.GetPointIds().SetId(12, nidMap[nodeIDs[12]]) elem.GetPointIds().SetId(13, nidMap[nodeIDs[13]]) elem.GetPointIds().SetId(14, nidMap[nodeIDs[14]]) else: elem = vtkWedge() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) elem.GetPointIds().SetId(3, nidMap[nodeIDs[3]]) elem.GetPointIds().SetId(4, nidMap[nodeIDs[4]]) elem.GetPointIds().SetId(5, nidMap[nodeIDs[5]]) self.grid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) elif isinstance(element, CHEXA8): nodeIDs = element.nodeIDs() elem = vtkHexahedron() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) elem.GetPointIds().SetId(3, nidMap[nodeIDs[3]]) elem.GetPointIds().SetId(4, nidMap[nodeIDs[4]]) elem.GetPointIds().SetId(5, nidMap[nodeIDs[5]]) elem.GetPointIds().SetId(6, nidMap[nodeIDs[6]]) elem.GetPointIds().SetId(7, nidMap[nodeIDs[7]]) self.grid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) elif isinstance(element, CHEXA20): nodeIDs = element.nodeIDs() #print "nodeIDs = ",nodeIDs if None not in nodeIDs: elem = vtkQuadraticHexahedron() elem.GetPointIds().SetId(8, nidMap[nodeIDs[8]]) elem.GetPointIds().SetId(9, nidMap[nodeIDs[9]]) elem.GetPointIds().SetId(10, nidMap[nodeIDs[10]]) elem.GetPointIds().SetId(11, nidMap[nodeIDs[11]]) elem.GetPointIds().SetId(12, nidMap[nodeIDs[12]]) elem.GetPointIds().SetId(13, nidMap[nodeIDs[13]]) elem.GetPointIds().SetId(14, nidMap[nodeIDs[14]]) elem.GetPointIds().SetId(15, nidMap[nodeIDs[15]]) elem.GetPointIds().SetId(16, nidMap[nodeIDs[16]]) elem.GetPointIds().SetId(17, nidMap[nodeIDs[17]]) elem.GetPointIds().SetId(18, nidMap[nodeIDs[18]]) elem.GetPointIds().SetId(19, nidMap[nodeIDs[19]]) else: elem = vtkHexahedron() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) elem.GetPointIds().SetId(3, nidMap[nodeIDs[3]]) elem.GetPointIds().SetId(4, nidMap[nodeIDs[4]]) elem.GetPointIds().SetId(5, nidMap[nodeIDs[5]]) elem.GetPointIds().SetId(6, nidMap[nodeIDs[6]]) elem.GetPointIds().SetId(7, nidMap[nodeIDs[7]]) self.grid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) elif (isinstance(element, LineElement) or isinstance(element, SpringElement)): elem = vtk.vtkLine() nodeIDs = element.nodeIDs() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) self.grid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) ### elif isinstance(element, CONM2): # not perfectly located del self.eidMap[eid] i -= 1 nid = element.Nid() c = element.Centroid() elem = vtk.vtkVertex() #elem = vtk.vtkSphere() #elem.SetRadius(1.0) #print str(element) points2.InsertPoint(j, *c) elem.GetPointIds().SetId(0, j) #elem.SetCenter(points.GetPoint(nidMap[nid])) self.grid2.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) j += 1 else: del self.eidMap[eid] i -= 1 print("skipping %s" % (element.type)) i += 1 ### self.grid.SetPoints(points) self.grid2.SetPoints(points2) #self.grid.GetPointData().SetScalars(self.gridResult) #print dir(self.grid) #.SetNumberOfComponents(0) #self.grid.GetCellData().SetNumberOfTuples(1); #self.grid.GetCellData().SetScalars(self.gridResult) self.grid.Modified() self.grid2.Modified() self.grid.Update() self.grid2.Update() print("updated grid")
def main(): titles = list() textMappers = list() textActors = list() uGrids = list() mappers = list() actors = list() renderers = list() uGrids.append(MakeUnstructuredGrid(vtk.vtkQuadraticEdge())) titles.append("VTK_QUADRATIC_EDGE (= 21)") uGrids.append(MakeUnstructuredGrid(vtk.vtkQuadraticTriangle())) titles.append("VTK_QUADRATIC_TRIANGLE (= 22)") uGrids.append(MakeUnstructuredGrid(vtk.vtkQuadraticQuad())) titles.append("VTK_QUADRATIC_QUAD (= 23)") uGrids.append(MakeQuadraticPolygon()) titles.append("VTK_QUADRATIC_POLYGON (= 36)") uGrids.append(MakeUnstructuredGrid(vtk.vtkQuadraticTetra())) titles.append("VTK_QUADRATIC_TETRA (= 24)") uGrids.append(MakeUnstructuredGrid(vtk.vtkQuadraticHexahedron())) titles.append("VTK_QUADRATIC_HEXAHEDRON (= 25)") uGrids.append(MakeUnstructuredGrid(vtk.vtkQuadraticWedge())) titles.append("VTK_QUADRATIC_WEDGE (= 26)") uGrids.append(MakeUnstructuredGrid(vtk.vtkQuadraticPyramid())) titles.append("VTK_QUADRATIC_PYRAMID (= 27)") uGrids.append(MakeUnstructuredGrid(vtk.vtkBiQuadraticQuad())) titles.append("VTK_BIQUADRATIC_QUAD (= 28)") uGrids.append(MakeUnstructuredGrid(vtk.vtkTriQuadraticHexahedron())) titles.append("VTK_TRIQUADRATIC_HEXAHEDRON (= 29)") uGrids.append(MakeUnstructuredGrid(vtk.vtkQuadraticLinearQuad())) titles.append("VTK_QUADRATIC_LINEAR_QUAD (= 30)") uGrids.append(MakeUnstructuredGrid(vtk.vtkQuadraticLinearWedge())) titles.append("VTK_QUADRATIC_LINEAR_WEDGE (= 31)") uGrids.append(MakeUnstructuredGrid(vtk.vtkBiQuadraticQuadraticWedge())) titles.append("VTK_BIQUADRATIC_QUADRATIC_WEDGE (= 32)") uGrids.append(MakeUnstructuredGrid( vtk.vtkBiQuadraticQuadraticHexahedron())) titles.append("VTK_BIQUADRATIC_QUADRATIC_HEXAHEDRON (= 33)") uGrids.append(MakeUnstructuredGrid(vtk.vtkBiQuadraticTriangle())) titles.append("VTK_BIQUADRATIC_TRIANGLE (= 34)") uGrids.append(MakeUnstructuredGrid(vtk.vtkCubicLine())) titles.append("VTK_CUBIC_LINE (= 35)") colors = vtk.vtkNamedColors() renWin = vtk.vtkRenderWindow() renWin.SetSize(600, 600) renWin.SetWindowName("Isoparametric Cell") iRen = vtk.vtkRenderWindowInteractor() iRen.SetRenderWindow(renWin) # Create one sphere for all sphere = vtk.vtkSphereSource() sphere.SetPhiResolution(21) sphere.SetThetaResolution(21) sphere.SetRadius(.08) # Create one text property for all textProperty = vtk.vtkTextProperty() textProperty.SetFontSize(10) textProperty.SetJustificationToCentered() # Create and link the mappers actors and renderers together. for i in range(0, len(uGrids)): print("Creating:", titles[i]) textMappers.append(vtk.vtkTextMapper()) textActors.append(vtk.vtkActor2D()) textMappers[i].GetTextProperty().SetFontSize(10) textMappers[i].GetTextProperty().ShadowOn() mappers.append(vtk.vtkDataSetMapper()) actors.append(vtk.vtkActor()) renderers.append(vtk.vtkRenderer()) mappers[i].SetInputData(uGrids[i]) actors[i].SetMapper(mappers[i]) actors[i].GetProperty().SetColor(colors.GetColor3d("Tomato")) actors[i].GetProperty().EdgeVisibilityOn() actors[i].GetProperty().SetLineWidth(3) actors[i].GetProperty().SetOpacity(.5) renderers[i].AddViewProp(actors[i]) textMappers[i].SetInput(titles[i]) textActors[i].SetMapper(textMappers[i]) textActors[i].SetPosition(50, 10) renderers[i].AddViewProp(textActors[i]) # Label the points labelMapper = vtk.vtkLabeledDataMapper() labelMapper.SetInputData(uGrids[i]) labelActor = vtk.vtkActor2D() labelActor.SetMapper(labelMapper) renderers[i].AddViewProp(labelActor) # Glyph the points pointMapper = vtk.vtkGlyph3DMapper() pointMapper.SetInputData(uGrids[i]) pointMapper.SetSourceConnection(sphere.GetOutputPort()) pointMapper.ScalingOff() pointMapper.ScalarVisibilityOff() pointActor = vtk.vtkActor() pointActor.SetMapper(pointMapper) pointActor.GetProperty().SetDiffuseColor(colors.GetColor3d("Banana")) pointActor.GetProperty().SetSpecular(.6) pointActor.GetProperty().SetSpecularColor(1.0, 1.0, 1.0) pointActor.GetProperty().SetSpecularPower(100) renderers[i].AddViewProp(pointActor) renWin.AddRenderer(renderers[i]) # Setup the viewports xGridDimensions = 4 yGridDimensions = 4 rendererSize = 240 renWin.SetSize(rendererSize * xGridDimensions, rendererSize * yGridDimensions) for row in range(0, yGridDimensions): for col in range(0, xGridDimensions): index = row * xGridDimensions + col # (xmin, ymin, xmax, ymax) viewport = [ float(col) / xGridDimensions, float(yGridDimensions - (row + 1)) / yGridDimensions, float(col + 1) / xGridDimensions, float(yGridDimensions - row) / yGridDimensions ] if index > (len(actors) - 1): # Add a renderer even if there is no actor. # This makes the render window background all the same color. ren = vtk.vtkRenderer() ren.SetBackground(colors.GetColor3d("SlateGray")) ren.SetViewport(viewport) renWin.AddRenderer(ren) continue renderers[index].SetViewport(viewport) renderers[index].SetBackground(colors.GetColor3d("SlateGray")) renderers[index].ResetCamera() renderers[index].GetActiveCamera().Azimuth(30) renderers[index].GetActiveCamera().Elevation(-30) renderers[index].ResetCameraClippingRange() iRen.Initialize() renWin.Render() iRen.Start()
def from_fea(mesh: pywim.fea.model.Mesh, inc: pywim.fea.result.Increment, outputs: List[str]): points = vtk.vtkPoints() points.SetNumberOfPoints(len(mesh.nodes)) for n in mesh.nodes: points.SetPoint(n.id - 1, n.x, n.y, n.z) grid = vtk.vtkUnstructuredGrid() grid.SetPoints(points) nels = 0 for g in mesh.elements: for c in g.connectivity: if g.type == 'HEXL8' or g.type == 'VOXL' or g.type == 'VOXLA': e = vtk.vtkHexahedron() elif g.type == 'TETL4': e = vtk.vtkTetra() elif g.type == 'WEDL6': e = vtk.vtkWedge() elif g.type == 'PSL4': e = vtk.vtkQuad() elif g.type == 'PSL3': e = vtk.vtkTriangle() elif g.type == 'PSQ6': e = vtk.vtkQuadraticTriangle() else: raise Exception('Unsupported element type: %s' % g.type) ids = e.GetPointIds() i = 0 for nid in c.nodes: ids.SetId(i, nid - 1) i += 1 grid.InsertNextCell(e.GetCellType(), ids) nels += 1 celldata = grid.GetCellData() pointdata = grid.GetPointData() def add_node_results(res: List[pywim.fea.result.Result]): array = vtk.vtkFloatArray() array.SetName(res.name) array.SetNumberOfComponents(res.size) for v in res.values: if res.size == 1: array.InsertNextTuple1(v.data[0]) elif res.size == 3: array.InsertNextTuple3(v.data[0], v.data[1], v.data[2]) elif res.size == 6: array.InsertNextTuple6(v.data[0], v.data[1], v.data[2], v.data[3], v.data[5], v.data[4]) pointdata.AddArray(array) def add_gp_results(res: List[pywim.fea.result.ResultMult]): ngps = 1 for v in res.values: ngps = max(ngps, len(v.values)) # Build a dictionary of element Id to index for quicker searching eid2index = {} index = 0 nlayers = 0 nsectpts = 0 nonlay_ngps = 0 for v in res.values: eid2index[v.id] = index index += 1 if v.values[0].layer == 0: nonlay_ngps = max(nonlay_ngps, len(v.values)) else: nlayers = max(nlayers, max([sv.layer for sv in v.values])) nsectpts = max(nsectpts, max([sv.section_point for sv in v.values])) lay_gp_iter = None nonlay_gp_iter = list(range(nonlay_ngps)) if nlayers > 0: ngps = int(round(ngps / (nlayers * nsectpts))) lay_gp_iter = [] for l in range(nlayers): for k in range(nsectpts): for g in range(ngps): lay_gp_iter.append((l, k, g)) # This is a severe limitation right now: # For layered data we are assuming all elements have the same number of # layers and section points, but we do check each element for total # gauss # pts to handle differences (e.g. WEDL6 vs HEXL8) def get_gauss_point_data(layered_output, elv, gpid): if not layered_output: g = gpid g_out_of_range = elv.values[0].layer > 0 else: this_ngps = int(round(len(elv.values) / (nlayers * nsectpts))) g = this_ngps * (gpid[0] * nsectpts + gpid[1]) + gpid[2] g_out_of_range = gpid[2] >= this_ngps or elv.values[ 0].layer == 0 if len(elv.values) < (g + 1) or g_out_of_range: return [0., 0., 0., 0., 0., 0.] else: return elv.values[g].data for gp_iter in (nonlay_gp_iter, lay_gp_iter): if gp_iter is None: continue for gp in gp_iter: if type(gp) is int: layered_output = False out_name = '{}_G{}'.format(res.name, gp + 1) else: layered_output = True out_name = '{}_L{}_K{}_G{}'.format(res.name, gp[0] + 1, gp[1] + 1, gp[2] + 1) array = vtk.vtkFloatArray() array.SetName(out_name) array.SetNumberOfComponents(res.size) for eid in range(1, nels + 1): elv = res.values[eid2index[eid]] if elv.id != eid: print('Element id mismatch: {} != {}'.format( eid, elv.id)) gpdata = get_gauss_point_data(layered_output, elv, gp) # last two vals intentionally swapped because VTU ordering is XX, YY, ZZ, XY, YZ, XZ if res.size == 1: array.InsertNextTuple1(gpdata[0]) elif res.size == 3: array.InsertNextTuple3(gpdata[0], gpdata[1], gpdata[2]) elif res.size == 6: array.InsertNextTuple6(gpdata[0], gpdata[1], gpdata[2], gpdata[3], gpdata[5], gpdata[4]) celldata.AddArray(array) def add_elem_results(res: List[pywim.fea.result.Result]): array = vtk.vtkFloatArray() array.SetName(res.name) array.SetNumberOfComponents(res.size) for e in res.values: if res.size == 1: array.InsertNextTuple1(e.data[0]) elif res.size == 3: array.InsertNextTuple3(e.data[0], e.data[1], e.data[2]) elif res.size == 6: array.InsertNextTuple6(e.data[0], e.data[1], e.data[2], e.data[3], e.data[5], e.data[4]) celldata.AddArray(array) def add_region_results_by_element(reg_result: RegionResult): elem_stats = reg_result.element_stats() wall_array = vtk.vtkFloatArray() wall_array.SetName('{}_wall'.format(reg_result.name)) wall_array.SetNumberOfComponents(3) skin_array = vtk.vtkFloatArray() skin_array.SetName('{}_skin'.format(reg_result.name)) skin_array.SetNumberOfComponents(3) infill_array = vtk.vtkFloatArray() infill_array.SetName('{}_infill'.format(reg_result.name)) infill_array.SetNumberOfComponents(3) for e in elem_stats.element_regions: if len(e.walls.gauss_point_data) > 0: wall_array.InsertNextTuple3(e.walls.min, e.walls.mean, e.walls.max) else: wall_array.InsertNextTuple3(float('NaN'), float('NaN'), float('NaN')) if len(e.skin.gauss_point_data) > 0: skin_array.InsertNextTuple3(e.skin.min, e.skin.mean, e.skin.max) else: skin_array.InsertNextTuple3(float('NaN'), float('NaN'), float('NaN')) if len(e.infill.gauss_point_data) > 0: infill_array.InsertNextTuple3(e.infill.min, e.infill.mean, e.infill.max) else: infill_array.InsertNextTuple3(float('NaN'), float('NaN'), float('NaN')) celldata.AddArray(wall_array) celldata.AddArray(skin_array) celldata.AddArray(infill_array) if 'node' in outputs: for res in inc.node_results: print('\t\tTranslating {} Node Result'.format(res.name)) add_node_results(res) if 'element' in outputs: for res in inc.element_results: print('\t\tTranslating {} Element Result'.format(res.name)) add_elem_results(res) ''' For now, region specific results are restricted to gauss point data. ''' if 'region' in outputs: try: mat_type = inc.gauss_point_results['material_type'] except StopIteration: raise Exception( 'No material_type information at the gauss points exists. Unable to detect regions.' ) for res in inc.gauss_point_results: if res.name == 'material_type': continue reg_result = region_filter(mat_type, res) if reg_result is None: print( '\tRegion filter on {} gauss point result not supported, skipping this result' .format(res.name)) continue print('\t\tTranslating {} Region Result By Element'.format( res.name)) add_region_results_by_element(reg_result) if 'gauss_point' in outputs: for res in inc.gauss_point_results: if res.name == 'material_type': continue print('\t\tTranslating {} Gauss Point Result'.format(res.name)) add_gp_results(res) return grid
def write_vtu(filename, fns, index=None, t=None): """ Write the supplied Function or Function s to a vtu or pvtu file with the supplied filename base. If the Function s are defined on multiple function spaces then separate output files are written for each function space. The optional integer index can be used to add an index to the output filenames. If t is supplied then a scalar field equal to t and with name "time" is added to the output files. All Function s should be on the same mesh and have unique names. In 1D all Function s must have Lagrange basis functions (continuous or discontinous) with degree 0 to 3. In 2D and 3D all Function s must have Lagrange basis functions (continuous or discontinuous) with degree 0 to 2. """ if isinstance(fns, dolfin.Function): return write_vtu(filename, [fns], index=index, t=t) if not isinstance(filename, str): raise InvalidArgumentException("filename must be a string") if not isinstance(fns, list): raise InvalidArgumentException( "fns must be a Function or a list of Function s") if len(fns) == 0: raise InvalidArgumentException( "fns must include at least one Function") for fn in fns: if not isinstance(fn, dolfin.Function): raise InvalidArgumentException( "fns must be a Function or a list of Function s") if not index is None and not isinstance(index, int): raise InvalidArgumentException("index must be an integer") if not t is None and not isinstance(t, (float, dolfin.Constant)): raise InvalidArgumentException("t must be a float or Constant") mesh = fns[0].function_space().mesh() dim = mesh.geometry().dim() if not dim in [1, 2, 3]: raise NotImplementedException( "Mesh dimension %i not supported by write_vtu" % dim) def expand_sub_fns(fn): n_sub_spaces = fn.function_space().num_sub_spaces() if n_sub_spaces > 1: fns = fn.split(deepcopy=True) for i, sfn in enumerate(copy.copy(fns)): sfn.rename("%s_%i" % (fn.name(), i + 1), "%s_%i" % (fn.name(), i + 1)) if sfn.function_space().num_sub_spaces() > 0: fns.remove(sfn) fns += expand_sub_fns(sfn) return fns elif n_sub_spaces == 1: e = fn.function_space().ufl_element().extract_component(0)[1] space = dolfin.FunctionSpace(mesh, e.family(), e.degree()) sfn = dolfin.Function(space, name="%s_1" % fn.name()) sfn.vector()[:] = fn.vector() return [sfn] else: return [fn] nfns = [] for i, fn in enumerate(fns): space = fn.function_space() if not space.mesh().id() == mesh.id(): raise InvalidArgumentException( "Require exactly one mesh in write_vtu") n_sub_spaces = space.num_sub_spaces() nfns += expand_sub_fns(fn) fns = nfns del (nfns) spaces = [] lfns = OrderedDict() for fn in fns: space = fn.function_space() assert (space.mesh().id() == mesh.id()) e = space.ufl_element() assert (e.cell().geometric_dimension() == dim) assert (e.cell().topological_dimension() == dim) if (not e.family() in ["Lagrange", "Discontinuous Lagrange"] or not dim in [1, 2, 3] or (dim == 1 and not e.degree() in [1, 2, 3]) or (dim in [2, 3] and not e.degree() in [1, 2])) and \ (not e.family() == "Discontinuous Lagrange" or not dim in [1, 2, 3] or not e.degree() == 0): raise NotImplementedException( 'Element family "%s" with degree %i in %i dimension(s) not supported by write_vtu' % (e.family(), e.degree(), dim)) if e in lfns: lfns[e].append(fn) else: spaces.append(space) lfns[e] = [fn] fns = lfns if len(spaces) == 1: filenames = [filename] else: filenames = [] for space in spaces: e = space.ufl_element() lfilename = "%s_P%i" % (filename, e.degree()) if e.family() == "Discontinuous Lagrange": lfilename = "%s_DG" % lfilename filenames.append(lfilename) if not t is None: for space in spaces: lt = dolfin.Function(space, name="time") if isinstance(t, float): lt.vector()[:] = t else: lt.assign(t) fns[space.ufl_element()].append(lt) names = {e: [] for e in fns} for e in fns: for fn in fns[e]: name = fn.name() if name in names[e]: raise InvalidArgumentException("Duplicate Function name: %s" % name) names[e].append(name) for filename, space in zip(filenames, spaces): e = space.ufl_element() degree = e.degree() vtu = vtk.vtkUnstructuredGrid() dof = space.dofmap() nodes = set() for i in range(mesh.num_cells()): cell = dof.cell_dofs(i) for node in cell: nodes.add(node) nodes = numpy.array(sorted(list(nodes)), dtype=numpy.intc) if degree == 0: xspace = dolfin.FunctionSpace(mesh, "CG", 1) xdof = xspace.dofmap() xnodes = set() for i in range(mesh.num_cells()): cell = xdof.cell_dofs(i) for node in cell: xnodes.add(node) xnodes = numpy.array(sorted(list(xnodes)), dtype=numpy.intc) else: xspace = space xdof = dof xnodes = nodes xnode_map = {node: i for i, node in enumerate(xnodes)} x = dolfin.interpolate(dolfin.Expression("x[0]"), xspace).vector().gather(xnodes) if dim > 1: y = dolfin.interpolate(dolfin.Expression("x[1]"), xspace).vector().gather(xnodes) if dim > 2: z = dolfin.interpolate(dolfin.Expression("x[2]"), xspace).vector().gather(xnodes) n = x.shape[0] points = vtk.vtkPoints() points.SetDataTypeToDouble() points.SetNumberOfPoints(n) if dim == 1: for i in range(n): points.SetPoint(i, x[i], 0.0, 0.0) elif dim == 2: for i in range(n): points.SetPoint(i, x[i], y[i], 0.0) else: for i in range(n): points.SetPoint(i, x[i], y[i], z[i]) vtu.SetPoints(points) id_list = vtk.vtkIdList() if dim == 1: if degree in [0, 1]: cell_type = vtk.vtkLine().GetCellType() id_list.SetNumberOfIds(2) cell_map = None elif degree == 2: cell_type = vtk.vtkQuadraticEdge().GetCellType() id_list.SetNumberOfIds(3) cell_map = None else: cell_type = vtk.vtkCubicLine().GetCellType() id_list.SetNumberOfIds(4) cell_map = None elif dim == 2: if degree in [0, 1]: cell_type = vtk.vtkTriangle().GetCellType() id_list.SetNumberOfIds(3) cell_map = None else: cell_type = vtk.vtkQuadraticTriangle().GetCellType() id_list.SetNumberOfIds(6) cell_map = {0: 0, 1: 1, 2: 2, 3: 5, 4: 3, 5: 4} else: if degree in [0, 1]: cell_type = vtk.vtkTetra().GetCellType() id_list.SetNumberOfIds(4) cell_map = None else: cell_type = vtk.vtkQuadraticTetra().GetCellType() id_list.SetNumberOfIds(10) cell_map = { 0: 0, 1: 1, 2: 2, 3: 3, 4: 9, 5: 6, 6: 8, 7: 7, 8: 5, 9: 4 } for i in range(mesh.num_cells()): cell = xdof.cell_dofs(i) assert (len(cell) == id_list.GetNumberOfIds()) if not cell_map is None: cell = [cell[cell_map[j]] for j in range(len(cell))] for j in range(len(cell)): id_list.SetId(j, xnode_map[cell[j]]) vtu.InsertNextCell(cell_type, id_list) if degree == 0: for fn in fns[e]: if not fn.value_rank() == 0: raise NotImplementedException( "Function rank %i not supported by write_vtu" % fn.value_rank()) data = fn.vector().gather(nodes) cell_data = vtk.vtkDoubleArray() cell_data.SetNumberOfComponents(1) cell_data.SetNumberOfValues(mesh.num_cells()) cell_data.SetName(fn.name()) for i, datum in enumerate(data): cell_data.SetValue(i, datum) vtu.GetCellData().AddArray(cell_data) vtu.GetCellData().SetActiveScalars(names[e][0]) else: for fn in fns[e]: if not fn.value_rank() == 0: raise NotImplementedException( "Function rank %i not supported by write_vtu" % fn.value_rank()) data = fn.vector().gather(nodes) point_data = vtk.vtkDoubleArray() point_data.SetNumberOfComponents(1) point_data.SetNumberOfValues(n) point_data.SetName(fn.name()) for i, datum in enumerate(data): point_data.SetValue(i, datum) vtu.GetPointData().AddArray(point_data) vtu.GetPointData().SetActiveScalars(names[e][0]) if dolfin.MPI.size(dolfin.mpi_comm_world()) > 1: writer = vtk.vtkXMLPUnstructuredGridWriter() writer.SetNumberOfPieces(dolfin.MPI.size(dolfin.mpi_comm_world())) writer.SetStartPiece(dolfin.MPI.rank(dolfin.mpi_comm_world())) writer.SetEndPiece(dolfin.MPI.rank(dolfin.mpi_comm_world())) ext = ".pvtu" else: writer = vtk.vtkXMLUnstructuredGridWriter() ext = ".vtu" if index is None: filename = "%s%s" % (filename, ext) else: filename = "%s_%i%s" % (filename, index, ext) writer.SetFileName(filename) writer.SetDataModeToAppended() writer.EncodeAppendedDataOff() writer.SetCompressorTypeToZLib() writer.GetCompressor().SetCompressionLevel(9) writer.SetBlockSize(2**15) writer.SetInput(vtu) writer.Write() if not writer.GetProgress() == 1.0 or not writer.GetErrorCode() == 0: raise IOException("Failed to write vtu file: %s" % filename) return
EidMap[eid] = b if isinstance(element, CTRIA3) or isinstance(element, CTRIAR): #print "ctria3" elem = vtk.vtkTriangle() nodeIDs = element.nodeIDs() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) Tria3cell = grid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) Color.InsertTuple1(Tria3cell, 3) elif isinstance(element, CTRIA6): continue nodeIDs = element.nodeIDs() if None not in nodeIDs: elem = vtk.vtkQuadraticTriangle() elem.GetPointIds().SetId(3, nidMap[nodeIDs[3]]) elem.GetPointIds().SetId(4, nidMap[nodeIDs[4]]) elem.GetPointIds().SetId(5, nidMap[nodeIDs[5]]) else: elem = vtk.vtkTriangle() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) Tria6cell = grid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) Color.InsertTuple1(Tria6cell, 4) elif isinstance(element, CTRIAX6): continue nodeIDs = element.nodeIDs()