def randomDistribute(
    thisDomain,  # domain ID to be calculated
    nDomains,  # total number of domains
    nNodesGlobal,  # global number of nodes in this nodelist
    xRangeTotal):  # total simulation volume

    assert thisDomain >= 0 and thisDomain < nDomains
    assert nDomains > 0

    import random
    g = random.Random()
    globalNodeIDs = []
    xNodePositions = []
    dxNodes = (xRangeTotal[1] - xRangeTotal[0]) / nNodesGlobal
    for globalNodeID in xrange(0, nNodesGlobal):
        mpi.barrier()
        domain0 = g.randint(0, nDomains - 1)
        domain = mpi.bcast(domain0)
        if domain == thisDomain:
            globalNodeIDs.append(globalNodeID)
            xNodePositions.append(xRangeTotal[0] +
                                  (globalNodeID + 0.5) * dxNodes)

    assert len(globalNodeIDs) == len(xNodePositions)
    assert mpi.allreduce(len(globalNodeIDs), mpi.SUM) == nNodesGlobal
    return globalNodeIDs, xNodePositions
Example #2
0
    def dumpState(self, cacheFileName):
        def writeNodeData(f, iproc, pos, m, H, vol, surface):
            f.writeObject(pos, "proc%06i/pos" % iproc)
            f.writeObject(m, "proc%06i/m" % iproc)
            f.writeObject(H, "proc%06i/H" % iproc)
            f.writeObject(vol, "proc%06i/vol" % iproc)
            f.writeObject(surface, "proc%06i/surface" % iproc)
            return

        if os.path.splitext(cacheFileName) != ".silo":
            cacheFileName += ".silo"
        if mpi.rank == 0:
            dire = os.path.dirname(cacheFileName)
            if dire and not os.path.exists(dire):
                os.makedirs(dire)
            f = SiloFileIO(cacheFileName, Create)
            f.writeObject(mpi.procs, "numGeneratingProcs")
            writeNodeData(f, 0, self.pos, self.m, self.H, self.vol, self.surface)
            for iproc in xrange(1, mpi.procs):
                pos, m, H, vol, surface = mpi.recv(source=iproc)[0]
                writeNodeData(f, iproc, pos, m, H, vol, surface)
            f.close()
        else:
            mpi.send((self.pos, self.m, self.H, self.vol, self.surface), dest=0)
        mpi.barrier()
        return
Example #3
0
    def dump(self, simulationTime, cycle, format="ascii"):

        # Build the name of the directory we will be stuffing the viz file
        # into.
        outputdir = os.path.join(self.baseDirectory, self.baseFileName)

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

        # Now build the output file name, including directory.  Make sure
        # the file does not already exist -- if it does we default to overwriting.
        filename = os.path.join(
            outputdir, self.baseFileName + "-time=%g-cycle=%i.gnu" %
            (simulationTime, cycle))
        ##         if os.path.exists(filename):
        ##             raise ValueError, "File %s already exists!  Aborting." % filename

        # Write the output.
        self.gnuDump(filename,
                     nodeLists=self._nodeLists,
                     time=simulationTime,
                     cycle=cycle,
                     scalarFieldGroups=self._scalarFieldGroups,
                     vectorFieldGroups=self._vectorFieldGroups,
                     tensorFieldGroups=self._tensorFieldGroups,
                     symTensorFieldGroups=self._symTensorFieldGroups)

        # That's it.
        return
def randomDistribute3d(thisDomain,     # domain ID to be calculated
                       nDomains,       # total number of domains
                       nNodesGlobal,   # global number of nodes in this nodelist
                       xyzRangeTotal): # total simulation volume

    assert thisDomain >= 0 and thisDomain < nDomains
    assert nDomains > 0

    import random
    g = random.Random()
    globalNodeIDs = []
    nodePositions = []
    for globalNodeID in xrange(0, nNodesGlobal):
        mpi.barrier()
        domain0 = g.randint(0, nDomains - 1)
        domain = mpi.bcast(domain0)
        if domain == thisDomain:
            globalNodeIDs.append(globalNodeID)
            nodePositions.append(Vector3d(g.uniform(xyzRangeTotal[0][0],
                                                    xyzRangeTotal[1][0]),
                                          g.uniform(xyzRangeTotal[0][1],
                                                    xyzRangeTotal[1][1]),
                                          g.uniform(xyzRangeTotal[0][2],
                                                    xyzRangeTotal[1][2])))

    assert len(globalNodeIDs) == len(nodePositions)
    assert mpi.allreduce(len(globalNodeIDs), mpi.SUM) == nNodesGlobal
    return globalNodeIDs, nodePositions
Example #5
0
 def testPipe(self):
     try:
         pipe = os.popen('ls')
         s = pipe.read()
         status = pipe.close()
     finally:
         mpi.barrier()
     return
Example #6
0
 def testPipe(self):
     try:
         pipe = os.popen('ls')
         s = pipe.read()
         status = pipe.close()
     finally:
         mpi.barrier()
     return
Example #7
0
 def __init__(self, fname):
     import mpi
     if (not os.path.exists(fname) and mpi.rank == 0):
         con = sqlite3.connect(fname, detect_types=sqlite3.PARSE_DECLTYPES)
         cur = con.cursor()
         cur.execute("CREATE TABLE npdb (id STRING PRIMARY KEY, arr ARRAY)")
         con.commit()
     mpi.barrier()
     
     self.con = sqlite3.connect(fname, timeout=60., detect_types=sqlite3.PARSE_DECLTYPES)
Example #8
0
 def ping(self, msg):
     if mpi.procs < 2: return
     try:
         if mpi.rank == 0:
             received, status = mpi.recv(1)
             self.failUnless(received == msg, 'Bad message received')
         elif mpi.rank == 1:
             mpi.send(msg, 0)
     finally:
         mpi.barrier()
Example #9
0
 def ping(self,msg):
     if mpi.procs < 2: return
     try:
         if mpi.rank == 0:
             received,status = mpi.recv(1)
             self.failUnless(received==msg,'Bad message received')
         elif mpi.rank == 1:
             mpi.send(msg,0)
     finally:
         mpi.barrier()
Example #10
0
 def __init__(self, fname):
     import mpi
     if (not os.path.exists(fname) and mpi.rank == 0):
         con = sqlite3.connect(fname, detect_types=sqlite3.PARSE_DECLTYPES)
         cur = con.cursor()
         cur.execute("CREATE TABLE npdb (id STRING PRIMARY KEY, arr ARRAY)")
         con.commit()
     mpi.barrier()
     
     self.con = sqlite3.connect(fname, timeout=60., detect_types=sqlite3.PARSE_DECLTYPES)
Example #11
0
def main():

    #BasicSuite = unittest.TestSuite()
    #BasicSuite.addTest(PyMPIBarrierTestCase())
    if mpi.rank == 0:
        print "** Testing pyMPI package with "+sys.executable+" on "+sys.platform
        print "Interpreter Version: "+sys.version
        mpi.barrier()
        unittest.TextTestRunner(stream = sys.stdout).run(BasicSuite)
    else:
        mpi.barrier()
        unittest.TextTestRunner(stream = _NullStream()).run(BasicSuite)
Example #12
0
def main():

    #BasicSuite = unittest.TestSuite()
    #BasicSuite.addTest(PyMPIBarrierTestCase())
    if mpi.rank == 0:
        print "** Testing pyMPI package with " + sys.executable + " on " + sys.platform
        print "Interpreter Version: " + sys.version
        mpi.barrier()
        unittest.TextTestRunner(stream=sys.stdout).run(BasicSuite)
    else:
        mpi.barrier()
        unittest.TextTestRunner(stream=_NullStream()).run(BasicSuite)
Example #13
0
 def dropViz(self, cycle=None, Time=None, dt=None):
     mpi.barrier()
     db = self.integrator.dataBase()
     db.updateConnectivityMap(False)
     bcs = self.integrator.uniqueBoundaryConditions()
     self.vizMethod(self.integrator,
                    baseFileName=self.vizBaseName,
                    baseDirectory=self.vizDir,
                    fields=list(self.vizFields),
                    fieldLists=list(self.vizFieldLists),
                    currentTime=self.time(),
                    currentCycle=self.totalSteps,
                    dumpGhosts=self.vizGhosts,
                    dumpDerivatives=self.vizDerivs,
                    boundaries=bcs)
     return
Example #14
0
    def parallelRunTest(self):
        myList = [ 0,0,0,0,0,0 ]

        myList[0] = 42
        myList[1] = "Hello"
        myList[2] = ["Another list", 2]
        myList[3] = ("A tuple", 2)
        myList[4] = 4+5j
        myList[5] = 3.14159

        for x in range(6):
            mpi.barrier()
            z = mpi.bcast( myList[x],0 )
            if z != myList[x]:
                self.fail( "Broadcast test 2 failed on test " + str(x))

        return
Example #15
0
    def parallelRunTest(self):
        myList = [0, 0, 0, 0, 0, 0]

        myList[0] = 42
        myList[1] = "Hello"
        myList[2] = ["Another list", 2]
        myList[3] = ("A tuple", 2)
        myList[4] = 4 + 5j
        myList[5] = 3.14159

        for x in range(6):
            mpi.barrier()
            z = mpi.bcast(myList[x], 0)
            if z != myList[x]:
                self.fail("Broadcast test 2 failed on test " + str(x))

        return
Example #16
0
 def parallelRunTest(self):
     import math
     if mpi.procs < 2:
         #Doing this serially as I only have one processor
         integralPi = self.integrate(mpi, 10000, self.f)
         integralSin = self.integrate(mpi, 10000, math.sin)
     else:
         # -----------------------------------------------
         # Split into two independent communicators
         # -----------------------------------------------
         split = mpi.procs / 2
         first = mpi.comm_create(range(0, split))
         second = mpi.comm_create(range(split, mpi.procs))
         mpi.barrier()
         if first:
             integralPi = self.integrate(first, 10000, self.f)
         elif second:
             integralSin = self.integrate(second, 10000, math.sin)
     return
Example #17
0
 def parallelRunTest(self):
     import math
     if mpi.procs < 2:
         #Doing this serially as I only have one processor
         integralPi = self.integrate(mpi,10000,self.f)
         integralSin = self.integrate(mpi,10000,math.sin)
     else:
         # -----------------------------------------------
         # Split into two independent communicators
         # -----------------------------------------------
         split = mpi.procs/2
         first = mpi.comm_create(range(0,split))
         second = mpi.comm_create(range(split,mpi.procs))
         mpi.barrier()
         if first:            
             integralPi = self.integrate(first,10000,self.f)
         elif second:
             integralSin = self.integrate(second,10000,math.sin)
     return
Example #18
0
 def dropViz(self, cycle=None, Time=None, dt=None):
     mpi.barrier()
     import time
     start = time.clock()
     db = self.integrator.dataBase
     db.updateConnectivityMap(False)
     bcs = self.integrator.uniqueBoundaryConditions()
     self.integrator.setGhostNodes()
     self.vizMethod(self.integrator,
                    baseFileName=self.vizBaseName,
                    baseDirectory=self.vizDir,
                    fields=list(self.vizFields),
                    fieldLists=list(self.vizFieldLists),
                    currentTime=self.time(),
                    currentCycle=self.totalSteps,
                    dumpGhosts=self.vizGhosts,
                    dumpDerivatives=self.vizDerivs,
                    boundaries=bcs)
     print "Wrote viz file in %0.2f seconds" % (time.clock() - start)
     return
Example #19
0
 def testPolyhedralMeshElementIDs(self):
     mesh, void = generatePolyhedralMesh([self.nodes],
                                         xmin=xmin,
                                         xmax=xmax,
                                         generateVoid=False,
                                         generateParallelConnectivity=False)
     for i in xrange(self.nodes.numInternalNodes):
         node = mesh.node(i)
         assert node.ID == i
     for i in xrange(mesh.numEdges):
         edge = mesh.edge(i)
         assert edge.ID == i
     for i in xrange(mesh.numFaces):
         face = mesh.face(i)
         assert face.ID == i
     for i in xrange(mesh.numZones):
         zone = mesh.zone(i)
         assert zone.ID == i
     mpi.barrier()
     return
Example #20
0
    def runTest(self):
        mpi.barrier()

        name = self.__class__.__name__
        if name[:5] == 'PyMPI': name = name[5:]
        if name[-8:] == 'TestCase': name = name[:-8]
        mpi.trace(name + '<')

        try:
            try:
                self.parallelRunTest()
                any_errors = mpi.gather([])
            except:
                any_errors = mpi.gather([self.__message()])

            if mpi.rank == 0 and any_errors:
                import string
                raise self.failureException, string.join(any_errors, '')
        finally:
            mpi.barrier()
            mpi.traceln('>')

        return
