2 .. _python_testMEDCouplingdataarray1_solution:
4 Playing with regular hexagons using DataArrayDouble
5 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9 import MEDCoupling as mc
11 # Building the coordinates of the initial hexagon, centered at 0,0
12 d = mc.DataArrayDouble(6,2)
16 d = d.fromPolarToCart()
17 d.setInfoOnComponents(["X [m]","Y [m]"])
20 print "Uniform array?", d.magnitude().isUniform(3.,1e-12)
21 # Translating the 7 hexagons with a translation
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
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]]
35 a = cI.deltaShiftIndex()
37 myNewNbOfTuples = oldNbOfTuples - sum(b.getValues())
38 o2n, newNbOfTuples = mc.DataArrayInt.ConvertIndexArrayToO2N(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)
46 print "Are d3 and d3_bis equal ? %s" % (str(d3.isEqual(d3_bis, 1e-12)))
47 # Now translate everything
49 # And build an unstructured mesh representing the final pattern
50 m = mc.MEDCouplingUMesh("My7hexagons",2)
52 print "Mesh dimension is", m.getMeshDimension()
53 print "Spatial dimension is", m.getCoords().getNumberOfComponents()
56 cell_connec = o2n[6*i:6*(i+1)]
57 m.insertNextCell(mc.NORM_POLYGON, cell_connec.getValues())
59 # Check that everything is coherent (will throw if not)
60 m.checkConsistencyLight()
61 # Write the result into a VTU file that can be read with ParaView
62 m.writeVTK("My7hexagons.vtu")