예제 #1
0
def metaDataScalarField(name, time, cycle, dim):
    optlistDef = None
    optlistMV = silo.DBoptlist()
    optlistVar = silo.DBoptlist()
    for optlist in (optlistMV, optlistVar):
        assert optlist.addOption(SA._DBOPT_CYCLE, cycle) == 0
        assert optlist.addOption(SA._DBOPT_DTIME, time) == 0
        assert optlist.addOption(SA._DBOPT_TENSOR_RANK, SA._DB_VARTYPE_SCALAR) == 0
    return (None, SA._DB_VARTYPE_SCALAR, optlistDef, optlistMV, optlistVar)
예제 #2
0
def metaDataVectorField(name, time, cycle, dim):
    assert dim in (2,3)
    optlistDef = silo.DBoptlist()
    optlistMV = silo.DBoptlist()
    optlistVar = silo.DBoptlist()
    for optlist in (optlistDef, optlistMV, optlistVar):
        assert optlist.addOption(SA._DBOPT_CYCLE, cycle) == 0
        assert optlist.addOption(SA._DBOPT_DTIME, time) == 0
    assert optlistMV.addOption(SA._DBOPT_TENSOR_RANK, SA._DB_VARTYPE_VECTOR) == 0
    assert optlistVar.addOption(SA._DBOPT_HIDE_FROM_GUI, 1) == 0
    assert optlistVar.addOption(SA._DBOPT_TENSOR_RANK, SA._DB_VARTYPE_SCALAR) == 0

    if dim == 2:
        return ("{<CELLS/%s_x>, <CELLS/%s_y>}" % (name, name), SA._DB_VARTYPE_VECTOR,
                optlistDef, optlistMV, optlistVar)
    else:
        return ("{<CELLS/%s_x>, <CELLS/%s_y>, <CELLS/%s_z>}" % (name, name, name), SA._DB_VARTYPE_VECTOR,
                optlistDef, optlistMV, optlistVar)
예제 #3
0
def metaDataTensorField(name, time, cycle, dim):
    assert dim in (2, 3)
    optlistDef = silo.DBoptlist()
    optlistMV = silo.DBoptlist()
    optlistVar = silo.DBoptlist()
    for optlist in (optlistDef, optlistMV, optlistVar):
        assert optlist.addOption(SA._DBOPT_CYCLE, cycle) == 0
        assert optlist.addOption(SA._DBOPT_DTIME, time) == 0
    assert optlistMV.addOption(SA._DBOPT_TENSOR_RANK,
                               SA._DB_VARTYPE_TENSOR) == 0
    assert optlistVar.addOption(SA._DBOPT_HIDE_FROM_GUI, 1) == 0
    assert optlistVar.addOption(SA._DBOPT_TENSOR_RANK,
                                SA._DB_VARTYPE_SCALAR) == 0

    if dim == 2:
        return ("{{%s_xx, %s_xy}, {%s_yx, %s_yy}}" % (name, name, name, name),
                SA._DB_VARTYPE_TENSOR, optlistDef, optlistMV, optlistVar)
    else:
        return (
            "{{%s_xx, %s_xy, %s_xz}, {%s_yx, %s_yy, %s_yz}, {%s_zx, %s_zy, %s_zz}}"
            % (name, name, name, name, name, name, name, name, name),
            SA._DB_VARTYPE_TENSOR, optlistDef, optlistMV, optlistVar)
