Salome HOME
30b4af3046b41c284b45cc54bbde9cd74a88e1a9
[tools/medcoupling.git] / src / MEDCoupling_Swig / MEDCouplingBasicsTest6.py
1 #  -*- coding: utf-8 -*-
2 # Copyright (C) 2017  CEA/DEN, EDF R&D
3 #
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License, or (at your option) any later version.
8 #
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 # Lesser General Public License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 #
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #
20
21 from MEDCoupling import *
22 import unittest
23 from math import pi,e,sqrt,cos,sin
24 from datetime import datetime
25 import rlcompleter,readline # this line has to be here, to ensure a usability of MEDCoupling/MEDLoader. B4 removing it please notify to anthony.geay@edf.fr
26
27 class MEDCouplingBasicsTest6(unittest.TestCase):
28     def testPointSetInvertOrientationOfAllCells(self):
29         """ Test of inversion of orientation of cells on a different geo types"""
30         def level1(self):
31             m=MEDCouplingUMesh("",1)
32             m.allocateCells()
33             m.insertNextCell(NORM_SEG2,[3,4])
34             m.insertNextCell(NORM_SEG2,[13,14])
35             m.insertNextCell(NORM_SEG3,[5,6,7])
36             m.insertNextCell(NORM_SEG2,[23,24])
37             m.insertNextCell(NORM_SEG3,[8,9,10])
38             ref0=DataArrayInt([0,3,6,10,13,17])
39             self.assertTrue(m.getNodalConnectivityIndex().isEqual(ref0))
40             m.invertOrientationOfAllCells()
41             self.assertTrue(m.getNodalConnectivity().isEqual(DataArrayInt([1,4,3, 1,14,13, 2,7,6,5, 1,24,23, 2,10,9,8])))
42             self.assertTrue(m.getNodalConnectivityIndex().isEqual(ref0))
43             pass
44
45         def level2(self):
46             m=MEDCouplingUMesh("",2)
47             m.allocateCells()
48             m.insertNextCell(NORM_TRI3,[1,2,3])
49             m.insertNextCell(NORM_QUAD4,[4,5,6,7])
50             m.insertNextCell(NORM_POLYGON,[8,9,10,11,12,13])
51             m.insertNextCell(NORM_TRI6,[14,15,16,17,18,19])
52             m.insertNextCell(NORM_QUAD8,[20,21,22,23,24,25,26,27])
53             m.insertNextCell(NORM_QPOLYG,[30,31,32,33,34,35, 36,37,38,39,40,41])
54             ref0=DataArrayInt([0,4,9,16,23,32,45])
55             self.assertTrue(m.getNodalConnectivityIndex().isEqual(ref0))
56             m.invertOrientationOfAllCells()
57             self.assertTrue(m.getNodalConnectivity().isEqual(DataArrayInt([3,1,3,2, 4,4,7,6,5, 5,8,13,12,11,10,9, 6,14,16,15,19,18,17, 8,20,23,22,21,27,26,25,24, 32,30,35,34,33,32,31,41,40,39,38,37,36])))
58             self.assertTrue(m.getNodalConnectivityIndex().isEqual(ref0))
59             pass
60
61         def level3(self):
62             m=MEDCouplingUMesh("",3)
63             m.allocateCells()
64             m.insertNextCell(NORM_TETRA4,[1,2,3,4])
65             m.insertNextCell(NORM_PYRA5,[5,6,7,8,9])
66             m.insertNextCell(NORM_PENTA6,[10,11,12,13,14,15])
67             m.insertNextCell(NORM_HEXA8,[20,21,22,23,24,25,26,27])
68             m.insertNextCell(NORM_TETRA10,[30,31,32,33,34,35,36,37,38,39])
69             m.insertNextCell(NORM_PYRA13,[40,41,42,43,44,45,46,47,48,49,50,51,52])
70             m.insertNextCell(NORM_HEXA20,[60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79])
71             m.insertNextCell(NORM_PENTA15,[80,81,82,83,84,85,86,87,88,89,90,91,92,93,94])
72             ref0=DataArrayInt([0,5,11,18,27,38,52,73,89])
73             self.assertTrue(m.getNodalConnectivityIndex().isEqual(ref0))
74             m.invertOrientationOfAllCells()
75             self.assertTrue(m.getNodalConnectivity().isEqual(DataArrayInt([14,1,3,2,4, 15,5,8,7,6,9, 16,10,12,11,13,15,14, 18,20,23,22,21,24,27,26,25, 20,30,32,31,33,36,35,34,37,39,38, 23,40,43,42,41,44,48,47,46,45,49,52,51,50, 30,60,63,62,61,64,67,66,65,71,70,69,68,75,74,73,72,76,79,78,77, 25,80,82,81,83,85,84,88,87,86,91,90,89,92,94,93])))
76             self.assertTrue(m.getNodalConnectivityIndex().isEqual(ref0))
77             pass
78
79         def gtumesh(self):
80             m=MEDCoupling1SGTUMesh("",NORM_SEG2)
81             m.setNodalConnectivity(DataArrayInt([1,2,3,4,5,6,7,8]))
82             self.assertEqual(m.getNumberOfCells(),4)
83             m2=m.deepCopy()
84             self.assertTrue(m2.isEqual(m,0.))
85             m.invertOrientationOfAllCells()
86             self.assertTrue(not m2.isEqual(m,0.))
87             m.getNodalConnectivity().isEqual(DataArrayInt([2,1,4,3,6,5,8,7]))
88             m.invertOrientationOfAllCells()
89             self.assertTrue(m2.isEqual(m,0.))
90             #
91             p=MEDCoupling1DGTUMesh("",NORM_POLYGON)
92             ref0=DataArrayInt([0,3,7,12])
93             p.setNodalConnectivity(DataArrayInt([1,2,3, 10,11,12,13, 20,21,22,23,24]),ref0)
94             p2=p.deepCopy()
95             self.assertTrue(p2.isEqual(p,0.))
96             self.assertEqual(p.getNumberOfCells(),3)
97             p.invertOrientationOfAllCells()
98             self.assertTrue(not p2.isEqual(p,0.))
99             self.assertTrue(p.getNodalConnectivityIndex().isEqual(ref0))
100             self.assertTrue(p.getNodalConnectivity().isEqual(DataArrayInt([1,3,2, 10,13,12,11, 20,24,23,22,21])))
101             p.invertOrientationOfAllCells()
102             self.assertTrue(p2.isEqual(p,0.))
103             pass
104         level1(self)
105         level2(self)
106         level3(self)
107         gtumesh(self)
108         pass
109
110     def testPenta18_1(self):
111         arr=DataArrayDouble([
112             (0.,1.,1.),(0.,0.,1.),(1.,0.,1.),
113             (0.,1.,0.),(0.,0.,0.),(1.,0.,0.),
114             (0.,0.5,1.),(0.5,0.,1.),(0.5,0.5,1.),
115             (0.,0.5,0.),(0.5,0.,0.),(0.5,0.5,0.),
116             (0.,1.,0.5),(0.,0.,0.5),(1.,0.,0.5),
117             (0.,0.5,0.5),(0.5,0.,0.5),(0.5,0.5,0.5)])
118         m=MEDCouplingUMesh("mesh",3)
119         m.setCoords(arr)
120         m.allocateCells(1)
121         m.insertNextCell(NORM_PENTA18,list(range(18)))
122         m.checkConsistencyLight()
123         self.assertTrue(m.getMeasureField(True).getArray().isEqual(DataArrayDouble([0.5]),1e-12))
124         #
125         f=MEDCouplingFieldDouble(ON_NODES)
126         f.setMesh(m)
127         f.setName("FieldOnPenta18")
128         f.setArray(DataArrayDouble(list(range(18))))
129         f.checkConsistencyLight()
130         #
131         m2,d,di,rd,rdi=m.buildDescendingConnectivity()
132         self.assertTrue(m2.getNodalConnectivity().isEqual(DataArrayInt([6,0,1,2,6,7,8,6,3,5,4,11,10,9,9,0,3,4,1,12,9,13,6,15,9,1,4,5,2,13,10,14,7,16,9,2,4,5,0,14,11,12,8,17])))
133         self.assertTrue(m2.getNodalConnectivityIndex().isEqual(DataArrayInt([0,7,14,24,34,44])))
134         self.assertTrue(d.isEqual(DataArrayInt([0,1,2,3,4])))
135         self.assertTrue(di.isEqual(DataArrayInt([0,5])))
136         self.assertTrue(rd.isEqual(DataArrayInt([0,0,0,0,0])))
137         self.assertTrue(rdi.isEqual(DataArrayInt([0,1,2,3,4,5])))
138         #
139         f2=MEDCouplingFieldDouble(ON_NODES)
140         f2.setMesh(m)
141         f2.setName("FieldOnPenta18Sub")
142         f2.setArray(DataArrayDouble(list(range(18))))
143         f2.checkConsistencyLight()
144         pass
145
146     def testSKLAReplaceDeletePacks(self):
147         index=DataArrayInt([0,3,5,6,6])
148         value=DataArrayInt([1,2,3, 2,3, 3  ])
149         sla=MEDCouplingSkyLineArray(index,value)
150         idx=DataArrayInt([0,3])
151         packs=[DataArrayInt([4,5]),DataArrayInt([6,7,8])]
152         sla.replaceSimplePacks(idx,packs)
153         self.assertTrue(sla.getIndexArray().isEqual(DataArrayInt([0,2,4,5,8])))
154         self.assertTrue(sla.getValuesArray().isEqual(DataArrayInt([4,5, 2,3, 3, 6,7,8])))
155         sla.deleteSimplePacks(idx)
156         self.assertTrue(sla.getIndexArray().isEqual(DataArrayInt([0,2,3])))
157         self.assertTrue(sla.getValuesArray().isEqual(DataArrayInt([2,3, 3])))
158         sla.deleteSimplePack(1)
159         self.assertTrue(sla.getIndexArray().isEqual(DataArrayInt([0,2])))
160         self.assertTrue(sla.getValuesArray().isEqual(DataArrayInt([2,3])))
161         pass
162
163     def testDADAsArcOfCircle(self):
164         d=DataArrayDouble([3.06915124862645,2.1464466094067824,2.85355345827285,2.3620444674400574,2.637955532559882,2.1464467447661937],3,2)
165         center,radius,ang=d.asArcOfCircle()
166         self.assertTrue((d-center).magnitude().isUniform(radius,1e-10))
167         self.assertAlmostEqual(ang,-4.712389294301196,12)
168         pass
169
170     def testDAMaxAbsValue(self):
171         d=DataArrayDouble([-2,3,1.2,-2.9])
172         a,b=d.getMaxAbsValue()
173         self.assertAlmostEqual(a,3.,13)
174         self.assertEqual(b,1)
175         a,b=(-d).getMaxAbsValue()
176         self.assertAlmostEqual(a,-3.,13)
177         self.assertEqual(b,1)
178         self.assertAlmostEqual((-d).getMaxAbsValueInArray(),-3.,13)
179         pass
180
181     def testDAIFindIdForEach1(self):
182         a1=DataArrayInt([17,27,2,10,-4,3,12,27,16])
183         b1=DataArrayInt([3,16,-4,27,17])
184         ret=a1.findIdForEach(b1)
185         self.assertTrue(ret.isEqual(DataArrayInt([5,8,4,7,0])))
186         self.assertTrue(a1[ret].isEqual(b1))
187         b2=DataArrayInt([3,16,22,27,17])
188         self.assertRaises(InterpKernelException,a1.findIdForEach,b2) # 22 not in a1 !
189         a1.rearrange(3)
190         self.assertRaises(InterpKernelException,a1.findIdForEach,b1) # a1 is not single component
191         pass
192     
193     def testAttractSeg3MidPtsAroundNodes1(self):
194         """ Test of MEDCouplingUMesh.attractSeg3MidPtsAroundNodes methods """
195         ptsExpToBeModified=DataArrayInt([95,96,97,98,101,103,104,106,108,110])
196         eps=1e-12
197         a=2./3.
198         b=1./3.
199         coo=DataArrayDouble([10,0,0,10,10,0,10,0,3.+b,10,0,6.+a,10,10,3.+b,10,10,6.+a,10,3.+b,0,10,6.+a,0,3.+b,0,0,6.+a,0,0,3.+b,10,0,6.+a,10,0,10,3.+b,6.+a,10,6.+a,6.+a,10,3.+b,3.+b,10,6.+a,3.+b,3.+b,0,3.+b,3.+b,0,6.+a,6.+a,0,3.+b,6.+a,0,6.+a,6.+a,10,3.+b,6.+a,10,6.+a,3.+b,10,3.+b,3.+b,10,6.+a,3.+b,3.+b,0,6.+a,3.+b,0,3.+b,6.+a,0,6.+a,6.+a,0,3.+b,3.+b,6.+a,6.+a,3.+b,6.+a,3.+b,6.+a,6.+a,6.+a,6.+a,6.+a,3.+b,3.+b,3.+b,6.+a,3.+b,3.+b,3.+b,6.+a,3.+b,6.+a,6.+a,3.+b,10,0,1.+a,10,0,5.,10,10,1.+a,10,10,5.,10,1.+a,0,10,5.,0,10,8.+b,0,5.,0,0,8.+b,0,0,5.,10,0,8.+b,10,0,10,1.+a,6.+a,10,5.,6.+a,10,8.+b,6.+a,10,1.+a,3.+b,10,3.+b,5.,10,5.,3.+b,10,6.+a,5.,10,8.+b,3.+b,10,3.+b,1.+a,10,6.+a,1.+a,3.+b,0,1.+a,3.+b,0,5.,6.+a,0,1.+a,5.,0,3.+b,6.+a,0,5.,5.,0,6.+a,8.+b,0,3.+b,8.+b,0,6.+a,6.+a,10,1.+a,8.+b,10,3.+b,6.+a,10,5.,8.+b,10,6.+a,3.+b,10,1.+a,5.,10,3.+b,3.+b,10,5.,5.,10,6.+a,3.+b,1.+a,0,5.,3.+b,0,6.+a,1.+a,0,8.+b,3.+b,0,3.+b,5.,0,5.,6.+a,0,6.+a,5.,0,8.+b,6.+a,0,3.+b,8.+b,0,6.+a,8.+b,0,3.+b,1.+a,6.+a,6.+a,1.+a,6.+a,5.,3.+b,6.+a,8.+b,3.+b,6.+a,3.+b,5.,6.+a,6.+a,5.,6.+a,5.,6.+a,6.+a,8.+b,6.+a,6.+a,3.+b,8.+b,6.+a,6.+a,8.+b,6.+a,3.+b,3.+b,5,3.+b,1.+a,3.+b,6.+a,3.+b,5.,6.+a,1.+a,3.+b,5.,3.+b,3.+b,8.+b,3.+b,3.+b,3.+b,6.+a,5.,3.+b,5.,3.+b,6.+a,6.+a,5.,6.+a,5.,3.+b,5.,6.+a,3.+b,8.+b,6.+a,3.+b,3.+b,8.+b,3.+b,6.+a,8.+b,3.+b,3.+b,3.+b,1.+a,6.+a,3.+b,1.+a,3.+b,6.+a,1.+a,6.+a,6.+a,1.+a],111,3)
200         conn=DataArrayInt([30,17,28,32,16,19,29,33,18,83,93,94,58,84,95,96,61,62,85,97,60,30,19,29,33,18,3,12,14,2,84,95,96,61,47,51,50,37,64,86,98,63,30,28,30,34,32,29,31,35,33,87,99,100,93,88,101,102,95,85,89,103,97,30,29,31,35,33,12,13,15,14,88,101,102,95,48,53,52,51,86,90,104,98,30,30,23,22,34,31,21,20,35,91,71,105,99,92,67,106,101,89,72,70,103,30,31,21,20,35,13,5,4,15,92,67,106,101,49,39,54,53,90,68,66,104,30,16,32,24,8,18,33,25,9,94,107,73,57,96,108,75,59,60,97,74,43,30,18,33,25,9,2,14,6,0,96,108,75,59,50,55,40,36,63,98,76,44,30,32,34,26,24,33,35,27,25,100,109,77,107,102,110,79,108,97,103,78,74,30,33,35,27,25,14,15,7,6,102,110,79,108,52,56,41,55,98,104,80,76,30,34,22,10,26,35,20,11,27,105,69,81,109,106,65,82,110,103,70,45,78,30,35,20,11,27,15,4,1,7,106,65,82,110,54,38,42,56,104,66,46,80])
201         connI=DataArrayInt([0,21,42,63,84,105,126,147,168,189,210,231,252])
202         m=MEDCouplingUMesh("mesh",3)
203         m.setConnectivity(conn,connI,True)
204         m.setCoords(coo.deepCopy())# deep copy coo because next line is going to modify it, if it works normaly
205         m.attractSeg3MidPtsAroundNodes(0.1,DataArrayInt([33,35])) # ze call is here !
206         self.assertTrue(not m.getCoords().isEqual(coo,eps)) # some points have had their position changed...
207         ptsExpNotToBeModified=ptsExpToBeModified.buildComplement(len(coo))
208         self.assertTrue(m.getCoords()[ptsExpNotToBeModified].isEqual(coo[ptsExpNotToBeModified],eps))
209         self.assertTrue((m.getCoords()[ptsExpToBeModified]-coo[ptsExpToBeModified]).magnitude().isUniform(4./3.,1e-12))
210         ptsPosExp=DataArrayDouble([6.+a,3.+b,3.+a,6.+a,3.,3.+b,6.+b,3.+b,3.+b,7.,3.+b,3.+b,6.+a,6.+a,3.+a,6.+b,6.+a,3.+b,7.,6.+a,3.+b,6.+a,7.,3.+b,6.+a,3.+b,3.,6.+a,6.+a,3.],10,3)
211         self.assertTrue(m.getCoords()[ptsExpToBeModified].isEqual(ptsPosExp,1e-12))
212         pass
213     
214     pass
215
216 if __name__ == '__main__':
217     unittest.main()