{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "This is a test for the python interface to particle local coordinates. \n", "\n", "We generate a regular mesh with swarm, then deform the mesh, then try and re-set the particles within the mesh via the local coordintes." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/javascript": [ "\n", " require.config({baseUrl: 'html',\n", " paths: {'control': [\"control\"],\n", " 'ok': [\"OK-min\"],\n", " 'gl-matrix': [\"gl-matrix-min\"],\n", " 'drawbox': [\"drawbox\"]\n", " }\n", " }\n", " );\n", "\n", " require([\"control\", \"ok\", \"gl-matrix\", \"drawbox\"], function() {\n", " console.log(\"Loaded scripts\");\n", " \n", " return {};\n", " });\n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import underworld as uw\n", "import glucifer\n", "import numpy as np\n", "import math" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# giordani mesh \n", "minCoord = [-1.,-1.]\n", "maxCoord = [ 1., 1.]\n", "mesh = uw.mesh.FeMesh_Cartesian( elementRes=(16,16), minCoord=minCoord, maxCoord=maxCoord)\n", "def circleMesh(mesh):\n", " with mesh.deform_mesh():\n", " for vert in mesh.data[:]:\n", " radius = np.max(np.abs(vert))\n", " angle = np.arctan2(vert[1],vert[0])\n", " vert[0] = radius*math.cos(angle)\n", " vert[1] = radius*math.sin(angle)\n", "\n", "# check.. note we need to use a lower tolerance for the q1 mesh because it always \n", "# under-integrates due to the mesh 'cutting corners'\n", "swarm = uw.swarm.Swarm(mesh)\n", "svar = swarm.add_variable('int',1)\n", "swarm.populate_using_layout(uw.swarm.layouts.GlobalSpaceFillerLayout(swarm,20))\n", "svar.data[:]=0\n", "\n", "for index,coord in enumerate(swarm.particleCoordinates.data):\n", " if (abs(coord[0])>0.4) or (abs(coord[1])>0.4):\n", " if (abs(coord[0])<0.5) and (abs(coord[1])<0.5):\n", " svar.data[index]=1\n", "\n", "\n", "meshfig = glucifer.Figure(figsize=(600,600))\n", "meshfig.append( glucifer.objects.Mesh(mesh) )\n", "meshfig.append( glucifer.objects.Points(swarm, svar, pointSize=5) )\n", "# lets also create the PICswarm\n", "picswarm = uw.swarm.VoronoiIntegrationSwarm(swarm)\n", "picswarm.repopulate()\n", "\n", "meshfig.show()" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "circleMesh(mesh)\n", "meshfig.show()" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def max_radius(partCoords):\n", " coord2 = np.inner(partCoords,partCoords)\n", " return math.sqrt(coord2.max())" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [], "source": [ "if not np.isclose( max_radius(swarm.particleCoordinates.data), math.sqrt(2), rtol=1e-2 ):\n", " raise RuntimeError(\"Particles should have a max radious of sqrt(2) at this point.\")" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# now lets move particles\n", "with swarm.deform_swarm():\n", " swarm.particleCoordinates.data[:] = uw.function.input().evaluate(picswarm)\n", "meshfig.show()" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [], "source": [ "if not np.isclose( max_radius(swarm.particleCoordinates.data), math.sqrt(1), rtol=1e-2 ):\n", " raise RuntimeError(\"Particles should have a max radious of sqrt(1) at this point.\")" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [], "source": [ "velfield = mesh.add_variable(2)\n", "presfield = mesh.subMesh.add_variable(1)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": true }, "outputs": [], "source": [ "allwalls = mesh.specialSets['AllWalls_VertexSet']\n", "bc = uw.conditions.DirichletCondition(velfield,indexSetsPerDof=(allwalls,allwalls))\n", "density = swarm.add_variable('double',1)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": true }, "outputs": [], "source": [ "sys = uw.systems.Stokes(velfield,presfield,1.,(0.,-density), conditions=bc)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": true }, "outputs": [], "source": [ "density.data[:] = 0.\n", "for index,coord in enumerate(swarm.particleCoordinates.data):\n", " if (coord[1]>0.) and (coord[0]>0.):\n", " density.data[index] = 1." ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": true }, "outputs": [], "source": [ "fig = glucifer.Figure()\n" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "fig.append(glucifer.objects.Points(swarm,density,pointSize=2.))\n", "fig.show()" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false }, "outputs": [], "source": [ "solver = uw.systems.Solver(sys)\n", "solver.solve()" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "fig = glucifer.Figure()\n", "fig.append(glucifer.objects.Points(swarm,uw.function.math.dot(velfield,velfield),pointSize=4.))\n", "fig.show()" ] }, { "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.10" } }, "nbformat": 4, "nbformat_minor": 0 }