]> SALOME platform Git repositories - modules/med.git/blob - src/MEDMEM/MEDMEM_Family.cxx
Salome HOME
update from the MedMemory V1.0.1
[modules/med.git] / src / MEDMEM / MEDMEM_Family.cxx
1 using namespace std;
2 /*
3  File MEDMEM_Family.cxx
4  $Header$
5 */
6
7 #include "MEDMEM_Mesh.hxx"
8 #include "MEDMEM_Family.hxx"
9
10 FAMILY::FAMILY():_identifier(0), _numberOfAttribute(0), 
11                  _attributeIdentifier((int*)NULL),_attributeValue((int*)NULL), _attributeDescription((string*)NULL),
12                  _numberOfGroup(0), _groupName((string*)NULL) 
13 {
14     MESSAGE("FAMILY::FAMILY()");
15 };
16
17 FAMILY::FAMILY(MESH* Mesh, int Identifier, string Name, int NumberOfAttribute,
18                int *AttributeIdentifier, int *AttributeValue, string AttributeDescription,
19                int NumberOfGroup, string GroupName,
20                int * MEDArrayNodeFamily,
21                int ** MEDArrayCellFamily,
22                int ** MEDArrayFaceFamily,
23                int ** MEDArrayEdgeFamily
24                ): SUPPORT(Mesh,Name),
25                   _identifier(Identifier), 
26                   _numberOfAttribute(NumberOfAttribute), 
27                   _numberOfGroup(NumberOfGroup)
28 {
29   MESSAGE("FAMILY(int Identifier, string Name, int NumberOfAttribute,int *AttributeIdentifier,int *AttributeValue,string AttributeDescription,int NumberOfGroup,string GroupName, int ** Number) : "<<Identifier);
30
31   _isOnAllElts = false ;
32   // replace them by pointerOf ?
33   _attributeIdentifier = new int[_numberOfAttribute] ;
34   memcpy(_attributeIdentifier,AttributeIdentifier,_numberOfAttribute*sizeof(int));
35   _attributeValue = new int[_numberOfAttribute] ;
36   memcpy(_attributeValue,AttributeValue,_numberOfAttribute*sizeof(int));
37
38   _attributeDescription=new string[_numberOfAttribute];
39   for (int i=0;i<NumberOfAttribute;i++) {
40     _attributeDescription[i].assign(AttributeDescription,i*MED_TAILLE_DESC,MED_TAILLE_DESC);
41     _attributeDescription[i].erase(strlen(_attributeDescription[i].c_str()));
42     //SCRUTE(_attributeDescription[i]);
43   }
44  
45   _groupName=new string[_numberOfGroup];
46   for (int i=0;i<NumberOfGroup;i++) {
47     _groupName[i].assign(GroupName,i*MED_TAILLE_LNOM,MED_TAILLE_LNOM);
48     _groupName[i].erase(strlen(_groupName[i].c_str()));
49     //SCRUTE(_groupName[i]);
50   }
51
52   // well, we must set SUPPORT attribut
53   _description = "FAMILY" ;
54   // on which geometric type :
55   bool Find = false ;
56   
57
58   // ************************ NODES RELATED PART *************************************************
59   // Scan every node family number <NodeFamilyNumber[i]> in order to create 
60   // the list <tmp_NodesList> of node numbers <i+1> belonging to the family <_identifier>
61   int NumberOfNodes         = _mesh->getNumberOfNodes();
62   int NumberOfNodesInFamily = 0 ;
63   //int * NodeFamilyNumber    = _mesh->getMEDArrayNodeFamily() ;   // EF : TEMPORAIRE !!
64   int * tmp_NodesList       = new int[NumberOfNodes] ;           // Un peu brutal...., oui mais ce n'est qu'un tableau de travail !
65   for (int i=0; i<NumberOfNodes; i++)
66     if ( _identifier == MEDArrayNodeFamily[i] ) {
67       tmp_NodesList[NumberOfNodesInFamily]=i+1 ;
68       NumberOfNodesInFamily++;
69     }
70   //SCRUTE(NumberOfNodesInFamily);
71
72   // If we found nodes set the family attributes adequatly
73   if (NumberOfNodesInFamily>0) {
74     
75     Find = true ;
76     
77     _entity = MED_NODE ;
78 //      _numberOfGeometricType = 1 ;
79 //      _geometricType = new medGeometryElement[1] ;
80 //      _geometricType[0]=MED_NONE ;
81     
82     // family on all NODE
83     if (NumberOfNodesInFamily==NumberOfNodes) {
84       _isOnAllElts = true ;
85       update();
86     } else {
87       _numberOfGeometricType = 1 ;
88       if (_geometricType!=NULL) delete[] _geometricType ;
89       _geometricType = new medGeometryElement[1] ;
90       _geometricType[0]=MED_NONE ;
91       _isOnAllElts= false ;
92       if (_numberOfElements!=NULL) delete[] _numberOfElements ;
93       _numberOfElements = new int[1] ;
94       _numberOfElements[0]=NumberOfNodesInFamily ;
95       _totalNumberOfElements=NumberOfNodesInFamily;
96       
97 //        _number=new MEDSKYLINEARRAY(1,NumberOfNodesInFamily) ;
98 //        int * NumberIndex = _number->getIndex();
99 //        int * NumberValue = _number->getValue();
100       int * NumberIndex = new int[2];
101       int * NumberValue = new int[NumberOfNodesInFamily];
102
103       NumberIndex[0]=1;                          //set the MEDSKYLINEARRAY Index table
104       NumberIndex[1]=1+NumberOfNodesInFamily;    //set the MEDSKYLINEARRAY Index table
105       for(int i=0; i<NumberOfNodesInFamily; i++) // OH LA LA... 
106         NumberValue[i]=tmp_NodesList[i] ;        // RESIZE de tmp_NodesList serait plus efficace !!!!!!!!
107       // PG : pas de problème, si ca marche. Il faudra déplacer le delete !
108       if(_number!=NULL) delete _number ;
109       _number=new MEDSKYLINEARRAY(1,NumberOfNodesInFamily,NumberIndex,NumberValue) ;
110       delete[] NumberIndex ;
111       delete[] NumberValue ;
112     }
113   }
114   delete[] tmp_NodesList ;
115   
116
117
118   // ************************ CELLS RELATED PART *************************************************
119   // If we previously found nodes in our family don't scan the CELLS because a
120   // family contains different geometic types of only one entity type.
121   // ?? Scan every cell family number <NodeFamilyNumber[i]> in order to create 
122   // ?? the list <tmp_NodesList> of node numbers <i+1> belonging to the family <_identifier>
123   if (!Find) {
124     Find = build(MED_CELL,MEDArrayCellFamily) ;
125   }
126   // on face ?
127   if (!Find) {
128     if ((_mesh->existConnectivity(MED_NODAL,MED_FACE))|(_mesh->existConnectivity(MED_DESCENDING,MED_FACE))) {
129       Find = build(MED_FACE,MEDArrayFaceFamily) ;
130     }
131   }
132   
133   // on edge ?
134   if (!Find) {
135     if ((_mesh->existConnectivity(MED_NODAL,MED_EDGE))|(_mesh->existConnectivity(MED_DESCENDING,MED_EDGE))) {
136       Find = build(MED_EDGE,MEDArrayEdgeFamily) ;
137     }
138   }
139   // That's all !
140
141   // if not find : no entity in familly !!!
142   if (!Find) {
143     _numberOfGeometricType = 0 ;
144     _isOnAllElts = false ;
145     MESSAGE ("FAMILY() : No entity found !") ;
146   } 
147   // already done by support !!!!
148 //    else { // set gauss point number to be equal one !
149 //      _numberOfGaussPoint = new int[_numberOfGeometricType] ;
150 //      for (int i=0; i<_numberOfGeometricType; i++)
151 //        _numberOfGaussPoint[i]=1 ;
152 //    }
153 };
154
155 FAMILY::FAMILY(const FAMILY & m):SUPPORT(m)
156 {
157   MESSAGE("FAMILY::FAMILY(FAMILY & m)");
158   _identifier = m._identifier;
159   _numberOfAttribute = m._numberOfAttribute;
160   if (m._attributeIdentifier != NULL)
161     {
162       _attributeIdentifier = new int[m._numberOfAttribute];
163       memcpy(_attributeIdentifier,m._attributeIdentifier,m._numberOfAttribute*sizeof(int));
164     }
165   else
166     _attributeIdentifier = (int *) NULL;
167   if (m._attributeValue != NULL)
168     {
169       _attributeValue = new int[m._numberOfAttribute];
170       memcpy(_attributeValue,m._attributeValue,m._numberOfAttribute*sizeof(int));
171     }
172   else
173     _attributeValue = (int *) NULL;
174   if (m._attributeDescription != NULL)
175     {
176       _attributeDescription = new string[m._numberOfAttribute];
177       for (int i=0;i<m._numberOfAttribute;i++)
178         _attributeDescription[i] = m._attributeDescription[i];
179     }
180   else
181     _attributeDescription = (string *) NULL;
182   _numberOfGroup = m._numberOfGroup;
183   if (m._groupName != NULL)
184     {
185       _groupName = new string[m._numberOfGroup];
186       for (int i=0;i<m._numberOfGroup;i++)
187         _groupName[i]=m._groupName[i];
188     }
189   else
190     _groupName = (string *) NULL;
191 };
192
193 FAMILY::FAMILY(const SUPPORT & s):SUPPORT(s)
194 {
195   MESSAGE("FAMILY::FAMILY(const SUPPORT & s)");
196
197   _identifier = 0;
198   _numberOfAttribute = 0;
199   _attributeIdentifier = (int*) NULL;
200   _attributeValue = (int*) NULL;
201   _attributeDescription = (string*) NULL;
202   _numberOfGroup = 0;
203   _groupName= (string*) NULL;
204 };
205
206 FAMILY::~FAMILY() 
207 {
208     MESSAGE("~FAMILY()");
209     if(_attributeIdentifier!=NULL)
210        delete[] _attributeIdentifier;
211     if(_attributeValue!=NULL)
212        delete[] _attributeValue;
213     if(_attributeDescription!=NULL)
214        delete[] _attributeDescription;
215     if(_groupName!=NULL)
216        delete[] _groupName;
217 };
218   
219 FAMILY & FAMILY::operator=(const FAMILY &fam) 
220 {
221     MESSAGE("FAMILY::operator=");
222     _identifier = fam._identifier;
223     _numberOfAttribute = fam._numberOfAttribute; 
224     _attributeIdentifier = fam._attributeIdentifier;
225     _attributeValue = fam._attributeValue;
226     _attributeDescription = fam._attributeDescription;
227     _numberOfGroup = fam._numberOfGroup;
228     _groupName = fam._groupName;
229     return *this;
230 };
231
232 ostream & operator<<(ostream &os, FAMILY &myFamily)
233 {
234   // how do cast without duplicate ?
235   os << (SUPPORT) myFamily;
236
237   os << "  - Identifier : "<<myFamily.getIdentifier()<<endl;
238   int numberofattributes = myFamily.getNumberOfAttributes();
239   os << "  - Attributes ("<<numberofattributes<<") :"<<endl;
240   for (int j=1;j<numberofattributes+1;j++)
241     os << "    * "<<myFamily.getAttributeIdentifier(j)<<" : "<<myFamily.getAttributeValue(j)<<", "<<myFamily.getAttributeDescription(j).c_str()<<endl;
242   int numberofgroups = myFamily.getNumberOfGroups();
243   os << "  - Groups ("<<numberofgroups<<") :"<<endl;
244   for (int j=1;j<numberofgroups+1;j++)
245     os << "    * "<<myFamily.getGroupName(j).c_str()<<endl ;
246
247   return os;
248 };
249
250 bool FAMILY::build(medEntityMesh Entity,int **FamilyNumber /* from MED file */)
251 {
252   MESSAGE("FAMILY::build(medEntityMesh Entity,int **FamilyNumber /* from MED file */)");
253   bool Find = false ;
254   // Get types information from <_mesh>
255   int    numberOfTypes             = _mesh->getNumberOfTypes(Entity) ;
256   const medGeometryElement * types       = _mesh->getTypes(Entity) ;
257   
258   int *  numberOfElementsInFamily         = new int[numberOfTypes] ;
259   int    numberOfElementTypesInFamily     = 0 ;
260   
261   medGeometryElement * tmp_Types  = new medGeometryElement[numberOfTypes];
262   int ** tmp_ElementsLists                = new int*[numberOfTypes] ;
263   //  int *  GeometricTypeNumber           = new int[numberOfTypes] ;
264   const int *  GlobalNumberingIndex          = _mesh->getGlobalNumberingIndex(Entity);
265   
266   // we search for all elements in this family
267   for (int TypeNumber=0; TypeNumber < numberOfTypes; TypeNumber++) {
268     
269     int NumberOfElements             = _mesh->getNumberOfElements(Entity,types[TypeNumber]) ;
270     int NumberOfElementsInThisFamily = 0 ;
271     int * ElementsOfThisFamilyNumber = FamilyNumber[TypeNumber];
272     int * tmp_ElementsList           = new int[NumberOfElements];
273       
274     for (int i=0; i<NumberOfElements; i++)
275       if (_identifier == ElementsOfThisFamilyNumber[i]) {
276         tmp_ElementsList[NumberOfElementsInThisFamily]=i+GlobalNumberingIndex[TypeNumber] ;
277         NumberOfElementsInThisFamily++;
278       }
279     
280     if (NumberOfElementsInThisFamily>0) {// we have found some elements
281       numberOfElementsInFamily[numberOfElementTypesInFamily]=NumberOfElementsInThisFamily;
282
283       //int * ElementsList = tmp_ElementsList.resize(NumberOfElementsInThisFamily);
284       int * ElementsList = new int[NumberOfElementsInThisFamily] ;
285       memcpy(ElementsList,tmp_ElementsList,sizeof(int)*NumberOfElementsInThisFamily); // RESIZE de tmp_NodesList serait plus efficace !!!!!!!!
286         
287       tmp_ElementsLists[numberOfElementTypesInFamily]=ElementsList ;
288       tmp_Types[numberOfElementTypesInFamily]=types[TypeNumber];
289       //      GeometricTypeNumber[numberOfElementTypesInFamily]=TypeNumber+1;
290       numberOfElementTypesInFamily++;
291       //      delete [] ElementsList;
292     }
293     delete[] tmp_ElementsList;
294   }
295   
296   // we define all attribut in SUPPORT :
297   if (numberOfElementTypesInFamily>0) { // we have found elements
298     Find = true ;
299     _entity = Entity ;
300     _numberOfGeometricType = numberOfElementTypesInFamily ;
301     if (_geometricType!=NULL) delete[] _geometricType ;
302     _geometricType = new medGeometryElement[numberOfElementTypesInFamily] ;
303     _isOnAllElts = false ;
304     if (_numberOfElements!=NULL) delete[] _numberOfElements ;
305     _numberOfElements = new int[numberOfElementTypesInFamily] ;
306     _totalNumberOfElements=0;
307
308     //_numberOfGaussPoint = new int[numberOfElementTypesInFamily] ;
309
310     //int * numberIndex = new int[numberOfElementTypesInFamily+1];
311     //numberIndex[0]=1;
312     for (int i=0; i<numberOfElementTypesInFamily; i++) {
313       _geometricType[i]=tmp_Types[i] ;
314       //numberIndex[i+1]=numberIndex[i]+numberOfElementsInFamily[i];
315       _numberOfElements[i]=numberOfElementsInFamily[i]; // plutot un resize !!
316       _totalNumberOfElements+=_numberOfElements[i];
317       //_numberOfGaussPoint[i]=1;
318     }
319     //    delete[] numberOfElementsInFamily;
320     //    delete[] tmp_Types;
321     //    delete[] GeometricTypeNumber;
322       
323     // family on all ELEMENT ?
324     if (_totalNumberOfElements == _mesh->getNumberOfElements(Entity,MED_ALL_ELEMENTS)) {
325       _isOnAllElts = true ;
326       // all others attributs are rights !
327       for (int i=0; i<_numberOfGeometricType; i++)
328         delete[] tmp_ElementsLists[i];
329     } else {
330       int *NumberValue = new int[_totalNumberOfElements];
331       int *NumberIndex = new int[_numberOfGeometricType+1];
332       NumberIndex[0]=1;
333       for (int i=0; i<_numberOfGeometricType; i++) {
334         NumberIndex[i+1]=NumberIndex[i]+_numberOfElements[i];
335         for (int j=NumberIndex[i]; j<NumberIndex[i+1]; j++)
336           NumberValue[j-1]=tmp_ElementsLists[i][j-NumberIndex[i]];
337         delete[] tmp_ElementsLists[i];
338       }
339       if(_number!=NULL) delete _number ;
340       _number = new MEDSKYLINEARRAY(_numberOfGeometricType,_totalNumberOfElements,NumberIndex,NumberValue);
341       delete[] NumberIndex ;
342       delete[] NumberValue ;
343     }
344   }
345   delete[] tmp_Types;
346   delete[] numberOfElementsInFamily;
347
348   delete[] tmp_ElementsLists;
349
350   return Find ;
351 }
352