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