Salome HOME
d954bfdab8313707f1a652c092d8917149183aeb
[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 and MEDLoader parts of the MED module. We need also mathematical functions, so we import the python math module::
16
17         from MEDCoupling import *
18         from MEDLoader import *
19         from math import *
20
21
22 Then define some variables::
23
24         medFileName = "MEDCoupling_cube3D.med"
25         MeshName = "3Dcube"
26         FieldName = "field"
27         Field2DName = "fieldBottomFace"
28
29 Retrieving 3D mesh and associated field
30 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
31
32 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.
33 This information is given by a number : 0,-1 or -2.
34
35  * 0 means  the high dimension of the mesh
36  * -1 means the second high dimension of the mesh
37  
38 and the iteration and order of the field. In our case, since there is no iteration, it's -1 for these 2 arguments::
39
40         mesh3D = MEDLoader.ReadUMeshFromFile(medFileName,MeshName,0)
41         f = MEDLoader.ReadFieldCell(medFileName,mesh3D.getName(),0,FieldName,-1,-1)
42
43
44 Retrieving 2D mesh and associated field
45 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
46
47 Do the same thing for the 2D mesh and the associated field::
48
49         mesh2D = MEDLoader.ReadUMeshFromFile(...)
50         f2 = MEDLoader.ReadFieldCell(...)
51
52 Retrieving mesh coords
53 ~~~~~~~~~~~~~~~~~~~~~~
54
55 ::
56
57         # Retrieving Coords Mesh
58         Coords3D = mesh3D.getCoords()
59         Values = Coords3D.getValuesAsTuple()
60
61 Retrieving field values
62 ~~~~~~~~~~~~~~~~~~~~~~~~
63
64 ::
65
66         # Retrieving field value on 0 tuple
67         pos= Values[...]
68         res=f.getValueOn(pos)
69
70         # Verify if value is OK
71         bar = mesh3D.getBarycenterAndOwner()
72         x=bar.getIJ(...)
73         y=bar.getIJ(...)
74         z=bar.getIJ(...)
75
76         from math import *
77         d = sqrt(x*x+y*y+z*z)
78         sinus = sin(d)
79
80         if abs(res[0]-sinus)<1.e-5:
81                 print "OK"
82         else:
83                 print "KO"
84
85 Solution
86 ~~~~~~~~
87
88 :ref:`python_testMEDCouplingRead_solution`