Salome HOME
Fix computation height of isocel triangle with base equal zero : NaN
[tools/medcoupling.git] / doc / tutorial / medloader_advancedAPI1_en.rst
1
2 Reading, Writing a MED file using MEDLoader's advanced API
3 ----------------------------------------------------------
4
5 The advanced API is incarnated by the MEDFile* classes in the MEDLoader library.
6
7 * MEDFileMesh, MEDFileUMesh, MEDFileCMesh
8 * MEDFileMeshes, MEDFileMeshMultiTS
9 * MEDFileField1TS, MEDFileFieldMultiTS
10 * MEDFileFields, MEDFileFieldGlobs
11 * MEDFileData
12
13 Objective
14 ~~~~~~~~~
15
16 Write a mesh and a field from scratch, re-read them and compare the result.
17
18 Topics covered:
19
20 * Read/Write Mesh using MEDLoader's advanced API
21 * Read/Write Field using MEDLoader's advanced API
22
23 Implementation start
24 ~~~~~~~~~~~~~~~~~~~~
25
26 To implement this exercise we use the Python scripting language and import the `medcoupling` Python module. ::
27
28     import medcoupling as mc
29
30
31 Writing and Reading meshes using MEDLoader's advanced API
32 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
33
34 First of all, creation of a mesh "targetMesh". ::
35
36         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 ]
37         targetConn=[0,3,4,1, 1,4,2, 4,5,2, 6,7,4,3, 7,8,5,4]
38         targetMesh=mc.MEDCouplingUMesh.New("MyMesh",2)
39         targetMesh.allocateCells(5)
40         targetMesh.insertNextCell(mc.NORM_TRI3,3,targetConn[4:7])
41         targetMesh.insertNextCell(mc.NORM_TRI3,3,targetConn[7:10])
42         targetMesh.insertNextCell(mc.NORM_QUAD4,4,targetConn[0:4])
43         targetMesh.insertNextCell(mc.NORM_QUAD4,4,targetConn[10:14])
44         targetMesh.insertNextCell(mc.NORM_QUAD4,4,targetConn[14:18])
45         myCoords=mc.DataArrayDouble.New(targetCoords,9,2)
46         targetMesh.setCoords(myCoords)
47         
48
49 .. note:: targetMesh is grouped by geometric type.
50
51 Build "targetMesh1" representing the sub-constituents (faces) of "targetMesh" reduced to cell ids [3,4,7,8]. 
52 ::
53
54         targetMeshConsti=targetMesh.buildDescendingConnectivity()[0]
55         targetMesh1=targetMeshConsti[[3,4,7,8]]
56         targetMesh1.setName(targetMesh.getName())
57
58 .. note:: "targetMesh1" will be recorded as a part of the same global mesh in the MED file, so it must have the same name!
59
60 Then we are ready to write targetMesh and targetMesh1 into TargetMesh2.med. ::
61
62         meshMEDFile=mc.MEDFileUMesh.New()
63         meshMEDFile.setMeshAtLevel(0,targetMesh)
64         meshMEDFile.setMeshAtLevel(-1,targetMesh1)
65         meshMEDFile.write("TargetMesh2.med",2) # 2 stands for write from scratch
66
67 Create 2 groups on level 0. The first called "grp0_Lev0" on cells [0,1,3] and the second called "grp1_Lev0" on cells [1,2,3,4] ::
68
69         grp0_0=mc.DataArrayInt.New([0,1,3]) ; grp0_0.setName("grp0_Lev0")
70         grp1_0=mc.DataArrayInt.New([1,2,3,4]) ; grp1_0.setName("grp1_Lev0")
71         meshMEDFile.setGroupsAtLevel(0,[grp0_0,grp1_0])
72
73 Create 3 groups on level -1. The 1st called "grp0_LevM1" on cells [0,1], the 2nd called "grp1_LevM1" on cells [0,1,2], and the 3rd called "grp2_LevM1" on cells [1,2,3] ::
74
75         grp0_M1=mc.DataArrayInt.New([0,1]) ; grp0_M1.setName("grp0_LevM1")
76         grp1_M1=mc.DataArrayInt.New([0,1,2]) ; grp1_M1.setName("grp1_LevM1")
77         grp2_M1=mc.DataArrayInt.New([1,2,3]) ; grp2_M1.setName("grp2_LevM1")
78         meshMEDFile.setGroupsAtLevel(-1,[grp0_M1,grp1_M1,grp2_M1])
79         
80
81 Then trying to read it. ::
82
83         meshMEDFileRead=mc.MEDFileMesh.New("TargetMesh2.med")
84         meshRead0=meshMEDFileRead.getMeshAtLevel(0)
85         meshRead1=meshMEDFileRead.getMeshAtLevel(-1)
86         print("Is the mesh at level 0 read in file equals targetMesh ? %s"%(meshRead0.isEqual(targetMesh,1e-12)))
87         print("Is the mesh at level -1 read in file equals targetMesh ? %s"%(meshRead1.isEqual(targetMesh1,1e-12)))
88
89 Print available levels for group "grp0_Lev0" ::
90
91         print(meshMEDFileRead.getGrpNonEmptyLevels("grp0_Lev0"))
92
93 Request for cell ids of group "grp0_Lev0" ::
94
95         grp0_0_read=meshMEDFileRead.getGroupArr(0,"grp0_Lev0")
96         print("Is group \"grp0_Lev0\" are the same ? %s"%(grp0_0_read.isEqual(grp0_0)))
97
98 Writing and Reading fields
99 ~~~~~~~~~~~~~~~~~~~~~~~~~~
100
101 Creation of a simple vector field on cells called f.  ::
102
103         f=mc.MEDCouplingFieldDouble.New(mc.ON_CELLS,mc.ONE_TIME)
104         f.setTime(5.6,7,8)
105         f.setArray(targetMesh.computeCellCenterOfMass())
106         f.setMesh(targetMesh)
107         f.setName("AFieldName")
108
109 Put f into a MEDFileField1TS for preparation of MED writing ::
110
111         fMEDFile=mc.MEDFileField1TS.New()
112         fMEDFile.setFieldNoProfileSBT(f)
113
114 Append field to "TargetMesh2.med" ::
115
116         fMEDFile.write("TargetMesh2.med",0) # 0 is very important here because we want to append to TargetMesh2.med and not to overwrite it
117
118 Read it : ::
119
120         fMEDFileRead=mc.MEDFileField1TS.New("TargetMesh2.med",f.getName(),7,8)
121         fRead1=fMEDFileRead.getFieldOnMeshAtLevel(mc.ON_CELLS,0,meshMEDFileRead) # fastest method. No reading of the supporting mesh.
122         fRead2=fMEDFileRead.getFieldAtLevel(mc.ON_CELLS,0) # like above but mesh is re-read from file...
123         print("Does the field f remain the same using fast method ? %s"%(fRead1.isEqual(f,1e-12,1e-12)))
124         print("Does the field f remain the same using slow method ? %s"%(fRead2.isEqual(f,1e-12,1e-12)))
125         
126 Writing and Reading fields on a "profile"
127 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
128
129 Build a reduction on cells [1,2,3] of f and call it fPart. ::
130
131         pfl=mc.DataArrayInt.New([1,2,3]) ; pfl.setName("My1stPfl")
132         fPart=f.buildSubPart(pfl)
133         fPart.setName("fPart")
134
135 Put it into MEDFileField1TS data structure. ::
136
137         fMEDFile2=mc.MEDFileField1TS.New()
138         fMEDFile2.setFieldProfile(fPart,meshMEDFileRead,0,pfl)
139         fMEDFile2.write("TargetMesh2.med",0) # 0 is very important here because we want to append to TargetMesh2.med and not to scratch it
140
141 Read "fPart" field from File "TargetMesh2.med". ::
142
143         fMEDFileRead2=mc.MEDFileField1TS.New("TargetMesh2.med",fPart.getName(),7,8)
144         fPartRead,pflRead=fMEDFileRead2.getFieldWithProfile(mc.ON_CELLS,0,meshMEDFileRead)
145         print(fPartRead.isEqualWithoutConsideringStr(fPart.getArray(),1e-12))
146         print(pflRead.isEqualWithoutConsideringStr(pfl))
147
148 Solution
149 ~~~~~~~~
150
151 :ref:`python_testMEDLoaderAdvancedAPI1_solution`