Example #21
0
    def runTest(self):
        mpi.barrier()

        name = self.__class__.__name__
        if name[:5] == 'PyMPI': name = name[5:]
        if name[-8:] == 'TestCase': name = name[:-8]
        mpi.trace(name+'<')

        try:
            try:
                self.parallelRunTest()
                any_errors = mpi.gather([])
            except:
                any_errors = mpi.gather([self.__message()])

            if mpi.rank == 0 and any_errors:
                import string
                raise self.failureException,string.join(any_errors,'')
        finally:
            mpi.barrier()
            mpi.traceln('>')

        return
Example #22
0
def writeGridFile( nx, ny, nz, Px, Py, Pz, nProcs):
    import mpi

    if nProcs == 0:
        nProcs = mpi.procs
    
    # case when Px Py and Pz are not specified, we compute reasonable, consistent values
    if Px == 0:
        # compute the block sizes Px, Py, Pz
        Px = largestDivisor(nProcs,  1.0/3.0)
        Py = largestDivisor(nProcs/Px, 1.0/2.0)
        Pz = nProcs/(Px*Py)
        if nProcs != Px*Py*Pz :
            print "FACTORIZATION FAILURE for ",nProcs,"  Px=",Px,"  Py=",Py,"  Pz=",Pz
    
    fileName = 'grid_%i_%ix%ix%i.cmg' % ( nProcs, nx, ny, nz)
    
    if mpi.rank == 0:
        fileWriter( fileName, nx, ny, nz, Px, Py, Pz)
    
    mpi.barrier()

    return fileName
logDir = os.path.join(jobDir, 'logs')
restartName = os.path.join(restartDir, jobName)

# Check if the necessary directories exist.  If not, create them.
if mpi.rank == 0:
    if not os.path.exists(jobDir):
        os.makedirs(jobDir)
    if not os.path.exists(vizDir):
        os.makedirs(vizDir)
    if not os.path.exists(restartDir):
        os.makedirs(restartDir)
    if not os.path.exists(outDir):
        os.makedirs(outDir)
    if not os.path.exists(logDir):
        os.makedirs(logDir)
mpi.barrier()
if not os.path.exists(restartDir):
    os.makedirs(restartDir)
mpi.barrier()

# If we're restarting, find the set of most recent restart files.
if restoreCycle is None:
    restoreCycle = findLastRestart(restartName)

# Here's a quick way to save a record of parameters used in this run.
shutil.copyfile(__file__,logDir+'/{}.ini.{}'.format(jobName,restoreCycle))

#-------------------------------------------------------------------------------
# NAV Node construction
# Here we create and populate node lists with initial conditions. In spheral, the
# construction order is as follows:
Example #24
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
Example #25
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
Example #26
0
def barrier():
    if not lparallel: return
    mpi.barrier()
Example #27
0
import numpy as nm
import mpi
import mpi.array
rank,size = mpi.init()

assert size >= 2

if(rank == 0):
    mydata = nm.ones((3,3,3),nm.int32)
else:
    mydata = nm.zeros((3,3,3),nm.int32)

result = mpi.array.gather( mydata, 0, mpi.MPI_COMM_WORLD )

for i in range(size):
    if (rank == i):
        print result
    mpi.barrier(mpi.MPI_COMM_WORLD)

mpi.finalize()
Example #28
0
    def dump(self, simulationTime, cycle, format="ascii"):

        # Build the name of the directory we will be stuffing the viz file
        # into.
        outputdir = self.baseDirectory  # os.path.join(self.baseDirectory, self.baseFileName)

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

        # Now build the output file name, including directory.  Make sure
        # the file does not already exist -- if it does we default to overwriting.
        filename = os.path.join(
            outputdir,
            self.baseFileName + "-time=%g-cycle=%i" % (simulationTime, cycle))
        ##         if os.path.exists(filename):
        ##             raise ValueError, "File %s already exists!  Aborting." % filename

        # Did the user provide a FieldList of cell geometries already?
        # start = TIME.clock()
        if self.cells:

            # Yep, so we build a disjoint set of cells as a polytope tessellation.
            mesh = eval("polytope.Tessellation%s()" % self.dimension)
            nDim = eval("Vector%s.nDimensions" % self.dimension)

            # Are we splitting into triangles/tets, or writing the native polygons/polyhera?
            if self.splitCells:
                index2zone = []
                for nodeListi in xrange(len(self.cells)):
                    n = self.cells[nodeListi].numInternalElements
                    for i in xrange(n):
                        celli = self.cells(nodeListi, i)
                        verts = celli.vertices()
                        noldnodes = len(mesh.nodes) / nDim
                        noldfaces = len(mesh.faces)
                        noldcells = len(mesh.cells)
                        for j in xrange(len(verts)):
                            for k in xrange(nDim):
                                mesh.nodes.append(verts[j][k])

                        if nDim == 2:
                            # Build the triangles
                            PCcelli = PolyClipper.Polygon()
                            PolyClipper.convertToPolygon(PCcelli, celli)
                            tris = PolyClipper.splitIntoTriangles(
                                PCcelli, 1e-10)
                            index2zone.append([])
                            mesh.faces.resize(noldfaces + 3 * len(tris))
                            mesh.cells.resize(noldcells + len(tris))
                            for k, tri in enumerate(tris):
                                mesh.faces[noldfaces + 3 * k +
                                           0].append(noldnodes + tri[0])
                                mesh.faces[noldfaces + 3 * k +
                                           0].append(noldnodes + tri[1])
                                mesh.faces[noldfaces + 3 * k +
                                           1].append(noldnodes + tri[1])
                                mesh.faces[noldfaces + 3 * k +
                                           1].append(noldnodes + tri[2])
                                mesh.faces[noldfaces + 3 * k +
                                           2].append(noldnodes + tri[2])
                                mesh.faces[noldfaces + 3 * k +
                                           2].append(noldnodes + tri[0])
                                mesh.cells[noldcells + k].append(noldfaces +
                                                                 3 * k)
                                mesh.cells[noldcells + k].append(noldfaces +
                                                                 3 * k + 1)
                                mesh.cells[noldcells + k].append(noldfaces +
                                                                 3 * k + 2)
                                index2zone[-1].append(noldcells + k)

                        else:
                            # Build the tetrahedra
                            assert nDim == 3
                            PCcelli = PolyClipper.Polyhedron()
                            PolyClipper.convertToPolyhedron(PCcelli, celli)
                            tets = PolyClipper.splitIntoTetrahedra(
                                PCcelli, 1e-10)
                            index2zone.append([])
                            mesh.faces.resize(noldfaces + 4 * len(tets))
                            mesh.cells.resize(noldcells + len(tets))
                            for k, tet in enumerate(tets):
                                mesh.faces[noldfaces + 4 * k +
                                           0].append(noldnodes + tet[0])
                                mesh.faces[noldfaces + 4 * k +
                                           0].append(noldnodes + tet[1])
                                mesh.faces[noldfaces + 4 * k +
                                           0].append(noldnodes + tet[2])

                                mesh.faces[noldfaces + 4 * k +
                                           1].append(noldnodes + tet[1])
                                mesh.faces[noldfaces + 4 * k +
                                           1].append(noldnodes + tet[3])
                                mesh.faces[noldfaces + 4 * k +
                                           1].append(noldnodes + tet[2])

                                mesh.faces[noldfaces + 4 * k +
                                           2].append(noldnodes + tet[3])
                                mesh.faces[noldfaces + 4 * k +
                                           2].append(noldnodes + tet[0])
                                mesh.faces[noldfaces + 4 * k +
                                           2].append(noldnodes + tet[2])

                                mesh.faces[noldfaces + 4 * k +
                                           3].append(noldnodes + tet[0])
                                mesh.faces[noldfaces + 4 * k +
                                           3].append(noldnodes + tet[3])
                                mesh.faces[noldfaces + 4 * k +
                                           3].append(noldnodes + tet[1])

                                mesh.cells[noldcells + k].append(noldfaces +
                                                                 4 * k)
                                mesh.cells[noldcells + k].append(noldfaces +
                                                                 4 * k + 1)
                                mesh.cells[noldcells + k].append(noldfaces +
                                                                 4 * k + 2)
                                mesh.cells[noldcells + k].append(noldfaces +
                                                                 4 * k + 3)
                                index2zone[-1].append(noldcells + k)

            else:
                index2zone = None
                copy2polytope(self.cells, mesh)
                # for nodeListi in xrange(len(self.cells)):
                #     n = self.cells[nodeListi].numInternalElements
                #     noldcells = len(mesh.cells)
                #     mesh.cells.resize(noldcells + n)
                #     for i in xrange(n):
                #         celli = self.cells(nodeListi, i)
                #         verts = celli.vertices
                #         facets = celli.facets
                #         noldnodes = len(mesh.nodes)/nDim
                #         noldfaces = len(mesh.faces)
                #         mesh.faces.resize(noldfaces + len(facets))
                #         for j in xrange(len(verts)):
                #             for k in xrange(nDim):
                #                 mesh.nodes.append(verts[j][k])
                #         for j in xrange(len(facets)):
                #             mesh.cells[noldcells + i].append(noldfaces + j)
                #             ipoints = facets[j].ipoints
                #             for k in ipoints:
                #                 mesh.faces[noldfaces + j].append(noldnodes + k)

        else:

            # We need to do the full up polytope tessellation.
            # Build the set of generators from our points.
            gens = vector_of_double()
            nDim = eval("Vector%s.nDimensions" % self.dimension)
            xmin = vector_of_double([1e100] * nDim)
            xmax = vector_of_double([-1e100] * nDim)
            for nodes in self._nodeLists:
                pos = nodes.positions()
                for i in xrange(nodes.numInternalNodes):
                    for j in xrange(nDim):
                        gens.append(pos[i][j])
                        xmin[j] = min(xmin[j], pos[i][j])
                        xmax[j] = max(xmax[j], pos[i][j])

            # Check the boundaries for any additional points we want to use for the bounding box.
            for bound in self._boundaries:
                try:
                    pb = dynamicCastBoundaryToPlanarBoundary2d(bound)
                    for p in (pb.enterPlane.point, pb.exitPlane.point):
                        for j in xrange(nDim):
                            xmin[j] = min(xmin[j], p[j])
                            xmax[j] = max(xmax[j], p[j])
                except:
                    pass

            # Globally reduce and puff up a bit.
            for j in xrange(nDim):
                xmin[j] = mpi.allreduce(xmin[j], mpi.MIN)
                xmax[j] = mpi.allreduce(xmax[j], mpi.MAX)
                delta = 0.01 * (xmax[j] - xmin[j])
                xmin[j] -= delta
                xmax[j] += delta

            # Build the PLC.
            plc = polytope.PLC2d()
            plc.facets.resize(4)
            for i in xrange(4):
                plc.facets[i].resize(2)
                plc.facets[i][0] = i
                plc.facets[i][1] = (i + 1) % 4
            plccoords = vector_of_double(8)
            plccoords[0] = xmin[0]
            plccoords[1] = xmin[1]
            plccoords[2] = xmax[0]
            plccoords[3] = xmin[1]
            plccoords[4] = xmax[0]
            plccoords[5] = xmax[1]
            plccoords[6] = xmin[0]
            plccoords[7] = xmax[1]

            # Blago!
            # f = open("generators_%i_of_%i.txt" % (mpi.rank, mpi.procs), "w")
            # f.write("# generators x    y\n")
            # for i in xrange(len(gens)/2):
            #     f.write("%g    %g\n" % (gens[2*i], gens[2*i+1]))
            # f.write("# PLC coords    x     y\n")
            # for i in xrange(len(plccoords)/2):
            #     f.write("%g    %g\n" % (plccoords[2*i], plccoords[2*i+1]))
            # f.close()
            # Blago!

            # Build the tessellation.
            if self.dimension == "2d":
                mesh = polytope.Tessellation2d()
                if "TriangleTessellator2d" in dir(polytope):
                    serial_tessellator = polytope.TriangleTessellator2d()
                else:
                    assert "BoostTessellator2d" in dir(polytope)
                    serial_tessellator = polytope.BoostTessellator2d()
            else:
                assert self.dimension == "3d"
                raise RuntimeError, "Sorry: 3D tessellation silo dumps are not supported yet."
            if mpi.procs > 1:
                tessellator = eval(
                    "polytope.DistributedTessellator%s(serial_tessellator, False, True)"
                    % self.dimension)
            else:
                tessellator = serial_tessellator
            index2zone = tessellator.tessellateDegenerate(
                gens, plccoords, plc, 1.0e-8, mesh)

        # print "Took %g sec to generate cells" % (TIME.clock() - start)
        # start = TIME.clock()

        # Figure out how many of each type of field we're dumping.
        intFields = [
            x for x in self._fields
            if isinstance(x, eval("IntField%s" % self.dimension))
        ]
        #[x for x in self._fields if isinstance(x, eval("UnsignedField%s" % self.dimension))] +
        #[x for x in self._fields if isinstance(x, eval("ULLField%s" % self.dimension))])
        scalarFields = [
            x for x in self._fields
            if isinstance(x, eval("ScalarField%s" % self.dimension))
        ]
        vectorFields = [
            x for x in self._fields
            if isinstance(x, eval("VectorField%s" % self.dimension))
        ]
        tensorFields = [
            x for x in self._fields
            if isinstance(x, eval("TensorField%s" % self.dimension))
        ]
        symTensorFields = [
            x for x in self._fields
            if isinstance(x, eval("SymTensorField%s" % self.dimension))
        ]

        # For tensor fields we like to dump out some extra info.
        for f in (tensorFields + symTensorFields):
            n = f.nodeList()
            tr = eval("ScalarField%s('%s_trace', n)" %
                      (self.dimension, f.name))
            det = eval("ScalarField%s('%s_determinant', n)" %
                       (self.dimension, f.name))
            mineigen = eval("ScalarField%s('%s_eigen_min', n)" %
                            (self.dimension, f.name))
            maxeigen = eval("ScalarField%s('%s_eigen_max', n)" %
                            (self.dimension, f.name))
            fvals = f.internalValues()
            for i in xrange(n.numInternalNodes):
                tr[i] = fvals[i].Trace()
                det[i] = fvals[i].Determinant()
                eigen = fvals[i].eigenValues()
                mineigen[i] = eigen.minElement()
                maxeigen[i] = eigen.maxElement()
            scalarFields += [tr, det, mineigen, maxeigen]
        # print "Took %g sec to build output fields" % (TIME.clock() - start)
        # start = TIME.clock()

        # Write the output.
        timeslice = siloMeshDump(filename,
                                 mesh,
                                 index2zone=index2zone,
                                 nodeLists=self._nodeLists,
                                 time=simulationTime,
                                 cycle=cycle,
                                 intFields=intFields,
                                 scalarFields=scalarFields,
                                 vectorFields=vectorFields,
                                 tensorFields=tensorFields,
                                 symTensorFields=symTensorFields)

        # print "Took %g sec to calls siloMeshDump" % (TIME.clock() - start)
        # start = TIME.clock()

        # Write the master file listing all the time slices.
        if mpi.rank == 0:
            mastername = os.path.join(self.baseDirectory, self.masterFileName)
            mf = open(mastername, "a")
            mf.write("%s\n" % timeslice)
            mf.close()
        mpi.barrier()

        # That's it.
        del mesh
        while gc.collect():
            pass
        return
