Salome HOME
eb2f02306c49eaee718d7a9ce079c2d6a6cbdb09
[tools/medcoupling.git] / doc / tutorial / atestMEDLoaderAdvancedAPI1.rst
1
2 .. _python_testMEDLoaderAdvancedAPI1_solution:
3
4 Reading, Writing a MED file using MEDLoader advanced API
5 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6
7 ::
8
9         import MEDLoader as ml
10         # Mesh creation
11         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 ]
12         targetConn = [0,3,4,1, 1,4,2, 4,5,2, 6,7,4,3, 7,8,5,4]
13         targetMesh = ml.MEDCouplingUMesh("MyMesh",2)
14         targetMesh.allocateCells(5)
15         targetMesh.insertNextCell(ml.NORM_TRI3,3,targetConn[4:7])
16         targetMesh.insertNextCell(ml.NORM_TRI3,3,targetConn[7:10])
17         targetMesh.insertNextCell(ml.NORM_QUAD4,4,targetConn[0:4])
18         targetMesh.insertNextCell(ml.NORM_QUAD4,4,targetConn[10:14])
19         targetMesh.insertNextCell(ml.NORM_QUAD4,4,targetConn[14:18])
20         myCoords = ml.DataArrayDouble(targetCoords,9,2)
21         myCoords.setInfoOnComponents(["X [km]","YY [mm]"])
22         targetMesh.setCoords(myCoords)
23         # Build the 2D faces from the 3D volumes (descending connectivity)
24         targetMeshConsti, _, _, _, _ = targetMesh.buildDescendingConnectivity()
25         targetMesh1 = targetMeshConsti[[3,4,7,8]]
26         targetMesh1.setName(targetMesh.getName())
27         #
28         # Meshes
29         #
30         meshMEDFile = ml.MEDFileUMesh()
31         meshMEDFile.setMeshAtLevel(0,targetMesh)
32         meshMEDFile.setMeshAtLevel(-1,targetMesh1)
33         # Some groups on cells Level 0
34         grp0_0 = ml.DataArrayInt([0,1,3]) 
35         grp0_0.setName("grp0_Lev0")
36         grp1_0 = ml.DataArrayInt([1,2,3,4])
37         grp1_0.setName("grp1_Lev0")
38         meshMEDFile.setGroupsAtLevel(0, [grp0_0,grp1_0])
39         # Some groups on cells Level -1
40         grp0_M1 = ml.DataArrayInt([0,1])
41         grp0_M1.setName("grp0_LevM1")
42         grp1_M1 = ml.DataArrayInt([0,1,2])
43         grp1_M1.setName("grp1_LevM1")
44         grp2_M1 = ml.DataArrayInt([1,2,3])
45         grp2_M1.setName("grp2_LevM1")
46         meshMEDFile.setGroupsAtLevel(-1,[grp0_M1,grp1_M1,grp2_M1])      
47         # Write everything
48         meshMEDFile.write("TargetMesh2.med",2) # 2 stands for write from scratch 
49         # Re-read and test equality
50         meshMEDFileRead = ml.MEDFileMesh.New("TargetMesh2.med")  # a new is needed because it returns a MEDFileUMesh (MEDFileMesh is abstract)
51         meshRead0 = meshMEDFileRead.getMeshAtLevel(0)
52         meshRead1 = meshMEDFileRead.getMeshAtLevel(-1)
53         print "Is level 0 in the file equal to 'targetMesh'?", meshRead0.isEqual(targetMesh,1e-12)
54         print "Is level 0 in the file equal to 'targetMesh1'?", meshRead1.isEqual(targetMesh1,1e-12)
55         # Read groups
56         print meshMEDFileRead.getGrpNonEmptyLevels("grp0_Lev0")
57         grp0_0_read = meshMEDFileRead.getGroupArr(0,"grp0_Lev0")
58         print "Is group 'grp0_Lev0' equal to what is read in the file?" , grp0_0_read.isEqual(grp0_0)
59         #
60         # Fields
61         #
62         f = ml.MEDCouplingFieldDouble(ml.ON_CELLS, ml.ONE_TIME)
63         f.setTime(5.6,7,8)
64         f.setArray(targetMesh.computeCellCenterOfMass())
65         f.setMesh(targetMesh)
66         f.setName("AFieldName")
67         # Prepare field for writing
68         fMEDFile = ml.MEDFileField1TS()
69         fMEDFile.setFieldNoProfileSBT(f)     # No profile desired on the field, Sort By Type
70         # *Append* the field to an existing file
71         fMEDFile.write("TargetMesh2.med",0) # 0 is very important here because we want to append to TargetMesh2.med and not to scratch it
72         # Read the field
73         fMEDFileRead = ml.MEDFileField1TS("TargetMesh2.med",f.getName(),7,8)
74         fRead1 = fMEDFileRead.getFieldOnMeshAtLevel(ml.ON_CELLS,0,meshMEDFileRead) # Quickest way, not re-reading mesh in the file.
75         fRead2 = fMEDFileRead.getFieldAtLevel(ml.ON_CELLS,0)                       # Like above, but this time the mesh is read!
76         print "Does the field remain OK with the quick method?", fRead1.isEqual(f,1e-12,1e-12)
77         print "Does the field remain OK with the slow method?", fRead2.isEqual(f,1e-12,1e-12)
78         #
79         # Writing and Reading fields on profile using MEDLoader advanced API
80         #
81         pfl = ml.DataArrayInt([1,2,3]) 
82         pfl.setName("My1stPfl")
83         fPart = f.buildSubPart(pfl)
84         fPart.setName("fPart")
85         #
86         fMEDFile2 = ml.MEDFileField1TS()
87         fMEDFile2.setFieldProfile(fPart,meshMEDFileRead,0,pfl) # 0 is the relative level (here 0 means 3D)
88         fMEDFile2.write("TargetMesh2.med",0) # 0 is paramount to indicate that we *append* (and no overwrite) to the MED file
89         #
90         fMEDFileRead2 = ml.MEDFileField1TS("TargetMesh2.med",fPart.getName(),7,8)
91         fPartRead, pflRead = fMEDFileRead2.getFieldWithProfile(ml.ON_CELLS,0,meshMEDFileRead)
92         print "Is the partial field correclty read?", fPartRead.isEqualWithoutConsideringStr(fPart.getArray(),1e-12)
93         print "Is the list of cell identifiers matching?", pflRead.isEqualWithoutConsideringStr(pfl)
94