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
def writeMasterSiloFile(ndim, nblock, jsplit, baseDirectory, baseName, procDir, materials, vars, label, time, cycle): nullOpts = silo.DBoptlist() # Decide which domains have information. if len(vars[0][0]) > 0: myvote = mpi.rank + 1 else: myvote = 0 maxproc = mpi.allreduce(myvote, mpi.MAX) assert maxproc <= mpi.procs # Pattern for constructing per domain variables. domainNamePatterns = [ os.path.join(procDir, "domain%i.silo:%%s" % i) for i in xrange(maxproc) ] # We need each domains nblock info. nblocks = [nblock] for sendproc in xrange(1, maxproc): if mpi.rank == sendproc: mpi.send(nblock, dest=0, tag=50) if mpi.rank == 0: nblocks.append(mpi.recv(source=sendproc, tag=50)[0]) # Create the master file. if mpi.rank == 0: fileName = os.path.join(baseDirectory, baseName + ".silo") f = silo.DBCreate(fileName, silo.DB_CLOBBER, silo.DB_LOCAL, label, silo.DB_HDF5) nullOpts = silo.DBoptlist() # Write the domain file names and types. domainNames = Spheral.vector_of_string( [p % "hblk0/hydro_mesh" for p in domainNamePatterns]) meshTypes = Spheral.vector_of_int([silo.DB_QUADMESH] * maxproc) optlist = silo.DBoptlist(1024) assert optlist.addOption(silo.DBOPT_CYCLE, cycle) == 0 assert optlist.addOption(silo.DBOPT_DTIME, time) == 0 assert silo.DBPutMultimesh(f, "hydro_mesh", domainNames, meshTypes, optlist) == 0 # Write material names. if materials: material_names = Spheral.vector_of_string( [p % "/hblk0/Materials" for p in domainNamePatterns]) matnames = Spheral.vector_of_string( ["void"] + [x.name for x in materials]) matnos = Spheral.vector_of_int(range(len(materials) + 1)) assert len(material_names) == maxproc assert len(matnames) == len(materials) + 1 assert len(matnos) == len(materials) + 1 optlist = silo.DBoptlist(1024) assert optlist.addOption(silo.DBOPT_CYCLE, cycle) == 0 assert optlist.addOption(silo.DBOPT_DTIME, time) == 0 assert optlist.addOption(silo.DBOPT_MMESH_NAME, "hydro_mesh") == 0 assert optlist.addOption(silo.DBOPT_MATNAMES, silo.DBOPT_NMATNOS, matnames) == 0 assert optlist.addOption(silo.DBOPT_MATNOS, silo.DBOPT_NMATNOS, matnos) == 0 assert silo.DBPutMultimat(f, "Materials", material_names, optlist) == 0 # Write the variables descriptors. types = Spheral.vector_of_int([silo.DB_QUADVAR] * maxproc) for var, varname in vars: domainVarNames = Spheral.vector_of_string() for iproc, p in enumerate(domainNamePatterns): domainVarNames.append(p % ("/hblk0/" + varname)) assert len(domainVarNames) == maxproc optlistMV = silo.DBoptlist() assert optlistMV.addOption(silo.DBOPT_CYCLE, cycle) == 0 assert optlistMV.addOption(silo.DBOPT_DTIME, time) == 0 #assert optlistMV.addOption(silo.DBOPT_TENSOR_RANK, silo.DB_VARTYPE_SCALAR) == 0 assert optlistMV.addOption(silo.DBOPT_BLOCKORIGIN, 0) == 0 assert optlistMV.addOption(silo.DBOPT_MMESH_NAME, "hydro_mesh") == 0 assert silo.DBPutMultivar(f, varname, domainVarNames, types, optlistMV) == 0 # Write the dummy variable "akap_0" to fool Hades into thinking we're actually Hydra or something. assert silo.DBPutQuadvar1( f, "akap_0", "hydro_mesh", Spheral.vector_of_double([0.0] * (ndim * ndim)), Spheral.vector_of_double(), silo.DB_ZONECENT, Spheral.vector_of_int([ndim] * ndim), nullOpts) == 0 # Write domain and mesh size info. assert silo.DBMkDir(f, "Decomposition") == 0 assert silo.DBWrite(f, "Decomposition/NumDomains", maxproc) == 0 assert silo.DBWrite(f, "Decomposition/NumLocalDomains", maxproc) == 0 assert silo.DBWrite(f, "Decomposition/NumBlocks", 1) == 0 #assert silo.DBWrite(f, "Decomposition/LocalName", "hblk") == 0 localDomains = Spheral.vector_of_int(range(maxproc)) domainFiles = Spheral.vector_of_vector_of_int( [Spheral.vector_of_int(range(maxproc))]) assert silo.DBWrite(f, "Decomposition/LocalDomains", localDomains) == 0 assert silo.DBWrite(f, "DomainFiles", domainFiles) == 0 for iproc in xrange(maxproc): assert silo.DBMkDir(f, "Decomposition/gmap%i" % iproc) == 0 stuff = Spheral.vector_of_int([0] * 12) for jdim in xrange(ndim): stuff[6 + jdim] = nblocks[iproc][jdim] if iproc in (0, maxproc - 1): assert silo.DBWrite( f, "Decomposition/gmap%i/NumNeighbors" % iproc, 1) == 0 else: assert silo.DBWrite( f, "Decomposition/gmap%i/NumNeighbors" % iproc, 2) == 0 assert silo.DBWrite(f, "Decomposition/gmap%i/gmap" % iproc, stuff) == 0 # Close the file. if mpi.rank == 0: assert silo.DBClose(f) == 0 del f return maxproc
def writeDomainSiloFile(ndim, baseDirectory, baseName, procDirBaseName, nodeLists, label, time, cycle, dumpGhosts, fieldwad): # How many points are we dumping? if dumpGhosts: ntot = sum([n.numNodes for n in nodeLists]) else: ntot = sum([n.numInternalNodes for n in nodeLists]) if ntot: # Create the file. fileName = os.path.join(baseDirectory, procDirBaseName % mpi.rank, baseName + ".silo") db = silo.DBCreate(fileName, silo.DB_CLOBBER, silo.DB_LOCAL, label, silo.DB_HDF5) nullOpts = silo.DBoptlist() # Read the per material info. coords = [[] for i in xrange(ndim)] for nodes in nodeLists: if dumpGhosts: pos = nodes.positions().allValues() else: pos = nodes.positions().internalValues() n = len(pos) for j in xrange(ndim): for i in xrange(n): coords[j].append(pos[i][j]) for j in xrange(ndim): assert len(coords[j]) == ntot coords = vector_of_vector_of_double( [vector_of_double(icoords) for icoords in coords]) # Write the Pointmesh. meshOpts = silo.DBoptlist(1024) assert meshOpts.addOption(silo.DBOPT_CYCLE, cycle) == 0 assert meshOpts.addOption(silo.DBOPT_DTIME, time) == 0 assert silo.DBPutPointmesh(db, "mesh", coords, meshOpts) == 0 # Write materials. matnos = [i for i in xrange(len(nodeLists))] assert len(matnos) == len(nodeLists) matlist = [] matnames = [] for (nodeList, imat) in zip(nodeLists, xrange(len(nodeLists))): if dumpGhosts: matlist += [imat] * nodeList.numNodes else: matlist += [imat] * nodeList.numInternalNodes matnames.append(nodeList.name) matlist = vector_of_int(matlist) matnames = vector_of_string(matnames) assert len(matlist) == ntot assert len(matnames) == len(nodeLists) 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, vector_of_string(matnames)) == 0 vecInt = vector_of_int() vecDouble = vector_of_double() assert silo.DBPutMaterial(db, "material", "mesh", vector_of_int(matnos), matlist, vecInt, vecInt, vecInt, vecInt, vecDouble, matOpts) == 0 # Write the variable descriptions for non-scalar variables (vector and tensors). writeDefvars(db, fieldwad) # Write the field components. varOpts = silo.DBoptlist(1024) assert varOpts.addOption(silo.DBOPT_CYCLE, cycle) == 0 assert varOpts.addOption(silo.DBOPT_DTIME, time) == 0 for name, desc, dtype, optlistDef, optlistMV, optlistVar, subvars in fieldwad: for subname, vals in subvars: if len(vals) > 0: if type(vals[0]) == float: ctor = vector_of_double else: ctor = vector_of_int assert silo.DBPutPointvar1(db, subname, "mesh", ctor(vals), varOpts) == 0 # That's it. assert silo.DBClose(db) == 0 del db return
def writeMasterSiloFile(ndim, baseDirectory, baseName, procDirBaseName, nodeLists, label, time, cycle, dumpGhosts, fieldwad): nullOpts = silo.DBoptlist() # Make sure which domains have information ntot = [] if dumpGhosts: nloc = sum([n.numNodes for n in nodeLists]) else: nloc = sum([n.numInternalNodes for n in nodeLists]) for iproc in xrange(mpi.procs): ntot.append(mpi.bcast(nloc, iproc)) # Pattern for constructing per domain variables. domainNamePatterns = [ os.path.join(procDirBaseName % i, baseName + ".silo:%s") for i in xrange(mpi.procs) if ntot[i] > 0 ] ndoms = len(domainNamePatterns) # Create the master file. if mpi.rank == 0: fileName = os.path.join(baseDirectory, baseName + ".silo") db = silo.DBCreate(fileName, silo.DB_CLOBBER, silo.DB_LOCAL, label, silo.DB_HDF5) # Write the domain file names and types. domainNames = vector_of_string() meshTypes = vector_of_int([silo.DB_POINTMESH] * ndoms) for p in domainNamePatterns: domainNames.append(p % "mesh") optlist = silo.DBoptlist(1024) assert optlist.addOption(silo.DBOPT_CYCLE, cycle) == 0 assert optlist.addOption(silo.DBOPT_DTIME, time) == 0 assert silo.DBPutMultimesh(db, "MESH", domainNames, meshTypes, optlist) == 0 # Extract the material names, and write per material info if any. if len(nodeLists) > 0: # Write material names. if mpi.rank == 0: materialNames = [x.name for x in nodeLists] material_names = [] matnames = [] matnos = [] for p in domainNamePatterns: material_names.append(p % "material") for (name, i) in zip(materialNames, range(len(materialNames))): matnames.append(name) matnos.append(i) assert len(material_names) == ndoms assert len(matnames) == len(nodeLists) assert len(matnos) == len(nodeLists) optlist = silo.DBoptlist(1024) assert optlist.addOption(silo.DBOPT_CYCLE, cycle) == 0 assert optlist.addOption(silo.DBOPT_DTIME, time) == 0 assert optlist.addOption(silo.DBOPT_MATNAMES, silo.DBOPT_NMATNOS, vector_of_string(matnames)) == 0 assert optlist.addOption(silo.DBOPT_MATNOS, silo.DBOPT_NMATNOS, vector_of_int(matnos)) == 0 assert silo.DBPutMultimat(db, "MATERIAL", vector_of_string(material_names), optlist) == 0 # Write the variable descriptions for non-scalar variables (vector and tensors). writeDefvars(db, fieldwad) # Write the variables descriptors. types = vector_of_int([silo.DB_POINTVAR] * ndoms) for name, desc, type, optlistDef, optlistMV, optlistVar, subvars in fieldwad: domainVarNames = vector_of_string() nlocalvals = sum([len(x[1]) for x in subvars]) if desc is None: for iproc, p in enumerate(domainNamePatterns): nvals = mpi.bcast(nlocalvals, root=iproc) if nvals > 0: domainVarNames.append(p % name) else: domainVarNames.append("EMPTY") assert len(domainVarNames) == ndoms if mpi.rank == 0: assert silo.DBPutMultivar(db, name, domainVarNames, types, optlistMV) == 0 else: for subname, vals in subvars: domainVarNames = vector_of_string() for iproc, p in enumerate(domainNamePatterns): nvals = mpi.bcast(nlocalvals, root=iproc) if nvals > 0: domainVarNames.append(p % subname) else: domainVarNames.append("EMPTY") assert len(domainVarNames) == ndoms if mpi.rank == 0: assert silo.DBPutMultivar(db, subname, domainVarNames, types, optlistVar) == 0 # That's it. if mpi.rank == 0: assert silo.DBClose(db) == 0 del db return
def writeDomainMeshSiloFile(dirName, mesh, index2zone, label, nodeLists, time, cycle, fieldwad, pretendRZ, nodeArrays, zoneArrays, faceArrays, meshType = silo.DB_UCDMESH): # Is there anything to do? numZones = len(mesh.cells) if numZones > 0: # Create the file. fileName = os.path.join(dirName, "domain%i.silo" % mpi.rank) db = silo.DBCreate(fileName, silo.DB_CLOBBER, silo.DB_LOCAL, label, silo.DB_HDF5) nullOpts = silo.DBoptlist() # Make directories for variables. assert silo.DBMkDir(db, "CELLS") == 0 assert silo.DBMkDir(db, "POINTS") == 0 # HACK # Determine our dimensionality if isinstance(mesh, polytope.Tessellation2d): nDim = 2 else: assert isinstance(mesh, polytope.Tessellation3d) nDim = 3 # Write a Polygonal zone list. zonelistName = { 2 : "zonelist", 3 : "PHzonelist" } # start = TIME.clock() if nDim == 2: # Read out the zone nodes. We rely on these already being arranged # counter-clockwise. zoneNodes = mesh.zoneNodes assert len(zoneNodes) == numZones assert silo.DBPutZonelist2(db, zonelistName[nDim], nDim, zoneNodes, 0, 0, vector_of_int([silo.DB_ZONETYPE_POLYGON]*numZones), vector_of_int([len(zn) for zn in zoneNodes]), # shapesize, vector_of_int([1]*numZones), nullOpts) == 0 # Write a Polyhedral zone list. if nDim == 3: # Construct the zone-face list. We use the ones complement of a face ID # to indicate that face needs to be reversed in reference to this zone. # This is the same convention as polytope, so just copy it. assert silo.DBPutPHZonelist(db, zonelistName[nDim], mesh.facesAsInts, mesh.cells, 0, (numZones - 1), nullOpts) == 0 # print " --> %g sec to write PHzonelist" % (TIME.clock() - start) # start = TIME.clock() # Construct the mesh node coordinates. assert len(mesh.nodes) % nDim == 0 if nDim == 2: coords = vector_of_vector_of_double([mesh.xnodes, mesh.ynodes]) else: coords = vector_of_vector_of_double([mesh.xnodes, mesh.ynodes, mesh.znodes]) assert len(coords) == nDim # print " --> %g sec to compute coords" % (TIME.clock() - start) # start = TIME.clock() # Write the mesh itself. meshOpts = silo.DBoptlist(1024) assert meshOpts.addOption(silo.DBOPT_CYCLE, cycle) == 0 assert meshOpts.addOption(silo.DBOPT_DTIME, time) == 0 assert meshOpts.addOption(silo.DBOPT_COORDSYS, silo.DB_CARTESIAN) == 0 assert meshOpts.addOption(silo.DBOPT_NSPACE, nDim) == 0 assert meshOpts.addOption(silo.DBOPT_TV_CONNECTIVITY, 1) == 0 if nDim == 2: if pretendRZ: assert meshOpts.addOption(silo.DBOPT_COORDSYS, silo.DB_CYLINDRICAL) == 0 assert meshOpts.addOption(silo.DBOPT_XLABEL, "z") == 0 assert meshOpts.addOption(silo.DBOPT_YLABEL, "r") == 0 assert silo.DBPutUcdmesh(db, "MESH", coords, numZones, zonelistName[nDim], "NULL", meshOpts) == 0 else: assert meshOpts.addOption(silo.DBOPT_PHZONELIST, zonelistName[nDim]) == 0 assert silo.DBPutUcdmesh(db, "MESH", coords, numZones, "NULL", "NULL", meshOpts) == 0 # print " --> %g sec to write mesh" % (TIME.clock() - start) # start = TIME.clock() # Write materials. if nodeLists: matnos = vector_of_int(range(len(nodeLists))) matnames = vector_of_string([nodeList.name for nodeList in nodeLists]) matlist = [] offset = 0 for imat, nodeList in enumerate(nodeLists): for i in xrange(nodeList.numInternalNodes): for j in index2zone[offset + i]: matlist.append(imat) offset += nodeList.numInternalNodes matlist = vector_of_int(matlist) assert len(matlist) == numZones assert len(matnames) == len(nodeLists) assert len(matnos) == len(nodeLists) 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(db, "MATERIAL", "MESH", matnos, vector_of_int(matlist), vector_of_int(), vector_of_int(), vector_of_int(), vector_of_int(), vector_of_double(), matOpts) == 0 assert silo.DBPutMaterial(db, "PointMATERIAL", "PointMESH", matnos, vector_of_int(matlist), vector_of_int(), vector_of_int(), vector_of_int(), vector_of_int(), vector_of_double(), matOpts) == 0 # print " --> %g sec to DBPutMaterial" % (TIME.clock() - start) # start = TIME.clock() # Write the variable descriptions for non-scalar variables (vector and tensors). #writeDefvars(db, fieldwad) # Write the field components. varOpts = silo.DBoptlist(1024) assert varOpts.addOption(silo.DBOPT_CYCLE, cycle) == 0 assert varOpts.addOption(silo.DBOPT_DTIME, time) == 0 for name, desc, vtype, optlistDef, optlistMV, optlistVar, subvars in fieldwad: for subname, vals in subvars: if len(vals) > 0: if type(vals[0]) == float: ctor = vector_of_double else: ctor = vector_of_int assert silo.DBPutUcdvar1(db, "CELLS_" + subname, "MESH", ctor(vals), ctor([]), silo.DB_ZONECENT, varOpts) == 0 # print " --> %g sec to DBPutUcdvar1" % (TIME.clock() - start) # start = TIME.clock() # # HACK: Write the field components on the point mesh as well. Remove when the vardef version is working. # varOpts = silo.DBoptlist(1024) # assert varOpts.addOption(silo.DBOPT_CYCLE, cycle) == 0 # assert varOpts.addOption(silo.DBOPT_DTIME, time) == 0 # for name, desc, vtype, optlistDef, optlistMV, optlistVar, subvars in fieldwad: # for subname, vals in subvars: # if len(vals) > 0: # if type(vals[0]) == float: # ctor = vector_of_double # else: # ctor = vector_of_int # assert silo.DBPutPointvar1(db, "POINTS_" + subname, "PointMESH", ctor(vals), varOpts) == 0 # print " --> %g sec to DBPutPointvar1" % (TIME.clock() - start) # start = TIME.clock() # print " --> %g sec to write materials" % (TIME.clock() - start) # start = TIME.clock() # Write the set of neighbor domains. thpt = vector_of_vector_of_int() thpt.append(vector_of_int([len(mesh.neighborDomains)])) thpt.append(vector_of_int([x for x in mesh.neighborDomains])) elemNames = vector_of_string(["num neighbor domains", "neighbor domains"]) assert silo.DBPutCompoundarray(db, "DOMAIN_NEIGHBOR_NUMS", elemNames, thpt, nullOpts) == 0 # Write the shared nodes for each neighbor domain. sharedNodes = mesh.sharedNodes for ineighborDomain in xrange(len(mesh.neighborDomains)): nodes = [list(sharedNodes[ineighborDomain])] assert len(nodes) == len(sharedNodes[ineighborDomain]) assert silo.DBPutCompoundarray(db, "DOMAIN_NEIGHBOR%i" % ineighborDomain, ["shared_nodes"], nodes, nullOpts) == 0 # If requested, write out annotations for the nodes, zones, and faces. if (not (nodeArrays is None) or not (zoneArrays is None) or not (faceArrays is None)): names = [] values = [] if not (nodeArrays is None): for pair in nodeArrays: assert len(pair) == 2 if len(pair[1]) > 0: names.append(pair[0] + "_node") values.append([]) for i in pair[1]: values[-1].append(i) assert len(values[-1]) == len(pair[1]) if not (zoneArrays is None): for pair in zoneArrays: assert len(pair) == 2 if len(pair[1]) > 0: names.append(pair[0] + "_zone") values.append([]) for i in pair[1]: values[-1].append(i) assert len(values[-1]) == len(pair[1]) if not (faceArrays is None): for pair in faceArrays: assert len(pair) == 2 if len(pair[1]) > 0: names.append(pair[0] + "_face") values.append([]) for i in pair[1]: values[-1].append(i) assert len(values[-1]) == len(pair[1]) assert len(names) == len(values) if len(names) > 0: assert silo.DBPutCompoundarray(db, "ANNOTATION_INT", names, values, nullOpts) == 0 # Write the point mesh. if nodeLists: ntot = sum([n.numInternalNodes for n in nodeLists]) coords = [] for j in xrange(nDim): coords.append([]) for nodes in nodeLists: pos = nodes.positions().internalValues() n = len(pos) for j in xrange(nDim): for i in xrange(n): coords[j].append(pos[i][j]) for j in xrange(nDim): assert len(coords[j]) == ntot coords = vector_of_vector_of_double([vector_of_double(vals) for vals in coords]) # Write the Pointmesh. meshOpts = silo.DBoptlist(1024) assert meshOpts.addOption(silo.DBOPT_CYCLE, cycle) == 0 assert meshOpts.addOption(silo.DBOPT_DTIME, time) == 0 assert silo.DBPutPointmesh(db, "PointMESH", coords, meshOpts) == 0 # That's it. assert silo.DBClose(db) == 0 mpi.barrier() return
def writeMasterMeshSiloFile(dirName, mesh, label, nodeLists, time, cycle, fieldwad, meshType = silo.DB_UCDMESH): # Figure out who has something to write. Silo can't handle empty variables... numZonesPerDomain = mpi.allreduce([(mpi.rank, len(mesh.cells))], mpi.SUM) numZonesPerDomain.sort() numZonesPerDomain = [x[1] for x in numZonesPerDomain] # Only processor 0 actually writes the file. linkfile = None if mpi.rank == 0: nullOpts = silo.DBoptlist() # Create the master file. p0, p1 = os.path.split(dirName) fileName = os.path.join(dirName, "OvlTop.silo") db = silo.DBCreate(fileName, silo.DB_CLOBBER, silo.DB_LOCAL, label, silo.DB_HDF5) # Make directories for variables. assert silo.DBMkDir(db, "CELLS") == 0 assert silo.DBMkDir(db, "POINTS") == 0 # HACK # Pattern for constructing per domain variables. domainNamePatterns = [("%s/domain%i.silo:" % (p1, i)) + "%s" for i in xrange(mpi.procs) if numZonesPerDomain[i] > 0] numDomains = len(domainNamePatterns) # Write the domain file names and types. domainNames = vector_of_string([p % "MESH" for p in domainNamePatterns]) meshTypes = vector_of_int([meshType]*numDomains) optlist = silo.DBoptlist(1024) assert optlist.addOption(silo.DBOPT_CYCLE, cycle) == 0 assert optlist.addOption(silo.DBOPT_DTIME, time) == 0 assert silo.DBPutMultimesh(db, "MMESH", domainNames, meshTypes, optlist) == 0 # Also write out the points as a point mesh. if nodeLists and mpi.rank == 0: domainNames = vector_of_string([p % "MESH" for p in domainNamePatterns]) meshTypes = vector_of_int([silo.DB_POINTMESH]*numDomains) optlist = silo.DBoptlist(1024) assert optlist.addOption(silo.DBOPT_CYCLE, cycle) == 0 assert optlist.addOption(silo.DBOPT_DTIME, time) == 0 assert silo.DBPutMultimesh(db, "MPointMESH", domainNames, meshTypes, optlist) == 0 # Extract the material names, and write per material info if any. if nodeLists: # Write material names (MESH) matnames = vector_of_string([x.name for x in nodeLists]) matnos = vector_of_int(range(len(matnames))) material_names = vector_of_string([p % "MATERIAL" for p in domainNamePatterns]) assert len(material_names) == numDomains assert len(matnames) == len(nodeLists) assert len(matnos) == len(nodeLists) optlist = silo.DBoptlist(1024) assert optlist.addOption(silo.DBOPT_CYCLE, cycle) == 0 assert optlist.addOption(silo.DBOPT_DTIME, time) == 0 assert optlist.addOption(silo.DBOPT_MMESH_NAME, "MMESH") == 0 assert optlist.addOption(silo.DBOPT_MATNAMES, silo.DBOPT_NMATNOS, matnames) == 0 assert optlist.addOption(silo.DBOPT_MATNOS, silo.DBOPT_NMATNOS, matnos) == 0 assert silo.DBPutMultimat(db, "MMATERIAL", material_names, optlist) == 0 # Write material names (PointMESH) material_names = vector_of_string([p % "PointMATERIAL" for p in domainNamePatterns]) assert len(material_names) == numDomains assert len(matnames) == len(nodeLists) assert len(matnos) == len(nodeLists) optlist = silo.DBoptlist(1024) assert optlist.addOption(silo.DBOPT_CYCLE, cycle) == 0 assert optlist.addOption(silo.DBOPT_DTIME, time) == 0 assert optlist.addOption(silo.DBOPT_MMESH_NAME, "MPointMESH") == 0 assert optlist.addOption(silo.DBOPT_MATNAMES, silo.DBOPT_NMATNOS, matnames) == 0 assert optlist.addOption(silo.DBOPT_MATNOS, silo.DBOPT_NMATNOS, matnos) == 0 assert silo.DBPutMultimat(db, "MPointMATERIAL", material_names, optlist) == 0 # Write the variable descriptions for non-scalar variables (vector and tensors). writeDefvars(db, fieldwad) # Write the attributes for each variable. # This is largely an Overlink thing, which only needs the components of types like vectors. if len(fieldwad) > 0: attrNames = vector_of_string() attrs = vector_of_vector_of_int() thpt = vector_of_int([OverlinkAttrs["ATTR_ZONAL"], OverlinkAttrs["ATTR_INTENSIVE"], OverlinkAttrs["ATTR_SECOND_ORDER"], 0, OverlinkAttrs["ATTR_FLOAT"]]) for name, desc, type, optlistDef, optlistMV, optlistVar, subvars in fieldwad: for subname, vals in subvars: attrNames.append(subname) attrs.append(thpt) assert len(attrNames) == len(attrs) silo.DBPutCompoundarray(db, "VAR_ATTRIBUTES", attrNames, attrs, nullOpts) # Write the variables descriptors. ucdTypes = vector_of_int([silo.DB_UCDVAR]*numDomains) for name, desc, type, optlistDef, optlistMV, optlistVar, subvars in fieldwad: domainVarNames = vector_of_string([p % ("CELLS_" + name) for p in domainNamePatterns]) assert len(domainVarNames) == numDomains assert silo.DBPutMultivar(db, "CELLS/" + name, domainVarNames, ucdTypes, optlistMV) == 0 if desc != None: for subname, vals in subvars: domainVarNames = vector_of_string([p % ("CELLS_" + subname) for p in domainNamePatterns]) assert len(domainVarNames) == numDomains assert silo.DBPutMultivar(db, "CELLS/" + subname, domainVarNames, ucdTypes, optlistVar) == 0 # # HACK: repeating for point values -- remove when vardefs work # # Write the variables descriptors. # ptTypes = [silo.DB_POINTVAR]*numDomains # for name, desc, type, optlistDef, optlistMV, optlistVar, subvars in fieldwad: # domainVarNames = [] # for p in domainNamePatterns: # domainVarNames.append(p % ("POINTS_" + name)) # assert len(domainVarNames) == numDomains # assert silo.DBPutMultivar(db, "POINTS/" + name, domainVarNames, ptTypes, optlistMV) == 0 # if desc != None: # for subname, vals in subvars: # domainVarNames = [] # for p in domainNamePatterns: # domainVarNames.append(p % ("POINTS_" + subname)) # assert len(domainVarNames) == numDomains # assert silo.DBPutMultivar(db, "POINTS/" + subname, domainVarNames, ptTypes, optlistVar) == 0 # Make a convenient symlink for the master file. linkfile = p1 + ".silo" fullpath = os.path.join(p0, linkfile) if os.path.exists(fullpath): os.remove(fullpath) targetfile = os.path.join(p1, "OvlTop.silo") os.symlink(targetfile, os.path.join(p0, linkfile)) # That's it. assert silo.DBClose(db) == 0 # Everyone gets the link file name. linkfile = mpi.bcast(linkfile, root=0) mpi.barrier() return linkfile