Salome HOME
8fb3b1c7fb18d93c8439a8c6595012b80bdf03bb
[tools/medcoupling.git] / src / MEDLoader / Swig / MEDLoaderSplitter.py
1 #  -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2016  CEA/DEN, EDF R&D
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 # Author : Anthony GEAY (CEA/DEN/DM2S/STMF/LGLS)
21
22 from MEDLoader import *
23 import os
24
25 class MEDLoaderSplitter:
26     @classmethod
27     def New(cls,mfd,idsLst):
28         """ mfd is a MEDFileData instance containing only one mesh. idsLst is a list of DataArrayInt containing each the ids per processor """
29         return MEDLoaderSplitter(fileName)
30         pass
31
32     def __init__(self,mfd,idsLst):
33         """ mfd is a MEDFileData instance containing only one mesh. idsLst is a list of DataArrayInt containing each the ids per processor """
34         mfmsh=mfd.getMeshes()
35         mfflds=mfd.getFields()
36         if len(mfmsh)!=1:
37             raise InterpKernelException("Works only with one mesh !")
38         mfflds=mfflds.partOfThisLyingOnSpecifiedMeshName(mfmsh[0].getName())
39         retf=self.__splitFields(mfmsh[0],mfflds,idsLst)
40         retm=self.__splitMesh(mfmsh[0],idsLst)
41         self._mfd_splitted=[MEDFileData() for i in xrange(len(idsLst))]
42         for a,b,c in zip(self._mfd_splitted,retf,retm):
43             a.setFields(b) ; a.setMeshes(c)
44             pass
45         pass
46
47     def getSplittedInstances(self):
48         return self._mfd_splitted
49     
50     @classmethod
51     def __splitMEDFileField1TSNode(cls,f,f1ts,ids):
52         fRet=f[ids]
53         f1ts.setFieldNoProfileSBT(fRet)
54         pass
55     
56     @classmethod
57     def __splitMEDFileField1TSCell(cls,f,f1ts,ids):
58         fRet=f[ids]
59         m=fRet.getMesh() ; m.zipCoords()
60         o2n=m.getRenumArrForMEDFileFrmt() ; fRet.renumberCells(o2n,False)
61         f1ts.setFieldNoProfileSBT(fRet)
62         pass
63     
64     def __splitMEDFileField1TS(self,mm,f1ts,idsLst):
65         ret=[f1ts.__class__() for i in xrange(len(idsLst))]
66         dico={ON_CELLS:self.__splitMEDFileField1TSCell,
67               ON_NODES:self.__splitMEDFileField1TSNode,
68               ON_GAUSS_PT:self.__splitMEDFileField1TSCell,
69               ON_GAUSS_NE:self.__splitMEDFileField1TSCell}
70         for t in f1ts.getTypesOfFieldAvailable():
71             f=f1ts.getFieldOnMeshAtLevel(t,0,mm)
72             for i,f0 in enumerate(ret):
73                 dico[t](f,f0,idsLst[i])
74                 pass
75             pass
76         return ret
77     
78     def __splitFields(self,mm,mfflds,idsLst):
79         ret0=[MEDFileFields() for i in xrange(len(idsLst))]
80         for fmts in mfflds:
81             if len(fmts.getPflsReallyUsed())!=0:
82                 print "Field \"%s\" contains profiles ! Not supported yet ! This field will be ignored !"%(fmts.getName())
83                 continue
84             pass
85             ret1=[fmts.__class__() for i in xrange(len(idsLst))]
86             for f1ts in fmts:
87                 for fmtsPart,f1tsPart in zip(ret1,self.__splitMEDFileField1TS(mm,f1ts,idsLst)):
88                     fmtsPart.pushBackTimeStep(f1tsPart)
89                     pass
90                 pass
91             for fieldsPart,fmtsPart in zip(ret0,ret1):
92                 fieldsPart.pushField(fmtsPart);
93                 pass
94             pass
95         return ret0
96
97     def __splitMesh(self,mfm,idsLst):
98         ret0=[MEDFileMeshes() for i in xrange(len(idsLst))]
99         m=mfm.getMeshAtLevel(0)
100         for ret,ids in zip(ret0,idsLst):
101             mlPart=mfm.createNewEmpty()
102             mPart=m[ids] ; trad=mPart.zipCoordsTraducer()
103             trad=trad.invertArrayO2N2N2O(mPart.getNumberOfNodes())
104             mlPart.setMeshAtLevel(0,mPart)
105             if 0 in mfm.getFamArrNonEmptyLevelsExt():
106                 mlPart.setFamilyFieldArr(0,mfm.getFamilyFieldAtLevel(0)[ids])
107                 pass
108             if 1 in mfm.getFamArrNonEmptyLevelsExt():
109                 mlPart.setFamilyFieldArr(1,mfm.getFamilyFieldAtLevel(1)[trad])
110                 pass
111             mlPart.copyFamGrpMapsFrom(mfm)
112             ret.pushMesh(mlPart)
113             pass
114         return ret0
115     pass