예제 #4
0
def writeDomainSiloFile(ndim, maxproc, baseDirectory, baseName,
                        procDirBaseName, materials, rhosamp, xminblock,
                        xmaxblock, nblock, jsplit, label, time, cycle,
                        pretendRZ):

    assert jsplit < ndim

    # Make sure the directories are there.
    if mpi.rank == 0:
        for iproc in xrange(maxproc):
            pth = os.path.join(baseDirectory, procDirBaseName)
            if not os.path.exists(pth):
                os.makedirs(pth)
    mpi.barrier()

    # Is there anything to do?
    if mpi.rank < maxproc:
        numZones = 1
        numNodes = 1
        nblocknodes = list(nblock)
        for i, x in enumerate(nblock):
            numZones *= x
            numNodes *= x + 1
            nblocknodes[i] = x + 1
        assert numZones > 0
        assert len(rhosamp) == numZones

        # Make a vector<int> version of nblock
        nblock_vec = Spheral.vector_of_int(ndim)
        for jdim in xrange(ndim):
            nblock_vec[jdim] = nblock[jdim]

        # Create the file.
        fileName = os.path.join(baseDirectory, procDirBaseName,
                                "domain%i.silo" % mpi.rank)
        f = silo.DBCreate(fileName, SA._DB_CLOBBER, SA._DB_LOCAL, label,
                          SA._DB_HDF5)
        nullOpts = silo.DBoptlist()

        # Make the hblk0 directory.
        assert silo.DBMkDir(f, "hblk0") == 0

        # Write the domain mesh.
        coords = Spheral.vector_of_vector_of_double(ndim)
        for jdim in xrange(ndim):
            coords[jdim] = Spheral.vector_of_double(nblocknodes[jdim])
            dx = (xmaxblock[jdim] - xminblock[jdim]) / nblock[jdim]
            for i in xrange(nblocknodes[jdim]):
                coords[jdim][i] = xminblock[jdim] + i * dx
        optlist = silo.DBoptlist()
        assert optlist.addOption(SA._DBOPT_CYCLE, cycle) == 0
        assert optlist.addOption(SA._DBOPT_DTIME, time) == 0
        if pretendRZ:
            assert optlist.addOption(SA._DBOPT_COORDSYS,
                                     SA._DB_CYLINDRICAL) == 0
        else:
            assert optlist.addOption(SA._DBOPT_COORDSYS, SA._DB_CARTESIAN) == 0
        assert silo.DBPutQuadmesh(f, "hblk0/hydro_mesh", coords, optlist) == 0

        # # Domain neighbors.
        # if maxproc > 1 and mpi.rank < maxproc:
        #     domain_neighbors = Spheral.vector_of_vector_of_int(2)
        #     if mpi.rank > 0:
        #         domain_neighbors[1].append(mpi.rank - 1)
        #     if mpi.rank < maxproc - 1:
        #         domain_neighbors[1].append(mpi.rank + 1)
        #     domain_neighbors[0].append(len(domain_neighbors[1]))
        #     assert silo.DBPutCompoundarray(f, "DOMAIN_NEIGHBOR_NUMS", Spheral.vector_of_string(len(domain_neighbors), "dummy"), domain_neighbors, nullOpts) == 0

        # # Domain shared nodes.
        # if maxproc > 1 and mpi.rank < maxproc:
        #     for idomain in xrange(len(domain_neighbors[1])):
        #         commelements = Spheral.vector_of_vector_of_int(11)
        #         if mpi.rank == 0:
        #             face = nnodes[jsplit] - 1
        #         elif mpi.rank == maxproc - 1:
        #             face = 0
        #         elif idomain == 0:
        #             face = 0
        #         else:
        #             face = nnodes[jsplit] - 1
        #         if jsplit == 0:
        #             for j in xrange(nnodes[1]):
        #                 for k in xrange(nnodes[2]):
        #                     commelements[6].append(face + j*nnodes[0] + k*nnodes[0]*nnodes[1])
        #         elif jsplit == 1:
        #             for i in xrange(nnodes[0]):
        #                 for k in xrange(nnodes[2]):
        #                     commelements[6].append(i + face*nnodes[0] + k*nnodes[0]*nnodes[1])
        #         else:
        #             for i in xrange(nnodes[0]):
        #                 for j in xrange(nnodes[1]):
        #                     commelements[6].append(i + j*nnodes[0] + face*nnodes[0]*nnodes[1])
        #         assert silo.DBPutCompoundarray(f, "DOMAIN_NEIGHBOR%i" % idomain, Spheral.vector_of_string(11, "dummy"), commelements, nullOpts) == 0

        # Write materials.
        if materials:
            matnos = Spheral.vector_of_int(1, 0)
            for i in xrange(len(materials)):
                matnos.append(i + 1)
            assert len(matnos) == len(materials) + 1
            matlist = Spheral.vector_of_int(numZones, 0)
            matnames = Spheral.vector_of_string(1, "void")
            for imat, nodeList in enumerate(materials):
                for i in xrange(numZones):
                    if rhosamp[i] > 0.0:
                        matlist[i] = imat + 1
                matnames.append(nodeList.name)
            assert len(matlist) == numZones
            assert len(matnames) == len(materials) + 1
            matOpts = silo.DBoptlist(1024)
            assert matOpts.addOption(SA._DBOPT_CYCLE, cycle) == 0
            assert matOpts.addOption(SA._DBOPT_DTIME, time) == 0
            assert matOpts.addOption(SA._DBOPT_MATNAMES, SA._DBOPT_NMATNOS,
                                     matnames) == 0
            assert silo.DBPutMaterial(f, "hblk0/Materials", "hydro_mesh",
                                      matnos, matlist, nblock_vec,
                                      Spheral.vector_of_int(),
                                      Spheral.vector_of_int(),
                                      Spheral.vector_of_int(),
                                      Spheral.vector_of_double(), matOpts) == 0

        # Write the field variables.
        varOpts = silo.DBoptlist(1024)
        assert varOpts.addOption(SA._DBOPT_CYCLE, cycle) == 0
        assert varOpts.addOption(SA._DBOPT_DTIME, time) == 0
        assert silo.DBPutQuadvar1(f, "hblk0/den", "hydro_mesh", rhosamp,
                                  Spheral.vector_of_double(), SA._DB_ZONECENT,
                                  nblock_vec, varOpts) == 0

        # That's it.
        assert silo.DBClose(f) == 0
        del f

    return
