File: XdmfSet.cxx

package info (click to toggle)
vtk6 6.1.0+dfsg2-6
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 165,164 kB
  • ctags: 226,428
  • sloc: cpp: 1,354,490; ansic: 730,748; python: 227,134; tcl: 48,285; xml: 8,290; yacc: 4,832; java: 3,827; perl: 3,108; lex: 1,809; sh: 1,437; asm: 471; makefile: 229
file content (417 lines) | stat: -rw-r--r-- 12,227 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
#include "XdmfSet.h"

#include "XdmfDataItem.h"
#include "XdmfDataDesc.h"
#include "XdmfArray.h"
#include "XdmfAttribute.h"
#include "XdmfDOM.h"
#include "XdmfMap.h"

XdmfSet::XdmfSet() {
  this->SetElementName("Set");
  this->IdsAreMine = 1;
  this->CellIdsAreMine = 1;
  this->FaceIdsAreMine = 1;
  this->SetType = XDMF_SET_TYPE_UNSET;
  this->Ids = NULL;
  this->CellIds = NULL;
  this->FaceIds = NULL;
  this->ShapeDesc = new XdmfDataDesc();
  this->Active = 0;
  this->Size = 0;
  this->Ghost = 0;
  this->Map = (XdmfMap **)calloc(1, sizeof(XdmfMap *));
  this->NumberOfMaps = 0;
  this->Attribute = (XdmfAttribute **)calloc(1, sizeof(XdmfAttribute *));
  this->NumberOfAttributes = 0;
  }

XdmfSet::~XdmfSet() {
    XdmfInt32 Index;

  if( this->IdsAreMine && this->Ids )  delete this->Ids;
  if( this->CellIdsAreMine && this->CellIds )  delete this->CellIds;
  if( this->FaceIdsAreMine && this->FaceIds )  delete this->FaceIds;
  for ( Index = 0; Index < this->NumberOfAttributes; Index ++ ){
      if (this->Attribute[Index]->GetDeleteOnGridDelete()){
          delete this->Attribute[Index];
      }
  }
  free(this->Attribute);
  for ( Index = 0; Index < this->NumberOfMaps; Index ++ ){
      if (this->Map[Index]->GetDeleteOnGridDelete()){
          delete this->Map[Index];
      }
  }
  free(this->Map);
  delete this->ShapeDesc;
  }

XdmfInt32
XdmfSet::Release(){
  if( this->IdsAreMine && this->Ids )  delete this->Ids;
  this->Ids = NULL;
  if( this->CellIdsAreMine && this->Ids )  delete this->CellIds;
  this->CellIds = NULL;
  if( this->FaceIdsAreMine && this->Ids )  delete this->FaceIds;
  this->FaceIds = NULL;
  return(XDMF_SUCCESS);
}

XdmfInt32
XdmfSet::Insert( XdmfElement *Child){
    if(Child && (
        XDMF_WORD_CMP(Child->GetElementName(), "Map") ||
        XDMF_WORD_CMP(Child->GetElementName(), "Attribute") ||
        XDMF_WORD_CMP(Child->GetElementName(), "DataItem") ||
        XDMF_WORD_CMP(Child->GetElementName(), "Information")
        )){
        XdmfInt32   status = XdmfElement::Insert(Child);
        if((status == XDMF_SUCCESS) && XDMF_WORD_CMP(Child->GetElementName(), "Map")){
            XdmfMap *ChildMap = (XdmfMap *)Child;
            this->NumberOfMaps++;
            this->Map = ( XdmfMap **)realloc( this->Map,
                this->NumberOfMaps * sizeof( XdmfMap * ));
            if(!this->Map) {
                XdmfErrorMessage("Realloc of Map List Failed");
                return(XDMF_FAIL);
            }
            this->Map[this->NumberOfMaps - 1] = ChildMap;
            }
        if((status == XDMF_SUCCESS) && XDMF_WORD_CMP(Child->GetElementName(), "Attribute")){
            XdmfAttribute *ChildAttribute = (XdmfAttribute *)Child;
            this->NumberOfAttributes++;
            this->Attribute = ( XdmfAttribute **)realloc( this->Attribute,
                this->NumberOfAttributes * sizeof( XdmfAttribute * ));
            if(!this->Attribute) {
                XdmfErrorMessage("Realloc of Attribute List Failed");
                return(XDMF_FAIL);
            }
            this->Attribute[this->NumberOfAttributes - 1] = ChildAttribute;
            }
    }else{
        XdmfErrorMessage("Set can only Insert Attribute, DataItem or Information elements");
    }
    return(XDMF_FAIL);
}

