def updateMesh(handler, jsonMesh, rename = False): if "undeletable" not in jsonMesh: jsonMesh["undeletable"] = False # This basically says to prefer the userId in the input if 'user_id' in jsonMesh: userId = jsonMesh['user_id'] else: userId = handler.user.user_id() if "id" in jsonMesh: meshDb = MeshWrapper.get_by_id(jsonMesh["id"]) name = jsonMesh["name"] else: # Make sure name isn't taken, or build one that isn't taken if "name" in jsonMesh: name = jsonMesh["name"] usedNames = set([x.name for x in db.Query(MeshWrapper).filter('user_id =', userId).run()]) if name in usedNames: if rename: i = 1 name = '{0}_{1}'.format(jsonMesh["name"], i) while name in usedNames: i = i + 1 name = '{0}_{1}'.format(jsonMesh["name"], i) else: raise Exception("Name is required on new meshes") meshDb = MeshWrapper() meshDb.user_id = userId meshDb.name = name meshDb.description = jsonMesh["description"] meshDb.meshFileId = jsonMesh["meshFileId"] meshDb.subdomains = jsonMesh["subdomains"] meshDb.uniqueSubdomains = jsonMesh["uniqueSubdomains"] meshDb.undeletable = jsonMesh["undeletable"] pymodel = pyurdme.URDMEModel(name = 'test') meshFileObj = fileserver.FileManager.getFile(handler, meshDb.meshFileId) pymodel.mesh = pyurdme.URDMEMesh.read_dolfin_mesh(str(meshFileObj["storePath"])) coordinates = pymodel.mesh.coordinates() minx = numpy.min(coordinates[:, 0]) maxx = numpy.max(coordinates[:, 0]) miny = numpy.min(coordinates[:, 1]) maxy = numpy.max(coordinates[:, 1]) minz = numpy.min(coordinates[:, 2]) maxz = numpy.max(coordinates[:, 2]) pymodel.add_species(pyurdme.Species('T', 1)) if len(meshDb.subdomains) == 0: meshDb.subdomains = [1] * len(coordinates) meshDb.uniqueSubdomains = [1] pymodel.set_subdomain_vector(numpy.array(meshDb.subdomains)) sd = pymodel.get_subdomain_vector() vol_accumulator = numpy.zeros(numpy.unique(sd).shape) for ndx, v in enumerate(pymodel.get_solver_datastructure()['vol']): vol_accumulator[sd[ndx] - 1] += v volumes = {} for s, v in enumerate(vol_accumulator): volumes[s + 1] = v meshDb.volumes = volumes meshDb.boundingBox = [[minx, maxx], [miny, maxy], [minz, maxz]] return meshDb.put().id()
def updateMesh(handler, jsonMesh, rename=False): if "undeletable" not in jsonMesh: jsonMesh["undeletable"] = False # This basically says to prefer the userId in the input if 'user_id' in jsonMesh: userId = jsonMesh['user_id'] else: userId = handler.user.user_id() if "id" in jsonMesh: meshDb = MeshWrapper.get_by_id(jsonMesh["id"]) name = jsonMesh["name"] else: # Make sure name isn't taken, or build one that isn't taken if "name" in jsonMesh: name = jsonMesh["name"] usedNames = set([ x.name for x in db.Query(MeshWrapper).filter( 'user_id =', userId).run() ]) if name in usedNames: if rename: i = 1 name = '{0}_{1}'.format(jsonMesh["name"], i) while name in usedNames: i = i + 1 name = '{0}_{1}'.format(jsonMesh["name"], i) else: raise Exception("Name is required on new meshes") meshDb = MeshWrapper() meshDb.user_id = userId meshDb.name = name meshDb.description = jsonMesh["description"] meshDb.meshFileId = jsonMesh["meshFileId"] meshDb.subdomains = jsonMesh["subdomains"] meshDb.uniqueSubdomains = jsonMesh["uniqueSubdomains"] meshDb.undeletable = jsonMesh["undeletable"] pymodel = pyurdme.URDMEModel(name='test') meshFileObj = fileserver.FileManager.getFile(handler, meshDb.meshFileId) pymodel.mesh = pyurdme.URDMEMesh.read_dolfin_mesh( str(meshFileObj["storePath"])) coordinates = pymodel.mesh.coordinates() minx = numpy.min(coordinates[:, 0]) maxx = numpy.max(coordinates[:, 0]) miny = numpy.min(coordinates[:, 1]) maxy = numpy.max(coordinates[:, 1]) minz = numpy.min(coordinates[:, 2]) maxz = numpy.max(coordinates[:, 2]) pymodel.add_species(pyurdme.Species('T', 1)) if len(meshDb.subdomains) == 0: meshDb.subdomains = [1] * len(coordinates) meshDb.uniqueSubdomains = [1] pymodel.set_subdomain_vector(numpy.array(meshDb.subdomains)) sd = pymodel.get_subdomain_vector() vol_accumulator = numpy.zeros(numpy.unique(sd).shape) for ndx, v in enumerate(pymodel.get_solver_datastructure()['vol']): vol_accumulator[sd[ndx] - 1] += v volumes = {} for s, v in enumerate(vol_accumulator): volumes[s + 1] = v meshDb.volumes = volumes meshDb.boundingBox = [[minx, maxx], [miny, maxy], [minz, maxz]] return meshDb.put().id()
def setupMeshes(handler): try: base = os.path.dirname(os.path.realpath(__file__)) + '/../static/spatial/' files = [ 'coli_with_membrane_mesh.xml', 'cylinder_mesh.xml', 'unit_cube_with_membrane_mesh.xml', 'unit_sphere_with_membrane_mesh.xml' ] descriptions = { 'coli_with_membrane_mesh.xml' : 'Simplified E-coli model mesh', 'cylinder_mesh.xml' : 'Cylindrical domain', 'unit_cube_with_membrane_mesh.xml' : 'Cubic domain of edge length 1.0', 'unit_sphere_with_membrane_mesh.xml' : 'Spherical domain with radius 1.0' } namesToFilenames = { 'E-coli with membrane' : 'coli_with_membrane_mesh.xml', 'Cylinder' : 'cylinder_mesh.xml', 'Unit cube' : 'unit_cube_with_membrane_mesh.xml', 'Unit sphere' : 'unit_sphere_with_membrane_mesh.xml' } converted = set() for wrapper in db.GqlQuery("SELECT * FROM MeshWrapper WHERE user_id = :1", handler.user.user_id()).run(): converted.add(wrapper.name) for name in set(namesToFilenames.keys()) - converted: fileName = namesToFilenames[name] meshDb = MeshWrapper() # To get the subdomains, there is a .txt file stored along with every .xml baseName, ext = os.path.splitext(fileName) subdomainsFile = open(os.path.join(base, baseName + '.txt'), 'r') subdomains = [] for line in subdomainsFile.read().split(): v, s = line.strip().split(',') v = int(v) s = int(float(s)) subdomains.append((v, s)) subdomainsFile.close() subdomains = [y for x, y in sorted(subdomains, key = lambda x : x[0])] uniqueSubdomains = list(set(subdomains)) meshFile = open(os.path.join(base, fileName), 'r') meshFileId = fileserver.FileManager.createFile(handler, "meshFiles", fileName, meshFile.read(), 777) meshFile.close() meshDb.user_id = handler.user.user_id() meshDb.name = name meshDb.description = descriptions[fileName] meshDb.meshFileId = int(meshFileId) meshDb.subdomains = subdomains meshDb.uniqueSubdomains = uniqueSubdomains meshDb.undeletable = True pymodel = pyurdme.URDMEModel(name = 'test') pymodel.mesh = pyurdme.URDMEMesh.read_dolfin_mesh(str(os.path.join(base, fileName))) coordinates = pymodel.mesh.coordinates() minx = numpy.min(coordinates[:, 0]) maxx = numpy.max(coordinates[:, 0]) miny = numpy.min(coordinates[:, 1]) maxy = numpy.max(coordinates[:, 1]) minz = numpy.min(coordinates[:, 2]) maxz = numpy.max(coordinates[:, 2]) pymodel.add_species(pyurdme.Species('T', 1)) pymodel.set_subdomain_vector(numpy.array(subdomains)) sd = pymodel.get_subdomain_vector() vol_accumulator = numpy.zeros(numpy.unique(sd).shape) for ndx, v in enumerate(pymodel.get_solver_datastructure()['vol']): vol_accumulator[sd[ndx] - 1] += v volumes = {} for s, v in enumerate(vol_accumulator): volumes[s + 1] = v meshDb.volumes = volumes meshDb.boundingBox = [[minx, maxx], [miny, maxy], [minz, maxz]] meshDb.put() except: traceback.print_exc() print "ERROR: Failed to import example public models"
def setupMeshes(handler): try: base = os.path.dirname( os.path.realpath(__file__)) + '/../static/spatial/' files = [ 'coli_with_membrane_mesh.xml', 'cylinder_mesh.xml', 'unit_cube_with_membrane_mesh.xml', 'unit_sphere_with_membrane_mesh.xml' ] descriptions = { 'coli_with_membrane_mesh.xml': 'Simplified E-coli model mesh', 'cylinder_mesh.xml': 'Cylindrical domain', 'unit_cube_with_membrane_mesh.xml': 'Cubic domain of edge length 1.0', 'unit_sphere_with_membrane_mesh.xml': 'Spherical domain with radius 1.0' } namesToFilenames = { 'E-coli with membrane': 'coli_with_membrane_mesh.xml', 'Cylinder': 'cylinder_mesh.xml', 'Unit cube': 'unit_cube_with_membrane_mesh.xml', 'Unit sphere': 'unit_sphere_with_membrane_mesh.xml' } converted = set() for wrapper in db.GqlQuery( "SELECT * FROM MeshWrapper WHERE user_id = :1", handler.user.user_id()).run(): converted.add(wrapper.name) for name in set(namesToFilenames.keys()) - converted: fileName = namesToFilenames[name] meshDb = MeshWrapper() # To get the subdomains, there is a .txt file stored along with every .xml baseName, ext = os.path.splitext(fileName) subdomainsFile = open(os.path.join(base, baseName + '.txt'), 'r') subdomains = [] for line in subdomainsFile.read().split(): v, s = line.strip().split(',') v = int(v) s = int(float(s)) subdomains.append((v, s)) subdomainsFile.close() subdomains = [y for x, y in sorted(subdomains, key=lambda x: x[0])] uniqueSubdomains = list(set(subdomains)) meshFile = open(os.path.join(base, fileName), 'r') meshFileId = fileserver.FileManager.createFile( handler, "meshFiles", fileName, meshFile.read(), 777) meshFile.close() meshDb.user_id = handler.user.user_id() meshDb.name = name meshDb.description = descriptions[fileName] meshDb.meshFileId = int(meshFileId) meshDb.subdomains = subdomains meshDb.uniqueSubdomains = uniqueSubdomains meshDb.undeletable = True pymodel = pyurdme.URDMEModel(name='test') pymodel.mesh = pyurdme.URDMEMesh.read_dolfin_mesh( str(os.path.join(base, fileName))) coordinates = pymodel.mesh.coordinates() minx = numpy.min(coordinates[:, 0]) maxx = numpy.max(coordinates[:, 0]) miny = numpy.min(coordinates[:, 1]) maxy = numpy.max(coordinates[:, 1]) minz = numpy.min(coordinates[:, 2]) maxz = numpy.max(coordinates[:, 2]) pymodel.add_species(pyurdme.Species('T', 1)) pymodel.set_subdomain_vector(numpy.array(subdomains)) sd = pymodel.get_subdomain_vector() vol_accumulator = numpy.zeros(numpy.unique(sd).shape) for ndx, v in enumerate(pymodel.get_solver_datastructure()['vol']): vol_accumulator[sd[ndx] - 1] += v volumes = {} for s, v in enumerate(vol_accumulator): volumes[s + 1] = v meshDb.volumes = volumes meshDb.boundingBox = [[minx, maxx], [miny, maxy], [minz, maxz]] meshDb.put() except: traceback.print_exc() print "ERROR: Failed to import example public models"