diff --git a/Example_Mesh_Creation.ipynb b/Example_Mesh_Creation.ipynb new file mode 100644 index 0000000..565d446 --- /dev/null +++ b/Example_Mesh_Creation.ipynb @@ -0,0 +1,269 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Notes on Spatial Information for StochSS\n", + "\n", + "When setting up a spatial problem in StochSS information about the geometry needs to be provided through two files: a\n", + "file containing the mesh information in DOLFIN XML format and a text file containing information about subdomains. The\n", + "file with subdomain information is optional in general (if it is not provided the entire mesh will belong to one subdomain).\n", + "Below is a tutorial on how to provide that information in a variety of cases.\n", + "\n", + "## Mesh Library \n", + "\n", + "The first and simpilest scenario is using one of the default builtĀ­in meshes provided by StochSS in the Mesh Library.\n", + "These include simple example mesh and subdomain combinations that are common in biological modeling. For example\n", + "there is a unit sphere with a membrane (i.e. the volume is one subdomain and the surface is another), a unit cube witha\n", + "membrane, a cylinder with the two ends being different subdomains among others. If for a given problem these are not\n", + "sufficient then mesh and subdomain information can be provided by attaching files. One simple case where creating and\n", + "attaching a file may be necessary could involve using the DOLFIN built in meshes but with different subdomains than\n", + "provided in the Mesh Library. This can be done as follows.\n", + "## Using DOLFIN Built-In Geometry with Subdomains Outside of Mesh\n", + "Library\n", + "For example consider a sphere with three different subdomains: the top half of the membrane, the bottom half of the\n", + "membrane and the volume. This can be done using pyurdme as follows (pyurdme and FEniCS are software packages\n", + "that are automatically installed during the StochSS installation and DOLFIN is a library within FEniCS).\n", + "First the appropriate libraries need to be imported:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "FILENAME = \"sphere_top_bot_mesh.xml\"\n", + "SD_FILENAME = \"sphere_top_botsubdomains.txt\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "import dolfin\n", + "import mshr\n", + "import os\n", + "import pyurdme" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "class top_half_membrane(dolfin.SubDomain):\n", + " def inside(self,x,on_boundary):\n", + " return on_boundary and x[2]>=0\n", + "class bottom_half_membrane(dolfin.SubDomain):\n", + " def inside(self,x,on_boundary):\n", + " return on_boundary and x[2]<=0" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "class sphere_top_bot(pyurdme.URDMEModel):\n", + " def __init__(self,model_name=\"polarization\"):\n", + " pyurdme.URDMEModel.__init__(self,model_name)\n", + " #DefineGeometry\n", + " sphere=mshr.Sphere(dolfin.Point(0.0,0.0,0.0),1.0)\n", + " self.mesh=pyurdme.URDMEMesh(mesh=mshr.generate_mesh(sphere,10))\n", + " cell_function=dolfin.CellFunction(\"size_t\",self.mesh)\n", + "\n", + " self.add_subdomain(top_half_membrane(),2)\n", + " self.add_subdomain(bottom_half_membrane(),3)\n", + " #Defineinitialpopulations\n", + " self.set_initial_condition_scatter({},[1])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "model=sphere_top_bot()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "sd=model.get_subdomain_vector()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "#This is a dolfin xml file with mesh information\n", + "dolfin.File(FILENAME) << model.mesh" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "import base64\n", + "import IPython.display\n", + "with open(FILENAME) as fd:\n", + " b64 = base64.b64encode(fd.read())\n", + " payload = b64.decode()\n", + " html = '

{title}

'\n", + " html = html.format(payload=payload,title=\"Click here to Download Mesh\",filename=FILENAME)\n", + "IPython.display.HTML(html)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "#This is the file to capture information about subdomains\n", + "with open(SD_FILENAME,'w') as sfd:\n", + " for ndx,val in enumerate(sd):\n", + " sfd.write(\"{0},{1}\\n\".format(ndx,val))\n", + "import base64\n", + "import IPython.display\n", + "with open(SD_FILENAME) as fd:\n", + " b64 = base64.b64encode(fd.read())\n", + " payload = b64.decode()\n", + " html = '

{title}

'\n", + " html = html.format(payload=payload,title=\"Click here to Download SD file\",filename=SD_FILENAME)\n", + "IPython.display.HTML(html)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "# IF you want to visualize your mesh, execute the following two cells" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Code to setup javascript display libraries\n", + "import os.path\n", + "import IPython.display\n", + "with open(os.path.join(os.path.dirname(pyurdme.__file__),'data/three.js_templates/js/three.js')) as fd:\n", + " bufa = fd.read()\n", + "with open(os.path.join(os.path.dirname(pyurdme.__file__),'data/three.js_templates/js/render.js')) as fd:\n", + " bufb = fd.read()\n", + "with open(os.path.join(os.path.dirname(pyurdme.__file__),'data/three.js_templates/js/OrbitControls.js')) as fd:\n", + " bufc = fd.read()\n", + "IPython.display.display(IPython.display.HTML(''))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "model.mesh" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 2", + "language": "python", + "name": "python2" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 2 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython2", + "version": "2.7.6" + } + }, + "nbformat": 4, + "nbformat_minor": 1 +}