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