/[escript]/trunk/finley/test/python/run_inputOutput.py
ViewVC logotype

Contents of /trunk/finley/test/python/run_inputOutput.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 6875 - (show annotations)
Fri Aug 9 02:20:21 2019 UTC (2 months, 1 week ago) by aellery
File MIME type: text/x-python
File size: 10842 byte(s)
Added a new function to finley and dudley, getConnectivityInfo, that returns a numpy array that contains the connectivity information for the mesh.

Example use:

import numpy
from esys.finley import *
domain=Rectangle(n0=10,n1=10)
info=domain.getConnectivityInfo()





1
2 ##############################################################################
3 #
4 # Copyright (c) 2003-2018 by The University of Queensland
5 # http://www.uq.edu.au
6 #
7 # Primary Business: Queensland, Australia
8 # Licensed under the Apache License, version 2.0
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Development until 2012 by Earth Systems Science Computational Center (ESSCC)
12 # Development 2012-2013 by School of Earth Sciences
13 # Development from 2014 by Centre for Geoscience Computing (GeoComp)
14 #
15 ##############################################################################
16
17 from __future__ import print_function, division
18
19 __copyright__="""Copyright (c) 2003-2018 by The University of Queensland
20 http://www.uq.edu.au
21 Primary Business: Queensland, Australia"""
22 __license__="""Licensed under the Apache License, version 2.0
23 http://www.apache.org/licenses/LICENSE-2.0"""
24 __url__="https://launchpad.net/escript-finley"
25
26 """
27 Test suite for input and output of meshes and data objects
28
29 :remark:
30
31 :var __author__: name of author
32 :var __licence__: licence agreement
33 :var __url__: url entry point on documentation
34 :var __version__: version
35 :var __date__: date of the version
36 """
37
38 import esys.escriptcore.utestselect as unittest, sys
39 from esys.escriptcore.testing import *
40 from esys.escript import *
41 from esys.finley import Rectangle, Brick, LoadMesh, ReadMesh, GetMeshFromFile, ReadGmsh
42 import os as os
43 import numpy as np
44
45 try:
46 FINLEY_WORKDIR=os.environ['FINLEY_WORKDIR']
47 except KeyError:
48 FINLEY_WORKDIR=os.path.join(os.getcwd(),"finley/test/")
49
50 try:
51 FINLEY_TEST_DATA=os.environ['FINLEY_TEST_DATA']
52 except KeyError:
53 FINLEY_TEST_DATA=os.path.join(os.getcwd(),"finley/test/python")
54
55 FINLEY_TEST_MESH_PATH=os.path.join(FINLEY_TEST_DATA,"data_meshes")
56
57 REL_TOL=1.e-6
58
59 # Number of elements scales up with number of MPI processes
60 mpisize = getMPISizeWorld()
61 NE0 = 7 * mpisize
62 NE1 = 11
63 NE2 = 5
64
65 class Test_InputOutputOnFinley(unittest.TestCase):
66 # Check that two domains are equal using Fourier integrals
67 # We cannot compare the X coordinates since they are on different domains
68 def domainsEqual(self, m1, m2, nft=100):
69 self.assertTrue(m1.getDim() == m2.getDim(), "Dimensions differ")
70 self.assertTrue(m1.getNumDataPointsGlobal() == m2.getNumDataPointsGlobal(), "Num data points differ")
71 for tagName in m1.showTagNames().split(", "):
72 self.assertTrue(m2.isValidTagName(tagName), "m1 has a tag '%s' not present in m2" % tagName)
73 for tagName in m2.showTagNames().split(", "):
74 self.assertTrue(m1.isValidTagName(tagName), "m2 has a tag '%s' not present in m1" % tagName)
75 self.assertTrue(m1.getTag(tagName) == m2.getTag(tagName), "values of tag '%s' differ" % tagName)
76 for fs in ["Solution", "ReducedSolution", "Function", "ReducedFunction", "ContinuousFunction", "ReducedContinuousFunction"]:
77 fs1 = eval("%s(m1)" % fs)
78 fs2 = eval("%s(m2)" % fs)
79 x1 = fs1.getX()
80 x2 = fs2.getX()
81 for n in range(1, nft+1):
82 integ1 = integrate(sin(n*x1))
83 integ2 = integrate(sin(n*x2))
84 self.assertTrue(Lsup(abs(integ1-integ2)) <= REL_TOL, "integrals for n=%d differ" % n)
85 return True
86
87 # Does optimize=True change Rectangle for order=1?
88 def test_Rectangle_optimize_order1(self):
89 mydomain1 = Rectangle(n0=NE0, n1=NE1, order=1, l0=1., l1=1., optimize=False,useElementsOnFace=0)
90 mydomain2 = Rectangle(n0=NE0, n1=NE1, order=1, l0=1., l1=1., optimize=True,useElementsOnFace=0)
91 self.domainsEqual(mydomain1, mydomain2)
92
93 # Does optimize=True change Rectangle for order=2?
94 def test_Rectangle_optimize_order2(self):
95 mydomain1 = Rectangle(n0=NE0, n1=NE1, order=2, l0=1., l1=1., optimize=False,useElementsOnFace=0)
96 mydomain2 = Rectangle(n0=NE0, n1=NE1, order=2, l0=1., l1=1., optimize=True,useElementsOnFace=0)
97 self.domainsEqual(mydomain1, mydomain2)
98
99 # Does optimize=True change Rectangle for order=-1?
100 def test_Rectangle_optimize_macro(self):
101 mydomain1 = Rectangle(n0=NE0, n1=NE1, order=-1, l0=1., l1=1., optimize=False, useElementsOnFace=0)
102 mydomain2 = Rectangle(n0=NE0, n1=NE1, order=-1, l0=1., l1=1., optimize=True,useElementsOnFace=0)
103 self.domainsEqual(mydomain1, mydomain2)
104
105 # Does optimize=True change Brick for order=1?
106 def test_Brick_optimize_order1(self):
107 mydomain1 = Brick(n0=NE0, n1=NE1, n2=NE2, order=1, l0=1., l1=1., l2=1., optimize=False,useElementsOnFace=0)
108 mydomain2 = Brick(n0=NE0, n1=NE1, n2=NE2, order=1, l0=1., l1=1., l2=1., optimize=True,useElementsOnFace=0)
109 self.domainsEqual(mydomain1, mydomain2)
110
111 # Does optimize=True change Brick for order=2?
112 def test_Brick_optimize_order2(self):
113 mydomain1 = Brick(n0=NE0, n1=NE1, n2=NE2, order=2, l0=1., l1=1., l2=1., optimize=False,useElementsOnFace=0)
114 mydomain2 = Brick(n0=NE0, n1=NE1, n2=NE2, order=2, l0=1., l1=1., l2=1., optimize=True,useElementsOnFace=0)
115 self.domainsEqual(mydomain1, mydomain2)
116 # Does optimize=True change Brick for order=-1?
117 def test_Brick_optimize_macro(self):
118 mydomain1 = Brick(n0=NE0, n1=NE1, n2=NE2, order=-1, l0=1., l1=1., l2=1., optimize=False,useElementsOnFace=0)
119 mydomain2 = Brick(n0=NE0, n1=NE1, n2=NE2, order=-1, l0=1., l1=1., l2=1., optimize=True,useElementsOnFace=0)
120 self.domainsEqual(mydomain1, mydomain2)
121
122 @unittest.skipIf(not loadIsConfigured(), "load not configured")
123 def test_data_dump_to_NetCDF_rectangle(self):
124 mydomain1 = Rectangle(n0=NE0, n1=NE1, order=1, l0=1., l1=1., optimize=False,useElementsOnFace=0)
125 d1=Data(mydomain1.getMPIRank(), Function(mydomain1))
126 d1.expand()
127 dumpfile=os.path.join(FINLEY_WORKDIR, "tempfile.dump.nc")
128 d1.dump(dumpfile)
129 d2=load(dumpfile, mydomain1)
130 self.assertTrue(Lsup(abs(d1-d2)) <= REL_TOL, "data objects differ")
131
132 @unittest.skipIf(not loadIsConfigured(), "load not configured")
133 def test_data_dump_to_NetCDF_brick(self):
134 mydomain1 = Brick(n0=NE0, n1=NE1, n2=NE2, order=2, l0=1., l1=1., l2=1., optimize=False)
135 d1=Data(mydomain1.getMPIRank(), Function(mydomain1))
136 d1.expand()
137 dumpfile=os.path.join(FINLEY_WORKDIR, "tempfile.dump.nc")
138 d1.dump(dumpfile)
139 d2=load(dumpfile, mydomain1)
140 self.assertTrue(Lsup(abs(d1-d2)) <= REL_TOL, "data objects differ")
141
142 # @unittest.skipIf(not loadIsConfigured() or mpisize > 1, "load not configured or mpisize > 1")
143 # def test_mesh_dump_to_NetCDF_rectangle(self):
144 # mydomain1 = Rectangle(n0=NE0, n1=NE1, order=1, l0=1., l1=1., optimize=False)
145 # dumpfile=os.path.join(FINLEY_WORKDIR, "tempfile.mesh.nc")
146 # mydomain1.dump(dumpfile)
147 # mydomain2=LoadMesh(dumpfile)
148 # self.domainsEqual(mydomain1, mydomain2)
149
150 @unittest.skipIf(mpisize>1, "more than 1 MPI rank")
151 def test_gmshTags(self):
152 dom=ReadGmsh(os.path.join(FINLEY_TEST_MESH_PATH, "tagtest.msh"),2)
153 tags=dom.showTagNames().split(', ')
154 self.assertEqual(tags,['tag1', 'tag2', 'tag3'],'error with tags')
155 self.assertEqual(dom.getTag('tag1'),1,'error with tag1')
156 self.assertEqual(dom.getTag('tag2'),2,'error with tag2')
157 self.assertEqual(dom.getTag('tag3'),3,'error with tag3')
158 self.assertRaises(ValueError, dom.getTag, 'tag4')
159
160 @unittest.skipIf(mpisize>1, "more than 1 MPI rank")
161 def test_gmshNamedTags(self):
162 dom=ReadGmsh(os.path.join(FINLEY_TEST_MESH_PATH, "test_Add.msh"),3)
163 sigma = Scalar(0,Function(dom))
164 sigma.expand()
165 sigma.setTaggedValue("v5",10)
166 self.assertEqual(Lsup(sigma),10.0,"Named value not set properly")
167
168 def test_flyTags(self):
169 dom=ReadMesh(os.path.join(FINLEY_TEST_MESH_PATH, "rectangle_8x10.fly"))
170 tags=sorted(dom.showTagNames().split(', '))
171 self.assertEqual(tags,sorted(['top', 'bottom', 'left', 'right']))
172 self.assertEqual(dom.getTag('top'),20,'error with top')
173 self.assertEqual(dom.getTag('bottom'),10,'error with bottom,')
174 self.assertEqual(dom.getTag('left'),1,'error with left')
175 self.assertEqual(dom.getTag('right'),2,'error with reight')
176 self.assertRaises(ValueError, dom.getTag, 'tag4')
177
178 # @unittest.skipIf(not loadIsConfigured() or mpisize>1, "load not configured or mpisize > 1")
179 # def test_mesh_dump_to_NetCDF_brick(self):
180 # mydomain1 = Brick(n0=NE0, n1=NE1, n2=NE2, order=2, l0=1., l1=1., l2=1., optimize=False)
181 # dumpfile=os.path.join(FINLEY_WORKDIR, "tempfile.mesh.nc")
182 # mydomain1.dump(dumpfile)
183 # mydomain2=LoadMesh(dumpfile)
184 # self.domainsEqual(mydomain1, mydomain2)
185
186 @unittest.skipIf(mpisize>15, "more than 15 MPI ranks")
187 def test_mesh_read_rectangle_from_finley_file(self):
188 mydomain1 = Rectangle(n0=8, n1=10, order=1, l0=1., l1=1., optimize=False)
189 mydomain2 = ReadMesh(os.path.join(FINLEY_TEST_MESH_PATH,"rectangle_8x10.fly"))
190 self.domainsEqual(mydomain1, mydomain2)
191
192 @unittest.skipIf(mpisize>15, "more than 15 MPI ranks")
193 def test_mesh_read_brick_from_finley_file(self):
194 mydomain1 = Brick(n0=8, n1=10, n2=12, order=1, l0=1., l1=1., l2=1., optimize=False)
195 mydomain2 = ReadMesh(os.path.join(FINLEY_TEST_MESH_PATH,"brick_8x10x12.fly"))
196 self.domainsEqual(mydomain1, mydomain2)
197
198 @unittest.skipIf(mpisize>1, "more than 1 MPI rank")
199 def test_GetMeshFromFile(self):
200 m=GetMeshFromFile(os.path.join(FINLEY_TEST_MESH_PATH,'tet10_gmsh.msh'), numDim=3)
201 del m
202 m=GetMeshFromFile(os.path.join(FINLEY_TEST_MESH_PATH, 'tet10.fly'))
203 # now we try some params
204 m=GetMeshFromFile(os.path.join(FINLEY_TEST_MESH_PATH,'tet10_gmsh.msh'), numDim=3, integrationOrder=2)
205
206 def test_connectivity_info(self):
207 if hasFeature("boostnumpy"):
208 domain=Rectangle(n0=3,n1=4)
209 testvalues=domain.getConnectivityInfo()
210 correctvalues=[[0., 1., 5., 4.],
211 [ 1., 2., 6., 5.],
212 [ 2., 3., 7., 6.],
213 [ 4., 5., 9., 8.],
214 [ 5., 6.,10., 9.],
215 [ 6., 7.,11.,10.],
216 [ 8., 9.,13.,12.],
217 [ 9.,10.,14.,13.],
218 [10.,11.,15.,14.],
219 [12.,13.,17.,16.],
220 [13.,14.,18.,17.],
221 [14.,15.,19.,18.]]
222 for i in range(0,testvalues.shape[0]):
223 for j in range(0,4):
224 self.assertEqual(testvalues[i][j],correctvalues[i][j])
225
226
227 if __name__ == '__main__':
228 run_tests(__name__, exit_on_failure=True)

  ViewVC Help
Powered by ViewVC 1.1.26