Salome HOME
[EDF28448] : MEDFileUMesh.declarePartsUpdated and MEDFileField1TS context manager...
[tools/medcoupling.git] / src / MEDLoader / Swig / MEDLoaderFinalize.py
1 #  -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2023  CEA, EDF
3 #
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License, or (at your option) any later version.
8 #
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 # Lesser General Public License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 #
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #
20
21 def MEDFileUMeshReduceToCells(self, level, keepCells, removeOrphanNodes=True):
22     """
23     Method returning a new MEDFileUMesh, restriction of self to level and keepCell cells at this level.
24     This method also 
25
26     :param level: Specifies the top level of the returned MEDFileUMesh expected
27     :param keepCells: A DataArrayInt specifying cell ids at level level of self
28     :param removeOrphanNodes: Specifies if orphan nodes should be removed at the end
29     
30     see also MEDFileUMesh.extractPart
31     """
32     import MEDLoader as ml
33     subLevs = [l for l in self.getNonEmptyLevels() if l<=level]
34     subMeshes = [self[lev] for lev in subLevs]
35     allFamilyFields = [self.getFamilyFieldAtLevel(lev) for lev in subLevs]
36     allRefMesh = subMeshes[0]
37     refMesh = allRefMesh[keepCells]
38
39     mmOut = ml.MEDFileUMesh()
40     # level 0
41     mmOut[0] = refMesh
42     mmOut.setFamilyFieldArr(0,allFamilyFields[0][keepCells])
43
44     # subLevels
45     for curLev,meshLev,famFieldLev in zip(subLevs[1:],subMeshes[1:],allFamilyFields[1:]):
46         allMeshLev,d,di, rd,rdi = allRefMesh.explodeMeshTo( curLev-level )
47         a,b = allMeshLev.areCellsIncludedIn(meshLev,2)
48         if not a:
49             raise RuntimeError("Error in mesh {}")
50         dlev,dlevi = ml.DataArrayInt.ExtractFromIndexedArrays( keepCells, d,di )
51         dlev2 = dlev.buildUniqueNotSorted()
52         cellsToKeepLev = ml.DataArrayInt.BuildIntersection([dlev2,b])
53         cellsToKeepLev = b.indicesOfSubPart(cellsToKeepLev)
54         cellsToKeepLev.sort()
55         mmOut[curLev] = meshLev[cellsToKeepLev]
56         mmOut.setFamilyFieldArr(curLev,famFieldLev[cellsToKeepLev])
57
58     allFamNodes = mmOut.getFamilyFieldAtLevel(1)
59     if allFamNodes:
60         mmOut.setFamilyFieldArr(1,allFamNodes[:])
61
62     if removeOrphanNodes:
63         mmOut.zipCoords()
64
65     mmOut.copyFamGrpMapsFrom(self)
66     return mmOut