Example #29
0
def plotFieldList(
        fieldList,
        xFunction="%s.x",
        yFunction="%s",
        plotGhosts=False,
        colorNodeLists=False,
        plot=None,
        xRange=[None, None],
        yRange=[None, None],
        plotStyle="ro",
        markerSize=4,
        lineStyle="linetype -1 linewidth 1 pointtype 4 pointsize 1.0",
        winTitle=None,
        lineTitle="",
        xlabel=None,
        ylabel=None,
        filterFunc=None,
        semilogy=False):

    # Do we need to make a new window?
    if plot is None:
        plot = newFigure()

    # How about a filtering function?
    if filterFunc is None:
        filterFunc = lambda x: True

    # Gather the fieldList info across all processors to process 0.
    globalNumNodes = []
    globalX = []
    globalY = []
    for field in fieldList:
        if plotGhosts:
            xvals = field.nodeList().positions().allValues()
            yvals = field.allValues()
        else:
            xvals = field.nodeList().positions().internalValues()
            yvals = field.internalValues()
        localX = []
        localY = []
        for x, y in zip(xvals, yvals):
            if filterFunc(x):
                localX.append(eval(xFunction % "x"))
                localY.append(eval(yFunction % "y"))
        n = len(localX)
        globalNumNodes.append(mpi.allreduce(n, mpi.SUM))
        globalX += mpi.allreduce(localX, mpi.SUM)
        globalY += mpi.allreduce(localY, mpi.SUM)

    if mpi.rank == 0:
        # Find the total number of nodes.
        totalNumNodes = sum(globalNumNodes)
        assert (len(globalNumNodes) == fieldList.numFields)
        assert (len(globalX) == totalNumNodes)
        assert (len(globalY) == totalNumNodes)

        # Set the labels.
        if winTitle: plt.title(winTitle)
        if xlabel: plt.xlabel(xlabel)
        if ylabel: plt.ylabel(ylabel)

        # Finally, loop over the fields and do the deed.
        assert len(globalX) == len(globalY)
        if colorNodeLists:
            cumulativeNumNodes = 0
            for fieldID in xrange(len(globalNumNodes)):
                n = globalNumNodes[fieldID]
                if n:
                    if semilogy:
                        plot.semilogy(
                            globalX[cumulativeNumNodes:cumulativeNumNodes + n],
                            globalY[cumulativeNumNodes:cumulativeNumNodes + n],
                            plotStyle,
                            ms=markerSize,
                            label="%s: %s" %
                            (lineTitle, fieldList[i].nodeList().name))
                    else:
                        plot.plot(
                            globalX[cumulativeNumNodes:cumulativeNumNodes + n],
                            globalY[cumulativeNumNodes:cumulativeNumNodes + n],
                            plotStyle,
                            ms=markerSize,
                            label="%s: %s" %
                            (lineTitle, fieldList[i].nodeList().name))
                    cumulativeNumNodes += n
        else:
            if semilogy:
                plot.semilogy(globalX,
                              globalY,
                              plotStyle,
                              ms=markerSize,
                              label=lineTitle)
            else:
                plot.plot(globalX,
                          globalY,
                          plotStyle,
                          ms=markerSize,
                          label=lineTitle)
        plot.axes.legend()

        # Set the ranges.
        xmin, xmax = plt.xlim()
        ymin, ymax = plt.ylim()
        if xRange[0]: xmin = xRange[0]
        if xRange[1]: xmax = xRange[1]
        if yRange[0]: ymin = yRange[0]
        if yRange[1]: ymax = yRange[1]
        plt.xlim(xmin, xmax)
        plt.ylim(ymin, ymax)

    # That's it, return the Gnuplot object.
    mpi.barrier()
    return plot
Example #30
0
def hadesDump(integrator,
              nsample,
              xmin,
              xmax,
              W,
              baseFileName,
              baseDirectory=".",
              procDirBaseName="domains",
              mask=None,
              materials=None):

    # Currently suppport 2D and 3D.
    db = integrator.dataBase
    if db.nDim == 2:
        import Spheral2d as sph
    elif db.nDim == 3:
        import Spheral3d as sph
    else:
        raise RuntimeError, "hadesDump ERROR: must be 2D or 3D"

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

    # Get the set of material names we're going to write.
    if materials is None:
        materials = list(db.fluidNodeLists())

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

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

    # Sample the density.
    ntot = reduce(mul, nsample)
    for nodes in materials:
        print "hadesDump: sampling density for %s..." % nodes.name
        r = sph.VectorFieldList()
        H = sph.SymTensorFieldList()
        rho = sph.ScalarFieldList()
        r.appendField(nodes.positions())
        H.appendField(nodes.Hfield())
        rho.appendField(nodes.massDensity())

        mf = nodes.mass()
        rhof = nodes.massDensity()
        wf = sph.ScalarField("volume", nodes)
        for i in xrange(nodes.numNodes):
            wf[i] = mf[i] / max(1e-100, rhof[i])
        w = sph.ScalarFieldList()
        w.copyFields()
        w.appendField(wf)
        #w.appendField(sph.ScalarField("weight", nodes, 1.0))

        fieldListSet = sph.FieldListSet()
        fieldListSet.ScalarFieldLists.append(rho)
        localMask = sph.IntFieldList()
        if mask is None:
            localMask.copyFields()
            localMask.appendField(sph.IntField("mask", nodes, 1))
        else:
            localMask.appendField(mask.fieldForNodeList(nodes))

        scalar_samples = sph.vector_of_vector_of_double()
        vector_samples = sph.vector_of_vector_of_Vector()
        tensor_samples = sph.vector_of_vector_of_Tensor()
        symTensor_samples = sph.vector_of_vector_of_SymTensor()

        (scalar_samples, vector_samples, tensor_samples,
         symTensor_samples) = sph.sampleMultipleFields2Lattice(
             fieldListSet, r, w, H, localMask, W, xmin, xmax,
             sph.vector_of_int(nsample))
        print "Generated %i scalar fields" % len(scalar_samples)

        # Write out the silo info
        writeSiloQuadMesh(scalar_data=scalar_samples,
                          ndim=db.nDim,
                          xmin=xmin,
                          xmax=xmax,
                          nglobal=nsample,
                          filename=baseFileName,
                          dirname=baseDirectory,
                          scalar_names=("den", ),
                          materials=materials,
                          time=integrator.currentTime,
                          cycle=integrator.currentCycle,
                          RZ=(GeometryRegistrar.coords() == CoordinateType.RZ))

    mpi.barrier()
    print "hadesDump finished: required %0.2f seconds" % (time.clock() - t0)
    return
Example #31
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
Example #32
0
    def dump(self, simulationTime, cycle, format="ascii"):

        # Build the name of the directory we will be stuffing the viz file
        # into.
        outputdir = os.path.join(self.baseDirectory, self.baseFileName)

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

        # Now build the output file name.
        filename = self.baseFileName + "-time=%g-cycle=%i" % (simulationTime,
                                                              cycle)

        # Build the set of generators from our points.
        gens = vector_of_double()
        for nodes in self._nodeLists:
            pos = nodes.positions()
            for i in xrange(nodes.numInternalNodes):
                for coord in pos[i]:
                    gens.append(coord)

        # Build the tessellation.
        if self.ndim == 2:
            mesh = polytope.Tessellation2d()
            if "TriangleTessellator2d" in dir(polytope):
                serial_tessellator = polytope.TriangleTessellator2d()
            else:
                assert "BoostTessellator2d" in dir(polytope)
                serial_tessellator = polytope.BoostTessellator2d()
        else:
            assert self.ndim == 3
            raise RuntimeError, "Sorry: 3D tessellation silo dumps are not supported yet."
        if mpi.procs > 1:
            tessellator = eval(
                "polytope.DistributedTessellator%s(serial_tessellator)" %
                self.dimension)
        else:
            tessellator = serial_tessellator
        tessellator.tessellate(gens, mesh)

        # Figure out how many of each type of field we're dumping.
        scalarFields = ([
            x for x in self._fields
            if isinstance(x, eval("ScalarField%s" % self.dimension))
        ] + [
            x for x in self._fields
            if isinstance(x, eval("IntField%s" % self.dimension))
        ])
        vectorFields = [
            x for x in self._fields
            if isinstance(x, eval("VectorField%s" % self.dimension))
        ]
        tensorFields = [
            x for x in self._fields
            if isinstance(x, eval("TensorField%s" % self.dimension))
        ]
        symTensorFields = [
            x for x in self._fields
            if isinstance(x, eval("SymTensorField%s" % self.dimension))
        ]

        # For tensor fields we like to dump out some extra info.
        for f in (tensorFields + symTensorFields):
            n = f.nodeList()
            tr = eval("ScalarField%s('%s_trace', n)" %
                      (self.dimension, f.name))
            det = eval("ScalarField%s('%s_determinant', n)" %
                       (self.dimension, f.name))
            mineigen = eval("ScalarField%s('%s_eigen_min', n)" %
                            (self.dimension, f.name))
            maxeigen = eval("ScalarField%s('%s_eigen_max', n)" %
                            (self.dimension, f.name))
            for i in xrange(n.numInternalNodes):
                tr[i] = f[i].Trace()
                det[i] = f[i].Determinant()
                eigen = f[i].eigenValues()
                mineigen[i] = eigen.minElement()
                maxeigen[i] = eigen.maxElement()
            scalarFields += [tr, det, mineigen, maxeigen]

        # Build the dictionary of (name, array) values that the polytope silo method can take.
        # Scalar fields.
        cellFields = {}
        for field in scalarFields:
            cellFields[self.siloMangleName(
                field.name)] = [float(x) for x in field.internalValues()]

        # Vector fields.
        for field in vectorFields:
            cellFields[field.name +
                       "_x"] = [x.x for x in field.internalValues()]
            cellFields[field.name +
                       "_y"] = [x.y for x in field.internalValues()]
            if self.ndim == 3:
                cellFields[field.name +
                           "_z"] = [x.z for x in field.internalValues()]

        # Tensor fields.
        for field in tensorFields:
            cellFields[field.name +
                       "_xx"] = [x.xx for x in field.internalValues()]
            cellFields[field.name +
                       "_xy"] = [x.xy for x in field.internalValues()]
            cellFields[field.name +
                       "_yx"] = [x.yx for x in field.internalValues()]
            cellFields[field.name +
                       "_yy"] = [x.yy for x in field.internalValues()]
            if self.ndim == 3:
                cellFields[field.name +
                           "_xz"] = [x.xz for x in field.internalValues()]
                cellFields[field.name +
                           "_yz"] = [x.yz for x in field.internalValues()]
                cellFields[field.name +
                           "_zx"] = [x.zx for x in field.internalValues()]
                cellFields[field.name +
                           "_zy"] = [x.zy for x in field.internalValues()]
                cellFields[field.name +
                           "_zz"] = [x.zz for x in field.internalValues()]

        # SymTensor fields.
        for field in symTensorFields:
            cellFields[field.name +
                       "_xx"] = [x.xx for x in field.internalValues()]
            cellFields[field.name +
                       "_xy"] = [x.xy for x in field.internalValues()]
            cellFields[field.name +
                       "_yy"] = [x.yy for x in field.internalValues()]
            if self.ndim == 3:
                cellFields[field.name +
                           "_xz"] = [x.xz for x in field.internalValues()]
                cellFields[field.name +
                           "_yz"] = [x.yz for x in field.internalValues()]
                cellFields[field.name +
                           "_zz"] = [x.zz for x in field.internalValues()]

        # Write the output.
        writeTessellation = eval("polytope.writeTessellation%s" %
                                 self.dimension)
        writeTessellation(mesh,
                          filename,
                          outputdir,
                          nodeFields=None,
                          edgeFields=None,
                          faceFields=None,
                          cellFields=cellFields,
                          cycle=cycle,
                          time=simulationTime)

        # Write the master file listing all the time slices.
        timeslice = "%s-%d.silo" % (filename, cycle)
        if mpi.rank == 0:
            mastername = os.path.join(self.baseDirectory, self.baseFileName,
                                      self.masterFileName)
            mf = open(mastername, "a")
            mf.write("%s\n" % timeslice)
            mf.close()
        mpi.barrier()

        # That's it.
        return
