mesh parsing example

This commit is contained in:
Jukka Aho
2015-05-29 23:47:24 +03:00
parent bc5ccf84a3
commit f22f59c70e
@@ -0,0 +1,191 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's collect here examples how to parse mesh files (i.e. topologies; nodes and connectivity) from different kind of sources."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"f = open(\"../testdata/tripod.GiD.msh\");\n",
"data = readlines(f)\n",
"close(f)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"Dict{Any,Any} with 0 entries"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type Node\n",
" node_id\n",
" coords\n",
"end\n",
"type Element\n",
" element_id\n",
" node_ids\n",
"end\n",
"nodes = Dict()\n",
"elements = Dict()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Entering section Coordinates\n",
"Leaving section Coordinates\n",
"Entering section Elements\n",
"Leaving section Elements\n",
"Mesh parsed\n"
]
}
],
"source": [
"section = \"\"\n",
"for line in data\n",
" line = strip(line)\n",
" if line == \"Coordinates\" || line == \"Elements\"\n",
" section = line\n",
" println(\"Entering section \",section)\n",
" end\n",
" if beginswith(line, \"end\")\n",
" println(\"Leaving section \",section)\n",
" section = \"\"\n",
" end\n",
" if section == \"Coordinates\"\n",
" m = matchall(r\"[-0-9.]+\", line)\n",
" if length(m) == 0\n",
" continue\n",
" end\n",
" id = integer(m[1])\n",
" coords = float(m[2:end])\n",
" nodes[id] = Node(id, coords)\n",
" end\n",
" if section == \"Elements\"\n",
" #println(line)\n",
" m = matchall(r\"[0-9]+\", line)\n",
" #println(m)\n",
" if length(m) == 0\n",
" continue\n",
" end\n",
" id = integer(m[1])\n",
" #println(line)\n",
" #println(m)\n",
" #print(id)\n",
" node_ids = m[2:end]\n",
" #println(id,node_ids)\n",
" elements[id] = Element(id, map(integer, node_ids))\n",
" end\n",
"end\n",
"println(\"Mesh parsed\")"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"10-element Array{Int64,1}:\n",
" 1411\n",
" 1527\n",
" 1338\n",
" 1581\n",
" 1467\n",
" 1431\n",
" 1375\n",
" 1494\n",
" 1557\n",
" 1455"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"elements[100].node_ids"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"10-element Array{Node,1}:\n",
" Node(1411,[1.55541,4.93615,12.9066])\n",
" Node(1527,[3.55077,4.78495,12.5057])\n",
" Node(1338,[1.26995,2.70013,12.9378])\n",
" Node(1581,[2.09046,5.60655,10.2598])\n",
" Node(1467,[2.56484,4.86055,12.7445])\n",
" Node(1431,[2.42483,3.74254,12.7719])\n",
" Node(1375,[1.41281,3.81814,12.923]) \n",
" Node(1494,[1.82293,5.27135,11.5832])\n",
" Node(1557,[2.82062,5.19575,11.3827])\n",
" Node(1455,[1.68021,4.15334,11.5988])"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"map(id -> nodes[id], elements[100].node_ids)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Julia 0.3.8",
"language": "julia",
"name": "julia-0.3"
},
"language_info": {
"name": "julia",
"version": "0.3.8"
}
},
"nbformat": 4,
"nbformat_minor": 0
}