Exemplo n.º 1
0
    def __init__(self,
                 nodeList,
                 nodeIndices,
                 sampleMethod,
                 filename,
                 header=None,
                 labels=None):
        self.restart = Spheral.RestartableObject(self)
        self.nodeList = nodeList
        self.sampleMethod = sampleMethod
        self.filename = filename
        self.cycleHistory = []
        self.timeHistory = []
        self.sampleHistory = []

        # Figure out the dimensionality.
        FieldConstructor = None
        if isinstance(nodeList, Spheral.NodeList1d):
            FieldConstructor = Spheral.IntField1d
        elif isinstance(nodeList, Spheral.NodeList2d):
            FieldConstructor = Spheral.IntField2d
        elif isinstance(nodeList, Spheral.NodeList3d):
            FieldConstructor = Spheral.IntField3d
        assert FieldConstructor is not None

        # Store the set of nodes we're going to sample as a field of flags.
        # This should automatically be safe as NodeLists/Fields get renumbered,
        # redistributed, deleted, added, or what have you.
        self.nodeFlags = FieldConstructor("flag nodes", nodeList, 0)
        if nodeIndices is None:
            nodeIndices = range(nodeList.numInternalNodes)
        self.nodeIndices = nodeIndices
        if isinstance(nodeIndices, list):
            for i in nodeIndices:
                assert i >= 0 and i < nodeList.numInternalNodes
                self.nodeFlags[i] = 1
        else:
            self.currentNodeIndices()

        # Open the history file.
        self.file = None
        if mpi.rank == 0:
            self.file = open(self.filename, "w")
            assert self.file is not None

            # Write the optional header string.
            if header:
                self.file.write(header + "\n")

            # Write the optional label line
            if labels:
                self.file.write(
                    ("# " + ((len(labels) + 2) * '"%20s" ') + "\n") %
                    (("cycle", "time") + labels))

        return
Exemplo n.º 2
0
    def __init__(
        self,
        geometry,
        position,
        samplefunc,
        W,
        db,
        filename,
        header=None,
        labels=None,
        initializefunc=None,
    ):
        self.restart = Spheral.RestartableObject(self)

        # Set up our internal data.
        self.geometry = geometry
        self.position = position
        self.samplefunc = samplefunc
        self.initializefunc = initializefunc
        self.W = W
        self.db = db
        self.filename = filename
        self.cycleHistory = []
        self.timeHistory = []
        self.sampleHistory = []

        # Open the history file.
        self.file = None
        if mpi.rank == 0:
            if os.path.exists(self.filename):
                os.remove(self.filename)
            self.file = open(self.filename, "w")
            assert self.file is not None

            # Write the optional header string.
            if header:
                self.file.write(header + "\n")

            # Write the optional label line
            if labels:
                self.file.write(
                    ("# " + ((len(labels) + 2) * '"%20s" ') + "\n") %
                    (("cycle", "time") + labels))

        return
Exemplo n.º 3
0
    def __init__(self, nodeList):
        self.restart = Spheral.RestartableObject(self)
        self.nodeList = nodeList
        self.cycleHistory = []
        self.timeHistory = []
        self.LzHistory = []

        # Figure out the dimensionality.
        FieldConstructor = None
        if isinstance(nodeList, Spheral.NodeList1d):
            FieldConstructor = Spheral.ScalarField1d
        elif isinstance(nodeList, Spheral.NodeList2d):
            FieldConstructor = Spheral.ScalarField2d
        elif isinstance(nodeList, Spheral.NodeList3d):
            FieldConstructor = Spheral.ScalarField3d
        assert FieldConstructor is not None

        return
