]> SALOME platform Git repositories - tools/medcoupling.git/blob - doc/tutorial/medloader_basicAPI1_en.rst
Salome HOME
Fix computation height of isocel triangle with base equal zero : NaN
[tools/medcoupling.git] / doc / tutorial / medloader_basicAPI1_en.rst
1
2 Reading, Writing a MED file using MEDLoader's basic API
3 -------------------------------------------------------
4
5 The basic API is incarnated by the MEDLoader class.
6 All methods in that class are static.
7 The sessions of read/write are done on each call of a method.
8
9 Objective
10 ~~~~~~~~~
11
12 Write a mesh and a field from scratch, re-read them and compare the result.
13
14 Topics covered:
15 * Write using MEDLoader's basic API
16 * Read using MEDLoader's basic API
17
18 Implementation start
19 ~~~~~~~~~~~~~~~~~~~~
20
21 To implement this exercise we use the Python scripting language and import the `medcoupling` Python module. ::
22
23         import medcoupling as mc
24
25 Writing/Reading a mesh
26 ~~~~~~~~~~~~~~~~~~~~~~
27
28 First of all, creation of a mesh "targetMesh". ::
29
30         targetCoords=[-0.3,-0.3, 0.2,-0.3, 0.7,-0.3, -0.3,0.2, 0.2,0.2, 0.7,0.2, -0.3,0.7, 0.2,0.7, 0.7,0.7 ]
31         targetConn=[0,3,4,1, 1,4,2, 4,5,2, 6,7,4,3, 7,8,5,4]
32         targetMesh=mc.MEDCouplingUMesh.New("MyMesh",2)
33         targetMesh.allocateCells(5)
34         targetMesh.insertNextCell(NORM_TRI3,3,targetConn[4:7])
35         targetMesh.insertNextCell(NORM_TRI3,3,targetConn[7:10])
36         targetMesh.insertNextCell(NORM_QUAD4,4,targetConn[0:4])
37         targetMesh.insertNextCell(NORM_QUAD4,4,targetConn[10:14])
38         targetMesh.insertNextCell(NORM_QUAD4,4,targetConn[14:18])
39         myCoords=mc.DataArrayDouble.New(targetCoords,9,2)
40         myCoords.setInfoOnComponents(["X [km]","YY [mm]"])
41         targetMesh.setCoords(myCoords)
42         
43 .. note:: targetMesh is ordered by geometric type.
44
45 We are then ready to write it. ::
46
47         mc.WriteUMesh("TargetMesh.med",targetMesh,True)
48
49 Then trying to read it. ::
50
51         meshRead=mc.ReadUMeshFromFile("TargetMesh.med",targetMesh.getName(),0)
52         print("Is the mesh read in file equals targetMesh? %s"%(meshRead.isEqual(targetMesh,1e-12)))
53
54 Writing/Reading a field on one time step at once
55 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
56 Creation of a vector field "f" on cell supported by "targetMesh". ::
57
58         f=mc.MEDCouplingFieldDouble.New(ON_CELLS,ONE_TIME)
59         f.setTime(5.6,7,8)
60         f.setArray(targetMesh.computeCellCenterOfMass())
61         f.setMesh(targetMesh)
62         f.setName("AFieldName")
63         mc.WriteField("MyFirstField.med",f,True)
64
65 .. note:: Mesh AND Field is written at once into MyFirstField.
66
67 Reading into MyFirstField.med ::
68
69         f2=mc.ReadFieldCell("MyFirstField.med",f.getMesh().getName(),0,f.getName(),7,8)
70         print("Is the field read in file equals f ? %s"%(f2.isEqual(f,1e-12,1e-12)))
71
72 Writing/Reading a field on one or many times steps in "multi-session mode"
73 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
74
75 Here contrary to the previous steps, we are going to write in a multi-session mode on the same MED file.
76 First dealing with the mesh. ::
77
78         mc.WriteUMesh("MySecondField.med",f.getMesh(),True)
79         
80 Then writing only array part of field. ::
81
82         mc.WriteFieldUsingAlreadyWrittenMesh("MySecondField.med",f)
83         
84 Then put a another time step. ::
85
86         f2=f.clone(True)
87         f2.getArray()[:]=2.0
88         f2.setTime(7.8,9,10)
89         mc.WriteFieldUsingAlreadyWrittenMesh("MySecondField.med",f2)
90
91 Now "MySecondField.med" file contains 2 time steps.
92
93 Solution
94 ~~~~~~~~
95
96 :ref:`python_testMEDLoaderBasicAPI1_solution`