예제 #5
0
def writeMasterSiloFile(ndim, nblock, jsplit, baseDirectory, baseName,
                        procDirBaseName, materials, rhosamp, label, time,
                        cycle):

    nullOpts = silo.DBoptlist()

    # Decide which domains have information.
    if len(rhosamp) > 0:
        myvote = mpi.rank + 1
    else:
        myvote = 0
    maxproc = mpi.allreduce(myvote, mpi.MAX)
    assert maxproc <= mpi.procs

    # Pattern for constructing per domain variables.
    domainNamePatterns = [
        os.path.join(procDirBaseName, "domain%i.silo:%%s" % i)
        for i in xrange(maxproc)
    ]
    domainVarNames = Spheral.vector_of_string()
    for iproc, p in enumerate(domainNamePatterns):
        domainVarNames.append(p % "/hblk0/den")
    assert len(domainVarNames) == maxproc

    # We need each domains nblock info.
    nblocks = [nblock]
    for sendproc in xrange(1, maxproc):
        if mpi.rank == sendproc:
            mpi.send(nblock, dest=0, tag=50)
        if mpi.rank == 0:
            nblocks.append(mpi.recv(source=sendproc, tag=50)[0])

    # Create the master file.
    if mpi.rank == 0:
        fileName = os.path.join(baseDirectory, baseName + ".silo")
        f = silo.DBCreate(fileName, SA._DB_CLOBBER, SA._DB_LOCAL, label,
                          SA._DB_HDF5)
        nullOpts = silo.DBoptlist()

        # Write the domain file names and types.
        domainNames = Spheral.vector_of_string()
        meshTypes = Spheral.vector_of_int(maxproc, SA._DB_QUADMESH)
        for p in domainNamePatterns:
            domainNames.append(p % "hblk0/hydro_mesh")
        optlist = silo.DBoptlist(1024)
        assert optlist.addOption(SA._DBOPT_CYCLE, cycle) == 0
        assert optlist.addOption(SA._DBOPT_DTIME, time) == 0
        assert silo.DBPutMultimesh(f, "hydro_mesh", domainNames, meshTypes,
                                   optlist) == 0

        # Write material names.
        material_names = Spheral.vector_of_string()
        matnames = Spheral.vector_of_string(1, "void")
        matnos = Spheral.vector_of_int(1, 0)
        for p in domainNamePatterns:
            material_names.append(p % "/hblk0/Materials")
        for i, name in enumerate([x.name for x in materials]):
            matnames.append(name)
            matnos.append(i + 1)
        assert len(material_names) == maxproc
        assert len(matnames) == len(materials) + 1
        assert len(matnos) == len(materials) + 1
        optlist = silo.DBoptlist(1024)
        assert optlist.addOption(SA._DBOPT_CYCLE, cycle) == 0
        assert optlist.addOption(SA._DBOPT_DTIME, time) == 0
        assert optlist.addOption(SA._DBOPT_MMESH_NAME, "hydro_mesh") == 0
        assert optlist.addOption(SA._DBOPT_MATNAMES, SA._DBOPT_NMATNOS,
                                 matnames) == 0
        assert optlist.addOption(SA._DBOPT_MATNOS, SA._DBOPT_NMATNOS,
                                 matnos) == 0
        assert silo.DBPutMultimat(f, "Materials", material_names, optlist) == 0

        # Write the variables descriptors.
        # We currently hardwire for the single density variable.
        types = Spheral.vector_of_int(maxproc, SA._DB_QUADVAR)
        assert len(domainVarNames) == maxproc
        optlistMV = silo.DBoptlist()
        assert optlistMV.addOption(SA._DBOPT_CYCLE, cycle) == 0
        assert optlistMV.addOption(SA._DBOPT_DTIME, time) == 0
        #assert optlistMV.addOption(SA._DBOPT_TENSOR_RANK, SA._DB_VARTYPE_SCALAR) == 0
        assert optlistMV.addOption(SA._DBOPT_BLOCKORIGIN, 0) == 0
        assert optlistMV.addOption(SA._DBOPT_MMESH_NAME, "hydro_mesh") == 0
        assert silo.DBPutMultivar(f, "den", domainVarNames, types,
                                  optlistMV) == 0

        # Write the dummy variable "akap_0" to tell Hades we're actually Hydra or something.
        assert silo.DBPutQuadvar1(f, "akap_0", "hydro_mesh",
                                  Spheral.vector_of_double(ndim * ndim, 0.0),
                                  Spheral.vector_of_double(), SA._DB_ZONECENT,
                                  Spheral.vector_of_int(ndim, ndim),
                                  nullOpts) == 0

        # Write domain and mesh size info.
        assert silo.DBMkDir(f, "Decomposition") == 0
        assert silo.DBWrite(f, "Decomposition/NumDomains", maxproc) == 0
        assert silo.DBWrite(f, "Decomposition/NumLocalDomains", maxproc) == 0
        assert silo.DBWrite(f, "Decomposition/NumBlocks", 1) == 0
        #assert silo.DBWrite(f, "Decomposition/LocalName", "hblk") == 0
        localDomains = Spheral.vector_of_int()
        domainFiles = Spheral.vector_of_vector_of_int(1)
        for i in xrange(maxproc):
            localDomains.append(i)
            domainFiles[0].append(i)
        assert silo.DBWrite(f, "Decomposition/LocalDomains", localDomains) == 0
        assert silo.DBWrite(f, "DomainFiles", domainFiles) == 0

        for iproc in xrange(maxproc):
            assert silo.DBMkDir(f, "Decomposition/gmap%i" % iproc) == 0
            stuff = Spheral.vector_of_int(12, 0)
            for jdim in xrange(ndim):
                stuff[6 + jdim] = nblocks[iproc][jdim]
            if iproc in (0, maxproc - 1):
                assert silo.DBWrite(
                    f, "Decomposition/gmap%i/NumNeighbors" % iproc, 1) == 0
            else:
                assert silo.DBWrite(
                    f, "Decomposition/gmap%i/NumNeighbors" % iproc, 2) == 0
            assert silo.DBWrite(f, "Decomposition/gmap%i/gmap" % iproc,
                                stuff) == 0

    # Close the file.
    if mpi.rank == 0:
        assert silo.DBClose(f) == 0
        del f

    return maxproc