Exemplo n.º 4
0
    def writeDomainSiloFile(ndim, maxproc, baseDirectory, baseName, procDir,
                            materials, vars, 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, procDir)
                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

            # Make a vector<int> version of nblock
            nblock_vec = Spheral.vector_of_int(nblock)

            # Create the file.
            fileName = os.path.join(baseDirectory, procDir,
                                    "domain%i.silo" % mpi.rank)
            f = silo.DBCreate(fileName, silo.DB_CLOBBER, silo.DB_LOCAL, label,
                              silo.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(
                [Spheral.vector_of_double()] * ndim)
            for jdim in xrange(ndim):
                coords[jdim] = Spheral.vector_of_double([0.0] *
                                                        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(silo.DBOPT_CYCLE, cycle) == 0
            assert optlist.addOption(silo.DBOPT_DTIME, time) == 0
            if pretendRZ:
                assert optlist.addOption(silo.DBOPT_COORDSYS,
                                         silo.DB_CYLINDRICAL) == 0
            else:
                assert optlist.addOption(silo.DBOPT_COORDSYS,
                                         silo.DB_CARTESIAN) == 0
            assert silo.DBPutQuadmesh(f, "hblk0/hydro_mesh", coords,
                                      optlist) == 0

            # Write materials.
            if materials:
                matnos = Spheral.vector_of_int(range(len(materials) + 1))
                assert len(matnos) == len(materials) + 1
                matlist = Spheral.vector_of_int([0] * numZones)
                matnames = Spheral.vector_of_string(["void"])
                for imat, nodeList in enumerate(materials):
                    for i in xrange(numZones):
                        if vars[0][0][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(silo.DBOPT_CYCLE, cycle) == 0
                assert matOpts.addOption(silo.DBOPT_DTIME, time) == 0
                assert matOpts.addOption(silo.DBOPT_MATNAMES,
                                         silo.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(silo.DBOPT_CYCLE, cycle) == 0
            assert varOpts.addOption(silo.DBOPT_DTIME, time) == 0
            for var, varname in vars:
                assert len(var) == numZones
                assert silo.DBPutQuadvar1(f, "hblk0/" + varname, "hydro_mesh",
                                          var, Spheral.vector_of_double(),
                                          silo.DB_ZONECENT, nblock_vec,
                                          varOpts) == 0

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

        return
Exemplo n.º 5
0
    def writeMasterSiloFile(ndim, nblock, jsplit, baseDirectory, baseName,
                            procDir, materials, vars, label, time, cycle):

        nullOpts = silo.DBoptlist()

        # Decide which domains have information.
        if len(vars[0][0]) > 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(procDir, "domain%i.silo:%%s" % i)
            for i in xrange(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, silo.DB_CLOBBER, silo.DB_LOCAL, label,
                              silo.DB_HDF5)
            nullOpts = silo.DBoptlist()

            # Write the domain file names and types.
            domainNames = Spheral.vector_of_string(
                [p % "hblk0/hydro_mesh" for p in domainNamePatterns])
            meshTypes = Spheral.vector_of_int([silo.DB_QUADMESH] * maxproc)
            optlist = silo.DBoptlist(1024)
            assert optlist.addOption(silo.DBOPT_CYCLE, cycle) == 0
            assert optlist.addOption(silo.DBOPT_DTIME, time) == 0
            assert silo.DBPutMultimesh(f, "hydro_mesh", domainNames, meshTypes,
                                       optlist) == 0

            # Write material names.
            if materials:
                material_names = Spheral.vector_of_string(
                    [p % "/hblk0/Materials" for p in domainNamePatterns])
                matnames = Spheral.vector_of_string(
                    ["void"] + [x.name for x in materials])
                matnos = Spheral.vector_of_int(range(len(materials) + 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(silo.DBOPT_CYCLE, cycle) == 0
                assert optlist.addOption(silo.DBOPT_DTIME, time) == 0
                assert optlist.addOption(silo.DBOPT_MMESH_NAME,
                                         "hydro_mesh") == 0
                assert optlist.addOption(silo.DBOPT_MATNAMES,
                                         silo.DBOPT_NMATNOS, matnames) == 0
                assert optlist.addOption(silo.DBOPT_MATNOS, silo.DBOPT_NMATNOS,
                                         matnos) == 0
                assert silo.DBPutMultimat(f, "Materials", material_names,
                                          optlist) == 0

            # Write the variables descriptors.
            types = Spheral.vector_of_int([silo.DB_QUADVAR] * maxproc)
            for var, varname in vars:
                domainVarNames = Spheral.vector_of_string()
                for iproc, p in enumerate(domainNamePatterns):
                    domainVarNames.append(p % ("/hblk0/" + varname))
                assert len(domainVarNames) == maxproc
                optlistMV = silo.DBoptlist()
                assert optlistMV.addOption(silo.DBOPT_CYCLE, cycle) == 0
                assert optlistMV.addOption(silo.DBOPT_DTIME, time) == 0
                #assert optlistMV.addOption(silo.DBOPT_TENSOR_RANK, silo.DB_VARTYPE_SCALAR) == 0
                assert optlistMV.addOption(silo.DBOPT_BLOCKORIGIN, 0) == 0
                assert optlistMV.addOption(silo.DBOPT_MMESH_NAME,
                                           "hydro_mesh") == 0
                assert silo.DBPutMultivar(f, varname, domainVarNames, types,
                                          optlistMV) == 0

            # Write the dummy variable "akap_0" to fool Hades into thinking we're actually Hydra or something.
            assert silo.DBPutQuadvar1(
                f, "akap_0", "hydro_mesh",
                Spheral.vector_of_double([0.0] * (ndim * ndim)),
                Spheral.vector_of_double(), silo.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(range(maxproc))
            domainFiles = Spheral.vector_of_vector_of_int(
                [Spheral.vector_of_int(range(maxproc))])
            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([0] * 12)
                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
Exemplo n.º 6
0
def hadesDump1(integrator,
               nsample,
               xmin,
               xmax,
               W,
               isotopes,
               baseFileName,
               baseDirectory=".",
               mask=None,
               dumpGhosts=True,
               materials="all"):

    # We currently only support 3-D.
    assert isinstance(integrator, Spheral.Integrator3d)
    assert len(nsample) == 3
    assert isinstance(xmin, Spheral.Vector3d)
    assert isinstance(xmax, Spheral.Vector3d)
    assert isinstance(W, Spheral.TableKernel3d)
    for x in isotopes:
        for xx in x:
            assert len(xx) == 2

    # Prepare to time how long this takes.
    t0 = time.clock()

    # Extract the data base.
    db = integrator.dataBase()

    # If requested, set ghost node info.
    if dumpGhosts and not integrator is None:
        state = Spheral.State3d(db, integrator.physicsPackages())
        derivs = Spheral.StateDerivatives3d(db, integrator.physicsPackages())
        integrator.setGhostNodes()
        integrator.applyGhostBoundaries(state, derivs)

    # Get the set of material names we're going to write.
    if materials == "all":
        materials = [n for n in db.fluidNodeLists()]
    assert len(materials) == len(isotopes)

    # HACK!  We are currently restricting to writing single material output!
    assert len(materials) == 1

    # Make sure the output directory exists.
    import mpi
    import os
    if mpi.rank == 0 and not os.path.exists(baseDirectory):
        try:
            os.makedirs(baseDirectory)
        except:
            raise "Cannot create output directory %s" % baseDirectory
    mpi.barrier()

    # Write the density header file.
    print "hadesDump: writing density header..."
    if mpi.rank == 0:
        filename = os.path.join(baseDirectory, baseFileName + ".spr")
        f = open(filename, "w")
        f.write("3\n")
        for i in xrange(3):
            f.write("%i\n" % nsample[i])
            f.write("%f\n" % xmin(i))
            f.write("%f\n" % ((xmax(i) - xmin(i)) / nsample[i]))
        f.write("3\n")
        f.close()
    mpi.barrier()

    # Sample the density.
    ntot = nsample[0] * nsample[1] * nsample[2]
    for nodes in materials:
        print "hadesDump: sampling density for %s..." % nodes.name
        r = Spheral.VectorFieldList3d()
        H = Spheral.SymTensorFieldList3d()
        rho = Spheral.ScalarFieldList3d()
        r.appendField(nodes.positions())
        w.appendField(nodes.weight())
        H.appendField(nodes.Hfield())
        rho.appendField(nodes.massDensity())

        w = Spheral.ScalarFieldList3d()
        w.copyFields()
        w.appendField(Spheral.ScalarField3d("weight", nodes, 1.0))

        fieldListSet = Spheral.FieldListSet3d()
        fieldListSet.ScalarFieldLists.append(rho)
        localMask = Spheral.IntFieldList3d()
        if mask is None:
            localMask.copyFields()
            localMask.appendField(Spheral.IntField3d("mask", nodes, 1))
        else:
            localMask.appendField(mask.fieldForNodeList(nodes))

        scalar_samples = Spheral.vector_of_vector_of_double()
        vector_samples = Spheral.vector_of_vector_of_Vector3d()
        tensor_samples = Spheral.vector_of_vector_of_Tensor3d()
        symTensor_samples = Spheral.vector_of_vector_of_SymTensor3d()
        nsample_vec = Spheral.vector_of_int(3)
        for i in xrange(3):
            nsample_vec[i] = nsample[i]

        Spheral.sampleMultipleFields2Lattice3d(fieldListSet, r, w, H,
                                               localMask, W, xmin, xmax,
                                               nsample_vec, scalar_samples,
                                               vector_samples, tensor_samples,
                                               symTensor_samples)
        print "Generated %i scalar fields" % len(scalar_samples)
        rhosamp = scalar_fields[0]
        nlocal = len(rhosamp)
        assert mpi.allreduce(nlocal, mpi.SUM) == ntot

        print "hadesDump: writing density for %s..." % nodes.name
        filename = os.path.join(baseDirectory, baseFileName + ".sdt")
        for sendProc in xrange(mpi.procs):
            if mpi.rank == sendProc:
                f = open(filename, "ab")
                f.write(struct.pack(nlocal * "f", *tuple(rhosamp)))
                f.close()
            mpi.barrier()

    # Write the material arrays.
    print "hadesDump: writing material flags..."
    filename = os.path.join(baseDirectory, baseFileName + "_mat.sdt")
    for sendProc in xrange(mpi.procs):
        if mpi.rank == sendProc:
            f = open(filename, "ab")
            f.write(struct.pack(nlocal * "i", *(nlocal * (1, ))))
            f.close()
        mpi.barrier()

    # Write the isotopes.
    print "hadesDump: writing isotopics..."
    if mpi.rank == 0:
        filename = os.path.join(baseDirectory, "isos.mat")
        f = open(filename, "w")
        i = 0
        for isofracs in isotopes:
            f.write("isofrac(%i) =" % i)
            for (iso, frac) in isofracs:
                f.write(" %i %f" % (iso, frac))
            f.write("\n")
            i += 1
        f.close()
    mpi.barrier()

    mpi.barrier()
    print "hadesDump finished: required %0.2f seconds" % (time.clock() - t0)
    return
Exemplo n.º 7
0
def hadesDump0(integrator,
               nsample,
               xmin,
               xmax,
               W,
               isotopes,
               baseFileName,
               baseDirectory=".",
               dumpGhosts=False,
               materials="all"):

    # We currently only support 3-D.
    assert isinstance(integrator, Spheral.Integrator3d)
    assert len(nsample) == 3
    assert isinstance(xmin, Spheral.Vector3d)
    assert isinstance(xmax, Spheral.Vector3d)
    assert isinstance(W, Spheral.TableKernel3d)
    for x in isotopes:
        for xx in x:
            assert len(xx) == 2

    # Prepare to time how long this takes.
    t0 = time.clock()

    # Extract the data base.
    db = integrator.dataBase()

    # If requested, set ghost node info.
    if dumpGhosts and not integrator is None:
        state = Spheral.State3d(db, integrator.physicsPackages())
        derivs = Spheral.StateDerivatives3d(db, integrator.physicsPackages())
        integrator.setGhostNodes()
        integrator.applyGhostBoundaries(state, derivs)

    # Get the set of material names we're going to write.
    if materials == "all":
        materials = [n for n in db.fluidNodeLists()]
    assert len(materials) == len(isotopes)

    # Make sure the output directory exists.
    import mpi
    import os
    if mpi.rank == 0 and not os.path.exists(baseDirectory):
        try:
            os.makedirs(baseDirectory)
        except:
            raise "Cannot create output directory %s" % baseDirectory
    mpi.barrier()

    # Open a file for the output.
    currentTime = integrator.currentTime
    currentCycle = integrator.currentCycle
    filename = baseDirectory + "/" + baseFileName + "-time=%g-cycle=%i.hades" % (
        currentTime, currentCycle)

    if mpi.rank == 0:
        f = open(filename, "wb")

        # Write the header info.
        #f.write(hadesHeader)
        f.write(struct.pack("I", len(materials)))
        f.write(struct.pack("ddd", *tuple(xmin.elements())))
        f.write(struct.pack("ddd", *tuple(xmax.elements())))
        f.write(struct.pack("III", *nsample))
        for materialIsotopes in isotopes:
            f.write(struct.pack("I", len(materialIsotopes)))
            for iso in materialIsotopes:
                f.write(struct.pack("Id", *iso))

    # For each material, sample the mass density and write it out.
    ntot = nsample[0] * nsample[1] * nsample[2]
    for nodes in materials:
        r = Spheral.VectorFieldList3d()
        w = Spheral.ScalarFieldList3d()
        H = Spheral.SymTensorFieldList3d()
        rho = Spheral.ScalarFieldList3d()
        r.appendField(nodes.positions())
        w.appendField(nodes.weight())
        H.appendField(nodes.Hfield())
        rho.appendField(nodes.massDensity())
        fieldListSet = Spheral.FieldListSet3d()
        fieldListSet.ScalarFieldLists.append(rho)
        rhosamp = Spheral.sampleMultipleFields2LatticeMash(
            fieldListSet, r, w, H, W, xmin, xmax, nsample)[0][0][1]
        assert mpi.allreduce(len(rhosamp), mpi.SUM) == ntot
        icum = 0
        for sendProc in xrange(mpi.procs):
            valsproc = [(i, x) for (i, x) in zip(range(ntot), rhosamp)
                        if x > 0.0]
            vals = mpi.bcast(valsproc, sendProc)
            if mpi.rank == 0:
                f.write(struct.pack("I", len(vals)))
                for i, x in vals:
                    f.write(struct.pack("id", i + icum, x))
            icum += len(vals)

    if mpi.rank == 0:
        # Close the file and we're done.
        f.close()

    mpi.barrier()
    print "hadesDump finished: required %0.2f seconds" % (time.clock() - t0)

    return
Exemplo n.º 8
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
Exemplo n.º 9
0
    def __init__(
        self,
        nodeLists,
        filename,
        precision=20,
        serialize=False,
        extraFields=[],
    ):
        self.nodeLists = nodeLists
        self.filename = filename
        self.precision = "%" + "%i.%ie" % (precision + 3, precision)
        self.serialize = serialize
        self.extraFields = extraFields
        self.delimiter = "$"
        self.writing = (mpi.rank == 0 or not serialize)

        # Some mappings to help us write out various data types.
        self._toString = {
            int: self._int2String,
            bool: self._bool2String,
            float: self._float2String,
            str: self._str2String,
            type(Spheral.Vector1d()): self._ContainerFloats2String,
            type(Spheral.Tensor1d()): self._ContainerFloats2String,
            type(Spheral.SymTensor1d()): self._ContainerFloats2String,
            type(Spheral.Vector2d()): self._ContainerFloats2String,
            type(Spheral.Tensor2d()): self._ContainerFloats2String,
            type(Spheral.SymTensor2d()): self._ContainerFloats2String,
            type(Spheral.Vector3d()): self._ContainerFloats2String,
            type(Spheral.Tensor3d()): self._ContainerFloats2String,
            type(Spheral.SymTensor3d()): self._ContainerFloats2String,
        }

        # Open the file.
        if self.writing:
            self.f = gzip.open(filename, "w")
        else:
            self.f = None

        if isinstance(nodeLists[0], Spheral.NodeList1d):
            SymTensorField = Spheral.SymTensorField1d
        elif isinstance(nodeLists[0], Spheral.NodeList2d):
            SymTensorField = Spheral.SymTensorField2d
        elif isinstance(nodeLists[0], Spheral.NodeList3d):
            SymTensorField = Spheral.SymTensorField3d

        # Walk the NodeLists and write the standard fields.
        for nodes in self.nodeLists:
            self.writeField(nodes.positions(), nodes.name, "positions")
            self.writeField(nodes.mass(), nodes.name, "mass")
            self.writeField(nodes.massDensity(), nodes.name, "density")
            self.writeField(nodes.velocity(), nodes.name, "velocity")
            self.writeField(nodes.specificThermalEnergy(), nodes.name,
                            "specificThermalEnergy")
            Hinv2 = SymTensorField("Hinv2", nodes)
            nodes.Hinverse(Hinv2)
            for i in xrange(nodes.numInternalNodes):
                thpt = (Hinv2[i] * Hinv2[i]).Symmetric()
                Hinv2[i] = thpt
            self.writeField(Hinv2, nodes.name, "Hinverse2")

        # Add any extra fields requested.
        for field in self.extraFields:
            nodes = field.nodeList()
            self.writeField(field, nodes.name, field.name)

        return