From e4988fd150e9d3b3363387a0f3f0cc5b0ba93792 Mon Sep 17 00:00:00 2001 From: Stephen Mardson McQuay Date: Wed, 6 Oct 2010 12:15:09 -0600 Subject: [PATCH] implementing a 2D mesh object for gmsh files --- bin/parse_gmsh.py | 122 +++++++++++++++ bin/plot.py | 12 ++ data/gmsh.geo | 10 ++ data/gmsh.msh | 362 +++++++++++++++++++++++++++++++++++++++++++++ test/baker.test.py | 2 +- 5 files changed, 507 insertions(+), 1 deletion(-) create mode 100755 bin/parse_gmsh.py create mode 100644 bin/plot.py create mode 100644 data/gmsh.geo create mode 100644 data/gmsh.msh diff --git a/bin/parse_gmsh.py b/bin/parse_gmsh.py new file mode 100755 index 0000000..af1b93b --- /dev/null +++ b/bin/parse_gmsh.py @@ -0,0 +1,122 @@ +#!/usr/bin/env python + +import sys +import pickle +from pudb import set_trace +from itertools import combinations + +THREE_NODE_TRIANGLE = 2 + +class point(object): + def __init__(self, index, x, y, z): + self.index = index + self.x = x + self.y = y + self.z = z + + self.points = [] + self.faces = [] + + def __getitem__(self, i): + if isinstance(i, int): + if i == 0: + return self.x + elif i == 1: + return self.y + elif i == 2: + return self.z + else: + raise IndexError("there are only (x,y,z)") + elif isinstance(i, str): + if i == 'x': + return self.x + elif i == 'y': + return self.y + elif i == 'z': + return self.z + else: + raise IndexError + else: + raise TypeError("only p.x, p[0], and p['x'] access allowed") + + def __str__(self): + return '(%f, %f, %f)' % (self.x, self.y, self.z) + __repr__ = __str__ + +class face(object): + def __init__(self, index): + self.index = index + self.neighbors = [] + +if __name__ == '__main__': + if len(sys.argv) != 2: + print >> sys.stderr, "usage: %s " % sys.argv[0] + sys.exit(1) + + gmsh_file = open(sys.argv[1], 'r') + + + gmsh_file.readline() # $MeshFormat + format = gmsh_file.readline() + gmsh_file.readline() # $EndMeshFormat + + gmsh_file.readline() # $Nodes + + node_count = int(gmsh_file.readline()) + + points = [] + for i in xrange(node_count): + cur_line = gmsh_file.readline() + (index, x,y,z) = cur_line.split() + index = int(index) - 1 + x = float(x) + y = float(y) + z = float(z) + + points.append(point(index, x,y,z)) + + gmsh_file.readline() # $EndNodes + gmsh_file.readline() # $Elements + + element_count = int(gmsh_file.readline()) + + faces = {} + faceobjs = {} + faces_for_point = {} + neighbors = {} + + for i in xrange(element_count): + cur_line = gmsh_file.readline() + cur_line = cur_line.split() + cur_face_index, type, rest = (int(cur_line[0]), + int(cur_line[1]), + [int(j) for j in cur_line[2:]]) + + if(type == THREE_NODE_TRIANGLE): + points_for_cur_face = [i-1 for i in rest[rest[0]+1:]] + + cur_face = face(cur_face_index) + + for cur_point in points_for_cur_face: + points[cur_point].faces.append(cur_face_index) + + faces[cur_face_index] = points_for_cur_face + cur_face.points = points_for_cur_face + + faceobjs[cur_face_index] = cur_face + edges = [tuple(sorted(i)) for i in combinations(points_for_cur_face, 2)] + + # edge is two points + for edge in edges: + if edge in neighbors: + neighbors[edge].append(cur_face_index) + else: + neighbors[edge] = [cur_face_index] + + for k,v in neighbors.iteritems(): + if len(v) > 1: + faceobjs[v[0]].neighbors.append(v[1]) + faceobjs[v[1]].neighbors.append(v[0]) + + pickle.dump([(p[0], p[1], p[2]) for p in points], open('/tmp/points.p', 'w')) + pickle.dump(faces , open('/tmp/faces.p', 'w')) diff --git a/bin/plot.py b/bin/plot.py new file mode 100644 index 0000000..5a13a1b --- /dev/null +++ b/bin/plot.py @@ -0,0 +1,12 @@ +from Blender import * +import bpy + +import pickle +points = pickle.load(open('/tmp/points.p', 'r')) +faces = pickle.load(open('/tmp/faces.p', 'r')) +faces = [faces[i] for i in faces] +me = bpy.data.meshes.new('points') +me.verts.extend(points) +me.faces.extend(faces) +scn = bpy.data.scenes.active +ob = scn.objects.new(me, 'points_obj') diff --git a/data/gmsh.geo b/data/gmsh.geo new file mode 100644 index 0000000..b273127 --- /dev/null +++ b/data/gmsh.geo @@ -0,0 +1,10 @@ +Point(0) = {0, 0, 0, 0.1}; +Point(1) = {1, 0, 0, 0.1}; +Point(2) = {1, 1, 0, 0.1}; +Point(3) = {0, 1, 0, 0.1}; +Line(1) = {0, 1}; +Line(2) = {1, 2}; +Line(3) = {2, 3}; +Line(4) = {3, 0}; +Line Loop(6) = {3, 4, 1, 2}; +Plane Surface(6) = {6}; diff --git a/data/gmsh.msh b/data/gmsh.msh new file mode 100644 index 0000000..6481682 --- /dev/null +++ b/data/gmsh.msh @@ -0,0 +1,362 @@ +$MeshFormat +2.1 0 8 +$EndMeshFormat +$Nodes +117 +1 0 0 0 +2 1 0 0 +3 1 1 0 +4 0 1 0 +5 0.1111111111108444 0 0 +6 0.2222222222216888 0 0 +7 0.3333333333326072 0 0 +8 0.4444444444435502 0 0 +9 0.5555555555546474 0 0 +10 0.6666666666658989 0 0 +11 0.7777777777771888 0 0 +12 0.8888888888885944 0 0 +13 1 0.1111111111108444 0 +14 1 0.2222222222216888 0 +15 1 0.3333333333326072 0 +16 1 0.4444444444435502 0 +17 1 0.5555555555546474 0 +18 1 0.6666666666658989 0 +19 1 0.7777777777771888 0 +20 1 0.8888888888885944 0 +21 0.8888888888890432 1 0 +22 0.7777777777780862 1 0 +23 0.6666666666673606 1 0 +24 0.5555555555567121 1 0 +25 0.4444444444457551 1 0 +26 0.3333333333344898 1 0 +27 0.2222222222231475 1 0 +28 0.1111111111115738 1 0 +29 0 0.8888888888890432 0 +30 0 0.7777777777780862 0 +31 0 0.6666666666673606 0 +32 0 0.5555555555567121 0 +33 0 0.4444444444457551 0 +34 0 0.3333333333344898 0 +35 0 0.2222222222231475 0 +36 0 0.1111111111115738 0 +37 0.6019481680509189 0.4913575543465167 0 +38 0.1704154153674942 0.6987672532229658 0 +39 0.2320323718440504 0.2988129032487034 0 +40 0.7203934687596977 0.8043583691231997 0 +41 0.7461530186841386 0.1929549013576664 0 +42 0.1592578463116252 0.08359262302228047 0 +43 0.9046732056739187 0.8386130897669745 0 +44 0.55391224379778 0.2396033220754424 0 +45 0.5421680685928277 0.7779286720011396 0 +46 0.8181774873951955 0.4749882420507863 0 +47 0.2326926161233039 0.485616852426389 0 +48 0.4134289336676409 0.3755948039322699 0 +49 0.3955857714716899 0.155614678676658 0 +50 0.3964523820262634 0.6106237011601788 0 +51 0.3183008551948364 0.8282596138649532 0 +52 0.747972626339022 0.3174544725147305 0 +53 0.115111514964065 0.5094807917873129 0 +54 0.08210779783140823 0.3953488242638357 0 +55 0.2161063870833996 0.8420170010432833 0 +56 0.2867674701932997 0.1004014894467341 0 +57 0.1117691280776918 0.2867373625973466 0 +58 0.7782802801475887 0.08764743691808025 0 +59 0.1656501824337609 0.9139110243626632 0 +60 0.09777744997828354 0.6250809283025385 0 +61 0.08694268970211994 0.1534377186923689 0 +62 0.8874248862012761 0.1766706245754574 0 +63 0.8143386010060611 0.8947998106334527 0 +64 0.9154635697338186 0.2965126361214718 0 +65 0.2042201551313745 0.1825907197057003 0 +66 0.1221557588570758 0.8160684445636995 0 +67 0.5615878107409328 0.6625913585966549 0 +68 0.5710015654393022 0.3513778243655594 0 +69 0.5543394306777543 0.1388073259925792 0 +70 0.4706111971459698 0.6893623434360537 0 +71 0.7241227362625926 0.4969553545911873 0 +72 0.4088030555370736 0.492829373616883 0 +73 0.4125834576270711 0.2624197734146648 0 +74 0.6735284038876777 0.3793797103525962 0 +75 0.4951834151010877 0.4327963513029263 0 +76 0.5017226402205569 0.5661622548734149 0 +77 0.3625254214966867 0.7226669967770163 0 +78 0.4924410166960045 0.8911616438491863 0 +79 0.4904641794826192 0.3100224675679338 0 +80 0.472506605803479 0.09184641247689862 0 +81 0.2587129787828705 0.392780509541241 0 +82 0.432030453434602 0.8049389726897674 0 +83 0.4825516434052989 0.1972004487891118 0 +84 0.6706338288381103 0.6104819591670753 0 +85 0.3122957619843937 0.2173797050034385 0 +86 0.3085810297272485 0.5445597672441419 0 +87 0.3257130019894861 0.4334554227112007 0 +88 0.8478065678339236 0.3756852149133095 0 +89 0.9120115837707329 0.487462686094354 0 +90 0.2744908164189229 0.6568143157400268 0 +91 0.3260460219226232 0.3246015526067311 0 +92 0.1999858480155758 0.5895516042137126 0 +93 0.2445600632027671 0.7548465042825554 0 +94 0.6212465789983248 0.8135684284801679 0 +95 0.7623215643436823 0.408892598884522 0 +96 0.935056344267695 0.3874876629810585 0 +97 0.8876344750775997 0.6238650861420173 0 +98 0.3865275250489252 0.06957251612005813 0 +99 0.8276328903990903 0.2706905087430281 0 +100 0.6562588147613345 0.1097474676684926 0 +101 0.7918715835039105 0.5706548964236209 0 +102 0.1720710679372316 0.3947962073108047 0 +103 0.6433094433695727 0.2470464320467238 0 +104 0.0714623294249179 0.06962829056524464 0 +105 0.07936210124661788 0.9218754085638319 0 +106 0.9335406355621672 0.9269560462763735 0 +107 0.676544555905739 0.9090008435792557 0 +108 0.3807337346163223 0.906063664281206 0 +109 0.5775911551499217 0.87833191758195 0 +110 0.6433967591677658 0.7346966171533521 0 +111 0.584405534175352 0.5826054888330772 0 +112 0.07806972484057069 0.7168722141069301 0 +113 0.2727244524809927 0.9150418839253508 0 +114 0.7695699867642202 0.697810806017272 0 +115 0.8934048903315858 0.7315672920096196 0 +116 0.8204760305070968 0.7934298735101037 0 +117 0.8887286388357746 0.06257152876739701 0 +$EndNodes +$Elements +236 +1 15 3 0 0 0 1 +2 15 3 0 1 0 2 +3 15 3 0 2 0 3 +4 15 3 0 3 0 4 +5 1 3 0 1 0 1 5 +6 1 3 0 1 0 5 6 +7 1 3 0 1 0 6 7 +8 1 3 0 1 0 7 8 +9 1 3 0 1 0 8 9 +10 1 3 0 1 0 9 10 +11 1 3 0 1 0 10 11 +12 1 3 0 1 0 11 12 +13 1 3 0 1 0 12 2 +14 1 3 0 2 0 2 13 +15 1 3 0 2 0 13 14 +16 1 3 0 2 0 14 15 +17 1 3 0 2 0 15 16 +18 1 3 0 2 0 16 17 +19 1 3 0 2 0 17 18 +20 1 3 0 2 0 18 19 +21 1 3 0 2 0 19 20 +22 1 3 0 2 0 20 3 +23 1 3 0 3 0 3 21 +24 1 3 0 3 0 21 22 +25 1 3 0 3 0 22 23 +26 1 3 0 3 0 23 24 +27 1 3 0 3 0 24 25 +28 1 3 0 3 0 25 26 +29 1 3 0 3 0 26 27 +30 1 3 0 3 0 27 28 +31 1 3 0 3 0 28 4 +32 1 3 0 4 0 4 29 +33 1 3 0 4 0 29 30 +34 1 3 0 4 0 30 31 +35 1 3 0 4 0 31 32 +36 1 3 0 4 0 32 33 +37 1 3 0 4 0 33 34 +38 1 3 0 4 0 34 35 +39 1 3 0 4 0 35 36 +40 1 3 0 4 0 36 1 +41 2 3 0 6 0 19 20 43 +42 2 3 0 6 0 5 6 42 +43 2 3 0 6 0 53 32 33 +44 2 3 0 6 0 33 34 54 +45 2 3 0 6 0 61 57 35 +46 2 3 0 6 0 41 58 62 +47 2 3 0 6 0 64 14 15 +48 2 3 0 6 0 42 56 65 +49 2 3 0 6 0 39 57 65 +50 2 3 0 6 0 59 27 28 +51 2 3 0 6 0 61 35 36 +52 2 3 0 6 0 53 33 54 +53 2 3 0 6 0 61 42 65 +54 2 3 0 6 0 61 65 57 +55 2 3 0 6 0 13 14 62 +56 2 3 0 6 0 62 14 64 +57 2 3 0 6 0 21 22 63 +58 2 3 0 6 0 60 31 32 +59 2 3 0 6 0 32 53 60 +60 2 3 0 6 0 34 35 57 +61 2 3 0 6 0 57 54 34 +62 2 3 0 6 0 10 11 58 +63 2 3 0 6 0 6 7 56 +64 2 3 0 6 0 6 56 42 +65 2 3 0 6 0 70 67 45 +66 2 3 0 6 0 37 68 74 +67 2 3 0 6 0 74 71 37 +68 2 3 0 6 0 75 68 37 +69 2 3 0 6 0 75 72 48 +70 2 3 0 6 0 50 72 76 +71 2 3 0 6 0 50 70 77 +72 2 3 0 6 0 44 68 79 +73 2 3 0 6 0 48 73 79 +74 2 3 0 6 0 80 8 9 +75 2 3 0 6 0 9 69 80 +76 2 3 0 6 0 82 70 45 +77 2 3 0 6 0 83 69 44 +78 2 3 0 6 0 83 73 49 +79 2 3 0 6 0 37 71 84 +80 2 3 0 6 0 49 73 85 +81 2 3 0 6 0 85 56 49 +82 2 3 0 6 0 86 72 50 +83 2 3 0 6 0 48 72 87 +84 2 3 0 6 0 47 81 87 +85 2 3 0 6 0 46 88 89 +86 2 3 0 6 0 50 77 90 +87 2 3 0 6 0 91 73 48 +88 2 3 0 6 0 91 81 39 +89 2 3 0 6 0 38 60 92 +90 2 3 0 6 0 92 60 53 +91 2 3 0 6 0 93 66 38 +92 2 3 0 6 0 46 71 95 +93 2 3 0 6 0 52 88 95 +94 2 3 0 6 0 95 88 46 +95 2 3 0 6 0 96 15 16 +96 2 3 0 6 0 96 88 64 +97 2 3 0 6 0 64 15 96 +98 2 3 0 6 0 17 18 97 +99 2 3 0 6 0 7 8 98 +100 2 3 0 6 0 49 56 98 +101 2 3 0 6 0 98 56 7 +102 2 3 0 6 0 41 62 99 +103 2 3 0 6 0 99 62 64 +104 2 3 0 6 0 99 52 41 +105 2 3 0 6 0 9 10 100 +106 2 3 0 6 0 100 69 9 +107 2 3 0 6 0 47 53 102 +108 2 3 0 6 0 102 53 54 +109 2 3 0 6 0 102 81 47 +110 2 3 0 6 0 44 69 103 +111 2 3 0 6 0 104 1 5 +112 2 3 0 6 0 104 61 36 +113 2 3 0 6 0 36 1 104 +114 2 3 0 6 0 105 4 29 +115 2 3 0 6 0 105 59 28 +116 2 3 0 6 0 28 4 105 +117 2 3 0 6 0 106 3 21 +118 2 3 0 6 0 106 43 20 +119 2 3 0 6 0 20 3 106 +120 2 3 0 6 0 98 8 80 +121 2 3 0 6 0 49 98 80 +122 2 3 0 6 0 85 39 65 +123 2 3 0 6 0 56 85 65 +124 2 3 0 6 0 106 21 63 +125 2 3 0 6 0 43 106 63 +126 2 3 0 6 0 88 52 99 +127 2 3 0 6 0 88 99 64 +128 2 3 0 6 0 104 5 42 +129 2 3 0 6 0 61 104 42 +130 2 3 0 6 0 70 50 76 +131 2 3 0 6 0 70 76 67 +132 2 3 0 6 0 75 48 79 +133 2 3 0 6 0 75 79 68 +134 2 3 0 6 0 83 49 80 +135 2 3 0 6 0 69 83 80 +136 2 3 0 6 0 82 51 77 +137 2 3 0 6 0 70 82 77 +138 2 3 0 6 0 74 52 95 +139 2 3 0 6 0 74 95 71 +140 2 3 0 6 0 75 37 76 +141 2 3 0 6 0 75 76 72 +142 2 3 0 6 0 86 47 87 +143 2 3 0 6 0 86 87 72 +144 2 3 0 6 0 91 39 85 +145 2 3 0 6 0 73 91 85 +146 2 3 0 6 0 83 44 79 +147 2 3 0 6 0 73 83 79 +148 2 3 0 6 0 86 50 90 +149 2 3 0 6 0 91 48 87 +150 2 3 0 6 0 81 91 87 +151 2 3 0 6 0 96 16 89 +152 2 3 0 6 0 88 96 89 +153 2 3 0 6 0 78 24 25 +154 2 3 0 6 0 45 78 82 +155 2 3 0 6 0 89 16 17 +156 2 3 0 6 0 97 89 17 +157 2 3 0 6 0 46 89 101 +158 2 3 0 6 0 97 101 89 +159 2 3 0 6 0 103 41 52 +160 2 3 0 6 0 103 68 44 +161 2 3 0 6 0 68 103 74 +162 2 3 0 6 0 103 52 74 +163 2 3 0 6 0 93 38 90 +164 2 3 0 6 0 93 77 51 +165 2 3 0 6 0 51 55 93 +166 2 3 0 6 0 93 90 77 +167 2 3 0 6 0 101 71 46 +168 2 3 0 6 0 71 101 84 +169 2 3 0 6 0 38 92 90 +170 2 3 0 6 0 92 53 47 +171 2 3 0 6 0 86 92 47 +172 2 3 0 6 0 86 90 92 +173 2 3 0 6 0 29 30 66 +174 2 3 0 6 0 105 29 66 +175 2 3 0 6 0 66 55 59 +176 2 3 0 6 0 93 55 66 +177 2 3 0 6 0 59 105 66 +178 2 3 0 6 0 78 25 108 +179 2 3 0 6 0 108 82 78 +180 2 3 0 6 0 78 45 109 +181 2 3 0 6 0 94 107 109 +182 2 3 0 6 0 109 45 94 +183 2 3 0 6 0 110 45 67 +184 2 3 0 6 0 94 45 110 +185 2 3 0 6 0 111 76 37 +186 2 3 0 6 0 111 84 67 +187 2 3 0 6 0 67 76 111 +188 2 3 0 6 0 37 84 111 +189 2 3 0 6 0 60 38 112 +190 2 3 0 6 0 66 30 112 +191 2 3 0 6 0 112 38 66 +192 2 3 0 6 0 113 27 59 +193 2 3 0 6 0 59 55 113 +194 2 3 0 6 0 112 30 31 +195 2 3 0 6 0 60 112 31 +196 2 3 0 6 0 113 55 51 +197 2 3 0 6 0 26 27 113 +198 2 3 0 6 0 108 25 26 +199 2 3 0 6 0 24 107 23 +200 2 3 0 6 0 24 78 109 +201 2 3 0 6 0 24 109 107 +202 2 3 0 6 0 110 67 84 +203 2 3 0 6 0 94 110 40 +204 2 3 0 6 0 107 22 23 +205 2 3 0 6 0 63 22 107 +206 2 3 0 6 0 40 63 107 +207 2 3 0 6 0 40 107 94 +208 2 3 0 6 0 41 100 58 +209 2 3 0 6 0 103 100 41 +210 2 3 0 6 0 58 100 10 +211 2 3 0 6 0 103 69 100 +212 2 3 0 6 0 102 57 39 +213 2 3 0 6 0 39 81 102 +214 2 3 0 6 0 54 57 102 +215 2 3 0 6 0 114 40 110 +216 2 3 0 6 0 84 101 114 +217 2 3 0 6 0 114 110 84 +218 2 3 0 6 0 114 101 97 +219 2 3 0 6 0 26 113 108 +220 2 3 0 6 0 108 51 82 +221 2 3 0 6 0 108 113 51 +222 2 3 0 6 0 43 115 19 +223 2 3 0 6 0 115 114 97 +224 2 3 0 6 0 115 97 18 +225 2 3 0 6 0 115 18 19 +226 2 3 0 6 0 116 63 40 +227 2 3 0 6 0 116 114 115 +228 2 3 0 6 0 40 114 116 +229 2 3 0 6 0 43 63 116 +230 2 3 0 6 0 43 116 115 +231 2 3 0 6 0 117 2 13 +232 2 3 0 6 0 12 2 117 +233 2 3 0 6 0 11 12 117 +234 2 3 0 6 0 62 58 117 +235 2 3 0 6 0 117 13 62 +236 2 3 0 6 0 11 117 58 +$EndElements diff --git a/test/baker.test.py b/test/baker.test.py index df4b4cb..583c723 100755 --- a/test/baker.test.py +++ b/test/baker.test.py @@ -37,7 +37,7 @@ class TestSequenceFunctions(unittest.TestCase): r = [[-1, -1], [0, 2], [1, -1]] result = baker.get_phis(X, r) -#result = [round(i, self.accuracy) for i in result] + #result = [round(i, self.accuracy) for i in result] right_answer = [1/3.0, 1/3.0, 1/3.0]