예제 #6
0
def writeDomainSiloFile(ndim, baseDirectory, baseName, procDirBaseName,
                        nodeLists, label, time, cycle, dumpGhosts, fieldwad):

    # Create the file.
    fileName = os.path.join(baseDirectory, procDirBaseName % mpi.rank,
                            baseName + ".silo")
    db = silo.DBCreate(fileName, SA._DB_CLOBBER, SA._DB_LOCAL, label,
                       SA._DB_HDF5)
    nullOpts = silo.DBoptlist()

    # Read the per material info.
    if dumpGhosts:
        ntot = sum([n.numNodes for n in nodeLists])
    else:
        ntot = sum([n.numInternalNodes for n in nodeLists])
    coords = vector_of_vector_of_double(ndim)
    for nodes in nodeLists:
        if dumpGhosts:
            pos = nodes.positions().allValues()
        else:
            pos = nodes.positions().internalValues()
        n = len(pos)
        for j in xrange(ndim):
            for i in xrange(n):
                coords[j].append(pos[i][j])
    for j in xrange(ndim):
        assert len(coords[j]) == ntot

    # Write the Pointmesh.
    meshOpts = silo.DBoptlist(1024)
    assert meshOpts.addOption(SA._DBOPT_CYCLE, cycle) == 0
    assert meshOpts.addOption(SA._DBOPT_DTIME, time) == 0
    assert silo.DBPutPointmesh(db, "mesh", coords, meshOpts) == 0

    # Write materials.
    matnos = vector_of_int()
    for i in xrange(len(nodeLists)):
        matnos.append(i)
    assert len(matnos) == len(nodeLists)
    matlist = vector_of_int()
    matnames = vector_of_string()
    for (nodeList, imat) in zip(nodeLists, xrange(len(nodeLists))):
        if dumpGhosts:
            matlist += vector_of_int(nodeList.numNodes, imat)
        else:
            matlist += vector_of_int(nodeList.numInternalNodes, imat)
        matnames.append(nodeList.name)
    assert len(matlist) == ntot
    assert len(matnames) == len(nodeLists)
    matOpts = silo.DBoptlist(1024)
    assert matOpts.addOption(SA._DBOPT_CYCLE, cycle) == 0
    assert matOpts.addOption(SA._DBOPT_DTIME, time) == 0
    assert matOpts.addOption(SA._DBOPT_MATNAMES, SA._DBOPT_NMATNOS,
                             matnames) == 0
    vecInt = vector_of_int(0)
    vecDouble = vector_of_double(0)
    assert silo.DBPutMaterial(db, "material", "mesh", matnos, matlist, vecInt,
                              vecInt, vecInt, vecInt, vecDouble, matOpts) == 0

    # Write the variable descriptions for non-scalar variables (vector and tensors).
    writeDefvars(db, fieldwad)

    # Write the field components.
    varOpts = silo.DBoptlist(1024)
    assert varOpts.addOption(SA._DBOPT_CYCLE, cycle) == 0
    assert varOpts.addOption(SA._DBOPT_DTIME, time) == 0
    for name, desc, type, optlistDef, optlistMV, optlistVar, subvars in fieldwad:
        for subname, vals in subvars:
            if len(vals) > 0:
                assert silo.DBPutPointvar1(db, subname, "mesh", vals,
                                           varOpts) == 0

    # That's it.
    assert silo.DBClose(db) == 0
    del db
    return
예제 #7
0
def writeMasterSiloFile(ndim, baseDirectory, baseName, procDirBaseName,
                        nodeLists, label, time, cycle, dumpGhosts, fieldwad):

    nullOpts = silo.DBoptlist()

    # Pattern for constructing per domain variables.
    domainNamePatterns = [
        os.path.join(procDirBaseName % i, baseName + ".silo:%s")
        for i in xrange(mpi.procs)
    ]

    # Create the master file.
    if mpi.rank == 0:
        fileName = os.path.join(baseDirectory, baseName + ".silo")
        db = silo.DBCreate(fileName, SA._DB_CLOBBER, SA._DB_LOCAL, label,
                           SA._DB_HDF5)

        # Write the domain file names and types.
        domainNames = vector_of_string()
        meshTypes = vector_of_int(mpi.procs, SA._DB_POINTMESH)
        for p in domainNamePatterns:
            domainNames.append(p % "mesh")
        optlist = silo.DBoptlist(1024)
        assert optlist.addOption(SA._DBOPT_CYCLE, cycle) == 0
        assert optlist.addOption(SA._DBOPT_DTIME, time) == 0
        assert silo.DBPutMultimesh(db, "MESH", domainNames, meshTypes,
                                   optlist) == 0

    # Extract the material names, and write per material info if any.
    if len(nodeLists) > 0:

        # Write material names.
        if mpi.rank == 0:
            materialNames = [x.name for x in nodeLists]
            material_names = vector_of_string()
            matnames = vector_of_string()
            matnos = vector_of_int()
            for p in domainNamePatterns:
                material_names.append(p % "material")
            for (name, i) in zip(materialNames, range(len(materialNames))):
                matnames.append(name)
                matnos.append(i)
            assert len(material_names) == mpi.procs
            assert len(matnames) == len(nodeLists)
            assert len(matnos) == len(nodeLists)
            optlist = silo.DBoptlist(1024)
            assert optlist.addOption(SA._DBOPT_CYCLE, cycle) == 0
            assert optlist.addOption(SA._DBOPT_DTIME, time) == 0
            assert optlist.addOption(SA._DBOPT_MATNAMES, SA._DBOPT_NMATNOS,
                                     matnames) == 0
            assert optlist.addOption(SA._DBOPT_MATNOS, SA._DBOPT_NMATNOS,
                                     matnos) == 0
            assert silo.DBPutMultimat(db, "MATERIAL", material_names,
                                      optlist) == 0

            # Write the variable descriptions for non-scalar variables (vector and tensors).
            writeDefvars(db, fieldwad)

        # Write the variables descriptors.
        types = vector_of_int(mpi.procs, SA._DB_POINTVAR)
        for name, desc, type, optlistDef, optlistMV, optlistVar, subvars in fieldwad:
            domainVarNames = vector_of_string()
            nlocalvals = sum([len(x[1]) for x in subvars])
            if desc is None:
                for iproc, p in enumerate(domainNamePatterns):
                    nvals = mpi.bcast(nlocalvals, root=iproc)
                    if nvals > 0:
                        domainVarNames.append(p % name)
                    else:
                        domainVarNames.append("EMPTY")
                assert len(domainVarNames) == mpi.procs
                if mpi.rank == 0:
                    assert silo.DBPutMultivar(db, name, domainVarNames, types,
                                              optlistMV) == 0
            else:
                for subname, vals in subvars:
                    domainVarNames = vector_of_string()
                    for iproc, p in enumerate(domainNamePatterns):
                        nvals = mpi.bcast(nlocalvals, root=iproc)
                        if nvals > 0:
                            domainVarNames.append(p % subname)
                        else:
                            domainVarNames.append("EMPTY")
                    assert len(domainVarNames) == mpi.procs
                    if mpi.rank == 0:
                        assert silo.DBPutMultivar(db, subname, domainVarNames,
                                                  types, optlistVar) == 0

    # That's it.
    if mpi.rank == 0:
        assert silo.DBClose(db) == 0
        del db

    return
