Manipulate DataArray objects

DataArrays (DataArrayInt and DataArrayDouble) are used in MEDCoupling to store contiguous arrays in memory. Each array entry is a tuple of data. They form the base of many data processing performed by MEDCoupling. It is thus useful to be able to manipulate them with ease.

DataArrayDouble are often used to manipulate fields in an optimized fashion, as will be shown later on.


In this exercise the aim is to create a set of 2D Cartesian coordinates. The first component will be called X (with unit “m”) and the second Y (same unit). The coordinates represent 7 regular hexagons (all inscribed in a 3cm radius circle). The 7 hexagons are built from a centered reference hexagon (3.4; 4.4).

Around this reference, 6 translated copies are created and each copy shares exactly one edge with the reference. The common nodes (tuples) are then merged. This shows how to use indirections, concept widely used in unstructured meshes.


Concepts at hand:

  • Create an instance of DataArrayDouble
  • Display a DataArrayDouble instance and invoke the getValue() method to convert it to a list
  • Use convenient notations da[…]
  • Learn about renumbering (old2new convention)
  • Invoke services from findCommonTuples

Starting the implementation

To start with, import the whole python module medcoupling and the standard math module.

from medcoupling import *
import math

This makes the following available:

  • all MEDCoupling classes
  • all enum (ON_CELLS, ON_NODES, ONE_TIME…)
  • all static methods

Create a DataArrayDouble instance containing 6 tuples

The target here is to create one DataArrayDouble containing the complete set of coordinates for one regular hexagon.


Which is equivalent to


or to



d contains 12 values grouped in 6 tuples containing each 2 components. The values in d are still un-initialized.

Initialize an instance of DataArrayDouble

Assign the value 3.0 (radius) to the first component of all tuples.


Initialize the 2nd component of each tuple i with the index value i.


Multiply the 2nd component by pi/3.



d now contains the polar coordinates of our regular hexagon centered at 0,0.

Convert from polar to Cartesian by invoking fromPolarToCart(), and assign the result to d.



fromPolarToCart() generates a new instance and d now points to it.

Assign the correct component information of d.:

d.setInfoOnComponents(["X [m]","Y [m]"])

Display the values only in form of a Python list.


Display d.


Verify that for each tuple of d its norm (returned by the magnitude() method) is indeed 3 (with an error less than 1.e-12):


Duplication and aggregation of DataArrayDouble

translationToPerform contains a list of vectors of size 2. This list has length 7 (7 hexagons) Cette liste de taille 7 (7 hexagones) and describes the appropriate translation for each copy of d.

Execute directly the following lines


Create the len(translationToPerform) copies of d and apply the corresponding translation.

for pos,t in enumerate(translationToPerform):

An alternative (and more compact) way to do it :

ds=[d.deepCopy() for i in list(range(len(translationToPerform)))]
for (elt,t) in zip(ds,translationToPerform) : elt+=t

Aggregating DataArrayDouble

From the list of DataArrayDouble instances ‘ds’, construct the aggregation ‘d2’ (a new instance of DataArrayDouble).



d2 now contains the full tuple set (6*7=42 tuples of 2 components each) which was in ds with the same order. This might sound obvious but the aggregation of meshes and fields is based on the exact same principle thus facilitating data access and indexing. This is a key difference to the MED file model, as will be seen later on.

Find identical tuples in d2

d2 contains 42 tuples but some of them are duplicated. To identify those (with a 1e-12 precision) invoke DataArrayDouble.findCommonTuples(). Use help(DataArrayDouble.findCommonTuples) to show the prototype of the method and store the result in c and cI:


c is an array containing all the common nodes grouped sequentially. cI contains the pointers to c allowing to identify all the nodes in a group ‘i’ (with ‘i’ in [0, 12) ). Thus the tuple ids of group ‘i’ start at index cI[i] in the list c, and finish at index cI[i+1].


DataArrayDouble.findCommonTuples() thus returns 2 values: an array containing a list of common tuples, and an index array which is used to navigate in the first list. This is a very classical return signature in MEDCoupling and often appears when manipulating unstructured meshes.

Manipulate DataArrayInt couple representing a pair (Data,IndexData)

The number of common tuples is len(cI)-1, i.e. 12 in our case. Get the list of tuple ids forming the first group of common nodes and store the result in tmp.


Check the result: all the tuples stored in tmp point to identical coordinates in d2.:



we see the tuple (3.,0.) repeated 3 times (with an error margin below 1e-12).

Now we will deduce from the 3 variables oldNbOfTuples, c and cI the number of truly different tuples in d2. To this end we compute the number of repetitions in d2 and we subtract this from oldNbOfTuples.

To get the repetition count invoke the method DataArrayInt.deltaShiftIndex(). It returns the size of each group. Store the result in a.


The number of repetitions in d2 for each group can now be stored in b.


Finally the new count of tuples can be computed as follows.


Build old to new array from c and cI

c and cI define a surjective function from a starting set X containing 42 tuples (oldNbOfTuples) to a set Y containing 24 tuples (myNewNbOfTuples).


This surjective function may also be represented thanks to an “old-2-new” array. This storage method takes the form of a DataArrayInt ‘o2n’ made of Card(X) tuples (in our case 42) with one component each. For each tuple (element) indexed by i in o2n, o2n[i] contains the new tuple id in Y.

The format ‘old-2-new’ is systematically used for all renumbering operations (one-to-one correspondence).

The static method DataArrayInt.ConvertIndexArrayToO2N() performs the conversion from one storage mode to the other (c, cI to o2n). We get for free the number of elements in Y, i.e. the variable newNbOfTuples.

print("Have I got the right result? %s"%(str(myNewNbOfTuples==newNbOfTuples)))

Using o2n and newNbOfTuples invoke DataArrayDouble.renumberAndReduce() on d2.


This method has one drawback: we don’t know for each group of common tuples in d2 what id was finally retained. For example: in the group 0 we know that tuples 0, 8 and 16 are identical (tmp.getValues()), but we don’t know which of the three has been retained in d3.

To make this choice explicit we use the new-2-old format. This storage mode is represented by a DataArrayInt n2o containing Card(Y) tuples (in our case 24) with one component each. For each tuple (element) with id ‘i’ in n2o, n2o[i] contains the tuple id which was chosen in X.

The method DataArrayInt.invertArrayO2N2N2O() allows to switch between the two representations. Try it on the variable o2n.


Using n2o we can deduce d3_bis from d2.

print("Have I got the right result (2)? %s"%(str(d3.isEqual(d3_bis,1e-12))))

Translate all tuples at once

All tuples (or nodes) are to be translated by the vector [3.3,4.4].


Build an unstructured mesh using d3 (coordinates) and o2n

Create an unstructured mesh m with coordinates d3. m has a mesh dimension of 2


Now allocate the number of cells with an upper bound of the actual number of cells.


Finally thanks to o2n we know the connectivity of all 7 hexagons using the coordinates stored in d3.

for i in list(range(7)):

Check that m is coherent.


To visually check m, write it in a VTU file (“My7hexagons.vtu”) and display it in ParaVis.



We write the data in VTU file, not in a MED file, because MEDCoupling doesn’t use the MED file pre-requisite. Also note that although we output VTK data, MEDCoupling in itself doesn’t depend on the VTK libraries.