Example #33
0
def dumpPhysicsState2Lattice(stateThingy,
                             nsample,
                             xmin,
                             xmax,
                             W,
                             baseFileName,
                             baseDirectory = ".",
                             mask = None,
                             currentTime = None,
                             currentCycle = None,
                             binary = True,
                             dumpGhosts = True,
                             scalarNames = "all",
                             vectorNames = "all",
                             tensorNames = vector_of_string(),
                             symTensorNames = vector_of_string()):

    assert (isinstance(stateThingy, Integrator2d) or
            isinstance(stateThingy, Integrator3d) or
            isinstance(stateThingy, State2d) or
            isinstance(stateThingy, State3d))

    import mpi

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

    # What did we get passed?
    if max([isinstance(stateThingy, x) for x in [Integrator2d, Integrator3d]]):
        integrator = stateThingy
        dataBase = integrator.dataBase()
        state = eval("State%id(integrator.dataBase(), integrator.physicsPackages())" % integrator.dataBase().nDim)
        currentTime = integrator.currentTime
        currentCycle = integrator.currentCycle
    elif max([isinstance(stateThingy, x) for x in [State2d, State3d]]):
        integrator = None
        dataBase = None
        state = stateThingy
        assert currentTime is not None
        assert currentCycle is not None
    assert state is not None

    # Determine the dimensionality.
    nsample_vec = vector_of_int()
    if isinstance(state, State2d):
        nDim = 2
        fieldListSet = FieldListSet2d()
        [nsample_vec.append(x) for x in [nsample[0], nsample[1], 1]]
        xmin = Vector3d(xmin.x, xmin.y, 0.0)
        xmax = Vector3d(xmax.x, xmax.y, 0.0)
    else:
        nDim = 3
        [nsample_vec.append(x) for x in [nsample[0], nsample[1], nsample[2]]]
        fieldListSet = FieldListSet3d()
    nsample = nsample_vec

    # If requested, set ghost node info.
    if dumpGhosts and not integrator is None:
        derivs = eval("StateDerivatives%id(integrator.dataBase(), integrator.physicsPackages())" % nDim)
        integrator.setGhostNodes()
        integrator.applyGhostBoundaries(state, derivs)

    # Determine the set of field names we're going to extract.
    if scalarNames == "all":
        scalarNames = state.scalarFieldNames()
    if vectorNames == "all":
        vectorNames = state.vectorFieldNames()
    if tensorNames == "all":
        tensorNames = state.tensorFieldNames()
    if symTensorNames == "all":
        symTensorNames = state.symTensorFieldNames()

    # Check that the requested names actually exist.
    for name in scalarNames:
        assert name in state.scalarFieldNames()
    for name in vectorNames:
        assert name in state.vectorFieldNames()
    for name in tensorNames:
        assert name in state.tensorFieldNames()
    for name in symTensorNames:
        assert name in state.symTensorFieldNames()

    # Build up the list of field lists in the state object.
    [fieldListSet.ScalarFieldLists.append(state.scalarFields(name)) for name in scalarNames]
    [fieldListSet.VectorFieldLists.append(state.vectorFields(name)) for name in vectorNames]
    [fieldListSet.TensorFieldLists.append(state.tensorFields(name)) for name in tensorNames]
    [fieldListSet.SymTensorFieldLists.append(state.symTensorFields(name)) for name in symTensorNames]
    assert len(fieldListSet.ScalarFieldLists) == len(scalarNames)
    assert len(fieldListSet.VectorFieldLists) == len(vectorNames)
    assert len(fieldListSet.TensorFieldLists) == len(tensorNames)
    assert len(fieldListSet.SymTensorFieldLists) == len(symTensorNames)

    # Build the sampling coordinates.
    assert len(nsample) == 3
    assert min(nsample) > 0
    assert xmax > xmin
    delta = ((xmax.x - xmin.x)/nsample[0],
             (xmax.y - xmin.y)/nsample[1],
             (xmax.z - xmin.z)/nsample[2])
    coords = vector_of_vector_of_double(3)
    for idim in xrange(3):
        coords[idim] = vector_of_double(nsample[idim] + 1)
        for i in xrange(nsample[idim] + 1):
            coords[idim][i] = xmin[idim] + i*delta[idim]
    
    # Get the state fields.
    r = state.vectorFields("position")
    H = state.symTensorFields("H")

    # Create uniform weighting.
    w = eval("ScalarFieldList%id()" % nDim)
    w.copyFields()
    for field in r:
        w.appendField(eval("ScalarField%id('weight', field.nodeList(), 1.0)" % nDim))

    # Did the user provide a mask?
    if not isinstance(mask, eval("IntFieldList%id" % nDim)):
        mask = eval("IntFieldList%id()" % nDim)
        mask.copyFields()
        for field in r:
            mask.appendField(eval("IntField%id('mask', field.nodeList(), 1)" % nDim))
    assert isinstance(mask, eval("IntFieldList%id" % nDim))

    # Make sure the mask covers all our fields!
    for field in r:
        assert(mask.haveNodeList(field.nodeList()))

    # Sample the fields.
    scalar_samples = vector_of_vector_of_double()
    vector_samples = eval("vector_of_vector_of_Vector%id()" % nDim)
    tensor_samples = eval("vector_of_vector_of_Tensor%id()" % nDim)
    symTensor_samples = eval("vector_of_vector_of_SymTensor%id()" % nDim)
    if nDim == 2:
        sampleMultipleFields2LatticeMash2d(fieldListSet,
                                           r,
                                           w,
                                           H,
                                           mask,
                                           W,
                                           Vector2d(xmin.x, xmin.y),
                                           Vector2d(xmax.x, xmax.y),
                                           nsample,
                                           scalar_samples,
                                           vector_samples,
                                           tensor_samples,
                                           symTensor_samples)
    else:
        sampleMultipleFields2LatticeMash3d(fieldListSet,
                                           r,
                                           w,
                                           H,
                                           mask,
                                           W,
                                           Vector3d(xmin.x, xmin.y, xmin.z),
                                           Vector3d(xmax.x, xmax.y, xmax.z),
                                           nsample,
                                           scalar_samples,
                                           vector_samples,
                                           tensor_samples,
                                           symTensor_samples)

    # Put the results together.
    assert len(scalar_samples) == len(scalarNames)
    assert len(vector_samples) == len(vectorNames)
    assert len(tensor_samples) == len(tensorNames)
    assert len(symTensor_samples) == len(symTensorNames)

    # The above sampling method returns the result distributed over all processors.
    # For the Visit dumping routine below to work though, we need to stitch the
    # fields back together.  Note this won't work if there are too many grid points!
    ntot = nsample[0]*nsample[1]*nsample[2]
    def gatherVector(vec):
        for i in xrange(len(vec)):
            if mpi.rank == 0:
                for sendProc in xrange(1, mpi.procs):
                    vals = mpi.recv(sendProc)[0]
                    print "Received %i values from processor %i" % (len(vals), sendProc)
                    vec[i] += vals
            else:
                mpi.send(vec[i], 0)
            if mpi.rank == 0:
                assert len(vec[i]) == ntot
    gatherVector(scalar_samples)
    gatherVector(vector_samples)
    gatherVector(tensor_samples)
    gatherVector(symTensor_samples)

    # We have to remove the spaces from field names before passing them
    # to visit.
    def stripSpaces(vec):
        for i in xrange(len(vec)):
            vec[i].replace(" ", "_")
    stripSpaces(scalarNames)
    stripSpaces(vectorNames)
    stripSpaces(tensorNames)
    stripSpaces(symTensorNames)

    # 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 ValueError, "Cannot create output directory %s" % baseDirectory
    mpi.barrier()

    # Dump the sucker.
    baseName = "%s-time=%g-cycle=%i" % (baseFileName, currentTime, currentCycle)
    filename = os.path.join(baseDirectory, baseName)
    mastername = os.path.join(baseDirectory, baseFileName + "-samplemaster.visit")
    print "Preparing to write %i fields" % (len(scalarNames) +
                                            len(vectorNames) +
                                            len(tensorNames) +
                                            len(symTensorNames))
    if mpi.rank == 0:
        writeRectilinearMesh3d(filename,
                               binary,
                               nsample,
                               coords,
                               scalarNames,
                               vectorNames,
                               tensorNames,
                               symTensorNames,
                               scalar_samples,
                               vector_samples,
                               tensor_samples,
                               symTensor_samples)

        # Add this file to the master file.
        mf = open(mastername, "a")
        mf.write("%s.vtk\n" % baseName)
        mf.close()

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

    return
