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