예제 #8
0
def writeDomainMeshSiloFile(dirName, mesh, index2zone, label, nodeLists, time, cycle, fieldwad,
                            pretendRZ, nodeArrays, zoneArrays, faceArrays,
                            meshType = SA._DB_UCDMESH):

    # Is there anything to do?
    numZones = len(mesh.cells)
    if numZones > 0:

        # Create the file.
        fileName = os.path.join(dirName, "domain%i.silo" % mpi.rank)
        db = silo.DBCreate(fileName, 
                           SA._DB_CLOBBER, SA._DB_LOCAL, label, SA._DB_HDF5)
        nullOpts = silo.DBoptlist()

        # Make directories for variables.
        assert silo.DBMkDir(db, "CELLS") == 0
        assert silo.DBMkDir(db, "POINTS") == 0      # HACK

        # Determine our dimensionality
        if isinstance(mesh, polytope.Tessellation2d):
            nDim = 2
        else:
            assert isinstance(mesh, polytope.Tessellation3d)
            nDim = 3

        # Write a Polygonal zone list.
        zonelistName = { 2 : "zonelist",
                         3 : "PHzonelist" }

        if nDim == 2:
        
            # Read out the zone nodes.  We rely on these already being arranged
            # counter-clockwise.
            zoneNodes = vector_of_vector_of_int()
            shapesize = vector_of_int()
            for zoneID in xrange(numZones):
                zone = mesh.cells[zoneID]
                nodes = vector_of_int()
                for iface in zone:
                    if iface < 0:
                        nodes.append(mesh.faces[~iface][1])
                    else:
                        nodes.append(mesh.faces[iface][0])
                zoneNodes.append(nodes)
                shapesize.append(len(nodes))
            assert len(zoneNodes) == numZones
            assert len(shapesize) == numZones
        
            assert silo.DBPutZonelist2(db, zonelistName[nDim], nDim, zoneNodes, 0, 0,
                                       vector_of_int(numZones, SA._DB_ZONETYPE_POLYGON),
                                       shapesize,
                                       vector_of_int(numZones, 1),
                                       nullOpts) == 0
        
        # Write a Polyhedral zone list.
        if nDim == 3:
        
            # Construct the face-node lists.
            numFaces = len(mesh.faces)
            faceNodes = vector_of_vector_of_int(numFaces)
            for iface in xrange(numFaces):
                for j in xrange(len(mesh.faces[iface])):
                    faceNodes[iface].append(mesh.faces[iface][j])
                assert len(faceNodes[iface]) == len(mesh.faces[iface])
            assert len(faceNodes) == numFaces
        
            # Construct the zone-face list.  We use the ones complement of a face ID
            # to indicate that face needs to be reversed in reference to this zone.
            # This is the same convention as polytope, so just copy it.
            zoneFaces = mesh.cells
            assert len(zoneFaces) == numZones
        
            assert silo.DBPutPHZonelist(db, zonelistName[nDim], faceNodes, zoneFaces, 0, (numZones - 1), nullOpts) == 0
        
        # Construct the mesh node coordinates.
        assert len(mesh.nodes) % nDim == 0
        numNodes = len(mesh.nodes)/nDim
        coords = vector_of_vector_of_double(nDim, vector_of_double(numNodes))
        for nodeID in xrange(numNodes):
            for idim in xrange(nDim):
                coords[idim][nodeID] = mesh.nodes[nDim*nodeID + idim]
        assert len(coords) == nDim
        
        # Write the mesh itself.
        meshOpts = silo.DBoptlist(1024)
        assert meshOpts.addOption(SA._DBOPT_CYCLE, cycle) == 0
        assert meshOpts.addOption(SA._DBOPT_DTIME, time) == 0
        assert meshOpts.addOption(SA._DBOPT_COORDSYS, SA._DB_CARTESIAN) == 0
        assert meshOpts.addOption(SA._DBOPT_NSPACE, nDim) == 0
        assert meshOpts.addOption(SA._DBOPT_TV_CONNECTIVITY, 1) == 0
        if nDim == 2:
            if pretendRZ:
                assert meshOpts.addOption(SA._DBOPT_COORDSYS, SA._DB_CYLINDRICAL) == 0
                assert meshOpts.addOption(SA._DBOPT_XLABEL, "z") == 0
                assert meshOpts.addOption(SA._DBOPT_YLABEL, "r") == 0
            assert silo.DBPutUcdmesh(db, "MESH", coords, numZones, zonelistName[nDim], "NULL", meshOpts) == 0
        else:
            assert meshOpts.addOption(SA._DBOPT_PHZONELIST, zonelistName[nDim]) == 0
            assert silo.DBPutUcdmesh(db, "MESH", coords, numZones, "NULL", "NULL", meshOpts) == 0
        
        # Write materials.
        if nodeLists:
            matnos = vector_of_int()
            for i in xrange(len(nodeLists)):
                matnos.append(i)
            assert len(matnos) == len(nodeLists)
            matlist = vector_of_int(numZones)
            matnames = vector_of_string()
            offset = 0
            for (nodeList, imat) in zip(nodeLists, xrange(len(nodeLists))):
                for i in xrange(nodeList.numInternalNodes):
                    for j in index2zone[offset + i]:
                        matlist[j] = imat
                matnames.append(nodeList.name)
                offset += nodeList.numInternalNodes
            assert len(matlist) == numZones
            assert len(matnames) == len(nodeLists)
            matOpts = silo.DBoptlist(1024)
            assert matOpts.addOption(SA._DBOPT_CYCLE, cycle) == 0
            assert matOpts.addOption(SA._DBOPT_DTIME, time) == 0
            assert matOpts.addOption(SA._DBOPT_MATNAMES, SA._DBOPT_NMATNOS, matnames) == 0
            assert silo.DBPutMaterial(db, "MATERIAL", "MESH", matnos, matlist, vector_of_int(),
                                      vector_of_int(), vector_of_int(), vector_of_int(), vector_of_double(),
                                      matOpts) == 0
            assert silo.DBPutMaterial(db, "PointMATERIAL", "PointMESH", matnos, matlist, vector_of_int(),
                                      vector_of_int(), vector_of_int(), vector_of_int(), vector_of_double(),
                                      matOpts) == 0
        
            # Write the variable descriptions for non-scalar variables (vector and tensors).
            #writeDefvars(db, fieldwad)
        
            # Write the field components.
            centering = SA._DB_ZONECENT
            varOpts = silo.DBoptlist(1024)
            assert varOpts.addOption(SA._DBOPT_CYCLE, cycle) == 0
            assert varOpts.addOption(SA._DBOPT_DTIME, time) == 0
            for name, desc, type, optlistDef, optlistMV, optlistVar, subvars in fieldwad:
                for subname, vals in subvars:
                    if len(vals) > 0:
                        if isinstance(vals, vector_of_double):
                            assert silo.DBPutUcdvar1(db, "CELLS_" + subname, "MESH", vals, vector_of_double(), centering, varOpts) == 0
                        elif isinstance(vals, vector_of_int):
                            assert silo.DBPutUcdvar1(db, "CELLS_" + subname, "MESH", vals, vector_of_int(), centering, varOpts) == 0
        
            # HACK: Write the field components on the point mesh as well.  Remove when the vardef version is working.
            centering = SA._DB_ZONECENT
            varOpts = silo.DBoptlist(1024)
            assert varOpts.addOption(SA._DBOPT_CYCLE, cycle) == 0
            assert varOpts.addOption(SA._DBOPT_DTIME, time) == 0
            for name, desc, type, optlistDef, optlistMV, optlistVar, subvars in fieldwad:
                for subname, vals in subvars:
                    if len(vals) > 0:
                        assert silo.DBPutPointvar1(db, "POINTS_" + subname, "PointMESH", vals, varOpts) == 0

        # Write the set of neighbor domains.
        thpt = vector_of_vector_of_int()
        thpt.append(vector_of_int(1, len(mesh.neighborDomains)))
        thpt.append(vector_of_int())
        for i in mesh.neighborDomains:
            thpt[-1].append(i)
        elemNames = vector_of_string()
        elemNames.append("num neighbor domains")
        elemNames.append("neighbor domains")
        assert silo.DBPutCompoundarray(db, "DOMAIN_NEIGHBOR_NUMS", elemNames, thpt, nullOpts) == 0
        
        # Write the shared nodes for each neighbor domain.
        sharedNodes = mesh.sharedNodes
        for ineighborDomain in xrange(len(mesh.neighborDomains)):
            nodes = vector_of_int()
            for i in sharedNodes[ineighborDomain]:
                nodes.append(i)
            assert len(nodes) == len(sharedNodes[ineighborDomain])
            assert silo.DBPutCompoundarray(db, "DOMAIN_NEIGHBOR%i" % ineighborDomain,
                                           vector_of_string(1, "shared_nodes"),
                                           vector_of_vector_of_int(1, nodes),
                                           nullOpts) == 0
        
        # If requested, write out annotations for the nodes, zones, and faces.
        if (not (nodeArrays is None) or
            not (zoneArrays is None) or
            not (faceArrays is None)):
            names = vector_of_string()
            values = vector_of_vector_of_int()
            if not (nodeArrays is None):
                for pair in nodeArrays:
                    assert len(pair) == 2
                    if len(pair[1]) > 0:
                        names.append(pair[0] + "_node")
                        values.append(vector_of_int())
                        for i in pair[1]:
                            values[-1].append(i)
                        assert len(values[-1]) == len(pair[1])
            if not (zoneArrays is None):
                for pair in zoneArrays:
                    assert len(pair) == 2
                    if len(pair[1]) > 0:
                        names.append(pair[0] + "_zone")
                        values.append(vector_of_int())
                        for i in pair[1]:
                            values[-1].append(i)
                        assert len(values[-1]) == len(pair[1])
            if not (faceArrays is None):
                for pair in faceArrays:
                    assert len(pair) == 2
                    if len(pair[1]) > 0:
                        names.append(pair[0] + "_face")
                        values.append(vector_of_int())
                        for i in pair[1]:
                            values[-1].append(i)
                        assert len(values[-1]) == len(pair[1])
            assert len(names) == len(values)
            if len(names) > 0:
                assert silo.DBPutCompoundarray(db, "ANNOTATION_INT", names, values, nullOpts) == 0
        
        # Write the point mesh.
        if nodeLists:
            ntot = sum([n.numInternalNodes for n in nodeLists])
            coords = vector_of_vector_of_double(nDim)
            for nodes in nodeLists:
                pos = nodes.positions().internalValues()
                n = len(pos)
                for j in xrange(nDim):
                    for i in xrange(n):
                        coords[j].append(pos[i][j])
            for j in xrange(nDim):
                assert len(coords[j]) == ntot
         
            # Write the Pointmesh.
            meshOpts = silo.DBoptlist(1024)
            assert meshOpts.addOption(SA._DBOPT_CYCLE, cycle) == 0
            assert meshOpts.addOption(SA._DBOPT_DTIME, time) == 0
            assert silo.DBPutPointmesh(db, "PointMESH", coords, meshOpts) == 0

        # That's it.
        assert silo.DBClose(db) == 0
        del db

    return
