def doNonLinear(self, ghosts, ncells): pts = vtk.vtkPoints() pts.SetNumberOfPoints(10) pts.InsertPoint(0, (0, 0, 0)) pts.InsertPoint(1, (1, 0, 0)) pts.InsertPoint(2, (0.5, 1, 0)) pts.InsertPoint(3, (0.5, 0.5, 1)) pts.InsertPoint(4, (0.5, 0, 0)) pts.InsertPoint(5, (1.25, 0.5, 0)) pts.InsertPoint(6, (0.25, 0.5, 0)) pts.InsertPoint(7, (0.25, 0.25, 0.5)) pts.InsertPoint(8, (0.75, 0.25, 0.5)) pts.InsertPoint(9, (0.5, 0.75, 0.5)) te = vtk.vtkQuadraticTetra() ptIds = te.GetPointIds() for i in range(10): ptIds.SetId(i, i) grid = vtk.vtkUnstructuredGrid() grid.Allocate(1, 1) grid.InsertNextCell(te.GetCellType(), te.GetPointIds()) grid.SetPoints(pts) grid.GetPointData().AddArray(ghosts) ugg = vtk.vtkUnstructuredGridGeometryFilter() ugg.SetInputData(grid) dss = vtk.vtkDataSetSurfaceFilter() dss.SetNonlinearSubdivisionLevel(2) dss.SetInputConnection(ugg.GetOutputPort()) dss.Update() self.assertEqual(dss.GetOutput().GetNumberOfCells(), ncells)
def doNonLinear(self, ghosts, ncells): pts = vtk.vtkPoints() pts.SetNumberOfPoints(10) pts.InsertPoint(0, (0, 0, 0)) pts.InsertPoint(1, (1, 0, 0)) pts.InsertPoint(2, (0.5, 1, 0)) pts.InsertPoint(3, (0.5, 0.5, 1)) pts.InsertPoint(4, (0.5, 0, 0)) pts.InsertPoint(5, (0.25, 0.5, 0)) pts.InsertPoint(6, (0.75, 0.5, 0)) pts.InsertPoint(7, (0.25, 0.25, 0.5)) pts.InsertPoint(8, (0.75, 0.25, 0.5)) pts.InsertPoint(9, (0.5, 0.75, 0.5)) te = vtk.vtkQuadraticTetra() ptIds = te.GetPointIds() for i in range(10): ptIds.SetId(i, i) grid = vtk.vtkUnstructuredGrid() grid.Allocate(1, 1) grid.InsertNextCell(te.GetCellType(), te.GetPointIds()) grid.SetPoints(pts) grid.GetPointData().AddArray(ghosts) ugg = vtk.vtkUnstructuredGridGeometryFilter() ugg.SetInputData(grid) dss = vtk.vtkDataSetSurfaceFilter() dss.SetNonlinearSubdivisionLevel(2) dss.SetInputConnection(ugg.GetOutputPort()) dss.Update() self.assertEqual(dss.GetOutput().GetNumberOfCells(), ncells)
def MakeQuadraticTetra(): aTetra = vtk.vtkQuadraticTetra() points = vtk.vtkPoints() pcoords = aTetra.GetParametricCoords() rng = vtk.vtkMinimalStandardRandomSequence() points.SetNumberOfPoints(aTetra.GetNumberOfPoints()) rng.SetSeed(5070) # for testing for i in range(0, aTetra.GetNumberOfPoints()): perturbation = [0.0] * 3 for j in range(0, 3): rng.Next() perturbation[j] = rng.GetRangeValue(-0.2, 0.2) aTetra.GetPointIds().SetId(i, i) points.SetPoint(i, pcoords[3 * i] + perturbation[0], pcoords[3 * i + 1] + perturbation[1], pcoords[3 * i + 2] + perturbation[2]) # Add the points and tetra to an unstructured grid uGrid = vtk.vtkUnstructuredGrid() uGrid.SetPoints(points) uGrid.InsertNextCell(aTetra.GetCellType(), aTetra.GetPointIds()) return uGrid
[6.75, 0.2, 0.5], [6.50, 0.6, 0.5]]) tetPoints.SetData(ntov(tetPointsCoords)) tetScalars = vtk.vtkFloatArray() tetScalars.SetNumberOfTuples(10) tetScalars.InsertValue(0, 1.0) tetScalars.InsertValue(1, 1.0) tetScalars.InsertValue(2, 1.0) tetScalars.InsertValue(3, 1.0) tetScalars.InsertValue(4, 0.0) tetScalars.InsertValue(5, 0.0) tetScalars.InsertValue(6, 0.0) tetScalars.InsertValue(7, 0.0) tetScalars.InsertValue(8, 0.0) tetScalars.InsertValue(9, 0.0) aTet = vtk.vtkQuadraticTetra() for i in range(aTet.GetNumberOfPoints()): aTet.GetPointIds().SetId(i, i) aTetGrid = vtk.vtkUnstructuredGrid() aTetGrid.Allocate(1, 1) aTetGrid.InsertNextCell(aTet.GetCellType(), aTet.GetPointIds()) aTetGrid.SetPoints(tetPoints) aTetGrid.GetPointData().SetScalars(tetScalars) # Quadratic hexahedron hexPoints = vtk.vtkPoints() hexPoints.SetNumberOfPoints(20) hexPointsCoords = np.array([ [8, 0, 0], [9, 0, 0], [9, 1, 0],
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()
tetPoints.InsertPoint(7, 0.25, 0.2, 0.5) tetPoints.InsertPoint(8, 0.75, 0.2, 0.5) tetPoints.InsertPoint(9, 0.50, 0.6, 0.5) tetScalars = vtk.vtkFloatArray() tetScalars.SetNumberOfTuples(10) tetScalars.InsertValue(0, 1.0) tetScalars.InsertValue(1, 1.0) tetScalars.InsertValue(2, 1.0) tetScalars.InsertValue(3, 1.0) tetScalars.InsertValue(4, 0.0) tetScalars.InsertValue(5, 0.0) tetScalars.InsertValue(6, 0.0) tetScalars.InsertValue(7, 0.0) tetScalars.InsertValue(8, 0.0) tetScalars.InsertValue(9, 0.0) aTet = vtk.vtkQuadraticTetra() aTet.GetPointIds().SetId(0, 0) aTet.GetPointIds().SetId(1, 1) aTet.GetPointIds().SetId(2, 2) aTet.GetPointIds().SetId(3, 3) aTet.GetPointIds().SetId(4, 4) aTet.GetPointIds().SetId(5, 5) aTet.GetPointIds().SetId(6, 6) aTet.GetPointIds().SetId(7, 7) aTet.GetPointIds().SetId(8, 8) aTet.GetPointIds().SetId(9, 9) aTetGrid = vtk.vtkUnstructuredGrid() aTetGrid.Allocate(1, 1) aTetGrid.InsertNextCell(aTet.GetCellType(), aTet.GetPointIds()) aTetGrid.SetPoints(tetPoints) aTetGrid.GetPointData().SetScalars(tetScalars)
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
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): """ Class defining a VTK element type
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): """ Class defining a VTK element type
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 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 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 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
continue elem = vtk.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]]) Tetra4cell = grid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) Color.InsertTuple1(Tetra4cell, 4) elif isinstance(element, CTETRA10): continue nodeIDs = element.nodeIDs() if None not in nodeIDs: elem = vtk.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 = vtk.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]]) Tetra10cell = grid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) Color.InsertTuple1(Tetra10cell, 4)