{ "cells": [ { "cell_type": "code", "execution_count": 64, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import struct\n", "import matplotlib.pyplot as plt\n", "import math\n", "from py_rmatools import rma\n", "\n", "%matplotlib qt\n", "plt.rcParams.update({'figure.max_open_warning': 0})" ] }, { "cell_type": "code", "execution_count": 65, "metadata": { "collapsed": true }, "outputs": [], "source": [ "meshFilename = 'ck_034.rm1'\n", "channelWidth = 100\n", "RMAfilename = 'CK011_WQ'\n", "\n", "#If RMA11\n", "constNum = [1]" ] }, { "cell_type": "code", "execution_count": 66, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def isElementOneD(nodelist):\n", " if len(nodelist) == 2:\n", " return True\n", " return False\n", "\n", "def isElementSquare(nodelist):\n", " if len(nodelist) == 4:\n", " return True\n", " return False\n", "\n", "def square2Triangle(ElementNum):\n", " nodelist = ElementDict[ElementNum]\n", " if isElementSquare(nodelist):\n", " ElementDict[ElementNum] = [nodelist[0], nodelist[1], nodelist[2]]\n", " ElementList.append(max(ElementList) + 1)\n", " ElementDict[ElementList[-1]]= [nodelist[0], nodelist[2], nodelist[3]]\n", " \n", "def oneD2triangle(ElementNum):\n", " if isElementOneD(ElementDict[ElementNum]):\n", " nAe = ElementDict[ElementNum][0] #nAe Node A existing\n", " nBe = ElementDict[ElementNum][1]\n", " \n", " if not nAe in node1Dduplicate: node1Dduplicate[nAe] = []\n", " if not nBe in node1Dduplicate: node1Dduplicate[nBe] = []\n", " \n", " xA = nodeDict[nAe][0]\n", " xB = nodeDict[nBe][0]\n", " yA = nodeDict[nAe][1]\n", " yB = nodeDict[nBe][1]\n", " \n", " normalVec = [-(yB - yA),(xB - xA)]\n", " dist = math.sqrt(normalVec[0]**2 + normalVec[1]**2)\n", " normalVec[0] = normalVec[0] / dist\n", " normalVec[1] = normalVec[1] / dist\n", " xA2 = xA + channelWidth * normalVec[0]\n", " xB2 = xB + channelWidth * normalVec[0]\n", " yA2 = yA + channelWidth * normalVec[1]\n", " yB2 = yB + channelWidth * normalVec[1]\n", " \n", "\n", " nA = max(NodeList) + 1\n", " nB = max(NodeList) + 2\n", " \n", " node1Dduplicate[nAe].append(nA)\n", " node1Dduplicate[nBe].append(nB) \n", " \n", " node2nodevalue[nA] = nAe\n", " node2nodevalue[nB] = nBe\n", " \n", " \n", " NodeList.append(nA)\n", " NodeList.append(nB)\n", " nodeDict[nA] = [xA2, yA2, -1.01]\n", " nodeDict[nB] = [xB2, yB2, -1.01]\n", " \n", " newEle = max(ElementList) + 1\n", " ElementList .append(newEle)\n", " ElementDict[ElementNum] = [nAe, nA, nBe]\n", " ElementDict[newEle] = [nA, nB, nBe]\n", " \n", "def RMA11toSerafin():\n", " f = open('{}.slf'.format(RMAfilename), 'wb')\n", "\n", " f.write(struct.pack(\">l\",80))\n", " str='{0: >80}'.format('SERAFIN ')\n", " f.write(str.encode('ascii'))\n", " f.write(struct.pack(\">l\",80))\n", "\n", " f.write(struct.pack(\">l\",8))\n", " f.write(struct.pack(\">l\",len(constNum)))\n", " f.write(struct.pack(\">l\",0))\n", " f.write(struct.pack(\">l\",8))\n", "\n", " for c in constName:\n", " f.write(struct.pack(\">l\",32))\n", " str='{0: <32}'.format(c)\n", " f.write(str.encode('ascii'))\n", " f.write(struct.pack(\">l\",32))\n", "\n", "\n", " f.write(struct.pack(\">l\",40))\n", " f.write(struct.pack(\">l\",1))\n", " f.write(struct.pack(\">l\",0))\n", " f.write(struct.pack(\">l\",0))\n", " f.write(struct.pack(\">l\",0))\n", " f.write(struct.pack(\">l\",0))\n", " f.write(struct.pack(\">l\",0))\n", " f.write(struct.pack(\">l\",0))\n", " f.write(struct.pack(\">l\",0))\n", " f.write(struct.pack(\">l\",0))\n", " f.write(struct.pack(\">l\",1))\n", " f.write(struct.pack(\">l\",40))\n", "\n", " f.write(struct.pack(\">l\",24))\n", " f.write(struct.pack(\">l\",R.year))\n", " f.write(struct.pack(\">l\",1))\n", " f.write(struct.pack(\">l\",1))\n", " f.write(struct.pack(\">l\",0))\n", " f.write(struct.pack(\">l\",0))\n", " f.write(struct.pack(\">l\",0))\n", " f.write(struct.pack(\">l\",24))\n", "\n", " f.write(struct.pack(\">l\",16))\n", " f.write(struct.pack(\">l\",len(ElementList)))\n", " f.write(struct.pack(\">l\",len(NodeList)))\n", " f.write(struct.pack(\">l\",3))\n", " f.write(struct.pack(\">l\",1))\n", " f.write(struct.pack(\">l\",16))\n", "\n", "\n", " f.write(struct.pack(\">l\",len(ElementList)*3*4))\n", " for el in ElementList:\n", " for nd in ElementDict[el]:\n", " f.write(struct.pack(\">l\",nodeOrdered[nd])) \n", " f.write(struct.pack(\">l\",len(ElementList)*3*4))\n", "\n", " f.write(struct.pack(\">l\",len(NodeList)))\n", " for i in range(0,len(NodeList)):\n", " f.write(struct.pack(\">l\",0))\n", " f.write(struct.pack(\">l\",len(NodeList)))\n", "\n", " f.write(struct.pack(\">l\",len(NodeList)*4))\n", " for key, value in nodeDict.items():\n", " f.write(struct.pack(\">f\",value[0]))\n", " f.write(struct.pack(\">l\",len(NodeList)*4))\n", "\n", " f.write(struct.pack(\">l\",len(NodeList)*4))\n", " for key, value in nodeDict.items():\n", " f.write(struct.pack(\">f\",value[1]))\n", " f.write(struct.pack(\">l\",len(NodeList)*4))\n", "\n", "\n", "\n", " while R.next():\n", " #for i in range(3):\n", " f.write(struct.pack(\">l\",4))\n", " f.write(struct.pack(\">f\",R.time * 3600))\n", " f.write(struct.pack(\">l\",4))\n", "\n", " for c in constNum:\n", " f.write(struct.pack(\">l\",len(NodeList)*4))\n", " for key, value in nodeDict.items():\n", " if key in node2nodevalue.keys():\n", " f.write(struct.pack(\">f\",R.constit[c][node2nodevalue[key]]))\n", " else:\n", " f.write(struct.pack(\">f\",R.constit[c][key]))\n", "\n", " f.write(struct.pack(\">l\",len(NodeList)*4))\n", "\n", " R.next()\n", "\n", " f.close() \n", "\n", " \n", " \n", "def RMA2toSerafin():\n", " f = open('{}.slf'.format(RMAfilename), 'wb')\n", "\n", " f.write(struct.pack(\">l\",80))\n", " str='{0: >80}'.format('SERAFIN ')\n", " f.write(str.encode('ascii'))\n", " f.write(struct.pack(\">l\",80))\n", "\n", " f.write(struct.pack(\">l\",8))\n", " f.write(struct.pack(\">l\",len(constName)))\n", " f.write(struct.pack(\">l\",0))\n", " f.write(struct.pack(\">l\",8))\n", "\n", " for c in constName:\n", " f.write(struct.pack(\">l\",32))\n", " str='{0: <32}'.format(c)\n", " f.write(str.encode('ascii'))\n", " f.write(struct.pack(\">l\",32))\n", "\n", "\n", " f.write(struct.pack(\">l\",40))\n", " f.write(struct.pack(\">l\",1))\n", " f.write(struct.pack(\">l\",0))\n", " f.write(struct.pack(\">l\",0))\n", " f.write(struct.pack(\">l\",0))\n", " f.write(struct.pack(\">l\",0))\n", " f.write(struct.pack(\">l\",0))\n", " f.write(struct.pack(\">l\",0))\n", " f.write(struct.pack(\">l\",0))\n", " f.write(struct.pack(\">l\",0))\n", " f.write(struct.pack(\">l\",1))\n", " f.write(struct.pack(\">l\",40))\n", "\n", " f.write(struct.pack(\">l\",24))\n", " f.write(struct.pack(\">l\",R.year))\n", " f.write(struct.pack(\">l\",1))\n", " f.write(struct.pack(\">l\",1))\n", " f.write(struct.pack(\">l\",0))\n", " f.write(struct.pack(\">l\",0))\n", " f.write(struct.pack(\">l\",0))\n", " f.write(struct.pack(\">l\",24))\n", "\n", " f.write(struct.pack(\">l\",16))\n", " f.write(struct.pack(\">l\",len(ElementList)))\n", " f.write(struct.pack(\">l\",len(NodeList)))\n", " f.write(struct.pack(\">l\",3))\n", " f.write(struct.pack(\">l\",1))\n", " f.write(struct.pack(\">l\",16))\n", "\n", "\n", " f.write(struct.pack(\">l\",len(ElementList)*3*4))\n", " for el in ElementList:\n", " for nd in ElementDict[el]:\n", " f.write(struct.pack(\">l\",nodeOrdered[nd]))\n", " f.write(struct.pack(\">l\",len(ElementList)*3*4))\n", "\n", " f.write(struct.pack(\">l\",len(NodeList)))\n", " for i in range(0,len(NodeList)):\n", " f.write(struct.pack(\">l\",0))\n", " f.write(struct.pack(\">l\",len(NodeList)))\n", "\n", " f.write(struct.pack(\">l\",len(NodeList)*4))\n", " for key, value in nodeDict.items():\n", " f.write(struct.pack(\">f\",value[0]))\n", " f.write(struct.pack(\">l\",len(NodeList)*4))\n", "\n", " f.write(struct.pack(\">l\",len(NodeList)*4))\n", " for key, value in nodeDict.items():\n", " f.write(struct.pack(\">f\",value[1]))\n", " f.write(struct.pack(\">l\",len(NodeList)*4))\n", "\n", "\n", "\n", " while R.next():\n", "\n", " f.write(struct.pack(\">l\",4))\n", " f.write(struct.pack(\">f\",R.time * 3600))\n", " f.write(struct.pack(\">l\",4))\n", "\n", "\n", " f.write(struct.pack(\">l\",len(NodeList)*4))\n", " for key, value in nodeDict.items():\n", " if key in node2nodevalue.keys():\n", " f.write(struct.pack(\">f\",R.xvel[node2nodevalue[key]]))\n", " else:\n", " f.write(struct.pack(\">f\",R.xvel[key]))\n", " f.write(struct.pack(\">l\",len(NodeList)*4))\n", "\n", " f.write(struct.pack(\">l\",len(NodeList)*4))\n", " for key, value in nodeDict.items():\n", " if key in node2nodevalue.keys():\n", " f.write(struct.pack(\">f\",R.yvel[node2nodevalue[key]]))\n", " else:\n", " f.write(struct.pack(\">f\",R.yvel[key]))\n", " f.write(struct.pack(\">l\",len(NodeList)*4))\n", " \n", " f.write(struct.pack(\">l\",len(NodeList)*4))\n", " for key, value in nodeDict.items():\n", " if key in node2nodevalue.keys():\n", " f.write(struct.pack(\">f\",R.depth[node2nodevalue[key]]))\n", " else:\n", " f.write(struct.pack(\">f\",R.depth[key]))\n", " f.write(struct.pack(\">l\",len(NodeList)*4))\n", " \n", " f.write(struct.pack(\">l\",len(NodeList)*4))\n", " for key, value in nodeDict.items():\n", " if key in node2nodevalue.keys():\n", " f.write(struct.pack(\">f\",R.elevation[node2nodevalue[key]]))\n", " else:\n", " f.write(struct.pack(\">f\",R.elevation[key]))\n", " f.write(struct.pack(\">l\",len(NodeList)*4))\n", " \n", " \n", " R.next()\n", "\n", " f.close() " ] }, { "cell_type": "code", "execution_count": 67, "metadata": { "collapsed": false }, "outputs": [], "source": [ "#Read mesh file and extract node (except mid node) and elements - plus convert 1D element to 2D for vizualisation\n", "NodeList = [] \n", "ElementList = []\n", "ElementDict = {}\n", "nodeDict = {}\n", "node1Dduplicate = {} #Original Number: List of Duplicates\n", "node2nodevalue = {} #link between the node number and the node value to use \n", " #(e.g. if node 10 is a 1D node: 10 is not duplicate so {1:1}, \n", " #but node 2050 (duplicate of 10) (1D to 2D) the value of the duplicated \n", " #node will be the same as the original so we might have {2050: 10})\n", " \n", "with open(meshFilename) as f:\n", " line = f.readline()\n", " line = f.readline()\n", " line = f.readline()\n", " line = f.readline()\n", " \n", " cpt = 1\n", " while line and line != ' 9999\\n':\n", " temp = line.split()\n", " ElementDict[int(temp[0])] = [int(temp[i]) for i in range(1,9,2) if temp[i] != '0' and int(temp[9]) < 100]\n", " ElementList.append(int(temp[0]))\n", " line = f.readline()\n", " \n", " for key, value in ElementDict.items():\n", " NodeList.extend(value)\n", " \n", " NodeList = list(set(NodeList))\n", "\n", " line = f.readline()\n", " while line and line != ' 9999\\n':\n", " temp = line.split()\n", " if int(temp[0]) in NodeList:\n", " nodeDict[int(temp[0])] = [float(temp[1]),float(temp[2]),float(temp[3])]\n", " line = f.readline()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 75, "metadata": { "collapsed": false }, "outputs": [], "source": [ "for e in ElementList:\n", " oneD2triangle(e)\n", " square2Triangle(e)\n", " \n", "for key in list(ElementDict): #Remove Special Element 902.....\n", " if len(ElementDict[key]) != 3:\n", " print(key, ElementDict[key])\n", " ElementDict.pop(key)\n", " ElementList.remove(key)\n", " \n", "nodeOrdered = {}\n", "cpt = 1\n", "for key, value in nodeDict.items():\n", " nodeOrdered[key] = cpt\n", " cpt +=1" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Open and Read First Step of the RMA File and Save a Serafin" ] }, { "cell_type": "code", "execution_count": 72, "metadata": { "collapsed": false }, "outputs": [], "source": [ "R=rma()\n", "R.open(RMAfilename)\n", "R.next()\n", "\n", "if R.type==b'RMA11 ':\n", " constName = []\n", " for c in constNum:\n", " constName.append(R.constit_name[c].decode(\"utf-8\"))\n", " RMA11toSerafin()\n", " \n", "if R.type==b'RMA2 ':\n", " constName = ['X-VEL','Y-VEL','DEPTH','FREE SURFACE'] \n", " RMA2toSerafin()\n", " \n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [] }, { "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 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.0" } }, "nbformat": 4, "nbformat_minor": 2 }