def testFieldNodeProfilRW2(self): fileName = "Pyfile23.med" mesh = MEDLoaderDataForTest.build3DSurfMesh_1() MEDLoader.WriteUMesh(fileName, mesh, True) # f1 = MEDLoader.MEDCouplingFieldDouble.New(MEDLoader.ON_NODES, MEDLoader.ONE_TIME) f1.setName("FieldMix") f1.setMesh(mesh) arr2 = [ 1071., 1171., 1010., 1110., 1020., 1120., 1030., 1130., 1040., 1140., 1050., 1150., 1060., 1160., 1070., 1170., 1080., 1180., 1090., 1190., 1091., 1191., 1092., 1192. ] array = MEDLoader.DataArrayDouble.New() array.setValues(arr2, 12, 2) f1.setArray(array) array.setInfoOnComponent(0, "plkj [mm]") array.setInfoOnComponent(1, "pqqqss [mm]") tmp = array.getPointer() f1.setTime(3.17, 2, 7) # renumArr = [3, 7, 2, 1, 5, 11, 10, 0, 9, 6, 8, 4] f1.renumberNodes(renumArr) f1.checkConsistencyLight() MEDLoader.WriteField(fileName, f1, False) #<- False important for the test f2 = MEDLoader.ReadFieldNode(fileName, f1.getMesh().getName(), 0, f1.getName(), 2, 7) self.assertTrue(f2.isEqual(f1, 1e-12, 1e-12)) # pass
def testMesh3DRW(self): mesh = MEDLoaderDataForTest.build3DMesh_1() mesh.checkConsistencyLight() MEDLoader.WriteUMesh("Pyfile5.med", mesh, True) mesh_rw = MEDLoader.ReadUMeshFromFile("Pyfile5.med", mesh.getName(), 0) self.assertTrue(mesh.isEqual(mesh_rw, 1e-12)) pass
def testFieldGaussRW1(self): fileName = "Pyfile13.med" f1 = MEDLoaderDataForTest.buildVecFieldOnGauss_1() MEDLoader.WriteField(fileName, f1, True) f2 = MEDLoader.ReadField(MEDLoader.ON_GAUSS_PT, fileName, f1.getMesh().getName(), 0, f1.getName(), 1, 5) self.assertTrue(f1.isEqual(f2, 1e-12, 1e-12)) pass
def testMesh3DSurfShuffleRW(self): fileName = "Pyfile15.med" mesh = MEDLoaderDataForTest.build3DSurfMesh_1() renumber1 = [2, 5, 1, 0, 3, 4] mesh.renumberCells(renumber1, False) mesh.checkConsistencyLight() MEDLoader.WriteUMesh(fileName, mesh, True) mesh_rw = MEDLoader.ReadUMeshFromFile(fileName, mesh.getName(), 0) self.assertTrue(mesh.isEqual(mesh_rw, 1e-12)) pass
def testFieldGaussNERW1(self): fileName = "Pyfile14.med" f1 = MEDLoaderDataForTest.buildVecFieldOnGaussNE_1() MEDLoader.WriteField(fileName, f1, True) self.assertEqual( [MEDLoader.ON_GAUSS_NE], MEDLoader.GetTypesOfField(fileName, '2DMesh_2', 'MyFieldOnGaussNE')) #Bug 22/5/2014 f2 = MEDLoader.ReadField(MEDLoader.ON_GAUSS_NE, fileName, f1.getMesh().getName(), 0, f1.getName(), 1, 5) self.assertTrue(f1.isEqual(f2, 1e-12, 1e-12)) pass
def read_typ3(fichier, nom_med): with open(fichier, "r") as fic: lines = fic.readlines() nb_som = int(lines[9].strip()) nb_cel = int(lines[11].strip()) nb_fac = int(lines[13].strip()) som = [map(float, line.split()) for line in lines[17:17 + nb_som]] # les connectivites ne partent pas de 0... s = read_between(fichier, ["Volumes->Faces"], ["Volumes->Vertices"]) tmp_cel = connectivity_from_string(s) # tmp_cel = [map(int, line.split()[1:]) for line in lines[18 + nb_som:18 + nb_som + nb_cel]] cel = [[p - 1 for p in c] for c in tmp_cel] s = read_between(fichier, ["Faces->Vertices"], ["Faces->Control volumes", "Faces->volumes"]) tmp_fac = connectivity_from_string(s) # tmp_fac = [map(int, line.split()[1:]) for line in lines[21 + nb_som + 2 * nb_cel + nb_fac:21 + nb_som + 2 * nb_cel + 2 * nb_fac]] fac = [[p - 1 for p in c] for c in tmp_fac] mesh = ml.MEDCouplingUMesh("mesh", 3) mesh.allocateCells(len(cel)) for e in range(len(cel)): con = [] for face in cel[e]: con.extend(fac[face]) con.append(-1) mesh.insertNextCell(ml.NORM_POLYHED, con[:-1]) mesh.finishInsertingCells() pts = [] for p in som: pts.extend(p) co = ml.DataArrayDouble(pts, len(pts) / 3, 3) mesh.setCoords(co) mf, d, di, r, ri = mesh.buildDescendingConnectivity() mf.setName("mesh") mm = ml.MEDFileUMesh.New() mm.setMeshAtLevel(0, mesh) mm.setMeshAtLevel(-1, mf) g = [] nb_vois = ri.deltaShiftIndex() for i in range(mf.getNumberOfCells()): if nb_vois[i] == 1: g.append(i) grp = ml.DataArrayInt.New(g) grp.setName("boundary") mm.addGroup(-1, grp) mm.write("{}/mesh.med".format(nom_med), 2)
def ConvertTo30(nameOfMEDFile): fn,ext=os.path.splitext(nameOfMEDFile) assert(ext in [".med",".rmed"]) realFnOut=fn+"_30.med" # initalVersion=ml.MEDFileVersionOfFileStr(nameOfMEDFile) # mfd=ml.MEDFileData(nameOfMEDFile) mfd.write30(realFnOut,2) # finalVersion=ml.MEDFileVersionOfFileStr(realFnOut) # print(("File \"%s\" has been converted to 3.0 successfuly ( %s -> %s ) !\nOutput file is here : \"%s\" !"%(fn,initalVersion,finalVersion,realFnOut))) pass
def testMultiMeshTypeWrite0(self): fname = "Pyfile73.med" m = MEDLoader.MEDCoupling1SGTUMesh("mesh", MEDLoader.NORM_QUAD4) m.allocateCells() m.insertNextCell([0, 2, 1, 3]) m.setCoords( MEDLoader.DataArrayDouble([0., 0., 1., 1., 1., 0., 0., 1.], 4, 2)) # ms = [m.deepCpy() for i in xrange(4)] for i, elt in enumerate(ms): elt.translate([float(i) * 1.5, 0.]) pass # m0 = MEDLoader.MEDCoupling1SGTUMesh.Merge1SGTUMeshes(ms) f = m0.getMeasureField(False) f.getArray().setInfoOnComponents(["ABC [defg]"]) MEDLoader.MEDLoader.WriteField(fname, f, True) # fRead = MEDLoader.MEDLoader.ReadFieldCell(fname, "merge", 0, f.getName(), -1, -1) fRead.setMesh(MEDLoader.MEDCoupling1SGTUMesh(fRead.getMesh())) self.assertTrue(f.isEqual(fRead, 1e-12, 1e-12)) # m0 = m0.buildUnstructured() m0.convertAllToPoly() m0 = MEDLoader.MEDCoupling1DGTUMesh(m0) f = m0.getMeasureField(False) f.getArray().setInfoOnComponents(["ABC [defg]"]) MEDLoader.MEDLoader.WriteField(fname, f, True) # fRead = MEDLoader.MEDLoader.ReadFieldCell(fname, "merge", 0, f.getName(), -1, -1) fRead.setMesh(MEDLoader.MEDCoupling1DGTUMesh(fRead.getMesh())) self.assertTrue(f.isEqual(fRead, 1e-12, 1e-12)) # m0 = MEDLoader.MEDCouplingCMesh() arr = MEDLoader.DataArrayDouble(4) arr.iota() m0.setCoords(arr, arr) m0.setName("mesh") f = m0.getMeasureField(False) f.getArray().setInfoOnComponents(["ABC [defg]"]) MEDLoader.MEDLoader.WriteField(fname, f, True) # fRead = MEDLoader.MEDLoader.ReadFieldCell(fname, "mesh", 0, f.getName(), -1, -1) self.assertTrue(f.isEqual(fRead, 1e-12, 1e-12)) # c = m0.buildUnstructured().getCoords() m0 = MEDLoader.MEDCouplingCurveLinearMesh("mesh") m0.setNodeGridStructure([4, 4]) m0.setCoords(c) f = m0.getMeasureField(False) f.getArray().setInfoOnComponents(["ABC [defg]"]) MEDLoader.MEDLoader.WriteField(fname, f, True) # fRead = MEDLoader.MEDLoader.ReadFieldCell(fname, "mesh", 0, f.getName(), -1, -1) self.assertTrue(f.isEqual(fRead, 1e-12, 1e-12)) pass
def testFieldRW1(self): f1 = MEDLoaderDataForTest.buildVecFieldOnCells_1() MEDLoader.WriteField("Pyfile6.med", f1, True) f2 = MEDLoader.ReadFieldCell("Pyfile6.med", f1.getMesh().getName(), 0, f1.getName(), 0, 1) self.assertTrue(f1.isEqual(f2, 1e-12, 1e-12)) # f1 = MEDLoaderDataForTest.buildVecFieldOnNodes_1() MEDLoader.WriteField("Pyfile7.med", f1, True) f2 = MEDLoader.ReadFieldNode("Pyfile7.med", f1.getMesh().getName(), 0, f1.getName(), 2, 3) self.assertTrue(f1.isEqual(f2, 1e-12, 1e-12)) self.assertRaises(Exception, MEDLoader.ReadFieldCell, "Pyfile7.med", f1.getMesh().getName(), 0, f1.getName(), 2, 3) pass
def testMultiMeshTypeWrite1(self): fname = "Pyfile74.med" m = MEDLoader.MEDCoupling1SGTUMesh("mesh", MEDLoader.NORM_QUAD4) m.allocateCells() m.insertNextCell([0, 2, 1, 3]) m.setCoords( MEDLoader.DataArrayDouble([0., 0., 1., 1., 1., 0., 0., 1.], 4, 2)) # ms = [m.deepCpy() for i in xrange(4)] for i, elt in enumerate(ms): elt.translate([float(i) * 1.5, 0.]) pass m0 = MEDLoader.MEDCoupling1SGTUMesh.Merge1SGTUMeshes(ms) MEDLoader.MEDLoader.WriteMesh(fname, m0, True) # mRead = MEDLoader.MEDLoader.ReadMeshFromFile(fname, "merge", 0) self.assertTrue(isinstance(mRead, MEDLoader.MEDCouplingUMesh)) mRead = MEDLoader.MEDCoupling1SGTUMesh(mRead) self.assertTrue(m0.isEqual(mRead, 1e-12)) # m0 = m0.buildUnstructured() m0.convertAllToPoly() m0 = MEDLoader.MEDCoupling1DGTUMesh(m0) MEDLoader.MEDLoader.WriteMesh(fname, m0, True) # mRead = MEDLoader.MEDLoader.ReadMeshFromFile(fname, "merge", 0) mRead = MEDLoader.MEDCoupling1DGTUMesh(mRead) self.assertTrue(m0.isEqual(mRead, 1e-12)) # m0 = MEDLoader.MEDCouplingCMesh() arr = MEDLoader.DataArrayDouble(4) arr.iota() m0.setCoords(arr, arr) m0.setName("mesh") MEDLoader.MEDLoader.WriteMesh(fname, m0, True) # mRead = MEDLoader.MEDLoader.ReadMeshFromFile(fname, 0) self.assertTrue(isinstance(mRead, MEDLoader.MEDCouplingCMesh)) self.assertTrue(m0.isEqual(mRead, 1e-12)) # c = m0.buildUnstructured().getCoords() m0 = MEDLoader.MEDCouplingCurveLinearMesh("mesh") m0.setNodeGridStructure([4, 4]) m0.setCoords(c) MEDLoader.MEDLoader.WriteMesh(fname, m0, True) # mRead = MEDLoader.MEDLoader.ReadMeshFromFile(fname, 0) self.assertTrue(isinstance(mRead, MEDLoader.MEDCouplingCurveLinearMesh)) self.assertTrue(m0.isEqual(mRead, 1e-12)) pass
def testGetAllFieldNamesRW1(self): fileName = "Pyfile22.med" mesh = MEDLoaderDataForTest.build2DMesh_2() f1 = MEDLoader.MEDCouplingFieldDouble.New(MEDLoader.ON_NODES, MEDLoader.ONE_TIME) f1.setName("Field1") f1.setTime(3.44, 5, 6) f1.setMesh(mesh) f1.fillFromAnalytic(2, "x+y") MEDLoader.WriteField(fileName, f1, True) f1.setTime(1002.3, 7, 8) f1.fillFromAnalytic(2, "x+77.*y") MEDLoader.WriteFieldUsingAlreadyWrittenMesh(fileName, f1) f1.setName("Field2") MEDLoader.WriteField(fileName, f1, False) f1.setName("Field3") mesh.setName("2DMesh_2Bis") MEDLoader.WriteField(fileName, f1, False) f1 = MEDLoader.MEDCouplingFieldDouble.New(MEDLoader.ON_CELLS, MEDLoader.ONE_TIME) f1.setName("Field8") f1.setTime(8.99, 7, 9) f1.setMesh(mesh) f1.fillFromAnalytic(3, "3*x+y") MEDLoader.WriteField(fileName, f1, False) fs = MEDLoader.GetAllFieldNames(fileName) self.assertEqual(4, len(fs)) self.assertTrue(fs[0] == "Field1") self.assertTrue(fs[1] == "Field2") self.assertTrue(fs[2] == "Field3") self.assertTrue(fs[3] == "Field8") pass
def testWriteUMeshesRW1(self): fileName = "Pyfile18.med" m3d = MEDLoaderDataForTest.build3DMesh_2() pt = [0., 0., -0.3] vec = [0., 0., 1.] nodes = m3d.findNodesOnPlane(pt, vec, 1e-12) m2d = m3d.buildFacePartOfMySelfNode(nodes, True) renumber = [1, 2, 0, 4, 3] m2d.renumberCells(renumber, False) m2d.setName("ExampleOfMultiDimW") meshes = [m2d, m3d] MEDLoader.WriteUMeshes(fileName, meshes, True) m3d_bis = MEDLoader.ReadUMeshFromFile(fileName, m2d.getName(), 0) self.assertTrue(not m3d_bis.isEqual(m3d, 1e-12)) m3d_bis.setName(m3d.getName()) self.assertTrue(m3d_bis.isEqual(m3d, 1e-12)) m2d_bis = MEDLoader.ReadUMeshFromFile(fileName, m2d.getName(), -1) #-1 for faces self.assertTrue(m2d_bis.isEqual(m2d, 1e-12)) # Creation of a field on faces. f1 = MEDLoader.MEDCouplingFieldDouble.New(MEDLoader.ON_CELLS, MEDLoader.ONE_TIME) f1.setName("FieldOnFacesShuffle") f1.setMesh(m2d) array = MEDLoader.DataArrayDouble.New() arr1 = [71., 171., 10., 110., 20., 120., 30., 130., 40., 140.] array.setValues(arr1, m2d.getNumberOfCells(), 2) array.setInfoOnComponent(0, "plkj [mm]") array.setInfoOnComponent(1, "pqqqss [mm]") f1.setArray(array) tmp = array.setValues(arr1, m2d.getNumberOfCells(), 2) f1.setTime(3.14, 2, 7) f1.checkConsistencyLight() MEDLoader.WriteFieldUsingAlreadyWrittenMesh(fileName, f1) f2 = MEDLoader.ReadFieldCell(fileName, f1.getMesh().getName(), -1, f1.getName(), 2, 7) self.assertTrue(f2.isEqual(f1, 1e-12, 1e-12)) pass
def read_typ2(fichier, nom_med): with open(fichier, "r") as fic: lines = fic.readlines() nb_som = int(lines[1].strip()) nb_cel = int(lines[3 + nb_som].strip()) som = [map(float, line.split()) for line in lines[2:2 + nb_som]] # les connectivites ne partent pas de 0... tmp_cel = [map(int, line.split()[1:]) for line in lines[4 + nb_som:]] cel = [[p - 1 for p in c] for c in tmp_cel] mesh = ml.MEDCouplingUMesh("mesh", 2) mesh.allocateCells(len(cel)) for p in cel: mesh.insertNextCell(ml.NORM_POLYGON, len(p), p) mesh.finishInsertingCells() pts = [] for p in som: pts.extend(p) co = ml.DataArrayDouble(pts, len(pts) / 2, 2) mesh.setCoords(co) mf, d, di, r, ri = mesh.buildDescendingConnectivity() mf.setName("mesh") mm = ml.MEDFileUMesh.New() mm.setMeshAtLevel(0, mesh) mm.setMeshAtLevel(-1, mf) g = [] nb_vois = ri.deltaShiftIndex() for i in range(mf.getNumberOfCells()): if nb_vois[i] == 1: g.append(i) grp = ml.DataArrayInt.New(g) grp.setName("boundary") mm.addGroup(-1, grp) mm.write("{}/mesh.med".format(nom_med), 2)
def testMultiFieldShuffleRW1(self): fileName = "Pyfile17.med" m = MEDLoaderDataForTest.build3DMesh_2() self.assertEqual(20, m.getNumberOfCells()) self.assertEqual(45, m.getNumberOfNodes()) polys = [1, 4, 6] m.convertToPolyTypes(polys) renum = [ 1, 3, 2, 8, 9, 12, 13, 16, 19, 0, 4, 7, 5, 15, 14, 17, 10, 18, 6, 11 ] m.renumberCells(renum, False) m.orientCorrectlyPolyhedrons() # Writing MEDLoader.WriteUMesh(fileName, m, True) f1Tmp = m.getMeasureField(False) f1 = f1Tmp.buildNewTimeReprFromThis(MEDLoader.ONE_TIME, False) f1.setTime(0., 1, 2) f_1 = f1.cloneWithMesh(True) MEDLoader.WriteFieldUsingAlreadyWrittenMesh(fileName, f1) f1.applyFunc("2*x") f1.setTime(0.01, 3, 4) f_2 = f1.cloneWithMesh(True) MEDLoader.WriteFieldUsingAlreadyWrittenMesh(fileName, f1) f1.applyFunc("2*x/3") f1.setTime(0.02, 5, 6) f_3 = f1.cloneWithMesh(True) MEDLoader.WriteFieldUsingAlreadyWrittenMesh(fileName, f1) # Reading its = [(1, 2), (3, 4), (5, 6)] fs = MEDLoader.ReadFieldsOnSameMesh(MEDLoader.ON_CELLS, fileName, f_1.getMesh().getName(), 0, f_1.getName(), its) self.assertEqual(3, len(fs)) self.assertTrue(fs[0].isEqual(f_1, 1e-12, 1e-12)) self.assertTrue(fs[1].isEqual(f_2, 1e-12, 1e-12)) self.assertTrue(fs[2].isEqual(f_3, 1e-12, 1e-12)) pass
def testFieldProfilRW1(self): fileName = "Pyfile12.med" mesh1 = MEDLoaderDataForTest.build3DMesh_1() da, b, newNbOfNodes = mesh1.mergeNodes(1e-12) MEDLoader.WriteUMesh(fileName, mesh1, True) part1 = [1, 2, 4, 13, 15] mesh2 = mesh1.buildPartOfMySelf(part1, True) mesh2.setName(mesh1.getName()) #<- important for the test # nbOfCells = mesh2.getNumberOfCells() self.assertEqual(5, nbOfCells) f1 = MEDLoader.MEDCouplingFieldDouble.New(MEDLoader.ON_CELLS, MEDLoader.ONE_TIME) f1.setName("VectorFieldOnCells") f1.setMesh(mesh2) array = MEDLoader.DataArrayDouble.New() array.alloc(nbOfCells, 2) f1.setArray(array) arr1 = [71., 171., 10., 110., 20., 120., 30., 130., 40., 140.] array.setValues(arr1, nbOfCells, 2) f1.setTime(3.14, 2, 7) f1.checkConsistencyLight() # MEDLoader.WriteField(fileName, f1, False) #<- False important for the test # f2 = MEDLoader.ReadFieldCell(fileName, f1.getMesh().getName(), 0, f1.getName(), 2, 7) tt = MEDLoader.GetTypesOfField(fileName, f1.getMesh().getName(), f1.getName()) self.assertEqual(tt, [MEDLoader.ON_CELLS]) f2.checkConsistencyLight() self.assertTrue(f1.isEqual(f2, 1e-12, 1e-12)) # pass
def testFieldWithTooLongName(self): """ This test is a non regression test, to check that in basic API the policies are taken into account. """ fname = "Pyfile75.med" # Coordinates coords = [0., 0., 0., 1., 1., 1., 1., 0.] # lvl 0 connectivity conn2D = [1, 2, 3, 4] # lvl 0 mesh m = MEDLoader.MEDCouplingUMesh.New("mesh", 2) m.allocateCells(1) m.insertNextCell(MEDLoader.NORM_QUAD4, 4, conn2D) m.finishInsertingCells() # assigning coordinates meshCoords = MEDLoader.DataArrayDouble.New() meshCoords.setValues(coords, 4, 2) m.setCoords(meshCoords) # f = MEDLoader.MEDCouplingFieldDouble.New(MEDLoader.ON_CELLS, MEDLoader.ONE_TIME) f.setMesh(m) d = MEDLoader.DataArrayDouble.New() d.alloc(1, 1) d.iota(1.) # seting a long name d.setInfoOnComponent(0, "CONCENTRATION of I129") f.setArray(d) f.setName("field") # mm = MEDLoader.MEDFileUMesh() MEDLoader.MEDLoader.SetTooLongStrPolicy(2) MEDLoader.MEDLoader.AssignStaticWritePropertiesTo(mm) self.assertEqual(2, mm.getTooLongStrPolicy()) MEDLoader.MEDLoader.SetTooLongStrPolicy(0) MEDLoader.MEDLoader.AssignStaticWritePropertiesTo(mm) self.assertEqual(0, mm.getTooLongStrPolicy()) del mm # MEDLoader.MEDLoader.SetTooLongStrPolicy(2) self.assertRaises( MEDLoader.InterpKernelException, MEDLoader.MEDLoader.WriteField, fname, f, True) # the component name is too long + policy 2 -> throw f.getArray().setInfoOnComponent(0, 'I129') MEDLoader.MEDLoader.WriteField(fname, f, True) pass
def image2med(self, imageFilepath, medFilepath=None): # Load the image file in a numpy array using PIL. img = Image.open(imageFilepath) imgbw = ImageOps.grayscale(img) # WARN: We keep only the grayscale. Maybe, it could be useful # to get the RGB scales each on one component of the field. # Creating a cartesian mesh with a grid of the size of the image # The sizes defined the number of pixel in a direction, then the # number of cells to create in the mesh in that direction. width, height = imgbw.size mesh = self.createMesh("grid_%sx%s" % (width, height), width, height) field = self.createField("imagefield", mesh, imgbw) # The MEDLoader can be used to save all the stuff in a med file. You # just have to specify the field and the MEDLoader will save the # underlying mesh. createFromScratch = True ML.WriteField(medFilepath, field, createFromScratch)
def testBigNbOfCompoNonReg(self): fileName = "Pyfile57.med" m = MEDLoader.MEDCouplingCMesh() m.setCoords(MEDLoader.DataArrayDouble([0, 1, 2, 3]), MEDLoader.DataArrayDouble([0, 1]), MEDLoader.DataArrayDouble([0, 1])) m = m.buildUnstructured() m.setName("TinyMesh") f = MEDLoader.MEDCouplingFieldDouble(MEDLoader.ON_CELLS) f.setMesh(m) nbOfCompo = 4100 arr = MEDLoader.DataArrayDouble(nbOfCompo * 3) arr.iota() arr.rearrange(nbOfCompo) arr.setInfoOnComponents(["c%i" % (i) for i in range(nbOfCompo)]) f.setArray(arr) f.setName("FieldBigCompo") MEDLoader.WriteField(fileName, f, True) f2 = MEDLoader.ReadFieldCell(fileName, m.getName(), 0, f.getName(), -1, -1) self.assertTrue(f.isEqual(f2, 1e-12, 1e-12)) pass
def testFieldNodeProfilRW1(self): fileName = "Pyfile19.med" fileName2 = "Pyfile20.med" m = MEDLoaderDataForTest.build2DMesh_1() nbOfNodes = m.getNumberOfNodes() MEDLoader.WriteUMesh(fileName, m, True) f1 = MEDLoader.MEDCouplingFieldDouble.New(MEDLoader.ON_NODES, MEDLoader.ONE_TIME) f1.setName("VFieldOnNodes") f1.setMesh(m) array = MEDLoader.DataArrayDouble.New() arr1 = [ 1., 101., 2., 102., 3., 103., 4., 104., 5., 105., 6., 106., 7., 107., 8., 108., 9., 109., 10., 110., 11., 111., 12., 112. ] array.setValues(arr1, nbOfNodes, 2) f1.setArray(array) array.setInfoOnComponent(0, "tyty [mm]") array.setInfoOnComponent(1, "uiop [MW]") f1.setTime(3.14, 2, 7) f1.checkConsistencyLight() arr2 = [1, 4] f2 = f1.buildSubPart(arr2) f2.getMesh().setName(f1.getMesh().getName()) MEDLoader.WriteField(fileName, f2, False) #<- False important for the test # f3 = MEDLoader.ReadFieldNode(fileName, f2.getMesh().getName(), 0, f2.getName(), 2, 7) f3.checkConsistencyLight() self.assertTrue(f3.isEqual(f2, 1e-12, 1e-12)) # arr3 = [1, 3, 0, 5, 2, 4] f2.renumberNodes(arr3) MEDLoader.WriteUMesh(fileName2, m, True) MEDLoader.WriteField(fileName2, f2, False) #<- False important for the test f3 = MEDLoader.ReadFieldNode(fileName2, f2.getMesh().getName(), 0, f2.getName(), 2, 7) f3.checkConsistencyLight() self.assertTrue(f3.isEqual(f2, 1e-12, 1e-12)) # pass
def testMultiWriteFieldOnMergeableNodesMeshes(self): fname = "Pyfile120.med" arr = MEDLoader.DataArrayDouble([(0, 0), (1, 0), (0, 1), (0, 0), (1, 0), (0, 1)]) m = MEDLoader.MEDCouplingUMesh("mesh", 2) m.setCoords(arr) m.allocateCells() m.insertNextCell(MEDLoader.NORM_TRI3, [0, 4, 2]) m.insertNextCell(MEDLoader.NORM_TRI3, [3, 1, 5]) m.setName("mesh") # f = MEDLoader.MEDCouplingFieldDouble(MEDLoader.ON_CELLS) f.setMesh(m) f.setArray(MEDLoader.DataArrayDouble([5, 6])) f.setName("field") # f.setTime(0., 0, 0) MEDLoader.WriteField(fname, f, True) f.setTime(1., 1, 0) MEDLoader.WriteField(fname, f, False) pass
def testMixCellAndNodesFieldRW1(self): fileName = "Pyfile21.med" mesh = MEDLoaderDataForTest.build3DSurfMesh_1() f1 = MEDLoader.MEDCouplingFieldDouble.New(MEDLoader.ON_CELLS, MEDLoader.ONE_TIME) f1.setName("FieldMix") f1.setMesh(mesh) array = MEDLoader.DataArrayDouble.New() f1.setArray(array) arr1 = [ 71., 171., 10., 110., 20., 120., 30., 130., 40., 140., 50., 150. ] array.setValues(arr1, 6, 2) array.setInfoOnComponent(0, "plkj [mm]") array.setInfoOnComponent(1, "pqqqss [mm]") f1.setTime(3.14, 2, 7) f1.checkConsistencyLight() # f2 = MEDLoader.MEDCouplingFieldDouble.New(MEDLoader.ON_NODES, MEDLoader.ONE_TIME) f2.setName("FieldMix") f2.setMesh(mesh) array = MEDLoader.DataArrayDouble.New() f2.setArray(array) arr2 = [ 1071., 1171., 1010., 1110., 1020., 1120., 1030., 1130., 1040., 1140., 1050., 1150., 1060., 1160., 1070., 1170., 1080., 1180., 1090., 1190., 1091., 1191., 1092., 1192. ] array.setValues(arr2, 12, 2) array.setInfoOnComponent(0, "plkj [mm]") array.setInfoOnComponent(1, "pqqqss [mm]") f2.setTime(3.14, 2, 7) f2.checkConsistencyLight() # MEDLoader.WriteField(fileName, f1, True) ts = MEDLoader.GetTypesOfField(fileName, f1.getMesh().getName(), f1.getName()) self.assertEqual(1, len(ts)) self.assertEqual(MEDLoader.ON_CELLS, ts[0]) fs = MEDLoader.GetAllFieldNamesOnMesh(fileName, f1.getMesh().getName()) self.assertEqual(1, len(fs)) self.assertTrue(fs[0] == "FieldMix") MEDLoader.WriteFieldUsingAlreadyWrittenMesh(fileName, f2) fs = MEDLoader.GetAllFieldNamesOnMesh(fileName, f1.getMesh().getName()) self.assertEqual(1, len(fs)) self.assertTrue(fs[0] == "FieldMix") # ts = MEDLoader.GetTypesOfField(fileName, f1.getMesh().getName(), f1.getName()) self.assertEqual(2, len(ts)) self.assertEqual(MEDLoader.ON_NODES, ts[0]) self.assertEqual(MEDLoader.ON_CELLS, ts[1]) # f3 = MEDLoader.ReadFieldNode(fileName, f1.getMesh().getName(), 0, f1.getName(), 2, 7) self.assertTrue(f3.isEqual(f2, 1e-12, 1e-12)) f3 = MEDLoader.ReadFieldCell(fileName, f1.getMesh().getName(), 0, f1.getName(), 2, 7) self.assertTrue(f3.isEqual(f1, 1e-12, 1e-12)) # pass
def testFieldRW3(self): fileName = "Pyfile11.med" VAL1 = 12345.67890314 VAL2 = -1111111111111. name1 = "AField" name3 = "AMesh1" f1 = MEDLoaderDataForTest.buildVecFieldOnCells_1() f1.getMesh().setName(name3) f1.setName(name1) f1.setTime(10., 8, 9) tmp = f1.getArray().getPointer() f1.getArray().setIJ(0, 0, VAL1) MEDLoader.WriteField(fileName, f1, True) f1.setTime(10.14, 18, 19) f1.getArray().setIJ(0, 0, VAL2) MEDLoader.WriteFieldUsingAlreadyWrittenMesh(fileName, f1) f1.getMesh().setName(name3) f1.setTime(10.55, 28, 29) f1.getArray().setIJ(0, 0, 3 * VAL1) MEDLoader.WriteFieldUsingAlreadyWrittenMesh(fileName, f1) vec = MEDLoader.GetMeshNamesOnField(fileName, name1) f1.setTime(10.66, 38, 39) f1.getArray().setIJ(0, 0, 3 * VAL2) MEDLoader.WriteFieldUsingAlreadyWrittenMesh(fileName, f1) f1.setTime(10.77, 48, 49) f1.getArray().setIJ(0, 0, 4 * VAL2) MEDLoader.WriteFieldUsingAlreadyWrittenMesh(fileName, f1) #ON NODES f1 = MEDLoaderDataForTest.buildVecFieldOnNodes_1() f1.setName(name1) f1.getMesh().setName(name3) f1.setTime(110., 8, 9) MEDLoader.WriteFieldUsingAlreadyWrittenMesh(fileName, f1) f1.setTime(110., 108, 109) tmp = f1.getArray().getPointer() f1.getArray().setIJ(0, 3, VAL1) MEDLoader.WriteFieldUsingAlreadyWrittenMesh(fileName, f1) f1.setTime(210., 208, 209) f1.getArray().setIJ(0, 3, VAL2) MEDLoader.WriteFieldUsingAlreadyWrittenMesh(fileName, f1) # it1 = MEDLoader.GetCellFieldIterations(fileName, name3, name1) self.assertEqual(5, len(it1)) self.assertEqual(8, it1[0][0]) self.assertEqual(9, it1[0][1]) self.assertEqual(18, it1[1][0]) self.assertEqual(19, it1[1][1]) self.assertEqual(28, it1[2][0]) self.assertEqual(29, it1[2][1]) self.assertEqual(38, it1[3][0]) self.assertEqual(39, it1[3][1]) self.assertEqual(48, it1[4][0]) self.assertEqual(49, it1[4][1]) it3 = MEDLoader.GetNodeFieldIterations(fileName, name3, name1) self.assertEqual(3, len(it3)) self.assertEqual(8, it3[0][0]) self.assertEqual(9, it3[0][1]) self.assertEqual(108, it3[1][0]) self.assertEqual(109, it3[1][1]) self.assertEqual(208, it3[2][0]) self.assertEqual(209, it3[2][1]) # # f1 = MEDLoader.ReadFieldCell(fileName, name3, 0, name1, 8, 9) self.assertAlmostEqual(VAL1, f1.getArray().getIJ(0, 0), 13) f1 = MEDLoader.ReadFieldCell(fileName, name3, 0, name1, 18, 19) self.assertAlmostEqual(VAL2, f1.getArray().getIJ(0, 0), 13) f1 = MEDLoader.ReadFieldCell(fileName, name3, 0, name1, 28, 29) self.assertAlmostEqual(3 * VAL1, f1.getArray().getIJ(0, 0), 13) f1 = MEDLoader.ReadFieldCell(fileName, name3, 0, name1, 38, 39) self.assertAlmostEqual(3 * VAL2, f1.getArray().getIJ(0, 0), 13) f1 = MEDLoader.ReadFieldCell(fileName, name3, 0, name1, 48, 49) self.assertAlmostEqual(4 * VAL2, f1.getArray().getIJ(0, 0), 13) # f1 = MEDLoader.ReadFieldNode(fileName, name3, 0, name1, 8, 9) self.assertAlmostEqual(71., f1.getArray().getIJ(0, 3), 13) f1 = MEDLoader.ReadFieldNode(fileName, name3, 0, name1, 108, 109) self.assertAlmostEqual(VAL1, f1.getArray().getIJ(0, 3), 13) f1 = MEDLoader.ReadFieldNode(fileName, name3, 0, name1, 208, 209) self.assertAlmostEqual(VAL2, f1.getArray().getIJ(0, 3), 13) pass
# Unstructured mesh and image making with paraview python ui #import os import MEDCoupling as mc import MEDLoader as ml mfl = ml.MEDFileMeshes("results_2D.med") meshesNames = ml.MEDFileMeshes.getMeshesNames(mfl) print("Nomi delle mesh...") print meshesNames mff = ml.MEDFileFields("results_2D.med") fileldsNames = ml.MEDFileFields.getFieldsNames(mff) print("Nomi dei campi...") print fileldsNames m2D = ml.ReadUMeshFromFile("results_2D.med", "mesh", 0) m2D.writeVTK("results_2D_only_mesh.vtu") field1 = ml.ReadFieldNode("results_2D.med", "mesh", 0, "DZ", 1, 1) file_name = mc.MEDCouplingFieldDouble.WriteVTK("results_2D_field_DZ", field1)
# Building cells print("Cella n.1 formata dai nodi --> ", nodalConnOfCell[:4]) mesh.insertNextCell(mc.NORM_QUAD4, nodalConnOfCell[:4]) print("Cella n.2 formata dai nodi --> ", nodalConnOfCell[4:7]) mesh.insertNextCell(mc.NORM_TRI3, nodalConnOfCell[4:7]) print("Cella n.3 formata dai nodi --> ", nodalConnOfCell[7:10]) mesh.insertNextCell(mc.NORM_TRI3, nodalConnOfCell[7:10]) print("Cella n.4 formata dai nodi --> ", nodalConnOfCell[10:14]) mesh.insertNextCell(mc.NORM_QUAD4, nodalConnOfCell[10:14]) print("Cella n.5 formata dai nodi --> ", nodalConnOfCell[14:]) mesh.insertNextCell(mc.NORM_QUAD4, nodalConnOfCell[14:]) # Restore mesh instance. mesh.finishInsertingCells() # Store coords in reshape array coordsArr = mc.DataArrayDouble(coords, 9, 3) mesh.setCoords(coordsArr) mesh.writeVTK("uMesh2D.vtu") ml.WriteUMesh("uMesh2D.med", mesh, True) # Python ui in paraview cmd = 'pvpython uMesh2Dpvcomm.py' os.system(cmd) # returns the exit status
def getWeirdMesh(xmin, xmax, ymin, ymax, lx, ly, str, func=None, extr=-1, unpo=False): polys = [] ic = 0 for iy, y in enumerate(ly[:-1]): for ix, x in enumerate(lx[:-1]): ic += 1 dx = lx[ix + 1] - x dy = ly[iy + 1] - y c = str[ic % len(str)] if func is None else str[int( min( max( func( xmin + (xmax - xmin) * (x + dx / 2 - lx[0]) / (lx[-1] - lx[0]), ymin + (ymax - ymin) * (y + dy / 2 - ly[0]) / (ly[-1] - ly[0])), 0), len(str) - 1))] if c == "-": # deux rectangles (horizontale) polys.extend([[[x + dx * i, y + dy * (j + k) / 2] for (i, j) in [(0, 0), (0, 1), (1, 1), (1, 0)]] for k in range(2)]) elif c == "|": # deux rectangles (verticale) polys.extend([[[x + dx * (i + k) / 2, y + j] for (i, j) in [(0, 0), (0, 1), (1, 1), (1, 0)]] for k in range(2)]) elif c == "+" or c == "#" or c.isdigit(): # petits carres n = 2 if c == "+" else 3 if c == "#" else int(c) polys.extend([[[x + dx * (i + k) / n, y + dy * (j + l) / n] for (i, j) in [(0, 0), (0, 1), (1, 1), (1, 0)]] for k in range(int(n)) for l in range(int(n))]) elif c == "x" or c == "*": # triangles en croix f = 2 * pi / 8 l = [[ x + dx / 2 * (1 + cos(f * k) / max(abs(cos(f * k)), abs(sin(f * k)))), y + dy / 2 * (1 + sin(f * k) / max(abs(cos(f * k)), abs(sin(f * k)))) ] for k in range(-1, 8, 1 + (c == "x"))] polys.extend([[l[i], [x + dx / 2, y + dy / 2], l[i + 1]] for i in range(len(l) - 1)]) elif c == "/" or c == "\\": polys.extend( [[[x + dx * i, y + dy * (1 - j if c == "/" else j)] for (i, j) in [(0, 1), (1, 0), p]] for p in [(0, 0), (1, 1)]]) else: # par defaut -> carre vide polys.append([[x + dx * i, y + dy * j] for (i, j) in [(0, 0), (0, 1), (1, 1), (1, 0)]]) polys = [[[ xmin + (x - lx[0]) * (xmax - xmin) / (lx[-1] - lx[0]), ymin + (y - ly[0]) * (ymax - ymin) / (ly[-1] - ly[0]) ] for [x, y] in p] for p in polys] mesh = ml.MEDCouplingUMesh("mesh", 2) mesh.allocateCells(len(polys)) off = 0 for p in polys: mesh.insertNextCell(ml.NORM_POLYGON, len(p), [off + i for i in range(len(p))]) off += len(p) mesh.finishInsertingCells() pts = [p for i in xange(len(polys)) for p in polys[i]] co = ml.DataArrayDouble(pts, len(pts), 2) mesh.setCoords(co) mesh.changeSpaceDimension(3) mesh.orientCorrectly2DCells([0., 0., -1.], False) mesh.changeSpaceDimension(2) mesh.mergeNodes(1e-8) mesh.conformize2D(1e-8) if unpo: mesh.unPolyze() if extr > 0: mesh.changeSpaceDimension(3) mesh1d = ml.MEDCouplingUMesh("ex", 1) mesh1d.allocateCells(extr) coo = [0., 0., 0.] for i in range(extr): mesh1d.insertNextCell(ml.NORM_SEG2, 2, [i, i + 1]) coo.extend([0., 0., float(i + 1) / float(extr)]) coo_arr = ml.DataArrayDouble(coo, extr + 1, 3) mesh1d.setCoords(coo_arr) mesh = mesh.buildExtrudedMesh(mesh1d, 0) mesh.setName("mesh") mf, desc, descIndx, revDesc, revDescIndx = mesh.buildDescendingConnectivity( ) mf.setName("mesh") mm = ml.MEDFileUMesh.New() mm.setMeshAtLevel(0, mesh) mm.setMeshAtLevel(-1, mf) bf = mf.computeCellCenterOfMass() noms_cl = [["left", "right"]] if extr > 0: noms_cl.append(["front", "back"]) noms_cl.append(["down", "up"]) for i in range(2 + (extr > 0)): # frontieres en x puis y (puis en z si extr) for j in range(2): # min puis max g = [] for idx, b in enumerate(bf): if abs(b[i] - [[xmin, xmax], [ymin, ymax], [0., 1.]][i][j]) < 1e-5: g.append(idx) grp = ml.DataArrayInt.New(g) grp.setName(noms_cl[i][j]) mm.addGroup(-1, grp) return mm
# $ echo "export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/lpaone/salome_meca/V2017.0.2/prerequisites/Hdf5-1814/lib" >> ~/.bashrc # $ echo "export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/lpaone/salome_meca/V2017.0.2/tools/Medfichier-321/lib" >> ~/.bashrc # $ echo "export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/lpaone/salome_meca/V2017.0.2/tools/Medcoupling-V8_3_0/lib" >> ~/.bashrc # # ADDING PATH FOR MODULE # make a file "MEDCoupling.pth" in /home/lpaone/anaconda2/lib/python2.7/site-packages that containts the line: # /home/lpaone/salome_meca/V2017.0.2/tools/Medcoupling-V8_3_0/lib/python2.7/site-packages import MEDLoader as ml # Mesh creation 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 ] targetConn = [0, 3, 4, 1, 1, 4, 2, 4, 5, 2, 6, 7, 4, 3, 7, 8, 5, 4] targetMesh = ml.MEDCouplingUMesh("MyMesh", 2) targetMesh.allocateCells(5) targetMesh.insertNextCell(ml.NORM_TRI3, 3, targetConn[4:7]) targetMesh.insertNextCell(ml.NORM_TRI3, 3, targetConn[7:10]) targetMesh.insertNextCell(ml.NORM_QUAD4, 4, targetConn[0:4]) targetMesh.insertNextCell(ml.NORM_QUAD4, 4, targetConn[10:14]) targetMesh.insertNextCell(ml.NORM_QUAD4, 4, targetConn[14:18]) myCoords = ml.DataArrayDouble(targetCoords, 9, 2) myCoords.setInfoOnComponents(["X [km]", "YY [mm]"]) targetMesh.setCoords(myCoords) # Writing mesh only ml.WriteUMesh("TargetMesh.med", targetMesh, True) # True means 'from scratch' # Re-read it and test equality meshRead = ml.ReadUMeshFromFile("TargetMesh.med", targetMesh.getName(), 0) print "Is the read mesh equal to 'targetMesh' ?", meshRead.isEqual( targetMesh, 1e-12)
def testEasyFieldRead1(self): fname = "Pyfile111.med" arr = MEDLoader.DataArrayDouble(4) arr.iota() m = MEDLoader.MEDCouplingCMesh() m.setCoords(arr, arr) m = m.buildUnstructured() m.setName("mesh") f = MEDLoader.MEDCouplingFieldDouble(MEDLoader.ON_CELLS) f.setName("field") f.setTime(3., 1, 2) da = MEDLoader.DataArrayDouble([2, 3, 4, 5, 6, 7, 8, 9, 10]) f.setArray(da) f.setMesh(m) MEDLoader.WriteField(fname, f, True) # self.assertTrue(f.isEqual(MEDLoader.ReadField(fname), 1e-12, 1e-12)) self.assertTrue( f.isEqual(MEDLoader.ReadField(fname, "field"), 1e-12, 1e-12)) self.assertTrue( f.isEqual(MEDLoader.ReadField(fname, "field", 1, 2), 1e-12, 1e-12)) self.assertTrue( f.isEqual( MEDLoader.ReadField(MEDLoader.ON_CELLS, fname, "mesh", 0, "field", 1, 2), 1e-12, 1e-12)) # f3 = f.deepCopy() f3.setArray(f.getArray() + 30) f3.setName("field2") f3.setTime(5., 4, 5) MEDLoader.WriteFieldUsingAlreadyWrittenMesh(fname, f3) # self.assertRaises(Exception, MEDLoader.ReadField, fname) # because several fields in fname now self.assertTrue( f.isEqual(MEDLoader.ReadField(fname, "field"), 1e-12, 1e-12)) self.assertTrue( f.isEqual(MEDLoader.ReadField(fname, "field", 1, 2), 1e-12, 1e-12)) self.assertTrue( f.isEqual( MEDLoader.ReadField(MEDLoader.ON_CELLS, fname, "mesh", 0, "field", 1, 2), 1e-12, 1e-12)) self.assertTrue( f3.isEqual(MEDLoader.ReadField(fname, "field2"), 1e-12, 1e-12)) self.assertTrue( f3.isEqual(MEDLoader.ReadField(fname, "field2", 4, 5), 1e-12, 1e-12)) self.assertTrue( f3.isEqual( MEDLoader.ReadField(MEDLoader.ON_CELLS, fname, "mesh", 0, "field2", 4, 5), 1e-12, 1e-12)) # f2 = f.deepCopy() f2.setTime(4., 3, 4) f2.setArray(f2.getArray() + 10) MEDLoader.WriteFieldUsingAlreadyWrittenMesh(fname, f2) # self.assertRaises( Exception, MEDLoader.ReadField, fname) # because unique field "field" has more than one time step self.assertRaises( Exception, MEDLoader.ReadField, fname, "field" ) # because unique field "field" has more than one time step self.assertTrue( f.isEqual(MEDLoader.ReadField(fname, "field", 1, 2), 1e-12, 1e-12)) self.assertTrue( f.isEqual( MEDLoader.ReadField(MEDLoader.ON_CELLS, fname, "mesh", 0, "field", 1, 2), 1e-12, 1e-12)) self.assertTrue( f2.isEqual(MEDLoader.ReadField(fname, "field", 3, 4), 1e-12, 1e-12)) self.assertTrue( f2.isEqual( MEDLoader.ReadField(MEDLoader.ON_CELLS, fname, "mesh", 0, "field", 3, 4), 1e-12, 1e-12)) # self.assertTrue( f.isEqual(MEDLoader.ReadField(fname, "field", 1, 2), 1e-12, 1e-12)) self.assertTrue( f.isEqual( MEDLoader.ReadField(MEDLoader.ON_CELLS, fname, "mesh", 0, "field", 1, 2), 1e-12, 1e-12)) self.assertTrue( f2.isEqual(MEDLoader.ReadField(fname, "field", 3, 4), 1e-12, 1e-12)) self.assertTrue( f2.isEqual( MEDLoader.ReadField(MEDLoader.ON_CELLS, fname, "mesh", 0, "field", 3, 4), 1e-12, 1e-12)) # self.assertTrue( f3.isEqual(MEDLoader.ReadField(fname, "field2"), 1e-12, 1e-12)) self.assertTrue( f3.isEqual(MEDLoader.ReadField(fname, "field2", 4, 5), 1e-12, 1e-12)) self.assertRaises(Exception, MEDLoader.ReadField, fname, "field2", 5, 5) # invalid time step self.assertTrue( f3.isEqual( MEDLoader.ReadField(MEDLoader.ON_CELLS, fname, "mesh", 0, "field2", 4, 5), 1e-12, 1e-12)) self.assertRaises(Exception, MEDLoader.ReadField, MEDLoader.ON_CELLS, fname, "mesh", 0, "field2", 5, 5) # invalid time step # Test on profile - restart from scratch mm = MEDLoader.MEDFileUMesh() mm[0] = m mm.write(fname, 2) # pfl = MEDLoader.DataArrayInt(list(range(8))) pfl.setName("PFL") # f = MEDLoader.MEDCouplingFieldDouble(MEDLoader.ON_CELLS) f.setName("field") f.setTime(3., 1, 2) da = MEDLoader.DataArrayDouble([2, 3, 4, 5, 6, 7, 8, 9]) f.setArray(da) f.setMesh(m[pfl]) f.checkConsistencyLight() # f1ts = MEDLoader.MEDFileField1TS() f1ts.setFieldProfile(f, mm, 0, pfl) f1ts.write(fname, 0) # self.assertTrue(f.isEqual(MEDLoader.ReadField(fname), 1e-12, 1e-12)) self.assertTrue( f.isEqual(MEDLoader.ReadField(fname, "field"), 1e-12, 1e-12)) self.assertTrue( f.isEqual(MEDLoader.ReadField(fname, "field", 1, 2), 1e-12, 1e-12)) self.assertTrue( f.isEqual( MEDLoader.ReadField(MEDLoader.ON_CELLS, fname, "mesh", 0, "field", 1, 2), 1e-12, 1e-12)) # f3 = f.deepCopy() f3.setArray(f.getArray() + 30) f3.setName("field2") f3.setTime(5., 4, 5) f1ts = MEDLoader.MEDFileField1TS() f1ts.setFieldProfile(f3, mm, 0, pfl) f1ts.write(fname, 0) # self.assertRaises(Exception, MEDLoader.ReadField, fname) # because several fields in fname now self.assertTrue( f.isEqual(MEDLoader.ReadField(fname, "field"), 1e-12, 1e-12)) self.assertTrue( f.isEqual(MEDLoader.ReadField(fname, "field", 1, 2), 1e-12, 1e-12)) self.assertTrue( f.isEqual( MEDLoader.ReadField(MEDLoader.ON_CELLS, fname, "mesh", 0, "field", 1, 2), 1e-12, 1e-12)) self.assertTrue( f3.isEqual(MEDLoader.ReadField(fname, "field2"), 1e-12, 1e-12)) self.assertTrue( f3.isEqual(MEDLoader.ReadField(fname, "field2", 4, 5), 1e-12, 1e-12)) self.assertTrue( f3.isEqual( MEDLoader.ReadField(MEDLoader.ON_CELLS, fname, "mesh", 0, "field2", 4, 5), 1e-12, 1e-12)) # f2 = f.deepCopy() f2.setTime(4., 3, 4) f2.setArray(f2.getArray() + 10) f1ts = MEDLoader.MEDFileField1TS() f1ts.setFieldProfile(f2, mm, 0, pfl) f1ts.write(fname, 0) # self.assertRaises( Exception, MEDLoader.ReadField, fname) # because unique field "field" has more than one time step self.assertRaises( Exception, MEDLoader.ReadField, fname, "field" ) # because unique field "field" has more than one time step self.assertTrue( f.isEqual(MEDLoader.ReadField(fname, "field", 1, 2), 1e-12, 1e-12)) self.assertTrue( f.isEqual( MEDLoader.ReadField(MEDLoader.ON_CELLS, fname, "mesh", 0, "field", 1, 2), 1e-12, 1e-12)) self.assertTrue( f2.isEqual(MEDLoader.ReadField(fname, "field", 3, 4), 1e-12, 1e-12)) self.assertTrue( f2.isEqual( MEDLoader.ReadField(MEDLoader.ON_CELLS, fname, "mesh", 0, "field", 3, 4), 1e-12, 1e-12)) # self.assertTrue( f.isEqual(MEDLoader.ReadField(fname, "field", 1, 2), 1e-12, 1e-12)) self.assertTrue( f.isEqual( MEDLoader.ReadField(MEDLoader.ON_CELLS, fname, "mesh", 0, "field", 1, 2), 1e-12, 1e-12)) self.assertTrue( f2.isEqual(MEDLoader.ReadField(fname, "field", 3, 4), 1e-12, 1e-12)) self.assertTrue( f2.isEqual( MEDLoader.ReadField(MEDLoader.ON_CELLS, fname, "mesh", 0, "field", 3, 4), 1e-12, 1e-12)) # self.assertTrue( f3.isEqual(MEDLoader.ReadField(fname, "field2"), 1e-12, 1e-12)) self.assertTrue( f3.isEqual(MEDLoader.ReadField(fname, "field2", 4, 5), 1e-12, 1e-12)) self.assertRaises(Exception, MEDLoader.ReadField, fname, "field2", 5, 5) # invalid time step self.assertTrue( f3.isEqual( MEDLoader.ReadField(MEDLoader.ON_CELLS, fname, "mesh", 0, "field2", 4, 5), 1e-12, 1e-12)) self.assertRaises(Exception, MEDLoader.ReadField, MEDLoader.ON_CELLS, fname, "mesh", 0, "field2", 5, 5) # invalid time step pass
def testFieldRW2(self): fileName = "Pyfile8.med" VAL1 = 12345.67890314 VAL2 = -1111111111111. f1 = MEDLoaderDataForTest.buildVecFieldOnCells_1() MEDLoader.WriteField(fileName, f1, True) f1.setTime(10., 8, 9) f1.getArray().setIJ(0, 0, VAL1) MEDLoader.WriteFieldUsingAlreadyWrittenMesh(fileName, f1) f1.setTime(10.14, 18, 19) f1.getArray().setIJ(0, 0, VAL2) MEDLoader.WriteFieldUsingAlreadyWrittenMesh(fileName, f1) #retrieving time steps... f2 = MEDLoader.ReadFieldCell(fileName, f1.getMesh().getName(), 0, f1.getName(), 8, 9) f1.setTime(10., 8, 9) f1.getArray().setIJ(0, 0, VAL1) self.assertTrue(f1.isEqual(f2, 1e-12, 1e-12)) f2 = MEDLoader.ReadFieldCell(fileName, f1.getMesh().getName(), 0, f1.getName(), 0, 1) f3 = MEDLoaderDataForTest.buildVecFieldOnCells_1() self.assertTrue(f3.isEqual(f2, 1e-12, 1e-12)) f2 = MEDLoader.ReadFieldCell(fileName, f1.getMesh().getName(), 0, f1.getName(), 18, 19) f1.setTime(10.14, 18, 19) f1.getArray().setIJ(0, 0, VAL2) self.assertTrue(f1.isEqual(f2, 1e-12, 1e-12)) #test of throw on invalid (dt,it) self.assertRaises(Exception, MEDLoader.ReadFieldCell, fileName, f1.getMesh().getName(), 0, f1.getName(), 28, 19) #ON NODES f1 = MEDLoaderDataForTest.buildVecFieldOnNodes_1() fileName2 = "Pyfile9.med" MEDLoader.WriteField(fileName2, f1, True) f1.setTime(110., 108, 109) tmp = f1.getArray().getPointer() f1.getArray().setIJ(0, 3, VAL1) MEDLoader.WriteFieldUsingAlreadyWrittenMesh(fileName2, f1) f1.setTime(210., 208, 209) f1.getArray().setIJ(0, 3, VAL2) MEDLoader.WriteFieldUsingAlreadyWrittenMesh(fileName2, f1) f2 = MEDLoader.ReadFieldNode(fileName2, f1.getMesh().getName(), 0, f1.getName(), 108, 109) f1.setTime(110., 108, 109) f1.getArray().setIJ(0, 3, VAL1) self.assertTrue(f1.isEqual(f2, 1e-12, 1e-12)) f2 = MEDLoader.ReadFieldNode(fileName2, f1.getMesh().getName(), 0, f1.getName(), 2, 3) f3 = MEDLoaderDataForTest.buildVecFieldOnNodes_1() self.assertTrue(f3.isEqual(f2, 1e-12, 1e-12)) f2 = MEDLoader.ReadFieldNode(fileName2, f1.getMesh().getName(), 0, f1.getName(), 208, 209) f1.setTime(210., 208, 209) f1.getArray().setIJ(0, 3, VAL2) self.assertTrue(f1.isEqual(f2, 1e-12, 1e-12)) pass
def testUsingAlreadyWrittenMesh2(self): """ This test focuses on MEDLoader.WriteFieldUsingAlreadyWrittenMesh with mesh different from UMesh. """ fname = "Pyfile76.med" mesh = MEDLoader.MEDCouplingCMesh("mesh") arrX = MEDLoader.DataArrayDouble([0, 1, 2, 3]) arrY = MEDLoader.DataArrayDouble([0, 2, 3, 5, 7]) arrZ = MEDLoader.DataArrayDouble([7]) mesh.setCoords(arrX, arrY, arrZ) # f1 = MEDLoader.MEDCouplingFieldDouble(MEDLoader.ON_NODES) f1.setName("f1") f1.setMesh(mesh) arr = MEDLoader.DataArrayDouble(20) arr.iota() f1.setArray(arr) f1.checkConsistencyLight() # f2 = MEDLoader.MEDCouplingFieldDouble(MEDLoader.ON_NODES) f2.setName("f2") f2.setMesh(mesh) arr = MEDLoader.DataArrayDouble(20) arr.iota() arr *= 3 f2.setArray(arr) f2.checkConsistencyLight() # f11 = f1.deepCopy() (f11.getArray())[:] *= 4 f11.setTime(1.1, 5, 6) # MEDLoader.WriteMesh(fname, f1.getMesh(), True) MEDLoader.WriteFieldUsingAlreadyWrittenMesh(fname, f1) MEDLoader.WriteFieldUsingAlreadyWrittenMesh(fname, f2) MEDLoader.WriteFieldUsingAlreadyWrittenMesh(fname, f11) ## f1r = MEDLoader.ReadFieldNode(fname, "mesh", 0, "f1", -1, -1) self.assertTrue(f1.isEqual(f1r, 1e-12, 1e-12)) self.assertTrue(f1r.getArray().isEqual( MEDLoader.DataArrayDouble([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11., 12., 13., 14., 15., 16., 17., 18., 19. ]), 1e-12)) f2r = MEDLoader.ReadFieldNode(fname, "mesh", 0, "f2", -1, -1) self.assertTrue(f2.isEqual(f2r, 1e-12, 1e-12)) self.assertTrue(f2r.getArray().isEqual( MEDLoader.DataArrayDouble([ 0., 3., 6., 9., 12., 15., 18., 21., 24., 27., 30., 33., 36., 39., 42., 45., 48., 51., 54., 57. ]), 1e-12)) f3r = MEDLoader.ReadFieldNode(fname, "mesh", 0, "f1", 5, 6) self.assertTrue(f11.isEqual(f3r, 1e-12, 1e-12)) self.assertTrue(f3r.getArray().isEqual( MEDLoader.DataArrayDouble([ 0., 4., 8., 12., 16., 20., 24., 28., 32., 36., 40., 44., 48., 52., 56., 60., 64., 68., 72., 76. ]), 1e-12)) pass
def testMultiMeshRW1(self): fileName = "Pyfile10.med" mesh1 = MEDLoaderDataForTest.build3DMesh_1() part1 = [1, 2, 4, 13, 15] mesh2 = mesh1.buildPartOfMySelf(part1, True) mesh2.setName("mesh2") part2 = [3, 4, 13, 14] mesh3 = mesh1.buildPartOfMySelf(part2, True) mesh3.setName("mesh3") mesh4 = MEDLoader.MEDCouplingUMesh.New() mesh4.setName("mesh4") mesh4.setMeshDimension(3) mesh4.allocateCells(1) conn = [0, 11, 1, 3] mesh4.insertNextCell(MEDLoader.NORM_TETRA4, 4, conn[0:4]) mesh4.finishInsertingCells() mesh4.setCoords(mesh1.getCoords()) meshes = [mesh1, mesh2, mesh3, mesh4] mnane = "3DToto" MEDLoader.WriteUMeshesPartition(fileName, mnane, meshes, True) # mesh5 = MEDLoader.ReadUMeshFromFile(fileName, mnane) mesh1.setName(mnane) part3 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17] mesh6 = mesh5.buildPartOfMySelf(part3, True) mesh6.setName(mnane) self.assertTrue(mesh6.isEqual(mesh1, 1e-12)) grps = MEDLoader.GetMeshGroupsNames(fileName, mnane) self.assertEqual(4, len(grps)) grps.index("mesh2") grps.index("mesh3") grps.index("mesh4") grps.index("3DMesh_1") # vec = ("mesh2", ) mesh2_2 = MEDLoader.ReadUMeshFromGroups(fileName, mnane, 0, vec) self.assertTrue(mesh2_2.isEqual(mesh2, 1e-12)) vec = ["mesh3"] mesh3_2 = MEDLoader.ReadUMeshFromGroups(fileName, mnane, 0, vec) self.assertTrue(mesh3_2.isEqual(mesh3, 1e-12)) vec = ["mesh4"] mesh4_2 = MEDLoader.ReadUMeshFromGroups(fileName, mnane, 0, vec) self.assertTrue(mesh4_2.isEqual(mesh4, 1e-12)) vec = "3DMesh_1" mesh1_2 = MEDLoader.ReadUMeshFromGroups(fileName, mnane, 0, vec) mesh1.setName("3DMesh_1") self.assertTrue(mesh1_2.isEqual(mesh1, 1e-12)) # vec = ["Family_-3", "Family_-5"] mesh2_2 = MEDLoader.ReadUMeshFromFamilies(fileName, mnane, 0, vec) mesh2_2.setName("mesh2") self.assertTrue(mesh2_2.isEqual(mesh2, 1e-12)) # ret = MEDLoader.GetMeshFamiliesNamesOnGroup(fileName, "3DToto", "3DMesh_1") self.assertEqual(4, len(ret)) ref = ['Family_-3', 'Family_-4', 'Family_-2', 'Family_-5'] self.assertIn(ref[0], ret) self.assertIn(ref[1], ret) self.assertIn(ref[2], ret) self.assertIn(ref[3], ret) # ret1 = MEDLoader.GetMeshGroupsNamesOnFamily(fileName, "3DToto", "Family_-3") self.assertEqual(2, len(ret1)) self.assertEqual(ret1[0], "3DMesh_1") self.assertEqual(ret1[1], "mesh2") pass