예제 #9
0
def writeMasterMeshSiloFile(dirName, mesh, label, nodeLists, time, cycle, fieldwad,
                            meshType = SA._DB_UCDMESH):

    # Figure out who has something to write. Silo can't handle empty variables...
    numZonesPerDomain = mpi.allreduce([(mpi.rank, len(mesh.cells))], mpi.SUM)
    numZonesPerDomain.sort()
    numZonesPerDomain = [x[1] for x in numZonesPerDomain]

    # Only processor 0 actually writes the file.
    linkfile = None
    if mpi.rank == 0:

        nullOpts = silo.DBoptlist()
        
        # Create the master file.
        p0, p1 = os.path.split(dirName)
        fileName = os.path.join(dirName, "OvlTop.silo")
        db = silo.DBCreate(fileName, 
                           SA._DB_CLOBBER, SA._DB_LOCAL, label, SA._DB_HDF5)
        
        # Make directories for variables.
        assert silo.DBMkDir(db, "CELLS") == 0
        assert silo.DBMkDir(db, "POINTS") == 0      # HACK

        # Pattern for constructing per domain variables.
        domainNamePatterns = [("%s/domain%i.silo:" % (p1, i)) + "%s" for i in xrange(mpi.procs) if numZonesPerDomain[i] > 0]
        numDomains = len(domainNamePatterns)
        
        # Write the domain file names and types.
        domainNames = vector_of_string()
        meshTypes = vector_of_int()
        i = 0
        for p in domainNamePatterns:
            domainNames.append(p % "MESH")
            meshTypes.append(meshType)
        optlist = silo.DBoptlist(1024)
        assert optlist.addOption(SA._DBOPT_CYCLE, cycle) == 0
        assert optlist.addOption(SA._DBOPT_DTIME, time) == 0
        assert silo.DBPutMultimesh(db, "MMESH", domainNames, meshTypes, optlist) == 0
        
        # Also write out the points as a point mesh.
        if nodeLists and mpi.rank == 0:
            domainNames = vector_of_string()
            meshTypes = vector_of_int(mpi.procs, SA._DB_POINTMESH)
            for p in domainNamePatterns:
                domainNames.append(p % "PointMESH")
            optlist = silo.DBoptlist(1024)
            assert optlist.addOption(SA._DBOPT_CYCLE, cycle) == 0
            assert optlist.addOption(SA._DBOPT_DTIME, time) == 0
            assert silo.DBPutMultimesh(db, "MPointMESH", domainNames, meshTypes, optlist) == 0
        
        # Extract the material names, and write per material info if any.
        if nodeLists:
        
            # Write material names (MESH)
            materialNames = [x.name for x in nodeLists]
            material_names = vector_of_string()
            matnames = vector_of_string()
            matnos = vector_of_int()
            for p in domainNamePatterns:
                material_names.append(p % "MATERIAL")
            for i, name in enumerate(materialNames):
                matnames.append(name)
                matnos.append(i)
            assert len(material_names) == numDomains
            assert len(matnames) == len(nodeLists)
            assert len(matnos) == len(nodeLists)
            optlist = silo.DBoptlist(1024)
            assert optlist.addOption(SA._DBOPT_CYCLE, cycle) == 0
            assert optlist.addOption(SA._DBOPT_DTIME, time) == 0
            assert optlist.addOption(SA._DBOPT_MMESH_NAME, "MMESH") == 0
            assert optlist.addOption(SA._DBOPT_MATNAMES, SA._DBOPT_NMATNOS, matnames) == 0
            assert optlist.addOption(SA._DBOPT_MATNOS, SA._DBOPT_NMATNOS, matnos) == 0
            assert silo.DBPutMultimat(db, "MMATERIAL", material_names, optlist) == 0
        
            # Write material names (PointMESH)
            materialNames = [x.name for x in nodeLists]
            material_names = vector_of_string()
            matnames = vector_of_string()
            matnos = vector_of_int()
            for p in domainNamePatterns:
                material_names.append(p % "PointMATERIAL")
            for i, name in enumerate(materialNames):
                matnames.append(name)
                matnos.append(i)
            assert len(material_names) == numDomains
            assert len(matnames) == len(nodeLists)
            assert len(matnos) == len(nodeLists)
            optlist = silo.DBoptlist(1024)
            assert optlist.addOption(SA._DBOPT_CYCLE, cycle) == 0
            assert optlist.addOption(SA._DBOPT_DTIME, time) == 0
            assert optlist.addOption(SA._DBOPT_MMESH_NAME, "MPointMESH") == 0
            assert optlist.addOption(SA._DBOPT_MATNAMES, SA._DBOPT_NMATNOS, matnames) == 0
            assert optlist.addOption(SA._DBOPT_MATNOS, SA._DBOPT_NMATNOS, matnos) == 0
            assert silo.DBPutMultimat(db, "MPointMATERIAL", material_names, optlist) == 0
        
            # Write the variable descriptions for non-scalar variables (vector and tensors).
            writeDefvars(db, fieldwad)
        
            # Write the attributes for each variable.
            # This is largely an Overlink thing, which only needs the components of types like vectors.
            if len(fieldwad) > 0:
                attrNames = vector_of_string()
                attrs = vector_of_vector_of_int()
                thpt = vector_of_int()
                thpt.append(OverlinkAttrs["ATTR_ZONAL"])
                thpt.append(OverlinkAttrs["ATTR_INTENSIVE"])
                thpt.append(OverlinkAttrs["ATTR_SECOND_ORDER"])
                thpt.append(0)
                thpt.append(OverlinkAttrs["ATTR_FLOAT"])
                for name, desc, type, optlistDef, optlistMV, optlistVar, subvars in fieldwad:
                    for subname, vals in subvars:
                        attrNames.append(subname)
                        attrs.append(thpt)
                assert len(attrNames) == len(attrs)
                silo.DBPutCompoundarray(db, "VAR_ATTRIBUTES", attrNames, attrs, nullOpts)
        
            # Write the variables descriptors.
            ucdTypes = vector_of_int(numDomains, SA._DB_UCDVAR)
            for name, desc, type, optlistDef, optlistMV, optlistVar, subvars in fieldwad:
                domainVarNames = vector_of_string()
                for p in domainNamePatterns:
                    domainVarNames.append(p % ("CELLS_" + name))
                assert len(domainVarNames) == numDomains
                assert silo.DBPutMultivar(db, "CELLS/" + name, domainVarNames, ucdTypes, optlistMV) == 0
                if desc != None:
                    for subname, vals in subvars:
                        domainVarNames = vector_of_string()
                        for p in domainNamePatterns:
                            domainVarNames.append(p % ("CELLS_" + subname))
                        assert len(domainVarNames) == numDomains
                        assert silo.DBPutMultivar(db, "CELLS/" + subname, domainVarNames, ucdTypes, optlistVar) == 0
        
            # HACK: repeating for point values -- remove when vardefs work
            # Write the variables descriptors.
            ptTypes = vector_of_int(numDomains, SA._DB_POINTVAR)
            for name, desc, type, optlistDef, optlistMV, optlistVar, subvars in fieldwad:
                domainVarNames = vector_of_string()
                for p in domainNamePatterns:
                    domainVarNames.append(p % ("POINTS_" + name))
                assert len(domainVarNames) == numDomains
                assert silo.DBPutMultivar(db, "POINTS/" + name, domainVarNames, ptTypes, optlistMV) == 0
                if desc != None:
                    for subname, vals in subvars:
                        domainVarNames = vector_of_string()
                        for p in domainNamePatterns:
                            domainVarNames.append(p % ("POINTS_" + subname))
                        assert len(domainVarNames) == numDomains
                        assert silo.DBPutMultivar(db, "POINTS/" + subname, domainVarNames, ptTypes, optlistVar) == 0
        
        # Make a convenient symlink for the master file.
        linkfile = p1 + ".silo"
        fullpath = os.path.join(p0, linkfile)
        if os.path.exists(fullpath):
            os.remove(fullpath)
        targetfile = os.path.join(p1, "OvlTop.silo")
        os.symlink(targetfile, os.path.join(p0, linkfile))

        # That's it.
        assert silo.DBClose(db) == 0
        del db

    # Everyone gets the link file name.
    linkfile = mpi.bcast(linkfile, root=0)

    return linkfile