XdmfInt32
XdmfSet::Build(){
    if(XdmfElement::Build() != XDMF_SUCCESS) return(XDMF_FAIL);
    this->Set("SetType", this->GetSetTypeAsString());
    if(this->Ids){
        XdmfDataItem    *di = NULL;
        XdmfXmlNode     node;
        //! Is there already a DataItem
        node = this->DOM->FindDataElement(0, this->GetElement());
        if(node) {
            di = (XdmfDataItem *)this->GetCurrentXdmfElement(node);
        }
        if(!di){
            di = new XdmfDataItem;
            node = this->DOM->InsertNew(this->GetElement(), "DataItem");
            di->SetDOM(this->DOM);
            di->SetElement(node);
        }
        di->SetArray(this->Ids);
        if(this->Ids->GetNumberOfElements() > 100) di->SetFormat(XDMF_FORMAT_HDF);
        di->Build();
        this->SetCurrentXdmfElement(di->GetElement(), NULL);
        delete di;
    }
    return(XDMF_SUCCESS);
}

XdmfConstString
XdmfSet::GetSetTypeAsString( void ){
  switch ( this->SetType ){
    case XDMF_SET_TYPE_CELL :
      return( "Cell" );
    case XDMF_SET_TYPE_FACE :
      return( "Face" );
    case XDMF_SET_TYPE_EDGE :
      return( "Edge" );
    case XDMF_SET_TYPE_NODE :
      return( "Node" );
    case XDMF_SET_TYPE_UNSET :
      return( "Unset" );
    default :
      break;
    }
    XdmfErrorMessage("Unknown SetType = " << this->SetType);
    return(0);
}


XdmfInt32
XdmfSet::SetSetTypeFromString( XdmfConstString regionType ){
if( XDMF_WORD_CMP( regionType, "Cell" ) ) {
  this->SetType = XDMF_SET_TYPE_CELL;
} else if( XDMF_WORD_CMP( regionType, "Face" ) ) {
  this->SetType = XDMF_SET_TYPE_FACE;
} else if( XDMF_WORD_CMP( regionType, "Edge" ) ) {
  this->SetType = XDMF_SET_TYPE_EDGE;
} else if( XDMF_WORD_CMP( regionType, "Node" ) ) {
  this->SetType = XDMF_SET_TYPE_NODE;
} else {
  XdmfErrorMessage("Unknown Set Type " << regionType );
  return( XDMF_FAIL );
  }
return( XDMF_SUCCESS );
}

// Ids
XdmfInt32
XdmfSet::SetIds(XdmfArray *someIds){
    if(someIds == this->Ids) return(XDMF_SUCCESS);
    if(this->IdsAreMine && this->Ids) delete this->Ids;
    this->IdsAreMine = 0;
    this->Ids = someIds;
    return(XDMF_SUCCESS);
}

XdmfArray *
XdmfSet::GetIds(XdmfInt32 Create){
    if(!this->Ids && Create){
        this->Ids = new XdmfArray;
        this->IdsAreMine = 1;
    }
    return(this->Ids);
}

// CellIds
XdmfInt32
XdmfSet::SetCellIds(XdmfArray *someCellIds){
    if(someCellIds == this->CellIds) return(XDMF_SUCCESS);
    if(this->CellIdsAreMine && this->CellIds) delete this->CellIds;
    this->CellIdsAreMine = 0;
    this->CellIds = someCellIds;
    return(XDMF_SUCCESS);
}

