1 #include "XdmfSet.h"
2 
3 #include "XdmfDataItem.h"
4 #include "XdmfDataDesc.h"
5 #include "XdmfArray.h"
6 #include "XdmfAttribute.h"
7 #include "XdmfDOM.h"
8 #include "XdmfMap.h"
9 
10 namespace xdmf2
11 {
12 
XdmfSet()13 XdmfSet::XdmfSet() {
14   this->SetElementName("Set");
15   this->IdsAreMine = 1;
16   this->CellIdsAreMine = 1;
17   this->FaceIdsAreMine = 1;
18   this->SetType = XDMF_SET_TYPE_UNSET;
19   this->Ids = NULL;
20   this->CellIds = NULL;
21   this->FaceIds = NULL;
22   this->ShapeDesc = new XdmfDataDesc();
23   this->Active = 0;
24   this->Size = 0;
25   this->Ghost = 0;
26   this->Map = (XdmfMap **)calloc(1, sizeof(XdmfMap *));
27   this->NumberOfMaps = 0;
28   this->Attribute = (XdmfAttribute **)calloc(1, sizeof(XdmfAttribute *));
29   this->NumberOfAttributes = 0;
30   }
31 
~XdmfSet()32 XdmfSet::~XdmfSet() {
33     XdmfInt32 Index;
34 
35   if( this->IdsAreMine && this->Ids )  delete this->Ids;
36   if( this->CellIdsAreMine && this->CellIds )  delete this->CellIds;
37   if( this->FaceIdsAreMine && this->FaceIds )  delete this->FaceIds;
38   for ( Index = 0; Index < this->NumberOfAttributes; Index ++ ){
39       if (this->Attribute[Index]->GetDeleteOnGridDelete()){
40           delete this->Attribute[Index];
41       }
42   }
43   free(this->Attribute);
44   for ( Index = 0; Index < this->NumberOfMaps; Index ++ ){
45       if (this->Map[Index]->GetDeleteOnGridDelete()){
46           delete this->Map[Index];
47       }
48   }
49   free(this->Map);
50   delete this->ShapeDesc;
51   }
52 
53 XdmfInt32
Release()54 XdmfSet::Release(){
55   if( this->IdsAreMine && this->Ids )  delete this->Ids;
56   this->Ids = NULL;
57   if( this->CellIdsAreMine && this->Ids )  delete this->CellIds;
58   this->CellIds = NULL;
59   if( this->FaceIdsAreMine && this->Ids )  delete this->FaceIds;
60   this->FaceIds = NULL;
61   return(XDMF_SUCCESS);
62 }
63 
64 XdmfInt32
Insert(XdmfElement * Child)65 XdmfSet::Insert( XdmfElement *Child){
66     if(Child && (
67         XDMF_WORD_CMP(Child->GetElementName(), "Map") ||
68         XDMF_WORD_CMP(Child->GetElementName(), "Attribute") ||
69         XDMF_WORD_CMP(Child->GetElementName(), "DataItem") ||
70         XDMF_WORD_CMP(Child->GetElementName(), "Information")
71         )){
72         XdmfInt32   status = XdmfElement::Insert(Child);
73         if((status == XDMF_SUCCESS) && XDMF_WORD_CMP(Child->GetElementName(), "Map")){
74             XdmfMap *ChildMap = (XdmfMap *)Child;
75             this->NumberOfMaps++;
76             this->Map = ( XdmfMap **)realloc( this->Map,
77                 this->NumberOfMaps * sizeof( XdmfMap * ));
78             if(!this->Map) {
79                 XdmfErrorMessage("Realloc of Map List Failed");
80                 return(XDMF_FAIL);
81             }
82             this->Map[this->NumberOfMaps - 1] = ChildMap;
83             }
84         if((status == XDMF_SUCCESS) && XDMF_WORD_CMP(Child->GetElementName(), "Attribute")){
85             XdmfAttribute *ChildAttribute = (XdmfAttribute *)Child;
86             this->NumberOfAttributes++;
87             this->Attribute = ( XdmfAttribute **)realloc( this->Attribute,
88                 this->NumberOfAttributes * sizeof( XdmfAttribute * ));
89             if(!this->Attribute) {
90                 XdmfErrorMessage("Realloc of Attribute List Failed");
91                 return(XDMF_FAIL);
92             }
93             this->Attribute[this->NumberOfAttributes - 1] = ChildAttribute;
94             }
95     }else{
96         XdmfErrorMessage("Set can only Insert Attribute, DataItem or Information elements");
97     }
98     return(XDMF_FAIL);
99 }
100 
101 XdmfInt32
Build()102 XdmfSet::Build(){
103     if(XdmfElement::Build() != XDMF_SUCCESS) return(XDMF_FAIL);
104     this->Set("SetType", this->GetSetTypeAsString());
105     if(this->Ids){
106         XdmfDataItem    *di = NULL;
107         XdmfXmlNode     node;
108         //! Is there already a DataItem
109         node = this->DOM->FindDataElement(0, this->GetElement());
110         if(node) {
111             di = (XdmfDataItem *)this->GetCurrentXdmfElement(node);
112         }
113         if(!di){
114             di = new XdmfDataItem;
115             node = this->DOM->InsertNew(this->GetElement(), "DataItem");
116             di->SetDOM(this->DOM);
117             di->SetElement(node);
118         }
119         di->SetArray(this->Ids);
120         if(this->Ids->GetNumberOfElements() > 100) di->SetFormat(XDMF_FORMAT_HDF);
121         di->Build();
122         this->SetCurrentXdmfElement(di->GetElement(), NULL);
123         delete di;
124     }
125     return(XDMF_SUCCESS);
126 }
127 
128 XdmfConstString
GetSetTypeAsString(void)129 XdmfSet::GetSetTypeAsString( void ){
130   switch ( this->SetType ){
131     case XDMF_SET_TYPE_CELL :
132       return( "Cell" );
133     case XDMF_SET_TYPE_FACE :
134       return( "Face" );
135     case XDMF_SET_TYPE_EDGE :
136       return( "Edge" );
137     case XDMF_SET_TYPE_NODE :
138       return( "Node" );
139     case XDMF_SET_TYPE_UNSET :
140       return( "Unset" );
141     default :
142       break;
143     }
144     XdmfErrorMessage("Unknown SetType = " << this->SetType);
145     return(0);
146 }
147 
148 
149 XdmfInt32
SetSetTypeFromString(XdmfConstString regionType)150 XdmfSet::SetSetTypeFromString( XdmfConstString regionType ){
151 if( XDMF_WORD_CMP( regionType, "Cell" ) ) {
152   this->SetType = XDMF_SET_TYPE_CELL;
153 } else if( XDMF_WORD_CMP( regionType, "Face" ) ) {
154   this->SetType = XDMF_SET_TYPE_FACE;
155 } else if( XDMF_WORD_CMP( regionType, "Edge" ) ) {
156   this->SetType = XDMF_SET_TYPE_EDGE;
157 } else if( XDMF_WORD_CMP( regionType, "Node" ) ) {
158   this->SetType = XDMF_SET_TYPE_NODE;
159 } else {
160   XdmfErrorMessage("Unknown Set Type " << regionType );
161   return( XDMF_FAIL );
162   }
163 return( XDMF_SUCCESS );
164 }
165 
166 // Ids
167 XdmfInt32
SetIds(XdmfArray * someIds)168 XdmfSet::SetIds(XdmfArray *someIds){
169     if(someIds == this->Ids) return(XDMF_SUCCESS);
170     if(this->IdsAreMine && this->Ids) delete this->Ids;
171     this->IdsAreMine = 0;
172     this->Ids = someIds;
173     return(XDMF_SUCCESS);
174 }
175 
176 XdmfArray *
GetIds(XdmfInt32 Create)177 XdmfSet::GetIds(XdmfInt32 Create){
178     if(!this->Ids && Create){
179         this->Ids = new XdmfArray;
180         this->IdsAreMine = 1;
181     }
182     return(this->Ids);
183 }
184 
185 // CellIds
186 XdmfInt32
SetCellIds(XdmfArray * someCellIds)187 XdmfSet::SetCellIds(XdmfArray *someCellIds){
188     if(someCellIds == this->CellIds) return(XDMF_SUCCESS);
189     if(this->CellIdsAreMine && this->CellIds) delete this->CellIds;
190     this->CellIdsAreMine = 0;
191     this->CellIds = someCellIds;
192     return(XDMF_SUCCESS);
193 }
194 
195 XdmfArray *
GetCellIds(XdmfInt32 Create)196 XdmfSet::GetCellIds(XdmfInt32 Create){
197     if(!this->CellIds && Create){
198         this->CellIds = new XdmfArray;
199         this->CellIdsAreMine = 1;
200     }
201     return(this->CellIds);
202 }
203 
204 // FaceIds
205 XdmfInt32
SetFaceIds(XdmfArray * someFaceIds)206 XdmfSet::SetFaceIds(XdmfArray *someFaceIds){
207     if(someFaceIds == this->FaceIds) return(XDMF_SUCCESS);
208     if(this->FaceIdsAreMine && this->FaceIds) delete this->FaceIds;
209     this->FaceIdsAreMine = 0;
210     this->FaceIds = someFaceIds;
211     return(XDMF_SUCCESS);
212 }
213 
214 XdmfArray *
GetFaceIds(XdmfInt32 Create)215 XdmfSet::GetFaceIds(XdmfInt32 Create){
216     if(!this->FaceIds && Create){
217         this->FaceIds = new XdmfArray;
218         this->FaceIdsAreMine = 1;
219     }
220     return(this->FaceIds);
221 }
222 
223 XdmfInt32
UpdateInformation()224 XdmfSet::UpdateInformation() {
225 
226 XdmfConstString  Value;
227 
228 if(XdmfElement::UpdateInformation() != XDMF_SUCCESS) return(XDMF_FAIL);
229 if( XDMF_WORD_CMP(this->GetElementType(), "Set") == 0){
230     XdmfErrorMessage("Element type" << this->GetElementType() << " is not of type 'Set'");
231     return(XDMF_FAIL);
232 }
233 
234 Value = this->Get( "Active" );
235 this->Active = 0;
236 if ( Value ){
237   if( XDMF_WORD_CMP( Value, "1" ) ) {
238     this->Active = 1;
239   }
240 }
241 free((void*)Value);
242 Value = this->Get( "Ghost" );
243 if(Value){
244     this->SetGhost(atoi(Value));
245     }
246 free((void*)Value);
247 Value = this->Get( "SetType" );
248 if( Value ){
249   this->SetSetTypeFromString( Value );
250 } else {
251   this->SetType = XDMF_SET_TYPE_NODE;
252 }
253 
254 // Allow Size | Length | Dimensions
255 free((void*)Value);
256 Value = this->Get( "Size" );
257 if(!Value) Value = this->Get("Length");
258 if(!Value) Value = this->Get("Dimensions");
259 if( Value ){
260   this->ShapeDesc->SetShapeFromString( Value );
261   this->SetSize( this->ShapeDesc->GetNumberOfElements());
262   free((void*)Value);
263 }else{
264     XdmfXmlNode  IdsNode;
265     IdsNode = this->DOM->FindDataElement( 0, Element );
266     if(!IdsNode){
267         XdmfErrorMessage("Dimensions of Set not set in XML and no DataItem found");
268     }
269     Value = this->DOM->Get( IdsNode, "Dimensions" );
270     if(!Value){
271         XdmfErrorMessage("Dimensions of Set not set in XML or DataItem");
272         return(XDMF_FAIL);
273     }else{
274         this->ShapeDesc->SetShapeFromString( Value );
275         free((void*)Value);
276     }
277     this->SetSize(this->ShapeDesc->GetNumberOfElements());
278 }
279 // Get Maps
280 XdmfInt32 OldNumberOfMaps = this->NumberOfMaps;
281 this->NumberOfMaps = this->DOM->FindNumberOfElements("Map", this->Element );
282 if( this->NumberOfMaps > 0 ){
283   XdmfInt32  Index;
284   XdmfMap  *iMap;
285   XdmfXmlNode    MapElement;
286 
287   for ( Index = 0; Index < OldNumberOfMaps; Index ++ )
288     {
289     delete this->Map[Index];
290     }
291   this->Map = ( XdmfMap **)realloc( this->Map,
292       this->NumberOfMaps * sizeof( XdmfMap * ));
293   for( Index = 0 ; Index < this->NumberOfMaps ; Index++ ){
294     iMap = new XdmfMap;
295 
296     this->Map[Index] = iMap;
297     MapElement = this->DOM->FindElement( "Map", Index, this->Element );
298     iMap->SetDOM( this->DOM );
299     iMap->SetElement( MapElement );
300     iMap->UpdateInformation();
301     }
302 }
303 // Get Attributes
304 XdmfInt32 OldNumberOfAttributes = this->NumberOfAttributes;
305 this->NumberOfAttributes = this->DOM->FindNumberOfElements("Attribute", this->Element );
306 if( this->NumberOfAttributes > 0 ){
307   XdmfInt32  Index;
308   XdmfAttribute  *iAttribute;
309   XdmfXmlNode    AttributeElement;
310 
311   for ( Index = 0; Index < OldNumberOfAttributes; Index ++ )
312     {
313     delete this->Attribute[Index];
314     }
315   this->Attribute = ( XdmfAttribute **)realloc( this->Attribute,
316       this->NumberOfAttributes * sizeof( XdmfAttribute * ));
317   for( Index = 0 ; Index < this->NumberOfAttributes ; Index++ ){
318     iAttribute = new XdmfAttribute;
319 
320     this->Attribute[Index] = iAttribute;
321     AttributeElement = this->DOM->FindElement( "Attribute", Index, this->Element );
322     iAttribute->SetDOM( this->DOM );
323     iAttribute->SetElement( AttributeElement );
324     iAttribute->UpdateInformation();
325     }
326 }
327 if(!this->Name) this->SetName(GetUnique("Set_"));
328 return( XDMF_SUCCESS );
329 }
330 
331 XdmfInt32
Update()332 XdmfSet::Update() {
333 
334 XdmfInt32   Status;
335 XdmfInt32   NumberOfDataItems = 1;
336 XdmfInt32   i;
337 
338 // check this out
339 if(XdmfElement::Update() != XDMF_SUCCESS) return(XDMF_FAIL);
340 
341 if( this->SetType == XDMF_SET_TYPE_UNSET ){
342   Status = this->UpdateInformation();
343   if( Status == XDMF_FAIL ) {
344     XdmfErrorMessage("Can't Initialize");
345     return( XDMF_FAIL );
346     }
347   }
348 
349 switch (this->SetType){
350     case XDMF_SET_TYPE_FACE :
351         NumberOfDataItems = 2;
352         break;
353     case XDMF_SET_TYPE_EDGE :
354         NumberOfDataItems = 3;
355         break;
356     default :
357         NumberOfDataItems = 1;
358         break;
359 }
360 
361 for(i=0 ; i < NumberOfDataItems ; i++){
362     XdmfXmlNode IdsNode;
363     XdmfInt32   *Mine;
364     XdmfArray   **Array;
365 
366     switch (this->SetType){
367         case XDMF_SET_TYPE_FACE :
368             if(i == 0){
369                 Mine = &this->CellIdsAreMine;
370                 Array = &this->CellIds;
371             }else{
372                 Mine = &this->IdsAreMine;
373                 Array = &this->Ids;
374             }
375             break;
376         case XDMF_SET_TYPE_EDGE :
377             if(i == 0){
378                 Mine = &this->CellIdsAreMine;
379                 Array = &this->CellIds;
380             }else if(i == 1) {
381                 Mine = &this->FaceIdsAreMine;
382                 Array = &this->FaceIds;
383             }else if(i == 2){
384                 Mine = &this->IdsAreMine;
385                 Array = &this->Ids;
386             }
387             break;
388         default :
389             Mine = &this->IdsAreMine;
390             Array = &this->Ids;
391             break;
392     }
393     IdsNode = this->DOM->FindDataElement(i, Element );
394     if( IdsNode ){
395         XdmfDataItem ValueReader;
396         ValueReader.SetDOM( this->DOM );
397         ValueReader.SetDsmBuffer(this->DsmBuffer);
398         if(ValueReader.SetElement(IdsNode) == XDMF_FAIL) return(XDMF_FAIL);
399         if(ValueReader.UpdateInformation() == XDMF_FAIL) return(XDMF_FAIL);
400         if(ValueReader.Update() == XDMF_FAIL) return(XDMF_FAIL);
401         if( *Mine && *Array){
402             delete *Array;
403             *Mine = 0;
404         }
405         // Steal the array
406         *Array = ValueReader.GetArray();
407         if( *Array == 0 ) {
408             XdmfErrorMessage("Error Retrieving Data Ids");
409             return( XDMF_FAIL );
410         }
411         ValueReader.SetArrayIsMine(0);
412         *Mine = 1;
413     } else {
414         XdmfErrorMessage("Set does not have enough DataItems. Error reading DataItem #" << i);
415         return( XDMF_FAIL );
416     }
417 }
418 
419 return( XDMF_SUCCESS );
420 }
421 
422 }
423