Example #34
0
def main():
    EMAN.appinit(sys.argv)
    if sys.argv[-1].startswith("usefs="):
        sys.argv = sys.argv[:-1]  # remove the runpar fileserver info

    (options, rawimage, refmap) = parse_command_line()

    sffile = options.sffile
    verbose = options.verbose
    shrink = options.shrink
    mask = options.mask
    first = options.first
    last = options.last
    scorefunc = options.scorefunc

    projfile = options.projection
    output_ptcls = options.update_rawimage
    cmplstfile = options.cmplstfile
    ortlstfile = options.ortlstfile
    startSym = options.startSym
    endSym = options.endSym

    if not options.nocmdlog:
        pid = EMAN.LOGbegin(sys.argv)
        EMAN.LOGInfile(pid, rawimage)
        EMAN.LOGInfile(pid, refmap)
        if projfile:
            EMAN.LOGOutfile(pid, projfile)
        if output_ptcls:
            EMAN.LOGOutfile(pid, output_ptcls)
        if cmplstfile:
            EMAN.LOGOutfile(pid, cmplstfile)
        if ortlstfile:
            EMAN.LOGOutfile(pid, ortlstfile)

    ptcls = []
    if not (mpi or pypar) or ((mpi and mpi.rank == 0) or (pypar and pypar.rank == 0)):
        ptcls = EMAN.image2list(rawimage)
        ptcls = ptcls[first:last]

        print "Read %d particle parameters" % (len(ptcls))
        # ptcls = ptcls[0:10]

    if mpi and mpi.size > 1:
        ptcls = mpi.bcast(ptcls)
        print "rank=%d\t%d particles" % (mpi.rank, len(ptcls))
    elif pypar and pypar.size() > 1:
        ptcls = pypar.broadcast(ptcls)
        print "rank=%d\t%d particles" % (pypar.rank(), len(ptcls))

    if sffile:
        sf = EMAN.XYData()
        sf.readFile(sffile)
        sf.logy()

    if not mpi or ((mpi and mpi.rank == 0) or (pypar and pypar.rank() == 0)):
        if cmplstfile and projfile:
            if output_ptcls:
                raw_tmp = output_ptcls
            else:
                raw_tmp = rawimage
            raw_tmp = rawimage
            fp = open("tmp-" + cmplstfile, "w")
            fp.write("#LST\n")
            for i in range(len(ptcls)):
                fp.write("%d\t%s\n" % (first + i, projfile))
                fp.write("%d\t%s\n" % (first + i, raw_tmp))
            fp.close()
        if (mpi and mpi.size > 1 and mpi.rank == 0) or (pypar and pypar.size() > 1 and pypar.rank() == 0):
            total_recv = 0
            if output_ptcls:
                total_recv += len(ptcls)
            if projfile:
                total_recv += len(ptcls)
            for r in range(total_recv):
                # print "before recv from %d" % (r)
                if mpi:
                    msg, status = mpi.recv()
                else:
                    msg = pypar.receive(r)
                    # print "after recv from %d" % (r)
                    # print msg, status
                d = emdata_load(msg[0])
                fname = msg[1]
                index = msg[2]
                d.writeImage(fname, index)
                print "wrtie %s %d" % (fname, index)
            if options.ortlstfile:
                solutions = []
                for r in range(1, mpi.size):
                    msg, status = mpi.recv(source=r, tag=r)
                    solutions += msg

                def ptcl_cmp(x, y):
                    eq = cmp(x[0], y[0])
                    if not eq:
                        return cmp(x[1], y[1])
                    else:
                        return eq

                solutions.sort(ptcl_cmp)
    if (not mpi or (mpi and ((mpi.size > 1 and mpi.rank > 0) or mpi.size == 1))) or (
        not pypar or (pypar and ((pypar.size() > 1 and pypar.rank() > 0) or pypar.size() == 1))
    ):
        map3d = EMAN.EMData()
        map3d.readImage(refmap, -1)
        map3d.normalize()
        if shrink > 1:
            map3d.meanShrink(shrink)
        map3d.realFilter(0, 0)  # threshold, remove negative pixels

        imgsize = map3d.ySize()

        img = EMAN.EMData()

        ctffilter = EMAN.EMData()
        ctffilter.setSize(imgsize + 2, imgsize, 1)
        ctffilter.setComplex(1)
        ctffilter.setRI(1)

        if (mpi and mpi.size > 1) or (pypar and pypar.size() > 1):
            ptclset = range(mpi.rank - 1, len(ptcls), mpi.size - 1)
        else:
            ptclset = range(0, len(ptcls))

        if mpi:
            print "Process %d/%d: %d/%d particles" % (mpi.rank, mpi.size, len(ptclset), len(ptcls))

        solutions = []
        for i in ptclset:
            ptcl = ptcls[i]
            e = EMAN.Euler(ptcl[2], ptcl[3], ptcl[4])
            dx = ptcl[5] - imgsize / 2
            dy = ptcl[6] - imgsize / 2
            print "%d\talt,az,phi=%8g,%8g,%8g\tx,y=%8g,%8g" % (
                i + first,
                e.alt() * 180 / pi,
                e.az() * 180 / pi,
                e.phi() * 180 / pi,
                dx,
                dy,
            ),

            img.readImage(ptcl[0], ptcl[1])
            img.setTAlign(-dx, -dy, 0)
            img.setRAlign(0, 0, 0)
            img.rotateAndTranslate()  # now img is centered
            img.applyMask(int(mask - max(abs(dx), abs(dy))), 6, 0, 0, 0)
            if img.hasCTF():
                fft = img.doFFT()

                ctfparm = img.getCTF()
                ctffilter.setCTF(ctfparm)
                if options.phasecorrected:
                    if sffile:
                        ctffilter.ctfMap(64, sf)  # Wiener filter with 1/CTF (no sign) correction
                else:
                    if sffile:
                        ctffilter.ctfMap(32, sf)  # Wiener filter with 1/CTF (including sign) correction
                    else:
                        ctffilter.ctfMap(2, EMAN.XYData())  # flip phase

                fft.mult(ctffilter)
                img2 = fft.doIFT()  # now img2 is the CTF-corrected raw image

                img.gimmeFFT()
                del fft
            else:
                img2 = img

            img2.normalize()
            if shrink > 1:
                img2.meanShrink(shrink)
            # if sffile:
            # 	snrcurve = img2.ctfCurve(9, sf)	# absolute SNR
            # else:
            # 	snrcurve = img2.ctfCurve(3, EMAN.XYData())		# relative SNR

            e.setSym(startSym)
            maxscore = -1e30  # the larger the better
            scores = []
            for s in range(e.getMaxSymEl()):
                ef = e.SymN(s)
                # proj = map3d.project3d(ef.alt(), ef.az(), ef.phi(), -6)		# Wen's direct 2D accumulation projection
                proj = map3d.project3d(
                    ef.alt(), ef.az(), ef.phi(), -1
                )  # Pawel's fast projection, ~3 times faster than mode -6 with 216^3
                # don't use mode -4, it modifies its own data
                # proj2 = proj
                proj2 = proj.matchFilter(img2)
                proj2.applyMask(int(mask - max(abs(dx), abs(dy))), 6, 0, 0, 0)
                if scorefunc == "ncccmp":
                    score = proj2.ncccmp(img2)
                elif scorefunc == "lcmp":
                    score = -proj2.lcmp(img2)[0]
                elif scorefunc == "pcmp":
                    score = -proj2.pcmp(img2)
                elif scorefunc == "fsccmp":
                    score = proj2.fscmp(img2, [])
                elif scorefunc == "wfsccmp":
                    score = proj2.fscmp(img2, snrcurve)
                if score > maxscore:
                    maxscore = score
                    best_proj = proj2
                    best_ef = ef
                    best_s = s
                scores.append(score)
                # proj2.writeImage("proj-debug.img",s)
                # print "\tsym %2d/%2d: euler=%8g,%8g,%8g\tscore=%12.7g\tbest=%2d euler=%8g,%8g,%8g score=%12.7g\n" % \
                # 		   (s,60,ef.alt()*180/pi,ef.az()*180/pi,ef.phi()*180/pi,score,best_s,best_ef.alt()*180/pi,best_ef.az()*180/pi,best_ef.phi()*180/pi,maxscore)
            scores = Numeric.array(scores)
            print "\tbest=%2d euler=%8g,%8g,%8g max score=%12.7g\tmean=%12.7g\tmedian=%12.7g\tmin=%12.7g\n" % (
                best_s,
                best_ef.alt() * 180 / pi,
                best_ef.az() * 180 / pi,
                best_ef.phi() * 180 / pi,
                maxscore,
                MLab.mean(scores),
                MLab.median(scores),
                MLab.min(scores),
            )
            if projfile:
                best_proj.setTAlign(dx, dy, 0)
                best_proj.setRAlign(0, 0, 0)
                best_proj.rotateAndTranslate()

                best_proj.set_center_x(ptcl[5])
                best_proj.set_center_y(ptcl[6])
                best_proj.setRAlign(best_ef)
                # print "before proj send from %d" % (mpi.rank)

                if mpi and mpi.size > 1:
                    mpi.send((emdata_dump(best_proj), projfile, i + first), 0)
                elif pypar and pypar.size() > 1:
                    pypar.send((emdata_dump(best_proj), projfile, i + first), 0)
                # print "after proj send from %d" % (mpi.rank)
                else:
                    best_proj.writeImage(projfile, i + first)

            img2.setTAlign(0, 0, 0)
            img2.setRAlign(best_ef)
            img2.setNImg(1)
            # print "before raw send from %d" % (mpi.rank)
            if output_ptcls:
                if mpi and mpi.size > 1:
                    mpi.send((emdata_dump(img2), output_ptcls, i + first), 0)
                elif pypar and pypar.size() > 1:
                    pypar.send((emdata_dump(img2), output_ptcls, i + first), 0)
                # print "after raw send from %d" % (mpi.rank)
                else:
                    img2.writeImage(output_ptcls, i + first)

            solutions.append((ptcl[0], ptcl[1], best_ef.alt(), best_ef.az(), best_ef.phi(), ptcl[5], ptcl[6]))
        if mpi and (mpi.size > 1 and mpi.rank > 0):
            mpi.send(solutions, 0, tag=mpi.rank)

    if mpi:
        mpi.barrier()
    elif pypar:
        pypar.barrier()
    if mpi:
        mpi.finalize()
    elif pypar:
        pypar.finalize()

    if options.cmplstfile:
        os.rename("tmp-" + cmplstfile, cmplstfile)
    if options.ortlstfile:
        lFile = open(options.ortlstfile, "w")
        lFile.write("#LST\n")
        for i in solutions:
            lFile.write(
                "%d\t%s\t%g\t%g\t%g\t%g\t%g\n"
                % (i[1], i[0], i[2] * 180.0 / pi, i[3] * 180.0 / pi, i[4] * 180.0 / pi, i[5], i[6])
            )
        lFile.close()

    if not options.nocmdlog:
        EMAN.LOGend()
Example #35
0
 def parallelRunTest(self):
     mpi.barrier()
     mpi.barrier()
     mpi.barrier()
     mpi.barrier()
     return
Example #36
0
def hadesDump(integrator,
              nsample,
              xmin,
              xmax,
              W,
              baseFileName,
              baseDirectory=".",
              procDirBaseName="domains",
              mask=None,
              materials=None):

    # Currently suppport 2D and 3D.
    db = integrator.dataBase()
    if db.nDim == 2:
        import Spheral2d as sph
    elif db.nDim == 3:
        import Spheral3d as sph
    else:
        raise RuntimeError, "hadesDump ERROR: must be 2D or 3D"

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

    # Get the set of material names we're going to write.
    if materials is None:
        materials = list(db.fluidNodeLists())

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

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

    # Sample the density.
    ntot = reduce(mul, nsample)
    for nodes in materials:
        print "hadesDump: sampling density for %s..." % nodes.name
        r = sph.VectorFieldList()
        H = sph.SymTensorFieldList()
        rho = sph.ScalarFieldList()
        r.appendField(nodes.positions())
        H.appendField(nodes.Hfield())
        rho.appendField(nodes.massDensity())

        mf = nodes.mass()
        rhof = nodes.massDensity()
        wf = sph.ScalarField("volume", nodes)
        for i in xrange(nodes.numNodes):
            wf[i] = mf[i] / max(1e-100, rhof[i])
        w = sph.ScalarFieldList()
        w.copyFields()
        w.appendField(wf)
        #w.appendField(sph.ScalarField("weight", nodes, 1.0))

        fieldListSet = sph.FieldListSet()
        fieldListSet.ScalarFieldLists.append(rho)
        localMask = sph.IntFieldList()
        if mask is None:
            localMask.copyFields()
            localMask.appendField(sph.IntField("mask", nodes, 1))
        else:
            localMask.appendField(mask.fieldForNodeList(nodes))

        scalar_samples = sph.vector_of_vector_of_double()
        vector_samples = sph.vector_of_vector_of_Vector()
        tensor_samples = sph.vector_of_vector_of_Tensor()
        symTensor_samples = sph.vector_of_vector_of_SymTensor()
        nsample_vec = sph.vector_of_int(db.nDim)
        for i in xrange(db.nDim):
            nsample_vec[i] = nsample[i]

        sph.sampleMultipleFields2Lattice(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)

        # Rearrange the sampled data into rectangular blocks due to Silo's quad mesh limitations.
        rhosamp, xminblock, xmaxblock, nblock, jsplit = shuffleIntoBlocks(
            db.nDim, scalar_samples[0], xmin, xmax, nsample)
        if rhosamp:
            print "rho range: ", min(rhosamp), max(rhosamp)
        print "     xmin: ", xmin
        print "     xmax: ", xmax
        print "xminblock: ", xminblock
        print "xmaxblock: ", xmaxblock
        print "   nblock: ", nblock
        assert mpi.allreduce(len(rhosamp), mpi.SUM) == ntot

    # Write the master file.
    maxproc = writeMasterSiloFile(ndim=db.nDim,
                                  nblock=nblock,
                                  jsplit=jsplit,
                                  baseDirectory=baseDirectory,
                                  baseName=baseFileName,
                                  procDirBaseName=procDirBaseName,
                                  materials=materials,
                                  rhosamp=rhosamp,
                                  label="Spheral++ cartesian sampled output",
                                  time=integrator.currentTime,
                                  cycle=integrator.currentCycle)

    # Write the process files.
    writeDomainSiloFile(ndim=db.nDim,
                        jsplit=jsplit,
                        maxproc=maxproc,
                        baseDirectory=baseDirectory,
                        baseName=baseFileName,
                        procDirBaseName=procDirBaseName,
                        materials=materials,
                        rhosamp=rhosamp,
                        xminblock=xminblock,
                        xmaxblock=xmaxblock,
                        nblock=nblock,
                        label="Spheral++ cartesian sampled output",
                        time=integrator.currentTime,
                        cycle=integrator.currentCycle,
                        pretendRZ=db.isRZ)

    mpi.barrier()
    print "hadesDump finished: required %0.2f seconds" % (time.clock() - t0)
    return