XdmfArray *
XdmfSet::GetCellIds(XdmfInt32 Create){
    if(!this->CellIds && Create){
        this->CellIds = new XdmfArray;
        this->CellIdsAreMine = 1;
    }
    return(this->CellIds);
}

// FaceIds
XdmfInt32
XdmfSet::SetFaceIds(XdmfArray *someFaceIds){
    if(someFaceIds == this->FaceIds) return(XDMF_SUCCESS);
    if(this->FaceIdsAreMine && this->FaceIds) delete this->FaceIds;
    this->FaceIdsAreMine = 0;
    this->FaceIds = someFaceIds;
    return(XDMF_SUCCESS);
}

XdmfArray *
XdmfSet::GetFaceIds(XdmfInt32 Create){
    if(!this->FaceIds && Create){
        this->FaceIds = new XdmfArray;
        this->FaceIdsAreMine = 1;
    }
    return(this->FaceIds);
}

XdmfInt32
XdmfSet::UpdateInformation() {

XdmfConstString  Value;

if(XdmfElement::UpdateInformation() != XDMF_SUCCESS) return(XDMF_FAIL);
if( XDMF_WORD_CMP(this->GetElementType(), "Set") == 0){
    XdmfErrorMessage("Element type" << this->GetElementType() << " is not of type 'Set'");
    return(XDMF_FAIL);
}

Value = this->Get( "Active" );
this->Active = 0;
if ( Value ){
  if( XDMF_WORD_CMP( Value, "1" ) ) {
    this->Active = 1;
  }
}
free((void*)Value);
Value = this->Get( "Ghost" );
if(Value){
    this->SetGhost(atoi(Value));
    }
free((void*)Value);
Value = this->Get( "SetType" );
if( Value ){
  this->SetSetTypeFromString( Value );
} else {
  this->SetType = XDMF_SET_TYPE_NODE;
}

// Allow Size | Length | Dimensions
free((void*)Value);
Value = this->Get( "Size" );
if(!Value) Value = this->Get("Length");
if(!Value) Value = this->Get("Dimensions");
if( Value ){
  this->ShapeDesc->SetShapeFromString( Value );
  this->SetSize( this->ShapeDesc->GetNumberOfElements());
  free((void*)Value);
}else{
    XdmfXmlNode  IdsNode;
    IdsNode = this->DOM->FindDataElement( 0, Element );
    if(!IdsNode){
        XdmfErrorMessage("Dimensions of Set not set in XML and no DataItem found");
    }
    Value = this->DOM->Get( IdsNode, "Dimensions" );
    if(!Value){
        XdmfErrorMessage("Dimensions of Set not set in XML or DataItem");
        return(XDMF_FAIL);
    }else{
        this->ShapeDesc->SetShapeFromString( Value );
        free((void*)Value);
    }
    this->SetSize(this->ShapeDesc->GetNumberOfElements());
}
// Get Maps
XdmfInt32 OldNumberOfMaps = this->NumberOfMaps;
this->NumberOfMaps = this->DOM->FindNumberOfElements("Map", this->Element );
if( this->NumberOfMaps > 0 ){
  XdmfInt32  Index;
  XdmfMap  *iMap;
  XdmfXmlNode    MapElement;

  for ( Index = 0; Index < OldNumberOfMaps; Index ++ )
    {
    delete this->Map[Index];
    }
  this->Map = ( XdmfMap **)realloc( this->Map,
      this->NumberOfMaps * sizeof( XdmfMap * ));
  for( Index = 0 ; Index < this->NumberOfMaps ; Index++ ){
    iMap = new XdmfMap;

    this->Map[Index] = iMap;
    MapElement = this->DOM->FindElement( "Map", Index, this->Element );
    iMap->SetDOM( this->DOM );    
    iMap->SetElement( MapElement );
    iMap->UpdateInformation();
    }
}
// Get Attributes
XdmfInt32 OldNumberOfAttributes = this->NumberOfAttributes;
this->NumberOfAttributes = this->DOM->FindNumberOfElements("Attribute", this->Element );
if( this->NumberOfAttributes > 0 ){
  XdmfInt32  Index;
  XdmfAttribute  *iAttribute;
  XdmfXmlNode    AttributeElement;

  for ( Index = 0; Index < OldNumberOfAttributes; Index ++ )
    {
    delete this->Attribute[Index];
    }
  this->Attribute = ( XdmfAttribute **)realloc( this->Attribute,
      this->NumberOfAttributes * sizeof( XdmfAttribute * ));
  for( Index = 0 ; Index < this->NumberOfAttributes ; Index++ ){
    iAttribute = new XdmfAttribute;

    this->Attribute[Index] = iAttribute;
    AttributeElement = this->DOM->FindElement( "Attribute", Index, this->Element );
    iAttribute->SetDOM( this->DOM );    
    iAttribute->SetElement( AttributeElement );
    iAttribute->UpdateInformation();
    }
}
if(!this->Name) this->SetName(GetUnique("Set_"));
return( XDMF_SUCCESS );
}

