Salome HOME
Fix computation height of isocel triangle with base equal zero : NaN
[tools/medcoupling.git] / doc / tutorial / medcoupling_Read.rst
1
2 Reading a med file
3 -------------------
4
5 Objective
6 ~~~~~~~~~
7
8 The MEDLoader class also allows to read a med file. 
9
10 We will use the case of the 3D cube in order to retrieve all meshes and fields along with value on one node.
11
12 Beginning of implementation
13 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
14
15 To implement this exercice we use the python language script and import the medcoupling module. We need also mathematical functions, so we import the python math module::
16
17     import medcoupling as mc
18         from math import *
19
20
21 Then define some variables::
22
23         medFileName = "MEDCoupling_cube3D.med"
24         MeshName = "3Dcube"
25         FieldName = "field"
26         Field2DName = "fieldBottomFace"
27
28 Retrieving 3D mesh and associated field
29 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
30
31 You need to use MEDLoader API in order to read med file. Read functions need to give the real dimension of the mesh to max.
32 This information is given by a number : 0,-1 or -2.
33
34  * 0 means  the high dimension of the mesh
35  * -1 means the second high dimension of the mesh
36  
37 and the iteration and order of the field. In our case, since there is no iteration, it's -1 for these 2 arguments::
38
39         mesh3D = mc.ReadUMeshFromFile(medFileName,MeshName,0)
40         f = mc.ReadFieldCell(medFileName,mesh3D.getName(),0,FieldName,-1,-1)
41
42
43 Retrieving 2D mesh and associated field
44 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
45
46 Do the same thing for the 2D mesh and the associated field::
47
48         mesh2D = mc.ReadUMeshFromFile(...)
49         f2 = mc.ReadFieldCell(...)
50
51 Retrieving mesh coords
52 ~~~~~~~~~~~~~~~~~~~~~~
53
54 ::
55
56         # Retrieving Coords Mesh
57         Coords3D = mesh3D.getCoords()
58         Values = Coords3D.getValuesAsTuple()
59
60 Retrieving field values
61 ~~~~~~~~~~~~~~~~~~~~~~~~
62
63 ::
64
65         # Retrieving field value on 0 tuple
66         pos= Values[...]
67         res=f.getValueOn(pos)
68
69         # Verify if value is OK
70         bar = mesh3D.computeCellCenterOfMass()
71         x=bar.getIJ(...)
72         y=bar.getIJ(...)
73         z=bar.getIJ(...)
74
75         from math import *
76         d = sqrt(x*x+y*y+z*z)
77         sinus = sin(d)
78
79         if abs(res[0]-sinus)<1.e-5:
80                 print("OK")
81         else:
82                 print("KO")
83
84 Solution
85 ~~~~~~~~
86
87 :ref:`python_testMEDCouplingRead_solution`