Example #37
0
    def dump(self, simulationTime, cycle,
             format = "ascii"):

        # Build the name of the directory we will be stuffing the viz file
        # into.
        outputdir = self.baseDirectory
        if mpi.procs > 1:
            outputdir += "/domain%04i" % mpi.rank

        # Make sure the output directory exists.
        import os
        if not os.path.exists(outputdir):
            try:
                os.makedirs(outputdir)
            except:
                raise ValueError, "Cannot create output directory %s" % outputdir
        mpi.barrier()

        # Now build the output file name, including directory.  Make sure
        # the file does not already exist.
        filename = os.path.join(outputdir,
                                self.baseFileName % (simulationTime, cycle) + 
                                self.extension)
##         if os.path.exists(filename):
##             raise ValueError, "File %s already exists!  Aborting." % filename

        # Open the output file for writing.
        try:
            f = open(filename, "w")
        except:
            raise ValueError, "Unable to open file %s for output" % filename

        # Build the header info for this file, and write it.
        header = self.constructHeader(simulationTime, cycle)
        f.writelines(header)

        # Write whether we're dumping text or binary data.
        if format == "binary":
            f.write(self.keyWords["binary"] + "\n")

            # Close the file, and reopen it in binary mode.
            f.close()
            f = open(filename, "ab")

        else:
            assert format == "ascii"
            f.write(self.keyWords["ascii"] + "\n")

        # Loop over each NodeList.
        for nodeList in self._nodeLists:

            # Write the NodeList declarator.
            if self.dumpGhosts:
                n = nodeList.numNodes
            else:
                n = nodeList.numInternalNodes
            f.write(self.keyWords["nodelist"] + ' "' +
                    nodeList.name + '" %i\n' % n)

            # Loop over the Fields, and call the private method to write
            # the Field data.
            for field in self.fields(nodeList):
                self._dumpField(field, f)

        # Close the data file.
        f.close()

        # Now, if this is a multidomain run we need to write a root
        # file linking together the individiual domain files.
        if mpi.procs > 1 and mpi.rank == 0:

            # Build the root file name, and open the file.
            rootname = self.baseFileName % (simulationTime, cycle) + "-root" + self.extension
            rootfile = os.path.join(self.baseDirectory, rootname)
##             if os.path.exists(rootfile):
##                 raise ValueError, "File %s already exists!  Aborting." % rootfile
            try:
                f = open(rootfile, "w")
            except:
                raise ValueError, "Unable to open root file %s for output" % rootfile

            # Write the standard header info.
            f.writelines(header)

            # Now write the individual domain file names.
            f.write(self.keyWords["filelist"] + " %i\n" % mpi.procs)
            for domain in xrange(mpi.procs):
                filename = "domain%04i" % domain + "/" + \
                           self.baseFileName % (simulationTime, cycle) + \
                           self.extension
                f.write(filename + "\n")

            # That's it, close the root file.
            f.close()

            # Add this root file to the master file.
            mastername = os.path.join(self.baseDirectory, self.masterFileName)
            mf = open(mastername, "a")
            mf.write("%s\n" % rootname)
            mf.close()

        elif mpi.procs == 1:

            # In serial we need to provide the master file.
            mastername = os.path.join(self.baseDirectory, self.masterFileName)
            mf = open(mastername, "a")
            mf.write("%s\n" % os.path.basename(filename))
            mf.close()

        mpi.barrier()
        return
Example #38
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
def pflatten_node_list(nl,filename,do_header=True,nl_id=0,silent=False):
    """Flatten physical field values from a node list to a rectangular ascii file.

    pflatten_node_list(nl,filename) extracts field variables from all nodes of nl,
    which must be a valid node list, and writes them as a rectangular table into
    the text file filename. (A short header is also written, using the # comment
    character so the resulting file can be easily read with numpy.loadtext.) The
    file will be overwritten if it exists. If filename has the .gz extension it
    will be compressed using gzip.

    pflatten_node_list(...,do_header=False) omits the header and appends the 
    flattened nl to the end of the file if one exists.

    pflatten_node_list(...,nl_id=id) places the integer id in the first column
    of every node (row) in the node list. This can be used when appending multiple
    lists to the same file, providing a convenient way to distinguish nodes from
    different lists when the file is later read. The default id (for single node
    list files) is 0.

    The format of the output table is (one line per node):
      id eos_id x y z vx vy vz m rho p T U hmin hmax

    The p in pflatten is for 'parallel', a reminder that all nodes will be
    processed in their local rank, without ever being communicated or collected
    in a single process. Each mpi rank will wait its turn to access the output 
    file, so the writing is in fact serial, but avoids bandwidth and memory waste
    and is thus suitable for large node lists from high-res runs.

    See also: spickle_node_list
    """

    # Make sure we are not wasting our time.
    assert isinstance(nl,(sph.Spheral.NodeSpace.FluidNodeList3d,
                          sph.Spheral.SolidMaterial.SolidNodeList3d)
                      ), "argument 1 must be a node list"
    assert isinstance(filename, str), "argument 2 must be a simple string"
    assert isinstance(do_header, bool), "true or false"
    assert isinstance(silent, bool), "true or false"
    assert isinstance(nl_id, int), "int only idents"
    assert not isinstance(nl_id, bool), "int only idents"

    # Determine if file should be compressed.
    if os.path.splitext(filename)[1] == '.gz':
        import gzip
        open = gzip.open
    else:
        import __builtin__
        open = __builtin__.open

    # Write the header.
    if do_header:
        nbGlobalNodes = mpi.allreduce(nl.numInternalNodes, mpi.SUM)
        header = header_template.format(nbGlobalNodes)
        if mpi.rank == 0:
            fid = open(filename,'w')
            fid.write(header)
            fid.close()
            pass
        pass
     
    # Start collecting data.
    if not silent:
        sys.stdout.write('Flattening ' + nl.label() + ' ' + nl.name + '........')
    
    # Get values of field variables stored in internal nodes.
    xloc = nl.positions().internalValues()
    vloc = nl.velocity().internalValues()
    mloc = nl.mass().internalValues()
    rloc = nl.massDensity().internalValues()
    uloc = nl.specificThermalEnergy().internalValues()
    Hloc = nl.Hfield().internalValues()
    #(pressure and temperature are stored in the eos object.)
    eos = nl.equationOfState()
    ploc = sph.ScalarField('ploc',nl)
    Tloc = sph.ScalarField('loc',nl)
    rref = nl.massDensity()
    uref = nl.specificThermalEnergy()
    eos.setPressure(ploc,rref,uref)
    eos.setTemperature(Tloc,rref,uref)

    # Procs take turns writing internal node values to file.
    for proc in range(mpi.procs):
        if proc == mpi.rank:
            fid = open(filename,'a')
            for nk in range(nl.numInternalNodes):
                line  = "{:2d}  ".format(nl_id)
                line += "{:2d}  ".format(getattr(nl,'eos_id',-1))
                line += "{0.x:+12.5e}  {0.y:+12.5e}  {0.z:+12.5e}  ".format(xloc[nk])
                line += "{0.x:+12.5e}  {0.y:+12.5e}  {0.z:+12.5e}  ".format(vloc[nk])
                line += "{0:+12.5e}  ".format(mloc[nk])
                line += "{0:+12.5e}  ".format(rloc[nk])
                line += "{0:+12.5e}  ".format(ploc[nk])
                line += "{0:+12.5e}  ".format(Tloc[nk])
                line += "{0:+12.5e}  ".format(uloc[nk])
                line += "{0:+12.5e}  ".format(Hloc[nk].Inverse().eigenValues().minElement())
                line += "{0:+12.5e}  ".format(Hloc[nk].Inverse().eigenValues().maxElement())
                line += "\n"
                fid.write(line)
                pass
            fid.close()
            pass
        mpi.barrier()
        pass
     
    # And Bob's our uncle.
    if not silent:
        print "Done."
Example #40
0
def siloPointmeshDump(baseName,
                      fields=[],
                      fieldLists=[],
                      baseDirectory=".",
                      procDirBaseName="proc-%06i",
                      label="Spheral++ point mesh",
                      time=0.0,
                      cycle=0,
                      dumpGhosts=False):

    # You have to give us something!
    if len(fields) + len(fieldLists) == 0:
        raise ValueError, "siloPointmeshDump called with no information to write."

    # Get the set of NodeLists we're working with.
    nodeListsDict = {}
    for field in fields:
        nodes = field.nodeList()
        nodeListsDict[nodes.name] = nodes
    for fl in fieldLists:
        nls = fl.nodeListPtrs()
        for nodes in nls:
            nodeListsDict[nodes.name] = nodes
    nodeLists = [nodeListsDict[name] for name in nodeListsDict]
    assert len(nodeLists) > 0

    # If needed, create the subdirectories for the processor files.
    if mpi.rank == 0:
        if not os.path.exists(baseDirectory):
            os.makedirs(baseDirectory)
        for i in xrange(mpi.procs):
            dire = os.path.join(baseDirectory, procDirBaseName % i)
            if not os.path.exists(dire):
                os.makedirs(dire)
    mpi.barrier()

    # We can only pretend this is an RZ mesh if it's 2D.
    ndim = dimension(nodeLists[0])
    if not ndim in (2, 3):
        raise ValueError, "You need to provide 2D or 3D information for siloPointMeshDump."

    # Characterize the fields we're going to write.
    allfields = fields[:]
    for fl in fieldLists:
        for f in fl:
            allfields.append(f)
    intFields, scalarFields, vectorFields, tensorFields, symTensorFields = [], [], [], [], []
    for f in allfields:
        if isinstance(f, eval("IntField%id" % ndim)):
            intFields.append(f)
        elif isinstance(f, eval("ScalarField%id" % ndim)):
            scalarFields.append(f)
        elif isinstance(f, eval("VectorField%id" % ndim)):
            vectorFields.append(f)
        elif isinstance(f, eval("TensorField%id" % ndim)):
            tensorFields.append(f)
        elif isinstance(f, eval("SymTensorField%id" % ndim)):
            symTensorFields.append(f)
        else:
            print "siloPointmeshDump WARNING: ignoring unknown field type."

    # For any tensor fields, dump the trace, determinant, min, and max eigen values.
    for f in (tensorFields + symTensorFields):
        n = f.nodeList()
        tr = eval("ScalarField%id('%s_trace', n)" % (ndim, f.name))
        det = eval("ScalarField%id('%s_determinant', n)" % (ndim, f.name))
        mineigen = eval("ScalarField%id('%s_eigen_min', n)" % (ndim, f.name))
        maxeigen = eval("ScalarField%id('%s_eigen_max', n)" % (ndim, f.name))
        if dumpGhosts:
            nvals = n.numNodes
        else:
            nvals = n.numInternalNodes
        for i in xrange(nvals):
            eigen = f[i].eigenValues()
            tr[i] = f[i].Trace()
            det[i] = f[i].Determinant()
            mineigen[i] = eigen.minElement()
            maxeigen[i] = eigen.maxElement()
        scalarFields += [tr, det, mineigen, maxeigen]

    # Extract all the fields we're going to write.
    fieldwad = extractFieldComponents(nodeLists, time, cycle, dumpGhosts,
                                      intFields, scalarFields, vectorFields,
                                      tensorFields, symTensorFields)

    # If we're domain 0 we write the master file.
    writeMasterSiloFile(ndim, baseDirectory, baseName, procDirBaseName,
                        nodeLists, label, time, cycle, dumpGhosts, fieldwad)

    # Each domain writes it's domain file.
    writeDomainSiloFile(ndim, baseDirectory, baseName, procDirBaseName,
                        nodeLists, label, time, cycle, dumpGhosts, fieldwad)
Example #41
0
        #correct answers
        correctAnswers = [0,0,0,    0,0,0,    0,0,0]
        for x in range(5):
            correctAnswers[x] = []

        correctAnswers[0] = [nprocs - mpi.rank]
        correctAnswers[1] = list2[ mpi.rank*3: mpi.rank*3 + 3]
        correctAnswers[2] = (2*mpi.rank, 2*mpi.rank+1)
        correctAnswers[3] = "fOo for"+str(mpi.rank%10)
        correctAnswers[4] = longList[2048*mpi.rank: 2048*(mpi.rank+1)]
        
        for x in range(5):
            if results[x] != correctAnswers[x]:
                failString = "scatter failed on test "+str(x)
                failString += " and process " + str(mpi.rank) + "\n"
                failString += "Scatter result was " + str(results[x])
                failString += " and it should be "+ str(correctAnswers[x])
                self.fail( failString);