XdmfInt32
XdmfSet::Update() {

XdmfInt32   Status;
XdmfInt32   NumberOfDataItems = 1;
XdmfInt32   i;

// check this out
if(XdmfElement::Update() != XDMF_SUCCESS) return(XDMF_FAIL);

if( this->SetType == XDMF_SET_TYPE_UNSET ){
  Status = this->UpdateInformation();
  if( Status == XDMF_FAIL ) {
    XdmfErrorMessage("Can't Initialize");
    return( XDMF_FAIL );
    }
  }

switch (this->SetType){
    case XDMF_SET_TYPE_FACE :
        NumberOfDataItems = 2;
        break;
    case XDMF_SET_TYPE_EDGE :
        NumberOfDataItems = 3;
        break;
    default :
        NumberOfDataItems = 1;
        break;
}

for(i=0 ; i < NumberOfDataItems ; i++){
    XdmfXmlNode IdsNode;
    XdmfInt32   *Mine;
    XdmfArray   **Array;

    switch (this->SetType){
        case XDMF_SET_TYPE_FACE :
            if(i == 0){
                Mine = &this->CellIdsAreMine;
                Array = &this->CellIds;
            }else{
                Mine = &this->IdsAreMine;
                Array = &this->Ids;
            }
            break;
        case XDMF_SET_TYPE_EDGE :
            if(i == 0){
                Mine = &this->CellIdsAreMine;
                Array = &this->CellIds;
            }else if(i == 1) {
                Mine = &this->FaceIdsAreMine;
                Array = &this->FaceIds;
            }else if(i == 2){
                Mine = &this->IdsAreMine;
                Array = &this->Ids;
            }
            break;
        default :
            Mine = &this->IdsAreMine;
            Array = &this->Ids;
            break;
    }
    IdsNode = this->DOM->FindDataElement(i, Element );
    if( IdsNode ){
        XdmfDataItem ValueReader;
        ValueReader.SetDOM( this->DOM );
        ValueReader.SetDsmBuffer(this->DsmBuffer);
        if(ValueReader.SetElement(IdsNode) == XDMF_FAIL) return(XDMF_FAIL);
        if(ValueReader.UpdateInformation() == XDMF_FAIL) return(XDMF_FAIL);
        if(ValueReader.Update() == XDMF_FAIL) return(XDMF_FAIL);
        if( *Mine && *Array){
            delete *Array;
            *Mine = 0;
        }
        // Steal the array
        *Array = ValueReader.GetArray();
        if( *Array == 0 ) {
            XdmfErrorMessage("Error Retrieving Data Ids");
            return( XDMF_FAIL );
        }
        ValueReader.SetArrayIsMine(0);
        *Mine = 1;
    } else {
        XdmfErrorMessage("Set does not have enough DataItems. Error reading DataItem #" << i);
        return( XDMF_FAIL );
    }
}

return( XDMF_SUCCESS );
}