Salome HOME
Move medtool folder to MED base repository
[modules/med.git] / doc / tutorial / atestMEDCouplingNumPy.rst
1
2 .. _python_testMEDCouplingNumPy_solution:
3
4 Playing with NumPy and SciPy
5 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6
7 ::
8         
9         import MEDCoupling as mc
10         
11         #
12         # NumPy
13         #
14         import numpy as np
15         
16         # Checking NumPy binding
17         assert(mc.MEDCouplingHasNumPyBindings())
18         # Playing with conversion and shared data
19         arr = mc.DataArrayDouble(12)
20         arr[:] = 4.
21         nparr = arr.toNumPyArray()
22         print nparr.__repr__()
23         print nparr.tolist()
24         nparr[::2] = 7.
25         print nparr.__repr__()
26         print arr.__repr__()
27         del arr
28         import gc; gc.collect()     # Make sure the object has been deleted
29         print nparr.__repr__()
30         arr2 = mc.DataArrayDouble(nparr)
31         print arr2.__repr__()
32         nparr[:] = 5.
33         print nparr.__repr__()
34         print arr2.__repr__()
35         # Writing to file
36         f = open("toto.data","w+b")
37         a = np.memmap(f,dtype='float64',mode='w+',offset=0,shape=nparr.shape)
38         a[:] = nparr[:]
39         f.flush()
40         # Re-reading file
41         f2 = open("toto.data","r+b")
42         b = np.memmap(f2,dtype='float64',mode='r',offset=0,shape=(12,))
43         a[:] = 3.14
44         f.flush()
45         b = np.memmap(f2,dtype='float64',mode='r',offset=0,shape=(12,))
46         print b.__repr__()
47         #
48         # SciPy
49         #
50         assert(mc.MEDCouplingHasSciPyBindings())
51         c1 = mc.MEDCouplingCMesh()
52         arr1 = mc.DataArrayDouble(7) 
53         arr1.iota() 
54         c1.setCoords(arr1,arr1,arr1)
55         c2 = mc.MEDCouplingCMesh()
56         arr2 = mc.DataArrayDouble(9)
57         arr2.iota() 
58         arr2 *= 6./8.
59         c2.setCoords(arr2,arr2,arr2)
60         c1 = c1.buildUnstructured()
61         c2 = c2.buildUnstructured()
62         c2.translate([6.,0.,0.])
63         c = mc.MEDCouplingUMesh.MergeUMeshes([c1,c2])
64         c.mergeNodes(1e-12)
65         skinAndNCFaces = c.computeSkin()
66         skinAndNCFaces.zipCoords()
67         # Isolating non conform cells
68         from MEDCouplingRemapper import MEDCouplingRemapper
69         rem = MEDCouplingRemapper()
70         rem.setMaxDistance3DSurfIntersect(1e-12)
71         rem.setMinDotBtwPlane3DSurfIntersect(0.99)
72         rem.prepare(skinAndNCFaces,skinAndNCFaces,"P0P0")
73         mat = rem.getCrudeCSRMatrix()
74         indptr = mc.DataArrayInt(mat.indptr)
75         indptr2 = indptr.deltaShiftIndex()
76         cellIdsOfSkin = indptr2.getIdsEqual(1)
77         skin = skinAndNCFaces[cellIdsOfSkin]
78         skin.writeVTK("skin.vtu")