#====================================================================
# main
#====================================================================

if __name__ == '__main__':
    try:
        unittest.main()
    except:
        pass
    mpi.barrier() # Wait for everyone!
Example #42
0
    #    for i in xrange(25000):
    #        ignore = self.reducer(dt,mpi.MIN)
    #    end = self.memory()

    #    # -----------------------------------------------
    #    # If we have grown much, something is wrong
    #    # -----------------------------------------------
    #    self.failUnless(
    #        (end-start)/(start+1e-6) < 0.10,
    #        'Memory grew less that 2 percent (grew from %d to %d)'%(start,end))
    #    return



##################################################################
#                        CLASS ALLREDUCE                         #
# Allreduce should test everything that reduce does (except that #
# slaves should have the value instead of None).                 #
##################################################################
class allreduce(reduce):
    reducer = mpi.allreduce
    compareSlave = reduce.compareRoot


if __name__ == '__main__':
    try:
        unittest.main()
    except:
        pass
    mpi.barrier() # Wait for everyone!
Example #43
0
def main():
	if sys.argv[-1].startswith("usefs="): sys.argv = sys.argv[:-1]	# remove the runpar fileserver info

	(options,args) =  parse_command_line()
	
	if not options.nolog and (not mpi or (mpi and mpi.rank==0)): EMAN.appinit(sys.argv)

	inputParm = EMAN.ccmlInputParm()
	sf = EMAN.XYData()
	if options.sfFileName != "" :
		readsf = sf.readFile(options.sfFileName)
		if ((readsf == -1) and (options.verbose > 0)) :
			print "The file of scattering factor does NOT exist"
	inputParm.scateringFactor = sf

	startNumOfRawImages = options.startNumOfRawImages
	#endNumOfRawImages = options.endNumOfRawImages

	refImageFileName = args[-1]
	numOfRefImages = options.numOfRefImages
	solutionFile = options.solutionFile

	# write log info to .emanlog file so that eman program can browse the history
	if not options.nolog and (not mpi or (mpi and mpi.rank==0)): 
		pid = EMAN.LOGbegin(sys.argv)
		for f in args[0:-1]: EMAN.LOGInfile(pid,f)
		EMAN.LOGReffile(pid,args[-1])
		if options.solutionFile: EMAN.LOGOutfile(pid,options.solutionFile)
		if options.listFile: EMAN.LOGOutfile(pid,options.listFile)
		if options.mrcSolutionFile: EMAN.LOGOutfile(pid,options.mrcSolutionFile)

	inputParm.sym = options.sym
	inputParm.FFTOverSampleScale = options.FFTOverSampleScale
	inputParm.pftStepSize = options.pftStepSize
	inputParm.deltaR = options.deltaR
	inputParm.RMin = options.RMin
	inputParm.RMax = options.RMax
	inputParm.searchMode = options.searchMode
	inputParm.scalingMode = options.scalingMode
	inputParm.residualMode = options.residualMode
	inputParm.weightMode = options.weightMode
	# inputParm.rawImageFN will be set later
	inputParm.refImagesFN = refImageFileName
	inputParm.rawImageIniParmFN = options.rawImageIniParmFN
	inputParm.rawImagePhaseCorrected = options.phasecorrected

	inputParm.maxNumOfRun = options.maxNumOfRun
	inputParm.zScoreCriterion = options.zScoreCriterion
	inputParm.residualCriterion = options.residualCriterion
	inputParm.solutionCenterDiffCriterion = options.solutionCenterDiffCriterion
	inputParm.solutionOrientationDiffCriterion = options.solutionOrientationDiffCriterion/180.0*pi
	inputParm.maxNumOfIteration = options.maxNumOfIteration
	inputParm.numOfRandomJump = options.numOfRandomJump
	inputParm.numOfFastShrink = options.numOfFastShrink
	inputParm.numOfStartConfigurations = options.numOfStartConfigurations
	inputParm.orientationSearchRange = options.orientationSearchRange/180.0*pi
	inputParm.centerSearchRange = options.centerSearchRange

	inputParm.numOfRefImages = options.numOfRefImages
	inputParm.refEulerConvention = options.refEulerConvention
	#maskR = options.maskR
	#if (maskR<=0): maskR = refImageSizeY/2

	inputParm.verbose = options.verbose
	verbose = options.verbose
	#verboseSolution = options.verboseSolution

	updataHeader = options.updataHeader
	solutionFile = options.solutionFile
	mrcSolutionFile = options.mrcSolutionFile
	iniCenterOrientationMode = options.iniCenterOrientationMode
	refCenterOrientationMode = options.refCenterOrientationMode

	rawImages = []
	if not mpi or (mpi and mpi.rank==0):
		for imgfile in args[0:-1]:
			imgnum = EMAN.fileCount(imgfile)[0]
			for i in range(imgnum): rawImages.append((imgfile, i))
	if mpi: rawImages = mpi.bcast(rawImages)
	
	endNumOfRawImages = options.endNumOfRawImages
	if endNumOfRawImages <=0  or endNumOfRawImages > len(rawImages):
		endNumOfRawImages = len(rawImages)

	numRawImages = endNumOfRawImages - startNumOfRawImages

	if mpi:
		ptclset = range(startNumOfRawImages + mpi.rank, endNumOfRawImages, mpi.size)
	else:
		ptclset = range(startNumOfRawImages, endNumOfRawImages)
	
	solutions = []

	rMask = options.rMask        #mask size is given
	if options.rMask <= 0 : rMask = refImageSizeY/2   #mask size = half image size
	
	rMask1 = options.rMask1             #output tnf mask size is given
	if options.rMask1 <= 0 : rMask1 = rMask    #output tnf mask size = half image size

	inputParm.rMask = rMask
	inputParm.rMask1 = rMask1

	rawImage = EMAN.EMData()
	rawImage.getEuler().setSym(inputParm.sym) #set the symmetry of the raw partile
	inputParm.rawImageFN = rawImages[0][0] #give the initial raw particle filename
	print "start to prepare------"
	rawImage.crossCommonLineSearchPrepare(inputParm) #prepare, create pseudo PFT of ref images
	print "end to prepare------"
	inputParm.rawImage = rawImage
	#for rawImgSN in ptclset:
	for index in range(len(ptclset)):
		rawImgSN = ptclset[index]
		inputParm.rawImageFN = rawImages[rawImgSN][0]
		inputParm.thisRawImageSN = rawImages[rawImgSN][1]
		if mpi: print "rank %d: %d in %d-%d (%d in %d-%d)" % (mpi.rank, rawImgSN, startNumOfRawImages, endNumOfRawImages, index, 0, len(ptclset))
		#rawImage.readImage(rawImages[rawImgSN][0], rawImages[rawImgSN][1])

		#rawImage.applyMask(rMask, 6) #apply mask type 6 [edge mean value] to raw image, center will be image center
		#rawImage.getEuler().setSym("icos")
		#if rawImage.hasCTF() == 1:
			#ctfParm = rawImage.getCTF()
			#inputParm.zScoreCriterion = options.zScoreCriterion + atan(abs(ctfParm[0])-1.5)/(pi/4) +0.59 #adjust zScore criterion -0.6 --> +1.2, 1.5, 2.0
			#inputParm.numOfRefImages = int(min(numOfRefImages, max(numOfRefImages*exp(-(abs(ctfParm[0])/2.0-0.15))+0.5, 5.0))) # adjust maxNumOfRun, the min is 2

		inputParm.thisRawImageSN = rawImgSN

		solutionCenterDiffCriterion = inputParm.solutionCenterDiffCriterion
		solutionOrientationDiffCriterion = inputParm.solutionOrientationDiffCriterion

		#initialize Center And Orientation by ont of the following modes

		if iniCenterOrientationMode == "iniparmfile" :
			inputParm.initializeCenterAndOrientationFromIniParmFile() # need to set "refEulerConvention"
		elif iniCenterOrientationMode == "headerfile" :
			inputParm.initializeCenterAndOrientationFromParticle() # need to set "refEulerConvention"
		else :
			inputParm.initializeCenterAndOrientationFromRandom()  # default is random orientation and physical center

		#set the refence Center And Orientation by ont of the following modes

		if refCenterOrientationMode == "iniparmfile" : inputParm.setRefCenterAndOrientationFromIniParmFile() # need to set "refEulerConvention"
		elif refCenterOrientationMode == "headerfile" : inputParm.setRefCenterAndOrientationFromParticle() # need to set "refEulerConvention"
		else : inputParm.setRefCenterAndOrientationFromInitializedParms() # default is copy the initial center and orientation

		rawImage.crossCommonLineSearchReadRawParticle(inputParm) #create pseudo PFT of raw image

		maxNumOfRun = inputParm.maxNumOfRun
		outputParmList = []
		numOfRun = 0
		passAllConsistencyCriteria = 0
		while (numOfRun < maxNumOfRun) or (len(outputParmList) < 2):

			if (iniCenterOrientationMode != "iniparmfile") and (iniCenterOrientationMode != "headerfile") :
				inputParm.initializeCenterAndOrientationFromRandom()  # default is random orientation and physical center
			if (refCenterOrientationMode != "iniparmfile") and (refCenterOrientationMode != "headerfile") :
				inputParm.setRefCenterAndOrientationFromInitializedParms() # default is copy the initial center and orientation

			numOfRun = numOfRun + 1
			print "numOfRun = ", numOfRun

			############################################################################
			############ execute cross common line search for reference ################
			############################################################################
			outputParm  = rawImage.crossCommonLineSearch(inputParm)
			############################################################################
			# pass criteria check
			outputParmList.append(outputParm) #if passed criteria, e.g. zscore, residualThreshold, etc
			############################################################################

			outputParmList.sort(lambda x, y: cmp(x.residual, y.residual))

			############################################################################
			########################## consistency check ###############################
			############################################################################
			#passConsistencyCriteria = 0
			finalOutputParmList = []
			lowestResidualList = []
			lengthOfList = len(outputParmList)
			if lengthOfList < 2 : continue
			for i in range(lengthOfList-1):
				thisOutputParm = outputParmList[i]
				numOfPairsPassConsistencyCheck = 0
				for j in range(i+1,lengthOfList):
					refOutputParm = outputParmList[j]
					tmpOutputParm = EMAN.ccmlOutputParm() #create a new output parm object
					tmpOutputParm.rawImageSN = thisOutputParm.rawImageSN #copy all paramenters
					tmpOutputParm.residual = thisOutputParm.residual
					tmpOutputParm.sigma = thisOutputParm.sigma
					tmpOutputParm.verbose = thisOutputParm.verbose
					tmpOutputParm.zScore = thisOutputParm.zScore
					tmpOutputParm.zScoreCriterion = thisOutputParm.zScoreCriterion

					tmpOutputParm.passAllCriteria = 0
					tmpOutputParm.setCalculatedCenterAndOrientation(thisOutputParm.cx,thisOutputParm.cy,thisOutputParm.q)
					tmpOutputParm.setRefCenterAndOrientation(refOutputParm.cx, refOutputParm.cy, refOutputParm.q)
					tmpOutputParm.calculateDifferenceWithRefParm() #calculated the difference

					centerDiff = tmpOutputParm.centerDiff
					orientationDiff = tmpOutputParm.orientationDiff
					
					#####  FLIP CASE :  if no consistency found, try flip this orientation
					if ((centerDiff > solutionCenterDiffCriterion) or (orientationDiff > solutionOrientationDiffCriterion)) :
						quatFlip = EMAN.Quaternion(refOutputParm.q.getEuler().alt(), refOutputParm.q.getEuler().az(), refOutputParm.q.getEuler().phi()+pi)
						tmpOutputParm.setRefCenterAndOrientation(refOutputParm.cx, refOutputParm.cy, quatFlip)
						tmpOutputParm.calculateDifferenceWithRefParm() #calculated the difference
						centerDiff = tmpOutputParm.centerDiff
						orientationDiff = tmpOutputParm.orientationDiff
						tmpOutputParm.setRefCenterAndOrientation(refOutputParm.cx, refOutputParm.cy, refOutputParm.q) #set back the exact orientation of reference 

					#Save the configurations with lowest residuals
					if (i<3) and (j==i+1) : lowestResidualList.append(tmpOutputParm)
					
					#make the good/answers list
					if ((centerDiff < solutionCenterDiffCriterion) and (orientationDiff < solutionOrientationDiffCriterion)) :
						numOfPairsPassConsistencyCheck += 1
						if numOfPairsPassConsistencyCheck == 1 : #save to the final list
							tmpOutputParm.passAllCriteria = 1
							finalOutputParmList.append(tmpOutputParm)
						if i==0 and numOfPairsPassConsistencyCheck >= options.numConsistentRun: #if the first one, check whether it has 3 pair of consistencies
							passAllConsistencyCriteria = 1
							break
						if i>0 : break #if not the first one, find one pair of consistency, then break
				
				#no break here, just for saving all possible solutions

			if passAllConsistencyCriteria and len(finalOutputParmList) >= options.numConsistentRun: break #if 3 consistent pair orientations were found, then stop


		rawImage.crossCommonLineSearchReleaseParticle(inputParm) # release the memory related to this raw particle

		# if no consistency found, keep the lowest ones as output
		if len(finalOutputParmList) == 0 : finalOutputParmList = lowestResidualList
		for i in range(len(finalOutputParmList)) : 
			if passAllConsistencyCriteria : finalOutputParmList[i].passAllCriteria = 1
			else : finalOutputParmList[i].passAllCriteria = 0

		if options.solutionFile:
			for i in range(len(finalOutputParmList)) : finalOutputParmList[i].outputResult(solutionFile)

		outputParm = finalOutputParmList[0] #just use the lowest residual as regular output
		if outputParm.passAllCriteria: 	passfail = "pass"
		else: passfail = "fail"

		print "Final result: euler=%g\t%g\t%g\tcenter=%g\t%g\tresidue=%g\t%s" % (outputParm.alt*180/pi, outputParm.az*180/pi, outputParm.phi*180/pi, outputParm.cx, outputParm.cy, outputParm.residual, passfail)
		
		if options.scoreFile:
			rawImage.readImage(rawImages[rawImgSN][0], rawImages[rawImgSN][1], 1) # read header only
			if rawImage.hasCTF(): 
				defocus = rawImage.getCTF()[0]
		else:
			defocus = 0

		solution = (rawImages[rawImgSN][0], rawImages[rawImgSN][1], outputParm.alt, outputParm.az, outputParm.phi, \
					   outputParm.cx, outputParm.cy, defocus, outputParm.residual, outputParm.passAllCriteria)
		solutions.append( solution )

		sys.stdout.flush()

	rawImage.crossCommonLineSearchFinalize(inputParm) #finalize, i.e. delete memories

	if mpi:
		if options.verbose: 
			print "rank %d: done and ready to output" % (mpi.rank)
			sys.stdout.flush()
		mpi.barrier()
		#print "rank %d: %s" % (mpi.rank, solutions)
		if mpi.rank==0:
			for r in range(1,mpi.size):
				msg, status = mpi.recv(source = r, tag = r)
				solutions += msg
			def ptcl_cmp(x, y):
				eq = cmp(x[0], y[0])
				if not eq: return cmp(x[1],y[1])
				else: return eq
			solutions.sort(ptcl_cmp)
		else:
			mpi.send(solutions, 0, tag = mpi.rank)

	if not mpi or (mpi and mpi.rank==0):
		if options.scoreFile:
			sFile = open(options.scoreFile, "w")
			sFile.write("#LST\n")
			for i in solutions:
				if i[-1]: 
					sFile.write("%d\t%s\tdefocus=%g\tresidual=%g\n" % (i[1], i[0], i[7], i[8]))
			sFile.close()
			
		if options.listFile:
			lFile = open(options.listFile, "w")
			lFile.write("#LST\n")
			for i in solutions:
				if i[-1]: 
					lFile.write("%d\t%s\t%g\t%g\t%g\t%g\t%g\n" % (i[1], i[0], i[2]*180.0/pi, i[3]*180.0/pi, i[4]*180.0/pi, i[5], i[6]))
			lFile.close()
		if options.mrcSolutionFile:
			outFile = open(options.mrcSolutionFile, "w")
			for i in solutions:
				if i[-1]:
					#rawImage.readImage(i[0], i[1], 1)
					rawImage.readImage(i[0], i[1])
					thisEu = EMAN.Euler(i[2], i[3], i[4])
					thisEu.convertToMRCAngle()
					alt = thisEu.alt_MRC()*180.0/pi
					az  = thisEu.az_MRC()*180.0/pi
					phi = thisEu.phi_MRC()*180.0/pi
		
					cx  = i[5]
					cy  = i[6]
					dx = cx - rawImage.xSize()/2
					dy = cy - rawImage.ySize()/2
					rawImage.applyMask(rMask1,6,dx,dy,0) #apply mask type 4 [outside=0] to raw image, center will be the solved center
					#tnfFileName = "%s-%d.tnf" % (os.path.basename(os.path.splitext(rawImages[rawImgSN][0])[0]), rawImages[rawImgSN][1])
					prefix = os.path.dirname(options.mrcSolutionFile).replace(" ", "")
					if prefix != "" : prefix = prefix + "/"
					tnfFileName = "%s%s-%d.tnf" % (prefix,os.path.basename(os.path.splitext(i[0])[0]), i[1])
					rawFFT = rawImage.doFFT()
					rawFFT.writeImage(tnfFileName,0)  #tnf file no header information, it is a pure FFT of raw image file
		
					outFile.write("%s\n" % (os.path.abspath(tnfFileName)))
					outFile.write(" %d, %.4f, %.4f, %.4f, %.4f, %.4f, 0.0\n" % (0, alt, az, phi, cy, cx))
			outFile.close()
		if updataHeader:
			for i in solutions:
				rawImage.readImage(i[0], i[1], 1)
				if options.verbose:
					cx  = rawImage.get_center_x()
					cy  = rawImage.get_center_y()
					alt = rawImage.alt()
					az  = rawImage.az()
					phi = rawImage.phi()
					print "Update header: %s %d\t%7.5f  %7.5f  %7.2f  %7.2f  %7.2f => %7.5f  %7.5f  %7.2f  %7.2f  %7.2f" % \
						(i[0], i[1], alt*180.0/pi, az*180.0/pi, phi*180.0/pi, cx, cy, i[2]*180.0/pi, i[3]*180.0/pi, i[4]*180.0/pi, i[5], i[6])
				rawImage.setRAlign(i[2], i[3], i[4])
				rawImage.set_center_x(i[5])
				rawImage.set_center_y(i[6])
				imgtype = EMAN.EMData.ANY
				rawImage.writeImage(i[0], i[1], imgtype, 1)
	if not options.nolog and (not mpi or (mpi and mpi.rank==0)): EMAN.LOGend()
