Salome HOME
extract medtool doc from MED
[modules/med.git] / medtool / doc / tutorial / atestMEDCouplingDataArray1.rst
1
2 .. _python_testMEDCouplingdataarray1_solution:
3
4 Playing with regular hexagons using DataArrayDouble
5 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6
7 ::
8
9         import MEDCoupling as mc 
10         import math
11         # Building the coordinates of the initial hexagon, centered at 0,0
12         d = mc.DataArrayDouble(6,2)
13         d[:,0] = 3.
14         d[:,1] = range(6)
15         d[:,1] *= math.pi/3.
16         d = d.fromPolarToCart()
17         d.setInfoOnComponents(["X [m]","Y [m]"])
18         print d.getValues()
19         print d
20         print "Uniform array?", d.magnitude().isUniform(3.,1e-12)
21         # Translating the 7 hexagons with a translation
22         radius = 3.
23         translationToPerform = [[0.,0.],[3./2.*radius,-radius*math.sqrt(3.)/2],[3./2.*radius,radius*math.sqrt(3.)/2],[0.,radius*math.sqrt(3.)],[-3./2.*radius,radius*math.sqrt(3.)/2],[-3./2.*radius,-radius*math.sqrt(3.)/2],[0.,-radius*math.sqrt(3.)]]
24         ds = len(translationToPerform)*[None]
25         for pos,t in enumerate(translationToPerform):
26                          ds[pos] = d[:]         # Perform a deep copy of d and place it at position 'pos' in ds
27                          ds[pos] += t             # Adding a vector to a set of coordinates does a translation
28                          pass
29         # Identifying duplicate tuples
30         d2 = mc.DataArrayDouble.Aggregate(ds)
31         oldNbOfTuples = d2.getNumberOfTuples()
32         c,cI = d2.findCommonTuples(1e-12)
33         tmp = c[cI[0]:cI[0+1]]
34         print tmp
35         a = cI.deltaShiftIndex()
36         b = a - 1
37         myNewNbOfTuples = oldNbOfTuples - sum(b.getValues())
38         o2n, newNbOfTuples = mc.DataArrayInt.BuildOld2NewArrayFromSurjectiveFormat2(oldNbOfTuples,c,cI)
39         print "Have I got the right number of tuples?"
40         print "myNewNbOfTuples = %d, newNbOfTuples = %d" % (myNewNbOfTuples, newNbOfTuples)
41         assert(myNewNbOfTuples == newNbOfTuples)
42         # Extracting the unique set of tuples 
43         d3 = d2.renumberAndReduce(o2n, newNbOfTuples)
44         n2o = o2n.invertArrayO2N2N2O(newNbOfTuples)
45         d3_bis = d2[n2o]
46         print "Are d3 and d3_bis equal ? %s" % (str(d3.isEqual(d3_bis, 1e-12)))
47         # Now translate everything
48         d3 += [3.3,4.4]
49         # And build an unstructured mesh representing the final pattern
50         m = mc.MEDCouplingUMesh("My7hexagons",2)
51         m.setCoords(d3)
52         print "Mesh dimension is", m.getMeshDimension()
53         print "Spatial dimension is", m.getCoords().getNumberOfComponents()
54         m.allocateCells(7)
55         for i in xrange(7):
56                 cell_connec = o2n[6*i:6*(i+1)]
57                 m.insertNextCell(mc.NORM_POLYGON, cell_connec.getValues())
58                 pass
59         # Check that everything is coherent (will throw if not)
60         m.checkCoherency()
61         # Write the result into a VTU file that can be read with ParaView
62         m.writeVTK("My7hexagons.vtu")
63