Example #44
0
                       str(HydroConstructor).split("'")[1].split(".")[-1],
                       str(Qconstructor).split("'")[1].split(".")[-1],
                       "nx=%i" % nx1)
restartDir = os.path.join(dataDir, "restarts")
restartBaseName = os.path.join(restartDir, "StandingWave-planar-1d-%i" % nx1)

#-------------------------------------------------------------------------------
# Check if the necessary output directories exist.  If not, create them.
#-------------------------------------------------------------------------------
import os, sys
if mpi.rank == 0:
    if clearDirectories and os.path.exists(dataDir):
        shutil.rmtree(dataDir)
    if not os.path.exists(restartDir):
        os.makedirs(restartDir)
mpi.barrier()

#-------------------------------------------------------------------------------
# Material properties.
#-------------------------------------------------------------------------------
eos = IsothermalEquationOfStateMKS(cs2, mu)

#-------------------------------------------------------------------------------
# Interpolation kernels.
#-------------------------------------------------------------------------------
WT = TableKernel(BSplineKernel(), 10000)
output("WT")

#-------------------------------------------------------------------------------
# Make the NodeList.
#-------------------------------------------------------------------------------
def run():
    mpi.barrier()
    print "run", mpi.rank
    mpi.barrier()
    os.environ["SDL_VIDEO_WINDOW_POS"] = "400,300"
    print "Just after run SDL_VIDEO_WIDOW_POS:", mpi.rank, os.environ["SDL_VIDEO_WINDOW_POS"]

    print "run", mpi.rank
    """
    tileConfig = CreateAMConfig()
    # fqdn = getfqdn()
    hostname = gethostname()
    machineDesc = tileConfig.getMachineDescByHostname(hostname)
 
    tileRects = []
    rects = []
    for tile in machineDesc.tiles:
        # tileConfig.getLocalDrawRect()
        rects.append(tileConfig.getAbsoluteFullDisplayRect(tile.uid))

    print hostname, machineDesc.hostname, rects
    # return
    """
    rects = [Rect(0, 0, 1280, 1024), Rect(1280, 0, 1280, 1024)]
    # rects = [Rect(0,0,1280,1024)]

    os.environ["DISPLAY"] = ":0.0"
    # os.environ['SDL_VIDEO_WINDOW_POS'] = "0,0"
    os.environ["SDL_VIDEO_WINDOW_POS"] = "0,0"
    import pygame

    os.system("export SDL_VIDEO_WINDOW_POS=0,0")

    imageFilename = sys.argv[1]

    # windowWidth = 2560 # 1280# 320
    # windowWidth = 3840 # 1280# 320
    windowWidth = 2560  # 1280# 320
    windowHeight = 1024  # 1024 # 280
    # windowWidth = 1280 # 1280# 320
    # windowHeight = 1024 # 1024 # 280
    app = App(windowWidth, windowHeight)

    renderers = []
    for i in range(len(rects)):
        displayRect = rects[i]
        if i == 0:
            renderer = glRenderer2D(multipleRenderers=True, firstRenderer=True)
            renderer.addFrameSetupObj(ScreenClearer(color=(0.9, 0.4, 0.4), clearDepth=False))
        else:
            renderer = glRenderer2D(multipleRenderers=True)
            renderer.addFrameSetupObj(ScreenClearer(color=(0.4, 0.9, 0.4), clearDepth=False))
        renderers.append(renderer)
        app.addRenderer(renderer)

    print "app.initialize"
    app.initialize(windowBorder=False)
    # app.initialize(windowBorder=True)

    for i in range(len(rects)):
        print "SETTING RECT:", rects[i]
        renderers[i].init(app.width, app.height, viewportRect=rects[i])

    class glBox:
        def __init__(self):
            self.hasDrawFunc = True
            self.hasEraseDrawFunc = True
            self.visible = True

        def update(self, app, secs):
            pass

        def draw(self, renderer):
            # glClearColor(.4, .4, .4, 1.0)
            # glClear(GL_COLOR_BUFFER_BIT)
            glBegin(GL_TRIANGLE_STRIP)
            glVertex2f(100, 200)
            glVertex2f(100, 100)
            glVertex2f(200, 200)
            glVertex2f(200, 100)
            glEnd()

    class glTxtrBox:
        def __init__(self, imageFilename="../data/test1.png", initialOffset=(0, 0)):
            self.hasDrawFunc = True
            self.hasEraseDrawFunc = True
            self.visible = True
            # self.texture = Texture( "../data/test1.png")
            self.easyTexture = EasyTexture(imageFilename, blend=False)
            # self.easyTexture.width = renderer.width * 0.8
            # self.easyTexture.height = renderer.height * 0.8
            self.easyTexture.width = renderer.width
            self.easyTexture.height = renderer.height
            self.useDrawPixel = False  # For comparison debugging
            self.useDrawPixel = False  # For comparison debugging
            self.xvel = 0.1
            self.yvel = 0.1
            self.easyTexture.zoom(2, 2)
            self.easyTexture.setOffset(initialOffset[0], initialOffset[1])

        def update(self, secs, app):
            self.easyTexture.pan(self.xvel * secs, self.yvel * secs)

        def draw(self, renderer):
            # glClearColor(.8, .8, .8, 1.0)
            # glClear(GL_COLOR_BUFFER_BIT)
            # glPixelStoref(GL_UNPACK_ALIGNMENT, 1)

            # self.texture.blit( Vec2(10,10), Rect(10,10, 30,30), (app.width, app.height) )
            # self.texture.blit( Vec2(50,50), Rect(0,0, 64,64), (app.width, app.height) )
            # self.texture.blit( Vec2(150,50), Rect(40,40, 64,64), (app.width, app.height) , blend=True)
            # self.texture.blit( Vec2(0.1 * renderer.width,0.1 * renderer.height), Rect(0,0, 30,30), (app.width, app.height) )
            self.easyTexture.draw()

    # box = glBox()
    # app.addDynamicObject(box)
    glEnable(GL_TEXTURE_2D)
    for i in range(len(renderers)):
        rect = rects[i]
        box = glTxtrBox(imageFilename, initialOffset=(rect.x, rect.y))
        app.addDynamicObject(box, addToRenderer=False)
        renderers[i].addDynamicObject(box)

    app.drawBounds = 0

    print "Running app"
    try:
        app.run()
    except:
        traceback.print_exc()